Open Source Snapmatic picture and Savegame viewer/editor for GTA V https://gta5view.syping.de/

PictureWidget.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*****************************************************************************
  2. * gta5view Grand Theft Auto V Profile Viewer
  3. * Copyright (C) 2016-2017 Syping
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *****************************************************************************/
  18. #include "PictureDialog.h"
  19. #include "PictureWidget.h"
  20. #include "UiModLabel.h"
  21. #include <QDesktopWidget>
  22. #include <QApplication>
  23. #include <QHBoxLayout>
  24. #include <QKeyEvent>
  25. #include <QPixmap>
  26. #include <QEvent>
  27. #include <QDebug>
  28. PictureWidget::PictureWidget(QWidget *parent) : QDialog(parent)
  29. {
  30. installEventFilter(this);
  31. widgetLayout = new QHBoxLayout(this);
  32. widgetLayout->setSpacing(0);
  33. widgetLayout->setContentsMargins(0, 0, 0, 0);
  34. pictureLabel = new UiModLabel(this);
  35. pictureLabel->setObjectName("pictureLabel");
  36. pictureLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  37. pictureLabel->setContextMenuPolicy(Qt::CustomContextMenu);
  38. pictureLabel->setAlignment(Qt::AlignCenter);
  39. widgetLayout->addWidget(pictureLabel);
  40. QObject::connect(pictureLabel, SIGNAL(mouseDoubleClicked(Qt::MouseButton)), this, SLOT(pictureDoubleClicked(Qt::MouseButton)));
  41. QObject::connect(pictureLabel, SIGNAL(customContextMenuRequested(QPoint)), parent, SLOT(exportCustomContextMenuRequested(QPoint)));
  42. QObject::connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(updateWindowSize(int)));
  43. setLayout(widgetLayout);
  44. }
  45. PictureWidget::~PictureWidget()
  46. {
  47. widgetLayout->removeWidget(pictureLabel);
  48. delete pictureLabel;
  49. delete widgetLayout;
  50. }
  51. bool PictureWidget::eventFilter(QObject *obj, QEvent *ev)
  52. {
  53. if (obj == this)
  54. {
  55. if (ev->type() == QEvent::KeyPress)
  56. {
  57. QKeyEvent *keyEvent = (QKeyEvent*)ev;
  58. switch (keyEvent->key()){
  59. case Qt::Key_Left:
  60. emit previousPictureRequested();
  61. break;
  62. case Qt::Key_Right:
  63. emit nextPictureRequested();
  64. break;
  65. }
  66. }
  67. }
  68. return false;
  69. }
  70. void PictureWidget::pictureDoubleClicked(Qt::MouseButton button)
  71. {
  72. if (button == Qt::LeftButton)
  73. {
  74. close();
  75. }
  76. }
  77. void PictureWidget::setImage(QImage image_, QRect rec)
  78. {
  79. image = image_;
  80. pictureLabel->setPixmap(QPixmap::fromImage(image.scaled(rec.width(), rec.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation)));
  81. }
  82. void PictureWidget::setImage(QImage image_)
  83. {
  84. image = image_;
  85. pictureLabel->setPixmap(QPixmap::fromImage(image.scaled(geometry().width(), geometry().height(), Qt::KeepAspectRatio, Qt::SmoothTransformation)));
  86. }
  87. void PictureWidget::updateWindowSize(int screenID)
  88. {
  89. if (screenID == QApplication::desktop()->screenNumber(this))
  90. {
  91. QRect desktopRect = QApplication::desktop()->screenGeometry(this);
  92. this->move(desktopRect.x(), desktopRect.y());
  93. this->resize(desktopRect.width(), desktopRect.height());
  94. this->showFullScreen();
  95. pictureLabel->setPixmap(QPixmap::fromImage(image.scaled(desktopRect.width(), desktopRect.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation)));
  96. }
  97. }