KanoopGuiQt 1.3.0
Qt GUI utility library
Loading...
Searching...
No Matches
dialog.h
1/******************************************************************************************
2**
3** dialogbase.h
4**
5** Moved out of Tesseract and made open source
6**
7** Author: Stephen Punak
8** Created: Sun Oct 22 17:41:53 2023
9**
10******************************************************************************************/
11#ifndef DIALOG_H
12#define DIALOG_H
13#include <QDialog>
14#include <QDialogButtonBox>
15#include <QStatusBar>
16#include <QUuid>
17#include <Kanoop/utility/loggingbaseclass.h>
18#include <Kanoop/entitymetadata.h>
19#include <Kanoop/timespan.h>
20#include <Kanoop/gui/libkanoopgui.h>
21
22#define COMPARE(a, b) compare(a, b)
23
24/**
25 * @brief QDialog subclass providing logging, validation, persistence, and button-box management.
26 *
27 * Dialog wires common input widget signals to validate(), manages OK/Apply/Cancel
28 * button enabling, optionally persists window position and size, and provides a
29 * built-in QStatusBar. Subclasses must implement validate().
30 */
31class LIBKANOOPGUI_EXPORT Dialog : public QDialog,
32 public LoggingBaseClass
33{
34 Q_OBJECT
35public:
36 /** @brief Construct with an optional parent. */
37 explicit Dialog(QWidget* parent = nullptr);
38
39 /**
40 * @brief Construct with a logging category and optional parent.
41 * @param loggingCategory Category name used for log output
42 * @param parent Optional QWidget parent
43 */
44 explicit Dialog(const QString& loggingCategory, QWidget* parent = nullptr);
45
46 /** @brief Destructor — closes any open log consumer. */
47 virtual ~Dialog();
48
49 /**
50 * @brief Return whether the dialog persists its position between sessions.
51 * @return true if position persistence is enabled
52 */
53 bool persistPosition() const { return _persistPosition; }
54
55 /**
56 * @brief Enable or disable position persistence.
57 * @param value true to persist position
58 */
59 void setPersistPosition(bool value) { _persistPosition = value; }
60
61 /**
62 * @brief Return whether the dialog persists its size between sessions.
63 * @return true if size persistence is enabled
64 */
65 bool persistSize() const { return _persistSize; }
66
67 /**
68 * @brief Enable or disable size persistence.
69 * @param value true to persist size
70 */
71 void setPersistSize(bool value) { _persistSize = value; }
72
73 /**
74 * @brief Return whether to restore to the parent screen on show.
75 * @return true if restore-to-parent-screen is enabled
76 */
77 bool restoreToParentScreen() const { return _restoreToParentScreen; }
78
79 /**
80 * @brief Enable or disable restore-to-parent-screen behaviour.
81 * @param value true to restore to the parent screen
82 */
83 void setRestoreToParentScreen(bool value) { _restoreToParentScreen = value; }
84
85protected:
86 /** @brief Perform layout and button-box wiring; call from the subclass constructor. */
88
89 /**
90 * @brief Return whether the current form state is valid.
91 * @return true if validate() last set the state to valid
92 */
93 bool isValid() const { return _valid; }
94
95 /**
96 * @brief Set the form validity state and update button enabling accordingly.
97 * @param value true if the form is valid
98 */
99 void setValid(bool value);
100
101 /**
102 * @brief Return whether the form has unsaved changes.
103 * @return true if the form is dirty
104 */
105 bool isDirty() const { return _dirty; }
106
107 /**
108 * @brief Set the dirty flag.
109 * @param value true if the form has unsaved changes
110 */
111 void setDirty(bool value);
112
113 /**
114 * @brief Return whether the form failed to load.
115 * @return true if form load failed
116 */
117 bool formLoadFailed() const { return _formLoadFailed; }
118
119 /**
120 * @brief Set the form load failure flag.
121 * @param value true if form load failed
122 */
123 void setFormLoadFailed(bool value) { _formLoadFailed = value; }
124
125 /**
126 * @brief Return whether the form has finished loading.
127 * @return true if form load is complete
128 */
129 bool formLoadComplete() const { return _formLoadComplete; }
130
131 /**
132 * @brief Enable or disable the Apply button.
133 * @param value true to enable
134 */
135 void setApplyEnabled(bool value);
136
137 /**
138 * @brief Enable or disable the Cancel button.
139 * @param value true to enable
140 */
141 void setCancelEnabled(bool value);
142
143 /**
144 * @brief Enable or disable the OK button.
145 * @param value true to enable
146 */
147 void setOkEnabled(bool value);
148
149 /**
150 * @brief Show or hide the status bar.
151 * @param value true to show
152 */
153 void setStatusBarVisible(bool value);
154
155 /**
156 * @brief Enable or disable the log consumer hook for this dialog.
157 * @param enabled true to enable
158 */
159 void setLogHookEnabled(bool enabled);
160
161 /** @brief Connect all child input widget signals to the validation slots. */
163
164 /**
165 * @brief Enable or disable all input child widgets of a given widget.
166 * @param widget Parent widget whose children to enable/disable
167 * @param enabled true to enable
168 */
169 void setChildInputWidgetsEnabled(QWidget* widget, bool enabled);
170
171 /**
172 * @brief Return the default text color from the current palette.
173 * @return Default text QColor
174 */
175 QColor defaultTextColor() const { return palette().color(QPalette::Text); }
176
177 /**
178 * @brief Return the default dialog size.
179 * @return Default QSize
180 */
181 QSize defaultSize() const { return _defaultSize; }
182
183 /**
184 * @brief Set the default dialog size.
185 * @param value Default QSize
186 */
187 void setDefaultSize(const QSize& value) { _defaultSize = value; }
188
189 /**
190 * @brief Set the default dialog size by width and height.
191 * @param width Default width in pixels
192 * @param height Default height in pixels
193 */
194 void setDefaultSize(int width, int height) { _defaultSize = QSize(width, height); }
195
196 /**
197 * @brief Return the dialog button box.
198 * @return Pointer to the QDialogButtonBox
199 */
200 QDialogButtonBox* buttonBox() const { return _buttonBox; }
201
202 /**
203 * @brief Return the dialog status bar.
204 * @return Pointer to the QStatusBar
205 */
206 QStatusBar* statusBar() const { return _statusBar; }
207
208 /** @brief Implement to validate the form and call setValid()/setDirty(). */
209 virtual void validate() = 0;
210
211 /**
212 * @brief Called when a log entry is delivered (no-op by default).
213 * @param entry The log entry
214 */
215 virtual void loggedItem(const Log::LogEntry& entry) { Q_UNUSED(entry) }
216
217 /** @brief Return true if a != b (used with COMPARE macro). */
218 bool compare(const QString& a, const QString& b) { return a != b; }
219 /** @brief Return true if a != b (used with COMPARE macro). */
220 bool compare(const QUuid& a, const QUuid& b) { return a != b; }
221 /** @brief Return true if a != b (used with COMPARE macro). */
222 bool compare(const QList<QUuid>& a, const QList<QUuid>& b) { return a != b; }
223 /** @brief Return true if a != b (used with COMPARE macro). */
224 bool compare(const int a, int b) { return a != b; }
225 /** @brief Return true if a != b (used with COMPARE macro). */
226 bool compare(const TimeSpan& a, const TimeSpan& b) { return a != b; }
227
228 /** @brief Called when the OK button is clicked (no-op by default). */
229 virtual void okClicked() {}
230 /** @brief Called when the Apply button is clicked (no-op by default). */
231 virtual void applyClicked() {}
232 /** @brief Called when the Cancel button is clicked (no-op by default). */
233 virtual void cancelClicked() {}
234
235protected slots:
236 /** @brief Update button enabling based on current validity and dirty state. */
238
239private:
240 /** @brief Shared constructor initialization. */
241 void commonInit();
242
243protected:
244 /** @brief Persist position on move. */
245 virtual void moveEvent(QMoveEvent *event) override;
246 /** @brief Persist size on resize. */
247 virtual void resizeEvent(QResizeEvent *event) override;
248 /** @brief Restore geometry and complete form load on first show. */
249 virtual void showEvent(QShowEvent *event) override;
250
251private:
252 /** @brief Connect QLineEdit signals. */
253 void connectLineEditSignals();
254 /** @brief Connect QTextEdit signals. */
255 void connectTextEditSignals();
256 /** @brief Connect QComboBox signals. */
257 void connectComboBoxSignals();
258 /** @brief Connect QRadioButton signals. */
259 void connectRadioButtonSignals();
260 /** @brief Connect QCheckBox signals. */
261 void connectCheckBoxSignals();
262 /** @brief Connect QSpinBox signals. */
263 void connectSpinBoxSignals();
264 /** @brief Connect button-box button signals. */
265 void connectButtonBoxSignals();
266 /** @brief Create and configure the button box. */
267 void setButtonBoxButtons();
268
269 /** @brief Close and destroy the log consumer. */
270 void closeLogConsumer();
271
272 template <typename T>
273 void appendToList(QList<QWidget*>& list, const QList<T>& typedList) const
274 {
275 for(T widget : typedList) {
276 QWidget* w = qobject_cast<QWidget*>(widget);
277 if(w != nullptr) {
278 list.append(w);
279 }
280 }
281 }
282
283 QDialogButtonBox* _buttonBox = nullptr;
284 QStatusBar* _statusBar = nullptr;
285 bool _formLoadComplete = false;
286 bool _formLoadFailed = false;
287 bool _valid = false;
288 bool _dirty = false;
289
290 bool _applyEnabled = true;
291 bool _cancelEnabled = true;
292 bool _okEnabled = true;
293
294 QSize _defaultSize;
295
296 bool _persistPosition = true;
297 bool _persistSize = true;
298 bool _restoreToParentScreen = true;
299
300 LogConsumer* _logConsumer = nullptr;
301
302signals:
303 /** @brief Emitted after an item is added via this dialog. */
304 void itemAdded(const EntityMetadata& metadata);
305 /** @brief Emitted after an item is deleted via this dialog. */
306 void itemDeleted(const EntityMetadata& metadata);
307 /** @brief Emitted after an item is updated via this dialog. */
308 void itemUpdated(const EntityMetadata& metadata);
309
310public slots:
311 /** @brief Called when application preferences change; override to react. */
312 virtual void onPreferencesChanged();
313
314protected slots:
315 /** @brief Trigger validation when a string input changes. */
316 virtual void stringChanged(const QString&);
317 /** @brief Trigger validation when an integer input changes. */
318 virtual void intChanged(int);
319 /** @brief Trigger validation when a boolean input changes. */
320 virtual void boolChanged(bool);
321 /** @brief Trigger validation when a parameterless signal fires. */
322 virtual void voidChanged();
323
324private slots:
325 /** @brief Forward logged entries to loggedItem(). */
326 void onLoggedItem(const Log::LogEntry& entry);
327 /** @brief Persist splitter state when moved. */
328 void onSplitterMoved();
329 /** @brief Handle OK button click. */
330 void onOkClicked();
331 /** @brief Handle Apply button click. */
332 void onApplyClicked();
333 /** @brief Handle Cancel button click. */
334 void onCancelClicked();
335};
336
337#endif // DIALOG_H
QDialog subclass providing logging, validation, persistence, and button-box management.
Definition dialog.h:33
void setPersistSize(bool value)
Enable or disable size persistence.
Definition dialog.h:71
void setStatusBarVisible(bool value)
Show or hide the status bar.
virtual void onPreferencesChanged()
Called when application preferences change; override to react.
bool isValid() const
Return whether the current form state is valid.
Definition dialog.h:93
void performLayout()
Perform layout and button-box wiring; call from the subclass constructor.
virtual void boolChanged(bool)
Trigger validation when a boolean input changes.
void setApplyEnabled(bool value)
Enable or disable the Apply button.
Dialog(const QString &loggingCategory, QWidget *parent=nullptr)
Construct with a logging category and optional parent.
void setDefaultSize(int width, int height)
Set the default dialog size by width and height.
Definition dialog.h:194
QDialogButtonBox * buttonBox() const
Return the dialog button box.
Definition dialog.h:200
bool restoreToParentScreen() const
Return whether to restore to the parent screen on show.
Definition dialog.h:77
virtual void enableAppropriateButtons()
Update button enabling based on current validity and dirty state.
void setFormLoadFailed(bool value)
Set the form load failure flag.
Definition dialog.h:123
bool compare(const QList< QUuid > &a, const QList< QUuid > &b)
Return true if a != b (used with COMPARE macro).
Definition dialog.h:222
QColor defaultTextColor() const
Return the default text color from the current palette.
Definition dialog.h:175
void setDefaultSize(const QSize &value)
Set the default dialog size.
Definition dialog.h:187
QStatusBar * statusBar() const
Return the dialog status bar.
Definition dialog.h:206
virtual void stringChanged(const QString &)
Trigger validation when a string input changes.
bool persistSize() const
Return whether the dialog persists its size between sessions.
Definition dialog.h:65
void itemAdded(const EntityMetadata &metadata)
Emitted after an item is added via this dialog.
virtual void validate()=0
Implement to validate the form and call setValid()/setDirty().
bool formLoadComplete() const
Return whether the form has finished loading.
Definition dialog.h:129
bool compare(const int a, int b)
Return true if a != b (used with COMPARE macro).
Definition dialog.h:224
void setOkEnabled(bool value)
Enable or disable the OK button.
void connectValidationSignals()
Connect all child input widget signals to the validation slots.
bool isDirty() const
Return whether the form has unsaved changes.
Definition dialog.h:105
void itemUpdated(const EntityMetadata &metadata)
Emitted after an item is updated via this dialog.
bool compare(const TimeSpan &a, const TimeSpan &b)
Return true if a != b (used with COMPARE macro).
Definition dialog.h:226
QSize defaultSize() const
Return the default dialog size.
Definition dialog.h:181
bool formLoadFailed() const
Return whether the form failed to load.
Definition dialog.h:117
void setPersistPosition(bool value)
Enable or disable position persistence.
Definition dialog.h:59
virtual void cancelClicked()
Called when the Cancel button is clicked (no-op by default).
Definition dialog.h:233
virtual void moveEvent(QMoveEvent *event) override
Persist position on move.
void setCancelEnabled(bool value)
Enable or disable the Cancel button.
virtual void resizeEvent(QResizeEvent *event) override
Persist size on resize.
bool compare(const QString &a, const QString &b)
Return true if a != b (used with COMPARE macro).
Definition dialog.h:218
Dialog(QWidget *parent=nullptr)
Construct with an optional parent.
virtual void applyClicked()
Called when the Apply button is clicked (no-op by default).
Definition dialog.h:231
void setDirty(bool value)
Set the dirty flag.
bool persistPosition() const
Return whether the dialog persists its position between sessions.
Definition dialog.h:53
void setValid(bool value)
Set the form validity state and update button enabling accordingly.
void setRestoreToParentScreen(bool value)
Enable or disable restore-to-parent-screen behaviour.
Definition dialog.h:83
virtual void okClicked()
Called when the OK button is clicked (no-op by default).
Definition dialog.h:229
void setLogHookEnabled(bool enabled)
Enable or disable the log consumer hook for this dialog.
virtual void voidChanged()
Trigger validation when a parameterless signal fires.
virtual ~Dialog()
Destructor — closes any open log consumer.
bool compare(const QUuid &a, const QUuid &b)
Return true if a != b (used with COMPARE macro).
Definition dialog.h:220
virtual void loggedItem(const Log::LogEntry &entry)
Called when a log entry is delivered (no-op by default).
Definition dialog.h:215
virtual void intChanged(int)
Trigger validation when an integer input changes.
virtual void showEvent(QShowEvent *event) override
Restore geometry and complete form load on first show.
void setChildInputWidgetsEnabled(QWidget *widget, bool enabled)
Enable or disable all input child widgets of a given widget.
void itemDeleted(const EntityMetadata &metadata)
Emitted after an item is deleted via this dialog.