Qt Utilities  5.7.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  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 
54 inline bool OptionPage::hasBeenShown() const
55 {
56  return m_widget != nullptr && m_shown;
57 }
58 
63 inline const QStringList &OptionPage::errors() const
64 {
65  return m_errors;
66 }
67 
76 inline QStringList &OptionPage::errors()
77 {
78  return m_errors;
79 }
80 
89 template <class UiClass> class QT_UTILITIES_EXPORT UiFileBasedOptionPage : public OptionPage {
90 public:
91  explicit UiFileBasedOptionPage(QWidget *parentWindow = nullptr);
93 
94  bool apply() = 0;
95  void reset() = 0;
96 
97 protected:
98  QWidget *setupWidget();
99  UiClass *ui();
100 
101 private:
102  std::unique_ptr<UiClass> m_ui;
103 };
104 
108 template <class UiClass>
110  : OptionPage(parentWindow)
111 {
112 }
113 
118 {
119 }
120 
124 template <class UiClass> QWidget *UiFileBasedOptionPage<UiClass>::setupWidget()
125 {
126  QWidget *widget = new QWidget();
127  if (!m_ui) {
128  m_ui.reset(new UiClass);
129  }
130  m_ui->setupUi(widget);
131  return widget;
132 }
133 
137 template <class UiClass> inline UiClass *UiFileBasedOptionPage<UiClass>::ui()
138 {
139  return m_ui.get();
140 }
141 }
142 
148 #define BEGIN_DECLARE_OPTION_PAGE(SomeClass) \
149  typedef ::Dialogs::OptionPage SomeClass##Base; \
150  class QT_UTILITIES_EXPORT SomeClass : public ::Dialogs::OptionPage { \
151  public: \
152  explicit SomeClass(QWidget *parentWidget = nullptr); \
153  ~SomeClass(); \
154  bool apply(); \
155  void reset(); \
156  \
157  private:
158 
164 #define BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
165  namespace Ui { \
166  class SomeClass; \
167  } \
168  typedef ::Dialogs::UiFileBasedOptionPage<Ui::SomeClass> SomeClass##Base; \
169  class QT_UTILITIES_EXPORT SomeClass : public ::Dialogs::UiFileBasedOptionPage<Ui::SomeClass> { \
170  public: \
171  ~SomeClass(); \
172  bool apply(); \
173  void reset(); \
174  \
175  private:
176 
182 #define BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
183  BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
184 public: \
185  explicit SomeClass(QWidget *parentWidget = nullptr); \
186  \
187 private:
188 
193 #define END_DECLARE_OPTION_PAGE \
194  } \
195  ;
196 
202 #define INSTANTIATE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
203  namespace Dialogs { \
204  template class UiFileBasedOptionPage<Ui::SomeClass>; \
205  }
206 
213 #define INSTANTIATE_UI_FILE_BASED_OPTION_PAGE_NS(SomeNamespace, SomeClass) \
214  namespace Dialogs { \
215  template class UiFileBasedOptionPage<::SomeNamespace::Ui::SomeClass>; \
216  }
217 
223 #define DECLARE_EXTERN_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
224  namespace Dialogs { \
225  namespace Ui { \
226  class SomeClass; \
227  } \
228  extern template class UiFileBasedOptionPage<Ui::SomeClass>; \
229  }
230 
237 #define DECLARE_EXTERN_UI_FILE_BASED_OPTION_PAGE_NS(SomeNamespace, SomeClass) \
238  namespace SomeNamespace { \
239  namespace Ui { \
240  class SomeClass; \
241  } \
242  } \
243  namespace Dialogs { \
244  extern template class UiFileBasedOptionPage<::SomeNamespace::Ui::SomeClass>; \
245  }
246 
252 #define DECLARE_SETUP_WIDGETS \
253 protected: \
254  QWidget *setupWidget(); \
255  \
256 private:
257 
263 #define DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
264  BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
265  END_DECLARE_OPTION_PAGE
266 
272 #define DECLARE_OPTION_PAGE(SomeClass) \
273  BEGIN_DECLARE_OPTION_PAGE(SomeClass) \
274  DECLARE_SETUP_WIDGETS \
275  END_DECLARE_OPTION_PAGE
276 
282 #define DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_SETUP(SomeClass) \
283  BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
284  DECLARE_SETUP_WIDGETS \
285  END_DECLARE_OPTION_PAGE
286 
287 #endif // DIALOGS_OPTIONSPAGE_H
QWidget * widget()
Returns the widget for the option page.
Definition: optionpage.cpp:45
UiFileBasedOptionPage(QWidget *parentWindow=nullptr)
Constructs a new UI file based option page.
Definition: optionpage.h:109
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:89
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:54
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:124
QWidget * parentWindow() const
Returns the parent window of the option page.
Definition: optionpage.h:46
~UiFileBasedOptionPage()
Destroys the option page.
Definition: optionpage.h:117