refactor: use QMetaEnum

This commit is contained in:
Gary Wang 2022-03-12 10:35:23 +08:00
parent 432266a6f0
commit c9dadcbcad
No known key found for this signature in database
GPG Key ID: 5D30A4F15EA78760
4 changed files with 84 additions and 134 deletions

View File

@ -167,12 +167,15 @@ void MainWindow::showUrls(const QList<QUrl> &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...

View File

@ -4,6 +4,28 @@
#include <QStandardPaths>
#include <QDebug>
#include <QDir>
#include <QMetaEnum>
namespace QEnumHelper
{
template <typename E>
E fromString(const QString &text, const E defaultValue)
{
bool ok;
E result = static_cast<E>(QMetaEnum::fromType<E>().keyToValue(text.toUtf8(), &ok));
if (!ok) {
return defaultValue;
}
return result;
}
template <typename E>
QString toString(E value)
{
const int intValue = static_cast<int>(value);
return QString::fromUtf8(QMetaEnum::fromType<E>().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<DoubleClickBehavior>(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<MouseWheelBehavior>(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<WindowSizeBehavior>(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<DoubleClickBehavior, QString> _map {
{ActionCloseWindow, "close"},
{ActionMaximizeWindow, "maximize"},
{ActionDoNothing, "ignore"}
};
return _map.value(dcb, "close");
}
QString Settings::mouseWheelBehaviorToString(MouseWheelBehavior mwb)
{
static QMap<MouseWheelBehavior, QString> _map {
{ActionZoomImage, "zoom"},
{ActionPrevNextImage, "switch"}
};
return _map.value(mwb, "zoom");
}
QString Settings::windowSizeBehaviorToString(WindowSizeBehavior wsb)
{
static QMap<WindowSizeBehavior, QString> _map {
{ActionAutoSize, "auto"},
{ActionMaximize, "maximized"}
};
return _map.value(wsb, "zoom");
}
DoubleClickBehavior Settings::stringToDoubleClickBehavior(QString str)
{
static QMap<QString, DoubleClickBehavior> _map {
{"close", ActionCloseWindow},
{"maximize", ActionMaximizeWindow},
{"ignore", ActionDoNothing}
};
return _map.value(str, ActionCloseWindow);
}
MouseWheelBehavior Settings::stringToMouseWheelBehavior(QString str)
{
static QMap<QString, MouseWheelBehavior> _map {
{"zoom", ActionZoomImage},
{"switch", ActionPrevNextImage}
};
return _map.value(str, ActionZoomImage);
}
WindowSizeBehavior Settings::stringToWindowSizeBehavior(QString str)
{
static QMap<QString, WindowSizeBehavior> _map {
{"auto", ActionAutoSize},
{"maximized", ActionMaximize}
};
return _map.value(str, ActionAutoSize);
}
Settings::Settings()
: QObject(qApp)
{

View File

@ -3,35 +3,29 @@
#include <QObject>
#include <QSettings>
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();

View File

@ -18,35 +18,35 @@ SettingsDialog::SettingsDialog(QWidget *parent)
QFormLayout * settingsForm = new QFormLayout(this);
static QMap<DoubleClickBehavior, QString> _dc_map {
{ ActionDoNothing, tr("Do nothing") },
{ ActionCloseWindow, tr("Close the window") },
{ ActionMaximizeWindow, tr("Toggle maximize") }
static QList< QPair<Settings::DoubleClickBehavior, QString> > _dc_options {
{ Settings::DoubleClickBehavior::Ignore, tr("Do nothing") },
{ Settings::DoubleClickBehavior::Close, tr("Close the window") },
{ Settings::DoubleClickBehavior::Maximize, tr("Toggle maximize") }
};
static QMap<MouseWheelBehavior, QString> _mw_map {
{ ActionZoomImage, tr("Zoom in and out") },
{ ActionPrevNextImage, tr("View next or previous item") }
static QList< QPair<Settings::MouseWheelBehavior, QString> > _mw_options {
{ Settings::MouseWheelBehavior::Zoom, tr("Zoom in and out") },
{ Settings::MouseWheelBehavior::Switch, tr("View next or previous item") }
};
static QMap<WindowSizeBehavior, QString> _iws_map {
{ ActionAutoSize, "Auto size" },
{ ActionMaximize, "Maximize" }
static QList< QPair<Settings::WindowSizeBehavior, QString> > _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<DoubleClickBehavior>(dcb)));
for (const QPair<Settings::DoubleClickBehavior, QString> & dcOption : _dc_options) {
dcbDropDown.append(dcOption.second);
}
QStringList mwbDropDown;
for (int mwb = MWActionStart; mwb <= MWActionEnd; mwb++) {
mwbDropDown.append(_mw_map.value(static_cast<MouseWheelBehavior>(mwb)));
for (const QPair<Settings::MouseWheelBehavior, QString> & mwOption : _mw_options) {
mwbDropDown.append(mwOption.second);
}
QStringList iwsbDropDown;
for (int iwsb = IWSActionStart; iwsb <= IWSActionEnd; iwsb++) {
iwsbDropDown.append(_iws_map.value(static_cast<WindowSizeBehavior>(iwsb)));
for (const QPair<Settings::WindowSizeBehavior, QString> & 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<int>(dcb));
m_mouseWheelBehavior->setModel(new QStringListModel(mwbDropDown));
MouseWheelBehavior mwb = Settings::instance()->mouseWheelBehavior();
Settings::MouseWheelBehavior mwb = Settings::instance()->mouseWheelBehavior();
m_mouseWheelBehavior->setCurrentIndex(static_cast<int>(mwb));
m_initWindowSizeBehavior->setModel(new QStringListModel(iwsbDropDown));
WindowSizeBehavior iwsb = Settings::instance()->initWindowSizeBehavior();
Settings::WindowSizeBehavior iwsb = Settings::instance()->initWindowSizeBehavior();
m_initWindowSizeBehavior->setCurrentIndex(static_cast<int>(iwsb));
connect(m_stayOnTop, &QCheckBox::stateChanged, this, [ = ](int state){
@ -70,15 +70,15 @@ SettingsDialog::SettingsDialog(QWidget *parent)
});
connect(m_doubleClickBehavior, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [ = ](int index){
Settings::instance()->setDoubleClickBehavior(static_cast<DoubleClickBehavior>(index));
Settings::instance()->setDoubleClickBehavior(_dc_options.at(index).first);
});
connect(m_mouseWheelBehavior, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [ = ](int index){
Settings::instance()->setMouseWheelBehavior(static_cast<MouseWheelBehavior>(index));
Settings::instance()->setMouseWheelBehavior(_mw_options.at(index).first);
});
connect(m_initWindowSizeBehavior, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [ = ](int index){
Settings::instance()->setInitWindowSizeBehavior(static_cast<WindowSizeBehavior>(index));
Settings::instance()->setInitWindowSizeBehavior(_iws_options.at(index).first);
});
this->setMinimumSize(300, 90); // not sure why it complain "Unable to set geometry"