Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
ConfigStore.h
Go to the documentation of this file.
1#pragma once
2#include <QMap>
3#include <QObject>
4#include <QSettings>
5#include <QString>
6#include <misc/global.h>
7
8namespace sf
9{
10
15class _MISC_CLASS ConfigStore final : public QObject
16{
17 public:
21 class _MISC_CLASS Values : public QMap<QString, QString>
22 {
23 public:
24 using QMap::QMap;
25 typedef ssize_t index_type;
26 static constexpr index_type npos{-1};
27
31 index_type indexOfKey(QAnyStringView key) const;
32 };
33
39 explicit ConfigStore(QObject* parent = nullptr);
40
46 explicit ConfigStore(const QString& path, QObject* parent = nullptr);
47
51 explicit ConfigStore(const ConfigStore& cs);
52
56 explicit ConfigStore(ConfigStore&&) noexcept;
57
61 ConfigStore& operator=(ConfigStore&) noexcept;
62
66 ConfigStore& operator=(ConfigStore&&) noexcept;
67
71 ~ConfigStore() override;
72
76 ConfigStore& assign(const ConfigStore& cfg_store);
77
81 ConfigStore& assign(const QString& filepath);
82
87 QSettings& operator()();
88
93 QString getPath() const;
94
99 void setKeyPrefix(QAnyStringView prefix);
100
104 QString getKeyPrefix() const;
105
113 QString getString(QAnyStringView section, QAnyStringView key, QAnyStringView def = {}) const;
114
121 void setString(QAnyStringView section, QAnyStringView key, QAnyStringView value);
122
130 std::string getStdString(const std::string& section, const std::string& key, const std::string& def) const;
131
138 void setStdString(const std::string& section, const std::string& key, const std::string& value);
139
147 bool getBool(QAnyStringView section, QAnyStringView key, bool def) const;
148
155 void setBool(QAnyStringView section, QAnyStringView key, bool value);
156
164 double getFloat(QAnyStringView section, QAnyStringView key, double def) const;
165
172 void setFloat(QAnyStringView section, QAnyStringView key, double value);
173
181 int getInt(QAnyStringView section, QAnyStringView key, int def = 0) const;
182
189 void setInt(QAnyStringView section, QAnyStringView key, int value);
190
196 void getKeys(QAnyStringView section, QStringList& keys) const;
197
203 QStringList getKeys(QAnyStringView section) const;
204
209 void removeKeys(QAnyStringView section);
210
215 void removeSection(QAnyStringView section);
216
222 bool isSection(QAnyStringView section) const;
223
229 void removeKey(QAnyStringView section, QAnyStringView key);
230
236 void setSectionValues(QAnyStringView section, const Values& values);
237
243 void getSectionValues(QAnyStringView section, Values& values) const;
244
248 QStringList getSections() const;
249
254 bool exists();
255
260 void setSectionDefault(QAnyStringView section);
261
267 {
276 explicit Scope(ConfigStore& cs, QAnyStringView section);
277
282
286 QSettings& settings();
287
290 };
291
296 QSettings& getSettings();
297
298 private:
299 class Private;
300 Private* _p;
301};
302
303}// namespace sf
Type to create a string list accessible by keys.
Definition ConfigStore.h:22
ssize_t index_type
Definition ConfigStore.h:25
index_type indexOfKey(QAnyStringView key) const
Gets the index of the given key in the map.
Class to store application configuration. Uses the global application settings or a given ini-file.
Definition ConfigStore.h:16
int getInt(QAnyStringView section, QAnyStringView key, int def=0) const
Gets a section value from the store.
void removeKey(QAnyStringView section, QAnyStringView key)
Removes the given key from the store.
void getSectionValues(QAnyStringView section, Values &values) const
Gets the section content as mapped key-values.
QStringList getKeys(QAnyStringView section) const
Gets all keys from the given section.
void setInt(QAnyStringView section, QAnyStringView key, int value)
Sets a section value in the store.
ConfigStore(const QString &path, QObject *parent=nullptr)
Constructor creating a store from the given filepath.
bool getBool(QAnyStringView section, QAnyStringView key, bool def) const
Gets a section value from the store.
bool isSection(QAnyStringView section) const
Check if a section exists.
ConfigStore(const ConfigStore &cs)
Copy constructor.
void setSectionValues(QAnyStringView section, const Values &values)
Replaces the section content with the given key-values.
bool exists()
Checks if the path exists.
void removeSection(QAnyStringView section)
Deletes all keys including the section.
void setString(QAnyStringView section, QAnyStringView key, QAnyStringView value)
Sets a section value in the store.
ConfigStore(ConfigStore &&) noexcept
Move constructor.
void setStdString(const std::string &section, const std::string &key, const std::string &value)
Sets a section value in the store.
void getKeys(QAnyStringView section, QStringList &keys) const
Gets all keys from the given section.
double getFloat(QAnyStringView section, QAnyStringView key, double def) const
Gets a section value from the store.
std::string getStdString(const std::string &section, const std::string &key, const std::string &def) const
Gets a section value from the store.
void setFloat(QAnyStringView section, QAnyStringView key, double value)
Sets a section value in the store.
void setBool(QAnyStringView section, QAnyStringView key, bool value)
Sets a section value in the store.
void removeKeys(QAnyStringView section)
Deletes the keys but keeps the group/section.
void setSectionDefault(QAnyStringView section)
Default section when the section is empty '{}'.
ConfigStore(QObject *parent=nullptr)
Qt constructor initializing the instance using ApplicationSettings::global().
QStringList getSections() const
Gets all sections from the store in a list.
QSettings & getSettings()
#define _MISC_CLASS
Definition misc/global.h:40
Definition Application.h:10
Begins and ends a scope of settings for a section. When create it calls beginGroup(....
Definition ConfigStore.h:267
QSettings & settings()
Gets the settings instance with the section/group set.
ConfigStore & _configStore
Holds the reference to the associated ConfigStore instance.
Definition ConfigStore.h:289
Scope(ConfigStore &cs, QAnyStringView section)
Constructor passing a section of the scope.