2022-06-19 16:17:38 +08:00
// SPDX-FileCopyrightText: 2022 Gary Wang <wzc782970009@gmail.com>
//
// SPDX-License-Identifier: MIT
2025-07-23 21:20:34 +08:00
2019-09-28 01:18:08 +08:00
#include "mainwindow.h"
2025-07-23 21:20:34 +08:00
2023-07-10 01:07:01 +08:00
#include "settings.h"
2025-07-23 21:20:34 +08:00
2024-12-31 10:57:21 +08:00
#ifdef Q_OS_MACOS
#include "fileopeneventhandler.h"
#endif // Q_OS_MACOS
2025-07-23 21:20:34 +08:00
2019-09-28 01:18:08 +08:00
#include <QApplication>
2019-10-01 10:37:14 +08:00
#include <QCommandLineParser>
2020-01-05 14:30:33 +08:00
#include <QDir>
2019-10-06 17:31:27 +08:00
#include <QTranslator>
2019-10-01 10:37:14 +08:00
#include <QUrl>
2025-07-23 21:20:34 +08:00
2025-05-31 13:10:21 +08:00
using namespace Qt :: Literals :: StringLiterals ;
2025-07-23 21:20:34 +08:00
2019-09-28 01:18:08 +08:00
int main ( int argc , char * argv [])
{
2025-05-31 13:10:21 +08:00
QCoreApplication :: setApplicationName ( u "Pineapple Pictures" _s );
2024-10-22 21:35:50 +08:00
QCoreApplication :: setApplicationVersion ( PPIC_VERSION_STRING );
2024-10-27 13:49:24 +08:00
QGuiApplication :: setHighDpiScaleFactorRoundingPolicy ( Settings :: instance () -> hiDpiScaleFactorBehavior ());
2025-07-23 21:20:34 +08:00
2024-10-27 13:49:24 +08:00
QApplication a ( argc , argv );
2025-07-23 21:20:34 +08:00
2019-10-06 17:31:27 +08:00
QTranslator translator ;
2024-12-24 22:37:16 +08:00
#if defined(TRANSLATION_RESOURCE_EMBEDDING)
2025-05-31 13:10:21 +08:00
const QString qmDir = u ":/i18n/" _s ;
2024-12-24 22:37:16 +08:00
#elif defined(QM_FILE_INSTALL_ABSOLUTE_DIR)
const QString qmDir = QT_STRINGIFY ( QM_FILE_INSTALL_ABSOLUTE_DIR );
2020-01-05 14:30:33 +08:00
#else
2024-12-24 22:37:16 +08:00
const QString qmDir = QDir ( QCoreApplication :: applicationDirPath ()). absoluteFilePath ( "translations" );
2020-01-05 14:30:33 +08:00
#endif
2025-05-31 13:10:21 +08:00
if ( translator . load ( QLocale (), u "PineapplePictures" _s , u "_" _s , qmDir )) {
2024-10-22 21:35:50 +08:00
QCoreApplication :: installTranslator ( & translator );
2020-12-25 13:36:34 +08:00
}
2025-07-23 21:20:34 +08:00
2024-10-22 21:35:50 +08:00
QGuiApplication :: setApplicationDisplayName ( QCoreApplication :: translate ( "main" , "Pineapple Pictures" ));
2025-07-23 21:20:34 +08:00
2024-07-28 16:16:20 +08:00
// commandline options
2025-05-31 13:10:21 +08:00
QCommandLineOption supportedImageFormats ( u "supported-image-formats" _s , QCoreApplication :: translate ( "main" , "List supported image format suffixes, and quit program." ));
2019-10-01 10:37:14 +08:00
// parse commandline arguments
QCommandLineParser parser ;
2024-07-28 16:16:20 +08:00
parser . addOption ( supportedImageFormats );
2019-10-01 10:37:14 +08:00
parser . addPositionalArgument ( "File list" , QCoreApplication :: translate ( "main" , "File list." ));
2019-10-06 17:31:27 +08:00
parser . addHelpOption ();
2019-10-01 10:37:14 +08:00
parser . process ( a );
2025-07-23 21:20:34 +08:00
2024-07-28 16:16:20 +08:00
if ( parser . isSet ( supportedImageFormats )) {
2025-04-09 19:24:27 +08:00
#if QT_VERSION < QT_VERSION_CHECK(6, 9, 0)
2024-07-28 16:16:20 +08:00
fputs ( qPrintable ( MainWindow :: supportedImageFormats (). join ( QChar ( '\n' ))), stdout );
:: exit ( EXIT_SUCCESS );
2025-04-09 19:24:27 +08:00
#else
QCommandLineParser :: showMessageAndExit ( QCommandLineParser :: MessageType :: Information ,
MainWindow :: supportedImageFormats (). join ( QChar ( '\n' )));
#endif
2024-07-28 16:16:20 +08:00
}
2025-07-23 21:20:34 +08:00
2019-09-28 01:18:08 +08:00
MainWindow w ;
w . show ();
2025-07-23 21:20:34 +08:00
2024-12-31 10:57:21 +08:00
#ifdef Q_OS_MACOS
FileOpenEventHandler * fileOpenEventHandler = new FileOpenEventHandler ( & a );
a . installEventFilter ( fileOpenEventHandler );
a . connect ( fileOpenEventHandler , & FileOpenEventHandler :: fileOpen , [ & w ]( const QUrl & url ){
2025-01-03 18:57:25 +08:00
if ( w . isHidden ()) {
w . showNormal ();
} else {
w . activateWindow ();
}
2026-07-13 15:40:59 +08:00
w . showFiles ({ url });
2024-12-31 10:57:21 +08:00
w . initWindowSize ();
});
2025-07-23 21:20:34 +08:00
2025-07-22 00:08:06 +08:00
// Handle dock icon clicks to show hidden window
a . connect ( & a , & QApplication :: applicationStateChanged , [ & w ]( Qt :: ApplicationState state ) {
if ( state == Qt :: ApplicationActive && w . isHidden ()) {
2026-07-13 15:40:59 +08:00
w . showFiles ({});
w . galleryCurrent ( false );
2025-07-22 00:08:06 +08:00
w . showNormal ();
w . raise ();
w . activateWindow ();
}
});
2024-12-31 10:57:21 +08:00
#endif // Q_OS_MACOS
2025-07-23 21:20:34 +08:00
2026-07-13 15:40:59 +08:00
QStringList fileList = parser . positionalArguments ();
w . showFiles ( fileList );
2025-07-23 21:20:34 +08:00
2022-03-11 16:21:56 +08:00
w . initWindowSize ();
2025-07-23 21:20:34 +08:00
2024-10-22 21:35:50 +08:00
return QApplication :: exec ();
2019-09-28 01:18:08 +08:00
}