ckb-next  v0.2.8 at branch master
ckb-next driver for corsair devices
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ckbsettings.h
Go to the documentation of this file.
1 #ifndef CKBSETTINGS_H
2 #define CKBSETTINGS_H
3 
4 #include <QSettings>
5 #include <QStringList>
6 #include <QMap>
7 #include <QVariant>
8 
9 // QSettings replacement with convenience functions
10 
12 {
13 public:
14  // Basic settings object. No default group.
15  CkbSettings();
16  // Settings object with a default group. Optionally erases any existing group with the same name.
17  CkbSettings(const QString& basePath, bool eraseExisting = false);
18  // CkbSettings from QSettings
19  CkbSettings(QSettings& settings);
20 
21  ~CkbSettings();
22 
23  // One-shot get/set
24  static QVariant get(const QString& key, const QVariant& defaultValue = QVariant());
25  static void set(const QString& key, const QVariant& value);
26 
27  // Whether or not CkbSettings is busy writing data. If busy, the constructors will block until it is not.
28  // The global set() will also block if busy, but global get() will not, unless the value has never been read before.
29  static bool isBusy();
30 
31  // Finalize all writes, clean up and release resources
32  static void cleanUp();
33 
34  // QSettings functions
35  void beginGroup(const QString& prefix);
36  void endGroup();
37  QStringList childGroups() const;
38  QStringList childKeys() const;
39  bool contains(const QString& key) const;
40  bool containsGroup(const QString& group);
41  QVariant value(const QString& key, const QVariant& defaultValue = QVariant()) const;
42  void setValue(const QString& key, const QVariant& value);
43  void remove(const QString& key);
44 
45 private:
46  QSettings* backing;
47  QStringList groups;
48  QStringList removeCache;
49  QMap<QString, QVariant> writeCache;
50 
51  inline QString pwd() const { return groups.join("/"); }
52  inline QString pwd(const QString& key) const { return pwd() + (groups.isEmpty() ? "" : "/") + key; }
53 };
54 
55 // Settings group wrapper. Useful for easily pushing/popping a group without worrying about everything breaking if you forgot endGroup.
56 
57 class SGroup
58 {
59 public:
60  inline SGroup(CkbSettings& settings, const QString& prefix) : _settings(settings) { settings.beginGroup(prefix); }
61  inline ~SGroup() { _settings.endGroup(); }
62 
63 private:
65 };
66 
67 #endif // CKBSETTINGS_H
void setValue(const QString &key, const QVariant &value)
QStringList groups
Definition: ckbsettings.h:47
static void set(const QString &key, const QVariant &value)
QStringList childKeys() const
QString pwd(const QString &key) const
Definition: ckbsettings.h:52
QSettings * backing
Definition: ckbsettings.h:46
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
void endGroup()
Definition: ckbsettings.cpp:85
~SGroup()
Definition: ckbsettings.h:61
SGroup(CkbSettings &settings, const QString &prefix)
Definition: ckbsettings.h:60
bool contains(const QString &key) const
static void cleanUp()
Definition: ckbsettings.cpp:46
Definition: keymap.h:49
QMap< QString, QVariant > writeCache
Definition: ckbsettings.h:49
bool containsGroup(const QString &group)
void beginGroup(const QString &prefix)
Definition: ckbsettings.cpp:81
QStringList childGroups() const
Definition: ckbsettings.cpp:89
QString pwd() const
Definition: ckbsettings.h:51
QStringList removeCache
Definition: ckbsettings.h:48
CkbSettings & _settings
Definition: ckbsettings.h:64
static bool isBusy()
Definition: ckbsettings.cpp:42