2022-06-19 16:17:38 +08:00
// SPDX-FileCopyrightText: 2022 Gary Wang <wzc782970009@gmail.com>
//
// SPDX-License-Identifier: MIT
2021-01-24 00:07:58 +08:00
# include "actionmanager.h"
# include "mainwindow.h"
2022-09-29 13:11:21 +08:00
# include <QGuiApplication>
# include <QSvgRenderer>
# include <QPainter>
2021-01-24 00:07:58 +08:00
2021-07-02 00:06:23 +08:00
# define ICON_NAME(name)\
2022-09-29 13:11:21 +08:00
QStringLiteral ( " :/icons/ " # name " .svg " )
2021-07-02 00:06:23 +08:00
2022-09-29 13:11:21 +08:00
# define ACTION_NAME(s) QStringLiteral(STRIFY(s))
# define STRIFY(s) #s
2021-07-02 00:06:23 +08:00
2021-01-24 00:07:58 +08:00
ActionManager : : ActionManager ( )
{
}
ActionManager : : ~ ActionManager ( )
{
}
2022-09-29 13:11:21 +08:00
QIcon ActionManager : : loadHidpiIcon ( const QString & resp , QSize sz )
{
QSvgRenderer r ( resp ) ;
QPixmap pm = QPixmap ( sz * qApp - > devicePixelRatio ( ) ) ;
pm . fill ( Qt : : transparent ) ;
QPainter p ( & pm ) ;
r . render ( & p ) ;
pm . setDevicePixelRatio ( qApp - > devicePixelRatio ( ) ) ;
return QIcon ( pm ) ;
}
2021-01-24 00:07:58 +08:00
void ActionManager : : setupAction ( MainWindow * mainWindow )
{
2024-04-03 19:56:46 +08:00
auto create_action = [ ] ( QWidget * w , QAction * * a , QString i , QString an , bool iconFromTheme = false ) {
2022-09-29 13:11:21 +08:00
* a = new QAction ( w ) ;
if ( ! i . isNull ( ) )
2024-04-03 19:56:46 +08:00
( * a ) - > setIcon ( iconFromTheme ? QIcon : : fromTheme ( i ) : ActionManager : : loadHidpiIcon ( i ) ) ;
2022-09-29 13:11:21 +08:00
( * a ) - > setObjectName ( an ) ;
w - > addAction ( * a ) ;
} ;
# define CREATE_NEW_ICON_ACTION(w, a, i) create_action(w, &a, ICON_NAME(i), ACTION_NAME(a))
2021-07-02 00:06:23 +08:00
CREATE_NEW_ICON_ACTION ( mainWindow , actionActualSize , zoom - original ) ;
CREATE_NEW_ICON_ACTION ( mainWindow , actionToggleMaximize , view - fullscreen ) ;
CREATE_NEW_ICON_ACTION ( mainWindow , actionZoomIn , zoom - in ) ;
CREATE_NEW_ICON_ACTION ( mainWindow , actionZoomOut , zoom - out ) ;
CREATE_NEW_ICON_ACTION ( mainWindow , actionToggleCheckerboard , view - background - checkerboard ) ;
CREATE_NEW_ICON_ACTION ( mainWindow , actionRotateClockwise , object - rotate - right ) ;
2022-09-29 13:11:21 +08:00
# undef CREATE_NEW_ICON_ACTION
2021-07-02 00:06:23 +08:00
2022-09-29 13:11:21 +08:00
# define CREATE_NEW_ACTION(w, a) create_action(w, &a, QString(), ACTION_NAME(a))
2024-04-03 19:56:46 +08:00
# define CREATE_NEW_THEMEICON_ACTION(w, a, i) create_action(w, &a, QLatin1String(STRIFY(i)), ACTION_NAME(a), true)
2024-07-31 19:41:51 +08:00
CREATE_NEW_ACTION ( mainWindow , actionRotateCounterClockwise ) ;
2021-10-08 14:06:18 +08:00
CREATE_NEW_ACTION ( mainWindow , actionPrevPicture ) ;
CREATE_NEW_ACTION ( mainWindow , actionNextPicture ) ;
2024-04-03 19:56:46 +08:00
CREATE_NEW_THEMEICON_ACTION ( mainWindow , actionOpen , document - open ) ;
2021-03-19 23:11:06 +08:00
CREATE_NEW_ACTION ( mainWindow , actionHorizontalFlip ) ;
2021-06-04 13:53:47 +08:00
CREATE_NEW_ACTION ( mainWindow , actionFitInView ) ;
2021-05-14 00:00:03 +08:00
CREATE_NEW_ACTION ( mainWindow , actionFitByWidth ) ;
2024-04-03 19:56:46 +08:00
CREATE_NEW_THEMEICON_ACTION ( mainWindow , actionCopyPixmap , edit - copy ) ;
2021-01-24 00:07:58 +08:00
CREATE_NEW_ACTION ( mainWindow , actionCopyFilePath ) ;
2024-04-03 19:56:46 +08:00
CREATE_NEW_THEMEICON_ACTION ( mainWindow , actionPaste , edit - paste ) ;
2024-04-15 01:16:00 +08:00
CREATE_NEW_THEMEICON_ACTION ( mainWindow , actionTrash , edit - delete ) ;
2021-01-24 00:07:58 +08:00
CREATE_NEW_ACTION ( mainWindow , actionToggleStayOnTop ) ;
CREATE_NEW_ACTION ( mainWindow , actionToggleProtectMode ) ;
2023-07-09 15:39:59 +08:00
CREATE_NEW_ACTION ( mainWindow , actionToggleAvoidResetTransform ) ;
2021-01-24 00:07:58 +08:00
CREATE_NEW_ACTION ( mainWindow , actionSettings ) ;
2024-04-03 19:56:46 +08:00
CREATE_NEW_THEMEICON_ACTION ( mainWindow , actionHelp , system - help ) ;
CREATE_NEW_THEMEICON_ACTION ( mainWindow , actionLocateInFileManager , system - file - manager ) ;
2024-04-20 13:53:44 +08:00
CREATE_NEW_THEMEICON_ACTION ( mainWindow , actionProperties , document - properties ) ;
2021-01-24 00:07:58 +08:00
CREATE_NEW_ACTION ( mainWindow , actionQuitApp ) ;
2022-09-29 13:11:21 +08:00
# undef CREATE_NEW_ACTION
2024-04-03 19:56:46 +08:00
# undef CREATE_NEW_THEMEICON_ACTION
2021-01-24 00:07:58 +08:00
retranslateUi ( mainWindow ) ;
QMetaObject : : connectSlotsByName ( mainWindow ) ;
}
void ActionManager : : retranslateUi ( MainWindow * mainWindow )
{
Q_UNUSED ( mainWindow ) ;
2021-11-25 22:54:37 +08:00
actionOpen - > setText ( QCoreApplication : : translate ( " MainWindow " , " &Open... " , nullptr ) ) ;
2021-07-02 00:06:23 +08:00
actionActualSize - > setText ( QCoreApplication : : translate ( " MainWindow " , " Actual size " , nullptr ) ) ;
actionToggleMaximize - > setText ( QCoreApplication : : translate ( " MainWindow " , " Toggle maximize " , nullptr ) ) ;
2021-02-18 20:03:17 +08:00
actionZoomIn - > setText ( QCoreApplication : : translate ( " MainWindow " , " Zoom in " , nullptr ) ) ;
actionZoomOut - > setText ( QCoreApplication : : translate ( " MainWindow " , " Zoom out " , nullptr ) ) ;
2021-07-02 00:06:23 +08:00
actionToggleCheckerboard - > setText ( QCoreApplication : : translate ( " MainWindow " , " Toggle Checkerboard " , nullptr ) ) ;
actionRotateClockwise - > setText ( QCoreApplication : : translate ( " MainWindow " , " Rotate right " , nullptr ) ) ;
2024-07-31 19:41:51 +08:00
actionRotateCounterClockwise - > setText ( QCoreApplication : : translate ( " MainWindow " , " Rotate left " , nullptr ) ) ;
2021-07-02 00:06:23 +08:00
2021-10-08 14:06:18 +08:00
actionPrevPicture - > setText ( QCoreApplication : : translate ( " MainWindow " , " Previous image " , nullptr ) ) ;
actionNextPicture - > setText ( QCoreApplication : : translate ( " MainWindow " , " Next image " , nullptr ) ) ;
2021-04-05 23:21:15 +08:00
actionHorizontalFlip - > setText ( QCoreApplication : : translate ( " MainWindow " , " Flip &Horizontally " , nullptr ) ) ;
2024-07-31 19:41:51 +08:00
actionFitInView - > setText ( QCoreApplication : : translate ( " MainWindow " , " Fit to view " , nullptr ) ) ;
actionFitByWidth - > setText ( QCoreApplication : : translate ( " MainWindow " , " Fit to width " , nullptr ) ) ;
2021-01-24 00:07:58 +08:00
actionCopyPixmap - > setText ( QCoreApplication : : translate ( " MainWindow " , " Copy P&ixmap " , nullptr ) ) ;
actionCopyFilePath - > setText ( QCoreApplication : : translate ( " MainWindow " , " Copy &File Path " , nullptr ) ) ;
actionPaste - > setText ( QCoreApplication : : translate ( " MainWindow " , " &Paste " , nullptr ) ) ;
2024-04-15 01:16:00 +08:00
actionTrash - > setText ( QCoreApplication : : translate ( " MainWindow " , " Move to Trash " , nullptr ) ) ;
2021-01-24 00:07:58 +08:00
actionToggleStayOnTop - > setText ( QCoreApplication : : translate ( " MainWindow " , " Stay on top " , nullptr ) ) ;
actionToggleProtectMode - > setText ( QCoreApplication : : translate ( " MainWindow " , " Protected mode " , nullptr ) ) ;
2023-10-15 17:42:46 +08:00
actionToggleAvoidResetTransform - > setText ( QCoreApplication : : translate ( " MainWindow " , " Keep transformation " , " The 'transformation' means the flip/rotation status that currently applied to the image view " ) ) ;
2021-01-24 00:07:58 +08:00
actionSettings - > setText ( QCoreApplication : : translate ( " MainWindow " , " Configure... " , nullptr ) ) ;
actionHelp - > setText ( QCoreApplication : : translate ( " MainWindow " , " Help " , nullptr ) ) ;
2022-02-09 00:04:58 +08:00
# ifdef Q_OS_WIN
actionLocateInFileManager - > setText (
QCoreApplication : : translate (
" MainWindow " , " Show in File Explorer " ,
" File Explorer is the name of explorer.exe under Windows "
)
) ;
# else
actionLocateInFileManager - > setText ( QCoreApplication : : translate ( " MainWindow " , " Show in directory " , nullptr ) ) ;
# endif // Q_OS_WIN
2021-01-24 00:07:58 +08:00
actionProperties - > setText ( QCoreApplication : : translate ( " MainWindow " , " Properties " , nullptr ) ) ;
actionQuitApp - > setText ( QCoreApplication : : translate ( " MainWindow " , " Quit " , nullptr ) ) ;
}
void ActionManager : : setupShortcuts ( )
{
2024-11-02 09:32:51 +08:00
actionOpen - > setShortcut ( QKeySequence : : Open ) ;
2021-10-08 14:06:18 +08:00
actionActualSize - > setShortcut ( QKeySequence ( Qt : : CTRL | Qt : : Key_0 ) ) ;
2024-11-02 09:32:51 +08:00
actionZoomIn - > setShortcut ( QKeySequence : : ZoomIn ) ;
actionZoomOut - > setShortcut ( QKeySequence : : ZoomOut ) ;
2021-12-17 13:36:20 +08:00
actionPrevPicture - > setShortcuts ( {
QKeySequence ( Qt : : Key_PageUp ) ,
QKeySequence ( Qt : : Key_Left ) ,
} ) ;
actionNextPicture - > setShortcuts ( {
QKeySequence ( Qt : : Key_PageDown ) ,
QKeySequence ( Qt : : Key_Right ) ,
} ) ;
2021-10-08 14:06:18 +08:00
actionHorizontalFlip - > setShortcut ( QKeySequence ( Qt : : CTRL | Qt : : Key_R ) ) ;
2024-11-02 09:32:51 +08:00
actionCopyPixmap - > setShortcut ( QKeySequence : : Copy ) ;
2021-04-05 23:21:15 +08:00
actionPaste - > setShortcut ( QKeySequence : : Paste ) ;
2024-04-15 01:16:00 +08:00
actionTrash - > setShortcut ( QKeySequence : : Delete ) ;
2021-04-05 23:21:15 +08:00
actionHelp - > setShortcut ( QKeySequence : : HelpContents ) ;
actionSettings - > setShortcut ( QKeySequence : : Preferences ) ;
2021-10-08 14:06:18 +08:00
actionProperties - > setShortcut ( QKeySequence ( Qt : : CTRL | Qt : : Key_I ) ) ;
2021-01-24 00:07:58 +08:00
actionQuitApp - > setShortcuts ( {
QKeySequence ( Qt : : Key_Space ) ,
QKeySequence ( Qt : : Key_Escape )
} ) ;
}