From b0459ba646c789a06890e294827c32f8f70f39dc Mon Sep 17 00:00:00 2001 From: fourtf Date: Mon, 2 Sep 2019 16:39:21 +0200 Subject: [PATCH] add basic hiding of SettingsTabs --- src/widgets/dialogs/SettingsDialog.cpp | 32 +++++++++++++++++----- src/widgets/settingspages/GeneralPage.cpp | 19 +++++++++---- src/widgets/settingspages/GeneralPage.hpp | 4 +-- src/widgets/settingspages/SettingsPage.cpp | 15 ++++++---- src/widgets/settingspages/SettingsPage.hpp | 2 +- 5 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/widgets/dialogs/SettingsDialog.cpp b/src/widgets/dialogs/SettingsDialog.cpp index 2474ac26a..3a695eb89 100644 --- a/src/widgets/dialogs/SettingsDialog.cpp +++ b/src/widgets/dialogs/SettingsDialog.cpp @@ -67,13 +67,31 @@ void SettingsDialog::initUi() auto edit = title.emplace().assign(&this->ui_.search); QTimer::singleShot(100, edit.getElement(), [edit = edit.getElement()]() { edit->setFocus(); }); - QObject::connect(edit.getElement(), &QLineEdit::textChanged, this, - [this](const QString &text) { - for (auto &&page : this->pages_) - { - page->filterElements(text); - } - }); + QObject::connect( + edit.getElement(), &QLineEdit::textChanged, this, + [this](const QString &text) { + // filter elements and hide pages + for (auto &&page : this->pages_) + { + // filterElements returns true if anything on the page matches the search query + page->tab()->setVisible(page->filterElements(text)); + } + + // TODO: add originally selected page + + // find next visible page + if (!this->selectedTab_->isVisible()) + { + for (auto &&tab : this->tabs_) + { + if (tab->isVisible()) + { + this->selectTab(tab); + break; + } + } + } + }); right.emplace() .assign(&this->ui_.pageStack) diff --git a/src/widgets/settingspages/GeneralPage.cpp b/src/widgets/settingspages/GeneralPage.cpp index 57f25a9c1..5c3b79e72 100644 --- a/src/widgets/settingspages/GeneralPage.cpp +++ b/src/widgets/settingspages/GeneralPage.cpp @@ -138,8 +138,10 @@ void SettingsLayout::addSeperator() this->addWidget(new Line(false)); } -void SettingsLayout::filterElements(const QString &query) +bool SettingsLayout::filterElements(const QString &query) { + bool any{}; + for (auto &&group : this->groups_) { // if group name matches then all should be visible @@ -152,7 +154,7 @@ void SettingsLayout::filterElements(const QString &query) // check if any match else { - auto any = false; + auto groupAny = false; for (auto &&widget : group.widgets) { @@ -161,7 +163,7 @@ void SettingsLayout::filterElements(const QString &query) if (keyword.contains(query, Qt::CaseInsensitive)) { widget.element->show(); - any = true; + groupAny = true; } else { @@ -170,9 +172,11 @@ void SettingsLayout::filterElements(const QString &query) } } - group.title->setVisible(any); + group.title->setVisible(groupAny); } } + + return any; } GeneralPage::GeneralPage() @@ -199,10 +203,13 @@ GeneralPage::GeneralPage() this->initExtra(); } -void GeneralPage::filterElements(const QString &query) +bool GeneralPage::filterElements(const QString &query) { if (this->settingsLayout_) - this->settingsLayout_->filterElements(query); + return this->settingsLayout_->filterElements(query) || + this->name_.contains(query) || query.isEmpty(); + else + return false; } void GeneralPage::initLayout(SettingsLayout &layout) diff --git a/src/widgets/settingspages/GeneralPage.hpp b/src/widgets/settingspages/GeneralPage.hpp index 83381cf1a..4e0c1e261 100644 --- a/src/widgets/settingspages/GeneralPage.hpp +++ b/src/widgets/settingspages/GeneralPage.hpp @@ -131,7 +131,7 @@ public: DescriptionLabel *addDescription(const QString &text); void addSeperator(); - void filterElements(const QString &query); + bool filterElements(const QString &query); private: struct Widget { @@ -156,7 +156,7 @@ class GeneralPage : public SettingsPage public: GeneralPage(); - void filterElements(const QString &query); + bool filterElements(const QString &query); private: void initLayout(SettingsLayout &layout); diff --git a/src/widgets/settingspages/SettingsPage.cpp b/src/widgets/settingspages/SettingsPage.cpp index 4b0d147f1..0d7d8d129 100644 --- a/src/widgets/settingspages/SettingsPage.cpp +++ b/src/widgets/settingspages/SettingsPage.cpp @@ -9,11 +9,14 @@ namespace chatterino { -void filterItemsRec(QObject *object, const QString &query) +bool filterItemsRec(QObject *object, const QString &query) { + bool any{}; + for (auto &&child : object->children()) { - auto setOpacity = [=](auto *widget, bool condition) { + auto setOpacity = [&](auto *widget, bool condition) { + any |= condition; widget->greyedOut = !condition; widget->update(); }; @@ -39,9 +42,10 @@ void filterItemsRec(QObject *object, const QString &query) } else { - filterItemsRec(child, query); + any |= filterItemsRec(child, query); } } + return any; } SettingsPage::SettingsPage(const QString &name, const QString &iconResource) @@ -50,9 +54,10 @@ SettingsPage::SettingsPage(const QString &name, const QString &iconResource) { } -void SettingsPage::filterElements(const QString &query) +bool SettingsPage::filterElements(const QString &query) { - filterItemsRec(this, query); + return filterItemsRec(this, query) || query.isEmpty() || + this->name_.contains(query); } const QString &SettingsPage::getName() diff --git a/src/widgets/settingspages/SettingsPage.hpp b/src/widgets/settingspages/SettingsPage.hpp index 1678b34f8..d25cad819 100644 --- a/src/widgets/settingspages/SettingsPage.hpp +++ b/src/widgets/settingspages/SettingsPage.hpp @@ -49,7 +49,7 @@ public: const QString &getName(); const QString &getIconResource(); - virtual void filterElements(const QString &query); + virtual bool filterElements(const QString &query); SettingsDialogTab *tab() const; void setTab(SettingsDialogTab *tab);