Qt Utilities  5.6.0
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 Dialogs {
12 
13 class SettingsDialog;
14 
16 {
17  friend class SettingsDialog;
18 
19 public:
20  explicit OptionPage(QWidget *parentWindow = nullptr);
21  virtual ~OptionPage();
22 
23  QWidget *parentWindow() const;
24  QWidget *widget();
25  bool hasBeenShown() const;
26  virtual bool apply() = 0;
27  virtual void reset() = 0;
28  bool matches(const QString &searchKeyWord);
29  const QStringList &errors() const;
30 
31 protected:
32  virtual QWidget *setupWidget() = 0;
33  QStringList &errors();
34 
35 private:
36  std::unique_ptr<QWidget> m_widget;
37  QWidget *m_parentWindow;
38  bool m_shown;
39  bool m_keywordsInitialized;
40  QStringList m_keywords;
41  QStringList m_errors;
42 };
43 
47 inline QWidget *OptionPage::parentWindow() const
48 {
49  return m_parentWindow;
50 }
51 
55 inline bool OptionPage::hasBeenShown() const
56 {
57  return m_widget != nullptr && m_shown;
58 }
59 
63 inline const QStringList &OptionPage::errors() const
64 {
65  return m_errors;
66 }
67 
74 inline QStringList &OptionPage::errors()
75 {
76  return m_errors;
77 }
78 
86 template <class UiClass>
88 {
89 public:
90  explicit UiFileBasedOptionPage(QWidget *parentWindow = nullptr);
92 
93  bool apply() = 0;
94  void reset() = 0;
95 
96 protected:
97  QWidget *setupWidget();
98  UiClass *ui();
99 
100 private:
101  std::unique_ptr<UiClass> m_ui;
102 };
103 
107 template <class UiClass>
109  OptionPage(parentWindow)
110 {}
111 
115 template <class UiClass>
117 {}
118 
122 template <class UiClass>
124 {
125  QWidget *widget = new QWidget();
126  if(!m_ui) {
127  m_ui.reset(new UiClass);
128  }
129  m_ui->setupUi(widget);
130  return widget;
131 }
132 
136 template <class UiClass>
138 {
139  return m_ui.get();
140 }
141 
142 }
143 
148 #define BEGIN_DECLARE_OPTION_PAGE(SomeClass) \
149  typedef ::Dialogs::OptionPage SomeClass ## Base; \
150  class QT_UTILITIES_EXPORT SomeClass : public ::Dialogs::OptionPage \
151  { \
152  public: \
153  explicit SomeClass(QWidget *parentWidget = nullptr); \
154  ~SomeClass(); \
155  bool apply(); \
156  void reset(); \
157  private:
158 
163 #define BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
164  namespace Ui { \
165  class SomeClass; \
166  } \
167  typedef ::Dialogs::UiFileBasedOptionPage<Ui::SomeClass> SomeClass ## Base; \
168  class QT_UTILITIES_EXPORT SomeClass : public ::Dialogs::UiFileBasedOptionPage<Ui::SomeClass> \
169  { \
170  public: \
171  ~SomeClass(); \
172  bool apply(); \
173  void reset(); \
174  private:
175 
180 #define BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
181  BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
182  public: \
183  explicit SomeClass(QWidget *parentWidget = nullptr); \
184  private:
185 
189 #define END_DECLARE_OPTION_PAGE \
190  };
191 
196 #define INSTANTIATE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
197  namespace Dialogs { \
198  template class UiFileBasedOptionPage<Ui::SomeClass>; \
199  }
200 
205 #define INSTANTIATE_UI_FILE_BASED_OPTION_PAGE_NS(SomeNamespace, SomeClass) \
206  namespace Dialogs { \
207  template class UiFileBasedOptionPage<::SomeNamespace::Ui::SomeClass>; \
208  }
209 
214 #define DECLARE_EXTERN_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
215  namespace Dialogs { \
216  namespace Ui { \
217  class SomeClass; \
218  } \
219  extern template class UiFileBasedOptionPage<Ui::SomeClass>; \
220  }
221 
226 #define DECLARE_EXTERN_UI_FILE_BASED_OPTION_PAGE_NS(SomeNamespace, SomeClass) \
227  namespace SomeNamespace { \
228  namespace Ui { \
229  class SomeClass; \
230  } \
231  } \
232  namespace Dialogs { \
233  extern template class UiFileBasedOptionPage<::SomeNamespace::Ui::SomeClass>; \
234  }
235 
240 #define DECLARE_SETUP_WIDGETS \
241  protected: \
242  QWidget *setupWidget(); \
243  private:
244 
249 #define DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
250  BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
251  END_DECLARE_OPTION_PAGE
252 
257 #define DECLARE_OPTION_PAGE(SomeClass) \
258  BEGIN_DECLARE_OPTION_PAGE(SomeClass) \
259  DECLARE_SETUP_WIDGETS \
260  END_DECLARE_OPTION_PAGE
261 
266 #define DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_SETUP(SomeClass) \
267  BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
268  DECLARE_SETUP_WIDGETS \
269  END_DECLARE_OPTION_PAGE
270 
271 #endif // DIALOGS_OPTIONSPAGE_H
QWidget * widget()
Returns the widget for the option page.
Definition: optionpage.cpp:42
UiFileBasedOptionPage(QWidget *parentWindow=nullptr)
Constructs a new UI file based option page.
Definition: optionpage.h:108
The OptionPage class is the base class for SettingsDialog pages.
Definition: optionpage.h:15
The UiFileBasedOptionPage class is the base class for SettingsDialog pages using UI files to describe...
Definition: optionpage.h:87
UiClass * ui()
Provides the derived class access to the UI class.
Definition: optionpage.h:137
#define QT_UTILITIES_EXPORT
Marks the symbol to be exported by the qtutilities library.
bool hasBeenShown() const
Returns an indication whether the option page has been shown yet.
Definition: optionpage.h:55
Provides common dialogs such as AboutDialog, EnterPasswordDialog and SettingsDialog.
Definition: dialogutils.h:12
const QStringList & errors() const
Returns the errors which haven been occurred when applying the changes.
Definition: optionpage.h:63
The SettingsDialog class provides a framework for creating settings dialogs with different categories...
QWidget * setupWidget()
Inflates the widget for the option page using the UI class.
Definition: optionpage.h:123
QWidget * parentWindow() const
Returns the parent window of the option page.
Definition: optionpage.h:47
~UiFileBasedOptionPage()
Destroys the option page.
Definition: optionpage.h:116