Debian Codegraph @

settingsdialog/optionpage.h

raw symbols SCIP
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
11 namespace QtUtilities {
12
13 class SettingsDialog;
14
15 class QT_UTILITIES_EXPORT OptionPageWidget : public QWidget {
16 Q_OBJECT
17
18 public:
19 explicit OptionPageWidget(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
20
21 Q_SIGNALS:
22 void paletteChanged();
23 void retranslationRequired();
24
25 protected:
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
37 public:
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
49 protected:
50 virtual QWidget *setupWidget() = 0;
51 QStringList &errors();
52
53 private:
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
62 /*!
63 * \brief Returns the parent window of the option page.
64 */
65 inline QWidget *OptionPage::parentWindow() const
66 {
67 return m_parentWindow;
68 }
69
70 /*!
71 * \brief Returns an indication whether the option page has been shown yet.
72 * \remarks If this is true, the method OptionPage::setupWidget() has already
73 * been called.
74 */
75 inline bool OptionPage::hasBeenShown() const
76 {
77 return m_widget != nullptr && m_shown;
78 }
79
80 /*!
81 * \brief Returns the errors which haven been occurred when applying the
82 * changes.
83 */
84 inline const QStringList &OptionPage::errors() const
85 {
86 return m_errors;
87 }
88
89 /*!
90 * \brief Returns the errors which haven been occurred when applying the
91 * changes.
92 *
93 * Error messages should be added when implementing apply() and something goes
94 * wrong.
95 * In this case, apply() should return false.
96 */
97 inline QStringList &OptionPage::errors()
98 {
99 return m_errors;
100 }
101
102 /*!
103 * \class Dialogs::UiFileBasedOptionPage
104 * \brief The UiFileBasedOptionPage class is the base class for SettingsDialog
105 * pages using UI files
106 * to describe the widget tree.
107 *
108 * \tparam UiClass Specifies the UI class generated by uic.
109 */
110 template <class UiClass> class QT_UTILITIES_EXPORT UiFileBasedOptionPage : public OptionPage {
111 public:
112 explicit UiFileBasedOptionPage(QWidget *parentWindow = nullptr);
113 ~UiFileBasedOptionPage() override;
114
115 bool apply() override = 0;
116 void reset() override = 0;
117
118 protected:
119 QWidget *setupWidget() override;
120 UiClass *ui();
121
122 private:
123 std::unique_ptr<UiClass> m_ui;
124 };
125
126 /*!
127 * \brief Constructs a new UI file based option page.
128 */
129 template <class UiClass>
130 UiFileBasedOptionPage<UiClass>::UiFileBasedOptionPage(QWidget *parentWindow)
131 : OptionPage(parentWindow)
132 {
133 }
134
135 /*!
136 * \brief Destroys the option page.
137 */
138 template <class UiClass> UiFileBasedOptionPage<UiClass>::~UiFileBasedOptionPage()
139 {
140 }
141
142 /*!
143 * \brief Inflates the widget for the option page using the UI class.
144 */
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
156 /*!
157 * \brief Provides the derived class access to the UI class.
158 */
159 template <class UiClass> inline UiClass *UiFileBasedOptionPage<UiClass>::ui()
160 {
161 return m_ui.get();
162 }
163 } // namespace QtUtilities
164
165 /*!
166 * \brief Declares the base class for a class inheriting from Dialogs::OptionPage.
167 */
168 #define BEGIN_DECLARE_TYPEDEF_OPTION_PAGE(SomeClass) using SomeClass##Base = ::QtUtilities::OptionPage;
169
170 /*!
171 * \brief Declares a class inheriting from Dialogs::OptionPage in a convenient
172 * way.
173 * \remarks Must be closed with END_DECLARE_OPTION_PAGE.
174 */
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
186 /*!
187 * \brief Declares a class inheriting from Dialogs::OptionPage in a convenient
188 * way.
189 * \remarks Must be closed with END_DECLARE_OPTION_PAGE.
190 */
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
201 /*!
202 * \brief Declares the base class for a class inheriting from Dialogs::UiFileBasedOptionPage.
203 */
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
210 /*!
211 * \brief Declares a class inheriting from Dialogs::UiFileBasedOptionPage in a
212 * convenient way.
213 * \remarks Must be closed with END_DECLARE_OPTION_PAGE.
214 */
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
225 /*!
226 * \brief Declares a class inheriting from Dialogs::UiFileBasedOptionPage in a
227 * convenient way.
228 * \remarks Must be closed with END_DECLARE_OPTION_PAGE.
229 */
230 #define BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
231 BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
232 public: \
233 explicit SomeClass(QWidget *parentWidget = nullptr); \
234 \
235 private:
236
237 /*!
238 * \brief Must be used after BEGIN_DECLARE_OPTION_PAGE and
239 * BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE.
240 */
241 #define END_DECLARE_OPTION_PAGE \
242 } \
243 ;
244
245 /*!
246 * \brief Instantiates a class declared with
247 * BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE in a convenient way.
248 * \remarks Might be required when the class is used by another application.
249 */
250 #define INSTANTIATE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
251 namespace QtUtilities { \
252 template class UiFileBasedOptionPage<Ui::SomeClass>; \
253 }
254
255 /*!
256 * \brief Instantiates a class declared with
257 * BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE inside a given namespace in a
258 * convenient way.
259 * \remarks Might be required when the class is used by another application.
260 */
261 #define INSTANTIATE_UI_FILE_BASED_OPTION_PAGE_NS(SomeNamespace, SomeClass) \
262 namespace QtUtilities { \
263 template class UiFileBasedOptionPage<::SomeNamespace::Ui::SomeClass>; \
264 }
265
266 /*!
267 * \brief Declares external instantiation of class declared with
268 * BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE in a convenient way.
269 * \remarks Might be required when the class comes from an external library.
270 */
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
279 /*!
280 * \brief Declares external instantiation of class declared with
281 * BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE inside a given namespace in a
282 * convenient way.
283 * \remarks Might be required when the class comes from an external library.
284 */
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
295 /*!
296 * \brief Declares the method setupWidget() in a convenient way.
297 * \remarks Can be used between BEGIN_DECLARE_OPTION_PAGE and
298 * END_DECLARE_OPTION_PAGE.
299 */
300 #define DECLARE_SETUP_WIDGETS \
301 protected: \
302 QWidget *setupWidget() override; \
303 \
304 private:
305
306 /*!
307 * \brief Declares a class inheriting from Dialogs::OptionPage in a convenient
308 * way.
309 * \remarks Doesn't allow to declare additional class members.
310 */
311 #define DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
312 BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
313 END_DECLARE_OPTION_PAGE
314
315 /*!
316 * \brief Declares a class inheriting from Dialogs::OptionPage in a convenient
317 * way.
318 * \remarks Doesn't allow to declare additional class members.
319 */
320 #define DECLARE_OPTION_PAGE(SomeClass) \
321 BEGIN_DECLARE_OPTION_PAGE(SomeClass) \
322 DECLARE_SETUP_WIDGETS \
323 END_DECLARE_OPTION_PAGE
324
325 /*!
326 * \brief Declares a class inheriting from Dialogs::UiFileBasedOptionPage in a
327 * convenient way.
328 * \remarks Doesn't allow to declare additional class members.
329 */
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