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
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

View File

@ -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 \

View File

@ -12,7 +12,7 @@
<context>
<name>GraphicsView</name>
<message>
<location filename="../graphicsview.cpp" line="243"/>
<location filename="../graphicsview.cpp" line="247"/>
<source>File url list is empty</source>
<translation type="unfinished"></translation>
</message>
@ -22,12 +22,12 @@
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../graphicsview.cpp" line="251"/>
<location filename="../graphicsview.cpp" line="255"/>
<source>Image data is invalid</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../graphicsview.cpp" line="258"/>
<location filename="../graphicsview.cpp" line="262"/>
<source>Not supported mimedata: %1</source>
<translation type="unfinished"></translation>
</message>
@ -40,69 +40,69 @@
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="382"/>
<location filename="../mainwindow.cpp" line="396"/>
<source>&amp;Copy</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="403"/>
<location filename="../mainwindow.cpp" line="417"/>
<source>Copy P&amp;ixmap</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="408"/>
<location filename="../mainwindow.cpp" line="422"/>
<source>Copy &amp;File Path</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="418"/>
<location filename="../mainwindow.cpp" line="432"/>
<source>&amp;Paste Image</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="424"/>
<location filename="../mainwindow.cpp" line="438"/>
<source>&amp;Paste Image File</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="429"/>
<location filename="../mainwindow.cpp" line="448"/>
<location filename="../mainwindow.cpp" line="443"/>
<location filename="../mainwindow.cpp" line="462"/>
<source>Stay on top</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="435"/>
<location filename="../mainwindow.cpp" line="449"/>
<location filename="../mainwindow.cpp" line="463"/>
<source>Protected mode</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="441"/>
<location filename="../mainwindow.cpp" line="455"/>
<source>Help</source>
<translation type="unfinished"></translation>
</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>
<translation type="unfinished"></translation>
</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>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="447"/>
<location filename="../mainwindow.cpp" line="461"/>
<source>Context menu option explanation:</source>
<translation type="unfinished"></translation>
</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>
<translation type="unfinished"></translation>
</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>
<translation type="unfinished"></translation>
</message>

View File

@ -12,7 +12,7 @@
<context>
<name>GraphicsView</name>
<message>
<location filename="../graphicsview.cpp" line="243"/>
<location filename="../graphicsview.cpp" line="247"/>
<source>File url list is empty</source>
<translation> URL </translation>
</message>
@ -22,12 +22,12 @@
<translation></translation>
</message>
<message>
<location filename="../graphicsview.cpp" line="251"/>
<location filename="../graphicsview.cpp" line="255"/>
<source>Image data is invalid</source>
<translation></translation>
</message>
<message>
<location filename="../graphicsview.cpp" line="258"/>
<location filename="../graphicsview.cpp" line="262"/>
<source>Not supported mimedata: %1</source>
<translation> MimeData %1</translation>
</message>
@ -40,7 +40,7 @@
<translation> URL </translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="382"/>
<location filename="../mainwindow.cpp" line="396"/>
<source>&amp;Copy</source>
<translation>(&amp;C)</translation>
</message>
@ -49,64 +49,64 @@
<translation type="vanished">(&amp;P)</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="403"/>
<location filename="../mainwindow.cpp" line="417"/>
<source>Copy P&amp;ixmap</source>
<translation>(&amp;I)</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="408"/>
<location filename="../mainwindow.cpp" line="422"/>
<source>Copy &amp;File Path</source>
<translation>(&amp;F)</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="418"/>
<location filename="../mainwindow.cpp" line="432"/>
<source>&amp;Paste Image</source>
<translation>(&amp;P)</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="424"/>
<location filename="../mainwindow.cpp" line="438"/>
<source>&amp;Paste Image File</source>
<translation>(&amp;P)</translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="429"/>
<location filename="../mainwindow.cpp" line="448"/>
<location filename="../mainwindow.cpp" line="443"/>
<location filename="../mainwindow.cpp" line="462"/>
<source>Stay on top</source>
<translation></translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="435"/>
<location filename="../mainwindow.cpp" line="449"/>
<location filename="../mainwindow.cpp" line="463"/>
<source>Protected mode</source>
<translation></translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="441"/>
<location filename="../mainwindow.cpp" line="455"/>
<source>Help</source>
<translation></translation>
</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>
<translation></translation>
</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>
<translation></translation>
</message>
<message>
<location filename="../mainwindow.cpp" line="447"/>
<location filename="../mainwindow.cpp" line="461"/>
<source>Context menu option explanation:</source>
<translation></translation>
</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>
<translation>使</translation>
</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>
<translation></translation>
</message>

View File

@ -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)
{
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)
{
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();
}
}

View File

@ -60,6 +60,7 @@ protected slots:
bool stayOnTop();
void quitAppAction(bool force = false);
void toggleFullscreen();
void toggleMaximize();
private:
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:
};