From c9dadcbcad9f293f28b04e81c39a67bc5423550b Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sat, 12 Mar 2022 10:35:23 +0800 Subject: [PATCH] refactor: use QMetaEnum --- app/mainwindow.cpp | 15 +++--- app/settings.cpp | 108 +++++++++++++---------------------------- app/settings.h | 51 ++++++++----------- app/settingsdialog.cpp | 44 ++++++++--------- 4 files changed, 84 insertions(+), 134 deletions(-) diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index 624dad2..e9db8c9 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -167,12 +167,15 @@ void MainWindow::showUrls(const QList &urls) void MainWindow::initWindowSize() { switch (Settings::instance()->initWindowSizeBehavior()) { - case ActionAutoSize: + case Settings::WindowSizeBehavior::Auto: adjustWindowSizeBySceneRect(); break; - case ActionMaximize: + case Settings::WindowSizeBehavior::Maximized: showMaximized(); break; + default: + adjustWindowSizeBySceneRect(); + break; } } @@ -334,15 +337,15 @@ void MainWindow::mouseDoubleClickEvent(QMouseEvent *event) } switch (Settings::instance()->doubleClickBehavior()) { - case ActionCloseWindow: + case Settings::DoubleClickBehavior::Close: quitAppAction(); event->accept(); break; - case ActionMaximizeWindow: + case Settings::DoubleClickBehavior::Maximize: toggleMaximize(); event->accept(); break; - case ActionDoNothing: + case Settings::DoubleClickBehavior::Ignore: break; } @@ -356,7 +359,7 @@ void MainWindow::wheelEvent(QWheelEvent *event) QPoint numDegrees = event->angleDelta() / 8; bool needWeelEvent = false, wheelUp = false; bool actionIsZoom = event->modifiers().testFlag(Qt::ControlModifier) - || Settings::instance()->mouseWheelBehavior() == ActionZoomImage; + || Settings::instance()->mouseWheelBehavior() == Settings::MouseWheelBehavior::Zoom; // NOTE: Only checking angleDelta since the QWheelEvent::pixelDelta() doc says // pixelDelta() value is driver specific and unreliable on X11... diff --git a/app/settings.cpp b/app/settings.cpp index 7fa7cba..0f07f19 100644 --- a/app/settings.cpp +++ b/app/settings.cpp @@ -4,6 +4,28 @@ #include #include #include +#include + +namespace QEnumHelper +{ + template + E fromString(const QString &text, const E defaultValue) + { + bool ok; + E result = static_cast(QMetaEnum::fromType().keyToValue(text.toUtf8(), &ok)); + if (!ok) { + return defaultValue; + } + return result; + } + + template + QString toString(E value) + { + const int intValue = static_cast(value); + return QString::fromUtf8(QMetaEnum::fromType().valueToKey(intValue)); + } +} Settings *Settings::m_settings_instance = nullptr; @@ -21,25 +43,25 @@ bool Settings::stayOnTop() return m_qsettings->value("stay_on_top", true).toBool(); } -DoubleClickBehavior Settings::doubleClickBehavior() const +Settings::DoubleClickBehavior Settings::doubleClickBehavior() const { - QString result = m_qsettings->value("double_click_behavior", "close").toString().toLower(); + QString result = m_qsettings->value("double_click_behavior", "Close").toString(); - return stringToDoubleClickBehavior(result); + return QEnumHelper::fromString(result, DoubleClickBehavior::Close); } -MouseWheelBehavior Settings::mouseWheelBehavior() const +Settings::MouseWheelBehavior Settings::mouseWheelBehavior() const { - QString result = m_qsettings->value("mouse_wheel_behavior", "close").toString().toLower(); + QString result = m_qsettings->value("mouse_wheel_behavior", "Zoom").toString(); - return stringToMouseWheelBehavior(result); + return QEnumHelper::fromString(result, MouseWheelBehavior::Zoom); } -WindowSizeBehavior Settings::initWindowSizeBehavior() const +Settings::WindowSizeBehavior Settings::initWindowSizeBehavior() const { - QString result = m_qsettings->value("init_window_size_behavior", "auto").toString().toLower(); + QString result = m_qsettings->value("init_window_size_behavior", "Auto").toString(); - return stringToWindowSizeBehavior(result); + return QEnumHelper::fromString(result, WindowSizeBehavior::Auto); } void Settings::setStayOnTop(bool on) @@ -50,84 +72,22 @@ void Settings::setStayOnTop(bool on) void Settings::setDoubleClickBehavior(DoubleClickBehavior dcb) { - m_qsettings->setValue("double_click_behavior", doubleClickBehaviorToString(dcb)); + m_qsettings->setValue("double_click_behavior", QEnumHelper::toString(dcb)); m_qsettings->sync(); } void Settings::setMouseWheelBehavior(MouseWheelBehavior mwb) { - m_qsettings->setValue("mouse_wheel_behavior", mouseWheelBehaviorToString(mwb)); + m_qsettings->setValue("mouse_wheel_behavior", QEnumHelper::toString(mwb)); m_qsettings->sync(); } void Settings::setInitWindowSizeBehavior(WindowSizeBehavior wsb) { - m_qsettings->setValue("init_window_size_behavior", windowSizeBehaviorToString(wsb)); + m_qsettings->setValue("init_window_size_behavior", QEnumHelper::toString(wsb)); m_qsettings->sync(); } -QString Settings::doubleClickBehaviorToString(DoubleClickBehavior dcb) -{ - static QMap _map { - {ActionCloseWindow, "close"}, - {ActionMaximizeWindow, "maximize"}, - {ActionDoNothing, "ignore"} - }; - - return _map.value(dcb, "close"); -} - -QString Settings::mouseWheelBehaviorToString(MouseWheelBehavior mwb) -{ - static QMap _map { - {ActionZoomImage, "zoom"}, - {ActionPrevNextImage, "switch"} - }; - - return _map.value(mwb, "zoom"); -} - -QString Settings::windowSizeBehaviorToString(WindowSizeBehavior wsb) -{ - static QMap _map { - {ActionAutoSize, "auto"}, - {ActionMaximize, "maximized"} - }; - - return _map.value(wsb, "zoom"); -} - -DoubleClickBehavior Settings::stringToDoubleClickBehavior(QString str) -{ - static QMap _map { - {"close", ActionCloseWindow}, - {"maximize", ActionMaximizeWindow}, - {"ignore", ActionDoNothing} - }; - - return _map.value(str, ActionCloseWindow); -} - -MouseWheelBehavior Settings::stringToMouseWheelBehavior(QString str) -{ - static QMap _map { - {"zoom", ActionZoomImage}, - {"switch", ActionPrevNextImage} - }; - - return _map.value(str, ActionZoomImage); -} - -WindowSizeBehavior Settings::stringToWindowSizeBehavior(QString str) -{ - static QMap _map { - {"auto", ActionAutoSize}, - {"maximized", ActionMaximize} - }; - - return _map.value(str, ActionAutoSize); -} - Settings::Settings() : QObject(qApp) { diff --git a/app/settings.h b/app/settings.h index 5fe9b3e..791967c 100644 --- a/app/settings.h +++ b/app/settings.h @@ -3,35 +3,29 @@ #include #include -enum DoubleClickBehavior { - ActionDoNothing, - ActionCloseWindow, - ActionMaximizeWindow, - - DCActionStart = ActionDoNothing, - DCActionEnd = ActionMaximizeWindow -}; - -enum MouseWheelBehavior { - ActionZoomImage, - ActionPrevNextImage, - - MWActionStart = ActionZoomImage, - MWActionEnd = ActionPrevNextImage -}; - -enum WindowSizeBehavior { - ActionAutoSize, - ActionMaximize, - - IWSActionStart = ActionAutoSize, - IWSActionEnd = ActionMaximize -}; - class Settings : public QObject { Q_OBJECT public: + enum DoubleClickBehavior { + Ignore, + Close, + Maximize, + }; + Q_ENUM(DoubleClickBehavior); + + enum MouseWheelBehavior { + Zoom, + Switch, + }; + Q_ENUM(MouseWheelBehavior); + + enum WindowSizeBehavior { + Auto, + Maximized, + }; + Q_ENUM(WindowSizeBehavior); + static Settings *instance(); bool stayOnTop(); @@ -44,13 +38,6 @@ public: void setMouseWheelBehavior(MouseWheelBehavior mwb); void setInitWindowSizeBehavior(WindowSizeBehavior wsb); - static QString doubleClickBehaviorToString(DoubleClickBehavior dcb); - static QString mouseWheelBehaviorToString(MouseWheelBehavior mwb); - static QString windowSizeBehaviorToString(WindowSizeBehavior wsb); - static DoubleClickBehavior stringToDoubleClickBehavior(QString str); - static MouseWheelBehavior stringToMouseWheelBehavior(QString str); - static WindowSizeBehavior stringToWindowSizeBehavior(QString str); - private: Settings(); diff --git a/app/settingsdialog.cpp b/app/settingsdialog.cpp index 1415e9b..8357602 100644 --- a/app/settingsdialog.cpp +++ b/app/settingsdialog.cpp @@ -18,35 +18,35 @@ SettingsDialog::SettingsDialog(QWidget *parent) QFormLayout * settingsForm = new QFormLayout(this); - static QMap _dc_map { - { ActionDoNothing, tr("Do nothing") }, - { ActionCloseWindow, tr("Close the window") }, - { ActionMaximizeWindow, tr("Toggle maximize") } + static QList< QPair > _dc_options { + { Settings::DoubleClickBehavior::Ignore, tr("Do nothing") }, + { Settings::DoubleClickBehavior::Close, tr("Close the window") }, + { Settings::DoubleClickBehavior::Maximize, tr("Toggle maximize") } }; - static QMap _mw_map { - { ActionZoomImage, tr("Zoom in and out") }, - { ActionPrevNextImage, tr("View next or previous item") } + static QList< QPair > _mw_options { + { Settings::MouseWheelBehavior::Zoom, tr("Zoom in and out") }, + { Settings::MouseWheelBehavior::Switch, tr("View next or previous item") } }; - static QMap _iws_map { - { ActionAutoSize, "Auto size" }, - { ActionMaximize, "Maximize" } + static QList< QPair > _iws_options { + { Settings::WindowSizeBehavior::Auto, "Auto size" }, + { Settings::WindowSizeBehavior::Maximized, "Maximize" } }; QStringList dcbDropDown; - for (int dcb = DCActionStart; dcb <= DCActionEnd; dcb++) { - dcbDropDown.append(_dc_map.value(static_cast(dcb))); + for (const QPair & dcOption : _dc_options) { + dcbDropDown.append(dcOption.second); } QStringList mwbDropDown; - for (int mwb = MWActionStart; mwb <= MWActionEnd; mwb++) { - mwbDropDown.append(_mw_map.value(static_cast(mwb))); + for (const QPair & mwOption : _mw_options) { + mwbDropDown.append(mwOption.second); } QStringList iwsbDropDown; - for (int iwsb = IWSActionStart; iwsb <= IWSActionEnd; iwsb++) { - iwsbDropDown.append(_iws_map.value(static_cast(iwsb))); + for (const QPair & iwsOption : _iws_options) { + iwsbDropDown.append(iwsOption.second); } settingsForm->addRow(tr("Stay on top when start-up"), m_stayOnTop); @@ -56,13 +56,13 @@ SettingsDialog::SettingsDialog(QWidget *parent) m_stayOnTop->setChecked(Settings::instance()->stayOnTop()); m_doubleClickBehavior->setModel(new QStringListModel(dcbDropDown)); - DoubleClickBehavior dcb = Settings::instance()->doubleClickBehavior(); + Settings::DoubleClickBehavior dcb = Settings::instance()->doubleClickBehavior(); m_doubleClickBehavior->setCurrentIndex(static_cast(dcb)); m_mouseWheelBehavior->setModel(new QStringListModel(mwbDropDown)); - MouseWheelBehavior mwb = Settings::instance()->mouseWheelBehavior(); + Settings::MouseWheelBehavior mwb = Settings::instance()->mouseWheelBehavior(); m_mouseWheelBehavior->setCurrentIndex(static_cast(mwb)); m_initWindowSizeBehavior->setModel(new QStringListModel(iwsbDropDown)); - WindowSizeBehavior iwsb = Settings::instance()->initWindowSizeBehavior(); + Settings::WindowSizeBehavior iwsb = Settings::instance()->initWindowSizeBehavior(); m_initWindowSizeBehavior->setCurrentIndex(static_cast(iwsb)); connect(m_stayOnTop, &QCheckBox::stateChanged, this, [ = ](int state){ @@ -70,15 +70,15 @@ SettingsDialog::SettingsDialog(QWidget *parent) }); connect(m_doubleClickBehavior, QOverload::of(&QComboBox::currentIndexChanged), this, [ = ](int index){ - Settings::instance()->setDoubleClickBehavior(static_cast(index)); + Settings::instance()->setDoubleClickBehavior(_dc_options.at(index).first); }); connect(m_mouseWheelBehavior, QOverload::of(&QComboBox::currentIndexChanged), this, [ = ](int index){ - Settings::instance()->setMouseWheelBehavior(static_cast(index)); + Settings::instance()->setMouseWheelBehavior(_mw_options.at(index).first); }); connect(m_initWindowSizeBehavior, QOverload::of(&QComboBox::currentIndexChanged), this, [ = ](int index){ - Settings::instance()->setInitWindowSizeBehavior(static_cast(index)); + Settings::instance()->setInitWindowSizeBehavior(_iws_options.at(index).first); }); this->setMinimumSize(300, 90); // not sure why it complain "Unable to set geometry"