From 27edfe63ee5bba5264953fcf5784f18033fe9a58 Mon Sep 17 00:00:00 2001 From: Uwe Kindler Date: Thu, 22 Jun 2023 14:45:29 +0200 Subject: [PATCH] Fixed issue #524: Sometimes sidebar visibility state is incorrect --- demo/MainWindow.cpp | 2 +- src/AutoHideSideBar.cpp | 75 +++++++++++++++++++++++++++-------------- src/AutoHideSideBar.h | 13 +++++++ 3 files changed, 63 insertions(+), 27 deletions(-) diff --git a/demo/MainWindow.cpp b/demo/MainWindow.cpp index 61f9874..fb0a51b 100644 --- a/demo/MainWindow.cpp +++ b/demo/MainWindow.cpp @@ -734,7 +734,7 @@ CMainWindow::CMainWindow(QWidget *parent) : CDockManager::setConfigFlag(CDockManager::FocusHighlighting, true); // uncomment if you would like to enable dock widget auto hiding - CDockManager::setAutoHideConfigFlags({CDockManager::DefaultAutoHideConfig | CDockManager::AutoHideCloseButtonCollapsesDock}); + CDockManager::setAutoHideConfigFlags({CDockManager::DefaultAutoHideConfig}); // uncomment if you would like to enable an equal distribution of the // available size of a splitter to all contained dock widgets diff --git a/src/AutoHideSideBar.cpp b/src/AutoHideSideBar.cpp index c64b079..f427ca3 100644 --- a/src/AutoHideSideBar.cpp +++ b/src/AutoHideSideBar.cpp @@ -127,27 +127,6 @@ void AutoHideSideBarPrivate::handleViewportEvent(QEvent* e) } break; - case QEvent::Resize: - if (_this->tabCount()) - { - auto ev = static_cast(e); - auto Tab = _this->tabAt(0); - int Size = isHorizontal() ? ev->size().height() : ev->size().width(); - int TabSize = isHorizontal() ? Tab->size().height() : Tab->size().width(); - // If the size of the side bar is less than the size of the first tab - // then there are no visible tabs in this side bar. This check will - // fail if someone will force a very big border via CSS!! - if (Size < TabSize) - { - _this->hide(); - } - } - else - { - _this->hide(); - } - break; - default: break; } @@ -288,20 +267,33 @@ void CAutoHideSideBar::removeTab(CAutoHideTab* SideTab) //============================================================================ bool CAutoHideSideBar::eventFilter(QObject *watched, QEvent *event) { - if (event->type() != QEvent::ShowToParent) + auto Tab = qobject_cast(watched); + if (!Tab) { return false; } - // As soon as on tab is shown, we need to show the side tab bar - auto Tab = qobject_cast(watched); - if (Tab) + switch (event->type()) { - show(); + case QEvent::ShowToParent: + show(); + break; + + case QEvent::Hide: + if (!hasVisibleTabs()) + { + hide(); + } + break; + + default: + break; } + return false; } + //============================================================================ Qt::Orientation CAutoHideSideBar::orientation() const { @@ -323,6 +315,37 @@ int CAutoHideSideBar::tabCount() const } +//============================================================================ +int CAutoHideSideBar::visibleTabCount() const +{ + int count = 0; + for (auto i = 0; i < tabCount(); i++) + { + if (tabAt(i)->isVisible()) + { + count++; + } + } + + return count; +} + + +//============================================================================ +bool CAutoHideSideBar::hasVisibleTabs() const +{ + for (auto i = 0; i < tabCount(); i++) + { + if (tabAt(i)->isVisible()) + { + return true; + } + } + + return false; +} + + //============================================================================ SideBarLocation CAutoHideSideBar::sideBarLocation() const { diff --git a/src/AutoHideSideBar.h b/src/AutoHideSideBar.h index 3575357..20a9e6a 100644 --- a/src/AutoHideSideBar.h +++ b/src/AutoHideSideBar.h @@ -134,6 +134,19 @@ public: */ int tabCount() const; + /** + * Returns the number of visible tabs + */ + int visibleTabCount() const; + + /** + * Returns true, if the sidebar contains visible tabs. + * The function returns as soon as it finds the first visible tab. + * That means, if you just want to find out if theee are visible tabs + * then this function is quicker than visibleTabCount() + */ + bool hasVisibleTabs() const; + /** * Getter for side tab bar area property */