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

ExportThread.cpp 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "SnapmaticPicture.h"
  19. #include "ProfileInterface.h"
  20. #include "PictureExport.h"
  21. #include "ProfileWidget.h"
  22. #include "ExportThread.h"
  23. #include "SavegameData.h"
  24. #include "config.h"
  25. #include <QStringBuilder>
  26. #include <QDesktopWidget>
  27. #include <QApplication>
  28. #include <QFileInfo>
  29. #include <QDebug>
  30. #include <QFile>
  31. ExportThread::ExportThread(QMap<ProfileWidget*,QString> profileMap, QString exportDirectory, bool pictureCopyEnabled, bool pictureExportEnabled, int exportCount, QObject *parent) : QThread(parent),
  32. profileMap(profileMap), exportDirectory(exportDirectory), pictureCopyEnabled(pictureCopyEnabled), pictureExportEnabled(pictureExportEnabled), exportCount(exportCount)
  33. {
  34. }
  35. void ExportThread::run()
  36. {
  37. QSettings settings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  38. // Picture Settings
  39. // Quality Settings
  40. settings.beginGroup("Pictures");
  41. int defaultQuality = 100;
  42. QSize defExportSize = QSize(960, 536);
  43. int customQuality = settings.value("CustomQuality", defaultQuality).toInt();
  44. if (customQuality < 1 || customQuality > 100)
  45. {
  46. customQuality = 100;
  47. }
  48. bool useCustomQuality = settings.value("CustomQualityEnabled", false).toBool();
  49. // Size Settings
  50. QSize cusExportSize = settings.value("CustomSize", defExportSize).toSize();
  51. if (cusExportSize.width() > 3840)
  52. {
  53. cusExportSize.setWidth(3840);
  54. }
  55. else if (cusExportSize.height() > 2160)
  56. {
  57. cusExportSize.setHeight(2160);
  58. }
  59. if (cusExportSize.width() < 1)
  60. {
  61. cusExportSize.setWidth(1);
  62. }
  63. else if (cusExportSize.height() < 1)
  64. {
  65. cusExportSize.setHeight(1);
  66. }
  67. QString sizeMode = settings.value("ExportSizeMode", "Default").toString();
  68. Qt::AspectRatioMode aspectRatio = (Qt::AspectRatioMode)settings.value("AspectRatio", Qt::KeepAspectRatio).toInt();
  69. settings.endGroup();
  70. // End Picture Settings
  71. int intExportProgress = 0;
  72. for (ProfileWidget *widget : profileMap.keys())
  73. {
  74. if (widget->isSelected())
  75. {
  76. if (widget->getWidgetType() == "SnapmaticWidget")
  77. {
  78. SnapmaticWidget *picWidget = qobject_cast<SnapmaticWidget*>(widget);
  79. SnapmaticPicture *picture = picWidget->getPicture();
  80. if (pictureExportEnabled)
  81. {
  82. QString exportFileName = PictureExport::getPictureFileName(picture);
  83. if (exportFileName.right(4) != ".jpg" && exportFileName.right(4) != ".png")
  84. {
  85. exportFileName += ".jpg";
  86. }
  87. intExportProgress++;
  88. emit exportStringUpdate(ProfileInterface::tr("Export file %1 of %2 files").arg(QString::number(intExportProgress), QString::number(exportCount)));
  89. emit exportProgressUpdate(intExportProgress);
  90. // Scale Picture
  91. QImage exportPicture = picture->getImage();
  92. if (sizeMode == "Desktop")
  93. {
  94. QRect desktopResolution = qApp->desktop()->screenGeometry();
  95. exportPicture = exportPicture.scaled(desktopResolution.width(), desktopResolution.height(), aspectRatio, Qt::SmoothTransformation);
  96. }
  97. else if (sizeMode == "Custom")
  98. {
  99. exportPicture = exportPicture.scaled(cusExportSize, aspectRatio, Qt::SmoothTransformation);
  100. }
  101. bool isSaved;
  102. if (useCustomQuality)
  103. {
  104. isSaved = exportPicture.save(exportDirectory % "/" % exportFileName, "JPEG", customQuality);
  105. }
  106. else
  107. {
  108. isSaved = exportPicture.save(exportDirectory % "/" % exportFileName, "JPEG", 100);
  109. }
  110. if (!isSaved)
  111. {
  112. failedExportPictures += exportFileName;
  113. }
  114. }
  115. if (pictureCopyEnabled)
  116. {
  117. QString exportFileName = PictureExport::getPictureFileName(picture);
  118. if (exportFileName.right(4) != ".g5e")
  119. {
  120. exportFileName += ".g5e";
  121. }
  122. intExportProgress++;
  123. emit exportStringUpdate(ProfileInterface::tr("Export file %1 of %2 files").arg(QString::number(intExportProgress), QString::number(exportCount)));
  124. emit exportProgressUpdate(intExportProgress);
  125. QString exportFilePath = exportDirectory % "/" % exportFileName;
  126. if (QFile::exists(exportFilePath)) {QFile::remove(exportFilePath);}
  127. if (!picture->exportPicture(exportDirectory % "/" % exportFileName, SnapmaticFormat::G5E_Format))
  128. {
  129. failedCopyPictures += exportFileName;
  130. }
  131. }
  132. }
  133. else if (widget->getWidgetType() == "SavegameWidget")
  134. {
  135. SavegameWidget *sgdWidget = qobject_cast<SavegameWidget*>(widget);
  136. SavegameData *savegame = sgdWidget->getSavegame();
  137. QString originalFileName = savegame->getSavegameFileName();
  138. QFileInfo originalFileInfo(originalFileName);
  139. QString exportFileName = originalFileInfo.fileName();
  140. intExportProgress++;
  141. emit exportStringUpdate(ProfileInterface::tr("Export file %1 of %2 files").arg(QString::number(intExportProgress), QString::number(exportCount)));
  142. emit exportProgressUpdate(intExportProgress);
  143. QString exportFilePath = exportDirectory % "/" % exportFileName;
  144. if (QFile::exists(exportFilePath)) {QFile::remove(exportFilePath);}
  145. if (!QFile::copy(originalFileName, exportFilePath))
  146. {
  147. failedSavegames += exportFileName;
  148. }
  149. }
  150. }
  151. }
  152. emit exportFinished();
  153. }
  154. QStringList ExportThread::getFailedCopyPictures()
  155. {
  156. return failedCopyPictures;
  157. }
  158. QStringList ExportThread::getFailedExportPictures()
  159. {
  160. return failedExportPictures;
  161. }
  162. QStringList ExportThread::getFailedSavegames()
  163. {
  164. return failedSavegames;
  165. }