Settings class converted to singleton pattern.
This commit is contained in:
parent
0f91a7302b
commit
d5157c5102
@ -174,6 +174,7 @@ set(APP_HEADERS
|
|||||||
# GUI headers.
|
# GUI headers.
|
||||||
src/gui/formmain.h
|
src/gui/formmain.h
|
||||||
src/gui/systemtrayicon.cpp
|
src/gui/systemtrayicon.cpp
|
||||||
|
src/gui/themefactory.cpp
|
||||||
|
|
||||||
# CORE headers.
|
# CORE headers.
|
||||||
)
|
)
|
||||||
|
@ -26,7 +26,16 @@
|
|||||||
#include "core/defs.h"
|
#include "core/defs.h"
|
||||||
|
|
||||||
|
|
||||||
QPointer<QSettings> Settings::s_instance;
|
QPointer<Settings> Settings::s_instance;
|
||||||
|
|
||||||
|
Settings::Settings(const QString &file_name, Format format, QObject *parent)
|
||||||
|
: QSettings(file_name, format, parent) {
|
||||||
|
}
|
||||||
|
|
||||||
|
Settings::~Settings() {
|
||||||
|
checkSettings();
|
||||||
|
qDebug("Deleting Settings instance.");
|
||||||
|
}
|
||||||
|
|
||||||
QSettings::Status Settings::checkSettings() {
|
QSettings::Status Settings::checkSettings() {
|
||||||
qDebug("Syncing settings.");
|
qDebug("Syncing settings.");
|
||||||
@ -35,28 +44,24 @@ QSettings::Status Settings::checkSettings() {
|
|||||||
return s_instance->status();
|
return s_instance->status();
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant Settings::value(const QString §ion,
|
Settings &Settings::getInstance() {
|
||||||
const QString &key,
|
|
||||||
const QVariant &default_value) {
|
|
||||||
if (s_instance.isNull()) {
|
if (s_instance.isNull()) {
|
||||||
setupSettings();
|
setupSettings();
|
||||||
}
|
}
|
||||||
return s_instance->value(QString("%1/%2").arg(section, key), default_value);
|
|
||||||
|
return *s_instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant Settings::value(const QString §ion,
|
||||||
|
const QString &key,
|
||||||
|
const QVariant &default_value) {
|
||||||
|
return QSettings::value(QString("%1/%2").arg(section, key), default_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Settings::setValue(const QString §ion,
|
void Settings::setValue(const QString §ion,
|
||||||
const QString &key,
|
const QString &key,
|
||||||
const QVariant &value) {
|
const QVariant &value) {
|
||||||
if (s_instance.isNull()) {
|
QSettings::setValue(QString("%1/%2").arg(section, key), value);
|
||||||
setupSettings();
|
|
||||||
}
|
|
||||||
s_instance->setValue(QString("%1/%2").arg(section, key), value);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Settings::deleteSettings() {
|
|
||||||
checkSettings();
|
|
||||||
qDebug("Deleting global settings.");
|
|
||||||
delete s_instance.data();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QSettings::Status Settings::setupSettings() {
|
QSettings::Status Settings::setupSettings() {
|
||||||
@ -70,15 +75,15 @@ QSettings::Status Settings::setupSettings() {
|
|||||||
APP_CFG_PATH;
|
APP_CFG_PATH;
|
||||||
|
|
||||||
if (QFile(app_path).exists()) {
|
if (QFile(app_path).exists()) {
|
||||||
s_instance = new QSettings(app_path, QSettings::IniFormat);
|
s_instance = new Settings(app_path, QSettings::IniFormat, qApp);
|
||||||
qDebug("Initializing settings in %s.",
|
qDebug("Initializing settings in %s.",
|
||||||
qPrintable(QDir::toNativeSeparators(app_path)));
|
qPrintable(QDir::toNativeSeparators(app_path)));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
s_instance = new QSettings(home_path, QSettings::IniFormat);
|
s_instance = new Settings(home_path, QSettings::IniFormat, qApp);
|
||||||
qDebug("Initializing settings in %s.",
|
qDebug("Initializing settings in %s.",
|
||||||
qPrintable(QDir::toNativeSeparators(home_path)));
|
qPrintable(QDir::toNativeSeparators(home_path)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return checkSettings();
|
return (*s_instance).checkSettings();
|
||||||
}
|
}
|
||||||
|
@ -4,27 +4,31 @@
|
|||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
|
|
||||||
|
|
||||||
class Settings {
|
class Settings : public QSettings {
|
||||||
private:
|
private:
|
||||||
// We use QPointer instead of QScopedPointer
|
// We use QPointer instead of QScopedPointer
|
||||||
// because of late s_instance usage in QApplication::aboutToQuit() listeners.
|
// because of late s_instance usage in QApplication::aboutToQuit() listeners.
|
||||||
static QPointer<QSettings> s_instance;
|
static QPointer<Settings> s_instance;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
// Singleton getter.
|
||||||
|
static Settings &getInstance();
|
||||||
|
|
||||||
|
// Constructor and destructor.
|
||||||
|
Settings(const QString & file_name, Format format, QObject * parent = 0);
|
||||||
|
~Settings();
|
||||||
|
|
||||||
// Getter/setter for settings values.
|
// Getter/setter for settings values.
|
||||||
static QVariant value(const QString §ion,
|
QVariant value(const QString §ion,
|
||||||
const QString &key,
|
const QString &key,
|
||||||
const QVariant &default_value = QVariant());
|
const QVariant &default_value = QVariant());
|
||||||
|
|
||||||
static void setValue(const QString §ion,
|
void setValue(const QString §ion,
|
||||||
const QString &key,
|
const QString &key,
|
||||||
const QVariant &value);
|
const QVariant &value);
|
||||||
|
|
||||||
// It's better to cleanup settings manually via this function.
|
|
||||||
static void deleteSettings();
|
|
||||||
|
|
||||||
// Synchronises settings.
|
// Synchronises settings.
|
||||||
static QSettings::Status checkSettings();
|
QSettings::Status checkSettings();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// Creates settings file in correct location.
|
// Creates settings file in correct location.
|
||||||
|
@ -19,10 +19,19 @@ void FormMain::processExecutionMessage(const QString &message) {
|
|||||||
qPrintable(message));
|
qPrintable(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FormMain::quit() {
|
||||||
|
qDebug("Quitting the application.");
|
||||||
|
qApp->quit();
|
||||||
|
}
|
||||||
|
|
||||||
void FormMain::cleanupResources() {
|
void FormMain::cleanupResources() {
|
||||||
Settings::deleteSettings();
|
qDebug("Cleaning up resources before the application exits.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void FormMain::createConnections() {
|
void FormMain::createConnections() {
|
||||||
|
// Menu "File" connections.
|
||||||
|
connect(m_ui->m_actionQuit, &QAction::triggered, this, &FormMain::quit);
|
||||||
|
|
||||||
|
// General connections.
|
||||||
connect(qApp, &QCoreApplication::aboutToQuit, this, &FormMain::cleanupResources);
|
connect(qApp, &QCoreApplication::aboutToQuit, this, &FormMain::cleanupResources);
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ class FormMain : public QMainWindow {
|
|||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void processExecutionMessage(const QString &message);
|
void processExecutionMessage(const QString &message);
|
||||||
|
void quit();
|
||||||
|
|
||||||
protected slots:
|
protected slots:
|
||||||
void cleanupResources();
|
void cleanupResources();
|
||||||
|
@ -14,17 +14,54 @@
|
|||||||
<string>MainWindow</string>
|
<string>MainWindow</string>
|
||||||
</property>
|
</property>
|
||||||
<widget class="QWidget" name="centralwidget"/>
|
<widget class="QWidget" name="centralwidget"/>
|
||||||
<widget class="QMenuBar" name="menubar">
|
<widget class="QMenuBar" name="m_menuBar">
|
||||||
<property name="geometry">
|
<property name="geometry">
|
||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>800</width>
|
<width>800</width>
|
||||||
<height>21</height>
|
<height>19</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
|
<widget class="QMenu" name="m_menuFile">
|
||||||
|
<property name="title">
|
||||||
|
<string>&File</string>
|
||||||
|
</property>
|
||||||
|
<addaction name="m_actionImport"/>
|
||||||
|
<addaction name="m_actionExport"/>
|
||||||
|
<addaction name="separator"/>
|
||||||
|
<addaction name="m_actionQuit"/>
|
||||||
|
</widget>
|
||||||
|
<widget class="QMenu" name="m_menuHelp">
|
||||||
|
<property name="title">
|
||||||
|
<string>&Help</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<widget class="QMenu" name="menu_View">
|
||||||
|
<property name="title">
|
||||||
|
<string>&View</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<addaction name="m_menuFile"/>
|
||||||
|
<addaction name="menu_View"/>
|
||||||
|
<addaction name="m_menuHelp"/>
|
||||||
</widget>
|
</widget>
|
||||||
<widget class="QStatusBar" name="statusbar"/>
|
<widget class="QStatusBar" name="statusbar"/>
|
||||||
|
<action name="m_actionImport">
|
||||||
|
<property name="text">
|
||||||
|
<string>&Import</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="m_actionExport">
|
||||||
|
<property name="text">
|
||||||
|
<string>E&xport</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="m_actionQuit">
|
||||||
|
<property name="text">
|
||||||
|
<string>&Quit</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
</widget>
|
</widget>
|
||||||
<resources/>
|
<resources/>
|
||||||
<connections/>
|
<connections/>
|
||||||
|
5
src/gui/themefactory.cpp
Normal file
5
src/gui/themefactory.cpp
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#include "themefactory.h"
|
||||||
|
|
||||||
|
|
||||||
|
ThemeFactory::ThemeFactory() {
|
||||||
|
}
|
10
src/gui/themefactory.h
Normal file
10
src/gui/themefactory.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#ifndef THEMEFACTORY_H
|
||||||
|
#define THEMEFACTORY_H
|
||||||
|
|
||||||
|
|
||||||
|
class ThemeFactory {
|
||||||
|
private:
|
||||||
|
ThemeFactory();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // THEMEFACTORY_H
|
@ -63,9 +63,9 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
window.show();
|
window.show();
|
||||||
|
|
||||||
if (Settings::value(APP_CFG_GEN, "first_start", true).toBool()) {
|
if (Settings::getInstance().value(APP_CFG_GEN, "first_start", true).toBool()) {
|
||||||
// TODO: Open initial "Welcome" dialog here.
|
// TODO: Open initial "Welcome" dialog here.
|
||||||
Settings::setValue(APP_CFG_GEN, "first_start", false);
|
Settings::getInstance().setValue(APP_CFG_GEN, "first_start", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user