| 1 | #include "./optionpage.h" |
| 2 | |
| 3 | #include <QCheckBox> |
| 4 | #include <QEvent> |
| 5 | #include <QGroupBox> |
| 6 | #include <QLabel> |
| 7 | #include <QPushButton> |
| 8 | #include <QRadioButton> |
| 9 | |
| 10 | #include <utility> |
| 11 | |
| 12 | namespace QtUtilities { |
| 13 | |
| 14 | /*! |
| 15 | * \class OptionPage |
| 16 | * \brief The OptionPage class is the base class for SettingsDialog pages. |
| 17 | * |
| 18 | * The specified \a parentWindow might be used by some implementations as parent |
| 19 | * when showing dialogs. |
| 20 | */ |
| 21 | |
| 22 | /*! |
| 23 | * \brief Constructs a option page. |
| 24 | */ |
| 25 | OptionPage::OptionPage(QWidget *parentWindow) |
| 26 | : m_parentWindow(parentWindow) |
| 27 | , m_shown(false) |
| 28 | , m_keywordsInitialized(false) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | /*! |
| 33 | * \brief Destroys the option page. |
| 34 | */ |
| 35 | OptionPage::~OptionPage() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | /*! |
| 40 | * \brief Returns the widget for the option page. |
| 41 | * |
| 42 | * If the widget has not been constructed yet, a new widget will be |
| 43 | * constructed using the OptionPage::setupWidget() method and the |
| 44 | * current configuration is applied. |
| 45 | * |
| 46 | * The option page keeps ownership over the returned widget. |
| 47 | */ |
| 48 | QWidget *OptionPage::widget() |
| 49 | { |
| 50 | if (!m_widget) { |
| 51 | m_widget.reset(setupWidget()); // ensure widget has been created |
| 52 | } |
| 53 | if (!m_shown) { |
| 54 | m_shown = true; |
| 55 | reset(); // show current configuration if not shown yet |
| 56 | } |
| 57 | return m_widget.get(); |
| 58 | } |
| 59 | |
| 60 | /*! |
| 61 | * \brief Returns whether the pages matches the specified |
| 62 | * \a searchKeyWord. |
| 63 | */ |
| 64 | bool OptionPage::matches(const QString &searchKeyWord) |
| 65 | { |
| 66 | if (searchKeyWord.isEmpty()) { |
| 67 | return true; |
| 68 | } |
| 69 | if (!m_keywordsInitialized) { |
| 70 | if (!m_widget) { |
| 71 | m_widget.reset(setupWidget()); // ensure widget has been created |
| 72 | } |
| 73 | m_keywords << m_widget->windowTitle(); |
| 74 | // find common subwidgets |
| 75 | for (const QLabel *label : m_widget->findChildren<QLabel *>()) |
| 76 | m_keywords << label->text(); |
| 77 | for (const QCheckBox *checkbox : m_widget->findChildren<QCheckBox *>()) |
| 78 | m_keywords << checkbox->text(); |
| 79 | for (const QRadioButton *checkbox : m_widget->findChildren<QRadioButton *>()) |
| 80 | m_keywords << checkbox->text(); |
| 81 | for (const QPushButton *pushButton : m_widget->findChildren<QPushButton *>()) |
| 82 | m_keywords << pushButton->text(); |
| 83 | for (const QGroupBox *groupBox : m_widget->findChildren<QGroupBox *>()) |
| 84 | m_keywords << groupBox->title(); |
| 85 | m_keywordsInitialized = true; |
| 86 | } |
| 87 | for (const QString &keyword : std::as_const(m_keywords)) |
| 88 | if (keyword.contains(searchKeyWord, Qt::CaseInsensitive)) |
| 89 | return true; |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | /*! |
| 94 | * \brief Emits the paletteChanged() signal. |
| 95 | */ |
| 96 | bool OptionPageWidget::event(QEvent *event) |
| 97 | { |
| 98 | switch (event->type()) { |
| 99 | case QEvent::PaletteChange: |
| 100 | emit paletteChanged(); |
| 101 | break; |
| 102 | case QEvent::LanguageChange: |
| 103 | emit retranslationRequired(); |
| 104 | break; |
| 105 | default:; |
| 106 | } |
| 107 | return QWidget::event(event); |
| 108 | } |
| 109 | |
| 110 | /*! |
| 111 | * \fn OptionPage::apply() |
| 112 | * \brief Applies altered settings. |
| 113 | * \remarks |
| 114 | * The SettingsDialog and any other classes/functions of this library will not call |
| 115 | * this method if the option page has not been shown yet. Hence it is (no longer) necessary |
| 116 | * to use OptionPage::hasBeenShown() to check whether the page has been initialized |
| 117 | * yet. |
| 118 | */ |
| 119 | |
| 120 | /*! |
| 121 | * \fn OptionPage::reset() |
| 122 | * \brief Discards altered settings and resets relevant widgets. |
| 123 | * \remarks |
| 124 | * The SettingsDialog and any other classes/functions of this library will not call |
| 125 | * this method if the option page has not been shown yet. Hence it is (no longer) necessary |
| 126 | * to use OptionPage::hasBeenShown() to check whether the page has been initialized |
| 127 | * yet. |
| 128 | */ |
| 129 | |
| 130 | /*! |
| 131 | * \fn OptionPage::setupWidget() |
| 132 | * \brief Creates the widget for the page. Called on the first invocation of |
| 133 | * widget(). |
| 134 | */ |
| 135 | } // namespace QtUtilities |