| 1 | #include "./optioncategory.h" |
| 2 | #include "./optionpage.h" |
| 3 | |
| 4 | #include <QCoreApplication> |
| 5 | #include <QEvent> |
| 6 | |
| 7 | namespace QtUtilities { |
| 8 | |
| 9 | /*! |
| 10 | * \class OptionCategory |
| 11 | * \brief The OptionCategory class wraps associated option pages. |
| 12 | */ |
| 13 | |
| 14 | /*! |
| 15 | * \brief Constructs a option category. |
| 16 | */ |
| 17 | OptionCategory::OptionCategory(QObject *parent) |
| 18 | : QObject(parent) |
| 19 | , m_currentIndex(0) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | /*! |
| 24 | * \brief Destroys the option category. |
| 25 | */ |
| 26 | OptionCategory::~OptionCategory() |
| 27 | { |
| 28 | qDeleteAll(m_pages); |
| 29 | } |
| 30 | |
| 31 | /*! |
| 32 | * \brief Applies all pages. |
| 33 | * \remarks Pages which have not been shown yet must have not been initialized anyways |
| 34 | * and hence are skipped. |
| 35 | * \sa OptionPage::apply() |
| 36 | */ |
| 37 | bool OptionCategory::applyAllPages() |
| 38 | { |
| 39 | for (OptionPage *page : m_pages) { |
| 40 | if (!page->hasBeenShown()) { |
| 41 | continue; |
| 42 | } |
| 43 | if (!page->apply()) { |
| 44 | return false; |
| 45 | } |
| 46 | } |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | /*! |
| 51 | * \brief Resets all pages. |
| 52 | * \remarks Pages which have not been shown yet must have not been initialized anyways |
| 53 | * and hence are skipped. |
| 54 | * \sa OptionPage::reset() |
| 55 | */ |
| 56 | void OptionCategory::resetAllPages() |
| 57 | { |
| 58 | for (OptionPage *page : m_pages) { |
| 59 | if (page->hasBeenShown()) { |
| 60 | page->reset(); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /*! |
| 66 | * \brief Triggers retranslation of all pages. |
| 67 | * \remarks Has no effect if the pages don't react to the LanguageChange event. |
| 68 | */ |
| 69 | void OptionCategory::retranslateAllPages() |
| 70 | { |
| 71 | auto event = QEvent(QEvent::LanguageChange); |
| 72 | for (auto *const page : m_pages) { |
| 73 | if (page->hasBeenShown()) { |
| 74 | QCoreApplication::sendEvent(page->widget(), &event); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /*! |
| 80 | * \brief Returns whether the option category matches the specified \a |
| 81 | * searchKeyWord. |
| 82 | */ |
| 83 | bool OptionCategory::matches(const QString &searchKeyWord) const |
| 84 | { |
| 85 | for (OptionPage *page : m_pages) { |
| 86 | if (page->matches(searchKeyWord)) { |
| 87 | return true; |
| 88 | } |
| 89 | } |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | /*! |
| 94 | * \brief Assigns the specified \a pages to the category. |
| 95 | * |
| 96 | * Previously assigned pages get deleted. The pagesChanged() signal is emitted. |
| 97 | * The category takes ownership over the given \a pages. |
| 98 | */ |
| 99 | void OptionCategory::assignPages(const QList<OptionPage *> &pages) |
| 100 | { |
| 101 | qDeleteAll(m_pages); |
| 102 | emit pagesChanged(m_pages = pages); |
| 103 | } |
| 104 | |
| 105 | /*! |
| 106 | * \fn OptionCategory::displayNameChanged() |
| 107 | * \brief Emitted when the display name changed. |
| 108 | */ |
| 109 | |
| 110 | /*! |
| 111 | * \fn OptionCategory::iconChanged() |
| 112 | * \brief Emitted when the icon changed. |
| 113 | */ |
| 114 | |
| 115 | /*! |
| 116 | * \fn OptionCategory::pagesChanged() |
| 117 | * \brief Emitted when the pages changed. |
| 118 | */ |
| 119 | } // namespace QtUtilities |