From ceebda7431eb91070309a12ee28659c41c0b8c1e Mon Sep 17 00:00:00 2001 From: Uwe Kindler Date: Fri, 12 Oct 2018 10:41:19 +0200 Subject: [PATCH] Properly implemented tab removal in new DockAreaTabBar class --- src/DockAreaTabBar.cpp | 121 ++++++++++++++++++++++++++++++----------- src/DockAreaWidget.cpp | 2 +- 2 files changed, 89 insertions(+), 34 deletions(-) diff --git a/src/DockAreaTabBar.cpp b/src/DockAreaTabBar.cpp index 75478a9..00dc6e7 100644 --- a/src/DockAreaTabBar.cpp +++ b/src/DockAreaTabBar.cpp @@ -46,6 +46,12 @@ struct DockAreaTabBarPrivate * Private data constructor */ DockAreaTabBarPrivate(CDockAreaTabBar* _public); + + /** + * Update tabs after current index changed or when tabs are removed. + * The function reassigns the stylesheet to update the tabs + */ + void updateTabs(); }; // struct DockAreaTabBarPrivate @@ -56,6 +62,33 @@ DockAreaTabBarPrivate::DockAreaTabBarPrivate(CDockAreaTabBar* _public) : } + +//============================================================================ +void DockAreaTabBarPrivate::updateTabs() +{ + // Set active TAB and update all other tabs to be inactive + for (int i = 0; i < _this->count(); ++i) + { + auto TabWidget = _this->tab(i); + if (!TabWidget) + { + continue; + } + + if (i == CurrentIndex) + { + TabWidget->show(); + TabWidget->setActiveTab(true); + _this->ensureWidgetVisible(TabWidget); + } + else + { + TabWidget->setActiveTab(false); + } + } +} + + //============================================================================ CDockAreaTabBar::CDockAreaTabBar(CDockAreaWidget* parent) : QScrollArea(parent), @@ -212,35 +245,8 @@ void CDockAreaTabBar::setCurrentIndex(int index) } emit currentChanging(index); - - // Set active TAB and update all other tabs to be inactive - for (int i = 0; i < count(); ++i) - { - QLayoutItem* item = d->TabsLayout->itemAt(i); - if (!item->widget()) - { - continue; - } - - auto TabWidget = dynamic_cast(item->widget()); - if (!TabWidget) - { - continue; - } - - if (i == index) - { - TabWidget->show(); - TabWidget->setActiveTab(true); - ensureWidgetVisible(TabWidget); - } - else - { - TabWidget->setActiveTab(false); - } - } - d->CurrentIndex = index; + d->updateTabs(); emit currentChanged(index); } @@ -262,7 +268,7 @@ void CDockAreaTabBar::insertTab(int Index, CDockWidgetTab* Tab) d->MenuOutdated = true; if (Index <= d->CurrentIndex) { - d->CurrentIndex++; + setCurrentIndex(d->CurrentIndex++); } } @@ -270,10 +276,61 @@ void CDockAreaTabBar::insertTab(int Index, CDockWidgetTab* Tab) //=========================================================================== void CDockAreaTabBar::removeTab(CDockWidgetTab* Tab) { - std::cout << "CDockAreaTabBar::removeTab " << std::endl; + if (!count()) + { + return; + } + qDebug() << "CDockAreaTabBar::removeTab "; + int NewCurrentIndex = currentIndex(); + int RemoveIndex = d->TabsLayout->indexOf(Tab); + if (count() == 1) + { + NewCurrentIndex = -1; + } + if (NewCurrentIndex > RemoveIndex) + { + NewCurrentIndex--; + } + else if (NewCurrentIndex == RemoveIndex) + { + NewCurrentIndex = -1; + // First we walk to the right to search for the next visible tab + for (int i = (RemoveIndex + 1); i < count(); ++i) + { + if (tab(i)->isVisibleTo(this)) + { + NewCurrentIndex = i - 1; + break; + } + } + + // If there is no visible tab right to this tab then we walk to + // the left to find a visible tab + if (NewCurrentIndex < 0) + { + for (int i = (RemoveIndex - 1); i >= 0; --i) + { + if (tab(i)->isVisibleTo(this)) + { + NewCurrentIndex = i; + break; + } + } + } + } + d->TabsLayout->removeWidget(Tab); Tab->disconnect(this); d->MenuOutdated = true; + qDebug() << "NewCurrentIndex " << NewCurrentIndex; + if (NewCurrentIndex != d->CurrentIndex) + { + setCurrentIndex(NewCurrentIndex); + } + else + { + d->updateTabs(); + } } @@ -306,7 +363,6 @@ void CDockAreaTabBar::onTabClicked() return; } setCurrentIndex(index); - std::cout << "emit tabBarClicked " << index << std::endl; emit tabBarClicked(index); } @@ -377,9 +433,8 @@ void CDockAreaTabBar::onTabWidgetMoved(const QPoint& GlobalPos) if (toIndex >= 0) { qDebug() << "tabMoved from " << fromIndex << " to " << toIndex; - d->CurrentIndex = toIndex; emit tabMoved(fromIndex, toIndex); - emit currentChanged(toIndex); + setCurrentIndex(toIndex); } } diff --git a/src/DockAreaWidget.cpp b/src/DockAreaWidget.cpp index 9815126..aa7f8d8 100644 --- a/src/DockAreaWidget.cpp +++ b/src/DockAreaWidget.cpp @@ -775,7 +775,7 @@ void CDockAreaWidget::reorderDockWidget(int fromIndex, int toIndex) auto Widget = d->ContentsLayout->widget(fromIndex); d->ContentsLayout->removeWidget(Widget); d->ContentsLayout->insertWidget(toIndex, Widget); - setCurrentIndex(d->TabBar->currentIndex()); + setCurrentIndex(toIndex); }