KanoopGuiQt 1.3.0
Qt GUI utility library
Loading...
Searching...
No Matches
guisettings.h
1/******************************************************************************************
2**
3** guisettings.h
4**
5** Moved from my Tesseract Engineering repo to open-source
6**
7** Author: Stephen Punak
8** Created: Fri Oct 27 09:12:34 2023
9**
10******************************************************************************************/
11#ifndef GUISETTINGS_H
12#define GUISETTINGS_H
13#include <QObject>
14#include <QSettings>
15#include <QWidget>
16#include <Kanoop/gui/libkanoopgui.h>
17
18class QHeaderView;
19class QSplitter;
21class TreeViewBase;
22
23/**
24 * @brief Persistent GUI settings storage for widget geometry, header states, and directories.
25 *
26 * GuiSettings wraps QSettings to provide typed, named accessors for common GUI state
27 * such as window positions, sizes, splitter states, header view states, and last-used
28 * directories. Subclass to add application-specific settings.
29 *
30 * A process-wide singleton can be registered with setGlobalInstance() and retrieved
31 * with globalInstance().
32 */
33class LIBKANOOPGUI_EXPORT GuiSettings : public QObject
34{
35 Q_OBJECT
36
37public:
38 /** @brief Construct a GuiSettings object backed by the default QSettings store. */
40
41 // Widget settings
42 /**
43 * @brief Persist the last position of a widget.
44 * @param widget Widget whose position to save (key is based on objectName)
45 * @param pos Position to save
46 */
47 void setLastWindowPosition(const QWidget* widget, const QPoint& pos) { _settings.setValue(makeKey(KEY_LAST_WIDGET_POS, widget->objectName()), pos); }
48
49 /**
50 * @brief Retrieve the last saved position of a widget.
51 * @param widget Widget to look up
52 * @param defaultSize Default size used to compute a centred position if no position is saved
53 * @return Saved position, or a sensible default
54 */
55 QPoint getLastWindowPosition(QWidget* widget, const QSize& defaultSize = QSize()) const;
56
57 /**
58 * @brief Persist the last size of a widget.
59 * @param widget Widget whose size to save
60 * @param size Size to save
61 */
62 void setLastWindowSize(const QWidget* widget, const QSize& size) { _settings.setValue(makeKey(KEY_LAST_WIDGET_SIZE, widget->objectName()), size); }
63
64 /**
65 * @brief Retrieve the last saved size of a widget.
66 * @param widget Widget to look up
67 * @param defaultSize Size to return if nothing is saved
68 * @return Saved size, or defaultSize
69 */
70 QSize getLastWindowSize(const QWidget* widget, const QSize& defaultSize = QSize());
71
72 /**
73 * @brief Return whether any geometry has been saved for a widget.
74 * @param widget Widget to check
75 * @return true if a saved position or size exists
76 */
77 bool widgetHasPersistentGeometry(const QWidget* widget) const;
78
79 /**
80 * @brief Save the current state of a QSplitter.
81 * @param splitter Splitter whose state to save
82 */
83 void saveLastSplitterState(QSplitter* splitter);
84
85 /**
86 * @brief Restore a previously saved QSplitter state.
87 * @param splitter Splitter to restore
88 */
89 void restoreLastSplitterState(QSplitter* splitter);
90
91 /**
92 * @brief Save the current state of a QHeaderView.
93 * @param header Header view whose state to save
94 */
95 void saveLastHeaderState(QHeaderView* header);
96
97 /**
98 * @brief Restore a previously saved QHeaderView state.
99 * @param header Header view to restore
100 */
101 void restoreLastHeaderState(QHeaderView* header);
102
103 /**
104 * @brief Save header state using the model's column definitions as keys.
105 * @param header Header view whose state to save
106 * @param model Model providing column metadata for keying
107 */
108 void saveLastHeaderState(QHeaderView* header, AbstractItemModel* model);
109
110 /**
111 * @brief Restore header state using the model's column definitions as keys.
112 * @param header Header view to restore
113 * @param model Model providing column metadata for keying
114 */
115 void restoreLastHeaderState(QHeaderView* header, AbstractItemModel* model);
116
117 /**
118 * @brief Save the expansion/selection state of a TreeViewBase.
119 * @param treeView Tree view whose state to save
120 */
122
123 /**
124 * @brief Restore the expansion/selection state of a TreeViewBase.
125 * @param treeView Tree view to restore
126 */
128
129 /**
130 * @brief Persist an arbitrary string value.
131 * @param key Settings key
132 * @param value String to save
133 */
134 void setStringValue(const QString& key, const QString& value) { _settings.setValue(key, value); }
135
136 /**
137 * @brief Retrieve an arbitrary string value.
138 * @param key Settings key
139 * @return Saved string, or empty string if not found
140 */
141 QString getStringValue(const QString& key) const { return _settings.value(key).toString(); }
142
143 /**
144 * @brief Persist an arbitrary QVariant value.
145 * @param key Settings key
146 * @param value Variant to save
147 */
148 void setVariantValue(const QString& key, const QVariant& value) { _settings.setValue(key, value); }
149
150 /**
151 * @brief Retrieve an arbitrary QVariant value.
152 * @param key Settings key
153 * @return Saved variant, or invalid QVariant if not found
154 */
155 QVariant getVariantValue(const QString& key) const { return _settings.value(key); }
156
157 /**
158 * @brief Persist an arbitrary byte array value.
159 * @param key Settings key
160 * @param value Byte array to save
161 */
162 void setByteArrayValue(const QString& key, const QByteArray& value) { _settings.setValue(key, value); }
163
164 /**
165 * @brief Retrieve an arbitrary byte array value.
166 * @param key Settings key
167 * @return Saved byte array, or empty array if not found
168 */
169 QByteArray getByteArrayValue(const QString& key) const { return _settings.value(key).toByteArray(); }
170
171 /**
172 * @brief Return the maximum number of recent files to track.
173 * @return Maximum recent file count
174 */
175 int maxRecentFiles() const { return _maxRecentFiles; }
176
177 /**
178 * @brief Set the maximum number of recent files to track.
179 * @param value Maximum recent file count
180 */
181 void setMaxRecentFiles(int value) { _maxRecentFiles = value; }
182
183 /**
184 * @brief Return the last directory used for a given file extension.
185 * @param extension File extension (without leading dot)
186 * @return Last directory path string
187 */
188 QString lastDirectory(const QString& extension) const { return _settings.value(makeFileTypeKey(KEY_LAST_DIRECTORY, extension)).toString(); }
189
190 /**
191 * @brief Return the last directory used for a given file-type integer.
192 * @param fileType Application-defined file type integer
193 * @return Last directory path string
194 */
195 QString lastDirectory(int fileType) const { return _settings.value(makeFileTypeKey(KEY_LAST_DIRECTORY, fileType)).toString(); }
196
197 /**
198 * @brief Save the last directory used for a given file extension.
199 * @param extension File extension (without leading dot)
200 * @param value Directory path to save
201 */
202 virtual void saveLastDirectory(const QString& extension, const QString& value) { _settings.setValue(makeFileTypeKey(KEY_LAST_DIRECTORY, extension), value); }
203
204 /**
205 * @brief Save the last directory used for a given file-type integer.
206 * @param fileType Application-defined file type integer
207 * @param value Directory path to save
208 */
209 virtual void saveLastDirectory(int fileType, const QString& value) { _settings.setValue(makeFileTypeKey(KEY_LAST_DIRECTORY, fileType), value); }
210
211 /**
212 * @brief Synchronize settings file to disk and reload any changed values
213 */
214 void sync();
215
216 /**
217 * @brief Return the persisted font size.
218 * @return Font point size, or 0 if not set
219 */
220 int fontSize() const { return _settings.value(makeStandardKey(KEY_FONT_SIZE)).toInt(); }
221
222 /**
223 * @brief Persist the font size.
224 * @param value Font point size
225 */
226 void setFontSize(int value) { _settings.setValue(makeStandardKey(KEY_FONT_SIZE), value); }
227
228 /**
229 * @brief Return the process-wide GuiSettings singleton.
230 * @return Global GuiSettings instance, or nullptr if not set
231 */
233
234 /**
235 * @brief Set the process-wide GuiSettings singleton.
236 * @param value Instance to register as global
237 */
238 static void setGlobalInstance(GuiSettings* value) { _globalInstance = value; }
239
240signals:
241 /** @brief Emitted when any setting value changes. */
243
244protected:
245 /** @brief Build a settings key prefixed with the application key. */
246 static QString makeStandardKey(const QString& key) { return QString("%1/%2").arg(KEY_APP).arg(key); }
247 /** @brief Build a compound settings key from two parts. */
248 static QString makeKey(const QString& key, const QString& subKey) { return QString("%1/%2").arg(key).arg(subKey); }
249 /** @brief Build a settings key based on a QObject's identity. */
250 static QString makeObjectKey(const QObject* object);
251 /** @brief Build a settings key based on a file extension. */
252 static QString makeFileTypeKey(const QString& key, const QString& extension);
253 /** @brief Build a settings key based on a file-type integer. */
254 static QString makeFileTypeKey(const QString& key, int fileType);
255 /** @brief Build a compound settings key from a base key and a QObject. */
256 static QString makeCompoundObjectKey(const QString& key, const QObject* object);
257
258 /** @brief Override to ensure sane default values on first run. */
259 virtual void ensureValidDefaults();
260
261 /** @brief Underlying QSettings storage. */
262 QSettings _settings;
263
264 /** @brief Settings key for the application group. */
265 static const QString KEY_APP;
266 /** @brief Settings key for the saved font size. */
267 static const QString KEY_FONT_SIZE;
268 /** @brief Settings key for the horizontal header state. */
269 static const QString KEY_HEADER_STATE_HORIZ;
270 /** @brief Settings key for the vertical header state. */
271 static const QString KEY_HEADER_STATE_VERT;
272 /** @brief Settings key for the last-used directory. */
273 static const QString KEY_LAST_DIRECTORY;
274 /** @brief Settings key for the last widget position. */
275 static const QString KEY_LAST_WIDGET_POS;
276 /** @brief Settings key for the last widget size. */
277 static const QString KEY_LAST_WIDGET_SIZE;
278 /** @brief Settings key for the horizontal model header state. */
279 static const QString KEY_MODEL_HEADER_STATE_HORIZ;
280 /** @brief Settings key for the vertical model header state. */
281 static const QString KEY_MODEL_HEADER_STATE_VERT;
282 /** @brief Settings key for the horizontal splitter state. */
283 static const QString KEY_SPLITTER_STATE_HORIZ;
284 /** @brief Settings key for the vertical splitter state. */
285 static const QString KEY_SPLITTER_STATE_VERT;
286 /** @brief Settings key for the tree view expansion state. */
287 static const QString KEY_TREEVIEW_STATE;
288
289private:
290 /** @brief Convert a list of UUIDs to a list of strings for persistence. */
291 static QStringList uuidListToStringList(const QList<QUuid>& uuids);
292 /** @brief Convert a list of strings back to UUIDs. */
293 static QList<QUuid> stringListToUuidList(const QStringList& values);
294
295 int _maxRecentFiles;
296
297 static GuiSettings* _globalInstance;
298};
299
300#endif // GUISETTINGS_H
Extended QAbstractItemModel providing EntityMetadata-based item lookup and header management.
Persistent GUI settings storage for widget geometry, header states, and directories.
Definition guisettings.h:34
static void setGlobalInstance(GuiSettings *value)
Set the process-wide GuiSettings singleton.
QSize getLastWindowSize(const QWidget *widget, const QSize &defaultSize=QSize())
Retrieve the last saved size of a widget.
void setStringValue(const QString &key, const QString &value)
Persist an arbitrary string value.
static const QString KEY_HEADER_STATE_HORIZ
Settings key for the horizontal header state.
static QString makeFileTypeKey(const QString &key, const QString &extension)
Build a settings key based on a file extension.
static QString makeObjectKey(const QObject *object)
Build a settings key based on a QObject's identity.
QByteArray getByteArrayValue(const QString &key) const
Retrieve an arbitrary byte array value.
static const QString KEY_SPLITTER_STATE_HORIZ
Settings key for the horizontal splitter state.
QSettings _settings
Underlying QSettings storage.
void setMaxRecentFiles(int value)
Set the maximum number of recent files to track.
virtual void saveLastDirectory(const QString &extension, const QString &value)
Save the last directory used for a given file extension.
void saveLastHeaderState(QHeaderView *header)
Save the current state of a QHeaderView.
virtual void saveLastDirectory(int fileType, const QString &value)
Save the last directory used for a given file-type integer.
static const QString KEY_LAST_WIDGET_SIZE
Settings key for the last widget size.
static QString makeFileTypeKey(const QString &key, int fileType)
Build a settings key based on a file-type integer.
QString getStringValue(const QString &key) const
Retrieve an arbitrary string value.
void saveTreeViewState(TreeViewBase *treeView)
Save the expansion/selection state of a TreeViewBase.
bool widgetHasPersistentGeometry(const QWidget *widget) const
Return whether any geometry has been saved for a widget.
void restoreLastHeaderState(QHeaderView *header)
Restore a previously saved QHeaderView state.
void restoreLastSplitterState(QSplitter *splitter)
Restore a previously saved QSplitter state.
void restoreTreeViewState(TreeViewBase *treeView)
Restore the expansion/selection state of a TreeViewBase.
static const QString KEY_LAST_WIDGET_POS
Settings key for the last widget position.
void restoreLastHeaderState(QHeaderView *header, AbstractItemModel *model)
Restore header state using the model's column definitions as keys.
static const QString KEY_LAST_DIRECTORY
Settings key for the last-used directory.
int maxRecentFiles() const
Return the maximum number of recent files to track.
static GuiSettings * globalInstance()
Return the process-wide GuiSettings singleton.
int fontSize() const
Return the persisted font size.
QVariant getVariantValue(const QString &key) const
Retrieve an arbitrary QVariant value.
void setVariantValue(const QString &key, const QVariant &value)
Persist an arbitrary QVariant value.
static QString makeCompoundObjectKey(const QString &key, const QObject *object)
Build a compound settings key from a base key and a QObject.
static QString makeStandardKey(const QString &key)
Build a settings key prefixed with the application key.
static const QString KEY_MODEL_HEADER_STATE_VERT
Settings key for the vertical model header state.
virtual void ensureValidDefaults()
Override to ensure sane default values on first run.
static const QString KEY_APP
Settings key for the application group.
void saveLastSplitterState(QSplitter *splitter)
Save the current state of a QSplitter.
void sync()
Synchronize settings file to disk and reload any changed values.
static const QString KEY_SPLITTER_STATE_VERT
Settings key for the vertical splitter state.
static const QString KEY_FONT_SIZE
Settings key for the saved font size.
void saveLastHeaderState(QHeaderView *header, AbstractItemModel *model)
Save header state using the model's column definitions as keys.
QString lastDirectory(const QString &extension) const
Return the last directory used for a given file extension.
QPoint getLastWindowPosition(QWidget *widget, const QSize &defaultSize=QSize()) const
Retrieve the last saved position of a widget.
void setByteArrayValue(const QString &key, const QByteArray &value)
Persist an arbitrary byte array value.
void setLastWindowSize(const QWidget *widget, const QSize &size)
Persist the last size of a widget.
Definition guisettings.h:62
void setFontSize(int value)
Persist the font size.
static const QString KEY_HEADER_STATE_VERT
Settings key for the vertical header state.
GuiSettings()
Construct a GuiSettings object backed by the default QSettings store.
QString lastDirectory(int fileType) const
Return the last directory used for a given file-type integer.
static const QString KEY_MODEL_HEADER_STATE_HORIZ
Settings key for the horizontal model header state.
void settingsChanged()
Emitted when any setting value changes.
static const QString KEY_TREEVIEW_STATE
Settings key for the tree view expansion state.
void setLastWindowPosition(const QWidget *widget, const QPoint &pos)
Persist the last position of a widget.
Definition guisettings.h:47
static QString makeKey(const QString &key, const QString &subKey)
Build a compound settings key from two parts.
QTreeView subclass integrating AbstractItemModel with rich navigation helpers.