add basic hiding of SettingsTabs

This commit is contained in:
fourtf 2019-09-02 16:39:21 +02:00
parent 54c26d2228
commit b0459ba646
5 changed files with 51 additions and 21 deletions

View file

@ -67,13 +67,31 @@ void SettingsDialog::initUi()
auto edit = title.emplace<QLineEdit>().assign(&this->ui_.search); auto edit = title.emplace<QLineEdit>().assign(&this->ui_.search);
QTimer::singleShot(100, edit.getElement(), QTimer::singleShot(100, edit.getElement(),
[edit = edit.getElement()]() { edit->setFocus(); }); [edit = edit.getElement()]() { edit->setFocus(); });
QObject::connect(edit.getElement(), &QLineEdit::textChanged, this, QObject::connect(
[this](const QString &text) { edit.getElement(), &QLineEdit::textChanged, this,
for (auto &&page : this->pages_) [this](const QString &text) {
{ // filter elements and hide pages
page->filterElements(text); 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<QStackedLayout>() right.emplace<QStackedLayout>()
.assign(&this->ui_.pageStack) .assign(&this->ui_.pageStack)

View file

@ -138,8 +138,10 @@ void SettingsLayout::addSeperator()
this->addWidget(new Line(false)); this->addWidget(new Line(false));
} }
void SettingsLayout::filterElements(const QString &query) bool SettingsLayout::filterElements(const QString &query)
{ {
bool any{};
for (auto &&group : this->groups_) for (auto &&group : this->groups_)
{ {
// if group name matches then all should be visible // if group name matches then all should be visible
@ -152,7 +154,7 @@ void SettingsLayout::filterElements(const QString &query)
// check if any match // check if any match
else else
{ {
auto any = false; auto groupAny = false;
for (auto &&widget : group.widgets) for (auto &&widget : group.widgets)
{ {
@ -161,7 +163,7 @@ void SettingsLayout::filterElements(const QString &query)
if (keyword.contains(query, Qt::CaseInsensitive)) if (keyword.contains(query, Qt::CaseInsensitive))
{ {
widget.element->show(); widget.element->show();
any = true; groupAny = true;
} }
else else
{ {
@ -170,9 +172,11 @@ void SettingsLayout::filterElements(const QString &query)
} }
} }
group.title->setVisible(any); group.title->setVisible(groupAny);
} }
} }
return any;
} }
GeneralPage::GeneralPage() GeneralPage::GeneralPage()
@ -199,10 +203,13 @@ GeneralPage::GeneralPage()
this->initExtra(); this->initExtra();
} }
void GeneralPage::filterElements(const QString &query) bool GeneralPage::filterElements(const QString &query)
{ {
if (this->settingsLayout_) 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) void GeneralPage::initLayout(SettingsLayout &layout)

View file

@ -131,7 +131,7 @@ public:
DescriptionLabel *addDescription(const QString &text); DescriptionLabel *addDescription(const QString &text);
void addSeperator(); void addSeperator();
void filterElements(const QString &query); bool filterElements(const QString &query);
private: private:
struct Widget { struct Widget {
@ -156,7 +156,7 @@ class GeneralPage : public SettingsPage
public: public:
GeneralPage(); GeneralPage();
void filterElements(const QString &query); bool filterElements(const QString &query);
private: private:
void initLayout(SettingsLayout &layout); void initLayout(SettingsLayout &layout);

View file

@ -9,11 +9,14 @@
namespace chatterino { namespace chatterino {
void filterItemsRec(QObject *object, const QString &query) bool filterItemsRec(QObject *object, const QString &query)
{ {
bool any{};
for (auto &&child : object->children()) for (auto &&child : object->children())
{ {
auto setOpacity = [=](auto *widget, bool condition) { auto setOpacity = [&](auto *widget, bool condition) {
any |= condition;
widget->greyedOut = !condition; widget->greyedOut = !condition;
widget->update(); widget->update();
}; };
@ -39,9 +42,10 @@ void filterItemsRec(QObject *object, const QString &query)
} }
else else
{ {
filterItemsRec(child, query); any |= filterItemsRec(child, query);
} }
} }
return any;
} }
SettingsPage::SettingsPage(const QString &name, const QString &iconResource) 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() const QString &SettingsPage::getName()

View file

@ -49,7 +49,7 @@ public:
const QString &getName(); const QString &getName();
const QString &getIconResource(); const QString &getIconResource();
virtual void filterElements(const QString &query); virtual bool filterElements(const QString &query);
SettingsDialogTab *tab() const; SettingsDialogTab *tab() const;
void setTab(SettingsDialogTab *tab); void setTab(SettingsDialogTab *tab);