From c9e3274188564319d1180b74e86d16d018173415 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Fri, 2 Jul 2021 00:06:23 +0800 Subject: [PATCH] chore: use QToolButton instead of QPushButton for bottom bar --- app/actionmanager.cpp | 31 ++++++++++++++++++----- app/actionmanager.h | 6 ++++- app/bottombuttongroup.cpp | 34 ++++++++----------------- app/bottombuttongroup.h | 12 +++------ app/mainwindow.cpp | 53 +++++++++++++++++++++------------------ app/mainwindow.h | 7 ++++-- 6 files changed, 76 insertions(+), 67 deletions(-) diff --git a/app/actionmanager.cpp b/app/actionmanager.cpp index 57bc00d..9401b30 100644 --- a/app/actionmanager.cpp +++ b/app/actionmanager.cpp @@ -4,11 +4,21 @@ #include -#define CREATE_NEW_ACTION(window, action)\ - action = new QAction(window);\ +#define ICON_NAME(name)\ + QStringLiteral(":/icons/" #name "") + +#define SETUP_NEW_ACTION(window, action)\ action->setObjectName(QString::fromUtf8( #action ));\ window->addAction(action); +#define CREATE_NEW_ACTION(window, action)\ + action = new QAction(window);\ + SETUP_NEW_ACTION(window, action) + +#define CREATE_NEW_ICON_ACTION(window, action, iconname)\ + action = new QAction(QIcon(ICON_NAME(iconname)), QString(), window);\ + SETUP_NEW_ACTION(window, action) + ActionManager::ActionManager() { @@ -21,15 +31,19 @@ ActionManager::~ActionManager() void ActionManager::setupAction(MainWindow *mainWindow) { - CREATE_NEW_ACTION(mainWindow, actionZoomIn); - CREATE_NEW_ACTION(mainWindow, actionZoomOut); + 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); + CREATE_NEW_ACTION(mainWindow, actionHorizontalFlip); CREATE_NEW_ACTION(mainWindow, actionFitInView); CREATE_NEW_ACTION(mainWindow, actionFitByWidth); CREATE_NEW_ACTION(mainWindow, actionCopyPixmap); CREATE_NEW_ACTION(mainWindow, actionCopyFilePath); CREATE_NEW_ACTION(mainWindow, actionPaste); - CREATE_NEW_ACTION(mainWindow, actionToggleCheckerboard); CREATE_NEW_ACTION(mainWindow, actionToggleStayOnTop); CREATE_NEW_ACTION(mainWindow, actionToggleProtectMode); CREATE_NEW_ACTION(mainWindow, actionSettings); @@ -46,15 +60,19 @@ void ActionManager::retranslateUi(MainWindow *mainWindow) { Q_UNUSED(mainWindow); + actionActualSize->setText(QCoreApplication::translate("MainWindow", "Actual size", nullptr)); + actionToggleMaximize->setText(QCoreApplication::translate("MainWindow", "Toggle maximize", nullptr)); actionZoomIn->setText(QCoreApplication::translate("MainWindow", "Zoom in", nullptr)); actionZoomOut->setText(QCoreApplication::translate("MainWindow", "Zoom out", nullptr)); + actionToggleCheckerboard->setText(QCoreApplication::translate("MainWindow", "Toggle Checkerboard", nullptr)); + actionRotateClockwise->setText(QCoreApplication::translate("MainWindow", "Rotate right", nullptr)); + actionHorizontalFlip->setText(QCoreApplication::translate("MainWindow", "Flip &Horizontally", nullptr)); actionFitInView->setText("Fit in view"); // TODO: what should it called? actionFitByWidth->setText("Fit by width"); // TODO: what should it called? 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)); - actionToggleCheckerboard->setText(QCoreApplication::translate("MainWindow", "Toggle Checkerboard", nullptr)); actionToggleStayOnTop->setText(QCoreApplication::translate("MainWindow", "Stay on top", nullptr)); actionToggleProtectMode->setText(QCoreApplication::translate("MainWindow", "Protected mode", nullptr)); actionSettings->setText(QCoreApplication::translate("MainWindow", "Configure...", nullptr)); @@ -65,6 +83,7 @@ void ActionManager::retranslateUi(MainWindow *mainWindow) void ActionManager::setupShortcuts() { + actionActualSize->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_0)); actionZoomIn->setShortcut(QKeySequence(QKeySequence::ZoomIn)); actionZoomOut->setShortcut(QKeySequence(QKeySequence::ZoomOut)); actionHorizontalFlip->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R)); diff --git a/app/actionmanager.h b/app/actionmanager.h index 4494b27..e848db4 100644 --- a/app/actionmanager.h +++ b/app/actionmanager.h @@ -16,15 +16,19 @@ public: void setupShortcuts(); public: + QAction *actionActualSize; + QAction *actionToggleMaximize; QAction *actionZoomIn; QAction *actionZoomOut; + QAction *actionToggleCheckerboard; + QAction *actionRotateClockwise; + QAction *actionHorizontalFlip; QAction *actionFitInView; QAction *actionFitByWidth; QAction *actionCopyPixmap; QAction *actionCopyFilePath; QAction *actionPaste; - QAction *actionToggleCheckerboard; QAction *actionToggleStayOnTop; QAction *actionToggleProtectMode; QAction *actionSettings; diff --git a/app/bottombuttongroup.cpp b/app/bottombuttongroup.cpp index c4a9120..54cf72e 100644 --- a/app/bottombuttongroup.cpp +++ b/app/bottombuttongroup.cpp @@ -4,11 +4,11 @@ #include -#include +#include #include #include -BottomButtonGroup::BottomButtonGroup(QWidget *parent) +BottomButtonGroup::BottomButtonGroup(const std::vector &actionList, QWidget *parent) : QGroupBox (parent) , m_opacityHelper(new OpacityHelper(this)) { @@ -23,37 +23,23 @@ BottomButtonGroup::BottomButtonGroup(QWidget *parent) "border-style: none;" "background-color:rgba(0,0,0,120)" "}" - "QPushButton {" + "QToolButton {" "background-color:rgba(225,255,255,0);" "color: white;" "border-style: none;" "}"); - auto newBtn = [](QString text, std::function func) -> QPushButton * { - QPushButton * btn = new QPushButton(QIcon(QStringLiteral(":/icons/") + text), ""); + auto newActionBtn = [this](QAction * action) -> QToolButton * { + QToolButton * btn = new QToolButton(this); + btn->setDefaultAction(action); btn->setIconSize(QSize(40, 40)); btn->setFixedSize(40, 40); - QObject::connect(btn, &QAbstractButton::clicked, btn, func); return btn; }; - addButton(newBtn("zoom-original", [this]() { - emit resetToOriginalBtnClicked(); - })); - addButton(newBtn("view-fullscreen", [this]() { - emit toggleWindowMaximum(); - })); - addButton(newBtn("zoom-in", [this]() { - emit zoomInBtnClicked(); - })); - addButton(newBtn("zoom-out", [this]() { - emit zoomOutBtnClicked(); - })); - addButton(newBtn("view-background-checkerboard", [this]() { - emit toggleCheckerboardBtnClicked(); - })); - addButton(newBtn("object-rotate-right", [this]() { - emit rotateRightBtnClicked(); - })); + + for (QAction * action : actionList) { + addButton(newActionBtn(action)); + } } void BottomButtonGroup::setOpacity(qreal opacity, bool animated) diff --git a/app/bottombuttongroup.h b/app/bottombuttongroup.h index de28b39..d2c8f22 100644 --- a/app/bottombuttongroup.h +++ b/app/bottombuttongroup.h @@ -1,6 +1,8 @@ #ifndef BOTTOMBUTTONGROUP_H #define BOTTOMBUTTONGROUP_H +#include + #include #include @@ -9,19 +11,11 @@ class BottomButtonGroup : public QGroupBox { Q_OBJECT public: - explicit BottomButtonGroup(QWidget *parent = nullptr); + explicit BottomButtonGroup(const std::vector & actionList, QWidget *parent = nullptr); void setOpacity(qreal opacity, bool animated = true); void addButton(QAbstractButton *button); -signals: - void resetToOriginalBtnClicked(); - void toggleWindowMaximum(); - void zoomInBtnClicked(); - void zoomOutBtnClicked(); - void toggleCheckerboardBtnClicked(); - void rotateRightBtnClicked(); - private: OpacityHelper * m_opacityHelper; }; diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index a58bb71..88557a1 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -105,24 +105,16 @@ MainWindow::MainWindow(QWidget *parent) connect(m_nextButton, &QAbstractButton::clicked, this, &MainWindow::galleryNext); - m_bottomButtonGroup = new BottomButtonGroup(this); + m_am->setupAction(this); - connect(m_bottomButtonGroup, &BottomButtonGroup::resetToOriginalBtnClicked, - this, [ = ](){ m_graphicsView->resetScale(); }); - connect(m_bottomButtonGroup, &BottomButtonGroup::toggleWindowMaximum, - this, &MainWindow::toggleMaximize); - connect(m_bottomButtonGroup, &BottomButtonGroup::zoomInBtnClicked, - this, &MainWindow::on_actionZoomIn_triggered); - connect(m_bottomButtonGroup, &BottomButtonGroup::zoomOutBtnClicked, - this, &MainWindow::on_actionZoomOut_triggered); - connect(m_bottomButtonGroup, &BottomButtonGroup::toggleCheckerboardBtnClicked, - this, &MainWindow::toggleCheckerboard); - connect(m_bottomButtonGroup, &BottomButtonGroup::rotateRightBtnClicked, - this, [ = ](){ - m_graphicsView->rotateView(); - m_graphicsView->displayScene(); - m_gv->setVisible(false); - }); + m_bottomButtonGroup = new BottomButtonGroup({ + m_am->actionActualSize, + m_am->actionToggleMaximize, + m_am->actionZoomIn, + m_am->actionZoomOut, + m_am->actionToggleCheckerboard, + m_am->actionRotateClockwise + }, this); m_bottomButtonGroup->setOpacity(0, false); m_gv->setOpacity(0, false); @@ -145,8 +137,6 @@ MainWindow::MainWindow(QWidget *parent) connect(fullscreenShorucut, &QShortcut::activated, this, &MainWindow::toggleFullscreen); - m_am->setupAction(this); - centerWindow(); QTimer::singleShot(0, this, [this](){ @@ -474,11 +464,6 @@ void MainWindow::updateWidgetsPosition() m_gv->move(width() - m_gv->width(), height() - m_gv->height()); } -void MainWindow::toggleCheckerboard() -{ - m_graphicsView->toggleCheckerboard(QGuiApplication::queryKeyboardModifiers().testFlag(Qt::ShiftModifier)); -} - void MainWindow::toggleProtectedMode() { m_protectedMode = !m_protectedMode; @@ -546,6 +531,16 @@ QSize MainWindow::sizeHint() const return QSize(710, 530); } +void MainWindow::on_actionActualSize_triggered() +{ + m_graphicsView->resetScale(); +} + +void MainWindow::on_actionToggleMaximize_triggered() +{ + toggleMaximize(); +} + void MainWindow::on_actionZoomIn_triggered() { if (m_graphicsView->scaleFactor() < 1000) { @@ -622,7 +617,15 @@ void MainWindow::on_actionPaste_triggered() void MainWindow::on_actionToggleCheckerboard_triggered() { - m_graphicsView->toggleCheckerboard(); + // TODO: is that okay to do this since we plan to support custom shortcuts? + m_graphicsView->toggleCheckerboard(QGuiApplication::queryKeyboardModifiers().testFlag(Qt::ShiftModifier)); +} + +void MainWindow::on_actionRotateClockwise_triggered() +{ + m_graphicsView->rotateView(); + m_graphicsView->displayScene(); + m_gv->setVisible(false); } void MainWindow::on_actionToggleStayOnTop_triggered() diff --git a/app/mainwindow.h b/app/mainwindow.h index 59509f5..da8475f 100644 --- a/app/mainwindow.h +++ b/app/mainwindow.h @@ -50,7 +50,6 @@ protected slots: void centerWindow(); void closeWindow(); void updateWidgetsPosition(); - void toggleCheckerboard(); void toggleProtectedMode(); void toggleStayOnTop(); bool stayOnTop() const; @@ -63,15 +62,19 @@ protected: QSize sizeHint() const override; private slots: + void on_actionActualSize_triggered(); + void on_actionToggleMaximize_triggered(); void on_actionZoomIn_triggered(); void on_actionZoomOut_triggered(); + void on_actionToggleCheckerboard_triggered(); + void on_actionRotateClockwise_triggered(); + void on_actionHorizontalFlip_triggered(); void on_actionFitInView_triggered(); void on_actionFitByWidth_triggered(); void on_actionCopyPixmap_triggered(); void on_actionCopyFilePath_triggered(); void on_actionPaste_triggered(); - void on_actionToggleCheckerboard_triggered(); void on_actionToggleStayOnTop_triggered(); void on_actionToggleProtectMode_triggered(); void on_actionSettings_triggered();