Qt Utilities  5.12.2
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 
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 Dialogs
144 
150 #define BEGIN_DECLARE_OPTION_PAGE(SomeClass) \
151  typedef ::Dialogs::OptionPage SomeClass##Base; \
152  class QT_UTILITIES_EXPORT SomeClass : public ::Dialogs::OptionPage { \
153  public: \
154  explicit SomeClass(QWidget *parentWidget = nullptr); \
155  ~SomeClass() override; \
156  bool apply() override; \
157  void reset() override; \
158  \
159  private:
160 
166 #define BEGIN_DECLARE_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
167  typedef ::Dialogs::OptionPage SomeClass##Base; \
168  class QT_UTILITIES_EXPORT SomeClass : public ::Dialogs::OptionPage { \
169  public: \
170  ~SomeClass() override; \
171  bool apply() override; \
172  void reset() override; \
173  \
174  private:
175 
181 #define BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
182  namespace Ui { \
183  class SomeClass; \
184  } \
185  typedef ::Dialogs::UiFileBasedOptionPage<Ui::SomeClass> SomeClass##Base; \
186  class QT_UTILITIES_EXPORT SomeClass : public ::Dialogs::UiFileBasedOptionPage<Ui::SomeClass> { \
187  public: \
188  ~SomeClass() override; \
189  bool apply() override; \
190  void reset() override; \
191  \
192  private:
193 
199 #define BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
200  BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
201 public: \
202  explicit SomeClass(QWidget *parentWidget = nullptr); \
203  \
204 private:
205 
210 #define END_DECLARE_OPTION_PAGE \
211  } \
212  ;
213 
219 #define INSTANTIATE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
220  namespace Dialogs { \
221  template class UiFileBasedOptionPage<Ui::SomeClass>; \
222  }
223 
230 #define INSTANTIATE_UI_FILE_BASED_OPTION_PAGE_NS(SomeNamespace, SomeClass) \
231  namespace Dialogs { \
232  template class UiFileBasedOptionPage<::SomeNamespace::Ui::SomeClass>; \
233  }
234 
240 #define DECLARE_EXTERN_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
241  namespace Dialogs { \
242  namespace Ui { \
243  class SomeClass; \
244  } \
245  extern template class UiFileBasedOptionPage<Ui::SomeClass>; \
246  }
247 
254 #define DECLARE_EXTERN_UI_FILE_BASED_OPTION_PAGE_NS(SomeNamespace, SomeClass) \
255  namespace SomeNamespace { \
256  namespace Ui { \
257  class SomeClass; \
258  } \
259  } \
260  namespace Dialogs { \
261  extern template class UiFileBasedOptionPage<::SomeNamespace::Ui::SomeClass>; \
262  }
263 
269 #define DECLARE_SETUP_WIDGETS \
270 protected: \
271  QWidget *setupWidget() override; \
272  \
273 private:
274 
280 #define DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
281  BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
282  END_DECLARE_OPTION_PAGE
283 
289 #define DECLARE_OPTION_PAGE(SomeClass) \
290  BEGIN_DECLARE_OPTION_PAGE(SomeClass) \
291  DECLARE_SETUP_WIDGETS \
292  END_DECLARE_OPTION_PAGE
293 
299 #define DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_SETUP(SomeClass) \
300  BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
301  DECLARE_SETUP_WIDGETS \
302  END_DECLARE_OPTION_PAGE
303 
304 #endif // DIALOGS_OPTIONSPAGE_H
UiFileBasedOptionPage(QWidget *parentWindow=nullptr)
Constructs a new UI file based option page.
Definition: optionpage.h:111
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:91
UiClass * ui()
Provides the derived class access to the UI class.
Definition: optionpage.h:139
#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:56
Provides common dialogs such as AboutDialog, EnterPasswordDialog and SettingsDialog.
Definition: dialogutils.h:12
~UiFileBasedOptionPage() override
Destroys the option page.
Definition: optionpage.h:119
const QStringList & errors() const
Returns the errors which haven been occurred when applying the changes.
Definition: optionpage.h:65
The SettingsDialog class provides a framework for creating settings dialogs with different categories...
QWidget * setupWidget() override
Inflates the widget for the option page using the UI class.
Definition: optionpage.h:126
QWidget * parentWindow() const
Returns the parent window of the option page.
Definition: optionpage.h:46