qtutilities/settingsdialog/optionpage.h

305 lines
15 KiB
C
Raw Normal View History

#ifndef DIALOGS_OPTIONSPAGE_H
#define DIALOGS_OPTIONSPAGE_H
2015-04-22 18:57:44 +02:00
#include "../global.h"
2015-04-22 18:57:44 +02:00
#include <QObject>
#include <QWidget>
#include <memory>
namespace Dialogs {
class SettingsDialog;
2017-05-01 03:16:25 +02:00
class QT_UTILITIES_EXPORT OptionPage {
friend class SettingsDialog;
2015-04-22 18:57:44 +02:00
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);
2016-04-04 14:49:40 +02:00
const QStringList &errors() const;
2015-04-22 18:57:44 +02:00
protected:
virtual QWidget *setupWidget() = 0;
2016-04-04 14:49:40 +02:00
QStringList &errors();
2015-04-22 18:57:44 +02:00
private:
std::unique_ptr<QWidget> m_widget;
QWidget *m_parentWindow;
bool m_shown;
bool m_keywordsInitialized;
QStringList m_keywords;
2016-04-04 14:49:40 +02:00
QStringList m_errors;
2015-04-22 18:57:44 +02:00
};
/*!
* \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.
* \remarks If this is true, the method OptionPage::setupWidget() has already
* been called.
2015-04-22 18:57:44 +02:00
*/
inline bool OptionPage::hasBeenShown() const
{
return m_widget != nullptr && m_shown;
}
2016-04-04 14:49:40 +02:00
/*!
2017-05-04 22:46:37 +02:00
* \brief Returns the errors which haven been occurred when applying the
* changes.
2016-04-04 14:49:40 +02:00
*/
inline const QStringList &OptionPage::errors() const
{
return m_errors;
}
/*!
2017-05-04 22:46:37 +02:00
* \brief Returns the errors which haven been occurred when applying the
* changes.
2016-04-04 14:49:40 +02:00
*
2017-05-04 22:46:37 +02:00
* Error messages should be added when implementing apply() and something goes
* wrong.
2016-04-04 14:49:40 +02:00
* In this case, apply() should return false.
*/
inline QStringList &OptionPage::errors()
{
return m_errors;
}
2015-04-22 18:57:44 +02:00
/*!
* \class Dialogs::UiFileBasedOptionPage
2017-05-04 22:46:37 +02:00
* \brief The UiFileBasedOptionPage class is the base class for SettingsDialog
* pages using UI files
2015-04-22 18:57:44 +02:00
* to describe the widget tree.
*
* \tparam UiClass Specifies the UI class generated by uic.
*/
2017-05-01 03:16:25 +02:00
template <class UiClass> class QT_UTILITIES_EXPORT UiFileBasedOptionPage : public OptionPage {
2015-04-22 18:57:44 +02:00
public:
explicit UiFileBasedOptionPage(QWidget *parentWindow = nullptr);
2018-10-10 21:12:58 +02:00
~UiFileBasedOptionPage() override;
2015-04-22 18:57:44 +02:00
2018-10-10 21:12:58 +02:00
bool apply() override = 0;
void reset() override = 0;
2015-04-22 18:57:44 +02:00
protected:
2018-10-10 21:12:58 +02:00
QWidget *setupWidget() override;
2015-04-22 18:57:44 +02:00
UiClass *ui();
private:
std::unique_ptr<UiClass> m_ui;
};
/*!
* \brief Constructs a new UI file based option page.
*/
template <class UiClass>
2017-05-01 03:16:25 +02:00
UiFileBasedOptionPage<UiClass>::UiFileBasedOptionPage(QWidget *parentWindow)
: OptionPage(parentWindow)
{
}
2015-04-22 18:57:44 +02:00
/*!
* \brief Destroys the option page.
*/
2017-05-01 03:16:25 +02:00
template <class UiClass> UiFileBasedOptionPage<UiClass>::~UiFileBasedOptionPage()
{
}
2015-04-22 18:57:44 +02:00
/*!
* \brief Inflates the widget for the option page using the UI class.
2015-04-22 18:57:44 +02:00
*/
2017-05-01 03:16:25 +02:00
template <class UiClass> QWidget *UiFileBasedOptionPage<UiClass>::setupWidget()
2015-04-22 18:57:44 +02:00
{
QWidget *widget = new QWidget();
2017-05-01 03:16:25 +02:00
if (!m_ui) {
2015-04-22 18:57:44 +02:00
m_ui.reset(new UiClass);
}
m_ui->setupUi(widget);
return widget;
}
/*!
* \brief Provides the derived class access to the UI class.
*/
2017-05-01 03:16:25 +02:00
template <class UiClass> inline UiClass *UiFileBasedOptionPage<UiClass>::ui()
2015-04-22 18:57:44 +02:00
{
return m_ui.get();
}
} // namespace Dialogs
2015-04-22 18:57:44 +02:00
/*!
2017-05-04 22:46:37 +02:00
* \brief Declares a class inheriting from Dialogs::OptionPage in a convenient
* way.
* \remarks Must be closed with END_DECLARE_OPTION_PAGE.
*/
2017-05-01 03:16:25 +02:00
#define BEGIN_DECLARE_OPTION_PAGE(SomeClass) \
typedef ::Dialogs::OptionPage SomeClass##Base; \
class QT_UTILITIES_EXPORT SomeClass : public ::Dialogs::OptionPage { \
public: \
explicit SomeClass(QWidget *parentWidget = nullptr); \
2018-10-10 21:12:58 +02:00
~SomeClass() override; \
bool apply() override; \
void reset() override; \
\
private:
/*!
* \brief Declares a class inheriting from Dialogs::OptionPage in a convenient
* way.
* \remarks Must be closed with END_DECLARE_OPTION_PAGE.
*/
#define BEGIN_DECLARE_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
typedef ::Dialogs::OptionPage SomeClass##Base; \
class QT_UTILITIES_EXPORT SomeClass : public ::Dialogs::OptionPage { \
public: \
2018-10-10 21:12:58 +02:00
~SomeClass() override; \
bool apply() override; \
void reset() override; \
2017-05-01 03:16:25 +02:00
\
2016-04-04 14:49:40 +02:00
private:
/*!
2017-05-04 22:46:37 +02:00
* \brief Declares a class inheriting from Dialogs::UiFileBasedOptionPage in a
* convenient way.
* \remarks Must be closed with END_DECLARE_OPTION_PAGE.
*/
2017-05-01 03:16:25 +02:00
#define BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
namespace Ui { \
class SomeClass; \
} \
typedef ::Dialogs::UiFileBasedOptionPage<Ui::SomeClass> SomeClass##Base; \
class QT_UTILITIES_EXPORT SomeClass : public ::Dialogs::UiFileBasedOptionPage<Ui::SomeClass> { \
public: \
2018-10-10 21:12:58 +02:00
~SomeClass() override; \
bool apply() override; \
void reset() override; \
2017-05-01 03:16:25 +02:00
\
2016-04-04 14:49:40 +02:00
private:
/*!
2017-05-04 22:46:37 +02:00
* \brief Declares a class inheriting from Dialogs::UiFileBasedOptionPage in a
* convenient way.
* \remarks Must be closed with END_DECLARE_OPTION_PAGE.
*/
2017-05-01 03:16:25 +02:00
#define BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_CTOR(SomeClass) \
public: \
explicit SomeClass(QWidget *parentWidget = nullptr); \
\
private:
/*!
2017-05-04 22:46:37 +02:00
* \brief Must be used after BEGIN_DECLARE_OPTION_PAGE and
* BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE.
*/
2017-05-01 03:16:25 +02:00
#define END_DECLARE_OPTION_PAGE \
} \
;
2016-04-04 14:49:40 +02:00
/*!
2017-05-04 22:46:37 +02:00
* \brief Instantiates a class declared with
* BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE in a convenient way.
* \remarks Might be required when the class is used by another application.
*/
2017-05-01 03:16:25 +02:00
#define INSTANTIATE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
namespace Dialogs { \
template class UiFileBasedOptionPage<Ui::SomeClass>; \
}
/*!
2017-05-04 22:46:37 +02:00
* \brief Instantiates a class declared with
* BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE inside a given namespace in a
* convenient way.
* \remarks Might be required when the class is used by another application.
*/
2017-05-01 03:16:25 +02:00
#define INSTANTIATE_UI_FILE_BASED_OPTION_PAGE_NS(SomeNamespace, SomeClass) \
namespace Dialogs { \
2017-05-04 22:46:37 +02:00
template class UiFileBasedOptionPage<::SomeNamespace::Ui::SomeClass>; \
}
/*!
2017-05-04 22:46:37 +02:00
* \brief Declares external instantiation of class declared with
* BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE in a convenient way.
* \remarks Might be required when the class comes from an external library.
*/
2017-05-01 03:16:25 +02:00
#define DECLARE_EXTERN_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
namespace Dialogs { \
namespace Ui { \
class SomeClass; \
} \
extern template class UiFileBasedOptionPage<Ui::SomeClass>; \
}
/*!
2017-05-04 22:46:37 +02:00
* \brief Declares external instantiation of class declared with
* BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE inside a given namespace in a
* convenient way.
* \remarks Might be required when the class comes from an external library.
*/
2017-05-01 03:16:25 +02:00
#define DECLARE_EXTERN_UI_FILE_BASED_OPTION_PAGE_NS(SomeNamespace, SomeClass) \
namespace SomeNamespace { \
namespace Ui { \
class SomeClass; \
} \
} \
namespace Dialogs { \
2017-05-04 22:46:37 +02:00
extern template class UiFileBasedOptionPage<::SomeNamespace::Ui::SomeClass>; \
}
/*!
* \brief Declares the method setupWidget() in a convenient way.
2017-05-04 22:46:37 +02:00
* \remarks Can be used between BEGIN_DECLARE_OPTION_PAGE and
* END_DECLARE_OPTION_PAGE.
*/
2017-05-01 03:16:25 +02:00
#define DECLARE_SETUP_WIDGETS \
protected: \
2018-10-10 21:12:58 +02:00
QWidget *setupWidget() override; \
2017-05-01 03:16:25 +02:00
\
private:
2016-04-04 14:49:40 +02:00
/*!
2017-05-04 22:46:37 +02:00
* \brief Declares a class inheriting from Dialogs::OptionPage in a convenient
* way.
* \remarks Doesn't allow to declare additional class members.
*/
2017-05-01 03:16:25 +02:00
#define DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
2016-04-04 14:49:40 +02:00
END_DECLARE_OPTION_PAGE
2016-08-27 15:18:44 +02:00
/*!
2017-05-04 22:46:37 +02:00
* \brief Declares a class inheriting from Dialogs::OptionPage in a convenient
* way.
2016-08-27 15:18:44 +02:00
* \remarks Doesn't allow to declare additional class members.
*/
2017-05-01 03:16:25 +02:00
#define DECLARE_OPTION_PAGE(SomeClass) \
BEGIN_DECLARE_OPTION_PAGE(SomeClass) \
DECLARE_SETUP_WIDGETS \
2016-08-27 15:18:44 +02:00
END_DECLARE_OPTION_PAGE
/*!
2017-05-04 22:46:37 +02:00
* \brief Declares a class inheriting from Dialogs::UiFileBasedOptionPage in a
* convenient way.
* \remarks Doesn't allow to declare additional class members.
*/
2017-05-01 03:16:25 +02:00
#define DECLARE_UI_FILE_BASED_OPTION_PAGE_CUSTOM_SETUP(SomeClass) \
BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE(SomeClass) \
DECLARE_SETUP_WIDGETS \
2016-04-04 14:49:40 +02:00
END_DECLARE_OPTION_PAGE
#endif // DIALOGS_OPTIONSPAGE_H