Open Source Snapmatic picture and Savegame viewer/editor for GTA V https://gta5view.syping.de/
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ImageEditorDialog.cpp 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*****************************************************************************
  2. * gta5view Grand Theft Auto V Profile Viewer
  3. * Copyright (C) 2017-2018 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 "ImageEditorDialog.h"
  19. #include "ui_ImageEditorDialog.h"
  20. #include "ProfileInterface.h"
  21. #include "SidebarGenerator.h"
  22. #include "StandardPaths.h"
  23. #include "ImportDialog.h"
  24. #include "AppEnv.h"
  25. #include "config.h"
  26. #include <QStringBuilder>
  27. #include <QImageReader>
  28. #include <QFileDialog>
  29. #include <QMessageBox>
  30. ImageEditorDialog::ImageEditorDialog(SnapmaticPicture *picture, QString profileName, QWidget *parent) :
  31. QDialog(parent), smpic(picture), profileName(profileName),
  32. ui(new Ui::ImageEditorDialog)
  33. {
  34. // Set Window Flags
  35. setWindowFlags(windowFlags()^Qt::WindowContextHelpButtonHint);
  36. ui->setupUi(this);
  37. ui->cmdClose->setDefault(true);
  38. ui->cmdClose->setFocus();
  39. // Set Icon for Close Button
  40. if (QIcon::hasThemeIcon("dialog-close"))
  41. {
  42. ui->cmdClose->setIcon(QIcon::fromTheme("dialog-close"));
  43. }
  44. else if (QIcon::hasThemeIcon("gtk-close"))
  45. {
  46. ui->cmdClose->setIcon(QIcon::fromTheme("gtk-close"));
  47. }
  48. // Set Icon for Import Button
  49. if (QIcon::hasThemeIcon("document-import"))
  50. {
  51. ui->cmdReplace->setIcon(QIcon::fromTheme("document-import"));
  52. }
  53. // Set Icon for Overwrite Button
  54. if (QIcon::hasThemeIcon("document-save"))
  55. {
  56. ui->cmdSave->setIcon(QIcon::fromTheme("document-save"));
  57. }
  58. else if (QIcon::hasThemeIcon("gtk-save"))
  59. {
  60. ui->cmdSave->setIcon(QIcon::fromTheme("gtk-save"));
  61. }
  62. // DPI calculation
  63. qreal screenRatio = AppEnv::screenRatio();
  64. snapmaticResolutionLW = 516 * screenRatio; // 430
  65. snapmaticResolutionLH = 288 * screenRatio; // 240
  66. ui->labPicture->setMinimumSize(snapmaticResolutionLW, snapmaticResolutionLH);
  67. ui->labCapacity->setText(tr("Capacity: %1").arg(QString::number(qRound((double)picture->getContentMaxLength() / 1024)) % " KB"));
  68. imageIsChanged = false;
  69. pictureCache = picture->getImage();
  70. ui->labPicture->setPixmap(QPixmap::fromImage(pictureCache).scaled(snapmaticResolutionLW, snapmaticResolutionLH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
  71. setMaximumSize(sizeHint());
  72. setMinimumSize(sizeHint());
  73. setFixedSize(sizeHint());
  74. }
  75. ImageEditorDialog::~ImageEditorDialog()
  76. {
  77. delete ui;
  78. }
  79. void ImageEditorDialog::on_cmdClose_clicked()
  80. {
  81. close();
  82. }
  83. void ImageEditorDialog::on_cmdReplace_clicked()
  84. {
  85. QSettings settings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  86. settings.beginGroup("FileDialogs");
  87. bool dontUseNativeDialog = settings.value("DontUseNativeDialog", false).toBool();
  88. settings.beginGroup("ImportReplace");
  89. fileDialogPreOpen: //Work?
  90. QFileDialog fileDialog(this);
  91. fileDialog.setFileMode(QFileDialog::ExistingFile);
  92. fileDialog.setViewMode(QFileDialog::Detail);
  93. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  94. fileDialog.setOption(QFileDialog::DontUseNativeDialog, dontUseNativeDialog);
  95. fileDialog.setWindowFlags(fileDialog.windowFlags()^Qt::WindowContextHelpButtonHint);
  96. fileDialog.setWindowTitle(ProfileInterface::tr("Import..."));
  97. fileDialog.setLabelText(QFileDialog::Accept, ProfileInterface::tr("Import"));
  98. // Getting readable Image formats
  99. QString imageFormatsStr = " ";
  100. for (QByteArray imageFormat : QImageReader::supportedImageFormats())
  101. {
  102. imageFormatsStr += QString("*.") % QString::fromUtf8(imageFormat).toLower() % " ";
  103. }
  104. QStringList filters;
  105. filters << ProfileInterface::tr("All image files (%1)").arg(imageFormatsStr.trimmed());
  106. filters << ProfileInterface::tr("All files (**)");
  107. fileDialog.setNameFilters(filters);
  108. QList<QUrl> sidebarUrls = SidebarGenerator::generateSidebarUrls(fileDialog.sidebarUrls());
  109. fileDialog.setSidebarUrls(sidebarUrls);
  110. fileDialog.setDirectory(settings.value(profileName % "+Directory", StandardPaths::documentsLocation()).toString());
  111. fileDialog.restoreGeometry(settings.value(profileName % "+Geometry", "").toByteArray());
  112. if (fileDialog.exec())
  113. {
  114. QStringList selectedFiles = fileDialog.selectedFiles();
  115. if (selectedFiles.length() == 1)
  116. {
  117. QString selectedFile = selectedFiles.at(0);
  118. QString selectedFileName = QFileInfo(selectedFile).fileName();
  119. QFile snapmaticFile(selectedFile);
  120. if (!snapmaticFile.open(QFile::ReadOnly))
  121. {
  122. QMessageBox::warning(this, ProfileInterface::tr("Import"), ProfileInterface::tr("Can't import %1 because file can't be open").arg("\""+selectedFileName+"\""));
  123. goto fileDialogPreOpen;
  124. }
  125. QImage *importImage = new QImage();
  126. QImageReader snapmaticImageReader;
  127. snapmaticImageReader.setDecideFormatFromContent(true);
  128. snapmaticImageReader.setDevice(&snapmaticFile);
  129. if (!snapmaticImageReader.read(importImage))
  130. {
  131. QMessageBox::warning(this, ProfileInterface::tr("Import"), ProfileInterface::tr("Can't import %1 because file can't be parsed properly").arg("\""+selectedFileName+"\""));
  132. delete importImage;
  133. goto fileDialogPreOpen;
  134. }
  135. ImportDialog *importDialog = new ImportDialog(this);
  136. importDialog->setImage(importImage);
  137. importDialog->setModal(true);
  138. importDialog->show();
  139. importDialog->exec();
  140. if (importDialog->isImportAgreed())
  141. {
  142. pictureCache = importDialog->image();
  143. ui->labPicture->setPixmap(QPixmap::fromImage(pictureCache).scaled(snapmaticResolutionLW, snapmaticResolutionLH, Qt::KeepAspectRatio, Qt::SmoothTransformation));
  144. imageIsChanged = true;
  145. }
  146. delete importDialog;
  147. }
  148. }
  149. settings.setValue(profileName % "+Geometry", fileDialog.saveGeometry());
  150. settings.setValue(profileName % "+Directory", fileDialog.directory().absolutePath());
  151. settings.endGroup();
  152. settings.endGroup();
  153. }
  154. void ImageEditorDialog::on_cmdSave_clicked()
  155. {
  156. if (imageIsChanged)
  157. {
  158. const QByteArray previousPicture = smpic->getPictureStream();
  159. bool success = smpic->setImage(pictureCache);
  160. if (success)
  161. {
  162. QString currentFilePath = smpic->getPictureFilePath();
  163. QString originalFilePath = smpic->getOriginalPictureFilePath();
  164. QString backupFileName = originalFilePath % ".bak";
  165. if (!QFile::exists(backupFileName))
  166. {
  167. QFile::copy(currentFilePath, backupFileName);
  168. }
  169. if (!smpic->exportPicture(currentFilePath))
  170. {
  171. smpic->setPictureStream(previousPicture);
  172. QMessageBox::warning(this, tr("Snapmatic Image Editor"), tr("Patching of Snapmatic Image failed because of I/O Error"));
  173. return;
  174. }
  175. smpic->emitCustomSignal("PictureUpdated");
  176. }
  177. else
  178. {
  179. QMessageBox::warning(this, tr("Snapmatic Image Editor"), tr("Patching of Snapmatic Image failed because of Image Error"));
  180. return;
  181. }
  182. }
  183. close();
  184. }
  185. void ImageEditorDialog::on_cmdQuestion_clicked()
  186. {
  187. QMessageBox::information(this, tr("Snapmatic Image Editor"), tr("Every taken Snapmatic have a different Capacity, a Snapmatic with higher Capacity can store a picture with better quality."));
  188. }