Qt Utilities: settingsdialog/optionpage.h Source File

Qt Utilities 6.21.1 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
15 class 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:
22 void paletteChanged();
23 void retranslationRequired();
24
25protected:
26 bool event(QEvent *) override;
27};
28
29 inline OptionPageWidget::OptionPageWidget(QWidget *parent, Qt::WindowFlags f)
30 : QWidget(parent, f)
31{
32}
33
34 class QT_UTILITIES_EXPORT OptionPage {
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
65 inline QWidget *OptionPage::parentWindow() const
66{
67 return m_parentWindow;
68}
69
75 inline bool OptionPage::hasBeenShown() const
76{
77 return m_widget != nullptr && m_shown;
78}
79
84 inline const QStringList &OptionPage::errors() const
85{
86 return m_errors;
87}
88
97 inline QStringList &OptionPage::errors()
98{
99 return m_errors;
100}
101
110 template <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>
130UiFileBasedOptionPage<UiClass>::UiFileBasedOptionPage(QWidget *parentWindow)
131 : OptionPage(parentWindow)
132{
133}
134
138 template <class UiClass> UiFileBasedOptionPage<UiClass>::~UiFileBasedOptionPage()
139{
140}
141
145 template <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
159 template <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
QtUtilities::OptionPageWidget
Definition optionpage.h:15
QtUtilities::OptionPageWidget::paletteChanged
void paletteChanged()
QtUtilities::OptionPageWidget::event
bool event(QEvent *) override
Emits the paletteChanged() signal.
Definition optionpage.cpp:96
QtUtilities::OptionPageWidget::retranslationRequired
void retranslationRequired()
QtUtilities::OptionPageWidget::OptionPageWidget
OptionPageWidget(QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
Definition optionpage.h:29
QtUtilities::OptionPage::parentWindow
QWidget * parentWindow() const
Returns the parent window of the option page.
Definition optionpage.h:65
QtUtilities::OptionPage::OptionPage
OptionPage(QWidget *parentWindow=nullptr)
Constructs a option page.
Definition optionpage.cpp:25
QtUtilities::OptionPage::reset
virtual void reset()=0
Discards altered settings and resets relevant widgets.
QtUtilities::OptionPage::apply
virtual bool apply()=0
Applies altered settings.
QtUtilities::OptionPage::errors
const QStringList & errors() const
Returns the errors which haven been occurred when applying the changes.
Definition optionpage.h:84
QtUtilities::OptionPage::matches
bool matches(const QString &searchKeyWord)
Returns whether the pages matches the specified searchKeyWord.
Definition optionpage.cpp:64
QtUtilities::OptionPage::SettingsDialog
friend class SettingsDialog
Definition optionpage.h:35
QtUtilities::OptionPage::hasBeenShown
bool hasBeenShown() const
Returns an indication whether the option page has been shown yet.
Definition optionpage.h:75
QtUtilities::OptionPage::setupWidget
virtual QWidget * setupWidget()=0
Creates the widget for the page.
QtUtilities::OptionPage::widget
QWidget * widget()
Returns the widget for the option page.
Definition optionpage.cpp:48
QtUtilities::SettingsDialog
The SettingsDialog class provides a framework for creating settings dialogs with different categories...
Definition settingsdialog.h:21
QtUtilities::UiFileBasedOptionPage::ui
UiClass * ui()
Provides the derived class access to the UI class.
Definition optionpage.h:159
QtUtilities::UiFileBasedOptionPage::UiFileBasedOptionPage
UiFileBasedOptionPage(QWidget *parentWindow=nullptr)
Constructs a new UI file based option page.
Definition optionpage.h:130
QtUtilities::UiFileBasedOptionPage::reset
void reset() override=0
Discards altered settings and resets relevant widgets.
QtUtilities::UiFileBasedOptionPage::~UiFileBasedOptionPage
~UiFileBasedOptionPage() override
Destroys the option page.
Definition optionpage.h:138
QtUtilities::UiFileBasedOptionPage::setupWidget
QWidget * setupWidget() override
Inflates the widget for the option page using the UI class.
Definition optionpage.h:145
QtUtilities::UiFileBasedOptionPage::apply
bool apply() override=0
Applies altered settings.
global.h
QT_UTILITIES_EXPORT
#define QT_UTILITIES_EXPORT
Marks the symbol to be exported by the qtutilities library.
Definition global.h:14
QtUtilities
!
Definition trylocker.h:8