From 3c3729711c86a621b425370fd11d4271e4208f1b Mon Sep 17 00:00:00 2001 From: jan Iversen Date: Tue, 17 Jul 2018 15:22:51 +0200 Subject: core: remove QSettings in qPref* and use a shared variable Add qPrefPrivate class which contains one QSettings variable, delete QSettings from qPref* class definitions this secures there are only instance of QSettings (QSettings needs to be in a QObject class to work) Signed-off-by: Jan Iversen --- core/CMakeLists.txt | 1 + core/settings/qPref.cpp | 2 +- core/settings/qPrefAnimations.cpp | 5 +- core/settings/qPrefAnimations.h | 4 - core/settings/qPrefDisplay.cpp | 4 +- core/settings/qPrefDisplay.h | 4 - core/settings/qPrefPrivate.cpp | 11 ++ core/settings/qPrefPrivate.h | 226 ++++++++++++++++++++++++++++++++++++++ core/settings/qPref_private.h | 213 ----------------------------------- 9 files changed, 245 insertions(+), 225 deletions(-) create mode 100644 core/settings/qPrefPrivate.cpp create mode 100644 core/settings/qPrefPrivate.h delete mode 100644 core/settings/qPref_private.h (limited to 'core') diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 5b476c779..9a7d108c5 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -102,6 +102,7 @@ set(SUBSURFACE_CORE_LIB_SRCS settings/qPref.cpp settings/qPrefAnimations.cpp settings/qPrefDisplay.cpp + settings/qPrefPrivate.cpp #Subsurface Qt have the Subsurface structs QObjectified for easy access via QML. subsurface-qt/DiveObjectHelper.cpp diff --git a/core/settings/qPref.cpp b/core/settings/qPref.cpp index 027d4af5a..1605e30df 100644 --- a/core/settings/qPref.cpp +++ b/core/settings/qPref.cpp @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-2.0 -#include "qPref_private.h" +#include "qPrefPrivate.h" #include "qPref.h" #include "ssrf-version.h" diff --git a/core/settings/qPrefAnimations.cpp b/core/settings/qPrefAnimations.cpp index a6ecc1d5b..9be8900c5 100644 --- a/core/settings/qPrefAnimations.cpp +++ b/core/settings/qPrefAnimations.cpp @@ -1,7 +1,8 @@ // SPDX-License-Identifier: GPL-2.0 #include "qPref.h" -#include "qPref_private.h" -#include "qPrefAnimations.h" +#include "qPrefPrivate.h" + +static const QString group = QStringLiteral("Animations"); qPrefAnimations::qPrefAnimations(QObject *parent) : QObject(parent) { diff --git a/core/settings/qPrefAnimations.h b/core/settings/qPrefAnimations.h index fb58b207e..4971aa06d 100644 --- a/core/settings/qPrefAnimations.h +++ b/core/settings/qPrefAnimations.h @@ -3,7 +3,6 @@ #define QPREFANIMATIONS_H #include -#include class qPrefAnimations : public QObject { Q_OBJECT @@ -28,9 +27,6 @@ signals: void animation_speed_changed(int value); private: - const QString group = QStringLiteral("Animations"); - QSettings setting; - // functions to load/sync variable with disk void disk_animation_speed(bool doSync); }; diff --git a/core/settings/qPrefDisplay.cpp b/core/settings/qPrefDisplay.cpp index a248294d1..55a0129a9 100644 --- a/core/settings/qPrefDisplay.cpp +++ b/core/settings/qPrefDisplay.cpp @@ -1,11 +1,13 @@ // SPDX-License-Identifier: GPL-2.0 #include "qPref.h" -#include "qPref_private.h" +#include "qPrefPrivate.h" #include "core/subsurface-string.h" #include #include +static const QString group = QStringLiteral("Display"); + qPrefDisplay::qPrefDisplay(QObject *parent) : QObject(parent) { } diff --git a/core/settings/qPrefDisplay.h b/core/settings/qPrefDisplay.h index 4a1c0ada5..cb6b872f9 100644 --- a/core/settings/qPrefDisplay.h +++ b/core/settings/qPrefDisplay.h @@ -3,7 +3,6 @@ #define QPREFDISPLAY_H #include -#include class qPrefDisplay : public QObject { Q_OBJECT @@ -44,9 +43,6 @@ signals: void theme_changed(const QString& value); private: - const QString group = QStringLiteral("Display"); - QSettings setting; - // functions to load/sync variable with disk void disk_divelist_font(bool doSync); void disk_font_size(bool doSync); diff --git a/core/settings/qPrefPrivate.cpp b/core/settings/qPrefPrivate.cpp new file mode 100644 index 000000000..53b99c4e5 --- /dev/null +++ b/core/settings/qPrefPrivate.cpp @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "qPrefPrivate.h" + +qPrefPrivate::qPrefPrivate(QObject *parent) : QObject(parent) +{ +} +qPrefPrivate *qPrefPrivate::instance() +{ + static qPrefPrivate *self = new qPrefPrivate; + return self; +} diff --git a/core/settings/qPrefPrivate.h b/core/settings/qPrefPrivate.h new file mode 100644 index 000000000..e4131e9e4 --- /dev/null +++ b/core/settings/qPrefPrivate.h @@ -0,0 +1,226 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef QPREFPRIVATE_H +#define QPREFPRIVATE_H + +// Header used by all qPref implementations to avoid duplicating code + +#include +#include +#include +#include "core/qthelper.h" + +// implementation class of the interface classes +class qPrefPrivate : public QObject { + Q_OBJECT + +public: + static qPrefPrivate *instance(); + + QSettings setting; + +private: + qPrefPrivate(QObject *parent = NULL); +}; + +//****** Macros to be used in the set functions ****** +#define COPY_TXT(name, string) \ +{ \ + free((void *)prefs.name); \ + prefs.name = copy_qstring(string); \ +} + +//****** Macros to be used in the disk functions, which are special ****** +#define LOADSYNC_BOOL(name, field) \ +{ \ + if (doSync) \ + qPrefPrivate::instance()->setting.setValue(group + name, prefs.field); \ + else \ + prefs.field = qPrefPrivate::instance()->setting.value(group + name, default_prefs.field).toBool(); \ +} + +#define LOADSYNC_DOUBLE(name, field) \ +{ \ + if (doSync) \ + qPrefPrivate::instance()->setting.setValue(group + name, prefs.field); \ + else \ + prefs.field = qPrefPrivate::instance()->setting.value(group + name, default_prefs.field).toDouble(); \ +} + +#define LOADSYNC_ENUM(name, type, field) \ +{ \ + if (doSync) \ + qPrefPrivate::instance()->setting.setValue(group + name, prefs.field); \ + else \ + prefs.field = (enum type)qPrefPrivate::instance()->setting.value(group + name, default_prefs.field).toInt(); \ +} + +#define LOADSYNC_INT(name, field) \ +{ \ + if (doSync) \ + qPrefPrivate::instance()->setting.setValue(group + name, prefs.field); \ + else \ + prefs.field = qPrefPrivate::instance()->setting.value(group + name, default_prefs.field).toInt(); \ +} + +#define LOADSYNC_INT_DEF(name, field, defval) \ +{ \ + if (doSync) \ + qPrefPrivate::instance()->setting.setValue(group + name, prefs.field); \ + else \ + prefs.field = qPrefPrivate::instance()->setting.value(group + name, defval).toInt(); \ +} + +#define LOADSYNC_TXT(name, field) \ +{ \ + if (doSync) \ + qPrefPrivate::instance()->setting.setValue(group + name, prefs.field); \ + else \ + prefs.field = copy_qstring(qPrefPrivate::instance()->setting.value(group + name, default_prefs.field).toString()); \ +} + +//******* Macros to generate disk function +#define DISK_LOADSYNC_BOOL(usegroup, name, field) \ +void qPref ## usegroup::disk_ ## field(bool doSync) \ +{ \ + LOADSYNC_BOOL(name, field); \ +} + +#define DISK_LOADSYNC_DOUBLE(usegroup, name, field) \ +void qPref ## usegroup::disk_ ## field(bool doSync) \ +{ \ + LOADSYNC_DOUBLE(name, field); \ +} + +#define DISK_LOADSYNC_ENUM(usegroup, name, type, field) \ +void qPref ## usegroup::disk_ ## field(bool doSync) \ +{ \ + LOADSYNC_ENUM(name, type, field); \ +} + +#define DISK_LOADSYNC_INT(usegroup, name, field) \ +void qPref ## usegroup::disk_ ## field(bool doSync) \ +{ \ + LOADSYNC_INT(name, field); \ +} + +#define DISK_LOADSYNC_INT_DEF(usegroup, name, field, defval) \ +void qPref ## usegroup::disk_ ## field(bool doSync) \ +{ \ + LOADSYNC_INT_DEF(name, field, defval); \ +} + +#define DISK_LOADSYNC_TXT(usegroup, name, field) \ +void qPref ## usegroup::disk_ ## field(bool doSync) \ +{ \ + LOADSYNC_TXT(name, field); \ +} + +//******* Macros to generate get function +#define GET_PREFERENCE_BOOL(usegroup, field) \ +bool qPref ## usegroup::field () const \ +{ \ + return prefs.field; \ +} + +#define GET_PREFERENCE_DOUBLE(usegroup, field) \ +double qPref ## usegroup::field () const \ +{ \ + return prefs.field; \ +} + +#define GET_PREFERENCE_ENUM(usegroup, type, field) \ +struct type qPref ## usegroup:: ## field () const \ +{ \ + return prefs.field; \ +} + +#define GET_PREFERENCE_INT(usegroup, field) \ +int qPref ## usegroup::field () const \ +{ \ + return prefs.field; \ +} + +#define GET_PREFERENCE_TXT(usegroup, field) \ +const QString qPref ## usegroup::field () const \ +{ \ + return prefs.field; \ +} + +//******* Macros to generate set function +#define SET_PREFERENCE_BOOL(usegroup, field) \ +void qPref ## usegroup::set_ ## field (bool value) \ +{ \ + if (value != prefs.field) { \ + prefs.field = value; \ + disk_ ## field(true); \ + emit field ## _changed(value); \ + } \ +} + +#define SET_PREFERENCE_DOUBLE(usegroup, field) \ +void qPref ## usegroup::set_ ## field (double value) \ +{ \ + if (value != prefs.field) { \ + prefs.field = value; \ + disk_ ## field(true); \ + emit field ## _changed(value); \ + } \ +} + +#define SET_PREFERENCE_ENUM(usegroup, type, field) \ +void qPref ## usegroup::set_ ## field (type value) \ +{ \ + if (value != prefs.field) { \ + prefs.field = value; \ + disk_ ## field(true); \ + emit field ## _changed(value); \ + } \ +} + +#define SET_PREFERENCE_INT(usegroup, field) \ +void qPref ## usegroup::set_ ## field (int value) \ +{ \ + if (value != prefs.field) { \ + prefs.field = value; \ + disk_ ## field(true); \ + emit field ## _changed(value); \ + } \ +} + +#define SET_PREFERENCE_TXT(usegroup, field) \ +void qPref ## usegroup::set_ ## field (const QString& value) \ +{ \ + if (value != prefs.field) { \ + COPY_TXT(field, value); \ + disk_ ## field(true); \ + emit field ## _changed(value); \ + } \ +} + +//******* Macros to generate set/set/loadsync combined +#define HANDLE_PREFERENCE_BOOL(usegroup, name, field) \ +GET_PREFERENCE_BOOL(usegroup, field); \ +SET_PREFERENCE_BOOL(usegroup, field); \ +DISK_LOADSYNC_BOOL(usegroup, name, field); + +#define HANDLE_PREFERENCE_DOUBLE(usegroup, name, field) \ +GET_PREFERENCE_DOUBLE(usegroup, field); \ +SET_PREFERENCE_DOUBLE(usegroup, field); \ +DISK_LOADSYNC_DOUBLE(usegroup, name, field); + +#define HANDLE_PREFERENCE_ENUM(usegroup, type, name, field) \ +GET_PREFERENCE_ENUM(usegroup, type, field); \ +SET_PREFERENCE_ENUM(usegroup, type, field); \ +DISK_LOADSYNC_ENUM(usegroup, name, type, field); + +#define HANDLE_PREFERENCE_INT(usegroup, name, field) \ +GET_PREFERENCE_INT(usegroup, field); \ +SET_PREFERENCE_INT(usegroup, field); \ +DISK_LOADSYNC_INT(usegroup, name, field); + +#define HANDLE_PREFERENCE_TXT(usegroup, name, field) \ +GET_PREFERENCE_TXT(usegroup, field); \ +SET_PREFERENCE_TXT(usegroup, field); \ +DISK_LOADSYNC_TXT(usegroup, name, field); + +#endif diff --git a/core/settings/qPref_private.h b/core/settings/qPref_private.h deleted file mode 100644 index de06029fa..000000000 --- a/core/settings/qPref_private.h +++ /dev/null @@ -1,213 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -#ifndef QPREFPRIVATE_H -#define QPREFPRIVATE_H - -// Header used by all qPref implementations to avoid duplicating code - -#include -#include -#include -#include "core/qthelper.h" - -//****** Macros to be used in the set functions ****** -#define COPY_TXT(name, string) \ -{ \ - free((void *)prefs.name); \ - prefs.name = copy_qstring(string); \ -} - -//****** Macros to be used in the disk functions, which are special ****** -#define LOADSYNC_BOOL(name, field) \ -{ \ - if (doSync) \ - setting.setValue(group + name, prefs.field); \ - else \ - prefs.field = setting.value(group + name, default_prefs.field).toBool(); \ -} - -#define LOADSYNC_DOUBLE(name, field) \ -{ \ - if (doSync) \ - setting.setValue(group + name, prefs.field); \ - else \ - prefs.field = setting.value(group + name, default_prefs.field).toDouble(); \ -} - -#define LOADSYNC_ENUM(name, type, field) \ -{ \ - if (doSync) \ - setting.setValue(group + name, prefs.field); \ - else \ - prefs.field = (enum type)setting.value(group + name, default_prefs.field).toInt(); \ -} - -#define LOADSYNC_INT(name, field) \ -{ \ - if (doSync) \ - setting.setValue(group + name, prefs.field); \ - else \ - prefs.field = setting.value(group + name, default_prefs.field).toInt(); \ -} - -#define LOADSYNC_INT_DEF(name, field, defval) \ -{ \ - if (doSync) \ - setting.setValue(group + name, prefs.field); \ - else \ - prefs.field = setting.value(group + name, defval).toInt(); \ -} - -#define LOADSYNC_TXT(name, field) \ -{ \ - if (doSync) \ - setting.setValue(group + name, prefs.field); \ - else \ - prefs.field = copy_qstring(setting.value(group + name, default_prefs.field).toString()); \ -} - -//******* Macros to generate disk function -#define DISK_LOADSYNC_BOOL(usegroup, name, field) \ -void qPref ## usegroup::disk_ ## field(bool doSync) \ -{ \ - LOADSYNC_BOOL(name, field); \ -} - -#define DISK_LOADSYNC_DOUBLE(usegroup, name, field) \ -void qPref ## usegroup::disk_ ## field(bool doSync) \ -{ \ - LOADSYNC_DOUBLE(name, field); \ -} - -#define DISK_LOADSYNC_ENUM(usegroup, name, type, field) \ -void qPref ## usegroup::disk_ ## field(bool doSync) \ -{ \ - LOADSYNC_ENUM(name, type, field); \ -} - -#define DISK_LOADSYNC_INT(usegroup, name, field) \ -void qPref ## usegroup::disk_ ## field(bool doSync) \ -{ \ - LOADSYNC_INT(name, field); \ -} - -#define DISK_LOADSYNC_INT_DEF(usegroup, name, field, defval) \ -void qPref ## usegroup::disk_ ## field(bool doSync) \ -{ \ - LOADSYNC_INT_DEF(name, field, defval); \ -} - -#define DISK_LOADSYNC_TXT(usegroup, name, field) \ -void qPref ## usegroup::disk_ ## field(bool doSync) \ -{ \ - LOADSYNC_TXT(name, field); \ -} - -//******* Macros to generate get function -#define GET_PREFERENCE_BOOL(usegroup, field) \ -bool qPref ## usegroup::field () const \ -{ \ - return prefs.field; \ -} - -#define GET_PREFERENCE_DOUBLE(usegroup, field) \ -double qPref ## usegroup::field () const \ -{ \ - return prefs.field; \ -} - -#define GET_PREFERENCE_ENUM(usegroup, type, field) \ -struct type qPref ## usegroup:: ## field () const \ -{ \ - return prefs.field; \ -} - -#define GET_PREFERENCE_INT(usegroup, field) \ -int qPref ## usegroup::field () const \ -{ \ - return prefs.field; \ -} - -#define GET_PREFERENCE_TXT(usegroup, field) \ -const QString qPref ## usegroup::field () const \ -{ \ - return prefs.field; \ -} - -//******* Macros to generate set function -#define SET_PREFERENCE_BOOL(usegroup, field) \ -void qPref ## usegroup::set_ ## field (bool value) \ -{ \ - if (value != prefs.field) { \ - prefs.field = value; \ - disk_ ## field(true); \ - emit field ## _changed(value); \ - } \ -} - -#define SET_PREFERENCE_DOUBLE(usegroup, field) \ -void qPref ## usegroup::set_ ## field (double value) \ -{ \ - if (value != prefs.field) { \ - prefs.field = value; \ - disk_ ## field(true); \ - emit field ## _changed(value); \ - } \ -} - -#define SET_PREFERENCE_ENUM(usegroup, type, field) \ -void qPref ## usegroup::set_ ## field (type value) \ -{ \ - if (value != prefs.field) { \ - prefs.field = value; \ - disk_ ## field(true); \ - emit field ## _changed(value); \ - } \ -} - -#define SET_PREFERENCE_INT(usegroup, field) \ -void qPref ## usegroup::set_ ## field (int value) \ -{ \ - if (value != prefs.field) { \ - prefs.field = value; \ - disk_ ## field(true); \ - emit field ## _changed(value); \ - } \ -} - -#define SET_PREFERENCE_TXT(usegroup, field) \ -void qPref ## usegroup::set_ ## field (const QString& value) \ -{ \ - if (value != prefs.field) { \ - COPY_TXT(field, value); \ - disk_ ## field(true); \ - emit field ## _changed(value); \ - } \ -} - -//******* Macros to generate set/set/loadsync combined -#define HANDLE_PREFERENCE_BOOL(usegroup, name, field) \ -GET_PREFERENCE_BOOL(usegroup, field); \ -SET_PREFERENCE_BOOL(usegroup, field); \ -DISK_LOADSYNC_BOOL(usegroup, name, field); - -#define HANDLE_PREFERENCE_DOUBLE(usegroup, name, field) \ -GET_PREFERENCE_DOUBLE(usegroup, field); \ -SET_PREFERENCE_DOUBLE(usegroup, field); \ -DISK_LOADSYNC_DOUBLE(usegroup, name, field); - -#define HANDLE_PREFERENCE_ENUM(usegroup, type, name, field) \ -GET_PREFERENCE_ENUM(usegroup, type, field); \ -SET_PREFERENCE_ENUM(usegroup, type, field); \ -DISK_LOADSYNC_ENUM(usegroup, name, type, field); - -#define HANDLE_PREFERENCE_INT(usegroup, name, field) \ -GET_PREFERENCE_INT(usegroup, field); \ -SET_PREFERENCE_INT(usegroup, field); \ -DISK_LOADSYNC_INT(usegroup, name, field); - -#define HANDLE_PREFERENCE_TXT(usegroup, name, field) \ -GET_PREFERENCE_TXT(usegroup, field); \ -SET_PREFERENCE_TXT(usegroup, field); \ -DISK_LOADSYNC_TXT(usegroup, name, field); - -#endif -- cgit v1.2.3-70-g09d2