config file support

This commit is contained in:
Gary Wang 2020-07-28 21:14:38 +08:00
parent 31fae2cc8c
commit 6709c21d70
8 changed files with 181 additions and 47 deletions

View File

@ -19,6 +19,7 @@ set (PPIC_CPP_FILES
navigatorview.cpp navigatorview.cpp
opacityhelper.cpp opacityhelper.cpp
toolbutton.cpp toolbutton.cpp
settings.cpp
) )
set (PPIC_HEADER_FILES set (PPIC_HEADER_FILES
@ -29,6 +30,7 @@ set (PPIC_HEADER_FILES
navigatorview.h navigatorview.h
opacityhelper.h opacityhelper.h
toolbutton.h toolbutton.h
settings.h
) )
set (PPIC_QRC_FILES set (PPIC_QRC_FILES

View File

@ -32,7 +32,8 @@ SOURCES += \
graphicsscene.cpp \ graphicsscene.cpp \
navigatorview.cpp \ navigatorview.cpp \
opacityhelper.cpp \ opacityhelper.cpp \
toolbutton.cpp toolbutton.cpp \
settings.cpp
HEADERS += \ HEADERS += \
mainwindow.h \ mainwindow.h \
@ -41,7 +42,8 @@ HEADERS += \
graphicsscene.h \ graphicsscene.h \
navigatorview.h \ navigatorview.h \
opacityhelper.h \ opacityhelper.h \
toolbutton.h toolbutton.h \
settings.h
TRANSLATIONS = \ TRANSLATIONS = \
languages/PineapplePictures.ts \ languages/PineapplePictures.ts \

View File

@ -12,7 +12,7 @@
<context> <context>
<name>GraphicsView</name> <name>GraphicsView</name>
<message> <message>
<location filename="../graphicsview.cpp" line="243"/> <location filename="../graphicsview.cpp" line="247"/>
<source>File url list is empty</source> <source>File url list is empty</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
@ -22,12 +22,12 @@
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../graphicsview.cpp" line="251"/> <location filename="../graphicsview.cpp" line="255"/>
<source>Image data is invalid</source> <source>Image data is invalid</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../graphicsview.cpp" line="258"/> <location filename="../graphicsview.cpp" line="262"/>
<source>Not supported mimedata: %1</source> <source>Not supported mimedata: %1</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
@ -40,69 +40,69 @@
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="382"/> <location filename="../mainwindow.cpp" line="396"/>
<source>&amp;Copy</source> <source>&amp;Copy</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="403"/> <location filename="../mainwindow.cpp" line="417"/>
<source>Copy P&amp;ixmap</source> <source>Copy P&amp;ixmap</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="408"/> <location filename="../mainwindow.cpp" line="422"/>
<source>Copy &amp;File Path</source> <source>Copy &amp;File Path</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="418"/> <location filename="../mainwindow.cpp" line="432"/>
<source>&amp;Paste Image</source> <source>&amp;Paste Image</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="424"/> <location filename="../mainwindow.cpp" line="438"/>
<source>&amp;Paste Image File</source> <source>&amp;Paste Image File</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="429"/> <location filename="../mainwindow.cpp" line="443"/>
<location filename="../mainwindow.cpp" line="448"/> <location filename="../mainwindow.cpp" line="462"/>
<source>Stay on top</source> <source>Stay on top</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="435"/>
<location filename="../mainwindow.cpp" line="449"/> <location filename="../mainwindow.cpp" line="449"/>
<location filename="../mainwindow.cpp" line="463"/>
<source>Protected mode</source> <source>Protected mode</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="441"/> <location filename="../mainwindow.cpp" line="455"/>
<source>Help</source> <source>Help</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="444"/> <location filename="../mainwindow.cpp" line="458"/>
<source>Launch application with image file path as argument to load the file.</source> <source>Launch application with image file path as argument to load the file.</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="445"/> <location filename="../mainwindow.cpp" line="459"/>
<source>Drag and drop image file onto the window is also supported.</source> <source>Drag and drop image file onto the window is also supported.</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="447"/> <location filename="../mainwindow.cpp" line="461"/>
<source>Context menu option explanation:</source> <source>Context menu option explanation:</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="448"/> <location filename="../mainwindow.cpp" line="462"/>
<source>Make window stay on top of all other windows.</source> <source>Make window stay on top of all other windows.</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="449"/> <location filename="../mainwindow.cpp" line="463"/>
<source>Avoid close window accidentally. (eg. by double clicking the window)</source> <source>Avoid close window accidentally. (eg. by double clicking the window)</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>

View File

@ -12,7 +12,7 @@
<context> <context>
<name>GraphicsView</name> <name>GraphicsView</name>
<message> <message>
<location filename="../graphicsview.cpp" line="243"/> <location filename="../graphicsview.cpp" line="247"/>
<source>File url list is empty</source> <source>File url list is empty</source>
<translation> URL </translation> <translation> URL </translation>
</message> </message>
@ -22,12 +22,12 @@
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../graphicsview.cpp" line="251"/> <location filename="../graphicsview.cpp" line="255"/>
<source>Image data is invalid</source> <source>Image data is invalid</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../graphicsview.cpp" line="258"/> <location filename="../graphicsview.cpp" line="262"/>
<source>Not supported mimedata: %1</source> <source>Not supported mimedata: %1</source>
<translation> MimeData %1</translation> <translation> MimeData %1</translation>
</message> </message>
@ -40,7 +40,7 @@
<translation> URL </translation> <translation> URL </translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="382"/> <location filename="../mainwindow.cpp" line="396"/>
<source>&amp;Copy</source> <source>&amp;Copy</source>
<translation>(&amp;C)</translation> <translation>(&amp;C)</translation>
</message> </message>
@ -49,64 +49,64 @@
<translation type="vanished">(&amp;P)</translation> <translation type="vanished">(&amp;P)</translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="403"/> <location filename="../mainwindow.cpp" line="417"/>
<source>Copy P&amp;ixmap</source> <source>Copy P&amp;ixmap</source>
<translation>(&amp;I)</translation> <translation>(&amp;I)</translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="408"/> <location filename="../mainwindow.cpp" line="422"/>
<source>Copy &amp;File Path</source> <source>Copy &amp;File Path</source>
<translation>(&amp;F)</translation> <translation>(&amp;F)</translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="418"/> <location filename="../mainwindow.cpp" line="432"/>
<source>&amp;Paste Image</source> <source>&amp;Paste Image</source>
<translation>(&amp;P)</translation> <translation>(&amp;P)</translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="424"/> <location filename="../mainwindow.cpp" line="438"/>
<source>&amp;Paste Image File</source> <source>&amp;Paste Image File</source>
<translation>(&amp;P)</translation> <translation>(&amp;P)</translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="429"/> <location filename="../mainwindow.cpp" line="443"/>
<location filename="../mainwindow.cpp" line="448"/> <location filename="../mainwindow.cpp" line="462"/>
<source>Stay on top</source> <source>Stay on top</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="435"/>
<location filename="../mainwindow.cpp" line="449"/> <location filename="../mainwindow.cpp" line="449"/>
<location filename="../mainwindow.cpp" line="463"/>
<source>Protected mode</source> <source>Protected mode</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="441"/> <location filename="../mainwindow.cpp" line="455"/>
<source>Help</source> <source>Help</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="444"/> <location filename="../mainwindow.cpp" line="458"/>
<source>Launch application with image file path as argument to load the file.</source> <source>Launch application with image file path as argument to load the file.</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="445"/> <location filename="../mainwindow.cpp" line="459"/>
<source>Drag and drop image file onto the window is also supported.</source> <source>Drag and drop image file onto the window is also supported.</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="447"/> <location filename="../mainwindow.cpp" line="461"/>
<source>Context menu option explanation:</source> <source>Context menu option explanation:</source>
<translation></translation> <translation></translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="448"/> <location filename="../mainwindow.cpp" line="462"/>
<source>Make window stay on top of all other windows.</source> <source>Make window stay on top of all other windows.</source>
<translation>使</translation> <translation>使</translation>
</message> </message>
<message> <message>
<location filename="../mainwindow.cpp" line="449"/> <location filename="../mainwindow.cpp" line="463"/>
<source>Avoid close window accidentally. (eg. by double clicking the window)</source> <source>Avoid close window accidentally. (eg. by double clicking the window)</source>
<translation></translation> <translation></translation>
</message> </message>

View File

@ -1,5 +1,6 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "settings.h"
#include "toolbutton.h" #include "toolbutton.h"
#include "bottombuttongroup.h" #include "bottombuttongroup.h"
#include "graphicsview.h" #include "graphicsview.h"
@ -27,7 +28,12 @@
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) :
QMainWindow(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->setAttribute(Qt::WA_TranslucentBackground, true);
this->setMinimumSize(350, 350); this->setMinimumSize(350, 350);
this->setWindowIcon(QIcon(":/icons/app-icon.svg")); this->setWindowIcon(QIcon(":/icons/app-icon.svg"));
@ -101,13 +107,7 @@ MainWindow::MainWindow(QWidget *parent) :
connect(m_bottomButtonGroup, &BottomButtonGroup::resetToOriginalBtnClicked, connect(m_bottomButtonGroup, &BottomButtonGroup::resetToOriginalBtnClicked,
this, [ = ](){ m_graphicsView->resetScale(); }); this, [ = ](){ m_graphicsView->resetScale(); });
connect(m_bottomButtonGroup, &BottomButtonGroup::toggleWindowMaximum, connect(m_bottomButtonGroup, &BottomButtonGroup::toggleWindowMaximum,
this, [ = ](){ this, &MainWindow::toggleMaximize);
if (isMaximized()) {
showNormal();
} else {
showMaximized();
}
});
connect(m_bottomButtonGroup, &BottomButtonGroup::zoomInBtnClicked, connect(m_bottomButtonGroup, &BottomButtonGroup::zoomInBtnClicked,
this, [ = ](){ m_graphicsView->zoomView(1.25); }); this, [ = ](){ m_graphicsView->zoomView(1.25); });
connect(m_bottomButtonGroup, &BottomButtonGroup::zoomOutBtnClicked, connect(m_bottomButtonGroup, &BottomButtonGroup::zoomOutBtnClicked,
@ -325,7 +325,7 @@ void MainWindow::mousePressEvent(QMouseEvent *event)
void MainWindow::mouseMoveEvent(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); move(event->globalPos() - m_oldMousePos);
event->accept(); event->accept();
} }
@ -342,9 +342,22 @@ void MainWindow::mouseReleaseEvent(QMouseEvent *event)
void MainWindow::mouseDoubleClickEvent(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) void MainWindow::wheelEvent(QWheelEvent *event)
@ -631,3 +644,12 @@ void MainWindow::toggleFullscreen()
showFullScreen(); showFullScreen();
} }
} }
void MainWindow::toggleMaximize()
{
if (isMaximized()) {
showNormal();
} else {
showMaximized();
}
}

View File

@ -60,6 +60,7 @@ protected slots:
bool stayOnTop(); bool stayOnTop();
void quitAppAction(bool force = false); void quitAppAction(bool force = false);
void toggleFullscreen(); void toggleFullscreen();
void toggleMaximize();
private: private:
QPoint m_oldMousePos; QPoint m_oldMousePos;

72
settings.cpp Normal file
View File

@ -0,0 +1,72 @@
#include "settings.h"
#include <QApplication>
#include <QStandardPaths>
#include <QDebug>
#include <QDir>
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<DoubleClickBehavior, QString> _map {
{ActionCloseWindow, "close"},
{ActionMaximizeWindow, "maximize"},
{ActionDoNothing, "ignore"}
};
return _map.value(dcb, "close");
}
DoubleClickBehavior Settings::stringToDoubleClickBehavior(QString str)
{
static QMap<QString, DoubleClickBehavior> _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);
}

35
settings.h Normal file
View File

@ -0,0 +1,35 @@
#pragma once
#include <QObject>
#include <QSettings>
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:
};