Qt Utilities 6.14.4
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
Loading...
Searching...
No Matches
optionpage.h
Go to the documentation of this file.
1#ifndef DIALOGS_OPTIONSPAGE_H
2#define DIALOGS_OPTIONSPAGE_H
3
4#include "../global.h"
5
6#include <QObject>
7#include <QWidget>
8
9#include <memory>
10
11namespace QtUtilities {
12
13class SettingsDialog;
14
15class QT_UTILITIES_EXPORT OptionPageWidget : public QWidget {
16 Q_OBJECT
17
18public:
19 explicit OptionPageWidget(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
20
21Q_SIGNALS:
24
25protected:
26 bool event(QEvent *) override;
27};
28
29inline OptionPageWidget::OptionPageWidget(QWidget *parent, Qt::WindowFlags f)
30 : QWidget(parent, f)
31{
32}
33
35 friend class SettingsDialog;
36
37public:
38 explicit OptionPage(QWidget *parentWindow = nullptr);
39 virtual ~OptionPage();
40
41 QWidget *parentWindow() const;
42 QWidget *widget();
43 bool hasBeenShown() const;
44 virtual bool apply() = 0;
45 virtual void reset() = 0;
46 bool matches(const QString &searchKeyWord);
47 const QStringList &errors() const;
48
49protected:
50 virtual QWidget *setupWidget() = 0;
51 QStringList &errors();
52
53private:
54 std::unique_ptr<QWidget> m_widget;
55 QWidget *m_parentWindow;
56 bool m_shown;
57 bool m_keywordsInitialized;
58 QStringList m_keywords;
59 QStringList m_errors;
60};
61
65inline QWidget *OptionPage::parentWindow() const
66{
67 return m_parentWindow;
68}
69
75inline bool OptionPage::hasBeenShown() const
76{
77 return m_widget != nullptr && m_shown;
78}
79
84inline const QStringList &OptionPage::errors() const
85{
86 return m_errors;
87}
88
97inline QStringList &OptionPage::errors()
98{
99 return m_errors;
100}
101
110template <class UiClass> class QT_UTILITIES_EXPORT UiFileBasedOptionPage : public OptionPage {
111public:
112 explicit UiFileBasedOptionPage(QWidget *parentWindow = nullptr);
113 ~UiFileBasedOptionPage() override;
114
115 bool apply() override = 0;
116 void reset() override = 0;
117
118protected:
119 QWidget *setupWidget() override;
120 UiClass *ui();
121
122private:
123 std::unique_ptr<UiClass> m_ui;
124};
125
129template <class UiClass>
131 : OptionPage(parentWindow)
132{
133}
134
141
145template <class UiClass> QWidget *UiFileBasedOptionPage<UiClass>::setupWidget()
146{
147 auto *const widget = new OptionPageWidget();
148 if (!m_ui) {
149 m_ui.reset(new UiClass);
150 }
151 m_ui->setupUi(widget);
152 QObject::connect(widget, &OptionPageWidget::retranslationRequired, [this, widget] { m_ui->retranslateUi(widget); });
153 return widget;
154}
155
159template <class UiClass> inline UiClass *UiFileBasedOptionPage<UiClass>::ui()
160{
161 return m_ui.get();
162}
163} // namespace QtUtilities
164
168#define BEGIN_DECLARE_TYPEDEF_OPTION_PAGE(SomeClass) using SomeClass##Base = ::QtUtilities::OptionPage;
169
175#define BEGIN_DECLARE_OPTION_PAGE(SomeClass) \
176 BEGIN_DECLARE_TYPEDEF_OPTION_PAGE(SomeClass) \
177 class QT_UTILITIES_EXPORT SomeClass : public ::QtUtilities::OptionPage { \
178 public: \
179 explicit SomeClass(QWidget *parentWidget = nullptr); \
180 ~SomeClass() override; \
181 bool apply() override; \
182 void reset() override; \
183 \
184 private:
185
191#define BEGIN_DECLARE_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
192 BEGIN_DECLARE_TYPEDEF_OPTION_PAGE(SomeClass) \
193 class QT_UTILITIES_EXPORT SomeClass : public ::QtUtilities::OptionPage { \
194 public: \
195 ~SomeClass() override; \
196 bool apply() override; \
197 void reset() override; \
198 \
199 private:
200
204#define BEGIN_DECLARE_TYPEDEF_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
205 namespace Ui { \
206 class SomeClass; \
207 } \
208 using SomeClass##Base = ::QtUtilities::UiFileBasedOptionPage<Ui::SomeClass>;
209
215#define BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
216 BEGIN_DECLARE_TYPEDEF_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
217 class QT_UTILITIES_EXPORT SomeClass : public ::QtUtilities::UiFileBasedOptionPage<Ui::SomeClass> { \
218 public: \
219 ~SomeClass() override; \
220 bool apply() override; \
221 void reset() override; \
222 \
223 private:
224
230#define BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
231 BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
232public: \
233 explicit SomeClass(QWidget *parentWidget = nullptr); \
234 \
235private:
236
241#define END_DECLARE_OPTION_PAGE \
242 } \
243 ;
244
250#define INSTANTIATE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
251 namespace QtUtilities { \
252 template class UiFileBasedOptionPage<Ui::SomeClass>; \
253 }
254
261#define INSTANTIATE_UI_FILE_BASED_OPTION_PAGE_NS(SomeNamespace, SomeClass) \
262 namespace QtUtilities { \
263 template class UiFileBasedOptionPage<::SomeNamespace::Ui::SomeClass>; \
264 }
265
271#define DECLARE_EXTERN_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
272 namespace QtUtilities { \
273 namespace Ui { \
274 class SomeClass; \
275 } \
276 extern template class UiFileBasedOptionPage<Ui::SomeClass>; \
277 }
278
285#define DECLARE_EXTERN_UI_FILE_BASED_OPTION_PAGE_NS(SomeNamespace, SomeClass) \
286 namespace SomeNamespace { \
287 namespace Ui { \
288 class SomeClass; \
289 } \
290 } \
291 namespace QtUtilities { \
292 extern template class UiFileBasedOptionPage<::SomeNamespace::Ui::SomeClass>; \
293 }
294
300#define DECLARE_SETUP_WIDGETS \
301protected: \
302 QWidget *setupWidget() override; \
303 \
304private:
305
311#define DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
312 BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
313 END_DECLARE_OPTION_PAGE
314
320#define DECLARE_OPTION_PAGE(SomeClass) \
321 BEGIN_DECLARE_OPTION_PAGE(SomeClass) \
322 DECLARE_SETUP_WIDGETS \
323 END_DECLARE_OPTION_PAGE
324
330#define DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_SETUP(SomeClass) \
331 BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
332 DECLARE_SETUP_WIDGETS \
333 END_DECLARE_OPTION_PAGE
334
335#endif // DIALOGS_OPTIONSPAGE_H
OptionPageWidget(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
Definition optionpage.h:29
The OptionPage class is the base class for SettingsDialog pages.
Definition optionpage.h:34
QWidget * parentWindow() const
Returns the parent window of the option page.
Definition optionpage.h:65
virtual void reset()=0
Discards altered settings and resets relevant widgets.
virtual bool apply()=0
Applies altered settings.
const QStringList & errors() const
Returns the errors which haven been occurred when applying the changes.
Definition optionpage.h:84
bool hasBeenShown() const
Returns an indication whether the option page has been shown yet.
Definition optionpage.h:75
virtual QWidget * setupWidget()=0
Creates the widget for the page.
The SettingsDialog class provides a framework for creating settings dialogs with different categories...
UiClass * ui()
Provides the derived class access to the UI class.
Definition optionpage.h:159
UiFileBasedOptionPage(QWidget *parentWindow=nullptr)
Constructs a new UI file based option page.
Definition optionpage.h:130
void reset() override=0
Discards altered settings and resets relevant widgets.
~UiFileBasedOptionPage() override
Destroys the option page.
Definition optionpage.h:138
QWidget * setupWidget() override
Inflates the widget for the option page using the UI class.
Definition optionpage.h:145
bool apply() override=0
Applies altered settings.
#define QT_UTILITIES_EXPORT
Marks the symbol to be exported by the qtutilities library.
Definition global.h:14