From 2b9e2bd1b0553e0d3a62296f238355da8eeca873 Mon Sep 17 00:00:00 2001 From: pajlada Date: Sun, 2 Jan 2022 15:00:19 +0100 Subject: [PATCH] Notebook::select now takes the optional parameter "focusPage". (#3446) --- CHANGELOG.md | 1 + src/widgets/Notebook.cpp | 43 ++++++++++++++++-------------- src/widgets/Notebook.hpp | 12 ++++----- src/widgets/dialogs/EmotePopup.cpp | 2 +- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8d8cf310..e581f3cd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,6 +82,7 @@ - Dev: Renamed CMake's build option `USE_SYSTEM_QT5KEYCHAIN` to `USE_SYSTEM_QTKEYCHAIN`. (#3103) - Dev: Add benchmarks that can be compiled with the `BUILD_BENCHMARKS` CMake flag. Off by default. (#3038) - Dev: Added CMake build option `BUILD_WITH_QTKEYCHAIN` to build with or without Qt5Keychain support (On by default). (#3318) +- Dev: Notebook::select\* functions now take an optional `focusPage` parameter (true by default) which keeps the default behaviour of selecting the page after it has been selected. If set to false, the page is _not_ focused after being selected. (#3446) ## 2.3.4 diff --git a/src/widgets/Notebook.cpp b/src/widgets/Notebook.cpp index 2526ed058..0aa5baba5 100644 --- a/src/widgets/Notebook.cpp +++ b/src/widgets/Notebook.cpp @@ -131,7 +131,7 @@ int Notebook::indexOf(QWidget *page) const return -1; } -void Notebook::select(QWidget *page) +void Notebook::select(QWidget *page, bool focusPage) { if (page == this->selectedPage_) { @@ -148,20 +148,23 @@ void Notebook::select(QWidget *page) item.tab->setSelected(true); item.tab->raise(); - if (item.selectedWidget == nullptr) + if (focusPage) { - item.page->setFocus(); - } - else - { - if (containsChild(page, item.selectedWidget)) + if (item.selectedWidget == nullptr) { - item.selectedWidget->setFocus(Qt::MouseFocusReason); + item.page->setFocus(); } else { - qCDebug(chatterinoWidget) - << "Notebook: selected child of page doesn't exist anymore"; + if (containsChild(page, item.selectedWidget)) + { + item.selectedWidget->setFocus(Qt::MouseFocusReason); + } + else + { + qCDebug(chatterinoWidget) << "Notebook: selected child of " + "page doesn't exist anymore"; + } } } } @@ -216,17 +219,17 @@ bool Notebook::containsChild(const QObject *obj, const QObject *child) }); } -void Notebook::selectIndex(int index) +void Notebook::selectIndex(int index, bool focusPage) { if (index < 0 || this->items_.count() <= index) { return; } - this->select(this->items_[index].page); + this->select(this->items_[index].page, focusPage); } -void Notebook::selectNextTab() +void Notebook::selectNextTab(bool focusPage) { if (this->items_.size() <= 1) { @@ -236,10 +239,10 @@ void Notebook::selectNextTab() auto index = (this->indexOf(this->selectedPage_) + 1) % this->items_.count(); - this->select(this->items_[index].page); + this->select(this->items_[index].page, focusPage); } -void Notebook::selectPreviousTab() +void Notebook::selectPreviousTab(bool focusPage) { if (this->items_.size() <= 1) { @@ -253,10 +256,10 @@ void Notebook::selectPreviousTab() index += this->items_.count(); } - this->select(this->items_[index].page); + this->select(this->items_[index].page, focusPage); } -void Notebook::selectLastTab() +void Notebook::selectLastTab(bool focusPage) { const auto size = this->items_.size(); if (size <= 1) @@ -264,7 +267,7 @@ void Notebook::selectLastTab() return; } - this->select(this->items_[size - 1].page); + this->select(this->items_[size - 1].page, focusPage); } int Notebook::getPageCount() const @@ -806,7 +809,7 @@ SplitContainer *SplitNotebook::getOrAddSelectedPage() : this->addPage(); } -void SplitNotebook::select(QWidget *page) +void SplitNotebook::select(QWidget *page, bool focusPage) { if (auto selectedPage = this->getSelectedPage()) { @@ -818,7 +821,7 @@ void SplitNotebook::select(QWidget *page) } } } - this->Notebook::select(page); + this->Notebook::select(page, focusPage); } } // namespace chatterino diff --git a/src/widgets/Notebook.hpp b/src/widgets/Notebook.hpp index 153dd533c..9ccc5ede8 100644 --- a/src/widgets/Notebook.hpp +++ b/src/widgets/Notebook.hpp @@ -33,11 +33,11 @@ public: void removeCurrentPage(); int indexOf(QWidget *page) const; - virtual void select(QWidget *page); - void selectIndex(int index); - void selectNextTab(); - void selectPreviousTab(); - void selectLastTab(); + virtual void select(QWidget *page, bool focusPage = true); + void selectIndex(int index, bool focusPage = true); + void selectNextTab(bool focusPage = true); + void selectPreviousTab(bool focusPage = true); + void selectLastTab(bool focusPage = true); int getPageCount() const; QWidget *getPageAt(int index) const; @@ -108,7 +108,7 @@ public: SplitContainer *addPage(bool select = false); SplitContainer *getOrAddSelectedPage(); - void select(QWidget *page) override; + void select(QWidget *page, bool focusPage = true) override; protected: void showEvent(QShowEvent *event) override; diff --git a/src/widgets/dialogs/EmotePopup.cpp b/src/widgets/dialogs/EmotePopup.cpp index e6828fc6e..603f257b2 100644 --- a/src/widgets/dialogs/EmotePopup.cpp +++ b/src/widgets/dialogs/EmotePopup.cpp @@ -203,7 +203,7 @@ void EmotePopup::addShortcuts() int result = target.toInt(&ok); if (ok) { - this->notebook_->selectIndex(result); + this->notebook_->selectIndex(result, false); } else {