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

ProfileInterface.cpp 96KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375
  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 "ProfileInterface.h"
  19. #include "ui_ProfileInterface.h"
  20. #include "PlayerListDialog.h"
  21. #include "SidebarGenerator.h"
  22. #include "SnapmaticWidget.h"
  23. #include "DatabaseThread.h"
  24. #include "SavegameWidget.h"
  25. #include "PictureDialog.h"
  26. #include "PictureExport.h"
  27. #include "StandardPaths.h"
  28. #include "ProfileLoader.h"
  29. #include "ExportThread.h"
  30. #include "ImportDialog.h"
  31. #include "UiModLabel.h"
  32. #include "pcg_basic.h"
  33. #include "AppEnv.h"
  34. #include "config.h"
  35. #include <QNetworkAccessManager>
  36. #include <QProgressDialog>
  37. #include <QNetworkRequest>
  38. #include <QStringBuilder>
  39. #include <QNetworkReply>
  40. #include <QImageReader>
  41. #include <QProgressBar>
  42. #include <QInputDialog>
  43. #include <QPushButton>
  44. #include <QSpacerItem>
  45. #include <QMessageBox>
  46. #include <QMouseEvent>
  47. #include <QFileDialog>
  48. #include <QVBoxLayout>
  49. #include <QEventLoop>
  50. #include <QScrollBar>
  51. #include <QClipboard>
  52. #include <QFileInfo>
  53. #include <QPalette>
  54. #include <QPainter>
  55. #include <QRegExp>
  56. #include <QAction>
  57. #include <QDebug>
  58. #include <QColor>
  59. #include <QTimer>
  60. #include <QStyle>
  61. #include <QFile>
  62. #include <QUrl>
  63. #include <QDir>
  64. #include <random>
  65. #include <ctime>
  66. #ifdef GTA5SYNC_TELEMETRY
  67. #include "TelemetryClass.h"
  68. #include <QJsonDocument>
  69. #include <QJsonObject>
  70. #endif
  71. #define importTimeFormat "HHmmss"
  72. #define findRetryLimit 500
  73. ProfileInterface::ProfileInterface(ProfileDatabase *profileDB, CrewDatabase *crewDB, DatabaseThread *threadDB, QWidget *parent) :
  74. QWidget(parent), profileDB(profileDB), crewDB(crewDB), threadDB(threadDB),
  75. ui(new Ui::ProfileInterface)
  76. {
  77. ui->setupUi(this);
  78. ui->cmdImport->setEnabled(false);
  79. ui->cmdCloseProfile->setEnabled(false);
  80. loadingStr = ui->labProfileLoading->text();
  81. enabledPicStr = tr("Enabled pictures: %1 of %2");
  82. selectedWidgts = 0;
  83. profileFolder = "";
  84. contextMenuOpened = false;
  85. isProfileLoaded = false;
  86. previousWidget = nullptr;
  87. profileLoader = nullptr;
  88. saSpacerItem = nullptr;
  89. updatePalette();
  90. QString appVersion = GTA5SYNC_APPVER;
  91. #ifndef GTA5SYNC_BUILDTYPE_REL
  92. #ifdef GTA5SYNC_COMMIT
  93. if (!appVersion.contains("-")) { appVersion = appVersion % "-" % GTA5SYNC_COMMIT; }
  94. #endif
  95. #endif
  96. ui->labVersion->setText(QString("%1 %2").arg(GTA5SYNC_APPSTR, appVersion));
  97. ui->saProfileContent->setFilesDropEnabled(true);
  98. ui->saProfileContent->setImageDropEnabled(true);
  99. // Set Icon for Close Button
  100. if (QIcon::hasThemeIcon("dialog-close"))
  101. {
  102. ui->cmdCloseProfile->setIcon(QIcon::fromTheme("dialog-close"));
  103. }
  104. else if (QIcon::hasThemeIcon("gtk-close"))
  105. {
  106. ui->cmdCloseProfile->setIcon(QIcon::fromTheme("gtk-close"));
  107. }
  108. // Set Icon for Import Button
  109. if (QIcon::hasThemeIcon("document-import"))
  110. {
  111. ui->cmdImport->setIcon(QIcon::fromTheme("document-import"));
  112. }
  113. else if (QIcon::hasThemeIcon("document-open"))
  114. {
  115. ui->cmdImport->setIcon(QIcon::fromTheme("document-open"));
  116. }
  117. // DPI calculation
  118. qreal screenRatio = AppEnv::screenRatio();
  119. #ifndef Q_OS_MAC
  120. ui->hlButtons->setSpacing(6 * screenRatio);
  121. ui->hlButtons->setContentsMargins(9 * screenRatio, 9 * screenRatio, 9 * screenRatio, 9 * screenRatio);
  122. #else
  123. if (QApplication::style()->objectName() == "macintosh")
  124. {
  125. ui->hlButtons->setSpacing(6 * screenRatio);
  126. ui->hlButtons->setContentsMargins(9 * screenRatio, 15 * screenRatio, 15 * screenRatio, 17 * screenRatio);
  127. }
  128. else
  129. {
  130. ui->hlButtons->setSpacing(6 * screenRatio);
  131. ui->hlButtons->setContentsMargins(9 * screenRatio, 9 * screenRatio, 9 * screenRatio, 9 * screenRatio);
  132. }
  133. #endif
  134. // Seed RNG
  135. pcg32_srandom_r(&rng, time(NULL), (intptr_t)&rng);
  136. setMouseTracking(true);
  137. installEventFilter(this);
  138. }
  139. ProfileInterface::~ProfileInterface()
  140. {
  141. for (ProfileWidget *widget : widgets.keys())
  142. {
  143. widget->removeEventFilter(this);
  144. widget->disconnect();
  145. delete widget;
  146. }
  147. widgets.clear();
  148. for (SavegameData *savegame : savegames)
  149. {
  150. delete savegame;
  151. }
  152. savegames.clear();
  153. for (SnapmaticPicture *picture : pictures)
  154. {
  155. delete picture;
  156. }
  157. pictures.clear();
  158. delete profileLoader;
  159. delete ui;
  160. }
  161. void ProfileInterface::setProfileFolder(QString folder, QString profile)
  162. {
  163. profileFolder = folder;
  164. profileName = profile;
  165. }
  166. void ProfileInterface::setupProfileInterface()
  167. {
  168. fixedPictures.clear();
  169. ui->labProfileLoading->setText(tr("Loading..."));
  170. profileLoader = new ProfileLoader(profileFolder, crewDB);
  171. QObject::connect(profileLoader, SIGNAL(savegameLoaded(SavegameData*, QString)), this, SLOT(savegameLoaded_event(SavegameData*, QString)));
  172. QObject::connect(profileLoader, SIGNAL(pictureLoaded(SnapmaticPicture*)), this, SLOT(pictureLoaded_event(SnapmaticPicture*)));
  173. QObject::connect(profileLoader, SIGNAL(pictureFixed(SnapmaticPicture*)), this, SLOT(pictureFixed_event(SnapmaticPicture*)));
  174. QObject::connect(profileLoader, SIGNAL(loadingProgress(int,int)), this, SLOT(loadingProgress(int,int)));
  175. QObject::connect(profileLoader, SIGNAL(finished()), this, SLOT(profileLoaded_p()));
  176. profileLoader->start();
  177. }
  178. void ProfileInterface::savegameLoaded_event(SavegameData *savegame, QString savegamePath)
  179. {
  180. savegameLoaded(savegame, savegamePath, false);
  181. }
  182. void ProfileInterface::savegameLoaded(SavegameData *savegame, QString savegamePath, bool inserted)
  183. {
  184. SavegameWidget *sgdWidget = new SavegameWidget(this);
  185. sgdWidget->setSavegameData(savegame, savegamePath);
  186. sgdWidget->setContentMode(contentMode);
  187. sgdWidget->setMouseTracking(true);
  188. sgdWidget->installEventFilter(this);
  189. widgets[sgdWidget] = "SGD" % QFileInfo(savegamePath).fileName();
  190. savegames += savegame;
  191. if (selectedWidgts != 0 || contentMode == 2) { sgdWidget->setSelectionMode(true); }
  192. QObject::connect(sgdWidget, SIGNAL(savegameDeleted()), this, SLOT(savegameDeleted_event()));
  193. QObject::connect(sgdWidget, SIGNAL(widgetSelected()), this, SLOT(profileWidgetSelected()));
  194. QObject::connect(sgdWidget, SIGNAL(widgetDeselected()), this, SLOT(profileWidgetDeselected()));
  195. QObject::connect(sgdWidget, SIGNAL(allWidgetsSelected()), this, SLOT(selectAllWidgets()));
  196. QObject::connect(sgdWidget, SIGNAL(allWidgetsDeselected()), this, SLOT(deselectAllWidgets()));
  197. QObject::connect(sgdWidget, SIGNAL(contextMenuTriggered(QContextMenuEvent*)), this, SLOT(contextMenuTriggeredSGD(QContextMenuEvent*)));
  198. if (inserted) { insertSavegameIPI(sgdWidget); }
  199. }
  200. void ProfileInterface::pictureLoaded_event(SnapmaticPicture *picture)
  201. {
  202. pictureLoaded(picture, false);
  203. }
  204. void ProfileInterface::pictureFixed_event(SnapmaticPicture *picture)
  205. {
  206. QString fixedPicture = picture->getPictureStr() % " (" % picture->getPictureTitl() % ")";
  207. fixedPictures << fixedPicture;
  208. }
  209. void ProfileInterface::pictureLoaded(SnapmaticPicture *picture, bool inserted)
  210. {
  211. SnapmaticWidget *picWidget = new SnapmaticWidget(profileDB, crewDB, threadDB, profileName, this);
  212. picWidget->setSnapmaticPicture(picture);
  213. picWidget->setContentMode(contentMode);
  214. picWidget->setMouseTracking(true);
  215. picWidget->installEventFilter(this);
  216. widgets[picWidget] = "PIC" % picture->getPictureSortStr();
  217. pictures += picture;
  218. if (selectedWidgts != 0 || contentMode == 2) { picWidget->setSelectionMode(true); }
  219. QObject::connect(picWidget, SIGNAL(pictureDeleted()), this, SLOT(pictureDeleted_event()));
  220. QObject::connect(picWidget, SIGNAL(widgetSelected()), this, SLOT(profileWidgetSelected()));
  221. QObject::connect(picWidget, SIGNAL(widgetDeselected()), this, SLOT(profileWidgetDeselected()));
  222. QObject::connect(picWidget, SIGNAL(allWidgetsSelected()), this, SLOT(selectAllWidgets()));
  223. QObject::connect(picWidget, SIGNAL(allWidgetsDeselected()), this, SLOT(deselectAllWidgets()));
  224. QObject::connect(picWidget, SIGNAL(nextPictureRequested(QWidget*)), this, SLOT(dialogNextPictureRequested(QWidget*)));
  225. QObject::connect(picWidget, SIGNAL(previousPictureRequested(QWidget*)), this, SLOT(dialogPreviousPictureRequested(QWidget*)));
  226. QObject::connect(picWidget, SIGNAL(contextMenuTriggered(QContextMenuEvent*)), this, SLOT(contextMenuTriggeredPIC(QContextMenuEvent*)));
  227. if (inserted) { insertSnapmaticIPI(picWidget); }
  228. }
  229. void ProfileInterface::loadingProgress(int value, int maximum)
  230. {
  231. ui->pbPictureLoading->setMaximum(maximum);
  232. ui->pbPictureLoading->setValue(value);
  233. ui->labProfileLoading->setText(loadingStr.arg(QString::number(value), QString::number(maximum)));
  234. }
  235. void ProfileInterface::insertSnapmaticIPI(QWidget *widget)
  236. {
  237. ProfileWidget *proWidget = qobject_cast<ProfileWidget*>(widget);
  238. if (widgets.contains(proWidget))
  239. {
  240. QString widgetKey = widgets[proWidget];
  241. QStringList widgetsKeyList = widgets.values();
  242. QStringList pictureKeyList = widgetsKeyList.filter("PIC", Qt::CaseSensitive);
  243. #if QT_VERSION >= 0x050600
  244. qSort(pictureKeyList.rbegin(), pictureKeyList.rend());
  245. #else
  246. qSort(pictureKeyList.begin(), pictureKeyList.end(), qGreater<QString>());
  247. #endif
  248. int picIndex = pictureKeyList.indexOf(QRegExp(widgetKey));
  249. ui->vlSnapmatic->insertWidget(picIndex, proWidget);
  250. qApp->processEvents();
  251. ui->saProfile->ensureWidgetVisible(proWidget, 0, 0);
  252. }
  253. }
  254. void ProfileInterface::insertSavegameIPI(QWidget *widget)
  255. {
  256. ProfileWidget *proWidget = qobject_cast<ProfileWidget*>(widget);
  257. if (widgets.contains(proWidget))
  258. {
  259. QString widgetKey = widgets[proWidget];
  260. QStringList widgetsKeyList = widgets.values();
  261. QStringList savegameKeyList = widgetsKeyList.filter("SGD", Qt::CaseSensitive);
  262. qSort(savegameKeyList.begin(), savegameKeyList.end());
  263. int sgdIndex = savegameKeyList.indexOf(QRegExp(widgetKey));
  264. ui->vlSavegame->insertWidget(sgdIndex, proWidget);
  265. qApp->processEvents();
  266. ui->saProfile->ensureWidgetVisible(proWidget, 0, 0);
  267. }
  268. }
  269. void ProfileInterface::dialogNextPictureRequested(QWidget *dialog)
  270. {
  271. PictureDialog *picDialog = qobject_cast<PictureDialog*>(dialog);
  272. ProfileWidget *proWidget = qobject_cast<ProfileWidget*>(sender());
  273. if (widgets.contains(proWidget))
  274. {
  275. QString widgetKey = widgets[proWidget];
  276. QStringList widgetsKeyList = widgets.values();
  277. QStringList pictureKeyList = widgetsKeyList.filter("PIC", Qt::CaseSensitive);
  278. #if QT_VERSION >= 0x050600
  279. qSort(pictureKeyList.rbegin(), pictureKeyList.rend());
  280. #else
  281. qSort(pictureKeyList.begin(), pictureKeyList.end(), qGreater<QString>());
  282. #endif
  283. int picIndex;
  284. if (picDialog->isIndexed())
  285. {
  286. picIndex = picDialog->getIndex();
  287. }
  288. else
  289. {
  290. picIndex = pictureKeyList.indexOf(QRegExp(widgetKey));
  291. }
  292. picIndex++;
  293. if (pictureKeyList.length() > picIndex)
  294. {
  295. QString newWidgetKey = pictureKeyList.at(picIndex);
  296. SnapmaticWidget *picWidget = (SnapmaticWidget*)widgets.key(newWidgetKey);
  297. //picDialog->setMaximumHeight(QWIDGETSIZE_MAX);
  298. picDialog->setSnapmaticPicture(picWidget->getPicture(), picIndex);
  299. //picDialog->setMaximumHeight(picDialog->height());
  300. }
  301. }
  302. }
  303. void ProfileInterface::dialogPreviousPictureRequested(QWidget *dialog)
  304. {
  305. PictureDialog *picDialog = qobject_cast<PictureDialog*>(dialog);
  306. ProfileWidget *proWidget = qobject_cast<ProfileWidget*>(sender());
  307. if (widgets.contains(proWidget))
  308. {
  309. QString widgetKey = widgets[proWidget];
  310. QStringList widgetsKeyList = widgets.values();
  311. QStringList pictureKeyList = widgetsKeyList.filter("PIC", Qt::CaseSensitive);
  312. #if QT_VERSION >= 0x050600
  313. qSort(pictureKeyList.rbegin(), pictureKeyList.rend());
  314. #else
  315. qSort(pictureKeyList.begin(), pictureKeyList.end(), qGreater<QString>());
  316. #endif
  317. int picIndex;
  318. if (picDialog->isIndexed())
  319. {
  320. picIndex = picDialog->getIndex();
  321. }
  322. else
  323. {
  324. picIndex = pictureKeyList.indexOf(QRegExp(widgetKey));
  325. }
  326. if (picIndex > 0)
  327. {
  328. picIndex--;
  329. QString newWidgetKey = pictureKeyList.at(picIndex );
  330. SnapmaticWidget *picWidget = (SnapmaticWidget*)widgets.key(newWidgetKey);
  331. //picDialog->setMaximumHeight(QWIDGETSIZE_MAX);
  332. picDialog->setSnapmaticPicture(picWidget->getPicture(), picIndex);
  333. //picDialog->setMaximumHeight(picDialog->height());
  334. }
  335. }
  336. }
  337. void ProfileInterface::sortingProfileInterface()
  338. {
  339. ui->vlSavegame->setEnabled(false);
  340. ui->vlSnapmatic->setEnabled(false);
  341. QStringList widgetsKeyList = widgets.values();
  342. qSort(widgetsKeyList.begin(), widgetsKeyList.end());
  343. for (QString widgetKey : widgetsKeyList)
  344. {
  345. ProfileWidget *widget = widgets.key(widgetKey);
  346. if (widget->getWidgetType() == "SnapmaticWidget")
  347. {
  348. ui->vlSnapmatic->insertWidget(0, widget);
  349. }
  350. else if (widget->getWidgetType() == "SavegameWidget")
  351. {
  352. ui->vlSavegame->addWidget(widget);
  353. }
  354. }
  355. ui->vlSavegame->setEnabled(true);
  356. ui->vlSnapmatic->setEnabled(true);
  357. qApp->processEvents();
  358. }
  359. void ProfileInterface::profileLoaded_p()
  360. {
  361. sortingProfileInterface();
  362. saSpacerItem = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
  363. ui->saProfileContent->layout()->addItem(saSpacerItem);
  364. ui->swProfile->setCurrentWidget(ui->pageProfile);
  365. ui->cmdCloseProfile->setEnabled(true);
  366. ui->cmdImport->setEnabled(true);
  367. isProfileLoaded = true;
  368. emit profileLoaded();
  369. if (!fixedPictures.isEmpty())
  370. {
  371. int fixedInt = 0;
  372. QString fixedStr;
  373. for (QString fixedPicture : fixedPictures)
  374. {
  375. if (fixedInt != 0) { fixedStr += "<br>"; }
  376. fixedStr += fixedPicture;
  377. fixedInt++;
  378. }
  379. QMessageBox::information(this, tr("Snapmatic Loader"), tr("<h4>Following Snapmatic Pictures got repaired</h4>%1").arg(fixedStr));
  380. }
  381. }
  382. void ProfileInterface::savegameDeleted_event()
  383. {
  384. savegameDeleted(qobject_cast<SavegameWidget*>(sender()), true);
  385. }
  386. void ProfileInterface::savegameDeleted(SavegameWidget *sgdWidget, bool isRemoteEmited)
  387. {
  388. SavegameData *savegame = sgdWidget->getSavegame();
  389. if (sgdWidget->isSelected()) { sgdWidget->setSelected(false); }
  390. widgets.remove(sgdWidget);
  391. sgdWidget->disconnect();
  392. sgdWidget->removeEventFilter(this);
  393. if (sgdWidget == previousWidget)
  394. {
  395. previousWidget = nullptr;
  396. }
  397. // Deleting when the widget did send a event cause a crash
  398. isRemoteEmited ? sgdWidget->deleteLater() : delete sgdWidget;
  399. savegames.removeAll(savegame);
  400. delete savegame;
  401. }
  402. void ProfileInterface::pictureDeleted_event()
  403. {
  404. pictureDeleted(qobject_cast<SnapmaticWidget*>(sender()), true);
  405. }
  406. void ProfileInterface::pictureDeleted(SnapmaticWidget *picWidget, bool isRemoteEmited)
  407. {
  408. SnapmaticPicture *picture = picWidget->getPicture();
  409. if (picWidget->isSelected()) { picWidget->setSelected(false); }
  410. widgets.remove(picWidget);
  411. picWidget->disconnect();
  412. picWidget->removeEventFilter(this);
  413. if (picWidget == previousWidget)
  414. {
  415. previousWidget = nullptr;
  416. }
  417. // Deleting when the widget did send a event cause a crash
  418. isRemoteEmited ? picWidget->deleteLater() : delete picWidget;
  419. pictures.removeAll(picture);
  420. delete picture;
  421. }
  422. void ProfileInterface::on_cmdCloseProfile_clicked()
  423. {
  424. emit profileClosed();
  425. }
  426. void ProfileInterface::on_cmdImport_clicked()
  427. {
  428. QSettings settings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  429. settings.beginGroup("FileDialogs");
  430. bool dontUseNativeDialog = settings.value("DontUseNativeDialog", false).toBool();
  431. settings.beginGroup("ImportCopy");
  432. fileDialogPreOpen: //Work?
  433. QFileDialog fileDialog(this);
  434. fileDialog.setFileMode(QFileDialog::ExistingFiles);
  435. fileDialog.setViewMode(QFileDialog::Detail);
  436. fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
  437. fileDialog.setOption(QFileDialog::DontUseNativeDialog, dontUseNativeDialog);
  438. fileDialog.setWindowFlags(fileDialog.windowFlags()^Qt::WindowContextHelpButtonHint);
  439. fileDialog.setWindowTitle(tr("Import..."));
  440. fileDialog.setLabelText(QFileDialog::Accept, tr("Import..."));
  441. // Getting readable Image formats
  442. QString imageFormatsStr = " ";
  443. for (QByteArray imageFormat : QImageReader::supportedImageFormats())
  444. {
  445. imageFormatsStr += QString("*.") % QString::fromUtf8(imageFormat).toLower() % " ";
  446. }
  447. QString importableFormatsStr = QString("*.g5e SGTA* PGTA*");
  448. if (!imageFormatsStr.trimmed().isEmpty())
  449. {
  450. importableFormatsStr = QString("*.g5e%1SGTA* PGTA*").arg(imageFormatsStr);
  451. }
  452. QStringList filters;
  453. filters << tr("Importable files (%1)").arg(importableFormatsStr);
  454. filters << tr("GTA V Export (*.g5e)");
  455. filters << tr("Savegames files (SGTA*)");
  456. filters << tr("Snapmatic pictures (PGTA*)");
  457. filters << tr("All image files (%1)").arg(imageFormatsStr.trimmed());
  458. filters << tr("All files (**)");
  459. fileDialog.setNameFilters(filters);
  460. QList<QUrl> sidebarUrls = SidebarGenerator::generateSidebarUrls(fileDialog.sidebarUrls());
  461. fileDialog.setSidebarUrls(sidebarUrls);
  462. fileDialog.setDirectory(settings.value(profileName % "+Directory", StandardPaths::documentsLocation()).toString());
  463. fileDialog.restoreGeometry(settings.value(profileName % "+Geometry", "").toByteArray());
  464. if (fileDialog.exec())
  465. {
  466. QStringList selectedFiles = fileDialog.selectedFiles();
  467. if (selectedFiles.length() == 1)
  468. {
  469. QString selectedFile = selectedFiles.at(0);
  470. QDateTime importDateTime = QDateTime::currentDateTime();
  471. if (!importFile(selectedFile, importDateTime, true)) goto fileDialogPreOpen; //Work?
  472. }
  473. else if (selectedFiles.length() > 1)
  474. {
  475. importFilesProgress(selectedFiles);
  476. }
  477. else
  478. {
  479. QMessageBox::warning(this, tr("Import..."), tr("No valid file is selected"));
  480. goto fileDialogPreOpen; //Work?
  481. }
  482. }
  483. settings.setValue(profileName % "+Geometry", fileDialog.saveGeometry());
  484. settings.setValue(profileName % "+Directory", fileDialog.directory().absolutePath());
  485. settings.endGroup();
  486. settings.endGroup();
  487. }
  488. bool ProfileInterface::importFilesProgress(QStringList selectedFiles)
  489. {
  490. int maximumId = selectedFiles.length();
  491. int overallId = 0;
  492. QString errorStr;
  493. QStringList failed;
  494. // Progress dialog
  495. QProgressDialog pbDialog(this);
  496. pbDialog.setWindowFlags(pbDialog.windowFlags()^Qt::WindowContextHelpButtonHint^Qt::WindowCloseButtonHint);
  497. pbDialog.setWindowTitle(tr("Import..."));
  498. pbDialog.setLabelText(tr("Import file %1 of %2 files").arg(QString::number(1), QString::number(maximumId)));
  499. pbDialog.setRange(1, maximumId);
  500. pbDialog.setValue(1);
  501. pbDialog.setModal(true);
  502. QList<QPushButton*> pbBtn = pbDialog.findChildren<QPushButton*>();
  503. pbBtn.at(0)->setDisabled(true);
  504. QList<QProgressBar*> pbBar = pbDialog.findChildren<QProgressBar*>();
  505. pbBar.at(0)->setTextVisible(false);
  506. pbDialog.setAutoClose(false);
  507. pbDialog.show();
  508. // THREADING HERE PLEASE
  509. QDateTime importDateTime = QDateTime::currentDateTime();
  510. for (QString selectedFile : selectedFiles)
  511. {
  512. overallId++;
  513. pbDialog.setValue(overallId);
  514. pbDialog.setLabelText(tr("Import file %1 of %2 files").arg(QString::number(overallId), QString::number(maximumId)));
  515. importDateTime = QDateTime::currentDateTime();
  516. if (!importFile(selectedFile, importDateTime, false))
  517. {
  518. failed << QFileInfo(selectedFile).fileName();
  519. }
  520. }
  521. pbDialog.close();
  522. for (QString curErrorStr : failed)
  523. {
  524. errorStr += ", " % curErrorStr;
  525. }
  526. if (errorStr != "")
  527. {
  528. errorStr.remove(0, 2);
  529. QMessageBox::warning(this, tr("Import..."), tr("Import failed with...\n\n%1").arg(errorStr));
  530. return false;
  531. }
  532. return true;
  533. }
  534. bool ProfileInterface::importFile(QString selectedFile, QDateTime importDateTime, bool notMultiple)
  535. {
  536. QString selectedFileName = QFileInfo(selectedFile).fileName();
  537. if (QFile::exists(selectedFile))
  538. {
  539. if ((selectedFileName.left(4) == "PGTA" && !selectedFileName.contains('.')) || selectedFileName.right(4) == ".g5e")
  540. {
  541. SnapmaticPicture *picture = new SnapmaticPicture(selectedFile);
  542. if (picture->readingPicture(true, true, true))
  543. {
  544. bool success = importSnapmaticPicture(picture, notMultiple);
  545. if (!success) delete picture;
  546. #ifdef GTA5SYNC_TELEMETRY
  547. if (success && notMultiple)
  548. {
  549. QSettings telemetrySettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  550. telemetrySettings.beginGroup("Telemetry");
  551. bool pushUsageData = telemetrySettings.value("PushUsageData", false).toBool();
  552. telemetrySettings.endGroup();
  553. if (pushUsageData && Telemetry->canPush())
  554. {
  555. QJsonDocument jsonDocument;
  556. QJsonObject jsonObject;
  557. jsonObject["Type"] = "ImportSuccess";
  558. jsonObject["ImportSize"] = QString::number(picture->getContentMaxLength());
  559. jsonObject["ImportTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
  560. jsonObject["ImportType"] = "Snapmatic";
  561. jsonDocument.setObject(jsonObject);
  562. Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
  563. }
  564. }
  565. #endif
  566. return success;
  567. }
  568. else
  569. {
  570. if (notMultiple) QMessageBox::warning(this, tr("Import..."), tr("Failed to read Snapmatic picture"));
  571. delete picture;
  572. return false;
  573. }
  574. }
  575. else if (selectedFileName.left(4) == "SGTA")
  576. {
  577. SavegameData *savegame = new SavegameData(selectedFile);
  578. if (savegame->readingSavegame())
  579. {
  580. bool success = importSavegameData(savegame, selectedFile, notMultiple);
  581. if (!success) delete savegame;
  582. #ifdef GTA5SYNC_TELEMETRY
  583. if (success && notMultiple)
  584. {
  585. QSettings telemetrySettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  586. telemetrySettings.beginGroup("Telemetry");
  587. bool pushUsageData = telemetrySettings.value("PushUsageData", false).toBool();
  588. telemetrySettings.endGroup();
  589. if (pushUsageData && Telemetry->canPush())
  590. {
  591. QJsonDocument jsonDocument;
  592. QJsonObject jsonObject;
  593. jsonObject["Type"] = "ImportSuccess";
  594. jsonObject["ImportTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
  595. jsonObject["ImportType"] = "Savegame";
  596. jsonDocument.setObject(jsonObject);
  597. Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
  598. }
  599. }
  600. #endif
  601. return success;
  602. }
  603. else
  604. {
  605. if (notMultiple) QMessageBox::warning(this, tr("Import..."), tr("Failed to read Savegame file"));
  606. delete savegame;
  607. return false;
  608. }
  609. }
  610. else if (isSupportedImageFile(selectedFileName))
  611. {
  612. SnapmaticPicture *picture = new SnapmaticPicture(":/template/template.g5e");
  613. if (picture->readingPicture(true, false, true, false))
  614. {
  615. if (!notMultiple)
  616. {
  617. QFile snapmaticFile(selectedFile);
  618. if (!snapmaticFile.open(QFile::ReadOnly))
  619. {
  620. delete picture;
  621. return false;
  622. }
  623. QImage snapmaticImage;
  624. QImageReader snapmaticImageReader;
  625. snapmaticImageReader.setDecideFormatFromContent(true);
  626. snapmaticImageReader.setDevice(&snapmaticFile);
  627. if (!snapmaticImageReader.read(&snapmaticImage))
  628. {
  629. delete picture;
  630. return false;
  631. }
  632. QString customImageTitle;
  633. QPixmap snapmaticPixmap(960, 536);
  634. snapmaticPixmap.fill(Qt::black);
  635. QPainter snapmaticPainter(&snapmaticPixmap);
  636. if (snapmaticImage.height() == snapmaticImage.width())
  637. {
  638. // Avatar mode
  639. int diffWidth = 0;
  640. int diffHeight = 0;
  641. snapmaticImage = snapmaticImage.scaled(470, 470, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  642. if (snapmaticImage.width() > snapmaticImage.height())
  643. {
  644. diffHeight = 470 - snapmaticImage.height();
  645. diffHeight = diffHeight / 2;
  646. }
  647. else if (snapmaticImage.width() < snapmaticImage.height())
  648. {
  649. diffWidth = 470 - snapmaticImage.width();
  650. diffWidth = diffWidth / 2;
  651. }
  652. snapmaticPainter.drawImage(145 + diffWidth, 66 + diffHeight, snapmaticImage);
  653. customImageTitle = ImportDialog::tr("Custom Avatar", "Custom Avatar Description in SC, don't use Special Character!");
  654. }
  655. else
  656. {
  657. // Picture mode
  658. int diffWidth = 0;
  659. int diffHeight = 0;
  660. snapmaticImage = snapmaticImage.scaled(960, 536, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  661. if (snapmaticImage.width() != 960)
  662. {
  663. diffWidth = 960 - snapmaticImage.width();
  664. diffWidth = diffWidth / 2;
  665. }
  666. else if (snapmaticImage.height() != 536)
  667. {
  668. diffHeight = 536 - snapmaticImage.height();
  669. diffHeight = diffHeight / 2;
  670. }
  671. snapmaticPainter.drawImage(0 + diffWidth, 0 + diffHeight, snapmaticImage);
  672. customImageTitle = ImportDialog::tr("Custom Picture", "Custom Picture Description in SC, don't use Special Character!");
  673. }
  674. snapmaticPainter.end();
  675. if (!picture->setImage(snapmaticPixmap.toImage()))
  676. {
  677. delete picture;
  678. return false;
  679. }
  680. SnapmaticProperties spJson = picture->getSnapmaticProperties();
  681. spJson.uid = getRandomUid();
  682. bool fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid));
  683. bool fExistsBackup = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".bak");
  684. bool fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".hidden");
  685. int cEnough = 0;
  686. while ((fExists || fExistsBackup || fExistsHidden) && cEnough < findRetryLimit)
  687. {
  688. spJson.uid = getRandomUid();
  689. fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid));
  690. fExistsBackup = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".bak");
  691. fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".hidden");
  692. cEnough++;
  693. }
  694. spJson.createdDateTime = importDateTime;
  695. spJson.createdTimestamp = spJson.createdDateTime.toTime_t();
  696. picture->setSnapmaticProperties(spJson);
  697. picture->setPicFileName(QString("PGTA5%1").arg(QString::number(spJson.uid)));
  698. picture->setPictureTitle(customImageTitle);
  699. picture->updateStrings();
  700. bool success = importSnapmaticPicture(picture, notMultiple);
  701. if (!success) delete picture;
  702. return success;
  703. }
  704. else
  705. {
  706. bool success = false;
  707. QFile snapmaticFile(selectedFile);
  708. if (!snapmaticFile.open(QFile::ReadOnly))
  709. {
  710. QMessageBox::warning(this, tr("Import..."), tr("Can't import %1 because file can't be open").arg("\""+selectedFileName+"\""));
  711. delete picture;
  712. return false;
  713. }
  714. QImage *snapmaticImage = new QImage();
  715. QImageReader snapmaticImageReader;
  716. snapmaticImageReader.setDecideFormatFromContent(true);
  717. snapmaticImageReader.setDevice(&snapmaticFile);
  718. if (!snapmaticImageReader.read(snapmaticImage))
  719. {
  720. QMessageBox::warning(this, tr("Import..."), tr("Can't import %1 because file can't be parsed properly").arg("\""+selectedFileName+"\""));
  721. delete snapmaticImage;
  722. delete picture;
  723. return false;
  724. }
  725. ImportDialog *importDialog = new ImportDialog(profileName, this);
  726. importDialog->setImage(snapmaticImage);
  727. importDialog->setModal(true);
  728. importDialog->show();
  729. importDialog->exec();
  730. if (importDialog->isImportAgreed())
  731. {
  732. if (picture->setImage(importDialog->image()))
  733. {
  734. SnapmaticProperties spJson = picture->getSnapmaticProperties();
  735. spJson.uid = getRandomUid();
  736. bool fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid));
  737. bool fExistsBackup = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".bak");
  738. bool fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".hidden");
  739. int cEnough = 0;
  740. while ((fExists || fExistsBackup || fExistsHidden) && cEnough < findRetryLimit)
  741. {
  742. spJson.uid = getRandomUid();
  743. fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid));
  744. fExistsBackup = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".bak");
  745. fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".hidden");
  746. cEnough++;
  747. }
  748. spJson.createdDateTime = importDateTime;
  749. spJson.createdTimestamp = spJson.createdDateTime.toTime_t();
  750. picture->setSnapmaticProperties(spJson);
  751. picture->setPicFileName(QString("PGTA5%1").arg(QString::number(spJson.uid)));
  752. picture->setPictureTitle(importDialog->getImageTitle());
  753. picture->updateStrings();
  754. success = importSnapmaticPicture(picture, notMultiple);
  755. #ifdef GTA5SYNC_TELEMETRY
  756. if (success)
  757. {
  758. QSettings telemetrySettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  759. telemetrySettings.beginGroup("Telemetry");
  760. bool pushUsageData = telemetrySettings.value("PushUsageData", false).toBool();
  761. telemetrySettings.endGroup();
  762. if (pushUsageData && Telemetry->canPush())
  763. {
  764. QJsonDocument jsonDocument;
  765. QJsonObject jsonObject;
  766. jsonObject["Type"] = "ImportSuccess";
  767. jsonObject["ExtraFlag"] = "Dialog";
  768. jsonObject["ImportSize"] = QString::number(picture->getContentMaxLength());
  769. jsonObject["ImportTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
  770. jsonObject["ImportType"] = "Image";
  771. jsonDocument.setObject(jsonObject);
  772. Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
  773. }
  774. }
  775. #endif
  776. }
  777. }
  778. else
  779. {
  780. delete picture;
  781. success = true;
  782. }
  783. delete importDialog;
  784. if (!success) delete picture;
  785. return success;
  786. }
  787. }
  788. else
  789. {
  790. delete picture;
  791. return false;
  792. }
  793. }
  794. else
  795. {
  796. SnapmaticPicture *picture = new SnapmaticPicture(selectedFile);
  797. SavegameData *savegame = new SavegameData(selectedFile);
  798. if (picture->readingPicture())
  799. {
  800. bool success = importSnapmaticPicture(picture, notMultiple);
  801. delete savegame;
  802. if (!success) delete picture;
  803. #ifdef GTA5SYNC_TELEMETRY
  804. if (success && notMultiple)
  805. {
  806. QSettings telemetrySettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  807. telemetrySettings.beginGroup("Telemetry");
  808. bool pushUsageData = telemetrySettings.value("PushUsageData", false).toBool();
  809. telemetrySettings.endGroup();
  810. if (pushUsageData && Telemetry->canPush())
  811. {
  812. QJsonDocument jsonDocument;
  813. QJsonObject jsonObject;
  814. jsonObject["Type"] = "ImportSuccess";
  815. jsonObject["ImportSize"] = QString::number(picture->getContentMaxLength());
  816. jsonObject["ImportTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
  817. jsonObject["ImportType"] = "Snapmatic";
  818. jsonDocument.setObject(jsonObject);
  819. Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
  820. }
  821. }
  822. #endif
  823. return success;
  824. }
  825. else if (savegame->readingSavegame())
  826. {
  827. bool success = importSavegameData(savegame, selectedFile, notMultiple);
  828. delete picture;
  829. if (!success) delete savegame;
  830. #ifdef GTA5SYNC_TELEMETRY
  831. if (success && notMultiple)
  832. {
  833. QSettings telemetrySettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  834. telemetrySettings.beginGroup("Telemetry");
  835. bool pushUsageData = telemetrySettings.value("PushUsageData", false).toBool();
  836. telemetrySettings.endGroup();
  837. if (pushUsageData && Telemetry->canPush())
  838. {
  839. QJsonDocument jsonDocument;
  840. QJsonObject jsonObject;
  841. jsonObject["Type"] = "ImportSuccess";
  842. jsonObject["ImportTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
  843. jsonObject["ImportType"] = "Savegame";
  844. jsonDocument.setObject(jsonObject);
  845. Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
  846. }
  847. }
  848. #endif
  849. return success;
  850. }
  851. else
  852. {
  853. #ifdef GTA5SYNC_DEBUG
  854. qDebug() << "ImportError SnapmaticPicture" << picture->getLastStep();
  855. qDebug() << "ImportError SavegameData" << savegame->getLastStep();
  856. #endif
  857. delete picture;
  858. delete savegame;
  859. if (notMultiple) QMessageBox::warning(this, tr("Import..."), tr("Can't import %1 because file format can't be detected").arg("\""+selectedFileName+"\""));
  860. return false;
  861. }
  862. }
  863. }
  864. if (notMultiple) QMessageBox::warning(this, tr("Import..."), tr("No valid file is selected"));
  865. return false;
  866. }
  867. bool ProfileInterface::importUrls(const QMimeData *mimeData)
  868. {
  869. QStringList pathList;
  870. for (QUrl currentUrl : mimeData->urls())
  871. {
  872. if (currentUrl.isLocalFile())
  873. {
  874. pathList += currentUrl.toLocalFile();
  875. }
  876. }
  877. if (pathList.length() == 1)
  878. {
  879. QString selectedFile = pathList.at(0);
  880. return importFile(selectedFile, QDateTime::currentDateTime(), true);
  881. }
  882. else if (pathList.length() > 1)
  883. {
  884. return importFilesProgress(pathList);
  885. }
  886. return false;
  887. }
  888. bool ProfileInterface::importRemote(QUrl remoteUrl)
  889. {
  890. bool retValue = false;
  891. QDialog urlPasteDialog(this);
  892. #if QT_VERSION >= 0x050000
  893. urlPasteDialog.setObjectName(QStringLiteral("UrlPasteDialog"));
  894. #else
  895. urlPasteDialog.setObjectName(QString::fromUtf8("UrlPasteDialog"));
  896. #endif
  897. urlPasteDialog.setWindowFlags(urlPasteDialog.windowFlags()^Qt::WindowContextHelpButtonHint^Qt::WindowCloseButtonHint);
  898. urlPasteDialog.setWindowTitle(tr("Import..."));
  899. urlPasteDialog.setModal(true);
  900. QVBoxLayout urlPasteLayout(&urlPasteDialog);
  901. #if QT_VERSION >= 0x050000
  902. urlPasteLayout.setObjectName(QStringLiteral("UrlPasteLayout"));
  903. #else
  904. urlPasteLayout.setObjectName(QString::fromUtf8("UrlPasteLayout"));
  905. #endif
  906. urlPasteDialog.setLayout(&urlPasteLayout);
  907. UiModLabel urlPasteLabel(&urlPasteDialog);
  908. #if QT_VERSION >= 0x050000
  909. urlPasteLabel.setObjectName(QStringLiteral("UrlPasteLabel"));
  910. #else
  911. urlPasteLabel.setObjectName(QString::fromUtf8("UrlPasteLabel"));
  912. #endif
  913. urlPasteLabel.setText(tr("Prepare Content for Import..."));
  914. urlPasteLayout.addWidget(&urlPasteLabel);
  915. urlPasteDialog.setFixedSize(urlPasteDialog.sizeHint());
  916. urlPasteDialog.show();
  917. QNetworkAccessManager *netManager = new QNetworkAccessManager();
  918. QNetworkRequest netRequest(remoteUrl);
  919. netRequest.setRawHeader("User-Agent", AppEnv::getUserAgent());
  920. netRequest.setRawHeader("Accept", "text/html");
  921. netRequest.setRawHeader("Accept-Charset", "utf-8");
  922. netRequest.setRawHeader("Accept-Language", "en-US,en;q=0.9");
  923. netRequest.setRawHeader("Connection", "keep-alive");
  924. QNetworkReply *netReply = netManager->get(netRequest);
  925. QEventLoop *downloadLoop = new QEventLoop();
  926. QObject::connect(netReply, SIGNAL(finished()), downloadLoop, SLOT(quit()));
  927. QTimer::singleShot(30000, downloadLoop, SLOT(quit()));
  928. downloadLoop->exec();
  929. downloadLoop->disconnect();
  930. delete downloadLoop;
  931. urlPasteDialog.close();
  932. if (netReply->isFinished())
  933. {
  934. QImage *snapmaticImage = new QImage();
  935. QImageReader snapmaticImageReader;
  936. snapmaticImageReader.setDecideFormatFromContent(true);
  937. snapmaticImageReader.setDevice(netReply);
  938. if (snapmaticImageReader.read(snapmaticImage))
  939. {
  940. retValue = importImage(snapmaticImage, QDateTime::currentDateTime());
  941. }
  942. else
  943. {
  944. delete snapmaticImage;
  945. }
  946. }
  947. else
  948. {
  949. netReply->abort();
  950. }
  951. delete netReply;
  952. delete netManager;
  953. return retValue;
  954. }
  955. bool ProfileInterface::importImage(QImage *snapmaticImage, QDateTime importDateTime)
  956. {
  957. SnapmaticPicture *picture = new SnapmaticPicture(":/template/template.g5e");
  958. if (picture->readingPicture(true, false, true, false))
  959. {
  960. bool success = false;
  961. ImportDialog *importDialog = new ImportDialog(profileName, this);
  962. importDialog->setImage(snapmaticImage);
  963. importDialog->setModal(true);
  964. importDialog->show();
  965. importDialog->exec();
  966. if (importDialog->isImportAgreed())
  967. {
  968. if (picture->setImage(importDialog->image()))
  969. {
  970. SnapmaticProperties spJson = picture->getSnapmaticProperties();
  971. spJson.uid = getRandomUid();
  972. bool fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid));
  973. bool fExistsBackup = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".bak");
  974. bool fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".hidden");
  975. int cEnough = 0;
  976. while ((fExists || fExistsBackup || fExistsHidden) && cEnough < findRetryLimit)
  977. {
  978. spJson.uid = getRandomUid();
  979. fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid));
  980. fExistsBackup = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".bak");
  981. fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(spJson.uid) % ".hidden");
  982. cEnough++;
  983. }
  984. spJson.createdDateTime = importDateTime;
  985. spJson.createdTimestamp = spJson.createdDateTime.toTime_t();
  986. picture->setSnapmaticProperties(spJson);
  987. picture->setPicFileName(QString("PGTA5%1").arg(QString::number(spJson.uid)));
  988. picture->setPictureTitle(importDialog->getImageTitle());
  989. picture->updateStrings();
  990. success = importSnapmaticPicture(picture, true);
  991. }
  992. }
  993. else
  994. {
  995. delete picture;
  996. success = true;
  997. }
  998. delete importDialog;
  999. if (!success) delete picture;
  1000. return success;
  1001. }
  1002. else
  1003. {
  1004. delete picture;
  1005. return false;
  1006. }
  1007. }
  1008. bool ProfileInterface::importSnapmaticPicture(SnapmaticPicture *picture, bool warn)
  1009. {
  1010. QString picFileName = picture->getPictureFileName();
  1011. QString adjustedFileName = picture->getOriginalPictureFileName();
  1012. if (picFileName.left(4) != "PGTA")
  1013. {
  1014. if (warn) QMessageBox::warning(this, tr("Import..."), tr("Failed to import the Snapmatic picture, file not begin with PGTA or end with .g5e"));
  1015. return false;
  1016. }
  1017. else if (QFile::exists(profileFolder % "/" % adjustedFileName) || QFile::exists(profileFolder % "/" % adjustedFileName % ".hidden"))
  1018. {
  1019. SnapmaticProperties snapmaticProperties = picture->getSnapmaticProperties();
  1020. if (warn)
  1021. {
  1022. int uchoice = QMessageBox::question(this, tr("Import..."), tr("A Snapmatic picture already exists with the uid %1, you want assign your import a new uid and timestamp?").arg(QString::number(snapmaticProperties.uid)), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
  1023. if (uchoice == QMessageBox::Yes)
  1024. {
  1025. // Update Snapmatic uid
  1026. snapmaticProperties.uid = getRandomUid();
  1027. snapmaticProperties.createdDateTime = QDateTime::currentDateTime();
  1028. snapmaticProperties.createdTimestamp = snapmaticProperties.createdDateTime.toTime_t();
  1029. bool fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(snapmaticProperties.uid));
  1030. bool fExistsBackup = QFile::exists(profileFolder % "/PGTA5" % QString::number(snapmaticProperties.uid) % ".bak");
  1031. bool fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(snapmaticProperties.uid) % ".hidden");
  1032. int cEnough = 0;
  1033. while ((fExists || fExistsBackup || fExistsHidden) && cEnough < findRetryLimit)
  1034. {
  1035. snapmaticProperties.uid = getRandomUid();
  1036. fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(snapmaticProperties.uid));
  1037. fExistsBackup = QFile::exists(profileFolder % "/PGTA5" % QString::number(snapmaticProperties.uid) % ".bak");
  1038. fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(snapmaticProperties.uid) % ".hidden");
  1039. cEnough++;
  1040. }
  1041. if (fExists || fExistsBackup || fExistsHidden)
  1042. {
  1043. // That should never happen
  1044. return false;
  1045. }
  1046. if (!picture->setSnapmaticProperties(snapmaticProperties))
  1047. {
  1048. // That should never happen
  1049. return false;
  1050. }
  1051. picture->updateStrings();
  1052. picFileName = picture->getPictureFileName();
  1053. adjustedFileName = picture->getOriginalPictureFileName();
  1054. }
  1055. else
  1056. {
  1057. return false;
  1058. }
  1059. }
  1060. else
  1061. {
  1062. // Update Snapmatic uid
  1063. snapmaticProperties.uid = getRandomUid();
  1064. snapmaticProperties.createdDateTime = QDateTime::currentDateTime();
  1065. snapmaticProperties.createdTimestamp = snapmaticProperties.createdDateTime.toTime_t();
  1066. bool fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(snapmaticProperties.uid));
  1067. bool fExistsBackup = QFile::exists(profileFolder % "/PGTA5" % QString::number(snapmaticProperties.uid) % ".bak");
  1068. bool fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(snapmaticProperties.uid) % ".hidden");
  1069. int cEnough = 0;
  1070. while ((fExists || fExistsBackup || fExistsHidden) && cEnough < findRetryLimit)
  1071. {
  1072. snapmaticProperties.uid = getRandomUid();
  1073. fExists = QFile::exists(profileFolder % "/PGTA5" % QString::number(snapmaticProperties.uid));
  1074. fExistsBackup = QFile::exists(profileFolder % "/PGTA5" % QString::number(snapmaticProperties.uid) % ".bak");
  1075. fExistsHidden = QFile::exists(profileFolder % "/PGTA5" % QString::number(snapmaticProperties.uid) % ".hidden");
  1076. cEnough++;
  1077. }
  1078. if (fExists || fExistsBackup || fExistsHidden)
  1079. {
  1080. // That should never happen
  1081. return false;
  1082. }
  1083. if (!picture->setSnapmaticProperties(snapmaticProperties))
  1084. {
  1085. // That should never happen
  1086. return false;
  1087. }
  1088. picture->updateStrings();
  1089. picFileName = picture->getPictureFileName();
  1090. adjustedFileName = picture->getOriginalPictureFileName();
  1091. }
  1092. }
  1093. if (picture->exportPicture(profileFolder % "/" % adjustedFileName, SnapmaticFormat::PGTA_Format))
  1094. {
  1095. picture->setSnapmaticFormat(SnapmaticFormat::PGTA_Format);
  1096. picture->setPicFilePath(profileFolder % "/" % adjustedFileName);
  1097. pictureLoaded(picture, true);
  1098. return true;
  1099. }
  1100. else
  1101. {
  1102. if (warn) QMessageBox::warning(this, tr("Import..."), tr("Failed to import the Snapmatic picture, can't copy the file into profile"));
  1103. return false;
  1104. }
  1105. }
  1106. bool ProfileInterface::importSavegameData(SavegameData *savegame, QString sgdPath, bool warn)
  1107. {
  1108. QString sgdFileName;
  1109. bool foundFree = 0;
  1110. int currentSgd = 0;
  1111. while (currentSgd < 15 && !foundFree)
  1112. {
  1113. QString sgdNumber = QString::number(currentSgd);
  1114. if (sgdNumber.length() == 1)
  1115. {
  1116. sgdNumber.insert(0, "0");
  1117. }
  1118. sgdFileName = "SGTA500" % sgdNumber;
  1119. if (!QFile::exists(profileFolder % "/" % sgdFileName))
  1120. {
  1121. foundFree = true;
  1122. }
  1123. currentSgd++;
  1124. }
  1125. if (foundFree)
  1126. {
  1127. if (QFile::copy(sgdPath, profileFolder % "/" % sgdFileName))
  1128. {
  1129. savegame->setSavegameFileName(profileFolder % "/" % sgdFileName);
  1130. savegameLoaded(savegame, profileFolder % "/" % sgdFileName, true);
  1131. return true;
  1132. }
  1133. else
  1134. {
  1135. if (warn) QMessageBox::warning(this, tr("Import..."), tr("Failed to import the Savegame, can't copy the file into profile"));
  1136. return false;
  1137. }
  1138. }
  1139. else
  1140. {
  1141. if (warn) QMessageBox::warning(this, tr("Import..."), tr("Failed to import the Savegame, no Savegame slot is left"));
  1142. return false;
  1143. }
  1144. }
  1145. void ProfileInterface::profileWidgetSelected()
  1146. {
  1147. if (selectedWidgts == 0)
  1148. {
  1149. for (ProfileWidget *widget : widgets.keys())
  1150. {
  1151. widget->setSelectionMode(true);
  1152. }
  1153. }
  1154. selectedWidgts++;
  1155. }
  1156. void ProfileInterface::profileWidgetDeselected()
  1157. {
  1158. if (selectedWidgts == 1)
  1159. {
  1160. int scrollBarValue = ui->saProfile->verticalScrollBar()->value();
  1161. for (ProfileWidget *widget : widgets.keys())
  1162. {
  1163. if (contentMode != 2)
  1164. {
  1165. widget->setSelectionMode(false);
  1166. }
  1167. }
  1168. ui->saProfile->verticalScrollBar()->setValue(scrollBarValue);
  1169. }
  1170. selectedWidgts--;
  1171. }
  1172. void ProfileInterface::selectAllWidgets()
  1173. {
  1174. for (ProfileWidget *widget : widgets.keys())
  1175. {
  1176. widget->setSelected(true);
  1177. }
  1178. }
  1179. void ProfileInterface::deselectAllWidgets()
  1180. {
  1181. for (ProfileWidget *widget : widgets.keys())
  1182. {
  1183. widget->setSelected(false);
  1184. }
  1185. }
  1186. void ProfileInterface::exportSelected()
  1187. {
  1188. if (selectedWidgts != 0)
  1189. {
  1190. int exportCount = 0;
  1191. int exportPictures = 0;
  1192. int exportSavegames = 0;
  1193. bool pictureCopyEnabled = false;
  1194. bool pictureExportEnabled = false;
  1195. QSettings settings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  1196. settings.beginGroup("FileDialogs");
  1197. //bool dontUseNativeDialog = settings.value("DontUseNativeDialog", false).toBool();
  1198. settings.beginGroup("ExportDirectory");
  1199. QString exportDirectory = QFileDialog::getExistingDirectory(this, tr("Export selected..."), settings.value(profileName, profileFolder).toString());
  1200. if (exportDirectory != "")
  1201. {
  1202. settings.setValue(profileName, exportDirectory);
  1203. for (ProfileWidget *widget : widgets.keys())
  1204. {
  1205. if (widget->isSelected())
  1206. {
  1207. if (widget->getWidgetType() == "SnapmaticWidget")
  1208. {
  1209. exportPictures++;
  1210. }
  1211. else if (widget->getWidgetType() == "SavegameWidget")
  1212. {
  1213. exportSavegames++;
  1214. }
  1215. }
  1216. }
  1217. if (exportPictures != 0)
  1218. {
  1219. QInputDialog inputDialog;
  1220. QStringList inputDialogItems;
  1221. inputDialogItems << tr("JPG pictures and GTA Snapmatic");
  1222. inputDialogItems << tr("JPG pictures only");
  1223. inputDialogItems << tr("GTA Snapmatic only");
  1224. QString ExportPreSpan;
  1225. QString ExportPostSpan;
  1226. #ifdef GTA5SYNC_WIN
  1227. ExportPreSpan = "<span style=\"color: #003399; font-size: 12pt\">";
  1228. ExportPostSpan = "</span>";
  1229. #else
  1230. ExportPreSpan = "<span style=\"font-weight: bold\">";
  1231. ExportPostSpan = "</span>";
  1232. #endif
  1233. bool itemSelected = false;
  1234. QString selectedItem = inputDialog.getItem(this, tr("Export selected..."), tr("%1Export Snapmatic pictures%2<br><br>JPG pictures make it possible to open the picture with a Image Viewer<br>GTA Snapmatic make it possible to import the picture into the game<br><br>Export as:").arg(ExportPreSpan, ExportPostSpan), inputDialogItems, 0, false, &itemSelected, inputDialog.windowFlags()^Qt::WindowContextHelpButtonHint);
  1235. if (itemSelected)
  1236. {
  1237. if (selectedItem == tr("JPG pictures and GTA Snapmatic"))
  1238. {
  1239. pictureExportEnabled = true;
  1240. pictureCopyEnabled = true;
  1241. }
  1242. else if (selectedItem == tr("JPG pictures only"))
  1243. {
  1244. pictureExportEnabled = true;
  1245. }
  1246. else if (selectedItem == tr("GTA Snapmatic only"))
  1247. {
  1248. pictureCopyEnabled = true;
  1249. }
  1250. else
  1251. {
  1252. pictureExportEnabled = true;
  1253. pictureCopyEnabled = true;
  1254. }
  1255. }
  1256. else
  1257. {
  1258. // Don't export anymore when any Cancel button got clicked
  1259. settings.endGroup();
  1260. settings.endGroup();
  1261. return;
  1262. }
  1263. }
  1264. // Counting the exports together
  1265. exportCount = exportCount + exportSavegames;
  1266. if (pictureExportEnabled && pictureCopyEnabled)
  1267. {
  1268. int exportPictures2 = exportPictures * 2;
  1269. exportCount = exportCount + exportPictures2;
  1270. }
  1271. else
  1272. {
  1273. exportCount = exportCount + exportPictures;
  1274. }
  1275. QProgressDialog pbDialog(this);
  1276. pbDialog.setWindowFlags(pbDialog.windowFlags()^Qt::WindowContextHelpButtonHint^Qt::WindowCloseButtonHint);
  1277. pbDialog.setWindowTitle(tr("Export selected..."));
  1278. pbDialog.setLabelText(tr("Initialising export..."));
  1279. pbDialog.setRange(0, exportCount);
  1280. QList<QPushButton*> pbBtn = pbDialog.findChildren<QPushButton*>();
  1281. pbBtn.at(0)->setDisabled(true);
  1282. QList<QProgressBar*> pbBar = pbDialog.findChildren<QProgressBar*>();
  1283. pbBar.at(0)->setTextVisible(false);
  1284. ExportThread *exportThread = new ExportThread(widgets, exportDirectory, pictureCopyEnabled, pictureExportEnabled, exportCount);
  1285. QObject::connect(exportThread, SIGNAL(exportStringUpdate(QString)), &pbDialog, SLOT(setLabelText(QString)));
  1286. QObject::connect(exportThread, SIGNAL(exportProgressUpdate(int)), &pbDialog, SLOT(setValue(int)));
  1287. QObject::connect(exportThread, SIGNAL(exportFinished()), &pbDialog, SLOT(close()));
  1288. exportThread->start();
  1289. pbDialog.setAutoClose(false);
  1290. pbDialog.exec();
  1291. QStringList getFailedSavegames = exportThread->getFailedSavegames();
  1292. QStringList getFailedCopyPictures = exportThread->getFailedCopyPictures();
  1293. QStringList getFailedExportPictures = exportThread->getFailedExportPictures();
  1294. QString errorStr;
  1295. QStringList errorList;
  1296. errorList << getFailedExportPictures;
  1297. errorList << getFailedCopyPictures;
  1298. errorList << getFailedSavegames;
  1299. for (QString curErrorStr : errorList)
  1300. {
  1301. errorStr += ", " % curErrorStr;
  1302. }
  1303. if (errorStr != "")
  1304. {
  1305. errorStr.remove(0, 2);
  1306. QMessageBox::warning(this, tr("Export selected..."), tr("Export failed with...\n\n%1").arg(errorStr));
  1307. }
  1308. if (exportThread->isFinished())
  1309. {
  1310. delete exportThread;
  1311. }
  1312. else
  1313. {
  1314. QEventLoop threadFinishLoop;
  1315. QObject::connect(exportThread, SIGNAL(finished()), &threadFinishLoop, SLOT(quit()));
  1316. threadFinishLoop.exec();
  1317. delete exportThread;
  1318. }
  1319. }
  1320. settings.endGroup();
  1321. settings.endGroup();
  1322. }
  1323. else
  1324. {
  1325. QMessageBox::information(this, tr("Export selected..."), tr("No Snapmatic pictures or Savegames files are selected"));
  1326. }
  1327. }
  1328. void ProfileInterface::deleteSelected()
  1329. {
  1330. if (selectedWidgts != 0)
  1331. {
  1332. if (QMessageBox::Yes == QMessageBox::warning(this, tr("Remove selected"), tr("You really want remove the selected Snapmatic picutres and Savegame files?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No))
  1333. {
  1334. for (ProfileWidget *widget : widgets.keys())
  1335. {
  1336. if (widget->isSelected())
  1337. {
  1338. if (widget->getWidgetType() == "SnapmaticWidget")
  1339. {
  1340. SnapmaticWidget *picWidget = qobject_cast<SnapmaticWidget*>(widget);
  1341. if (picWidget->getPicture()->deletePictureFile())
  1342. {
  1343. pictureDeleted(picWidget);
  1344. }
  1345. }
  1346. else if (widget->getWidgetType() == "SavegameWidget")
  1347. {
  1348. SavegameWidget *sgdWidget = qobject_cast<SavegameWidget*>(widget);
  1349. SavegameData *savegame = sgdWidget->getSavegame();
  1350. QString fileName = savegame->getSavegameFileName();
  1351. if (!QFile::exists(fileName) || QFile::remove(fileName))
  1352. {
  1353. savegameDeleted(sgdWidget);
  1354. }
  1355. }
  1356. }
  1357. }
  1358. if (selectedWidgts != 0)
  1359. {
  1360. QMessageBox::warning(this, tr("Remove selected"), tr("Failed to remove all selected Snapmatic pictures and/or Savegame files"));
  1361. }
  1362. }
  1363. }
  1364. else
  1365. {
  1366. QMessageBox::information(this, tr("Remove selected"), tr("No Snapmatic pictures or Savegames files are selected"));
  1367. }
  1368. }
  1369. void ProfileInterface::importFiles()
  1370. {
  1371. on_cmdImport_clicked();
  1372. }
  1373. void ProfileInterface::settingsApplied(int _contentMode, bool languageChanged)
  1374. {
  1375. if (languageChanged) retranslateUi();
  1376. contentMode = _contentMode;
  1377. if (contentMode == 2)
  1378. {
  1379. for (ProfileWidget *widget : widgets.keys())
  1380. {
  1381. widget->setSelectionMode(true);
  1382. widget->setContentMode(contentMode);
  1383. if (languageChanged) widget->retranslate();
  1384. }
  1385. }
  1386. else
  1387. {
  1388. for (ProfileWidget *widget : widgets.keys())
  1389. {
  1390. if (selectedWidgts == 0)
  1391. {
  1392. widget->setSelectionMode(false);
  1393. }
  1394. widget->setContentMode(contentMode);
  1395. if (languageChanged) widget->retranslate();
  1396. }
  1397. }
  1398. #ifdef Q_OS_MAC
  1399. // DPI calculation
  1400. qreal screenRatio = AppEnv::screenRatio();
  1401. if (QApplication::style()->objectName() == "macintosh")
  1402. {
  1403. ui->hlButtons->setSpacing(6 * screenRatio);
  1404. ui->hlButtons->setContentsMargins(9 * screenRatio, 15 * screenRatio, 15 * screenRatio, 17 * screenRatio);
  1405. }
  1406. else
  1407. {
  1408. ui->hlButtons->setSpacing(6 * screenRatio);
  1409. ui->hlButtons->setContentsMargins(9 * screenRatio, 9 * screenRatio, 9 * screenRatio, 9 * screenRatio);
  1410. }
  1411. #endif
  1412. }
  1413. void ProfileInterface::enableSelected()
  1414. {
  1415. QList<SnapmaticWidget*> snapmaticWidgets;
  1416. for (ProfileWidget *widget : widgets.keys())
  1417. {
  1418. if (widget->isSelected())
  1419. {
  1420. if (widget->getWidgetType() == "SnapmaticWidget")
  1421. {
  1422. SnapmaticWidget *snapmaticWidget = qobject_cast<SnapmaticWidget*>(widget);
  1423. snapmaticWidgets += snapmaticWidget;
  1424. }
  1425. }
  1426. }
  1427. if (snapmaticWidgets.isEmpty())
  1428. {
  1429. QMessageBox::information(this, QApplication::translate("UserInterface", "Show In-game"), QApplication::translate("ProfileInterface", "No Snapmatic pictures are selected"));
  1430. return;
  1431. }
  1432. QStringList fails;
  1433. for (SnapmaticWidget *widget : snapmaticWidgets)
  1434. {
  1435. SnapmaticPicture *picture = widget->getPicture();
  1436. if (!widget->makePictureVisible())
  1437. {
  1438. fails << QString("%1 [%2]").arg(picture->getPictureTitle(), picture->getPictureString());
  1439. }
  1440. }
  1441. if (!fails.isEmpty())
  1442. {
  1443. QMessageBox::warning(this, QApplication::translate("UserInterface", "Show In-game"), QApplication::translate("ProfileInterface", "%1 failed with...\n\n%2", "Action failed with...").arg(QApplication::translate("UserInterface", "Show In-game"), fails.join(", ")));
  1444. }
  1445. }
  1446. void ProfileInterface::disableSelected()
  1447. {
  1448. QList<SnapmaticWidget*> snapmaticWidgets;
  1449. for (ProfileWidget *widget : widgets.keys())
  1450. {
  1451. if (widget->isSelected())
  1452. {
  1453. if (widget->getWidgetType() == "SnapmaticWidget")
  1454. {
  1455. SnapmaticWidget *snapmaticWidget = qobject_cast<SnapmaticWidget*>(widget);
  1456. snapmaticWidgets += snapmaticWidget;
  1457. }
  1458. }
  1459. }
  1460. if (snapmaticWidgets.isEmpty())
  1461. {
  1462. QMessageBox::information(this, QApplication::translate("UserInterface", "Hide In-game"), QApplication::translate("ProfileInterface", "No Snapmatic pictures are selected"));
  1463. return;
  1464. }
  1465. QStringList fails;
  1466. for (SnapmaticWidget *widget : snapmaticWidgets)
  1467. {
  1468. SnapmaticPicture *picture = widget->getPicture();
  1469. if (!widget->makePictureHidden())
  1470. {
  1471. fails << QString("%1 [%2]").arg(picture->getPictureTitle(), picture->getPictureString());
  1472. }
  1473. }
  1474. if (!fails.isEmpty())
  1475. {
  1476. QMessageBox::warning(this, QApplication::translate("UserInterface", "Hide In-game"), QApplication::translate("ProfileInterface", "%1 failed with...\n\n%2", "Action failed with...").arg(QApplication::translate("UserInterface", "Hide In-game"), fails.join(", ")));
  1477. }
  1478. }
  1479. int ProfileInterface::selectedWidgets()
  1480. {
  1481. return selectedWidgts;
  1482. }
  1483. void ProfileInterface::contextMenuTriggeredPIC(QContextMenuEvent *ev)
  1484. {
  1485. SnapmaticWidget *picWidget = qobject_cast<SnapmaticWidget*>(sender());
  1486. if (picWidget != previousWidget)
  1487. {
  1488. if (previousWidget != nullptr)
  1489. {
  1490. previousWidget->setStyleSheet(QLatin1String(""));
  1491. }
  1492. picWidget->setStyleSheet(QString("QFrame#SnapmaticFrame{background-color: rgb(%1, %2, %3)}QLabel#labPicStr{color: rgb(%4, %5, %6)}").arg(QString::number(highlightBackColor.red()), QString::number(highlightBackColor.green()), QString::number(highlightBackColor.blue()), QString::number(highlightTextColor.red()), QString::number(highlightTextColor.green()), QString::number(highlightTextColor.blue())));
  1493. previousWidget = picWidget;
  1494. }
  1495. QMenu contextMenu(picWidget);
  1496. QMenu editMenu(SnapmaticWidget::tr("Edi&t"), picWidget);
  1497. if (picWidget->isHidden())
  1498. {
  1499. editMenu.addAction(SnapmaticWidget::tr("Show &In-game"), picWidget, SLOT(makePictureVisibleSlot()));
  1500. }
  1501. else
  1502. {
  1503. editMenu.addAction(SnapmaticWidget::tr("Hide &In-game"), picWidget, SLOT(makePictureHiddenSlot()));
  1504. }
  1505. editMenu.addAction(PictureDialog::tr("&Edit Properties..."), picWidget, SLOT(editSnapmaticProperties()));
  1506. editMenu.addAction(PictureDialog::tr("&Overwrite Image..."), picWidget, SLOT(editSnapmaticImage()));
  1507. editMenu.addSeparator();
  1508. editMenu.addAction(PictureDialog::tr("Open &Map Viewer..."), picWidget, SLOT(openMapViewer()));
  1509. editMenu.addAction(PictureDialog::tr("Open &JSON Editor..."), picWidget, SLOT(editSnapmaticRawJson()));
  1510. QMenu exportMenu(SnapmaticWidget::tr("&Export"), this);
  1511. exportMenu.addAction(PictureDialog::tr("Export as &Picture..."), picWidget, SLOT(on_cmdExport_clicked()));
  1512. exportMenu.addAction(PictureDialog::tr("Export as &Snapmatic..."), picWidget, SLOT(on_cmdCopy_clicked()));
  1513. contextMenu.addAction(SnapmaticWidget::tr("&View"), picWidget, SLOT(on_cmdView_clicked()));
  1514. contextMenu.addMenu(&editMenu);
  1515. contextMenu.addMenu(&exportMenu);
  1516. contextMenu.addAction(SnapmaticWidget::tr("&Remove"), picWidget, SLOT(on_cmdDelete_clicked()));
  1517. contextMenu.addSeparator();
  1518. if (!picWidget->isSelected()) { contextMenu.addAction(SnapmaticWidget::tr("&Select"), picWidget, SLOT(pictureSelected())); }
  1519. if (picWidget->isSelected()) { contextMenu.addAction(SnapmaticWidget::tr("&Deselect"), picWidget, SLOT(pictureSelected())); }
  1520. if (selectedWidgets() != widgets.count())
  1521. {
  1522. contextMenu.addAction(SnapmaticWidget::tr("Select &All"), picWidget, SLOT(selectAllWidgets()), QKeySequence::fromString("Ctrl+A"));
  1523. }
  1524. if (selectedWidgets() != 0)
  1525. {
  1526. contextMenu.addAction(SnapmaticWidget::tr("&Deselect All"), picWidget, SLOT(deselectAllWidgets()), QKeySequence::fromString("Ctrl+D"));
  1527. }
  1528. contextMenuOpened = true;
  1529. contextMenu.exec(ev->globalPos());
  1530. contextMenuOpened = false;
  1531. hoverProfileWidgetCheck();
  1532. }
  1533. void ProfileInterface::contextMenuTriggeredSGD(QContextMenuEvent *ev)
  1534. {
  1535. SavegameWidget *sgdWidget = qobject_cast<SavegameWidget*>(sender());
  1536. if (sgdWidget != previousWidget)
  1537. {
  1538. if (previousWidget != nullptr)
  1539. {
  1540. previousWidget->setStyleSheet(QLatin1String(""));
  1541. }
  1542. sgdWidget->setStyleSheet(QString("QFrame#SavegameFrame{background-color: rgb(%1, %2, %3)}QLabel#labSavegameStr{color: rgb(%4, %5, %6)}").arg(QString::number(highlightBackColor.red()), QString::number(highlightBackColor.green()), QString::number(highlightBackColor.blue()), QString::number(highlightTextColor.red()), QString::number(highlightTextColor.green()), QString::number(highlightTextColor.blue())));
  1543. previousWidget = sgdWidget;
  1544. }
  1545. QMenu contextMenu(sgdWidget);
  1546. contextMenu.addAction(SavegameWidget::tr("&View"), sgdWidget, SLOT(on_cmdView_clicked()));
  1547. contextMenu.addAction(SavegameWidget::tr("&Export"), sgdWidget, SLOT(on_cmdCopy_clicked()));
  1548. contextMenu.addAction(SavegameWidget::tr("&Remove"), sgdWidget, SLOT(on_cmdDelete_clicked()));
  1549. contextMenu.addSeparator();
  1550. if (!sgdWidget->isSelected()) { contextMenu.addAction(SavegameWidget::tr("&Select"), sgdWidget, SLOT(savegameSelected())); }
  1551. if (sgdWidget->isSelected()) { contextMenu.addAction(SavegameWidget::tr("&Deselect"), sgdWidget, SLOT(savegameSelected())); }
  1552. if (selectedWidgets() != widgets.count())
  1553. {
  1554. contextMenu.addAction(SavegameWidget::tr("Select &All"), sgdWidget, SLOT(selectAllWidgets()), QKeySequence::fromString("Ctrl+A"));
  1555. }
  1556. if (selectedWidgets() != 0)
  1557. {
  1558. contextMenu.addAction(SavegameWidget::tr("&Deselect All"), sgdWidget, SLOT(deselectAllWidgets()), QKeySequence::fromString("Ctrl+D"));
  1559. }
  1560. contextMenuOpened = true;
  1561. contextMenu.exec(ev->globalPos());
  1562. contextMenuOpened = false;
  1563. hoverProfileWidgetCheck();
  1564. }
  1565. void ProfileInterface::on_saProfileContent_dropped(const QMimeData *mimeData)
  1566. {
  1567. if (!mimeData) return;
  1568. if (mimeData->hasImage())
  1569. {
  1570. QImage *snapmaticImage = new QImage(qvariant_cast<QImage>(mimeData->imageData()));
  1571. importImage(snapmaticImage, QDateTime::currentDateTime());
  1572. }
  1573. else if (mimeData->hasUrls())
  1574. {
  1575. importUrls(mimeData);
  1576. }
  1577. }
  1578. void ProfileInterface::retranslateUi()
  1579. {
  1580. ui->retranslateUi(this);
  1581. QString appVersion = GTA5SYNC_APPVER;
  1582. #ifndef GTA5SYNC_BUILDTYPE_REL
  1583. #ifdef GTA5SYNC_COMMIT
  1584. if (!appVersion.contains("-")) { appVersion = appVersion % "-" % GTA5SYNC_COMMIT; }
  1585. #endif
  1586. #endif
  1587. ui->labVersion->setText(QString("%1 %2").arg(GTA5SYNC_APPSTR, appVersion));
  1588. }
  1589. bool ProfileInterface::eventFilter(QObject *watched, QEvent *event)
  1590. {
  1591. if (event->type() == QEvent::KeyPress)
  1592. {
  1593. if (isProfileLoaded)
  1594. {
  1595. QKeyEvent *keyEvent = dynamic_cast<QKeyEvent*>(event);
  1596. switch (keyEvent->key())
  1597. {
  1598. case Qt::Key_V:
  1599. if (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier) && !QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier))
  1600. {
  1601. const QMimeData *clipboardData = QApplication::clipboard()->mimeData();
  1602. if (clipboardData->hasImage())
  1603. {
  1604. QImage *snapmaticImage = new QImage(qvariant_cast<QImage>(clipboardData->imageData()));
  1605. importImage(snapmaticImage, QDateTime::currentDateTime());
  1606. }
  1607. else if (clipboardData->hasUrls())
  1608. {
  1609. if (clipboardData->urls().length() >= 2)
  1610. {
  1611. importUrls(clipboardData);
  1612. }
  1613. else if (clipboardData->urls().length() == 1)
  1614. {
  1615. QUrl clipboardUrl = clipboardData->urls().at(0);
  1616. if (clipboardUrl.isLocalFile())
  1617. {
  1618. importFile(clipboardUrl.toLocalFile(), QDateTime::currentDateTime(), true);
  1619. }
  1620. else
  1621. {
  1622. importRemote(clipboardUrl);
  1623. }
  1624. }
  1625. }
  1626. else if (clipboardData->hasText())
  1627. {
  1628. QUrl clipboardUrl = QUrl::fromUserInput(clipboardData->text());
  1629. if (clipboardUrl.isValid())
  1630. {
  1631. if (clipboardUrl.isLocalFile())
  1632. {
  1633. importFile(clipboardUrl.toLocalFile(), QDateTime::currentDateTime(), true);
  1634. }
  1635. else
  1636. {
  1637. importRemote(clipboardUrl);
  1638. }
  1639. }
  1640. }
  1641. }
  1642. }
  1643. }
  1644. }
  1645. else if (event->type() == QEvent::MouseMove)
  1646. {
  1647. if ((watched->objectName() == "SavegameWidget" || watched->objectName() == "SnapmaticWidget") && isProfileLoaded)
  1648. {
  1649. ProfileWidget *pWidget = qobject_cast<ProfileWidget*>(watched);
  1650. if (pWidget->underMouse())
  1651. {
  1652. bool styleSheetChanged = false;
  1653. if (pWidget->getWidgetType() == "SnapmaticWidget")
  1654. {
  1655. if (pWidget != previousWidget)
  1656. {
  1657. pWidget->setStyleSheet(QString("QFrame#SnapmaticFrame{background-color: rgb(%1, %2, %3)}QLabel#labPicStr{color: rgb(%4, %5, %6)}").arg(QString::number(highlightBackColor.red()), QString::number(highlightBackColor.green()), QString::number(highlightBackColor.blue()), QString::number(highlightTextColor.red()), QString::number(highlightTextColor.green()), QString::number(highlightTextColor.blue())));
  1658. styleSheetChanged = true;
  1659. }
  1660. }
  1661. else if (pWidget->getWidgetType() == "SavegameWidget")
  1662. {
  1663. if (pWidget != previousWidget)
  1664. {
  1665. pWidget->setStyleSheet(QString("QFrame#SavegameFrame{background-color: rgb(%1, %2, %3)}QLabel#labSavegameStr{color: rgb(%4, %5, %6)}").arg(QString::number(highlightBackColor.red()), QString::number(highlightBackColor.green()), QString::number(highlightBackColor.blue()), QString::number(highlightTextColor.red()), QString::number(highlightTextColor.green()), QString::number(highlightTextColor.blue())));
  1666. styleSheetChanged = true;
  1667. }
  1668. }
  1669. if (styleSheetChanged)
  1670. {
  1671. if (previousWidget != nullptr)
  1672. {
  1673. previousWidget->setStyleSheet(QLatin1String(""));
  1674. }
  1675. previousWidget = pWidget;
  1676. }
  1677. }
  1678. return true;
  1679. }
  1680. }
  1681. else if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease || event->type() == QEvent::WindowActivate)
  1682. {
  1683. if ((watched->objectName() == "SavegameWidget" || watched->objectName() == "SnapmaticWidget") && isProfileLoaded)
  1684. {
  1685. ProfileWidget *pWidget = nullptr;
  1686. for (ProfileWidget *widget : widgets.keys())
  1687. {
  1688. QPoint mousePos = widget->mapFromGlobal(QCursor::pos());
  1689. if (widget->rect().contains(mousePos))
  1690. {
  1691. pWidget = widget;
  1692. break;
  1693. }
  1694. }
  1695. if (pWidget != nullptr)
  1696. {
  1697. bool styleSheetChanged = false;
  1698. if (pWidget->getWidgetType() == "SnapmaticWidget")
  1699. {
  1700. if (pWidget != previousWidget)
  1701. {
  1702. pWidget->setStyleSheet(QString("QFrame#SnapmaticFrame{background-color: rgb(%1, %2, %3)}QLabel#labPicStr{color: rgb(%4, %5, %6)}").arg(QString::number(highlightBackColor.red()), QString::number(highlightBackColor.green()), QString::number(highlightBackColor.blue()), QString::number(highlightTextColor.red()), QString::number(highlightTextColor.green()), QString::number(highlightTextColor.blue())));
  1703. styleSheetChanged = true;
  1704. }
  1705. }
  1706. else if (pWidget->getWidgetType() == "SavegameWidget")
  1707. {
  1708. if (pWidget != previousWidget)
  1709. {
  1710. pWidget->setStyleSheet(QString("QFrame#SavegameFrame{background-color: rgb(%1, %2, %3)}QLabel#labSavegameStr{color: rgb(%4, %5, %6)}").arg(QString::number(highlightBackColor.red()), QString::number(highlightBackColor.green()), QString::number(highlightBackColor.blue()), QString::number(highlightTextColor.red()), QString::number(highlightTextColor.green()), QString::number(highlightTextColor.blue())));
  1711. styleSheetChanged = true;
  1712. }
  1713. }
  1714. if (styleSheetChanged)
  1715. {
  1716. if (previousWidget != nullptr)
  1717. {
  1718. previousWidget->setStyleSheet(QLatin1String(""));
  1719. }
  1720. previousWidget = pWidget;
  1721. }
  1722. }
  1723. }
  1724. }
  1725. else if (event->type() == QEvent::WindowDeactivate && isProfileLoaded)
  1726. {
  1727. if (previousWidget != nullptr)
  1728. {
  1729. previousWidget->setStyleSheet(QLatin1String(""));
  1730. previousWidget = nullptr;
  1731. }
  1732. }
  1733. else if (event->type() == QEvent::Leave && isProfileLoaded && !contextMenuOpened)
  1734. {
  1735. if (watched->objectName() == "SavegameWidget" || watched->objectName() == "SnapmaticWidget")
  1736. {
  1737. ProfileWidget *pWidget = qobject_cast<ProfileWidget*>(watched);
  1738. QPoint mousePos = pWidget->mapFromGlobal(QCursor::pos());
  1739. if (!pWidget->geometry().contains(mousePos))
  1740. {
  1741. if (previousWidget != nullptr)
  1742. {
  1743. previousWidget->setStyleSheet(QLatin1String(""));
  1744. previousWidget = nullptr;
  1745. }
  1746. }
  1747. }
  1748. else if (watched->objectName() == "ProfileInterface")
  1749. {
  1750. if (previousWidget != nullptr)
  1751. {
  1752. previousWidget->setStyleSheet(QLatin1String(""));
  1753. previousWidget = nullptr;
  1754. }
  1755. }
  1756. }
  1757. return false;
  1758. }
  1759. void ProfileInterface::hoverProfileWidgetCheck()
  1760. {
  1761. ProfileWidget *pWidget = nullptr;
  1762. for (ProfileWidget *widget : widgets.keys())
  1763. {
  1764. if (widget->underMouse())
  1765. {
  1766. pWidget = widget;
  1767. break;
  1768. }
  1769. }
  1770. if (pWidget != nullptr)
  1771. {
  1772. bool styleSheetChanged = false;
  1773. if (pWidget->getWidgetType() == "SnapmaticWidget")
  1774. {
  1775. if (pWidget != previousWidget)
  1776. {
  1777. pWidget->setStyleSheet(QString("QFrame#SnapmaticFrame{background-color: rgb(%1, %2, %3)}QLabel#labPicStr{color: rgb(%4, %5, %6)}").arg(QString::number(highlightBackColor.red()), QString::number(highlightBackColor.green()), QString::number(highlightBackColor.blue()), QString::number(highlightTextColor.red()), QString::number(highlightTextColor.green()), QString::number(highlightTextColor.blue())));
  1778. styleSheetChanged = true;
  1779. }
  1780. }
  1781. else if (pWidget->getWidgetType() == "SavegameWidget")
  1782. {
  1783. if (pWidget != previousWidget)
  1784. {
  1785. pWidget->setStyleSheet(QString("QFrame#SavegameFrame{background-color: rgb(%1, %2, %3)}QLabel#labSavegameStr{color: rgb(%4, %5, %6)}").arg(QString::number(highlightBackColor.red()), QString::number(highlightBackColor.green()), QString::number(highlightBackColor.blue()), QString::number(highlightTextColor.red()), QString::number(highlightTextColor.green()), QString::number(highlightTextColor.blue())));
  1786. styleSheetChanged = true;
  1787. }
  1788. }
  1789. if (styleSheetChanged)
  1790. {
  1791. if (previousWidget != nullptr)
  1792. {
  1793. previousWidget->setStyleSheet(QLatin1String(""));
  1794. }
  1795. previousWidget = pWidget;
  1796. }
  1797. }
  1798. else
  1799. {
  1800. if (previousWidget != nullptr)
  1801. {
  1802. previousWidget->setStyleSheet(QLatin1String(""));
  1803. previousWidget = nullptr;
  1804. }
  1805. }
  1806. }
  1807. void ProfileInterface::updatePalette()
  1808. {
  1809. QPalette palette;
  1810. QColor baseColor = palette.base().color();
  1811. highlightBackColor = palette.highlight().color();
  1812. highlightTextColor = palette.highlightedText().color();
  1813. ui->saProfile->setStyleSheet(QString("QWidget#saProfileContent{background-color: rgb(%1, %2, %3)}").arg(QString::number(baseColor.red()), QString::number(baseColor.green()), QString::number(baseColor.blue())));
  1814. if (previousWidget != nullptr)
  1815. {
  1816. if (previousWidget->getWidgetType() == "SnapmaticWidget")
  1817. {
  1818. previousWidget->setStyleSheet(QString("QFrame#SnapmaticFrame{background-color: rgb(%1, %2, %3)}QLabel#labPicStr{color: rgb(%4, %5, %6)}").arg(QString::number(highlightBackColor.red()), QString::number(highlightBackColor.green()), QString::number(highlightBackColor.blue()), QString::number(highlightTextColor.red()), QString::number(highlightTextColor.green()), QString::number(highlightTextColor.blue())));
  1819. }
  1820. else if (previousWidget->getWidgetType() == "SavegameWidget")
  1821. {
  1822. previousWidget->setStyleSheet(QString("QFrame#SavegameFrame{background-color: rgb(%1, %2, %3)}QLabel#labSavegameStr{color: rgb(%4, %5, %6)}").arg(QString::number(highlightBackColor.red()), QString::number(highlightBackColor.green()), QString::number(highlightBackColor.blue()), QString::number(highlightTextColor.red()), QString::number(highlightTextColor.green()), QString::number(highlightTextColor.blue())));
  1823. }
  1824. }
  1825. }
  1826. bool ProfileInterface::isSupportedImageFile(QString selectedFileName)
  1827. {
  1828. for (QByteArray imageFormat : QImageReader::supportedImageFormats())
  1829. {
  1830. QString imageFormatStr = QString(".") % QString::fromUtf8(imageFormat).toLower();
  1831. if (selectedFileName.length() >= imageFormatStr.length() && selectedFileName.toLower().right(imageFormatStr.length()) == imageFormatStr)
  1832. {
  1833. return true;
  1834. }
  1835. }
  1836. return false;
  1837. }
  1838. void ProfileInterface::massTool(MassTool tool)
  1839. {
  1840. switch(tool)
  1841. {
  1842. case MassTool::Qualify:
  1843. {
  1844. QList<SnapmaticWidget*> snapmaticWidgets;
  1845. for (ProfileWidget *widget : widgets.keys())
  1846. {
  1847. if (widget->isSelected())
  1848. {
  1849. if (widget->getWidgetType() == "SnapmaticWidget")
  1850. {
  1851. SnapmaticWidget *snapmaticWidget = qobject_cast<SnapmaticWidget*>(widget);
  1852. snapmaticWidgets += snapmaticWidget;
  1853. }
  1854. }
  1855. }
  1856. if (snapmaticWidgets.isEmpty())
  1857. {
  1858. QMessageBox::information(this, tr("Qualify as Avatar"), tr("No Snapmatic pictures are selected"));
  1859. return;
  1860. }
  1861. // Prepare Progress
  1862. int maximumId = snapmaticWidgets.length();
  1863. int overallId = 0;
  1864. QProgressDialog pbDialog(this);
  1865. pbDialog.setWindowFlags(pbDialog.windowFlags()^Qt::WindowContextHelpButtonHint^Qt::WindowCloseButtonHint);
  1866. pbDialog.setWindowTitle(tr("Patch selected..."));
  1867. pbDialog.setLabelText(tr("Patch file %1 of %2 files").arg(QString::number(1), QString::number(maximumId)));
  1868. pbDialog.setRange(1, maximumId);
  1869. pbDialog.setValue(1);
  1870. pbDialog.setModal(true);
  1871. QList<QPushButton*> pbBtn = pbDialog.findChildren<QPushButton*>();
  1872. pbBtn.at(0)->setDisabled(true);
  1873. QList<QProgressBar*> pbBar = pbDialog.findChildren<QProgressBar*>();
  1874. pbBar.at(0)->setTextVisible(false);
  1875. pbDialog.setAutoClose(false);
  1876. pbDialog.show();
  1877. // Begin Progress
  1878. QStringList fails;
  1879. for (SnapmaticWidget *snapmaticWidget : snapmaticWidgets)
  1880. {
  1881. // Update Progress
  1882. overallId++;
  1883. pbDialog.setValue(overallId);
  1884. pbDialog.setLabelText(tr("Patch file %1 of %2 files").arg(QString::number(overallId), QString::number(maximumId)));
  1885. SnapmaticPicture *picture = snapmaticWidget->getPicture();
  1886. SnapmaticProperties snapmaticProperties = picture->getSnapmaticProperties();
  1887. snapmaticProperties.isSelfie = true;
  1888. snapmaticProperties.isMug = false;
  1889. snapmaticProperties.isFromRSEditor = false;
  1890. snapmaticProperties.isFromDirector = false;
  1891. snapmaticProperties.isMeme = false;
  1892. QString currentFilePath = picture->getPictureFilePath();
  1893. QString originalFilePath = picture->getOriginalPictureFilePath();
  1894. QString backupFileName = originalFilePath % ".bak";
  1895. if (!QFile::exists(backupFileName))
  1896. {
  1897. QFile::copy(currentFilePath, backupFileName);
  1898. }
  1899. SnapmaticProperties fallbackProperties = picture->getSnapmaticProperties();
  1900. picture->setSnapmaticProperties(snapmaticProperties);
  1901. if (!picture->exportPicture(currentFilePath))
  1902. {
  1903. picture->setSnapmaticProperties(fallbackProperties);
  1904. fails << QString("%1 [%2]").arg(picture->getPictureTitle(), picture->getPictureString());
  1905. }
  1906. else
  1907. {
  1908. picture->emitUpdate();
  1909. qApp->processEvents();
  1910. }
  1911. }
  1912. pbDialog.close();
  1913. if (!fails.isEmpty())
  1914. {
  1915. QMessageBox::warning(this, tr("Qualify as Avatar"), tr("%1 failed with...\n\n%2", "Action failed with...").arg(tr("Qualify", "%1 failed with..."), fails.join(", ")));
  1916. }
  1917. }
  1918. break;
  1919. case MassTool::Players:
  1920. {
  1921. QList<SnapmaticWidget*> snapmaticWidgets;
  1922. for (ProfileWidget *widget : widgets.keys())
  1923. {
  1924. if (widget->isSelected())
  1925. {
  1926. if (widget->getWidgetType() == "SnapmaticWidget")
  1927. {
  1928. SnapmaticWidget *snapmaticWidget = qobject_cast<SnapmaticWidget*>(widget);
  1929. snapmaticWidgets += snapmaticWidget;
  1930. }
  1931. }
  1932. }
  1933. if (snapmaticWidgets.isEmpty())
  1934. {
  1935. QMessageBox::information(this, tr("Change Players..."), tr("No Snapmatic pictures are selected"));
  1936. return;
  1937. }
  1938. QStringList players;
  1939. if (snapmaticWidgets.length() == 1)
  1940. {
  1941. players = snapmaticWidgets.at(0)->getPicture()->getSnapmaticProperties().playersList;
  1942. }
  1943. PlayerListDialog *playerListDialog = new PlayerListDialog(players, profileDB, this);
  1944. playerListDialog->setModal(true);
  1945. playerListDialog->show();
  1946. playerListDialog->exec();
  1947. if (!playerListDialog->isListUpdated())
  1948. {
  1949. return;
  1950. }
  1951. players = playerListDialog->getPlayerList();
  1952. delete playerListDialog;
  1953. // Prepare Progress
  1954. int maximumId = snapmaticWidgets.length();
  1955. int overallId = 0;
  1956. QProgressDialog pbDialog(this);
  1957. pbDialog.setWindowFlags(pbDialog.windowFlags()^Qt::WindowContextHelpButtonHint^Qt::WindowCloseButtonHint);
  1958. pbDialog.setWindowTitle(tr("Patch selected..."));
  1959. pbDialog.setLabelText(tr("Patch file %1 of %2 files").arg(QString::number(1), QString::number(maximumId)));
  1960. pbDialog.setRange(1, maximumId);
  1961. pbDialog.setValue(1);
  1962. pbDialog.setModal(true);
  1963. QList<QPushButton*> pbBtn = pbDialog.findChildren<QPushButton*>();
  1964. pbBtn.at(0)->setDisabled(true);
  1965. QList<QProgressBar*> pbBar = pbDialog.findChildren<QProgressBar*>();
  1966. pbBar.at(0)->setTextVisible(false);
  1967. pbDialog.setAutoClose(false);
  1968. pbDialog.show();
  1969. // Begin Progress
  1970. QStringList fails;
  1971. for (SnapmaticWidget *snapmaticWidget : snapmaticWidgets)
  1972. {
  1973. // Update Progress
  1974. overallId++;
  1975. pbDialog.setValue(overallId);
  1976. pbDialog.setLabelText(tr("Patch file %1 of %2 files").arg(QString::number(overallId), QString::number(maximumId)));
  1977. SnapmaticPicture *picture = snapmaticWidget->getPicture();
  1978. SnapmaticProperties snapmaticProperties = picture->getSnapmaticProperties();
  1979. snapmaticProperties.playersList = players;
  1980. QString currentFilePath = picture->getPictureFilePath();
  1981. QString originalFilePath = picture->getOriginalPictureFilePath();
  1982. QString backupFileName = originalFilePath % ".bak";
  1983. if (!QFile::exists(backupFileName))
  1984. {
  1985. QFile::copy(currentFilePath, backupFileName);
  1986. }
  1987. SnapmaticProperties fallbackProperties = picture->getSnapmaticProperties();
  1988. picture->setSnapmaticProperties(snapmaticProperties);
  1989. if (!picture->exportPicture(currentFilePath))
  1990. {
  1991. picture->setSnapmaticProperties(fallbackProperties);
  1992. fails << QString("%1 [%2]").arg(picture->getPictureTitle(), picture->getPictureString());
  1993. }
  1994. else
  1995. {
  1996. picture->emitUpdate();
  1997. qApp->processEvents();
  1998. }
  1999. }
  2000. pbDialog.close();
  2001. if (!fails.isEmpty())
  2002. {
  2003. QMessageBox::warning(this, tr("Change Players..."), tr("%1 failed with...\n\n%2", "Action failed with...").arg(tr("Change Players", "%1 failed with..."), fails.join(", ")));
  2004. }
  2005. }
  2006. break;
  2007. case MassTool::Crew:
  2008. {
  2009. QList<SnapmaticWidget*> snapmaticWidgets;
  2010. for (ProfileWidget *widget : widgets.keys())
  2011. {
  2012. if (widget->isSelected())
  2013. {
  2014. if (widget->getWidgetType() == "SnapmaticWidget")
  2015. {
  2016. SnapmaticWidget *snapmaticWidget = qobject_cast<SnapmaticWidget*>(widget);
  2017. snapmaticWidgets += snapmaticWidget;
  2018. }
  2019. }
  2020. }
  2021. if (snapmaticWidgets.isEmpty())
  2022. {
  2023. QMessageBox::information(this, tr("Change Crew..."), tr("No Snapmatic pictures are selected"));
  2024. return;
  2025. }
  2026. int crewID = 0;
  2027. if (snapmaticWidgets.length() == 1)
  2028. {
  2029. crewID = snapmaticWidgets.at(0)->getPicture()->getSnapmaticProperties().crewID;
  2030. }
  2031. {
  2032. preSelectionCrewID:
  2033. bool ok;
  2034. int indexNum = 0;
  2035. QStringList itemList;
  2036. QStringList crewList = crewDB->getCrews();
  2037. if (!crewList.contains(QLatin1String("0")))
  2038. {
  2039. crewList += QLatin1String("0");
  2040. }
  2041. crewList.sort();
  2042. for (QString crew : crewList)
  2043. {
  2044. itemList += QString("%1 (%2)").arg(crew, crewDB->getCrewName(crew.toInt()));
  2045. }
  2046. if (crewList.contains(QString::number(crewID)))
  2047. {
  2048. indexNum = crewList.indexOf(QRegExp(QString::number(crewID)));
  2049. }
  2050. QString newCrew = QInputDialog::getItem(this, QApplication::translate("SnapmaticEditor", "Snapmatic Crew"), QApplication::translate("SnapmaticEditor", "New Snapmatic crew:"), itemList, indexNum, true, &ok, windowFlags()^Qt::Dialog^Qt::WindowMinMaxButtonsHint);
  2051. if (ok && !newCrew.isEmpty())
  2052. {
  2053. if (newCrew.contains(" ")) newCrew = newCrew.split(" ").at(0);
  2054. if (newCrew.length() > 10) return;
  2055. for (QChar crewChar : newCrew)
  2056. {
  2057. if (!crewChar.isNumber())
  2058. {
  2059. QMessageBox::warning(this, tr("Change Crew..."), tr("Failed to enter a valid Snapmatic Crew ID"));
  2060. goto preSelectionCrewID;
  2061. }
  2062. }
  2063. if (!crewList.contains(newCrew))
  2064. {
  2065. crewDB->addCrew(crewID);
  2066. }
  2067. crewID = newCrew.toInt();
  2068. }
  2069. else
  2070. {
  2071. return;
  2072. }
  2073. }
  2074. // Prepare Progress
  2075. int maximumId = snapmaticWidgets.length();
  2076. int overallId = 0;
  2077. QProgressDialog pbDialog(this);
  2078. pbDialog.setWindowFlags(pbDialog.windowFlags()^Qt::WindowContextHelpButtonHint^Qt::WindowCloseButtonHint);
  2079. pbDialog.setWindowTitle(tr("Patch selected..."));
  2080. pbDialog.setLabelText(tr("Patch file %1 of %2 files").arg(QString::number(1), QString::number(maximumId)));
  2081. pbDialog.setRange(1, maximumId);
  2082. pbDialog.setValue(1);
  2083. pbDialog.setModal(true);
  2084. QList<QPushButton*> pbBtn = pbDialog.findChildren<QPushButton*>();
  2085. pbBtn.at(0)->setDisabled(true);
  2086. QList<QProgressBar*> pbBar = pbDialog.findChildren<QProgressBar*>();
  2087. pbBar.at(0)->setTextVisible(false);
  2088. pbDialog.setAutoClose(false);
  2089. pbDialog.show();
  2090. // Begin Progress
  2091. QStringList fails;
  2092. for (SnapmaticWidget *snapmaticWidget : snapmaticWidgets)
  2093. {
  2094. // Update Progress
  2095. overallId++;
  2096. pbDialog.setValue(overallId);
  2097. pbDialog.setLabelText(tr("Patch file %1 of %2 files").arg(QString::number(overallId), QString::number(maximumId)));
  2098. SnapmaticPicture *picture = snapmaticWidget->getPicture();
  2099. SnapmaticProperties snapmaticProperties = picture->getSnapmaticProperties();
  2100. snapmaticProperties.crewID = crewID;
  2101. QString currentFilePath = picture->getPictureFilePath();
  2102. QString originalFilePath = picture->getOriginalPictureFilePath();
  2103. QString backupFileName = originalFilePath % ".bak";
  2104. if (!QFile::exists(backupFileName))
  2105. {
  2106. QFile::copy(currentFilePath, backupFileName);
  2107. }
  2108. SnapmaticProperties fallbackProperties = picture->getSnapmaticProperties();
  2109. picture->setSnapmaticProperties(snapmaticProperties);
  2110. if (!picture->exportPicture(currentFilePath))
  2111. {
  2112. picture->setSnapmaticProperties(fallbackProperties);
  2113. fails << QString("%1 [%2]").arg(picture->getPictureTitle(), picture->getPictureString());
  2114. }
  2115. else
  2116. {
  2117. picture->emitUpdate();
  2118. qApp->processEvents();
  2119. }
  2120. }
  2121. pbDialog.close();
  2122. if (!fails.isEmpty())
  2123. {
  2124. QMessageBox::warning(this, tr("Change Crew..."), tr("%1 failed with...\n\n%2", "Action failed with...").arg(tr("Change Crew", "%1 failed with..."), fails.join(", ")));
  2125. }
  2126. }
  2127. break;
  2128. case MassTool::Title:
  2129. {
  2130. QList<SnapmaticWidget*> snapmaticWidgets;
  2131. for (ProfileWidget *widget : widgets.keys())
  2132. {
  2133. if (widget->isSelected())
  2134. {
  2135. if (widget->getWidgetType() == "SnapmaticWidget")
  2136. {
  2137. SnapmaticWidget *snapmaticWidget = qobject_cast<SnapmaticWidget*>(widget);
  2138. snapmaticWidgets += snapmaticWidget;
  2139. }
  2140. }
  2141. }
  2142. if (snapmaticWidgets.isEmpty())
  2143. {
  2144. QMessageBox::information(this, tr("Change Title..."), tr("No Snapmatic pictures are selected"));
  2145. return;
  2146. }
  2147. QString snapmaticTitle;
  2148. if (snapmaticWidgets.length() == 1)
  2149. {
  2150. snapmaticTitle = snapmaticWidgets.at(0)->getPicture()->getPictureTitle();
  2151. }
  2152. {
  2153. preSelectionTitle:
  2154. bool ok;
  2155. QString newTitle = QInputDialog::getText(this, QApplication::translate("SnapmaticEditor", "Snapmatic Title"), QApplication::translate("SnapmaticEditor", "New Snapmatic title:"), QLineEdit::Normal, snapmaticTitle, &ok, windowFlags()^Qt::Dialog^Qt::WindowMinMaxButtonsHint);
  2156. if (ok && !newTitle.isEmpty())
  2157. {
  2158. if (!SnapmaticPicture::verifyTitle(newTitle))
  2159. {
  2160. QMessageBox::warning(this, tr("Change Title..."), tr("Failed to enter a valid Snapmatic title"));
  2161. goto preSelectionTitle;
  2162. }
  2163. snapmaticTitle = newTitle;
  2164. }
  2165. else
  2166. {
  2167. return;
  2168. }
  2169. }
  2170. // Prepare Progress
  2171. int maximumId = snapmaticWidgets.length();
  2172. int overallId = 0;
  2173. QProgressDialog pbDialog(this);
  2174. pbDialog.setWindowFlags(pbDialog.windowFlags()^Qt::WindowContextHelpButtonHint^Qt::WindowCloseButtonHint);
  2175. pbDialog.setWindowTitle(tr("Patch selected..."));
  2176. pbDialog.setLabelText(tr("Patch file %1 of %2 files").arg(QString::number(overallId), QString::number(maximumId)));
  2177. pbDialog.setRange(1, maximumId);
  2178. pbDialog.setValue(1);
  2179. pbDialog.setModal(true);
  2180. QList<QPushButton*> pbBtn = pbDialog.findChildren<QPushButton*>();
  2181. pbBtn.at(0)->setDisabled(true);
  2182. QList<QProgressBar*> pbBar = pbDialog.findChildren<QProgressBar*>();
  2183. pbBar.at(0)->setTextVisible(false);
  2184. pbDialog.setAutoClose(false);
  2185. pbDialog.show();
  2186. // Begin Progress
  2187. QStringList fails;
  2188. for (SnapmaticWidget *snapmaticWidget : snapmaticWidgets)
  2189. {
  2190. // Update Progress
  2191. overallId++;
  2192. pbDialog.setValue(overallId);
  2193. pbDialog.setLabelText(tr("Patch file %1 of %2 files").arg(QString::number(overallId), QString::number(maximumId)));
  2194. SnapmaticPicture *picture = snapmaticWidget->getPicture();
  2195. QString currentFilePath = picture->getPictureFilePath();
  2196. QString originalFilePath = picture->getOriginalPictureFilePath();
  2197. QString backupFileName = originalFilePath % ".bak";
  2198. if (!QFile::exists(backupFileName))
  2199. {
  2200. QFile::copy(currentFilePath, backupFileName);
  2201. }
  2202. QString fallbackTitle = picture->getPictureTitle();
  2203. picture->setPictureTitle(snapmaticTitle);
  2204. if (!picture->exportPicture(currentFilePath))
  2205. {
  2206. picture->setPictureTitle(fallbackTitle);
  2207. fails << QString("%1 [%2]").arg(picture->getPictureTitle(), picture->getPictureString());
  2208. }
  2209. else
  2210. {
  2211. picture->emitUpdate();
  2212. qApp->processEvents();
  2213. }
  2214. }
  2215. pbDialog.close();
  2216. if (!fails.isEmpty())
  2217. {
  2218. QMessageBox::warning(this, tr("Change Title..."), tr("%1 failed with...\n\n%2", "Action failed with...").arg(tr("Change Title", "%1 failed with..."), fails.join(", ")));
  2219. }
  2220. }
  2221. break;
  2222. }
  2223. }
  2224. int ProfileInterface::getRandomUid()
  2225. {
  2226. int random_int = pcg32_boundedrand_r(&rng, 2147483647);
  2227. return random_int;
  2228. }