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

SavegameWidget.cpp 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "SavegameWidget.h"
  19. #include "ui_SavegameWidget.h"
  20. #include "SidebarGenerator.h"
  21. #include "SavegameDialog.h"
  22. #include "StandardPaths.h"
  23. #include "SavegameData.h"
  24. #include "SavegameCopy.h"
  25. #include "AppEnv.h"
  26. #include "config.h"
  27. #include <QFileDialog>
  28. #include <QMessageBox>
  29. #include <QSettings>
  30. #include <QFileInfo>
  31. #include <QPalette>
  32. #include <QColor>
  33. #include <QBrush>
  34. #include <QTimer>
  35. #include <QDebug>
  36. #include <QFile>
  37. #include <QMenu>
  38. #include <QUrl>
  39. #ifdef GTA5SYNC_TELEMETRY
  40. #include "TelemetryClass.h"
  41. #include <QJsonDocument>
  42. #include <QJsonObject>
  43. #include <QDateTime>
  44. #endif
  45. SavegameWidget::SavegameWidget(QWidget *parent) :
  46. ProfileWidget(parent),
  47. ui(new Ui::SavegameWidget)
  48. {
  49. ui->setupUi(this);
  50. ui->cmdCopy->setVisible(false);
  51. ui->cmdView->setVisible(false);
  52. ui->cmdDelete->setVisible(false);
  53. ui->cbSelected->setVisible(false);
  54. qreal screenRatio = AppEnv::screenRatio();
  55. ui->labSavegamePic->setFixedSize(48 * screenRatio, 27 * screenRatio);
  56. QPixmap savegamePixmap(":/img/savegame.png");
  57. if (screenRatio != 1) savegamePixmap = savegamePixmap.scaledToHeight(ui->labSavegamePic->height(), Qt::SmoothTransformation);
  58. ui->labSavegamePic->setPixmap(savegamePixmap);
  59. QString exportSavegameStr = tr("Export Savegame...");
  60. Q_UNUSED(exportSavegameStr)
  61. labelAutosaveStr = tr("AUTOSAVE - %1\n%2");
  62. labelSaveStr = tr("SAVE %3 - %1\n%2");
  63. ui->SavegameFrame->setMouseTracking(true);
  64. ui->labSavegamePic->setMouseTracking(true);
  65. ui->labSavegameStr->setMouseTracking(true);
  66. ui->cbSelected->setMouseTracking(true);
  67. sgdata = nullptr;
  68. }
  69. SavegameWidget::~SavegameWidget()
  70. {
  71. delete ui;
  72. }
  73. void SavegameWidget::setSavegameData(SavegameData *savegame, QString savegamePath)
  74. {
  75. // BETA CODE
  76. QString savegameString = savegame->getSavegameStr();
  77. QString fileName = QFileInfo(savegame->getSavegameFileName()).fileName();
  78. renderString(savegameString, fileName);
  79. sgdStr = savegameString;
  80. sgdPath = savegamePath;
  81. sgdata = savegame;
  82. }
  83. void SavegameWidget::renderString(const QString &savegameString, const QString &fileName)
  84. {
  85. bool validNumber;
  86. QString savegameName = tr("WRONG FORMAT");
  87. QString savegameDate = tr("WRONG FORMAT");
  88. QStringList savegameNDL = QString(savegameString).split(" - ");
  89. if (savegameNDL.length() >= 2)
  90. {
  91. savegameDate = savegameNDL.at(savegameNDL.length() - 1);
  92. savegameName = QString(savegameString).remove(savegameString.length() - savegameDate.length() - 3, savegameDate.length() + 3);
  93. }
  94. int savegameNumber = QString(fileName).remove(0,5).toInt(&validNumber) + 1;
  95. if (validNumber)
  96. {
  97. if (savegameNumber == 16)
  98. {
  99. ui->labSavegameStr->setText(labelAutosaveStr.arg(savegameDate, savegameName));
  100. }
  101. else
  102. {
  103. ui->labSavegameStr->setText(labelSaveStr.arg(savegameDate, savegameName, QString::number(savegameNumber)));
  104. }
  105. }
  106. else
  107. {
  108. ui->labSavegameStr->setText(labelSaveStr.arg(savegameDate, savegameName, tr("UNKNOWN")));
  109. }
  110. }
  111. void SavegameWidget::retranslate()
  112. {
  113. labelAutosaveStr = tr("AUTOSAVE - %1\n%2");
  114. labelSaveStr = tr("SAVE %3 - %1\n%2");
  115. QString fileName = QFileInfo(sgdata->getSavegameFileName()).fileName();
  116. renderString(sgdStr, fileName);
  117. }
  118. void SavegameWidget::on_cmdCopy_clicked()
  119. {
  120. SavegameCopy::copySavegame(this, sgdPath);
  121. }
  122. void SavegameWidget::on_cmdDelete_clicked()
  123. {
  124. int uchoice = QMessageBox::question(this, tr("Delete Savegame"), tr("Are you sure to delete %1 from your savegames?").arg("\""+sgdStr+"\""), QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
  125. if (uchoice == QMessageBox::Yes)
  126. {
  127. if (!QFile::exists(sgdPath))
  128. {
  129. emit savegameDeleted();
  130. #ifdef GTA5SYNC_TELEMETRY
  131. QSettings telemetrySettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  132. telemetrySettings.beginGroup("Telemetry");
  133. bool pushUsageData = telemetrySettings.value("PushUsageData", false).toBool();
  134. telemetrySettings.endGroup();
  135. if (pushUsageData && Telemetry->canPush())
  136. {
  137. QJsonDocument jsonDocument;
  138. QJsonObject jsonObject;
  139. jsonObject["Type"] = "DeleteSuccess";
  140. jsonObject["ExtraFlags"] = "Savegame";
  141. jsonObject["DeletedTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
  142. jsonDocument.setObject(jsonObject);
  143. Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
  144. }
  145. #endif
  146. }
  147. else if (QFile::remove(sgdPath))
  148. {
  149. #ifdef GTA5SYNC_TELEMETRY
  150. QSettings telemetrySettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  151. telemetrySettings.beginGroup("Telemetry");
  152. bool pushUsageData = telemetrySettings.value("PushUsageData", false).toBool();
  153. telemetrySettings.endGroup();
  154. if (pushUsageData && Telemetry->canPush())
  155. {
  156. QJsonDocument jsonDocument;
  157. QJsonObject jsonObject;
  158. jsonObject["Type"] = "DeleteSuccess";
  159. jsonObject["ExtraFlags"] = "Savegame";
  160. jsonObject["DeletedTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
  161. jsonDocument.setObject(jsonObject);
  162. Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
  163. }
  164. #endif
  165. emit savegameDeleted();
  166. }
  167. else
  168. {
  169. QMessageBox::warning(this, tr("Delete Savegame"), tr("Failed at deleting %1 from your savegames").arg("\""+sgdStr+"\""));
  170. }
  171. }
  172. }
  173. void SavegameWidget::on_cmdView_clicked()
  174. {
  175. SavegameDialog *savegameDialog = new SavegameDialog(this);
  176. savegameDialog->setSavegameData(sgdata, sgdPath, true);
  177. savegameDialog->setModal(true);
  178. #ifdef Q_OS_ANDROID
  179. // Android ...
  180. savegameDialog->showMaximized();
  181. #else
  182. savegameDialog->show();
  183. #endif
  184. savegameDialog->exec();
  185. delete savegameDialog;
  186. }
  187. void SavegameWidget::mousePressEvent(QMouseEvent *ev)
  188. {
  189. ProfileWidget::mousePressEvent(ev);
  190. }
  191. void SavegameWidget::mouseReleaseEvent(QMouseEvent *ev)
  192. {
  193. ProfileWidget::mouseReleaseEvent(ev);
  194. if (ui->cbSelected->isVisible())
  195. {
  196. if (rect().contains(ev->pos()) && ev->button() == Qt::LeftButton)
  197. {
  198. ui->cbSelected->setChecked(!ui->cbSelected->isChecked());
  199. }
  200. }
  201. else
  202. {
  203. if (getContentMode() == 0 && rect().contains(ev->pos()) && ev->button() == Qt::LeftButton)
  204. {
  205. if (ev->modifiers().testFlag(Qt::ShiftModifier))
  206. {
  207. ui->cbSelected->setChecked(!ui->cbSelected->isChecked());
  208. }
  209. else
  210. {
  211. on_cmdView_clicked();
  212. }
  213. }
  214. else if (!ui->cbSelected->isVisible() && getContentMode() == 1 && ev->button() == Qt::LeftButton && ev->modifiers().testFlag(Qt::ShiftModifier))
  215. {
  216. ui->cbSelected->setChecked(!ui->cbSelected->isChecked());
  217. }
  218. }
  219. }
  220. void SavegameWidget::mouseDoubleClickEvent(QMouseEvent *ev)
  221. {
  222. ProfileWidget::mouseDoubleClickEvent(ev);
  223. if (!ui->cbSelected->isVisible() && getContentMode() == 1 && ev->button() == Qt::LeftButton)
  224. {
  225. on_cmdView_clicked();
  226. }
  227. }
  228. void SavegameWidget::setSelected(bool isSelected)
  229. {
  230. ui->cbSelected->setChecked(isSelected);
  231. }
  232. void SavegameWidget::savegameSelected()
  233. {
  234. setSelected(!ui->cbSelected->isChecked());
  235. }
  236. void SavegameWidget::contextMenuEvent(QContextMenuEvent *ev)
  237. {
  238. emit contextMenuTriggered(ev);
  239. }
  240. void SavegameWidget::on_cbSelected_stateChanged(int arg1)
  241. {
  242. if (arg1 == Qt::Checked)
  243. {
  244. emit widgetSelected();
  245. }
  246. else if (arg1 == Qt::Unchecked)
  247. {
  248. emit widgetDeselected();
  249. }
  250. }
  251. bool SavegameWidget::isSelected()
  252. {
  253. return ui->cbSelected->isChecked();
  254. }
  255. void SavegameWidget::setSelectionMode(bool selectionMode)
  256. {
  257. ui->cbSelected->setVisible(selectionMode);
  258. }
  259. void SavegameWidget::selectAllWidgets()
  260. {
  261. emit allWidgetsSelected();
  262. }
  263. void SavegameWidget::deselectAllWidgets()
  264. {
  265. emit allWidgetsDeselected();
  266. }
  267. SavegameData* SavegameWidget::getSavegame()
  268. {
  269. return sgdata;
  270. }
  271. QString SavegameWidget::getWidgetType()
  272. {
  273. return "SavegameWidget";
  274. }