KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
appsettings.h
1/******************************************************************************************
2**
3** appsettings.h
4**
5** Application-wide settings storage for non-GUI and GUI applications.
6** Parent class of GuiSettings (KanoopGuiQt).
7**
8** Author: Stephen Punak
9** Created: Sun Apr 26 2026
10**
11******************************************************************************************/
12#ifndef APPSETTINGS_H
13#define APPSETTINGS_H
14#include <QObject>
15#include <QSettings>
16#include <QUuid>
17#include <Kanoop/kanoopcommon.h>
18
19/**
20 * @brief Persistent application settings storage backed by QSettings.
21 *
22 * AppSettings provides typed, named accessors for arbitrary key/value persistence
23 * and last-used-directory tracking. It has no Qt GUI dependencies and is suitable
24 * as a base class for non-GUI applications. Subclass to add application-specific
25 * settings; for GUI applications, subclass GuiSettings (in KanoopGuiQt) instead.
26 *
27 * A process-wide singleton can be registered with setGlobalInstance() and retrieved
28 * with globalInstance().
29 */
30class KANOOP_EXPORT AppSettings : public QObject
31{
32 Q_OBJECT
33
34public:
35 /** @brief Construct an AppSettings object backed by the default QSettings store. */
37
38 /**
39 * @brief Persist an arbitrary string value.
40 * @param key Settings key
41 * @param value String to save
42 */
43 void setStringValue(const QString& key, const QString& value) { _settings.setValue(key, value); }
44
45 /**
46 * @brief Retrieve an arbitrary string value.
47 * @param key Settings key
48 * @return Saved string, or empty string if not found
49 */
50 QString getStringValue(const QString& key) const { return _settings.value(key).toString(); }
51
52 /**
53 * @brief Persist an arbitrary QVariant value.
54 * @param key Settings key
55 * @param value Variant to save
56 */
57 void setVariantValue(const QString& key, const QVariant& value) { _settings.setValue(key, value); }
58
59 /**
60 * @brief Retrieve an arbitrary QVariant value.
61 * @param key Settings key
62 * @return Saved variant, or invalid QVariant if not found
63 */
64 QVariant getVariantValue(const QString& key) const { return _settings.value(key); }
65
66 /**
67 * @brief Persist an arbitrary byte array value.
68 * @param key Settings key
69 * @param value Byte array to save
70 */
71 void setByteArrayValue(const QString& key, const QByteArray& value) { _settings.setValue(key, value); }
72
73 /**
74 * @brief Retrieve an arbitrary byte array value.
75 * @param key Settings key
76 * @return Saved byte array, or empty array if not found
77 */
78 QByteArray getByteArrayValue(const QString& key) const { return _settings.value(key).toByteArray(); }
79
80 /**
81 * @brief Return the maximum number of recent files to track.
82 * @return Maximum recent file count
83 */
84 int maxRecentFiles() const { return _maxRecentFiles; }
85
86 /**
87 * @brief Set the maximum number of recent files to track.
88 * @param value Maximum recent file count
89 */
90 void setMaxRecentFiles(int value) { _maxRecentFiles = value; }
91
92 /**
93 * @brief Return the last directory used for a given file extension.
94 * @param extension File extension (without leading dot)
95 * @return Last directory path string
96 */
97 QString lastDirectory(const QString& extension) const { return _settings.value(makeFileTypeKey(KEY_LAST_DIRECTORY, extension)).toString(); }
98
99 /**
100 * @brief Return the last directory used for a given file-type integer.
101 * @param fileType Application-defined file type integer
102 * @return Last directory path string
103 */
104 QString lastDirectory(int fileType) const { return _settings.value(makeFileTypeKey(KEY_LAST_DIRECTORY, fileType)).toString(); }
105
106 /**
107 * @brief Save the last directory used for a given file extension.
108 * @param extension File extension (without leading dot)
109 * @param value Directory path to save
110 */
111 virtual void saveLastDirectory(const QString& extension, const QString& value) { _settings.setValue(makeFileTypeKey(KEY_LAST_DIRECTORY, extension), value); }
112
113 /**
114 * @brief Save the last directory used for a given file-type integer.
115 * @param fileType Application-defined file type integer
116 * @param value Directory path to save
117 */
118 virtual void saveLastDirectory(int fileType, const QString& value) { _settings.setValue(makeFileTypeKey(KEY_LAST_DIRECTORY, fileType), value); }
119
120 /**
121 * @brief Synchronize settings file to disk and reload any changed values.
122 */
123 void sync();
124
125 /**
126 * @brief Return the process-wide AppSettings singleton.
127 * @return Global instance, or nullptr if not set.
128 */
129 static AppSettings* globalInstance() { return _globalInstance; }
130
131 /**
132 * @brief Set the process-wide AppSettings singleton.
133 * @param value Instance to register as global
134 */
135 static void setGlobalInstance(AppSettings* value) { _globalInstance = value; }
136
137signals:
138 /** @brief Emitted when any setting value changes. */
140
141protected:
142 /** @brief Build a settings key prefixed with the application key. */
143 static QString makeStandardKey(const QString& key) { return QString("%1/%2").arg(KEY_APP).arg(key); }
144 /** @brief Build a compound settings key from two parts. */
145 static QString makeKey(const QString& key, const QString& subKey) { return QString("%1/%2").arg(key).arg(subKey); }
146 /** @brief Build a settings key based on a QObject's identity. */
147 static QString makeObjectKey(const QObject* object);
148 /** @brief Build a settings key based on a file extension. */
149 static QString makeFileTypeKey(const QString& key, const QString& extension);
150 /** @brief Build a settings key based on a file-type integer. */
151 static QString makeFileTypeKey(const QString& key, int fileType);
152 /** @brief Build a compound settings key from a base key and a QObject. */
153 static QString makeCompoundObjectKey(const QString& key, const QObject* object);
154
155 /** @brief Override to ensure sane default values on first run. */
156 virtual void ensureValidDefaults() {}
157
158 /** @brief Underlying QSettings storage. */
159 QSettings _settings;
160
161 /** @brief Settings key for the application group. */
162 static const QString KEY_APP;
163 /** @brief Settings key for the last-used directory. */
164 static const QString KEY_LAST_DIRECTORY;
165
166private:
167 int _maxRecentFiles;
168
169 static AppSettings* _globalInstance;
170};
171
172#endif // APPSETTINGS_H
Persistent application settings storage backed by QSettings.
Definition appsettings.h:31
static QString makeStandardKey(const QString &key)
Build a settings key prefixed with the application key.
void settingsChanged()
Emitted when any setting value changes.
QString getStringValue(const QString &key) const
Retrieve an arbitrary string value.
Definition appsettings.h:50
static const QString KEY_LAST_DIRECTORY
Settings key for the last-used directory.
static AppSettings * globalInstance()
Return the process-wide AppSettings singleton.
QVariant getVariantValue(const QString &key) const
Retrieve an arbitrary QVariant value.
Definition appsettings.h:64
void setByteArrayValue(const QString &key, const QByteArray &value)
Persist an arbitrary byte array value.
Definition appsettings.h:71
QByteArray getByteArrayValue(const QString &key) const
Retrieve an arbitrary byte array value.
Definition appsettings.h:78
virtual void saveLastDirectory(const QString &extension, const QString &value)
Save the last directory used for a given file extension.
static QString makeFileTypeKey(const QString &key, int fileType)
Build a settings key based on a file-type integer.
static const QString KEY_APP
Settings key for the application group.
QSettings _settings
Underlying QSettings storage.
int maxRecentFiles() const
Return the maximum number of recent files to track.
Definition appsettings.h:84
static void setGlobalInstance(AppSettings *value)
Set the process-wide AppSettings singleton.
static QString makeKey(const QString &key, const QString &subKey)
Build a compound settings key from two parts.
virtual void saveLastDirectory(int fileType, const QString &value)
Save the last directory used for a given file-type integer.
QString lastDirectory(const QString &extension) const
Return the last directory used for a given file extension.
Definition appsettings.h:97
static QString makeObjectKey(const QObject *object)
Build a settings key based on a QObject's identity.
static QString makeFileTypeKey(const QString &key, const QString &extension)
Build a settings key based on a file extension.
void sync()
Synchronize settings file to disk and reload any changed values.
void setMaxRecentFiles(int value)
Set the maximum number of recent files to track.
Definition appsettings.h:90
AppSettings()
Construct an AppSettings object backed by the default QSettings store.
QString lastDirectory(int fileType) const
Return the last directory used for a given file-type integer.
void setStringValue(const QString &key, const QString &value)
Persist an arbitrary string value.
Definition appsettings.h:43
void setVariantValue(const QString &key, const QVariant &value)
Persist an arbitrary QVariant value.
Definition appsettings.h:57
static QString makeCompoundObjectKey(const QString &key, const QObject *object)
Build a compound settings key from a base key and a QObject.
virtual void ensureValidDefaults()
Override to ensure sane default values on first run.