chore: use QToolButton instead of QPushButton for bottom bar

This commit is contained in:
Gary Wang 2021-07-02 00:06:23 +08:00
parent e20562cf16
commit c9e3274188
6 changed files with 76 additions and 67 deletions

View File

@ -4,11 +4,21 @@
#include <QCoreApplication> #include <QCoreApplication>
#define CREATE_NEW_ACTION(window, action)\ #define ICON_NAME(name)\
action = new QAction(window);\ QStringLiteral(":/icons/" #name "")
#define SETUP_NEW_ACTION(window, action)\
action->setObjectName(QString::fromUtf8( #action ));\ action->setObjectName(QString::fromUtf8( #action ));\
window->addAction(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() ActionManager::ActionManager()
{ {
@ -21,15 +31,19 @@ ActionManager::~ActionManager()
void ActionManager::setupAction(MainWindow *mainWindow) void ActionManager::setupAction(MainWindow *mainWindow)
{ {
CREATE_NEW_ACTION(mainWindow, actionZoomIn); CREATE_NEW_ICON_ACTION(mainWindow, actionActualSize, zoom-original);
CREATE_NEW_ACTION(mainWindow, actionZoomOut); 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, actionHorizontalFlip);
CREATE_NEW_ACTION(mainWindow, actionFitInView); CREATE_NEW_ACTION(mainWindow, actionFitInView);
CREATE_NEW_ACTION(mainWindow, actionFitByWidth); CREATE_NEW_ACTION(mainWindow, actionFitByWidth);
CREATE_NEW_ACTION(mainWindow, actionCopyPixmap); CREATE_NEW_ACTION(mainWindow, actionCopyPixmap);
CREATE_NEW_ACTION(mainWindow, actionCopyFilePath); CREATE_NEW_ACTION(mainWindow, actionCopyFilePath);
CREATE_NEW_ACTION(mainWindow, actionPaste); CREATE_NEW_ACTION(mainWindow, actionPaste);
CREATE_NEW_ACTION(mainWindow, actionToggleCheckerboard);
CREATE_NEW_ACTION(mainWindow, actionToggleStayOnTop); CREATE_NEW_ACTION(mainWindow, actionToggleStayOnTop);
CREATE_NEW_ACTION(mainWindow, actionToggleProtectMode); CREATE_NEW_ACTION(mainWindow, actionToggleProtectMode);
CREATE_NEW_ACTION(mainWindow, actionSettings); CREATE_NEW_ACTION(mainWindow, actionSettings);
@ -46,15 +60,19 @@ void ActionManager::retranslateUi(MainWindow *mainWindow)
{ {
Q_UNUSED(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)); actionZoomIn->setText(QCoreApplication::translate("MainWindow", "Zoom in", nullptr));
actionZoomOut->setText(QCoreApplication::translate("MainWindow", "Zoom out", 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)); actionHorizontalFlip->setText(QCoreApplication::translate("MainWindow", "Flip &Horizontally", nullptr));
actionFitInView->setText("Fit in view"); // TODO: what should it called? actionFitInView->setText("Fit in view"); // TODO: what should it called?
actionFitByWidth->setText("Fit by width"); // TODO: what should it called? actionFitByWidth->setText("Fit by width"); // TODO: what should it called?
actionCopyPixmap->setText(QCoreApplication::translate("MainWindow", "Copy P&ixmap", nullptr)); actionCopyPixmap->setText(QCoreApplication::translate("MainWindow", "Copy P&ixmap", nullptr));
actionCopyFilePath->setText(QCoreApplication::translate("MainWindow", "Copy &File Path", nullptr)); actionCopyFilePath->setText(QCoreApplication::translate("MainWindow", "Copy &File Path", nullptr));
actionPaste->setText(QCoreApplication::translate("MainWindow", "&Paste", 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)); actionToggleStayOnTop->setText(QCoreApplication::translate("MainWindow", "Stay on top", nullptr));
actionToggleProtectMode->setText(QCoreApplication::translate("MainWindow", "Protected mode", nullptr)); actionToggleProtectMode->setText(QCoreApplication::translate("MainWindow", "Protected mode", nullptr));
actionSettings->setText(QCoreApplication::translate("MainWindow", "Configure...", nullptr)); actionSettings->setText(QCoreApplication::translate("MainWindow", "Configure...", nullptr));
@ -65,6 +83,7 @@ void ActionManager::retranslateUi(MainWindow *mainWindow)
void ActionManager::setupShortcuts() void ActionManager::setupShortcuts()
{ {
actionActualSize->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_0));
actionZoomIn->setShortcut(QKeySequence(QKeySequence::ZoomIn)); actionZoomIn->setShortcut(QKeySequence(QKeySequence::ZoomIn));
actionZoomOut->setShortcut(QKeySequence(QKeySequence::ZoomOut)); actionZoomOut->setShortcut(QKeySequence(QKeySequence::ZoomOut));
actionHorizontalFlip->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R)); actionHorizontalFlip->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));

View File

@ -16,15 +16,19 @@ public:
void setupShortcuts(); void setupShortcuts();
public: public:
QAction *actionActualSize;
QAction *actionToggleMaximize;
QAction *actionZoomIn; QAction *actionZoomIn;
QAction *actionZoomOut; QAction *actionZoomOut;
QAction *actionToggleCheckerboard;
QAction *actionRotateClockwise;
QAction *actionHorizontalFlip; QAction *actionHorizontalFlip;
QAction *actionFitInView; QAction *actionFitInView;
QAction *actionFitByWidth; QAction *actionFitByWidth;
QAction *actionCopyPixmap; QAction *actionCopyPixmap;
QAction *actionCopyFilePath; QAction *actionCopyFilePath;
QAction *actionPaste; QAction *actionPaste;
QAction *actionToggleCheckerboard;
QAction *actionToggleStayOnTop; QAction *actionToggleStayOnTop;
QAction *actionToggleProtectMode; QAction *actionToggleProtectMode;
QAction *actionSettings; QAction *actionSettings;

View File

@ -4,11 +4,11 @@
#include <functional> #include <functional>
#include <QPushButton> #include <QToolButton>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <QDebug> #include <QDebug>
BottomButtonGroup::BottomButtonGroup(QWidget *parent) BottomButtonGroup::BottomButtonGroup(const std::vector<QAction *> &actionList, QWidget *parent)
: QGroupBox (parent) : QGroupBox (parent)
, m_opacityHelper(new OpacityHelper(this)) , m_opacityHelper(new OpacityHelper(this))
{ {
@ -23,37 +23,23 @@ BottomButtonGroup::BottomButtonGroup(QWidget *parent)
"border-style: none;" "border-style: none;"
"background-color:rgba(0,0,0,120)" "background-color:rgba(0,0,0,120)"
"}" "}"
"QPushButton {" "QToolButton {"
"background-color:rgba(225,255,255,0);" "background-color:rgba(225,255,255,0);"
"color: white;" "color: white;"
"border-style: none;" "border-style: none;"
"}"); "}");
auto newBtn = [](QString text, std::function<void()> func) -> QPushButton * { auto newActionBtn = [this](QAction * action) -> QToolButton * {
QPushButton * btn = new QPushButton(QIcon(QStringLiteral(":/icons/") + text), ""); QToolButton * btn = new QToolButton(this);
btn->setDefaultAction(action);
btn->setIconSize(QSize(40, 40)); btn->setIconSize(QSize(40, 40));
btn->setFixedSize(40, 40); btn->setFixedSize(40, 40);
QObject::connect(btn, &QAbstractButton::clicked, btn, func);
return btn; return btn;
}; };
addButton(newBtn("zoom-original", [this]() {
emit resetToOriginalBtnClicked(); for (QAction * action : actionList) {
})); addButton(newActionBtn(action));
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();
}));
} }
void BottomButtonGroup::setOpacity(qreal opacity, bool animated) void BottomButtonGroup::setOpacity(qreal opacity, bool animated)

