2017-11-22 14:59:40 +01:00
/*****************************************************************************
2018-05-24 22:32:00 +02:00
* gta5view Grand Theft Auto V Profile Viewer
2018-01-11 08:41:00 +01:00
* Copyright ( C ) 2017 - 2018 Syping
2017-11-22 14:59:40 +01:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "JsonEditorDialog.h"
# include "ui_JsonEditorDialog.h"
# include "SnapmaticEditor.h"
# include "AppEnv.h"
2018-07-28 04:55:55 +02:00
# include "config.h"
2017-11-22 14:59:40 +01:00
# include <QStringBuilder>
# include <QJsonDocument>
2018-07-28 04:55:55 +02:00
# include <QJsonObject>
2017-11-22 14:59:40 +01:00
# include <QMessageBox>
# if QT_VERSION >= 0x050200
# include <QFontDatabase>
# include <QDebug>
# endif
2018-07-28 04:55:55 +02:00
# ifdef GTA5SYNC_TELEMETRY
# include "TelemetryClass.h"
# endif
2017-11-22 14:59:40 +01:00
JsonEditorDialog : : JsonEditorDialog ( SnapmaticPicture * picture , QWidget * parent ) :
QDialog ( parent ) , smpic ( picture ) ,
ui ( new Ui : : JsonEditorDialog )
{
// Set Window Flags
setWindowFlags ( windowFlags ( ) ^ Qt : : WindowContextHelpButtonHint ^ Qt : : WindowMinMaxButtonsHint ) ;
ui - > setupUi ( this ) ;
2018-06-07 17:07:30 +02:00
ui - > cmdClose - > setDefault ( true ) ;
2018-01-11 08:41:00 +01:00
ui - > cmdClose - > setFocus ( ) ;
// Set Icon for Close Button
2017-11-22 14:59:40 +01:00
if ( QIcon : : hasThemeIcon ( " dialog-close " ) )
{
ui - > cmdClose - > setIcon ( QIcon : : fromTheme ( " dialog-close " ) ) ;
}
2018-01-11 08:41:00 +01:00
else if ( QIcon : : hasThemeIcon ( " gtk-close " ) )
{
ui - > cmdClose - > setIcon ( QIcon : : fromTheme ( " gtk-close " ) ) ;
}
// Set Icon for Save Button
if ( QIcon : : hasThemeIcon ( " document-save " ) )
{
ui - > cmdSave - > setIcon ( QIcon : : fromTheme ( " document-save " ) ) ;
}
else if ( QIcon : : hasThemeIcon ( " gtk-save " ) )
{
ui - > cmdSave - > setIcon ( QIcon : : fromTheme ( " gtk-save " ) ) ;
}
2017-11-22 14:59:40 +01:00
jsonCode = picture - > getJsonStr ( ) ;
# if QT_VERSION >= 0x050200
ui - > txtJSON - > setFont ( QFontDatabase : : systemFont ( QFontDatabase : : FixedFont ) ) ;
2020-08-25 15:32:32 +02:00
# else
QFont jsonFont = ui - > txtJSON - > font ( ) ;
jsonFont . setStyleHint ( QFont : : Monospace ) ;
jsonFont . setFixedPitch ( true ) ;
ui - > txtJSON - > setFont ( jsonFont ) ;
2017-11-22 14:59:40 +01:00
# endif
2017-12-12 04:45:46 +01:00
QFontMetrics fontMetrics ( ui - > txtJSON - > font ( ) ) ;
2020-08-25 17:29:41 +02:00
# if QT_VERSION >= 0x050B00
ui - > txtJSON - > setTabStopDistance ( fontMetrics . horizontalAdvance ( " " ) ) ;
# else
2017-12-12 04:45:46 +01:00
ui - > txtJSON - > setTabStopWidth ( fontMetrics . width ( " " ) ) ;
2020-08-25 17:29:41 +02:00
# endif
2017-11-22 14:59:40 +01:00
QJsonDocument jsonDocument = QJsonDocument : : fromJson ( jsonCode . toUtf8 ( ) ) ;
ui - > txtJSON - > setStyleSheet ( " QPlainTextEdit{background-color: rgb(46, 47, 48); color: rgb(238, 231, 172);} " ) ;
ui - > txtJSON - > setPlainText ( QString : : fromUtf8 ( jsonDocument . toJson ( QJsonDocument : : Indented ) ) . trimmed ( ) ) ;
jsonHl = new JSHighlighter ( ui - > txtJSON - > document ( ) ) ;
// DPI calculation
qreal screenRatio = AppEnv : : screenRatio ( ) ;
2017-12-12 04:45:46 +01:00
# ifndef Q_OS_MAC
ui - > hlButtons - > setSpacing ( 6 * screenRatio ) ;
ui - > hlButtons - > setContentsMargins ( 9 * screenRatio , 0 , 9 * screenRatio , 0 ) ;
ui - > vlInterface - > setContentsMargins ( 0 , 0 , 0 , 9 * screenRatio ) ;
# else
ui - > hlButtons - > setSpacing ( 6 * screenRatio ) ;
2017-11-22 14:59:40 +01:00
ui - > hlButtons - > setContentsMargins ( 9 * screenRatio , 0 , 9 * screenRatio , 0 ) ;
2017-12-12 04:45:46 +01:00
ui - > vlInterface - > setContentsMargins ( 0 , 0 , 0 , 9 * screenRatio ) ;
# endif
2017-11-22 14:59:40 +01:00
if ( screenRatio > 1 )
{
ui - > lineJSON - > setMinimumHeight ( qRound ( 1 * screenRatio ) ) ;
ui - > lineJSON - > setMaximumHeight ( qRound ( 1 * screenRatio ) ) ;
2019-01-13 14:32:12 +01:00
ui - > lineJSON - > setLineWidth ( qRound ( 1 * screenRatio ) ) ;
2017-11-22 14:59:40 +01:00
}
resize ( 450 * screenRatio , 550 * screenRatio ) ;
}
JsonEditorDialog : : ~ JsonEditorDialog ( )
{
delete jsonHl ;
delete ui ;
}
void JsonEditorDialog : : closeEvent ( QCloseEvent * ev )
{
QString jsonPatched = QString ( ui - > txtJSON - > toPlainText ( ) ) . replace ( " \t " , " " ) ;
QJsonDocument jsonNew = QJsonDocument : : fromJson ( jsonPatched . toUtf8 ( ) ) ;
QJsonDocument jsonOriginal = QJsonDocument : : fromJson ( jsonCode . toUtf8 ( ) ) ;
QString originalCode = QString : : fromUtf8 ( jsonOriginal . toJson ( QJsonDocument : : Compact ) ) ;
QString newCode = QString : : fromUtf8 ( jsonNew . toJson ( QJsonDocument : : Compact ) ) ;
if ( newCode ! = originalCode )
{
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 ) ;
if ( button = = QMessageBox : : Yes )
{
if ( saveJsonContent ( ) )
{
ev - > accept ( ) ;
}
else
{
ev - > ignore ( ) ;
}
return ;
}
else if ( button = = QMessageBox : : No )
{
ev - > accept ( ) ;
return ;
}
else
{
ev - > ignore ( ) ;
return ;
}
}
}
bool JsonEditorDialog : : saveJsonContent ( )
{
QString jsonPatched = QString ( ui - > txtJSON - > toPlainText ( ) ) . replace ( " \t " , " " ) ;
QJsonDocument jsonNew = QJsonDocument : : fromJson ( jsonPatched . toUtf8 ( ) ) ;
if ( ! jsonNew . isEmpty ( ) )
{
QJsonDocument jsonOriginal = QJsonDocument : : fromJson ( jsonCode . toUtf8 ( ) ) ;
QString originalCode = QString : : fromUtf8 ( jsonOriginal . toJson ( QJsonDocument : : Compact ) ) ;
QString newCode = QString : : fromUtf8 ( jsonNew . toJson ( QJsonDocument : : Compact ) ) ;
if ( newCode ! = originalCode )
{
QString currentFilePath = smpic - > getPictureFilePath ( ) ;
QString originalFilePath = smpic - > getOriginalPictureFilePath ( ) ;
QString backupFileName = originalFilePath % " .bak " ;
if ( ! QFile : : exists ( backupFileName ) )
{
QFile : : copy ( currentFilePath , backupFileName ) ;
}
smpic - > setJsonStr ( newCode , true ) ;
if ( ! smpic - > isJsonOk ( ) )
{
2017-12-12 04:45:46 +01:00
QString lastStep = smpic - > getLastStep ( false ) ;
QString readableError ;
if ( lastStep . contains ( " JSONINCOMPLETE " ) & & lastStep . contains ( " JSONERROR " ) )
{
readableError = SnapmaticPicture : : tr ( " JSON is incomplete and malformed " ) ;
}
else if ( lastStep . contains ( " JSONINCOMPLETE " ) )
{
readableError = SnapmaticPicture : : tr ( " JSON is incomplete " ) ;
}
else if ( lastStep . contains ( " JSONERROR " ) )
{
readableError = SnapmaticPicture : : tr ( " JSON is malformed " ) ;
}
else
{
readableError = tr ( " JSON Error " ) ;
}
QMessageBox : : warning ( this , SnapmaticEditor : : tr ( " Snapmatic Properties " ) , SnapmaticEditor : : tr ( " Patching of Snapmatic Properties failed because of %1 " ) . arg ( readableError ) ) ;
2017-11-22 14:59:40 +01:00
smpic - > setJsonStr ( originalCode , true ) ;
return false ;
}
if ( ! smpic - > exportPicture ( currentFilePath ) )
{
QMessageBox : : warning ( this , SnapmaticEditor : : tr ( " Snapmatic Properties " ) , SnapmaticEditor : : tr ( " Patching of Snapmatic Properties failed because of I/O Error " ) ) ;
smpic - > setJsonStr ( originalCode , true ) ;
return false ;
}
jsonCode = newCode ;
2018-07-27 04:26:10 +02:00
smpic - > updateStrings ( ) ;
2017-11-22 14:59:40 +01:00
smpic - > emitUpdate ( ) ;
2018-07-28 04:55:55 +02:00
# ifdef GTA5SYNC_TELEMETRY
QSettings telemetrySettings ( GTA5SYNC_APPVENDOR , GTA5SYNC_APPSTR ) ;
telemetrySettings . beginGroup ( " Telemetry " ) ;
bool pushUsageData = telemetrySettings . value ( " PushUsageData " , false ) . toBool ( ) ;
telemetrySettings . endGroup ( ) ;
if ( pushUsageData & & Telemetry - > canPush ( ) )
{
QJsonDocument jsonDocument ;
QJsonObject jsonObject ;
jsonObject [ " Type " ] = " JSONEdited " ;
jsonObject [ " EditedSize " ] = QString : : number ( smpic - > getContentMaxLength ( ) ) ;
jsonObject [ " EditedTime " ] = QString : : number ( QDateTime : : currentDateTimeUtc ( ) . toTime_t ( ) ) ;
jsonDocument . setObject ( jsonObject ) ;
Telemetry - > push ( TelemetryCategory : : PersonalData , jsonDocument ) ;
}
# endif
2017-11-22 14:59:40 +01:00
return true ;
}
return true ;
}
else
{
QMessageBox : : warning ( this , SnapmaticEditor : : tr ( " Snapmatic Properties " ) , SnapmaticEditor : : tr ( " Patching of Snapmatic Properties failed because of JSON Error " ) ) ;
return false ;
}
}
void JsonEditorDialog : : on_cmdClose_clicked ( )
{
2018-07-28 04:55:55 +02:00
close ( ) ;
2017-11-22 14:59:40 +01:00
}
void JsonEditorDialog : : on_cmdSave_clicked ( )
{
if ( saveJsonContent ( ) )
{
2018-07-28 04:55:55 +02:00
close ( ) ;
2017-11-22 14:59:40 +01:00
}
}