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

JsonEditorDialog.cpp 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*****************************************************************************
  2. * gta5view Grand Theft Auto V Profile Viewer
  3. * Copyright (C) 2017-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 "JsonEditorDialog.h"
  19. #include "ui_JsonEditorDialog.h"
  20. #include "SnapmaticEditor.h"
  21. #include "AppEnv.h"
  22. #include <QStringBuilder>
  23. #include <QJsonDocument>
  24. #include <QMessageBox>
  25. #if QT_VERSION >= 0x050200
  26. #include <QFontDatabase>
  27. #include <QDebug>
  28. #endif
  29. JsonEditorDialog::JsonEditorDialog(SnapmaticPicture *picture, QWidget *parent) :
  30. QDialog(parent), smpic(picture),
  31. ui(new Ui::JsonEditorDialog)
  32. {
  33. // Set Window Flags
  34. setWindowFlags(windowFlags()^Qt::WindowContextHelpButtonHint^Qt::WindowMinMaxButtonsHint);
  35. ui->setupUi(this);
  36. ui->cmdClose->setDefault(true);
  37. ui->cmdClose->setFocus();
  38. // Set Icon for Close Button
  39. if (QIcon::hasThemeIcon("dialog-close"))
  40. {
  41. ui->cmdClose->setIcon(QIcon::fromTheme("dialog-close"));
  42. }
  43. else if (QIcon::hasThemeIcon("gtk-close"))
  44. {
  45. ui->cmdClose->setIcon(QIcon::fromTheme("gtk-close"));
  46. }
  47. // Set Icon for Save Button
  48. if (QIcon::hasThemeIcon("document-save"))
  49. {
  50. ui->cmdSave->setIcon(QIcon::fromTheme("document-save"));
  51. }
  52. else if (QIcon::hasThemeIcon("gtk-save"))
  53. {
  54. ui->cmdSave->setIcon(QIcon::fromTheme("gtk-save"));
  55. }
  56. jsonCode = picture->getJsonStr();
  57. #if QT_VERSION >= 0x050200
  58. ui->txtJSON->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
  59. #endif
  60. QFontMetrics fontMetrics(ui->txtJSON->font());
  61. ui->txtJSON->setTabStopWidth(fontMetrics.width(" "));
  62. QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonCode.toUtf8());
  63. ui->txtJSON->setStyleSheet("QPlainTextEdit{background-color: rgb(46, 47, 48); color: rgb(238, 231, 172);}");
  64. ui->txtJSON->setPlainText(QString::fromUtf8(jsonDocument.toJson(QJsonDocument::Indented)).trimmed());
  65. jsonHl = new JSHighlighter(ui->txtJSON->document());
  66. // DPI calculation
  67. qreal screenRatio = AppEnv::screenRatio();
  68. #ifndef Q_OS_MAC
  69. ui->hlButtons->setSpacing(6 * screenRatio);
  70. ui->hlButtons->setContentsMargins(9 * screenRatio, 0, 9 * screenRatio, 0);
  71. ui->vlInterface->setContentsMargins(0, 0, 0, 9 * screenRatio);
  72. #else
  73. ui->hlButtons->setSpacing(6 * screenRatio);
  74. ui->hlButtons->setContentsMargins(9 * screenRatio, 0, 9 * screenRatio, 0);
  75. ui->vlInterface->setContentsMargins(0, 0, 0, 9 * screenRatio);
  76. #endif
  77. if (screenRatio > 1)
  78. {
  79. ui->lineJSON->setMinimumHeight(qRound(1 * screenRatio));
  80. ui->lineJSON->setMaximumHeight(qRound(1 * screenRatio));
  81. }
  82. resize(450 * screenRatio, 550 * screenRatio);
  83. }
  84. JsonEditorDialog::~JsonEditorDialog()
  85. {
  86. delete jsonHl;
  87. delete ui;
  88. }
  89. void JsonEditorDialog::closeEvent(QCloseEvent *ev)
  90. {
  91. QString jsonPatched = QString(ui->txtJSON->toPlainText()).replace("\t", " ");
  92. QJsonDocument jsonNew = QJsonDocument::fromJson(jsonPatched.toUtf8());
  93. QJsonDocument jsonOriginal = QJsonDocument::fromJson(jsonCode.toUtf8());
  94. QString originalCode = QString::fromUtf8(jsonOriginal.toJson(QJsonDocument::Compact));
  95. QString newCode = QString::fromUtf8(jsonNew.toJson(QJsonDocument::Compact));
  96. if (newCode != originalCode)
  97. {
  98. QMessageBox::StandardButton button = QMessageBox::warning(this, SnapmaticEditor::tr("Snapmatic Properties"), SnapmaticEditor::tr("<h4>Unsaved changes detected</h4>You want to save the JSON content before you quit?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::Cancel);
  99. if (button == QMessageBox::Yes)
  100. {
  101. if (saveJsonContent())
  102. {
  103. ev->accept();
  104. }
  105. else
  106. {
  107. ev->ignore();
  108. }
  109. return;
  110. }
  111. else if (button == QMessageBox::No)
  112. {
  113. ev->accept();
  114. return;
  115. }
  116. else
  117. {
  118. ev->ignore();
  119. return;
  120. }
  121. }
  122. }
  123. bool JsonEditorDialog::saveJsonContent()
  124. {
  125. QString jsonPatched = QString(ui->txtJSON->toPlainText()).replace("\t", " ");
  126. QJsonDocument jsonNew = QJsonDocument::fromJson(jsonPatched.toUtf8());
  127. if (!jsonNew.isEmpty())
  128. {
  129. QJsonDocument jsonOriginal = QJsonDocument::fromJson(jsonCode.toUtf8());
  130. QString originalCode = QString::fromUtf8(jsonOriginal.toJson(QJsonDocument::Compact));
  131. QString newCode = QString::fromUtf8(jsonNew.toJson(QJsonDocument::Compact));
  132. if (newCode != originalCode)
  133. {
  134. QString currentFilePath = smpic->getPictureFilePath();
  135. QString originalFilePath = smpic->getOriginalPictureFilePath();
  136. QString backupFileName = originalFilePath % ".bak";
  137. if (!QFile::exists(backupFileName))
  138. {
  139. QFile::copy(currentFilePath, backupFileName);
  140. }
  141. smpic->setJsonStr(newCode, true);
  142. if (!smpic->isJsonOk())
  143. {
  144. QString lastStep = smpic->getLastStep(false);
  145. QString readableError;
  146. if (lastStep.contains("JSONINCOMPLETE") && lastStep.contains("JSONERROR"))
  147. {
  148. readableError = SnapmaticPicture::tr("JSON is incomplete and malformed");
  149. }
  150. else if (lastStep.contains("JSONINCOMPLETE"))
  151. {
  152. readableError = SnapmaticPicture::tr("JSON is incomplete");
  153. }
  154. else if (lastStep.contains("JSONERROR"))
  155. {
  156. readableError = SnapmaticPicture::tr("JSON is malformed");
  157. }
  158. else
  159. {
  160. readableError = tr("JSON Error");
  161. }
  162. QMessageBox::warning(this, SnapmaticEditor::tr("Snapmatic Properties"), SnapmaticEditor::tr("Patching of Snapmatic Properties failed because of %1").arg(readableError));
  163. smpic->setJsonStr(originalCode, true);
  164. return false;
  165. }
  166. if (!smpic->exportPicture(currentFilePath))
  167. {
  168. QMessageBox::warning(this, SnapmaticEditor::tr("Snapmatic Properties"), SnapmaticEditor::tr("Patching of Snapmatic Properties failed because of I/O Error"));
  169. smpic->setJsonStr(originalCode, true);
  170. return false;
  171. }
  172. jsonCode = newCode;
  173. smpic->emitUpdate();
  174. return true;
  175. }
  176. return true;
  177. }
  178. else
  179. {
  180. QMessageBox::warning(this, SnapmaticEditor::tr("Snapmatic Properties"), SnapmaticEditor::tr("Patching of Snapmatic Properties failed because of JSON Error"));
  181. return false;
  182. }
  183. }
  184. void JsonEditorDialog::on_cmdClose_clicked()
  185. {
  186. this->close();
  187. }
  188. void JsonEditorDialog::on_cmdSave_clicked()
  189. {
  190. if (saveJsonContent())
  191. {
  192. this->close();
  193. }
  194. }