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

SnapmaticEditor.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*****************************************************************************
  2. * gta5view Grand Theft Auto V Profile Viewer
  3. * Copyright (C) 2016-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 "SnapmaticEditor.h"
  19. #include "ui_SnapmaticEditor.h"
  20. #include "SnapmaticPicture.h"
  21. #include "PlayerListDialog.h"
  22. #include "StringParser.h"
  23. #include "AppEnv.h"
  24. #include "config.h"
  25. #include <QStringListIterator>
  26. #include <QStringBuilder>
  27. #include <QTextDocument>
  28. #include <QInputDialog>
  29. #include <QMessageBox>
  30. #include <QDebug>
  31. #include <QFile>
  32. #ifdef GTA5SYNC_TELEMETRY
  33. #include "TelemetryClass.h"
  34. #include <QJsonDocument>
  35. #include <QJsonObject>
  36. #endif
  37. SnapmaticEditor::SnapmaticEditor(CrewDatabase *crewDB, ProfileDatabase *profileDB, QWidget *parent) :
  38. QDialog(parent), crewDB(crewDB), profileDB(profileDB),
  39. ui(new Ui::SnapmaticEditor)
  40. {
  41. // Set Window Flags
  42. setWindowFlags(windowFlags()^Qt::WindowContextHelpButtonHint);
  43. ui->setupUi(this);
  44. ui->cmdCancel->setDefault(true);
  45. ui->cmdCancel->setFocus();
  46. // Set Icon for Apply Button
  47. if (QIcon::hasThemeIcon("dialog-ok-apply"))
  48. {
  49. ui->cmdApply->setIcon(QIcon::fromTheme("dialog-ok-apply"));
  50. }
  51. else if (QIcon::hasThemeIcon("dialog-apply"))
  52. {
  53. ui->cmdApply->setIcon(QIcon::fromTheme("dialog-apply"));
  54. }
  55. else if (QIcon::hasThemeIcon("gtk-apply"))
  56. {
  57. ui->cmdApply->setIcon(QIcon::fromTheme("gtk-apply"));
  58. }
  59. else if (QIcon::hasThemeIcon("dialog-ok"))
  60. {
  61. ui->cmdApply->setIcon(QIcon::fromTheme("dialog-ok"));
  62. }
  63. else if (QIcon::hasThemeIcon("gtk-ok"))
  64. {
  65. ui->cmdApply->setIcon(QIcon::fromTheme("dialog-ok"));
  66. }
  67. // Set Icon for Cancel Button
  68. if (QIcon::hasThemeIcon("dialog-cancel"))
  69. {
  70. ui->cmdCancel->setIcon(QIcon::fromTheme("dialog-cancel"));
  71. }
  72. else if (QIcon::hasThemeIcon("gtk-cancel"))
  73. {
  74. ui->cmdCancel->setIcon(QIcon::fromTheme("gtk-cancel"));
  75. }
  76. snapmaticTitle = QString();
  77. smpic = 0;
  78. #ifndef Q_OS_ANDROID
  79. // DPI calculation
  80. qreal screenRatio = AppEnv::screenRatio();
  81. resize(400 * screenRatio, 360 * screenRatio);
  82. #endif
  83. }
  84. SnapmaticEditor::~SnapmaticEditor()
  85. {
  86. delete ui;
  87. }
  88. void SnapmaticEditor::selfie_toggled(bool checked)
  89. {
  90. if (checked)
  91. {
  92. isSelfie = true;
  93. }
  94. else
  95. {
  96. isSelfie = false;
  97. }
  98. }
  99. void SnapmaticEditor::mugshot_toggled(bool checked)
  100. {
  101. if (checked)
  102. {
  103. isMugshot = true;
  104. ui->cbDirector->setEnabled(false);
  105. ui->cbDirector->setChecked(false);
  106. }
  107. else
  108. {
  109. isMugshot = false;
  110. ui->cbDirector->setEnabled(true);
  111. }
  112. }
  113. void SnapmaticEditor::editor_toggled(bool checked)
  114. {
  115. if (checked)
  116. {
  117. isEditor = true;
  118. ui->cbDirector->setEnabled(false);
  119. ui->cbDirector->setChecked(false);
  120. }
  121. else
  122. {
  123. isEditor = false;
  124. ui->cbDirector->setEnabled(true);
  125. }
  126. }
  127. void SnapmaticEditor::on_rbSelfie_toggled(bool checked)
  128. {
  129. if (checked)
  130. {
  131. mugshot_toggled(false);
  132. editor_toggled(false);
  133. selfie_toggled(true);
  134. }
  135. }
  136. void SnapmaticEditor::on_rbMugshot_toggled(bool checked)
  137. {
  138. if (checked)
  139. {
  140. selfie_toggled(false);
  141. editor_toggled(false);
  142. mugshot_toggled(true);
  143. }
  144. }
  145. void SnapmaticEditor::on_rbEditor_toggled(bool checked)
  146. {
  147. if (checked)
  148. {
  149. selfie_toggled(false);
  150. mugshot_toggled(false);
  151. editor_toggled(true);
  152. }
  153. }
  154. void SnapmaticEditor::on_rbCustom_toggled(bool checked)
  155. {
  156. if (checked)
  157. {
  158. selfie_toggled(false);
  159. mugshot_toggled(false);
  160. editor_toggled(false);
  161. }
  162. }
  163. void SnapmaticEditor::setSnapmaticPicture(SnapmaticPicture *picture)
  164. {
  165. smpic = picture;
  166. snapmaticProperties = smpic->getSnapmaticProperties();
  167. ui->rbCustom->setChecked(true);
  168. crewID = snapmaticProperties.crewID;
  169. isSelfie = snapmaticProperties.isSelfie;
  170. isMugshot = snapmaticProperties.isMug;
  171. isEditor = snapmaticProperties.isFromRSEditor;
  172. playersList = snapmaticProperties.playersList;
  173. ui->cbDirector->setChecked(snapmaticProperties.isFromDirector);
  174. ui->cbMeme->setChecked(snapmaticProperties.isMeme);
  175. if (isSelfie)
  176. {
  177. ui->rbSelfie->setChecked(true);
  178. }
  179. else if (isMugshot)
  180. {
  181. ui->rbMugshot->setChecked(true);
  182. }
  183. else if (isEditor)
  184. {
  185. ui->rbEditor->setChecked(true);
  186. }
  187. else
  188. {
  189. ui->rbCustom->setChecked(true);
  190. }
  191. setSnapmaticCrew(returnCrewName(crewID));
  192. setSnapmaticTitle(picture->getPictureTitle());
  193. setSnapmaticPlayers(insertPlayerNames(playersList));
  194. }
  195. void SnapmaticEditor::insertPlayerNames(QStringList *players)
  196. {
  197. for (int i = 0; i < players->size(); ++i)
  198. {
  199. players->replace(i, profileDB->getPlayerName(players->at(i)));
  200. }
  201. }
  202. QStringList SnapmaticEditor::insertPlayerNames(const QStringList &players)
  203. {
  204. QStringList playersWI = players;
  205. insertPlayerNames(&playersWI);
  206. return playersWI;
  207. }
  208. void SnapmaticEditor::setSnapmaticPlayers(const QStringList &players)
  209. {
  210. QString editStr = QString("<a href=\"g5e://editplayers\" style=\"text-decoration: none;\">%1</a>").arg(tr("Edit"));
  211. QString playersStr;
  212. if (players.length() != 1)
  213. {
  214. playersStr = tr("Players: %1 (%2)", "Multiple Player are inserted here");
  215. }
  216. else
  217. {
  218. playersStr = tr("Player: %1 (%2)", "One Player is inserted here");
  219. }
  220. if (players.length() != 0)
  221. {
  222. ui->labPlayers->setText(playersStr.arg(players.join(", "), editStr));
  223. }
  224. else
  225. {
  226. ui->labPlayers->setText(playersStr.arg(QApplication::translate("PictureDialog", "No Players"), editStr));
  227. }
  228. #ifndef Q_OS_ANDROID
  229. ui->gbValues->resize(ui->gbValues->width(), ui->gbValues->heightForWidth(ui->gbValues->width()));
  230. ui->frameWidget->resize(ui->gbValues->width(), ui->frameWidget->heightForWidth(ui->frameWidget->width()));
  231. if (heightForWidth(width()) > height()) { resize(width(), heightForWidth(width())); }
  232. #endif
  233. }
  234. void SnapmaticEditor::setSnapmaticTitle(const QString &title)
  235. {
  236. if (title.length() > 39)
  237. {
  238. snapmaticTitle = title.left(39);
  239. }
  240. else
  241. {
  242. snapmaticTitle = title;
  243. }
  244. QString editStr = QString("<a href=\"g5e://edittitle\" style=\"text-decoration: none;\">%1</a>").arg(tr("Edit"));
  245. QString titleStr = tr("Title: %1 (%2)").arg(StringParser::escapeString(snapmaticTitle), editStr);
  246. ui->labTitle->setText(titleStr);
  247. if (SnapmaticPicture::verifyTitle(snapmaticTitle))
  248. {
  249. ui->labAppropriate->setText(tr("Appropriate: %1").arg(QString("<span style=\"color: green\">%1</span>").arg(tr("Yes", "Yes, should work fine"))));
  250. }
  251. else
  252. {
  253. ui->labAppropriate->setText(tr("Appropriate: %1").arg(QString("<span style=\"color: red\">%1</span>").arg(tr("No", "No, could lead to issues"))));
  254. }
  255. #ifndef Q_OS_ANDROID
  256. ui->gbValues->resize(ui->gbValues->width(), ui->gbValues->heightForWidth(ui->gbValues->width()));
  257. ui->frameWidget->resize(ui->gbValues->width(), ui->frameWidget->heightForWidth(ui->frameWidget->width()));
  258. if (heightForWidth(width()) > height()) { resize(width(), heightForWidth(width())); }
  259. #endif
  260. }
  261. void SnapmaticEditor::setSnapmaticCrew(const QString &crew)
  262. {
  263. QString editStr = QString("<a href=\"g5e://editcrew\" style=\"text-decoration: none;\">%1</a>").arg(tr("Edit"));
  264. QString crewStr = tr("Crew: %1 (%2)").arg(StringParser::escapeString(crew), editStr);
  265. ui->labCrew->setText(crewStr);
  266. #ifndef Q_OS_ANDROID
  267. ui->gbValues->resize(ui->gbValues->width(), ui->gbValues->heightForWidth(ui->gbValues->width()));
  268. ui->frameWidget->resize(ui->gbValues->width(), ui->frameWidget->heightForWidth(ui->frameWidget->width()));
  269. if (heightForWidth(width()) > height()) { resize(width(), heightForWidth(width())); }
  270. #endif
  271. }
  272. QString SnapmaticEditor::returnCrewName(int crewID_)
  273. {
  274. return crewDB->getCrewName(crewID_);
  275. }
  276. void SnapmaticEditor::on_cmdCancel_clicked()
  277. {
  278. close();
  279. }
  280. void SnapmaticEditor::on_cmdApply_clicked()
  281. {
  282. if (ui->cbQualify->isChecked())
  283. {
  284. qualifyAvatar();
  285. }
  286. snapmaticProperties.crewID = crewID;
  287. snapmaticProperties.isSelfie = isSelfie;
  288. snapmaticProperties.isMug = isMugshot;
  289. snapmaticProperties.isFromRSEditor = isEditor;
  290. snapmaticProperties.isFromDirector = ui->cbDirector->isChecked();
  291. snapmaticProperties.isMeme = ui->cbMeme->isChecked();
  292. snapmaticProperties.playersList = playersList;
  293. if (smpic)
  294. {
  295. QString currentFilePath = smpic->getPictureFilePath();
  296. QString originalFilePath = smpic->getOriginalPictureFilePath();
  297. QString backupFileName = originalFilePath % ".bak";
  298. if (!QFile::exists(backupFileName))
  299. {
  300. QFile::copy(currentFilePath, backupFileName);
  301. }
  302. SnapmaticProperties fallbackProperties = smpic->getSnapmaticProperties();
  303. QString fallbackTitle = smpic->getPictureTitle();
  304. smpic->setSnapmaticProperties(snapmaticProperties);
  305. smpic->setPictureTitle(snapmaticTitle);
  306. if (!smpic->exportPicture(currentFilePath))
  307. {
  308. QMessageBox::warning(this, tr("Snapmatic Properties"), tr("Patching of Snapmatic Properties failed because of I/O Error"));
  309. smpic->setSnapmaticProperties(fallbackProperties);
  310. smpic->setPictureTitle(fallbackTitle);
  311. }
  312. else
  313. {
  314. smpic->updateStrings();
  315. smpic->emitUpdate();
  316. #ifdef GTA5SYNC_TELEMETRY
  317. QSettings telemetrySettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  318. telemetrySettings.beginGroup("Telemetry");
  319. bool pushUsageData = telemetrySettings.value("PushUsageData", false).toBool();
  320. telemetrySettings.endGroup();
  321. if (pushUsageData && Telemetry->canPush())
  322. {
  323. QJsonDocument jsonDocument;
  324. QJsonObject jsonObject;
  325. jsonObject["Type"] = "PropertyEdited";
  326. jsonObject["EditedSize"] = QString::number(smpic->getContentMaxLength());
  327. jsonObject["EditedTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
  328. jsonDocument.setObject(jsonObject);
  329. Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
  330. }
  331. #endif
  332. }
  333. }
  334. close();
  335. }
  336. void SnapmaticEditor::qualifyAvatar()
  337. {
  338. ui->rbSelfie->setChecked(true);
  339. ui->cbDirector->setChecked(false);
  340. ui->cbMeme->setChecked(false);
  341. ui->cmdApply->setDefault(true);
  342. }
  343. void SnapmaticEditor::on_cbQualify_toggled(bool checked)
  344. {
  345. if (checked)
  346. {
  347. ui->cbMeme->setEnabled(false);
  348. ui->cbDirector->setEnabled(false);
  349. ui->rbCustom->setEnabled(false);
  350. ui->rbSelfie->setEnabled(false);
  351. ui->rbEditor->setEnabled(false);
  352. ui->rbMugshot->setEnabled(false);
  353. }
  354. else
  355. {
  356. ui->cbMeme->setEnabled(true);
  357. ui->rbCustom->setEnabled(true);
  358. ui->rbSelfie->setEnabled(true);
  359. ui->rbEditor->setEnabled(true);
  360. ui->rbMugshot->setEnabled(true);
  361. if (ui->rbSelfie->isChecked() || ui->rbCustom->isChecked())
  362. {
  363. ui->cbDirector->setEnabled(true);
  364. }
  365. }
  366. }
  367. void SnapmaticEditor::on_labPlayers_linkActivated(const QString &link)
  368. {
  369. if (link == "g5e://editplayers")
  370. {
  371. PlayerListDialog *playerListDialog = new PlayerListDialog(playersList, profileDB, this);
  372. connect(playerListDialog, SIGNAL(playerListUpdated(QStringList)), this, SLOT(playerListUpdated(QStringList)));
  373. playerListDialog->setModal(true);
  374. playerListDialog->show();
  375. playerListDialog->exec();
  376. delete playerListDialog;
  377. }
  378. }
  379. void SnapmaticEditor::on_labTitle_linkActivated(const QString &link)
  380. {
  381. if (link == "g5e://edittitle")
  382. {
  383. bool ok;
  384. QString newTitle = QInputDialog::getText(this, tr("Snapmatic Title"), tr("New Snapmatic title:"), QLineEdit::Normal, snapmaticTitle, &ok, windowFlags());
  385. if (ok && !newTitle.isEmpty())
  386. {
  387. setSnapmaticTitle(newTitle);
  388. }
  389. }
  390. }
  391. void SnapmaticEditor::on_labCrew_linkActivated(const QString &link)
  392. {
  393. if (link == "g5e://editcrew")
  394. {
  395. bool ok;
  396. int indexNum = 0;
  397. QStringList itemList;
  398. QStringList crewList = crewDB->getCrews();
  399. if (!crewList.contains(QLatin1String("0")))
  400. {
  401. crewList += QLatin1String("0");
  402. }
  403. crewList.sort();
  404. for (QString crew : crewList)
  405. {
  406. itemList += QString("%1 (%2)").arg(crew, returnCrewName(crew.toInt()));
  407. }
  408. if (crewList.contains(QString::number(crewID)))
  409. {
  410. indexNum = crewList.indexOf(QRegExp(QString::number(crewID)));
  411. }
  412. QString newCrew = QInputDialog::getItem(this, tr("Snapmatic Crew"), tr("New Snapmatic crew:"), itemList, indexNum, true, &ok, windowFlags());
  413. if (ok && !newCrew.isEmpty())
  414. {
  415. if (newCrew.contains(" ")) newCrew = newCrew.split(" ").at(0);
  416. if (newCrew.length() > 10) return;
  417. for (QChar crewChar : newCrew)
  418. {
  419. if (!crewChar.isNumber())
  420. {
  421. return;
  422. }
  423. }
  424. if (!crewList.contains(newCrew))
  425. {
  426. crewDB->addCrew(crewID);
  427. }
  428. crewID = newCrew.toInt();
  429. setSnapmaticCrew(returnCrewName(crewID));
  430. }
  431. }
  432. }
  433. void SnapmaticEditor::playerListUpdated(QStringList playerList)
  434. {
  435. playersList = playerList;
  436. setSnapmaticPlayers(insertPlayerNames(playerList));
  437. }