View File

@ -1,6 +1,8 @@
#ifndef BOTTOMBUTTONGROUP_H #ifndef BOTTOMBUTTONGROUP_H
#define BOTTOMBUTTONGROUP_H #define BOTTOMBUTTONGROUP_H
#include <vector>
#include <QAbstractButton> #include <QAbstractButton>
#include <QGroupBox> #include <QGroupBox>
@ -9,19 +11,11 @@ class BottomButtonGroup : public QGroupBox
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit BottomButtonGroup(QWidget *parent = nullptr); explicit BottomButtonGroup(const std::vector<QAction *> & actionList, QWidget *parent = nullptr);
void setOpacity(qreal opacity, bool animated = true); void setOpacity(qreal opacity, bool animated = true);
void addButton(QAbstractButton *button); void addButton(QAbstractButton *button);
signals:
void resetToOriginalBtnClicked();
void toggleWindowMaximum();
void zoomInBtnClicked();
void zoomOutBtnClicked();
void toggleCheckerboardBtnClicked();
void rotateRightBtnClicked();
private: private:
OpacityHelper * m_opacityHelper; OpacityHelper * m_opacityHelper;
}; };

View File

@ -105,24 +105,16 @@ MainWindow::MainWindow(QWidget *parent)
connect(m_nextButton, &QAbstractButton::clicked, connect(m_nextButton, &QAbstractButton::clicked,
this, &MainWindow::galleryNext); this, &MainWindow::galleryNext);
m_bottomButtonGroup = new BottomButtonGroup(this); m_am->setupAction(this);
connect(m_bottomButtonGroup, &BottomButtonGroup::resetToOriginalBtnClicked, m_bottomButtonGroup = new BottomButtonGroup({
this, [ = ](){ m_graphicsView->resetScale(); }); m_am->actionActualSize,
connect(m_bottomButtonGroup, &BottomButtonGroup::toggleWindowMaximum, m_am->actionToggleMaximize,
this, &MainWindow::toggleMaximize); m_am->actionZoomIn,
connect(m_bottomButtonGroup, &BottomButtonGroup::zoomInBtnClicked, m_am->actionZoomOut,
this, &MainWindow::on_actionZoomIn_triggered); m_am->actionToggleCheckerboard,
connect(m_bottomButtonGroup, &BottomButtonGroup::zoomOutBtnClicked, m_am->actionRotateClockwise
this, &MainWindow::on_actionZoomOut_triggered); }, this);
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->setOpacity(0, false); m_bottomButtonGroup->setOpacity(0, false);
m_gv->setOpacity(0, false); m_gv->setOpacity(0, false);
@ -145,8 +137,6 @@ MainWindow::MainWindow(QWidget *parent)
connect(fullscreenShorucut, &QShortcut::activated, connect(fullscreenShorucut, &QShortcut::activated,
this, &MainWindow::toggleFullscreen); this, &MainWindow::toggleFullscreen);
m_am->setupAction(this);
centerWindow(); centerWindow();
QTimer::singleShot(0, this, [this](){ QTimer::singleShot(0, this, [this](){
@ -474,11 +464,6 @@ void MainWindow::updateWidgetsPosition()
m_gv->move(width() - m_gv->width(), height() - m_gv->height()); 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() void MainWindow::toggleProtectedMode()
{ {
m_protectedMode = !m_protectedMode; m_protectedMode = !m_protectedMode;
@ -546,6 +531,16 @@ QSize MainWindow::sizeHint() const
return QSize(710, 530); return QSize(710, 530);
} }
void MainWindow::on_actionActualSize_triggered()
{
m_graphicsView->resetScale();
}
void MainWindow::on_actionToggleMaximize_triggered()
{
toggleMaximize();
}
void MainWindow::on_actionZoomIn_triggered() void MainWindow::on_actionZoomIn_triggered()
{ {
if (m_graphicsView->scaleFactor() < 1000) { if (m_graphicsView->scaleFactor() < 1000) {
@ -622,7 +617,15 @@ void MainWindow::on_actionPaste_triggered()
void MainWindow::on_actionToggleCheckerboard_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() void MainWindow::on_actionToggleStayOnTop_triggered()

View File

@ -50,7 +50,6 @@ protected slots:
void centerWindow(); void centerWindow();
void closeWindow(); void closeWindow();
void updateWidgetsPosition(); void updateWidgetsPosition();
void toggleCheckerboard();
void toggleProtectedMode(); void toggleProtectedMode();
void toggleStayOnTop(); void toggleStayOnTop();
bool stayOnTop() const; bool stayOnTop() const;
@ -63,15 +62,19 @@ protected:
QSize sizeHint() const override; QSize sizeHint() const override;
private slots: private slots:
void on_actionActualSize_triggered();
void on_actionToggleMaximize_triggered();
void on_actionZoomIn_triggered(); void on_actionZoomIn_triggered();
void on_actionZoomOut_triggered(); void on_actionZoomOut_triggered();
void on_actionToggleCheckerboard_triggered();
void on_actionRotateClockwise_triggered();
void on_actionHorizontalFlip_triggered(); void on_actionHorizontalFlip_triggered();
void on_actionFitInView_triggered(); void on_actionFitInView_triggered();
void on_actionFitByWidth_triggered(); void on_actionFitByWidth_triggered();
void on_actionCopyPixmap_triggered(); void on_actionCopyPixmap_triggered();
void on_actionCopyFilePath_triggered(); void on_actionCopyFilePath_triggered();
void on_actionPaste_triggered(); void on_actionPaste_triggered();
void on_actionToggleCheckerboard_triggered();
void on_actionToggleStayOnTop_triggered(); void on_actionToggleStayOnTop_triggered();
void on_actionToggleProtectMode_triggered(); void on_actionToggleProtectMode_triggered();
void on_actionSettings_triggered(); void on_actionSettings_triggered();