Qt Utilities  6.4.1
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
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 
11 namespace QtUtilities {
12 
13 class SettingsDialog;
14 
16  friend class SettingsDialog;
17 
18 public:
19  explicit OptionPage(QWidget *parentWindow = nullptr);
20  virtual ~OptionPage();
21 
22  QWidget *parentWindow() const;
23  QWidget *widget();
24  bool hasBeenShown() const;
25  virtual bool apply() = 0;
26  virtual void reset() = 0;
27  bool matches(const QString &searchKeyWord);
28  const QStringList &errors() const;
29 
30 protected:
31  virtual QWidget *setupWidget() = 0;
32  QStringList &errors();
33 
34 private:
35  std::unique_ptr<QWidget> m_widget;
36  QWidget *m_parentWindow;
37  bool m_shown;
38  bool m_keywordsInitialized;
39  QStringList m_keywords;
40  QStringList m_errors;
41 };
42 
46 inline QWidget *OptionPage::parentWindow() const
47 {
48  return m_parentWindow;
49 }
50 
56 inline bool OptionPage::hasBeenShown() const
57 {
58  return m_widget != nullptr && m_shown;
59 }
60 
65 inline const QStringList &OptionPage::errors() const
66 {
67  return m_errors;
68 }
69 
78 inline QStringList &OptionPage::errors()
79 {
80  return m_errors;
81 }
82 
91 template <class UiClass> class QT_UTILITIES_EXPORT UiFileBasedOptionPage : public OptionPage {
92 public:
93  explicit UiFileBasedOptionPage(QWidget *parentWindow = nullptr);
94  ~UiFileBasedOptionPage() override;
95 
96  bool apply() override = 0;
97  void reset() override = 0;
98 
99 protected:
100  QWidget *setupWidget() override;
101  UiClass *ui();
102 
103 private:
104  std::unique_ptr<UiClass> m_ui;
105 };
106 
110 template <class UiClass>
112  : OptionPage(parentWindow)
113 {
114 }
115 
120 {
121 }
122 
126 template <class UiClass> QWidget *UiFileBasedOptionPage<UiClass>::setupWidget()
127 {
128  QWidget *widget = new QWidget();
129  if (!m_ui) {
130  m_ui.reset(new UiClass);
131  }
132  m_ui->setupUi(widget);
133  return widget;
134 }
135 
139 template <class UiClass> inline UiClass *UiFileBasedOptionPage<UiClass>::ui()
140 {
141  return m_ui.get();
142 }
143 } // namespace QtUtilities
144 
148 #define BEGIN_DECLARE_TYPEDEF_OPTION_PAGE(SomeClass) using SomeClass##Base = ::QtUtilities::OptionPage;
149 
155 #define BEGIN_DECLARE_OPTION_PAGE(SomeClass) \
156  BEGIN_DECLARE_TYPEDEF_OPTION_PAGE(SomeClass) \
157  class QT_UTILITIES_EXPORT SomeClass : public ::QtUtilities::OptionPage { \
158  public: \
159  explicit SomeClass(QWidget *parentWidget = nullptr); \
160  ~SomeClass() override; \
161  bool apply() override; \
162  void reset() override; \
163  \
164  private:
165 
171 #define BEGIN_DECLARE_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
172  BEGIN_DECLARE_TYPEDEF_OPTION_PAGE(SomeClass) \
173  class QT_UTILITIES_EXPORT SomeClass : public ::QtUtilities::OptionPage { \
174  public: \
175  ~SomeClass() override; \
176  bool apply() override; \
177  void reset() override; \
178  \
179  private:
180 
184 #define BEGIN_DECLARE_TYPEDEF_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
185  namespace Ui { \
186  class SomeClass; \
187  } \
188  using SomeClass##Base = ::QtUtilities::UiFileBasedOptionPage<Ui::SomeClass>;
189 
195 #define BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
196  BEGIN_DECLARE_TYPEDEF_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
197  class QT_UTILITIES_EXPORT SomeClass : public ::QtUtilities::UiFileBasedOptionPage<Ui::SomeClass> { \
198  public: \
199  ~SomeClass() override; \
200  bool apply() override; \
201  void reset() override; \
202  \
203  private:
204 
210 #define BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
211  BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
212 public: \
213  explicit SomeClass(QWidget *parentWidget = nullptr); \
214  \
215 private:
216 
221 #define END_DECLARE_OPTION_PAGE \
222  } \
223  ;
224 
230 #define INSTANTIATE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
231  namespace QtUtilities { \
232  template class UiFileBasedOptionPage<Ui::SomeClass>; \
233  }
234 
241 #define INSTANTIATE_UI_FILE_BASED_OPTION_PAGE_NS(SomeNamespace, SomeClass) \
242  namespace QtUtilities { \
243  template class UiFileBasedOptionPage<::SomeNamespace::Ui::SomeClass>; \
244  }
245 
251 #define DECLARE_EXTERN_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
252  namespace QtUtilities { \
253  namespace Ui { \
254  class SomeClass; \
255  } \
256  extern template class UiFileBasedOptionPage<Ui::SomeClass>; \
257  }
258 
265 #define DECLARE_EXTERN_UI_FILE_BASED_OPTION_PAGE_NS(SomeNamespace, SomeClass) \
266  namespace SomeNamespace { \
267  namespace Ui { \
268  class SomeClass; \
269  } \
270  } \
271  namespace QtUtilities { \
272  extern template class UiFileBasedOptionPage<::SomeNamespace::Ui::SomeClass>; \
273  }
274 
280 #define DECLARE_SETUP_WIDGETS \
281 protected: \
282  QWidget *setupWidget() override; \
283  \
284 private:
285 
291 #define DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
292  BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
293  END_DECLARE_OPTION_PAGE
294 
300 #define DECLARE_OPTION_PAGE(SomeClass) \
301  BEGIN_DECLARE_OPTION_PAGE(SomeClass) \
302  DECLARE_SETUP_WIDGETS \
303  END_DECLARE_OPTION_PAGE
304 
310 #define DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_SETUP(SomeClass) \
311  BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
312  DECLARE_SETUP_WIDGETS \
313  END_DECLARE_OPTION_PAGE
314 
315 #endif // DIALOGS_OPTIONSPAGE_H
The OptionPage class is the base class for SettingsDialog pages.
Definition: optionpage.h:15
QWidget * parentWindow() const
Returns the parent window of the option page.
Definition: optionpage.h:46
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:65
virtual void reset()=0
Discards altered settings and resets relevant widgets.
virtual QWidget * setupWidget()=0
Creates the widget for the page.
bool hasBeenShown() const
Returns an indication whether the option page has been shown yet.
Definition: optionpage.h:56
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:139
UiFileBasedOptionPage(QWidget *parentWindow=nullptr)
Constructs a new UI file based option page.
Definition: optionpage.h:111
void reset() override=0
Discards altered settings and resets relevant widgets.
~UiFileBasedOptionPage() override
Destroys the option page.
Definition: optionpage.h:119
QWidget * setupWidget() override
Inflates the widget for the option page using the UI class.
Definition: optionpage.h:126
bool apply() override=0
Applies altered settings.
#define QT_UTILITIES_EXPORT
Marks the symbol to be exported by the qtutilities library.