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

ImportDialog.cpp 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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 "ImportDialog.h"
  19. #include "ui_ImportDialog.h"
  20. #include "SidebarGenerator.h"
  21. #include "StandardPaths.h"
  22. #include "imagecropper.h"
  23. #include "AppEnv.h"
  24. #include "config.h"
  25. #include <QStringBuilder>
  26. #include <QInputDialog>
  27. #include <QImageReader>
  28. #include <QColorDialog>
  29. #include <QFileDialog>
  30. #include <QMessageBox>
  31. #include <QSettings>
  32. #include <QPainter>
  33. #include <QPixmap>
  34. #include <QImage>
  35. #include <QDebug>
  36. #include <QStyle>
  37. #include <QFile>
  38. #include <QRgb>
  39. // IMAGES VALUES
  40. #define snapmaticResolutionW 960
  41. #define snapmaticResolutionH 536
  42. #define snapmaticAvatarResolution 470
  43. #define snapmaticAvatarPlacementW 145
  44. #define snapmaticAvatarPlacementH 66
  45. ImportDialog::ImportDialog(QString profileName, QWidget *parent) :
  46. QDialog(parent), profileName(profileName),
  47. ui(new Ui::ImportDialog)
  48. {
  49. // Set Window Flags
  50. setWindowFlags(windowFlags()^Qt::WindowContextHelpButtonHint);
  51. ui->setupUi(this);
  52. ui->cmdOK->setDefault(true);
  53. ui->cmdOK->setFocus();
  54. importAgreed = false;
  55. settingsLocked = false;
  56. watermarkAvatar = true;
  57. watermarkPicture = false;
  58. insideAvatarZone = false;
  59. avatarAreaImage = QImage(":/img/avatarareaimport.png");
  60. selectedColour = QColor::fromRgb(0, 0, 0, 255);
  61. // Set Icon for OK Button
  62. if (QIcon::hasThemeIcon("dialog-ok"))
  63. {
  64. ui->cmdOK->setIcon(QIcon::fromTheme("dialog-ok"));
  65. }
  66. else if (QIcon::hasThemeIcon("gtk-ok"))
  67. {
  68. ui->cmdOK->setIcon(QIcon::fromTheme("gtk-ok"));
  69. }
  70. // Set Icon for Cancel Button
  71. if (QIcon::hasThemeIcon("dialog-cancel"))
  72. {
  73. ui->cmdCancel->setIcon(QIcon::fromTheme("dialog-cancel"));
  74. }
  75. else if (QIcon::hasThemeIcon("gtk-cancel"))
  76. {
  77. ui->cmdCancel->setIcon(QIcon::fromTheme("gtk-cancel"));
  78. }
  79. ui->cbIgnore->setChecked(false);
  80. ui->labColour->setText(tr("Background Colour: <span style=\"color: %1\">%1</span>").arg(selectedColour.name()));
  81. ui->labBackgroundImage->setText(tr("Background Image:"));
  82. ui->cmdBackgroundWipe->setVisible(false);
  83. // Set Import Settings
  84. QSettings settings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  85. settings.beginGroup("Import");
  86. QString currentProfile = settings.value("Profile", "Default").toString();
  87. settings.endGroup();
  88. processSettings(currentProfile);
  89. // DPI calculation
  90. qreal screenRatio = AppEnv::screenRatio();
  91. snapmaticResolutionLW = 516 * screenRatio; // 430
  92. snapmaticResolutionLH = 288 * screenRatio; // 240
  93. ui->labPicture->setMinimumSize(snapmaticResolutionLW, snapmaticResolutionLH);
  94. ui->vlButtom->setSpacing(6 * screenRatio);
  95. #ifndef Q_OS_MAC
  96. ui->vlButtom->setContentsMargins(9 * screenRatio, 6 * screenRatio, 9 * screenRatio, 9 * screenRatio);
  97. #else
  98. if (QApplication::style()->objectName() == "macintosh")
  99. {
  100. ui->vlButtom->setContentsMargins(9 * screenRatio, 9 * screenRatio, 9 * screenRatio, 9 * screenRatio);
  101. }
  102. else
  103. {
  104. ui->vlButtom->setContentsMargins(9 * screenRatio, 6 * screenRatio, 9 * screenRatio, 9 * screenRatio);
  105. }
  106. #endif
  107. // Options menu
  108. optionsMenu = new QMenu(this);
  109. optionsMenu->addAction(tr("&Import new Picture..."), this, SLOT(importNewPicture()));
  110. optionsMenu->addAction(tr("&Crop Picture..."), this, SLOT(cropPicture()));
  111. optionsMenu->addSeparator();
  112. optionsMenu->addAction(tr("&Load Settings..."), this, SLOT(loadImportSettings()));
  113. optionsMenu->addAction(tr("&Save Settings..."), this, SLOT(saveImportSettings()));
  114. ui->cmdOptions->setMenu(optionsMenu);
  115. setMaximumSize(sizeHint());
  116. setMinimumSize(sizeHint());
  117. setFixedSize(sizeHint());
  118. }
  119. ImportDialog::~ImportDialog()
  120. {
  121. delete optionsMenu;
  122. delete ui;
  123. }
  124. void ImportDialog::processImage()
  125. {
  126. if (workImage.isNull()) return;
  127. QImage snapmaticImage = workImage;
  128. QPixmap snapmaticPixmap(snapmaticResolutionW, snapmaticResolutionH);
  129. snapmaticPixmap.fill(selectedColour);
  130. QPainter snapmaticPainter(&snapmaticPixmap);
  131. if (!backImage.isNull())
  132. {
  133. if (!ui->cbStretch->isChecked())
  134. {
  135. int diffWidth = 0;
  136. int diffHeight = 0;
  137. if (backImage.width() != snapmaticResolutionW)
  138. {
  139. diffWidth = snapmaticResolutionW - backImage.width();
  140. diffWidth = diffWidth / 2;
  141. }
  142. else if (backImage.height() != snapmaticResolutionH)
  143. {
  144. diffHeight = snapmaticResolutionH - backImage.height();
  145. diffHeight = diffHeight / 2;
  146. }
  147. snapmaticPainter.drawImage(0 + diffWidth, 0 + diffHeight, backImage);
  148. }
  149. else
  150. {
  151. snapmaticPainter.drawImage(0, 0, QImage(backImage).scaled(snapmaticResolutionW, snapmaticResolutionH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
  152. }
  153. if (ui->cbAvatar->isChecked() && ui->cbForceAvatarColour->isChecked())
  154. {
  155. snapmaticPainter.fillRect(snapmaticAvatarPlacementW, snapmaticAvatarPlacementH, snapmaticAvatarResolution, snapmaticAvatarResolution, selectedColour);
  156. }
  157. }
  158. if (insideAvatarZone)
  159. {
  160. // Avatar mode
  161. int diffWidth = 0;
  162. int diffHeight = 0;
  163. if (!ui->cbIgnore->isChecked())
  164. {
  165. snapmaticImage = snapmaticImage.scaled(snapmaticAvatarResolution, snapmaticAvatarResolution, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  166. if (snapmaticImage.width() > snapmaticImage.height())
  167. {
  168. diffHeight = snapmaticAvatarResolution - snapmaticImage.height();
  169. diffHeight = diffHeight / 2;
  170. }
  171. else if (snapmaticImage.width() < snapmaticImage.height())
  172. {
  173. diffWidth = snapmaticAvatarResolution - snapmaticImage.width();
  174. diffWidth = diffWidth / 2;
  175. }
  176. }
  177. else
  178. {
  179. snapmaticImage = snapmaticImage.scaled(snapmaticAvatarResolution, snapmaticAvatarResolution, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  180. }
  181. snapmaticPainter.drawImage(snapmaticAvatarPlacementW + diffWidth, snapmaticAvatarPlacementH + diffHeight, snapmaticImage);
  182. if (ui->cbWatermark->isChecked()) { processWatermark(&snapmaticPainter); }
  183. imageTitle = tr("Custom Avatar", "Custom Avatar Description in SC, don't use Special Character!");
  184. }
  185. else
  186. {
  187. // Picture mode
  188. int diffWidth = 0;
  189. int diffHeight = 0;
  190. if (!ui->cbIgnore->isChecked())
  191. {
  192. snapmaticImage = snapmaticImage.scaled(snapmaticResolutionW, snapmaticResolutionH, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  193. if (snapmaticImage.width() != snapmaticResolutionW)
  194. {
  195. diffWidth = snapmaticResolutionW - snapmaticImage.width();
  196. diffWidth = diffWidth / 2;
  197. }
  198. else if (snapmaticImage.height() != snapmaticResolutionH)
  199. {
  200. diffHeight = snapmaticResolutionH - snapmaticImage.height();
  201. diffHeight = diffHeight / 2;
  202. }
  203. }
  204. else
  205. {
  206. snapmaticImage = snapmaticImage.scaled(snapmaticResolutionW, snapmaticResolutionH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  207. }
  208. snapmaticPainter.drawImage(0 + diffWidth, 0 + diffHeight, snapmaticImage);
  209. if (ui->cbWatermark->isChecked()) { processWatermark(&snapmaticPainter); }
  210. imageTitle = tr("Custom Picture", "Custom Picture Description in SC, don't use Special Character!");
  211. }
  212. snapmaticPainter.end();
  213. newImage = snapmaticPixmap.toImage();
  214. ui->labPicture->setPixmap(snapmaticPixmap.scaled(snapmaticResolutionLW, snapmaticResolutionLH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
  215. }
  216. void ImportDialog::processWatermark(QPainter *snapmaticPainter)
  217. {
  218. bool blackWatermark = false;
  219. bool redWatermark = false;
  220. if (selectedColour.red() > 127)
  221. {
  222. if (selectedColour.green() > 127 || selectedColour.blue() > 127)
  223. {
  224. redWatermark = true;
  225. }
  226. }
  227. else
  228. {
  229. redWatermark = true;
  230. }
  231. if (selectedColour.lightness() > 127)
  232. {
  233. blackWatermark = true;
  234. }
  235. // draw watermark
  236. if (redWatermark)
  237. {
  238. snapmaticPainter->drawImage(0, 0, QImage(":/img/watermark_2r.png"));
  239. }
  240. else
  241. {
  242. QImage viewWatermark = QImage(":/img/watermark_2b.png");
  243. if (!blackWatermark)
  244. {
  245. viewWatermark.invertPixels(QImage::InvertRgb);
  246. }
  247. snapmaticPainter->drawImage(0, 0, viewWatermark);
  248. }
  249. QImage textWatermark = QImage(":/img/watermark_1b.png");
  250. if (!blackWatermark)
  251. {
  252. textWatermark.invertPixels(QImage::InvertRgb);
  253. }
  254. snapmaticPainter->drawImage(0, 0, textWatermark);
  255. }
  256. void ImportDialog::processSettings(QString settingsProfile, bool setDefault)
  257. {
  258. QSettings settings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  259. settings.beginGroup("Import");
  260. if (setDefault)
  261. {
  262. settings.setValue("Profile", settingsProfile);
  263. }
  264. if (settingsProfile == "Default")
  265. {
  266. watermarkAvatar = true;
  267. watermarkPicture = false;
  268. selectedColour = QColor::fromRgb(0, 0, 0, 255);
  269. backImage = QImage();
  270. ui->cbStretch->setChecked(false);
  271. ui->cbForceAvatarColour->setChecked(false);
  272. }
  273. else
  274. {
  275. settings.beginGroup(settingsProfile);
  276. watermarkAvatar = settings.value("WatermarkAvatar", true).toBool();
  277. watermarkPicture = settings.value("WatermarkPicture", false).toBool();
  278. backImage = qvariant_cast<QImage>(settings.value("BackgroundImage", QImage()));
  279. selectedColour = qvariant_cast<QColor>(settings.value("SelectedColour", QColor::fromRgb(0, 0, 0, 255)));
  280. ui->cbStretch->setChecked(settings.value("BackgroundStretch", false).toBool());
  281. ui->cbForceAvatarColour->setChecked(settings.value("ForceAvatarColour", false).toBool());
  282. settings.endGroup();
  283. }
  284. if (!workImage.isNull())
  285. {
  286. if (ui->cbAvatar->isChecked())
  287. {
  288. ui->cbWatermark->setChecked(watermarkAvatar);
  289. }
  290. else
  291. {
  292. ui->cbWatermark->setChecked(watermarkPicture);
  293. }
  294. }
  295. ui->labColour->setText(tr("Background Colour: <span style=\"color: %1\">%1</span>").arg(selectedColour.name()));
  296. if (!backImage.isNull())
  297. {
  298. ui->labBackgroundImage->setText(tr("Background Image: %1").arg(tr("Storage", "Background Image: Storage")));
  299. ui->cmdBackgroundWipe->setVisible(true);
  300. }
  301. else
  302. {
  303. ui->labBackgroundImage->setText(tr("Background Image:"));
  304. ui->cmdBackgroundWipe->setVisible(false);
  305. }
  306. settings.endGroup();
  307. }
  308. void ImportDialog::saveSettings(QString settingsProfile)
  309. {
  310. QSettings settings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  311. settings.beginGroup("Import");
  312. settings.beginGroup(settingsProfile);
  313. settings.setValue("WatermarkAvatar", watermarkAvatar);
  314. settings.setValue("WatermarkPicture", watermarkPicture);
  315. settings.setValue("BackgroundImage", backImage);
  316. settings.setValue("SelectedColour", selectedColour);
  317. settings.setValue("BackgroundStretch", ui->cbStretch->isChecked());
  318. settings.setValue("ForceAvatarColour", ui->cbForceAvatarColour->isChecked());
  319. settings.endGroup();
  320. settings.setValue("Profile", settingsProfile);
  321. settings.endGroup();
  322. }
  323. void ImportDialog::cropPicture()
  324. {
  325. qreal screenRatio = AppEnv::screenRatio();
  326. QDialog cropDialog(this);
  327. #if QT_VERSION >= 0x050000
  328. cropDialog.setObjectName(QStringLiteral("CropDialog"));
  329. #else
  330. cropDialog.setObjectName(QString::fromUtf8("CropDialog"));
  331. #endif
  332. cropDialog.setWindowTitle(tr("Crop Picture..."));
  333. cropDialog.setWindowFlags(cropDialog.windowFlags()^Qt::WindowContextHelpButtonHint);
  334. cropDialog.setModal(true);
  335. QVBoxLayout cropLayout;
  336. #if QT_VERSION >= 0x050000
  337. cropLayout.setObjectName(QStringLiteral("CropLayout"));
  338. #else
  339. cropLayout.setObjectName(QString::fromUtf8("CropLayout"));
  340. #endif
  341. cropLayout.setContentsMargins(0, 0, 0, 0);
  342. cropLayout.setSpacing(0);
  343. cropDialog.setLayout(&cropLayout);
  344. ImageCropper imageCropper(&cropDialog);
  345. #if QT_VERSION >= 0x050000
  346. imageCropper.setObjectName(QStringLiteral("ImageCropper"));
  347. #else
  348. imageCropper.setObjectName(QString::fromUtf8("ImageCropper"));
  349. #endif
  350. imageCropper.setBackgroundColor(Qt::black);
  351. imageCropper.setCroppingRectBorderColor(QColor(255, 255, 255, 127));
  352. imageCropper.setImage(QPixmap::fromImage(workImage, Qt::AutoColor));
  353. imageCropper.setProportion(QSize(1, 1));
  354. imageCropper.setFixedSize(workImage.size());
  355. cropLayout.addWidget(&imageCropper);
  356. QHBoxLayout buttonLayout;
  357. #if QT_VERSION >= 0x050000
  358. cropLayout.setObjectName(QStringLiteral("ButtonLayout"));
  359. #else
  360. cropLayout.setObjectName(QString::fromUtf8("ButtonLayout"));
  361. #endif
  362. cropLayout.addLayout(&buttonLayout);
  363. QPushButton cropButton(&cropDialog);
  364. #if QT_VERSION >= 0x050000
  365. cropButton.setObjectName(QStringLiteral("CropButton"));
  366. #else
  367. cropButton.setObjectName(QString::fromUtf8("CropButton"));
  368. #endif
  369. cropButton.setMinimumSize(0, 40 * screenRatio);
  370. cropButton.setText(tr("&Crop"));
  371. cropButton.setToolTip(tr("Crop Picture"));
  372. QObject::connect(&cropButton, SIGNAL(clicked(bool)), &cropDialog, SLOT(accept()));
  373. buttonLayout.addWidget(&cropButton);
  374. cropDialog.show();
  375. cropDialog.setFixedSize(cropDialog.sizeHint());
  376. if (cropDialog.exec() == QDialog::Accepted)
  377. {
  378. QImage *croppedImage = new QImage(imageCropper.cropImage().toImage());
  379. setImage(croppedImage);
  380. }
  381. }
  382. void ImportDialog::importNewPicture()
  383. {
  384. QSettings settings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  385. settings.beginGroup("FileDialogs");
  386. bool dontUseNativeDialog = settings.value("DontUseNativeDialog", false).toBool();
  387. settings.beginGroup("ImportCopy");
  388. fileDialogPreOpen: //Work?
  389. QFileDialog fileDialog(this);
  390. fileDialog.setFileMode(QFileDialog::ExistingFile);
  391. fileDialog.setViewMode(QFileDialog::Detail);
  392. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  393. fileDialog.setOption(QFileDialog::DontUseNativeDialog, dontUseNativeDialog);
  394. fileDialog.setWindowFlags(fileDialog.windowFlags()^Qt::WindowContextHelpButtonHint);
  395. fileDialog.setWindowTitle(QApplication::translate("ProfileInterface", "Import..."));
  396. fileDialog.setLabelText(QFileDialog::Accept, QApplication::translate("ProfileInterface", "Import"));
  397. // Getting readable Image formats
  398. QString imageFormatsStr = " ";
  399. for (QByteArray imageFormat : QImageReader::supportedImageFormats())
  400. {
  401. imageFormatsStr += QString("*.") % QString::fromUtf8(imageFormat).toLower() % " ";
  402. }
  403. QStringList filters;
  404. filters << QApplication::translate("ProfileInterface", "All image files (%1)").arg(imageFormatsStr.trimmed());
  405. filters << QApplication::translate("ProfileInterface", "All files (**)");
  406. fileDialog.setNameFilters(filters);
  407. QList<QUrl> sidebarUrls = SidebarGenerator::generateSidebarUrls(fileDialog.sidebarUrls());
  408. fileDialog.setSidebarUrls(sidebarUrls);
  409. fileDialog.setDirectory(settings.value(profileName % "+Directory", StandardPaths::documentsLocation()).toString());
  410. fileDialog.restoreGeometry(settings.value(profileName % "+Geometry", "").toByteArray());
  411. if (fileDialog.exec())
  412. {
  413. QStringList selectedFiles = fileDialog.selectedFiles();
  414. if (selectedFiles.length() == 1)
  415. {
  416. QString selectedFile = selectedFiles.at(0);
  417. QString selectedFileName = QFileInfo(selectedFile).fileName();
  418. QFile snapmaticFile(selectedFile);
  419. if (!snapmaticFile.open(QFile::ReadOnly))
  420. {
  421. QMessageBox::warning(this, QApplication::translate("ProfileInterface", "Import"), QApplication::translate("ProfileInterface", "Can't import %1 because file can't be open").arg("\""+selectedFileName+"\""));
  422. goto fileDialogPreOpen;
  423. }
  424. QImage *importImage = new QImage();
  425. QImageReader snapmaticImageReader;
  426. snapmaticImageReader.setDecideFormatFromContent(true);
  427. snapmaticImageReader.setDevice(&snapmaticFile);
  428. if (!snapmaticImageReader.read(importImage))
  429. {
  430. QMessageBox::warning(this, QApplication::translate("ProfileInterface", "Import"), QApplication::translate("ProfileInterface", "Can't import %1 because file can't be parsed properly").arg("\""+selectedFileName+"\""));
  431. delete importImage;
  432. goto fileDialogPreOpen;
  433. }
  434. setImage(importImage);
  435. }
  436. }
  437. settings.setValue(profileName % "+Geometry", fileDialog.saveGeometry());
  438. settings.setValue(profileName % "+Directory", fileDialog.directory().absolutePath());
  439. settings.endGroup();
  440. settings.endGroup();
  441. }
  442. void ImportDialog::loadImportSettings()
  443. {
  444. if (settingsLocked)
  445. {
  446. QMessageBox::information(this, tr("Load Settings..."), tr("Please import a new picture first"));
  447. return;
  448. }
  449. bool ok;
  450. QStringList profileList;
  451. profileList << tr("Default", "Default as Default Profile")
  452. << tr("Profile %1", "Profile %1 as Profile 1").arg("1")
  453. << tr("Profile %1", "Profile %1 as Profile 1").arg("2")
  454. << tr("Profile %1", "Profile %1 as Profile 1").arg("3")
  455. << tr("Profile %1", "Profile %1 as Profile 1").arg("4")
  456. << tr("Profile %1", "Profile %1 as Profile 1").arg("5");
  457. QString sProfile = QInputDialog::getItem(this, tr("Load Settings..."), tr("Please select your settings profile"), profileList, 0, false, &ok, windowFlags());
  458. if (ok)
  459. {
  460. QString pProfile;
  461. if (sProfile == tr("Default", "Default as Default Profile"))
  462. {
  463. pProfile = "Default";
  464. }
  465. else if (sProfile == tr("Profile %1", "Profile %1 as Profile 1").arg("1"))
  466. {
  467. pProfile = "Profile 1";
  468. }
  469. else if (sProfile == tr("Profile %1", "Profile %1 as Profile 1").arg("2"))
  470. {
  471. pProfile = "Profile 2";
  472. }
  473. else if (sProfile == tr("Profile %1", "Profile %1 as Profile 1").arg("3"))
  474. {
  475. pProfile = "Profile 3";
  476. }
  477. else if (sProfile == tr("Profile %1", "Profile %1 as Profile 1").arg("4"))
  478. {
  479. pProfile = "Profile 4";
  480. }
  481. else if (sProfile == tr("Profile %1", "Profile %1 as Profile 1").arg("5"))
  482. {
  483. pProfile = "Profile 5";
  484. }
  485. processSettings(pProfile, true);
  486. processImage();
  487. }
  488. }
  489. void ImportDialog::saveImportSettings()
  490. {
  491. if (settingsLocked)
  492. {
  493. QMessageBox::information(this, tr("Save Settings..."), tr("Please import a new picture first"));
  494. return;
  495. }
  496. bool ok;
  497. QStringList profileList;
  498. profileList << tr("Profile %1", "Profile %1 as Profile 1").arg("1")
  499. << tr("Profile %1", "Profile %1 as Profile 1").arg("2")
  500. << tr("Profile %1", "Profile %1 as Profile 1").arg("3")
  501. << tr("Profile %1", "Profile %1 as Profile 1").arg("4")
  502. << tr("Profile %1", "Profile %1 as Profile 1").arg("5");
  503. QString sProfile = QInputDialog::getItem(this, tr("Save Settings..."), tr("Please select your settings profile"), profileList, 0, false, &ok, windowFlags());
  504. if (ok)
  505. {
  506. QString pProfile;
  507. if (sProfile == tr("Profile %1", "Profile %1 as Profile 1").arg("1"))
  508. {
  509. pProfile = "Profile 1";
  510. }
  511. else if (sProfile == tr("Profile %1", "Profile %1 as Profile 1").arg("2"))
  512. {
  513. pProfile = "Profile 2";
  514. }
  515. else if (sProfile == tr("Profile %1", "Profile %1 as Profile 1").arg("3"))
  516. {
  517. pProfile = "Profile 3";
  518. }
  519. else if (sProfile == tr("Profile %1", "Profile %1 as Profile 1").arg("4"))
  520. {
  521. pProfile = "Profile 4";
  522. }
  523. else if (sProfile == tr("Profile %1", "Profile %1 as Profile 1").arg("5"))
  524. {
  525. pProfile = "Profile 5";
  526. }
  527. saveSettings(pProfile);
  528. }
  529. }
  530. QImage ImportDialog::image()
  531. {
  532. return newImage;
  533. }
  534. void ImportDialog::setImage(QImage *image_)
  535. {
  536. workImage = QImage();
  537. if (image_->width() == image_->height())
  538. {
  539. insideAvatarZone = true;
  540. ui->cbAvatar->setChecked(true);
  541. if (image_->height() > snapmaticResolutionH)
  542. {
  543. workImage = image_->scaled(snapmaticResolutionH, snapmaticResolutionH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  544. delete image_;
  545. }
  546. else
  547. {
  548. workImage = *image_;
  549. delete image_;
  550. }
  551. }
  552. else if (image_->width() > snapmaticResolutionW && image_->width() > image_->height())
  553. {
  554. insideAvatarZone = false;
  555. ui->cbAvatar->setChecked(false);
  556. workImage = image_->scaledToWidth(snapmaticResolutionW, Qt::SmoothTransformation);
  557. delete image_;
  558. }
  559. else if (image_->height() > snapmaticResolutionH && image_->height() > image_->width())
  560. {
  561. insideAvatarZone = false;
  562. ui->cbAvatar->setChecked(false);
  563. workImage = image_->scaledToHeight(snapmaticResolutionH, Qt::SmoothTransformation);
  564. delete image_;
  565. }
  566. else
  567. {
  568. insideAvatarZone = false;
  569. ui->cbAvatar->setChecked(false);
  570. workImage = *image_;
  571. delete image_;
  572. }
  573. processImage();
  574. lockSettings(false);
  575. }
  576. void ImportDialog::lockSettings(bool lock)
  577. {
  578. ui->cbAvatar->setDisabled(lock);
  579. ui->cbForceAvatarColour->setDisabled(lock);
  580. ui->cbIgnore->setDisabled(lock);
  581. ui->cbStretch->setDisabled(lock);
  582. ui->cbWatermark->setDisabled(lock);
  583. ui->cmdBackgroundChange->setDisabled(lock);
  584. ui->cmdBackgroundWipe->setDisabled(lock);
  585. ui->cmdColourChange->setDisabled(lock);
  586. ui->labBackgroundImage->setDisabled(lock);
  587. ui->labColour->setDisabled(lock);
  588. ui->gbSettings->setDisabled(lock);
  589. ui->gbBackground->setDisabled(lock);
  590. ui->cmdOK->setDisabled(lock);
  591. settingsLocked = lock;
  592. }
  593. void ImportDialog::enableOverwriteMode()
  594. {
  595. setWindowTitle(QApplication::translate("ImageEditorDialog", "Overwrite Image..."));
  596. ui->cmdOK->setText(QApplication::translate("ImageEditorDialog", "&Overwrite"));
  597. ui->cmdOK->setToolTip(QApplication::translate("ImageEditorDialog", "Apply changes"));
  598. ui->cmdCancel->setText(QApplication::translate("ImageEditorDialog", "&Close"));
  599. ui->cmdCancel->setToolTip(QApplication::translate("ImageEditorDialog", "Discard changes"));
  600. ui->cmdCancel->setDefault(true);
  601. ui->cmdCancel->setFocus();
  602. lockSettings(true);
  603. }
  604. bool ImportDialog::isImportAgreed()
  605. {
  606. return importAgreed;
  607. }
  608. bool ImportDialog::areSettingsLocked()
  609. {
  610. return settingsLocked;
  611. }
  612. QString ImportDialog::getImageTitle()
  613. {
  614. return imageTitle;
  615. }
  616. void ImportDialog::on_cbIgnore_toggled(bool checked)
  617. {
  618. Q_UNUSED(checked)
  619. processImage();
  620. }
  621. void ImportDialog::on_cbAvatar_toggled(bool checked)
  622. {
  623. if (!workImage.isNull() && workImage.width() == workImage.height() && !checked)
  624. {
  625. if (QMessageBox::No == QMessageBox::warning(this, tr("Snapmatic Avatar Zone"), tr("Are you sure to use a square image outside of the Avatar Zone?\nWhen you want to use it as Avatar the image will be detached!"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No))
  626. {
  627. ui->cbAvatar->setChecked(true);
  628. insideAvatarZone = true;
  629. return;
  630. }
  631. }
  632. insideAvatarZone = ui->cbAvatar->isChecked();
  633. watermarkBlock = true;
  634. if (insideAvatarZone)
  635. {
  636. ui->cbWatermark->setChecked(watermarkAvatar);
  637. }
  638. else
  639. {
  640. ui->cbWatermark->setChecked(watermarkPicture);
  641. }
  642. watermarkBlock = false;
  643. processImage();
  644. }
  645. void ImportDialog::on_cmdCancel_clicked()
  646. {
  647. close();
  648. }
  649. void ImportDialog::on_cmdOK_clicked()
  650. {
  651. importAgreed = true;
  652. close();
  653. }
  654. void ImportDialog::on_labPicture_labelPainted()
  655. {
  656. if (insideAvatarZone)
  657. {
  658. QImage avatarAreaFinalImage(avatarAreaImage);
  659. if (selectedColour.lightness() > 127)
  660. {
  661. avatarAreaFinalImage.setColor(1, qRgb(0, 0, 0));
  662. }
  663. QPainter labelPainter(ui->labPicture);
  664. labelPainter.drawImage(0, 0, avatarAreaFinalImage.scaled(snapmaticResolutionLW, snapmaticResolutionLH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
  665. labelPainter.end();
  666. }
  667. }
  668. void ImportDialog::on_cmdColourChange_clicked()
  669. {
  670. QColor newSelectedColour = QColorDialog::getColor(selectedColour, this, tr("Select Colour..."));
  671. if (newSelectedColour.isValid())
  672. {
  673. selectedColour = newSelectedColour;
  674. ui->labColour->setText(tr("Background Colour: <span style=\"color: %1\">%1</span>").arg(selectedColour.name()));
  675. processImage();
  676. }
  677. }
  678. void ImportDialog::on_cmdBackgroundChange_clicked()
  679. {
  680. QSettings settings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  681. settings.beginGroup("FileDialogs");
  682. bool dontUseNativeDialog = settings.value("DontUseNativeDialog", false).toBool();
  683. settings.beginGroup("ImportBackground");
  684. fileDialogPreOpen:
  685. QFileDialog fileDialog(this);
  686. fileDialog.setFileMode(QFileDialog::ExistingFiles);
  687. fileDialog.setViewMode(QFileDialog::Detail);
  688. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  689. fileDialog.setOption(QFileDialog::DontUseNativeDialog, dontUseNativeDialog);
  690. fileDialog.setWindowFlags(fileDialog.windowFlags()^Qt::WindowContextHelpButtonHint);
  691. fileDialog.setWindowTitle(QApplication::translate("ProfileInterface", "Import..."));
  692. fileDialog.setLabelText(QFileDialog::Accept, QApplication::translate("ProfileInterface", "Import"));
  693. // Getting readable Image formats
  694. QString imageFormatsStr = " ";
  695. for (QByteArray imageFormat : QImageReader::supportedImageFormats())
  696. {
  697. imageFormatsStr += QString("*.") % QString::fromUtf8(imageFormat).toLower() % " ";
  698. }
  699. QStringList filters;
  700. filters << QApplication::translate("ProfileInterface", "All image files (%1)").arg(imageFormatsStr.trimmed());
  701. filters << QApplication::translate("ProfileInterface", "All files (**)");
  702. fileDialog.setNameFilters(filters);
  703. QList<QUrl> sidebarUrls = SidebarGenerator::generateSidebarUrls(fileDialog.sidebarUrls());
  704. fileDialog.setSidebarUrls(sidebarUrls);
  705. fileDialog.setDirectory(settings.value("Directory", StandardPaths::documentsLocation()).toString());
  706. fileDialog.restoreGeometry(settings.value("Geometry", "").toByteArray());
  707. if (fileDialog.exec())
  708. {
  709. QStringList selectedFiles = fileDialog.selectedFiles();
  710. if (selectedFiles.length() == 1)
  711. {
  712. QString selectedFile = selectedFiles.at(0);
  713. QString selectedFileName = QFileInfo(selectedFile).fileName();
  714. QFile snapmaticFile(selectedFile);
  715. if (!snapmaticFile.open(QFile::ReadOnly))
  716. {
  717. QMessageBox::warning(this, QApplication::translate("ProfileInterface", "Import"), QApplication::translate("ProfileInterface", "Can't import %1 because file can't be open").arg("\""+selectedFileName+"\""));
  718. goto fileDialogPreOpen;
  719. }
  720. QImage importImage;
  721. QImageReader snapmaticImageReader;
  722. snapmaticImageReader.setDecideFormatFromContent(true);
  723. snapmaticImageReader.setDevice(&snapmaticFile);
  724. if (!snapmaticImageReader.read(&importImage))
  725. {
  726. QMessageBox::warning(this, QApplication::translate("ProfileInterface", "Import"), QApplication::translate("ProfileInterface", "Can't import %1 because file can't be parsed properly").arg("\""+selectedFileName+"\""));
  727. goto fileDialogPreOpen;
  728. }
  729. backImage = importImage.scaled(snapmaticResolutionW, snapmaticResolutionH, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  730. backgroundPath = selectedFile;
  731. ui->labBackgroundImage->setText(tr("Background Image: %1").arg(tr("File", "Background Image: File")));
  732. ui->cmdBackgroundWipe->setVisible(true);
  733. processImage();
  734. }
  735. }
  736. settings.setValue("Geometry", fileDialog.saveGeometry());
  737. settings.setValue("Directory", fileDialog.directory().absolutePath());
  738. settings.endGroup();
  739. settings.endGroup();
  740. }
  741. void ImportDialog::on_cmdBackgroundWipe_clicked()
  742. {
  743. backImage = QImage();
  744. ui->labBackgroundImage->setText(tr("Background Image:"));
  745. ui->cmdBackgroundWipe->setVisible(false);
  746. processImage();
  747. }
  748. void ImportDialog::on_cbStretch_toggled(bool checked)
  749. {
  750. Q_UNUSED(checked)
  751. processImage();
  752. }
  753. void ImportDialog::on_cbForceAvatarColour_toggled(bool checked)
  754. {
  755. Q_UNUSED(checked)
  756. processImage();
  757. }
  758. void ImportDialog::on_cbWatermark_toggled(bool checked)
  759. {
  760. if (!watermarkBlock)
  761. {
  762. if (insideAvatarZone)
  763. {
  764. watermarkAvatar = checked;
  765. }
  766. else
  767. {
  768. watermarkPicture = checked;
  769. }
  770. processImage();
  771. }
  772. }