Settings class converted to singleton pattern.
This commit is contained in:
parent
0f91a7302b
commit
d5157c5102
@ -174,6 +174,7 @@ set(APP_HEADERS
|
||||
# GUI headers.
|
||||
src/gui/formmain.h
|
||||
src/gui/systemtrayicon.cpp
|
||||
src/gui/themefactory.cpp
|
||||
|
||||
# CORE headers.
|
||||
)
|
||||
|
@ -26,7 +26,16 @@
|
||||
#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() {
|
||||
qDebug("Syncing settings.");
|
||||
@ -35,28 +44,24 @@ QSettings::Status Settings::checkSettings() {
|
||||
return s_instance->status();
|
||||
}
|
||||
|
||||
QVariant Settings::value(const QString §ion,
|
||||
const QString &key,
|
||||
const QVariant &default_value) {
|
||||
Settings &Settings::getInstance() {
|
||||
if (s_instance.isNull()) {
|
||||
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,
|
||||
const QString &key,
|
||||
const QVariant &value) {
|
||||
if (s_instance.isNull()) {
|
||||
setupSettings();
|
||||
}
|
||||
s_instance->setValue(QString("%1/%2").arg(section, key), value);
|
||||
}
|
||||
|
||||
void Settings::deleteSettings() {
|
||||
checkSettings();
|
||||
qDebug("Deleting global settings.");
|
||||
delete s_instance.data();
|
||||
QSettings::setValue(QString("%1/%2").arg(section, key), value);
|
||||
}
|
||||
|
||||
QSettings::Status Settings::setupSettings() {
|
||||
@ -70,15 +75,15 @@ QSettings::Status Settings::setupSettings() {
|
||||
APP_CFG_PATH;
|
||||
|
||||
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.",
|
||||
qPrintable(QDir::toNativeSeparators(app_path)));
|
||||
}
|
||||
else {
|
||||
s_instance = new QSettings(home_path, QSettings::IniFormat);
|
||||
s_instance = new Settings(home_path, QSettings::IniFormat, qApp);
|
||||
qDebug("Initializing settings in %s.",
|
||||
qPrintable(QDir::toNativeSeparators(home_path)));
|
||||
}
|
||||
|
||||
return checkSettings();
|
||||
return (*s_instance).checkSettings();
|
||||
}
|
||||
|
@ -4,27 +4,31 @@
|
||||
#include <QSettings>
|
||||
|
||||
|
||||
class Settings {
|
||||
class Settings : public QSettings {
|
||||
private:
|
||||
// We use QPointer instead of QScopedPointer
|
||||
// because of late s_instance usage in QApplication::aboutToQuit() listeners.
|
||||
static QPointer<QSettings> s_instance;
|
||||
static QPointer<Settings> s_instance;
|
||||
|
||||
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.
|
||||
static QVariant value(const QString §ion,
|
||||
const QString &key,
|
||||
const QVariant &default_value = QVariant());
|
||||
QVariant value(const QString §ion,
|
||||
const QString &key,
|
||||
const QVariant &default_value = QVariant());
|
||||
|
||||
static void setValue(const QString §ion,
|
||||
const QString &key,
|
||||
const QVariant &value);
|
||||
|
||||
// It's better to cleanup settings manually via this function.
|
||||
static void deleteSettings();
|
||||
void setValue(const QString §ion,
|
||||
const QString &key,
|
||||
const QVariant &value);
|
||||
|
||||
// Synchronises settings.
|
||||
static QSettings::Status checkSettings();
|
||||
QSettings::Status checkSettings();
|
||||
|
||||
protected:
|
||||
// Creates settings file in correct location.
|
||||
|
@ -19,10 +19,19 @@ void FormMain::processExecutionMessage(const QString &message) {
|
||||
qPrintable(message));
|
||||
}
|
||||
|
||||
void FormMain::quit() {
|
||||
qDebug("Quitting the application.");
|
||||
qApp->quit();
|
||||
}
|
||||
|
||||
void FormMain::cleanupResources() {
|
||||
Settings::deleteSettings();
|
||||
qDebug("Cleaning up resources before the application exits.");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ class FormMain : public QMainWindow {
|
||||
|
||||
public slots:
|
||||
void processExecutionMessage(const QString &message);
|
||||
void quit();
|
||||
|
||||
protected slots:
|
||||
void cleanupResources();
|
||||
|
@ -14,17 +14,54 @@
|
||||
<string>MainWindow</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralwidget"/>
|
||||
<widget class="QMenuBar" name="menubar">
|
||||
<widget class="QMenuBar" name="m_menuBar">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>21</height>
|
||||
<height>19</height>
|
||||
</rect>
|
||||
</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 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>
|
||||
<resources/>
|
||||
<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();
|
||||
|
||||
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.
|
||||
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