feat: simple config dialog

This commit is contained in:
Gary Wang 2020-07-29 00:57:43 +08:00
parent 6709c21d70
commit a2adb0e1d4
7 changed files with 116 additions and 7 deletions

View File

@ -20,6 +20,7 @@ set (PPIC_CPP_FILES
opacityhelper.cpp opacityhelper.cpp
toolbutton.cpp toolbutton.cpp
settings.cpp settings.cpp
settingsdialog.cpp
) )
set (PPIC_HEADER_FILES set (PPIC_HEADER_FILES
@ -31,6 +32,7 @@ set (PPIC_HEADER_FILES
opacityhelper.h opacityhelper.h
toolbutton.h toolbutton.h
settings.h settings.h
settingsdialog.h
) )
set (PPIC_QRC_FILES set (PPIC_QRC_FILES

View File

@ -33,7 +33,8 @@ SOURCES += \
navigatorview.cpp \ navigatorview.cpp \
opacityhelper.cpp \ opacityhelper.cpp \
toolbutton.cpp \ toolbutton.cpp \
settings.cpp settings.cpp \
settingsdialog.cpp
HEADERS += \ HEADERS += \
mainwindow.h \ mainwindow.h \
@ -43,7 +44,8 @@ HEADERS += \
navigatorview.h \ navigatorview.h \
opacityhelper.h \ opacityhelper.h \
toolbutton.h \ toolbutton.h \
settings.h settings.h \
settingsdialog.h
TRANSLATIONS = \ TRANSLATIONS = \
languages/PineapplePictures.ts \ languages/PineapplePictures.ts \

View File

@ -6,6 +6,7 @@
#include "graphicsview.h" #include "graphicsview.h"
#include "navigatorview.h" #include "navigatorview.h"
#include "graphicsscene.h" #include "graphicsscene.h"
#include "settingsdialog.h"
#include <QMouseEvent> #include <QMouseEvent>
#include <QMovie> #include <QMovie>
@ -28,7 +29,7 @@
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent) QMainWindow(parent)
{ {
if (Settings::instance()->alwaysOnTop()) { if (Settings::instance()->stayOnTop()) {
this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
} else { } else {
this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint); this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
@ -445,12 +446,21 @@ void MainWindow::contextMenuEvent(QContextMenuEvent *event)
}); });
stayOnTopMode->setCheckable(true); stayOnTopMode->setCheckable(true);
stayOnTopMode->setChecked(stayOnTop()); stayOnTopMode->setChecked(stayOnTop());
QAction * protectedMode = new QAction(tr("Protected mode")); QAction * protectedMode = new QAction(tr("Protected mode"));
connect(protectedMode, &QAction::triggered, this, [ = ](){ connect(protectedMode, &QAction::triggered, this, [ = ](){
toggleProtectedMode(); toggleProtectedMode();
}); });
protectedMode->setCheckable(true); protectedMode->setCheckable(true);
protectedMode->setChecked(m_protectedMode); protectedMode->setChecked(m_protectedMode);
QAction * toggleSettings = new QAction(tr("Configure..."));
connect(toggleSettings, &QAction::triggered, this, [ = ](){
SettingsDialog * sd = new SettingsDialog(this);
sd->exec();
sd->deleteLater();
});
QAction * helpAction = new QAction(tr("Help")); QAction * helpAction = new QAction(tr("Help"));
connect(helpAction, &QAction::triggered, this, [ = ](){ connect(helpAction, &QAction::triggered, this, [ = ](){
QStringList sl { QStringList sl {
@ -478,6 +488,7 @@ void MainWindow::contextMenuEvent(QContextMenuEvent *event)
menu->addAction(stayOnTopMode); menu->addAction(stayOnTopMode);
menu->addAction(protectedMode); menu->addAction(protectedMode);
menu->addSeparator(); menu->addSeparator();
menu->addAction(toggleSettings);
menu->addAction(helpAction); menu->addAction(helpAction);
menu->exec(mapToGlobal(event->pos())); menu->exec(mapToGlobal(event->pos()));
menu->deleteLater(); menu->deleteLater();

View File

@ -16,9 +16,9 @@ Settings *Settings::instance()
return m_settings_instance; return m_settings_instance;
} }
bool Settings::alwaysOnTop() bool Settings::stayOnTop()
{ {
return m_qsettings->value("always_on_top", true).toBool(); return m_qsettings->value("stay_on_top", true).toBool();
} }
DoubleClickBehavior Settings::doubleClickBehavior() DoubleClickBehavior Settings::doubleClickBehavior()
@ -28,6 +28,18 @@ DoubleClickBehavior Settings::doubleClickBehavior()
return stringToDoubleClickBehavior(result); return stringToDoubleClickBehavior(result);
} }
void Settings::setStayOnTop(bool on)
{
m_qsettings->setValue("stay_on_top", on);
m_qsettings->sync();
}
void Settings::setDoubleClickBehavior(DoubleClickBehavior dcb)
{
m_qsettings->setValue("double_click_behavior", doubleClickBehaviorToString(dcb));
m_qsettings->sync();
}
QString Settings::doubleClickBehaviorToString(DoubleClickBehavior dcb) QString Settings::doubleClickBehaviorToString(DoubleClickBehavior dcb)
{ {
static QMap<DoubleClickBehavior, QString> _map { static QMap<DoubleClickBehavior, QString> _map {

View File

@ -6,7 +6,10 @@
enum DoubleClickBehavior { enum DoubleClickBehavior {
ActionDoNothing, ActionDoNothing,
ActionCloseWindow, ActionCloseWindow,
ActionMaximizeWindow ActionMaximizeWindow,
ActionStart = ActionDoNothing,
ActionEnd = ActionMaximizeWindow
}; };
class Settings : public QObject class Settings : public QObject
@ -15,9 +18,12 @@ class Settings : public QObject
public: public:
static Settings *instance(); static Settings *instance();
bool alwaysOnTop(); bool stayOnTop();
DoubleClickBehavior doubleClickBehavior(); DoubleClickBehavior doubleClickBehavior();
void setStayOnTop(bool on);
void setDoubleClickBehavior(DoubleClickBehavior dcb);
static QString doubleClickBehaviorToString(DoubleClickBehavior dcb); static QString doubleClickBehaviorToString(DoubleClickBehavior dcb);
static DoubleClickBehavior stringToDoubleClickBehavior(QString str); static DoubleClickBehavior stringToDoubleClickBehavior(QString str);

50
settingsdialog.cpp Normal file
View File

@ -0,0 +1,50 @@
#include "settingsdialog.h"
#include "settings.h"
#include <QCheckBox>
#include <QComboBox>
#include <QFormLayout>
#include <QStringListModel>
SettingsDialog::SettingsDialog(QWidget *parent)
: QDialog(parent)
, m_stayOnTop(new QCheckBox)
, m_doubleClickBehavior(new QComboBox)
{
QFormLayout * settingsForm = new QFormLayout(this);
static QMap<DoubleClickBehavior, QString> _map {
{ ActionDoNothing, tr("Do nothing") },
{ ActionCloseWindow, tr("Close the window") },
{ ActionMaximizeWindow, tr("Toggle maximize") }
};
QStringList dropDown;
for (int dcb = ActionStart; dcb <= ActionEnd; dcb++) {
dropDown.append(_map.value(static_cast<DoubleClickBehavior>(dcb)));
}
settingsForm->addRow(tr("Stay on top when start-up"), m_stayOnTop);
settingsForm->addRow(tr("Double-click behavior"), m_doubleClickBehavior);
m_stayOnTop->setChecked(Settings::instance()->stayOnTop());
m_doubleClickBehavior->setModel(new QStringListModel(dropDown));
DoubleClickBehavior dcb = Settings::instance()->doubleClickBehavior();
m_doubleClickBehavior->setCurrentIndex(static_cast<int>(dcb));
connect(m_stayOnTop, &QCheckBox::stateChanged, this, [ = ](int state){
Settings::instance()->setStayOnTop(state == Qt::Checked);
});
connect(m_doubleClickBehavior, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [ = ](int index){
Settings::instance()->setDoubleClickBehavior(static_cast<DoubleClickBehavior>(index));
});
setMinimumSize(200,50); // FIXME: use minimumSizeHint() instead?
}
SettingsDialog::~SettingsDialog()
{
}

26
settingsdialog.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef SETTINGSDIALOG_H
#define SETTINGSDIALOG_H
#include <QObject>
#include <QWidget>
#include <QDialog>
class QCheckBox;
class QComboBox;
class SettingsDialog : public QDialog
{
Q_OBJECT
public:
explicit SettingsDialog(QWidget *parent = nullptr);
~SettingsDialog();
signals:
public slots:
private:
QCheckBox * m_stayOnTop = nullptr;
QComboBox * m_doubleClickBehavior = nullptr;
};
#endif // SETTINGSDIALOG_H