summaryrefslogtreecommitdiffstats
path: root/core/settings/qPrefPrivate.h
diff options
context:
space:
mode:
authorGravatar jan Iversen <jani@apache.org>2018-07-17 15:22:51 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2018-07-18 10:40:08 -0700
commit3c3729711c86a621b425370fd11d4271e4208f1b (patch)
tree6d689f6b3ce4e24997d1a79e63e3039eef8c7642 /core/settings/qPrefPrivate.h
parentc0e9e978a1aa70713f9478391ebde353708a2f07 (diff)
downloadsubsurface-3c3729711c86a621b425370fd11d4271e4208f1b.tar.gz
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 <jani@apache.org>
Diffstat (limited to 'core/settings/qPrefPrivate.h')
-rw-r--r--core/settings/qPrefPrivate.h226
1 files changed, 226 insertions, 0 deletions
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<class> implementations to avoid duplicating code
+
+#include <QSettings>
+#include <QVariant>
+#include <QObject>
+#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