Open Source Snapmatic picture and Savegame viewer/editor for GTA V https://gta5view.syping.de/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SnapmaticEditor.cpp 13KB

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