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

PictureDialog.cpp 35KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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 "PictureDialog.h"
  19. #include "PictureWidget.h"
  20. #include "ProfileDatabase.h"
  21. #include "ui_PictureDialog.h"
  22. #include "SidebarGenerator.h"
  23. #include "MapLocationDialog.h"
  24. #include "ImageEditorDialog.h"
  25. #include "JsonEditorDialog.h"
  26. #include "SnapmaticEditor.h"
  27. #include "StandardPaths.h"
  28. #include "PictureExport.h"
  29. #include "ImportDialog.h"
  30. #include "StringParser.h"
  31. #include "GlobalString.h"
  32. #include "UiModLabel.h"
  33. #include "AppEnv.h"
  34. #include "config.h"
  35. #ifdef GTA5SYNC_WIN
  36. #if QT_VERSION >= 0x050200
  37. #include <QtWinExtras/QtWin>
  38. #include <QtWinExtras/QWinEvent>
  39. #endif
  40. #endif
  41. #include <QStringBuilder>
  42. #include <QDesktopWidget>
  43. #include <QJsonDocument>
  44. #include <QApplication>
  45. #include <QFontMetrics>
  46. #include <QJsonObject>
  47. #include <QSizePolicy>
  48. #include <QStaticText>
  49. #include <QFileDialog>
  50. #include <QMessageBox>
  51. #include <QJsonObject>
  52. #include <QVariantMap>
  53. #include <QJsonArray>
  54. #include <QKeyEvent>
  55. #include <QMimeData>
  56. #include <QToolBar>
  57. #include <QPainter>
  58. #include <QPicture>
  59. #include <QBitmap>
  60. #include <QBuffer>
  61. #include <QImage>
  62. #include <QDebug>
  63. #include <QList>
  64. #include <QDrag>
  65. #include <QIcon>
  66. #include <QUrl>
  67. #include <QDir>
  68. #ifdef GTA5SYNC_TELEMETRY
  69. #include "TelemetryClass.h"
  70. #endif
  71. // Macros for better Overview + RAM
  72. #define locX QString::number(picture->getSnapmaticProperties().location.x)
  73. #define locY QString::number(picture->getSnapmaticProperties().location.y)
  74. #define locZ QString::number(picture->getSnapmaticProperties().location.z)
  75. #define crewID QString::number(picture->getSnapmaticProperties().crewID)
  76. #define picArea picture->getSnapmaticProperties().location.area
  77. #define picPath picture->getPictureFilePath()
  78. #define picTitl StringParser::escapeString(picture->getPictureTitle())
  79. #define plyrsList picture->getSnapmaticProperties().playersList
  80. #define created picture->getSnapmaticProperties().createdDateTime.toString(Qt::DefaultLocaleShortDate)
  81. PictureDialog::PictureDialog(ProfileDatabase *profileDB, CrewDatabase *crewDB, QWidget *parent) :
  82. QDialog(parent), profileDB(profileDB), crewDB(crewDB),
  83. ui(new Ui::PictureDialog)
  84. {
  85. primaryWindow = false;
  86. setupPictureDialog();
  87. }
  88. PictureDialog::PictureDialog(ProfileDatabase *profileDB, CrewDatabase *crewDB, QString profileName, QWidget *parent) :
  89. QDialog(parent), profileDB(profileDB), crewDB(crewDB), profileName(profileName),
  90. ui(new Ui::PictureDialog)
  91. {
  92. primaryWindow = false;
  93. setupPictureDialog();
  94. }
  95. PictureDialog::PictureDialog(bool primaryWindow, ProfileDatabase *profileDB, CrewDatabase *crewDB, QWidget *parent) :
  96. QDialog(parent), primaryWindow(primaryWindow), profileDB(profileDB), crewDB(crewDB),
  97. ui(new Ui::PictureDialog)
  98. {
  99. setupPictureDialog();
  100. }
  101. PictureDialog::PictureDialog(bool primaryWindow, ProfileDatabase *profileDB, CrewDatabase *crewDB, QString profileName, QWidget *parent) :
  102. QDialog(parent), primaryWindow(primaryWindow), profileDB(profileDB), crewDB(crewDB), profileName(profileName),
  103. ui(new Ui::PictureDialog)
  104. {
  105. setupPictureDialog();
  106. }
  107. void PictureDialog::setupPictureDialog()
  108. {
  109. // Set Window Flags
  110. setWindowFlags(windowFlags()^Qt::WindowContextHelpButtonHint^Qt::CustomizeWindowHint);
  111. #ifdef Q_OS_LINUX
  112. // for stupid Window Manager (GNOME 3 should feel triggered)
  113. setWindowFlags(windowFlags()^Qt::Dialog^Qt::Window);
  114. #endif
  115. // Setup User Interface
  116. ui->setupUi(this);
  117. windowTitleStr = this->windowTitle();
  118. jsonDrawString = ui->labJSON->text();
  119. ui->cmdManage->setEnabled(false);
  120. fullscreenWidget = nullptr;
  121. rqFullscreen = false;
  122. previewMode = false;
  123. naviEnabled = false;
  124. indexed = false;
  125. smpic = nullptr;
  126. crewStr = "";
  127. // Avatar area
  128. qreal screenRatio = AppEnv::screenRatio();
  129. if (screenRatio != 1)
  130. {
  131. avatarAreaPicture = QImage(":/img/avatararea.png").scaledToHeight(536 * screenRatio, Qt::FastTransformation);
  132. }
  133. else
  134. {
  135. avatarAreaPicture = QImage(":/img/avatararea.png");
  136. }
  137. avatarLocX = 145;
  138. avatarLocY = 66;
  139. avatarSize = 470;
  140. // Overlay area
  141. renderOverlayPicture();
  142. overlayEnabled = true;
  143. // Manage menu
  144. manageMenu = new QMenu(this);
  145. manageMenu->addAction(tr("Export as &Picture..."), this, SLOT(exportSnapmaticPicture()));
  146. manageMenu->addAction(tr("Export as &Snapmatic..."), this, SLOT(copySnapmaticPicture()));
  147. manageMenu->addSeparator();
  148. manageMenu->addAction(tr("&Edit Properties..."), this, SLOT(editSnapmaticProperties()));
  149. manageMenu->addAction(tr("&Overwrite Image..."), this, SLOT(editSnapmaticImage()));
  150. manageMenu->addSeparator();
  151. QAction *openViewerAction = manageMenu->addAction(tr("Open &Map Viewer..."), this, SLOT(openPreviewMap()));
  152. openViewerAction->setShortcut(Qt::Key_M);
  153. manageMenu->addAction(tr("Open &JSON Editor..."), this, SLOT(editSnapmaticRawJson()));
  154. ui->cmdManage->setMenu(manageMenu);
  155. // Global map
  156. globalMap = GlobalString::getGlobalMap();
  157. // Event connects
  158. connect(ui->labJSON, SIGNAL(resized(QSize)), this, SLOT(adaptNewDialogSize(QSize)));
  159. // Set Icon for Close Button
  160. if (QIcon::hasThemeIcon("dialog-close"))
  161. {
  162. ui->cmdClose->setIcon(QIcon::fromTheme("dialog-close"));
  163. }
  164. else if (QIcon::hasThemeIcon("gtk-close"))
  165. {
  166. ui->cmdClose->setIcon(QIcon::fromTheme("gtk-close"));
  167. }
  168. installEventFilter(this);
  169. installEventFilter(ui->labPicture);
  170. ui->labPicture->setFixedSize(960 * screenRatio, 536 * screenRatio);
  171. ui->labPicture->setFocusPolicy(Qt::StrongFocus);
  172. // Pre-adapt window for DPI
  173. setFixedWidth(960 * screenRatio);
  174. setFixedHeight(536 * screenRatio);
  175. }
  176. PictureDialog::~PictureDialog()
  177. {
  178. #ifdef GTA5SYNC_WIN
  179. #if QT_VERSION >= 0x050200
  180. if (naviEnabled)
  181. {
  182. for (QObject *obj : layout()->menuBar()->children())
  183. {
  184. delete obj;
  185. }
  186. delete layout()->menuBar();
  187. }
  188. #endif
  189. #endif
  190. for (QObject *obj : manageMenu->children())
  191. {
  192. delete obj;
  193. }
  194. delete manageMenu;
  195. delete ui;
  196. }
  197. void PictureDialog::closeEvent(QCloseEvent *ev)
  198. {
  199. Q_UNUSED(ev)
  200. if (primaryWindow)
  201. {
  202. emit endDatabaseThread();
  203. }
  204. }
  205. void PictureDialog::addPreviousNextButtons()
  206. {
  207. // Windows Vista additions
  208. #ifdef GTA5SYNC_WIN
  209. #if QT_VERSION >= 0x050200
  210. QToolBar *uiToolbar = new QToolBar("Picture Toolbar", this);
  211. uiToolbar->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
  212. uiToolbar->setObjectName("uiToolbar");
  213. uiToolbar->addAction(QIcon(":/img/back.png"), "", this, SLOT(previousPictureRequestedSlot()));
  214. uiToolbar->addAction(QIcon(":/img/next.png"), "", this, SLOT(nextPictureRequestedSlot()));
  215. layout()->setMenuBar(uiToolbar);
  216. naviEnabled = true;
  217. #endif
  218. #endif
  219. }
  220. #ifdef GTA5SYNC_WIN
  221. #if QT_VERSION >= 0x050200
  222. #ifdef GTA5SYNC_APV
  223. bool PictureDialog::nativeEvent(const QByteArray &eventType, void *message, long *result)
  224. {
  225. *result = 0;
  226. MSG *msg = static_cast<MSG*>(message);
  227. LRESULT lRet = 0;
  228. if (naviEnabled && QtWin::isCompositionEnabled())
  229. {
  230. if (msg->message == WM_NCCALCSIZE && msg->wParam == TRUE)
  231. {
  232. NCCALCSIZE_PARAMS *pncsp = reinterpret_cast<NCCALCSIZE_PARAMS*>(msg->lParam);
  233. int sideBorderSize = ((frameSize().width() - size().width()) / 2);
  234. #ifdef GTA5SYNC_APV_SIDE
  235. int buttomBorderSize = sideBorderSize;
  236. #else
  237. int buttomBorderSize = (frameSize().height() - size().height());
  238. #endif
  239. pncsp->rgrc[0].left += sideBorderSize;
  240. pncsp->rgrc[0].right -= sideBorderSize;
  241. pncsp->rgrc[0].bottom -= buttomBorderSize;
  242. }
  243. else if (msg->message == WM_NCHITTEST)
  244. {
  245. int CLOSE_BUTTON_ID = 20;
  246. lRet = HitTestNCA(msg->hwnd, msg->lParam);
  247. DwmDefWindowProc(msg->hwnd, msg->message, msg->wParam, msg->lParam, &lRet);
  248. *result = lRet;
  249. if (lRet != CLOSE_BUTTON_ID) { return QWidget::nativeEvent(eventType, message, result); }
  250. }
  251. else
  252. {
  253. return QWidget::nativeEvent(eventType, message, result);
  254. }
  255. }
  256. else
  257. {
  258. return QWidget::nativeEvent(eventType, message, result);
  259. }
  260. return true;
  261. }
  262. LRESULT PictureDialog::HitTestNCA(HWND hWnd, LPARAM lParam)
  263. {
  264. int LEFTEXTENDWIDTH = 0;
  265. int RIGHTEXTENDWIDTH = 0;
  266. int BOTTOMEXTENDWIDTH = 0;
  267. int TOPEXTENDWIDTH = layout()->menuBar()->height();
  268. POINT ptMouse = {(int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam)};
  269. RECT rcWindow;
  270. GetWindowRect(hWnd, &rcWindow);
  271. RECT rcFrame = {};
  272. AdjustWindowRectEx(&rcFrame, WS_OVERLAPPEDWINDOW & ~WS_CAPTION, FALSE, NULL);
  273. USHORT uRow = 1;
  274. USHORT uCol = 1;
  275. bool fOnResizeBorder = false;
  276. if (ptMouse.y >= rcWindow.top && ptMouse.y < rcWindow.top + TOPEXTENDWIDTH)
  277. {
  278. fOnResizeBorder = (ptMouse.y < (rcWindow.top - rcFrame.top));
  279. uRow = 0;
  280. }
  281. else if (ptMouse.y < rcWindow.bottom && ptMouse.y >= rcWindow.bottom - BOTTOMEXTENDWIDTH)
  282. {
  283. uRow = 2;
  284. }
  285. if (ptMouse.x >= rcWindow.left && ptMouse.x < rcWindow.left + LEFTEXTENDWIDTH)
  286. {
  287. uCol = 0;
  288. }
  289. else if (ptMouse.x < rcWindow.right && ptMouse.x >= rcWindow.right - RIGHTEXTENDWIDTH)
  290. {
  291. uCol = 2;
  292. }
  293. LRESULT hitTests[3][3] =
  294. {
  295. { HTTOPLEFT, fOnResizeBorder ? HTTOP : HTCAPTION, HTTOPRIGHT },
  296. { HTLEFT, HTNOWHERE, HTRIGHT },
  297. { HTBOTTOMLEFT, HTBOTTOM, HTBOTTOMRIGHT },
  298. };
  299. return hitTests[uRow][uCol];
  300. }
  301. void PictureDialog::resizeEvent(QResizeEvent *event)
  302. {
  303. Q_UNUSED(event)
  304. // int newDialogHeight = ui->labPicture->pixmap()->height();
  305. // newDialogHeight = newDialogHeight + ui->jsonFrame->height();
  306. // if (naviEnabled) newDialogHeight = newDialogHeight + layout()->menuBar()->height();
  307. // int buttomBorderSize = (frameSize().height() - size().height());
  308. // int sideBorderSize = ((frameSize().width() - size().width()) / 2);
  309. // int brokenDialogHeight = newDialogHeight + (buttomBorderSize - sideBorderSize);
  310. // if (event->size().height() == brokenDialogHeight)
  311. // {
  312. // qDebug() << "BROKEN 1";
  313. // setGeometry(geometry().x(), geometry().y(), width(), newDialogHeight);
  314. // qDebug() << "BROKEN 2";
  315. // event->ignore();
  316. // }
  317. }
  318. #endif
  319. #endif
  320. #endif
  321. void PictureDialog::adaptNewDialogSize(QSize newLabelSize)
  322. {
  323. Q_UNUSED(newLabelSize)
  324. int newDialogHeight = ui->labPicture->pixmap()->height();
  325. newDialogHeight = newDialogHeight + ui->jsonFrame->height();
  326. if (naviEnabled) newDialogHeight = newDialogHeight + layout()->menuBar()->height();
  327. setMaximumSize(width(), newDialogHeight);
  328. setMinimumSize(width(), newDialogHeight);
  329. setFixedHeight(newDialogHeight);
  330. ui->labPicture->updateGeometry();
  331. ui->jsonFrame->updateGeometry();
  332. updateGeometry();
  333. }
  334. void PictureDialog::stylizeDialog()
  335. {
  336. #ifdef GTA5SYNC_WIN
  337. #if QT_VERSION >= 0x050200
  338. if (QtWin::isCompositionEnabled())
  339. {
  340. QPalette palette;
  341. QtWin::extendFrameIntoClientArea(this, 0, this->layout()->menuBar()->height(), 0, 0);
  342. ui->jsonFrame->setStyleSheet(QString("QFrame { background: %1; }").arg(palette.window().color().name()));
  343. setStyleSheet("PictureDialog { background: transparent; }");
  344. }
  345. else
  346. {
  347. QPalette palette;
  348. QtWin::resetExtendedFrame(this);
  349. ui->jsonFrame->setStyleSheet(QString("QFrame { background: %1; }").arg(palette.window().color().name()));
  350. setStyleSheet(QString("PictureDialog { background: %1; }").arg(QtWin::realColorizationColor().name()));
  351. }
  352. #endif
  353. #endif
  354. }
  355. bool PictureDialog::event(QEvent *event)
  356. {
  357. #ifdef GTA5SYNC_WIN
  358. #if QT_VERSION >= 0x050200
  359. if (naviEnabled)
  360. {
  361. if (event->type() == QWinEvent::CompositionChange || event->type() == QWinEvent::ColorizationChange)
  362. {
  363. stylizeDialog();
  364. }
  365. }
  366. #endif
  367. #endif
  368. return QDialog::event(event);
  369. }
  370. void PictureDialog::nextPictureRequestedSlot()
  371. {
  372. emit nextPictureRequested();
  373. }
  374. void PictureDialog::previousPictureRequestedSlot()
  375. {
  376. emit previousPictureRequested();
  377. }
  378. bool PictureDialog::eventFilter(QObject *obj, QEvent *ev)
  379. {
  380. bool returnValue = false;
  381. if (obj == this || obj == ui->labPicture)
  382. {
  383. if (ev->type() == QEvent::KeyPress)
  384. {
  385. QKeyEvent *keyEvent = dynamic_cast<QKeyEvent*>(ev);
  386. switch (keyEvent->key()){
  387. case Qt::Key_Left:
  388. emit previousPictureRequested();
  389. returnValue = true;
  390. break;
  391. case Qt::Key_Right:
  392. emit nextPictureRequested();
  393. returnValue = true;
  394. break;
  395. case Qt::Key_1:
  396. if (previewMode)
  397. {
  398. previewMode = false;
  399. renderPicture();
  400. }
  401. else
  402. {
  403. previewMode = true;
  404. renderPicture();
  405. }
  406. break;
  407. case Qt::Key_2:
  408. if (overlayEnabled)
  409. {
  410. overlayEnabled = false;
  411. if (!previewMode) renderPicture();
  412. }
  413. else
  414. {
  415. overlayEnabled = true;
  416. if (!previewMode) renderPicture();
  417. }
  418. break;
  419. case Qt::Key_M:
  420. openPreviewMap();
  421. returnValue = true;
  422. break;
  423. #if QT_VERSION >= 0x050300
  424. case Qt::Key_Exit:
  425. ui->cmdClose->click();
  426. returnValue = true;
  427. break;
  428. #endif
  429. case Qt::Key_Enter: case Qt::Key_Return:
  430. on_labPicture_mouseDoubleClicked(Qt::LeftButton);
  431. returnValue = true;
  432. break;
  433. case Qt::Key_Escape:
  434. ui->cmdClose->click();
  435. returnValue = true;
  436. break;
  437. }
  438. }
  439. #ifdef GTA5SYNC_WIN
  440. #if QT_VERSION >= 0x050200
  441. if (obj != ui->labPicture && naviEnabled)
  442. {
  443. if (ev->type() == QEvent::MouseButtonPress)
  444. {
  445. QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(ev);
  446. if (mouseEvent->pos().y() <= layout()->menuBar()->height())
  447. {
  448. if (mouseEvent->button() == Qt::LeftButton)
  449. {
  450. dragPosition = mouseEvent->pos();
  451. dragStart = true;
  452. }
  453. }
  454. }
  455. if (ev->type() == QEvent::MouseButtonRelease)
  456. {
  457. QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(ev);
  458. if (mouseEvent->pos().y() <= layout()->menuBar()->height())
  459. {
  460. if (mouseEvent->button() == Qt::LeftButton)
  461. {
  462. dragStart = false;
  463. }
  464. }
  465. }
  466. if (ev->type() == QEvent::MouseMove && dragStart)
  467. {
  468. QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(ev);
  469. if (mouseEvent->pos().y() <= layout()->menuBar()->height())
  470. {
  471. if (mouseEvent->buttons() & Qt::LeftButton)
  472. {
  473. QPoint diff = mouseEvent->pos() - dragPosition;
  474. move(QPoint(pos() + diff));
  475. updateGeometry();
  476. }
  477. }
  478. }
  479. }
  480. #endif
  481. #endif
  482. }
  483. return returnValue;
  484. }
  485. void PictureDialog::triggerFullscreenDoubeClick()
  486. {
  487. on_labPicture_mouseDoubleClicked(Qt::LeftButton);
  488. }
  489. void PictureDialog::exportCustomContextMenuRequestedPrivate(const QPoint &pos, bool fullscreen)
  490. {
  491. rqFullscreen = fullscreen;
  492. manageMenu->popup(pos);
  493. }
  494. void PictureDialog::exportCustomContextMenuRequested(const QPoint &pos)
  495. {
  496. exportCustomContextMenuRequestedPrivate(pos, true);
  497. }
  498. void PictureDialog::mousePressEvent(QMouseEvent *ev)
  499. {
  500. QDialog::mousePressEvent(ev);
  501. }
  502. void PictureDialog::dialogNextPictureRequested()
  503. {
  504. emit nextPictureRequested();
  505. }
  506. void PictureDialog::dialogPreviousPictureRequested()
  507. {
  508. emit previousPictureRequested();
  509. }
  510. void PictureDialog::renderOverlayPicture()
  511. {
  512. // Generating Overlay Preview
  513. qreal screenRatio = AppEnv::screenRatio();
  514. QRect preferedRect = QRect(0, 0, 200 * screenRatio, 160 * screenRatio);
  515. QString overlayText = tr("Key 1 - Avatar Preview Mode\nKey 2 - Toggle Overlay\nArrow Keys - Navigate");
  516. QFont overlayPainterFont;
  517. overlayPainterFont.setPixelSize(12 * screenRatio);
  518. QFontMetrics fontMetrics(overlayPainterFont);
  519. QRect overlaySpace = fontMetrics.boundingRect(preferedRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextDontClip | Qt::TextWordWrap, overlayText);
  520. int hOverlay = Qt::AlignTop;
  521. if (overlaySpace.height() < 74 * screenRatio)
  522. {
  523. hOverlay = Qt::AlignVCenter;
  524. preferedRect.setHeight(71 * screenRatio);
  525. overlaySpace.setHeight(80 * screenRatio);
  526. }
  527. else
  528. {
  529. overlaySpace.setHeight(overlaySpace.height() + 6 * screenRatio);
  530. }
  531. QImage overlayImage(overlaySpace.size(), QImage::Format_ARGB32_Premultiplied);
  532. overlayImage.fill(Qt::transparent);
  533. QPainter overlayPainter(&overlayImage);
  534. overlayPainter.setPen(QColor::fromRgb(255, 255, 255, 255));
  535. overlayPainter.setFont(overlayPainterFont);
  536. overlayPainter.drawText(preferedRect, Qt::AlignLeft | hOverlay | Qt::TextDontClip | Qt::TextWordWrap, overlayText);
  537. overlayPainter.end();
  538. if (overlaySpace.width() < 194 * screenRatio)
  539. {
  540. overlaySpace.setWidth(200 * screenRatio);
  541. }
  542. else
  543. {
  544. overlaySpace.setWidth(overlaySpace.width() + 6 * screenRatio);
  545. }
  546. QImage overlayBorderImage(overlaySpace.width(), overlaySpace.height(), QImage::Format_ARGB6666_Premultiplied);
  547. overlayBorderImage.fill(QColor(15, 15, 15, 162));
  548. overlayTempImage = QImage(overlaySpace.width(), overlaySpace.height(), QImage::Format_ARGB6666_Premultiplied);
  549. overlayTempImage.fill(Qt::transparent);
  550. QPainter overlayTempPainter(&overlayTempImage);
  551. overlayTempPainter.drawImage(0, 0, overlayBorderImage);
  552. overlayTempPainter.drawImage(3 * screenRatio, 3 * screenRatio, overlayImage);
  553. overlayTempPainter.end();
  554. }
  555. void PictureDialog::setSnapmaticPicture(SnapmaticPicture *picture, bool readOk, bool _indexed, int _index)
  556. {
  557. if (smpic != nullptr)
  558. {
  559. QObject::disconnect(smpic, SIGNAL(updated()), this, SLOT(updated()));
  560. QObject::disconnect(smpic, SIGNAL(customSignal(QString)), this, SLOT(customSignal(QString)));
  561. }
  562. snapmaticPicture = QImage();
  563. indexed = _indexed;
  564. index = _index;
  565. smpic = picture;
  566. if (!readOk)
  567. {
  568. QMessageBox::warning(this, tr("Snapmatic Picture Viewer"), tr("Failed at %1").arg(picture->getLastStep()));
  569. return;
  570. }
  571. if (picture->isPicOk())
  572. {
  573. snapmaticPicture = picture->getImage();
  574. renderPicture();
  575. ui->cmdManage->setEnabled(true);
  576. }
  577. if (picture->isJsonOk())
  578. {
  579. crewStr = crewDB->getCrewName(crewID);
  580. if (globalMap.contains(picArea))
  581. {
  582. picAreaStr = globalMap[picArea];
  583. }
  584. else
  585. {
  586. picAreaStr = picArea;
  587. }
  588. setWindowTitle(windowTitleStr.arg(picTitl));
  589. ui->labJSON->setText(jsonDrawString.arg(locX, locY, locZ, generatePlayersString(), generateCrewString(), picTitl, picAreaStr, created));
  590. }
  591. else
  592. {
  593. ui->labJSON->setText(jsonDrawString.arg("0", "0", "0", tr("No Players"), tr("No Crew"), tr("Unknown Location")));
  594. QMessageBox::warning(this,tr("Snapmatic Picture Viewer"),tr("Failed at %1").arg(picture->getLastStep()));
  595. }
  596. QObject::connect(smpic, SIGNAL(updated()), this, SLOT(updated()));
  597. QObject::connect(smpic, SIGNAL(customSignal(QString)), this, SLOT(customSignal(QString)));
  598. emit newPictureCommited(snapmaticPicture);
  599. }
  600. void PictureDialog::setSnapmaticPicture(SnapmaticPicture *picture, bool readOk, int index)
  601. {
  602. setSnapmaticPicture(picture, readOk, true, index);
  603. }
  604. void PictureDialog::setSnapmaticPicture(SnapmaticPicture *picture, bool readOk)
  605. {
  606. setSnapmaticPicture(picture, readOk, false, 0);
  607. }
  608. void PictureDialog::setSnapmaticPicture(SnapmaticPicture *picture, int index)
  609. {
  610. setSnapmaticPicture(picture, true, index);
  611. }
  612. void PictureDialog::setSnapmaticPicture(SnapmaticPicture *picture)
  613. {
  614. setSnapmaticPicture(picture, true);
  615. }
  616. void PictureDialog::renderPicture()
  617. {
  618. qreal screenRatio = AppEnv::screenRatio();
  619. if (!previewMode)
  620. {
  621. if (overlayEnabled)
  622. {
  623. QPixmap shownImagePixmap(960 * screenRatio, 536 * screenRatio);
  624. shownImagePixmap.fill(Qt::transparent);
  625. QPainter shownImagePainter(&shownImagePixmap);
  626. if (screenRatio == 1)
  627. {
  628. shownImagePainter.drawImage(0, 0, snapmaticPicture);
  629. shownImagePainter.drawImage(3 * screenRatio, 3 * screenRatio, overlayTempImage);
  630. }
  631. else
  632. {
  633. shownImagePainter.drawImage(0, 0, snapmaticPicture.scaledToHeight(536 * screenRatio, Qt::SmoothTransformation));
  634. shownImagePainter.drawImage(3 * screenRatio, 3 * screenRatio, overlayTempImage);
  635. }
  636. shownImagePainter.end();
  637. ui->labPicture->setPixmap(shownImagePixmap);
  638. }
  639. else
  640. {
  641. if (screenRatio != 1)
  642. {
  643. QPixmap shownImagePixmap(960 * screenRatio, 536 * screenRatio);
  644. shownImagePixmap.fill(Qt::transparent);
  645. QPainter shownImagePainter(&shownImagePixmap);
  646. shownImagePainter.drawImage(0, 0, snapmaticPicture.scaledToHeight(536 * screenRatio, Qt::SmoothTransformation));
  647. shownImagePainter.end();
  648. ui->labPicture->setPixmap(shownImagePixmap);
  649. }
  650. else
  651. {
  652. ui->labPicture->setPixmap(QPixmap::fromImage(snapmaticPicture));
  653. }
  654. }
  655. }
  656. else
  657. {
  658. // Generating Avatar Preview
  659. QPixmap avatarPixmap(960 * screenRatio, 536 * screenRatio);
  660. QPainter snapPainter(&avatarPixmap);
  661. QFont snapPainterFont;
  662. snapPainterFont.setPixelSize(12 * screenRatio);
  663. if (screenRatio == 1)
  664. {
  665. snapPainter.drawImage(0, 0, snapmaticPicture);
  666. }
  667. else
  668. {
  669. snapPainter.drawImage(0, 0, snapmaticPicture.scaledToHeight(536 * screenRatio, Qt::SmoothTransformation));
  670. }
  671. snapPainter.drawImage(0, 0, avatarAreaPicture);
  672. snapPainter.setPen(QColor::fromRgb(255, 255, 255, 255));
  673. snapPainter.setFont(snapPainterFont);
  674. snapPainter.drawText(QRect(3 * screenRatio, 3 * screenRatio, 140 * screenRatio, 536 * screenRatio), Qt::AlignLeft | Qt::TextWordWrap, tr("Avatar Preview Mode\nPress 1 for Default View"));
  675. snapPainter.end();
  676. ui->labPicture->setPixmap(avatarPixmap);
  677. }
  678. }
  679. void PictureDialog::crewNameUpdated()
  680. {
  681. SnapmaticPicture *picture = smpic; // used by macro
  682. QString crewIDStr = crewID;
  683. if (crewIDStr == crewStr)
  684. {
  685. crewStr = crewDB->getCrewName(crewIDStr);
  686. ui->labJSON->setText(jsonDrawString.arg(locX, locY, locZ, generatePlayersString(), generateCrewString(), picTitl, picAreaStr, created));
  687. }
  688. }
  689. void PictureDialog::playerNameUpdated()
  690. {
  691. SnapmaticPicture *picture = smpic; // used by macro
  692. if (plyrsList.count() >= 1)
  693. {
  694. ui->labJSON->setText(jsonDrawString.arg(locX, locY, locZ, generatePlayersString(), generateCrewString(), picTitl, picAreaStr, created));
  695. }
  696. }
  697. QString PictureDialog::generateCrewString()
  698. {
  699. SnapmaticPicture *picture = smpic; // used by macro
  700. QString crewIDStr = crewID; // save operation time
  701. if (crewIDStr != "0" && !crewIDStr.isEmpty())
  702. {
  703. return QString("<a href=\"https://socialclub.rockstargames.com/crew/" % QString(crewStr).replace(" ", "_") % "/" % crewIDStr % "\">" % crewStr % "</a>");
  704. }
  705. return tr("No Crew");
  706. }
  707. QString PictureDialog::generatePlayersString()
  708. {
  709. SnapmaticPicture *picture = smpic; // used by macro
  710. const QStringList playersList = plyrsList; // save operation time
  711. QString plyrsStr;
  712. if (playersList.length() >= 1)
  713. {
  714. for (QString player : playersList)
  715. {
  716. QString playerName;
  717. playerName = profileDB->getPlayerName(player);
  718. plyrsStr += ", <a href=\"https://socialclub.rockstargames.com/member/" % playerName % "/" % player % "\">" % playerName % "</a>";
  719. }
  720. plyrsStr.remove(0, 2);
  721. }
  722. else
  723. {
  724. plyrsStr = tr("No Players");
  725. }
  726. return plyrsStr;
  727. }
  728. void PictureDialog::exportSnapmaticPicture()
  729. {
  730. if (rqFullscreen && fullscreenWidget != nullptr)
  731. {
  732. PictureExport::exportAsPicture(fullscreenWidget, smpic);
  733. }
  734. else
  735. {
  736. PictureExport::exportAsPicture(this, smpic);
  737. }
  738. }
  739. void PictureDialog::copySnapmaticPicture()
  740. {
  741. if (rqFullscreen && fullscreenWidget != nullptr)
  742. {
  743. PictureExport::exportAsSnapmatic(fullscreenWidget, smpic);
  744. }
  745. else
  746. {
  747. PictureExport::exportAsSnapmatic(this, smpic);
  748. }
  749. }
  750. void PictureDialog::on_labPicture_mouseDoubleClicked(Qt::MouseButton button)
  751. {
  752. if (button == Qt::LeftButton)
  753. {
  754. QRect desktopRect = QApplication::desktop()->screenGeometry(this);
  755. PictureWidget *pictureWidget = new PictureWidget(this); // Work!
  756. pictureWidget->setObjectName("PictureWidget");
  757. #if QT_VERSION >= 0x050600
  758. pictureWidget->setWindowFlags(pictureWidget->windowFlags()^Qt::FramelessWindowHint^Qt::WindowStaysOnTopHint^Qt::MaximizeUsingFullscreenGeometryHint);
  759. #else
  760. pictureWidget->setWindowFlags(pictureWidget->windowFlags()^Qt::FramelessWindowHint^Qt::WindowStaysOnTopHint);
  761. #endif
  762. pictureWidget->setWindowTitle(windowTitle());
  763. pictureWidget->setStyleSheet("QLabel#pictureLabel{background-color: black;}");
  764. pictureWidget->setImage(snapmaticPicture, desktopRect);
  765. pictureWidget->setModal(true);
  766. fullscreenWidget = pictureWidget;
  767. QObject::connect(this, SIGNAL(newPictureCommited(QImage)), pictureWidget, SLOT(setImage(QImage)));
  768. QObject::connect(pictureWidget, SIGNAL(nextPictureRequested()), this, SLOT(dialogNextPictureRequested()));
  769. QObject::connect(pictureWidget, SIGNAL(previousPictureRequested()), this, SLOT(dialogPreviousPictureRequested()));
  770. pictureWidget->move(desktopRect.x(), desktopRect.y());
  771. pictureWidget->resize(desktopRect.width(), desktopRect.height());
  772. #ifdef GTA5SYNC_WIN
  773. #if QT_VERSION >= 0x050200
  774. QtWin::markFullscreenWindow(pictureWidget, true);
  775. #endif
  776. #endif
  777. pictureWidget->showFullScreen();
  778. pictureWidget->setFocus();
  779. pictureWidget->raise();
  780. pictureWidget->exec();
  781. fullscreenWidget = nullptr; // Work!
  782. delete pictureWidget; // Work!
  783. }
  784. }
  785. void PictureDialog::on_labPicture_customContextMenuRequested(const QPoint &pos)
  786. {
  787. exportCustomContextMenuRequestedPrivate(ui->labPicture->mapToGlobal(pos), false);
  788. }
  789. bool PictureDialog::isIndexed()
  790. {
  791. return indexed;
  792. }
  793. int PictureDialog::getIndex()
  794. {
  795. return index;
  796. }
  797. void PictureDialog::openPreviewMap()
  798. {
  799. SnapmaticPicture *picture = smpic;
  800. MapLocationDialog *mapLocDialog;
  801. if (rqFullscreen && fullscreenWidget != nullptr)
  802. {
  803. mapLocDialog = new MapLocationDialog(picture->getSnapmaticProperties().location.x, picture->getSnapmaticProperties().location.y, fullscreenWidget);
  804. }
  805. else
  806. {
  807. mapLocDialog = new MapLocationDialog(picture->getSnapmaticProperties().location.x, picture->getSnapmaticProperties().location.y, this);
  808. }
  809. mapLocDialog->setWindowIcon(windowIcon());
  810. mapLocDialog->setModal(true);
  811. #ifndef Q_OS_ANDROID
  812. mapLocDialog->show();
  813. #else
  814. mapLocDialog->showMaximized();
  815. #endif
  816. mapLocDialog->exec();
  817. if (mapLocDialog->propUpdated())
  818. {
  819. // Update Snapmatic Properties
  820. SnapmaticProperties localSpJson = picture->getSnapmaticProperties();
  821. localSpJson.location.x = mapLocDialog->getXpos();
  822. localSpJson.location.y = mapLocDialog->getYpos();
  823. localSpJson.location.z = 0;
  824. // Update Snapmatic Picture
  825. QString currentFilePath = picture->getPictureFilePath();
  826. QString originalFilePath = picture->getOriginalPictureFilePath();
  827. QString backupFileName = originalFilePath % ".bak";
  828. if (!QFile::exists(backupFileName))
  829. {
  830. QFile::copy(currentFilePath, backupFileName);
  831. }
  832. SnapmaticProperties fallbackProperties = picture->getSnapmaticProperties();
  833. picture->setSnapmaticProperties(localSpJson);
  834. if (!picture->exportPicture(currentFilePath))
  835. {
  836. QMessageBox::warning(this, SnapmaticEditor::tr("Snapmatic Properties"), SnapmaticEditor::tr("Patching of Snapmatic Properties failed because of I/O Error"));
  837. picture->setSnapmaticProperties(fallbackProperties);
  838. }
  839. else
  840. {
  841. updated();
  842. #ifdef GTA5SYNC_TELEMETRY
  843. QSettings telemetrySettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  844. telemetrySettings.beginGroup("Telemetry");
  845. bool pushUsageData = telemetrySettings.value("PushUsageData", false).toBool();
  846. telemetrySettings.endGroup();
  847. if (pushUsageData && Telemetry->canPush())
  848. {
  849. QJsonDocument jsonDocument;
  850. QJsonObject jsonObject;
  851. jsonObject["Type"] = "LocationEdited";
  852. jsonObject["ExtraFlags"] = "Viewer";
  853. jsonObject["EditedSize"] = QString::number(picture->getContentMaxLength());
  854. jsonObject["EditedTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
  855. jsonDocument.setObject(jsonObject);
  856. Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
  857. }
  858. #endif
  859. }
  860. }
  861. delete mapLocDialog;
  862. }
  863. void PictureDialog::editSnapmaticProperties()
  864. {
  865. SnapmaticPicture *picture = smpic;
  866. SnapmaticEditor *snapmaticEditor;
  867. if (rqFullscreen && fullscreenWidget != nullptr)
  868. {
  869. snapmaticEditor = new SnapmaticEditor(crewDB, profileDB, fullscreenWidget);
  870. }
  871. else
  872. {
  873. snapmaticEditor = new SnapmaticEditor(crewDB, profileDB, this);
  874. }
  875. snapmaticEditor->setWindowIcon(windowIcon());
  876. snapmaticEditor->setSnapmaticPicture(picture);
  877. snapmaticEditor->setModal(true);
  878. #ifndef Q_OS_ANDROID
  879. snapmaticEditor->show();
  880. #else
  881. snapmaticEditor->showMaximized();
  882. #endif
  883. snapmaticEditor->exec();
  884. delete snapmaticEditor;
  885. }
  886. void PictureDialog::editSnapmaticImage()
  887. {
  888. QImage *currentImage = new QImage(smpic->getImage());
  889. ImportDialog *importDialog;
  890. if (rqFullscreen && fullscreenWidget != nullptr)
  891. {
  892. importDialog = new ImportDialog(profileName, fullscreenWidget);
  893. }
  894. else
  895. {
  896. importDialog = new ImportDialog(profileName, this);
  897. }
  898. importDialog->setWindowIcon(windowIcon());
  899. importDialog->setImage(currentImage);
  900. importDialog->enableOverwriteMode();
  901. importDialog->setModal(true);
  902. importDialog->exec();
  903. if (importDialog->isImportAgreed())
  904. {
  905. const QByteArray previousPicture = smpic->getPictureStream();
  906. bool success = smpic->setImage(importDialog->image());
  907. if (success)
  908. {
  909. QString currentFilePath = smpic->getPictureFilePath();
  910. QString originalFilePath = smpic->getOriginalPictureFilePath();
  911. QString backupFileName = originalFilePath % ".bak";
  912. if (!QFile::exists(backupFileName))
  913. {
  914. QFile::copy(currentFilePath, backupFileName);
  915. }
  916. if (!smpic->exportPicture(currentFilePath))
  917. {
  918. smpic->setPictureStream(previousPicture);
  919. QMessageBox::warning(this, QApplication::translate("ImageEditorDialog", "Snapmatic Image Editor"), QApplication::translate("ImageEditorDialog", "Patching of Snapmatic Image failed because of I/O Error"));
  920. return;
  921. }
  922. smpic->emitCustomSignal("PictureUpdated");
  923. #ifdef GTA5SYNC_TELEMETRY
  924. QSettings telemetrySettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  925. telemetrySettings.beginGroup("Telemetry");
  926. bool pushUsageData = telemetrySettings.value("PushUsageData", false).toBool();
  927. telemetrySettings.endGroup();
  928. if (pushUsageData && Telemetry->canPush())
  929. {
  930. QJsonDocument jsonDocument;
  931. QJsonObject jsonObject;
  932. jsonObject["Type"] = "ImageEdited";
  933. jsonObject["ExtraFlags"] = "Viewer";
  934. jsonObject["EditedSize"] = QString::number(smpic->getContentMaxLength());
  935. jsonObject["EditedTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
  936. jsonDocument.setObject(jsonObject);
  937. Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
  938. }
  939. #endif
  940. }
  941. else
  942. {
  943. QMessageBox::warning(this, QApplication::translate("ImageEditorDialog", "Snapmatic Image Editor"), QApplication::translate("ImageEditorDialog", "Patching of Snapmatic Image failed because of Image Error"));
  944. return;
  945. }
  946. }
  947. delete importDialog;
  948. }
  949. void PictureDialog::editSnapmaticRawJson()
  950. {
  951. SnapmaticPicture *picture = smpic;
  952. JsonEditorDialog *jsonEditor;
  953. if (rqFullscreen && fullscreenWidget != nullptr)
  954. {
  955. jsonEditor = new JsonEditorDialog(picture, fullscreenWidget);
  956. }
  957. else
  958. {
  959. jsonEditor = new JsonEditorDialog(picture, this);
  960. }
  961. jsonEditor->setWindowIcon(windowIcon());
  962. jsonEditor->setModal(true);
  963. #ifndef Q_OS_ANDROID
  964. jsonEditor->show();
  965. #else
  966. jsonEditor->showMaximized();
  967. #endif
  968. jsonEditor->exec();
  969. delete jsonEditor;
  970. }
  971. void PictureDialog::updated()
  972. {
  973. SnapmaticPicture *picture = smpic; // used by macro
  974. crewStr = crewDB->getCrewName(crewID);
  975. if (globalMap.contains(picArea))
  976. {
  977. picAreaStr = globalMap[picArea];
  978. }
  979. else
  980. {
  981. picAreaStr = picArea;
  982. }
  983. setWindowTitle(windowTitleStr.arg(picTitl));
  984. ui->labJSON->setText(jsonDrawString.arg(locX, locY, locZ, generatePlayersString(), generateCrewString(), picTitl, picAreaStr, created));
  985. }
  986. void PictureDialog::customSignal(QString signal)
  987. {
  988. SnapmaticPicture *picture = smpic; // used by macro
  989. if (signal == "PictureUpdated")
  990. {
  991. snapmaticPicture = picture->getImage();
  992. renderPicture();
  993. }
  994. }