#ifndef OPTIONSPAGE_H #define OPTIONSPAGE_H #include #include #include #include namespace Dialogs { class LIB_EXPORT OptionPage { public: explicit OptionPage(QWidget *parentWindow = nullptr); virtual ~OptionPage(); QWidget *parentWindow() const; QWidget *widget(); bool hasBeenShown() const; virtual bool apply() = 0; virtual void reset() = 0; bool matches(const QString &searchKeyWord); const QStringList &errors() const; protected: virtual QWidget *setupWidget() = 0; QStringList &errors(); private: std::unique_ptr m_widget; QWidget *m_parentWindow; bool m_shown; bool m_keywordsInitialized; QStringList m_keywords; QStringList m_errors; }; /*! * \brief Returns the parent window of the option page. */ inline QWidget *OptionPage::parentWindow() const { return m_parentWindow; } /*! * \brief Returns an indication whether the option page has been shown yet. */ inline bool OptionPage::hasBeenShown() const { return m_widget != nullptr && m_shown; } /*! * \brief Returns the errors which haven been occurred when applying the changes. */ inline const QStringList &OptionPage::errors() const { return m_errors; } /*! * \brief Returns the errors which haven been occurred when applying the changes. * * Error messages should be added when implementing apply() and something goes wrong. * In this case, apply() should return false. */ inline QStringList &OptionPage::errors() { return m_errors; } /*! * \class Dialogs::UiFileBasedOptionPage * \brief The UiFileBasedOptionPage class is the base class for SettingsDialog pages using UI files * to describe the widget tree. * * \tparam UiClass Specifies the UI class generated by uic. */ template class LIB_EXPORT UiFileBasedOptionPage : public OptionPage { public: explicit UiFileBasedOptionPage(QWidget *parentWindow = nullptr); virtual ~UiFileBasedOptionPage(); virtual bool apply() = 0; virtual void reset() = 0; protected: virtual QWidget *setupWidget(); UiClass *ui(); private: std::unique_ptr m_ui; }; /*! * \brief Constructs a new UI file based option page. */ template UiFileBasedOptionPage::UiFileBasedOptionPage(QWidget *parentWindow) : OptionPage(parentWindow) {} /*! * \brief Destroys the option page. */ template UiFileBasedOptionPage::~UiFileBasedOptionPage() {} /*! * \brief Sets up the widget for the option page using the UI class. */ template QWidget *UiFileBasedOptionPage::setupWidget() { QWidget *widget = new QWidget(); if(!m_ui) { m_ui.reset(new UiClass); } m_ui->setupUi(widget); return widget; } template inline UiClass *UiFileBasedOptionPage::ui() { return m_ui.get(); } } #define BEGIN_DECLARE_OPTION_PAGE(SomeClass) \ class SomeClass : public ::Dialogs::OptionPage \ { \ public: \ explicit SomeClass(QWidget *parentWidget = nullptr); \ ~SomeClass(); \ bool apply(); \ void reset(); \ private: #define BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \ namespace Ui { \ class SomeClass; \ } \ typedef ::Dialogs::UiFileBasedOptionPage SomeClass ## Base; \ class SomeClass : public ::Dialogs::UiFileBasedOptionPage \ { \ public: \ explicit SomeClass(QWidget *parentWidget = nullptr); \ ~SomeClass(); \ bool apply(); \ void reset(); \ private: #define END_DECLARE_OPTION_PAGE \ }; #define DECLARE_SETUP_WIDGETS \ protected: \ QWidget *setupWidget(); \ private: #define DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \ BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \ END_DECLARE_OPTION_PAGE #define DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_SETUP(SomeClass) \ BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \ DECLARE_SETUP_WIDGETS \ END_DECLARE_OPTION_PAGE #endif // OPTIONSPAGE_H