From f202bb58f49b2b6b045891ac9572644d676908be Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Thu, 25 Nov 2021 22:54:37 +0800 Subject: [PATCH] feat: new action for open a dialog to select image files --- app/actionmanager.cpp | 4 ++++ app/actionmanager.h | 2 ++ app/mainwindow.cpp | 15 +++++++++++++++ app/mainwindow.h | 2 ++ 4 files changed, 23 insertions(+) diff --git a/app/actionmanager.cpp b/app/actionmanager.cpp index 875d9bc..5ea2b85 100644 --- a/app/actionmanager.cpp +++ b/app/actionmanager.cpp @@ -41,6 +41,7 @@ void ActionManager::setupAction(MainWindow *mainWindow) CREATE_NEW_ACTION(mainWindow, actionPrevPicture); CREATE_NEW_ACTION(mainWindow, actionNextPicture); + CREATE_NEW_ACTION(mainWindow, actionOpen); CREATE_NEW_ACTION(mainWindow, actionHorizontalFlip); CREATE_NEW_ACTION(mainWindow, actionFitInView); CREATE_NEW_ACTION(mainWindow, actionFitByWidth); @@ -63,6 +64,8 @@ void ActionManager::retranslateUi(MainWindow *mainWindow) { Q_UNUSED(mainWindow); + actionOpen->setText(QCoreApplication::translate("MainWindow", "&Open...", nullptr)); + actionActualSize->setText(QCoreApplication::translate("MainWindow", "Actual size", nullptr)); actionToggleMaximize->setText(QCoreApplication::translate("MainWindow", "Toggle maximize", nullptr)); actionZoomIn->setText(QCoreApplication::translate("MainWindow", "Zoom in", nullptr)); @@ -89,6 +92,7 @@ void ActionManager::retranslateUi(MainWindow *mainWindow) void ActionManager::setupShortcuts() { + actionOpen->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_O)); actionActualSize->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_0)); actionZoomIn->setShortcut(QKeySequence(QKeySequence::ZoomIn)); actionZoomOut->setShortcut(QKeySequence(QKeySequence::ZoomOut)); diff --git a/app/actionmanager.h b/app/actionmanager.h index f2aacfb..2577fde 100644 --- a/app/actionmanager.h +++ b/app/actionmanager.h @@ -16,6 +16,8 @@ public: void setupShortcuts(); public: + QAction *actionOpen; + QAction *actionActualSize; QAction *actionToggleMaximize; QAction *actionZoomIn; diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index d47be05..266eff3 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -27,6 +27,8 @@ #include #include #include +#include +#include MainWindow::MainWindow(QWidget *parent) : FramelessWindow(parent) @@ -406,6 +408,8 @@ void MainWindow::contextMenuEvent(QContextMenuEvent *event) QAction * helpAction = m_am->actionHelp; QAction * propertiesAction = m_am->actionProperties; + menu->addAction(m_am->actionOpen); + if (copyMenu->actions().count() == 1) { menu->addActions(copyMenu->actions()); } else { @@ -540,6 +544,17 @@ QSize MainWindow::sizeHint() const return QSize(710, 530); } +void MainWindow::on_actionOpen_triggered() +{ + QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation); + QUrl pictureUrl = picturesLocations.isEmpty() ? QUrl::fromLocalFile(picturesLocations.first()) + : QUrl::fromLocalFile(QDir::homePath()); + QList urls(QFileDialog::getOpenFileUrls(this, QString(), pictureUrl)); + if (!urls.isEmpty()) { + showUrls(urls); + } +} + void MainWindow::on_actionActualSize_triggered() { m_graphicsView->resetScale(); diff --git a/app/mainwindow.h b/app/mainwindow.h index 29c9d1a..e8e7fd1 100644 --- a/app/mainwindow.h +++ b/app/mainwindow.h @@ -68,6 +68,8 @@ protected: QSize sizeHint() const override; private slots: + void on_actionOpen_triggered(); + void on_actionActualSize_triggered(); void on_actionToggleMaximize_triggered(); void on_actionZoomIn_triggered();