2017-10-09 08:35:48 +02:00
/*****************************************************************************
2018-05-24 22:32:00 +02:00
* gta5view Grand Theft Auto V Profile Viewer
2023-10-05 17:10:01 +02:00
* Copyright ( C ) 2016 - 2023 Syping
2017-10-09 08:35:48 +02: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 "config.h"
2020-04-24 05:36:11 +02:00
# include "AppEnv.h"
2017-10-09 08:35:48 +02:00
# include "PictureExport.h"
# include "PictureDialog.h"
# include "StandardPaths.h"
# include <QStringBuilder>
# include <QApplication>
# include <QMessageBox>
# include <QFileDialog>
# include <QSettings>
2017-10-12 22:21:45 +02:00
# include <QSaveFile>
2020-04-24 05:36:11 +02:00
# include <QScreen>
2023-10-06 00:20:48 +02:00
# include <QDebug>
2017-10-12 22:21:45 +02:00
2017-10-09 08:35:48 +02:00
PictureExport : : PictureExport ( )
{
}
void PictureExport : : exportAsPicture ( QWidget * parent , SnapmaticPicture * picture )
{
QSettings settings ( GTA5SYNC_APPVENDOR , GTA5SYNC_APPSTR ) ;
// Picture Settings
// Quality Settings
settings . beginGroup ( " Pictures " ) ;
QString defaultExportFormat = settings . value ( " DefaultExportFormat " , " .jpg " ) . toString ( ) ;
settings . endGroup ( ) ;
// End Picture Settings
settings . beginGroup ( " FileDialogs " ) ;
2017-10-12 22:21:45 +02:00
bool dontUseNativeDialog = settings . value ( " DontUseNativeDialog " , false ) . toBool ( ) ;
2017-10-09 08:35:48 +02:00
settings . beginGroup ( " ExportAsPicture " ) ;
fileDialogPreSave : //Work?
QFileDialog fileDialog ( parent ) ;
fileDialog . setFileMode ( QFileDialog : : AnyFile ) ;
fileDialog . setViewMode ( QFileDialog : : Detail ) ;
fileDialog . setAcceptMode ( QFileDialog : : AcceptSave ) ;
2017-10-12 22:21:45 +02:00
fileDialog . setOption ( QFileDialog : : DontUseNativeDialog , dontUseNativeDialog ) ;
2017-10-09 08:35:48 +02:00
fileDialog . setOption ( QFileDialog : : DontConfirmOverwrite , true ) ;
fileDialog . setDefaultSuffix ( " suffix " ) ;
2023-10-06 00:20:48 +02:00
fileDialog . setWindowFlag ( Qt : : WindowContextHelpButtonHint , false ) ;
2017-10-09 08:35:48 +02:00
fileDialog . setWindowTitle ( PictureDialog : : tr ( " Export as Picture... " ) ) ;
fileDialog . setLabelText ( QFileDialog : : Accept , PictureDialog : : tr ( " Export " ) ) ;
QStringList filters ;
filters < < PictureDialog : : tr ( " JPEG Graphics (*.jpg *.jpeg) " ) ;
filters < < PictureDialog : : tr ( " Portable Network Graphics (*.png) " ) ;
fileDialog . setNameFilters ( filters ) ;
fileDialog . setDirectory ( settings . value ( " Directory " , StandardPaths : : picturesLocation ( ) ) . toString ( ) ) ;
2017-10-12 22:21:45 +02:00
fileDialog . restoreGeometry ( settings . value ( parent - > objectName ( ) % " +Geometry " , " " ) . toByteArray ( ) ) ;
2017-10-09 08:35:48 +02:00
QString newPictureFileName = getPictureFileName ( picture ) % defaultExportFormat ;
fileDialog . selectFile ( newPictureFileName ) ;
2020-11-25 00:36:12 +01:00
if ( fileDialog . exec ( ) ) {
2017-10-09 08:35:48 +02:00
QStringList selectedFiles = fileDialog . selectedFiles ( ) ;
2020-11-25 00:36:12 +01:00
if ( selectedFiles . length ( ) = = 1 ) {
2017-10-09 08:35:48 +02:00
QString saveFileFormat ;
QString selectedFile = selectedFiles . at ( 0 ) ;
2023-10-06 00:20:48 +02:00
if ( selectedFile . endsWith ( " .jpg " ) | | selectedFile . endsWith ( " .jpeg " ) ) {
2017-10-09 08:35:48 +02:00
saveFileFormat = " JPEG " ;
}
2023-10-06 00:20:48 +02:00
else if ( selectedFile . endsWith ( " .png " ) ) {
2017-10-09 08:35:48 +02:00
saveFileFormat = " PNG " ;
}
2023-10-06 00:20:48 +02:00
else if ( selectedFile . endsWith ( " .suffix " ) ) {
2020-11-25 00:36:12 +01:00
if ( fileDialog . selectedNameFilter ( ) = = " JPEG picture (*.jpg) " ) {
2017-10-09 08:35:48 +02:00
selectedFile . replace ( " .suffix " , " .jpg " ) ;
}
2020-11-25 00:36:12 +01:00
else if ( fileDialog . selectedNameFilter ( ) = = " Portable Network Graphics (*.png) " ) {
2017-10-09 08:35:48 +02:00
selectedFile . replace ( " .suffix " , " .png " ) ;
}
2020-11-25 00:36:12 +01:00
else {
2017-10-09 08:35:48 +02:00
selectedFile . replace ( " .suffix " , " .jpg " ) ;
}
}
2020-11-25 00:36:12 +01:00
if ( QFile : : exists ( selectedFile ) ) {
if ( QMessageBox : : No = = QMessageBox : : warning ( parent , PictureDialog : : tr ( " Export as Picture " ) , PictureDialog : : tr ( " Overwrite %1 with current Snapmatic picture? " ) . arg ( " \" " + selectedFile + " \" " ) , QMessageBox : : Yes | QMessageBox : : No , QMessageBox : : Yes ) ) {
2017-10-09 08:35:48 +02:00
goto fileDialogPreSave ; //Work?
}
}
QImage exportPicture = picture - > getImage ( ) ;
2017-10-12 22:21:45 +02:00
int errorId = 0 ;
bool isSaved = false ;
2023-10-06 00:20:48 +02:00
QSaveFile picFile ( selectedFile ) ;
if ( picFile . open ( QIODevice : : WriteOnly ) ) {
isSaved = exportPicture . save ( & picFile , saveFileFormat . toStdString ( ) . c_str ( ) , 100 ) ;
2020-11-25 00:36:12 +01:00
if ( isSaved ) {
2023-10-06 00:20:48 +02:00
isSaved = picFile . commit ( ) ;
2017-10-12 22:21:45 +02:00
}
2020-11-25 00:36:12 +01:00
else {
2017-10-12 22:21:45 +02:00
errorId = 1 ;
}
2017-10-09 08:35:48 +02:00
}
2020-11-25 00:36:12 +01:00
else {
2017-10-12 22:21:45 +02:00
errorId = 2 ;
2017-10-09 08:35:48 +02:00
}
2020-11-25 00:36:12 +01:00
if ( ! isSaved ) {
switch ( errorId ) {
2017-10-12 22:21:45 +02:00
case 0 :
QMessageBox : : warning ( parent , PictureDialog : : tr ( " Export as Picture " ) , PictureDialog : : tr ( " Failed to export the picture because the system occurred a write failure " ) ) ;
break ;
case 1 :
QMessageBox : : warning ( parent , PictureDialog : : tr ( " Export as Picture " ) , PictureDialog : : tr ( " Failed to export the picture because the format detection failures " ) ) ;
break ;
case 2 :
QMessageBox : : warning ( parent , PictureDialog : : tr ( " Export as Picture " ) , PictureDialog : : tr ( " Failed to export the picture because the file can't be written " ) ) ;
break ;
default :
QMessageBox : : warning ( parent , PictureDialog : : tr ( " Export as Picture " ) , PictureDialog : : tr ( " Failed to export the picture because of an unknown reason " ) ) ;
}
2017-10-09 08:35:48 +02:00
goto fileDialogPreSave ; //Work?
}
}
2020-11-25 00:36:12 +01:00
else {
2017-10-09 08:35:48 +02:00
QMessageBox : : warning ( parent , PictureDialog : : tr ( " Export as Picture " ) , PictureDialog : : tr ( " No valid file is selected " ) ) ;
goto fileDialogPreSave ; //Work?
}
}
settings . setValue ( parent - > objectName ( ) % " +Geometry " , fileDialog . saveGeometry ( ) ) ;
settings . setValue ( " Directory " , fileDialog . directory ( ) . absolutePath ( ) ) ;
settings . endGroup ( ) ;
settings . endGroup ( ) ;
}
void PictureExport : : exportAsSnapmatic ( QWidget * parent , SnapmaticPicture * picture )
{
QSettings settings ( GTA5SYNC_APPVENDOR , GTA5SYNC_APPSTR ) ;
settings . beginGroup ( " FileDialogs " ) ;
2017-10-12 22:21:45 +02:00
bool dontUseNativeDialog = settings . value ( " DontUseNativeDialog " , false ) . toBool ( ) ;
2017-10-09 08:35:48 +02:00
settings . beginGroup ( " ExportAsSnapmatic " ) ;
2017-10-12 22:21:45 +02:00
QString adjustedPicPath = picture - > getOriginalPictureFileName ( ) ;
2017-10-09 08:35:48 +02:00
fileDialogPreSave : //Work?
QFileInfo sgdFileInfo ( adjustedPicPath ) ;
QFileDialog fileDialog ( parent ) ;
fileDialog . setFileMode ( QFileDialog : : AnyFile ) ;
fileDialog . setViewMode ( QFileDialog : : Detail ) ;
fileDialog . setAcceptMode ( QFileDialog : : AcceptSave ) ;
2017-10-12 22:21:45 +02:00
fileDialog . setOption ( QFileDialog : : DontUseNativeDialog , dontUseNativeDialog ) ;
2017-10-09 08:35:48 +02:00
fileDialog . setOption ( QFileDialog : : DontConfirmOverwrite , true ) ;
fileDialog . setDefaultSuffix ( " .rem " ) ;
2023-10-06 00:20:48 +02:00
fileDialog . setWindowFlag ( Qt : : WindowContextHelpButtonHint , false ) ;
2017-10-09 08:35:48 +02:00
fileDialog . setWindowTitle ( PictureDialog : : tr ( " Export as Snapmatic... " ) ) ;
fileDialog . setLabelText ( QFileDialog : : Accept , PictureDialog : : tr ( " Export " ) ) ;
QStringList filters ;
filters < < PictureDialog : : tr ( " GTA V Export (*.g5e) " ) ;
2020-11-25 00:36:12 +01:00
# ifndef GTA5SYNC_FLATPAK
2017-10-09 08:35:48 +02:00
filters < < PictureDialog : : tr ( " GTA V Raw Export (*.auto) " ) ;
2020-11-25 00:36:12 +01:00
# endif
2017-10-09 08:35:48 +02:00
filters < < PictureDialog : : tr ( " Snapmatic pictures (PGTA*) " ) ;
fileDialog . setNameFilters ( filters ) ;
fileDialog . setDirectory ( settings . value ( " Directory " , StandardPaths : : documentsLocation ( ) ) . toString ( ) ) ;
2017-10-12 22:21:45 +02:00
fileDialog . restoreGeometry ( settings . value ( parent - > objectName ( ) % " +Geometry " , " " ) . toByteArray ( ) ) ;
2017-10-09 08:35:48 +02:00
fileDialog . selectFile ( QString ( picture - > getExportPictureFileName ( ) % " .g5e " ) ) ;
2020-11-25 00:36:12 +01:00
if ( fileDialog . exec ( ) ) {
2017-10-09 08:35:48 +02:00
QStringList selectedFiles = fileDialog . selectedFiles ( ) ;
2020-11-25 00:36:12 +01:00
if ( selectedFiles . length ( ) = = 1 ) {
2017-10-09 08:35:48 +02:00
QString selectedFile = selectedFiles . at ( 0 ) ;
bool isAutoExt = false ;
2020-11-25 00:36:12 +01:00
# ifndef GTA5SYNC_FLATPAK
2023-10-06 00:20:48 +02:00
if ( selectedFile . endsWith ( " .auto " ) ) {
2017-10-09 08:35:48 +02:00
isAutoExt = true ;
QString dirPath = QFileInfo ( selectedFile ) . dir ( ) . path ( ) ;
QString stockFileName = sgdFileInfo . fileName ( ) ;
selectedFile = dirPath % " / " % stockFileName ;
}
2020-11-25 00:36:12 +01:00
# endif
2023-10-06 00:20:48 +02:00
if ( selectedFile . endsWith ( " .rem " ) ) {
2017-10-09 08:35:48 +02:00
selectedFile . remove ( selectedFile . length ( ) - 4 , 4 ) ;
}
2020-11-25 00:36:12 +01:00
if ( QFile : : exists ( selectedFile ) ) {
if ( QMessageBox : : No = = QMessageBox : : warning ( parent , PictureDialog : : tr ( " Export as Snapmatic " ) , PictureDialog : : tr ( " Overwrite %1 with current Snapmatic picture? " ) . arg ( " \" " + selectedFile + " \" " ) , QMessageBox : : Yes | QMessageBox : : No , QMessageBox : : Yes ) ) {
2017-10-09 08:35:48 +02:00
goto fileDialogPreSave ; //Work?
}
}
2023-10-06 00:20:48 +02:00
if ( selectedFile . endsWith ( " .g5e " ) ) {
2017-10-09 08:35:48 +02:00
bool isExported = picture - > exportPicture ( selectedFile , SnapmaticFormat : : G5E_Format ) ;
2020-11-25 00:36:12 +01:00
if ( ! isExported ) {
2017-10-09 08:35:48 +02:00
QMessageBox : : warning ( parent , PictureDialog : : tr ( " Export as Snapmatic " ) , PictureDialog : : tr ( " Failed to export current Snapmatic picture " ) ) ;
goto fileDialogPreSave ; //Work?
}
}
2020-11-25 00:36:12 +01:00
else {
2023-02-27 14:17:50 +01:00
bool isCopied = picture - > exportPicture ( selectedFile , SnapmaticFormat : : PGTA5_Format ) ;
2020-11-25 00:36:12 +01:00
if ( ! isCopied ) {
2017-10-09 08:35:48 +02:00
QMessageBox : : warning ( parent , PictureDialog : : tr ( " Export as Snapmatic " ) , PictureDialog : : tr ( " Failed to export current Snapmatic picture " ) ) ;
goto fileDialogPreSave ; //Work?
}
2020-11-25 00:36:12 +01:00
else {
2017-10-09 08:35:48 +02:00
if ( isAutoExt ) QMessageBox : : information ( parent , PictureDialog : : tr ( " Export as Snapmatic " ) , PictureDialog : : tr ( " Exported Snapmatic to \" %1 \" because of using the .auto extension. " ) . arg ( selectedFile ) ) ;
}
}
}
2020-11-25 00:36:12 +01:00
else {
2017-10-09 08:35:48 +02:00
QMessageBox : : warning ( parent , PictureDialog : : tr ( " Export as Snapmatic " ) , PictureDialog : : tr ( " No valid file is selected " ) ) ;
goto fileDialogPreSave ; //Work?
}
}
settings . setValue ( parent - > objectName ( ) % " +Geometry " , fileDialog . saveGeometry ( ) ) ;
settings . setValue ( " Directory " , fileDialog . directory ( ) . absolutePath ( ) ) ;
settings . endGroup ( ) ;
}
QString PictureExport : : getPictureFileName ( SnapmaticPicture * picture )
{
return picture - > getExportPictureFileName ( ) ;
}