From 6709c21d702f909b162a10f1e468d1c4b23dfa79 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Tue, 28 Jul 2020 21:14:38 +0800 Subject: [PATCH] config file support --- CMakeLists.txt | 2 + PineapplePictures.pro | 6 ++- languages/PineapplePictures.ts | 34 ++++++------- languages/PineapplePictures_zh_CN.ts | 34 ++++++------- mainwindow.cpp | 44 ++++++++++++----- mainwindow.h | 1 + settings.cpp | 72 ++++++++++++++++++++++++++++ settings.h | 35 ++++++++++++++ 8 files changed, 181 insertions(+), 47 deletions(-) create mode 100644 settings.cpp create mode 100644 settings.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c4c847..0504c85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set (PPIC_CPP_FILES navigatorview.cpp opacityhelper.cpp toolbutton.cpp + settings.cpp ) set (PPIC_HEADER_FILES @@ -29,6 +30,7 @@ set (PPIC_HEADER_FILES navigatorview.h opacityhelper.h toolbutton.h + settings.h ) set (PPIC_QRC_FILES diff --git a/PineapplePictures.pro b/PineapplePictures.pro index f7b92e3..bfb93b8 100644 --- a/PineapplePictures.pro +++ b/PineapplePictures.pro @@ -32,7 +32,8 @@ SOURCES += \ graphicsscene.cpp \ navigatorview.cpp \ opacityhelper.cpp \ - toolbutton.cpp + toolbutton.cpp \ + settings.cpp HEADERS += \ mainwindow.h \ @@ -41,7 +42,8 @@ HEADERS += \ graphicsscene.h \ navigatorview.h \ opacityhelper.h \ - toolbutton.h + toolbutton.h \ + settings.h TRANSLATIONS = \ languages/PineapplePictures.ts \ diff --git a/languages/PineapplePictures.ts b/languages/PineapplePictures.ts index bb76b77..6e7114d 100644 --- a/languages/PineapplePictures.ts +++ b/languages/PineapplePictures.ts @@ -12,7 +12,7 @@ GraphicsView - + File url list is empty @@ -22,12 +22,12 @@ - + Image data is invalid - + Not supported mimedata: %1 @@ -40,69 +40,69 @@ - + &Copy - + Copy P&ixmap - + Copy &File Path - + &Paste Image - + &Paste Image File - - + + Stay on top - + Protected mode - + Help - + Launch application with image file path as argument to load the file. - + Drag and drop image file onto the window is also supported. - + Context menu option explanation: - + Make window stay on top of all other windows. - + Avoid close window accidentally. (eg. by double clicking the window) diff --git a/languages/PineapplePictures_zh_CN.ts b/languages/PineapplePictures_zh_CN.ts index d37f278..a0e1998 100644 --- a/languages/PineapplePictures_zh_CN.ts +++ b/languages/PineapplePictures_zh_CN.ts @@ -12,7 +12,7 @@ GraphicsView - + File url list is empty 文件 URL 列表为空 @@ -22,12 +22,12 @@ 文件不是有效的图片文件 - + Image data is invalid 图片数据无效 - + Not supported mimedata: %1 不受支持的 MimeData 格式:%1 @@ -40,7 +40,7 @@ 文件 URL 列表为空 - + &Copy 复制(&C) @@ -49,64 +49,64 @@ 复制位图(&P) - + Copy P&ixmap 复制位图(&I) - + Copy &File Path 复制文件路径(&F) - + &Paste Image 粘贴图像(&P) - + &Paste Image File 粘贴图像文件(&P) - - + + Stay on top 总在最前 - + Protected mode 保护模式 - + Help 帮助 - + Launch application with image file path as argument to load the file. 以图片文件的路径作为参数运行程序即可直接打开图片文件。 - + Drag and drop image file onto the window is also supported. 也支持拖放图片文件到窗口内来加载图片。 - + Context menu option explanation: 菜单项说明: - + Make window stay on top of all other windows. 使窗口始终至于其它非置顶窗口上方。 - + Avoid close window accidentally. (eg. by double clicking the window) 避免窗口意外关闭。(如:不小心双击了窗口触发了关闭窗口行为) diff --git a/mainwindow.cpp b/mainwindow.cpp index d883f02..0bc37f5 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,5 +1,6 @@ #include "mainwindow.h" +#include "settings.h" #include "toolbutton.h" #include "bottombuttongroup.h" #include "graphicsview.h" @@ -27,7 +28,12 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { - this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); + if (Settings::instance()->alwaysOnTop()) { + this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); + } else { + this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint); + } + this->setAttribute(Qt::WA_TranslucentBackground, true); this->setMinimumSize(350, 350); this->setWindowIcon(QIcon(":/icons/app-icon.svg")); @@ -101,13 +107,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(m_bottomButtonGroup, &BottomButtonGroup::resetToOriginalBtnClicked, this, [ = ](){ m_graphicsView->resetScale(); }); connect(m_bottomButtonGroup, &BottomButtonGroup::toggleWindowMaximum, - this, [ = ](){ - if (isMaximized()) { - showNormal(); - } else { - showMaximized(); - } - }); + this, &MainWindow::toggleMaximize); connect(m_bottomButtonGroup, &BottomButtonGroup::zoomInBtnClicked, this, [ = ](){ m_graphicsView->zoomView(1.25); }); connect(m_bottomButtonGroup, &BottomButtonGroup::zoomOutBtnClicked, @@ -325,7 +325,7 @@ void MainWindow::mousePressEvent(QMouseEvent *event) void MainWindow::mouseMoveEvent(QMouseEvent *event) { - if (event->buttons() & Qt::LeftButton && m_clickedOnWindow) { + if (event->buttons() & Qt::LeftButton && m_clickedOnWindow && !isMaximized()) { move(event->globalPos() - m_oldMousePos); event->accept(); } @@ -342,9 +342,22 @@ void MainWindow::mouseReleaseEvent(QMouseEvent *event) void MainWindow::mouseDoubleClickEvent(QMouseEvent *event) { - quitAppAction(); + switch (Settings::instance()->doubleClickBehavior()) { + case ActionCloseWindow: + quitAppAction(); + event->accept(); + break; + case ActionMaximizeWindow: + toggleMaximize(); + event->accept(); + break; + case ActionDoNothing: + break; + } - return QMainWindow::mouseDoubleClickEvent(event); + // blumia: don't call parent constructor here, seems it will cause mouse move + // event get called even if we set event->accept(); + // return QMainWindow::mouseDoubleClickEvent(event); } void MainWindow::wheelEvent(QWheelEvent *event) @@ -631,3 +644,12 @@ void MainWindow::toggleFullscreen() showFullScreen(); } } + +void MainWindow::toggleMaximize() +{ + if (isMaximized()) { + showNormal(); + } else { + showMaximized(); + } +} diff --git a/mainwindow.h b/mainwindow.h index 0d3c8de..24d73c7 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -60,6 +60,7 @@ protected slots: bool stayOnTop(); void quitAppAction(bool force = false); void toggleFullscreen(); + void toggleMaximize(); private: QPoint m_oldMousePos; diff --git a/settings.cpp b/settings.cpp new file mode 100644 index 0000000..1d589fb --- /dev/null +++ b/settings.cpp @@ -0,0 +1,72 @@ +#include "settings.h" + +#include +#include +#include +#include + +Settings *Settings::m_settings_instance = nullptr; + +Settings *Settings::instance() +{ + if (!m_settings_instance) { + m_settings_instance = new Settings; + } + + return m_settings_instance; +} + +bool Settings::alwaysOnTop() +{ + return m_qsettings->value("always_on_top", true).toBool(); +} + +DoubleClickBehavior Settings::doubleClickBehavior() +{ + QString result = m_qsettings->value("double_click_behavior", "close").toString().toLower(); + + return stringToDoubleClickBehavior(result); +} + +QString Settings::doubleClickBehaviorToString(DoubleClickBehavior dcb) +{ + static QMap _map { + {ActionCloseWindow, "close"}, + {ActionMaximizeWindow, "maximize"}, + {ActionDoNothing, "ignore"} + }; + + return _map.value(dcb, "close"); +} + +DoubleClickBehavior Settings::stringToDoubleClickBehavior(QString str) +{ + static QMap _map { + {"close", ActionCloseWindow}, + {"maximize", ActionMaximizeWindow}, + {"ignore", ActionDoNothing} + }; + + return _map.value(str, ActionCloseWindow); +} + +Settings::Settings() + : QObject(qApp) +{ + QString configPath; + + QString portableConfigDirPath = QDir(QCoreApplication::applicationDirPath()).absoluteFilePath("data"); + QFileInfo portableConfigDirInfo(portableConfigDirPath); + if (portableConfigDirInfo.exists() && portableConfigDirInfo.isDir() && portableConfigDirInfo.isWritable()) { + // we can use it. + configPath = portableConfigDirPath; + } + + // %LOCALAPPDATA% under Windows. + if (configPath.isEmpty()) { + configPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation); + } + + m_qsettings = new QSettings(QDir(configPath).absoluteFilePath("config.ini"), QSettings::IniFormat, this); +} + diff --git a/settings.h b/settings.h new file mode 100644 index 0000000..0f729e4 --- /dev/null +++ b/settings.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include + +enum DoubleClickBehavior { + ActionDoNothing, + ActionCloseWindow, + ActionMaximizeWindow +}; + +class Settings : public QObject +{ + Q_OBJECT +public: + static Settings *instance(); + + bool alwaysOnTop(); + DoubleClickBehavior doubleClickBehavior(); + + static QString doubleClickBehaviorToString(DoubleClickBehavior dcb); + static DoubleClickBehavior stringToDoubleClickBehavior(QString str); + +private: + Settings(); + + static Settings *m_settings_instance; + + QSettings *m_qsettings; + +signals: + +public slots: +}; +