From 398232246e10f1b530b095f6e26bb9dd11376401 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Wed, 14 Oct 2015 21:58:03 +0200 Subject: [PATCH] TabStackedWidget: Only send show event to one widget after removing tab When removing current tab, the widget was removed from stackwidget, which would send the show event to next widget in stack and then send show event to another widget when changing the current index from ComboTabBar. --- src/lib/tabwidget/tabstackedwidget.cpp | 58 ++++++++++++++++++++++++++ src/lib/tabwidget/tabstackedwidget.h | 4 ++ 2 files changed, 62 insertions(+) diff --git a/src/lib/tabwidget/tabstackedwidget.cpp b/src/lib/tabwidget/tabstackedwidget.cpp index e1b3b6607..d0de72abb 100644 --- a/src/lib/tabwidget/tabstackedwidget.cpp +++ b/src/lib/tabwidget/tabstackedwidget.cpp @@ -32,6 +32,8 @@ TabStackedWidget::TabStackedWidget(QWidget* parent) : QWidget(parent) , m_stack(0) , m_tabBar(0) + , m_currentIndex(-1) + , m_previousIndex(-1) { m_stack = new QStackedWidget(this); m_mainLayout = new QVBoxLayout; @@ -92,6 +94,16 @@ void TabStackedWidget::tabWasMoved(int from, int to) void TabStackedWidget::tabWasRemoved(int index) { + if (m_previousIndex == index) + m_previousIndex = -1; + else if (m_previousIndex > index) + --m_previousIndex; + + if (m_currentIndex == index) + m_currentIndex = -1; + else if (m_currentIndex > index) + --m_currentIndex; + m_tabBar->removeTab(index); } @@ -167,6 +179,9 @@ void TabStackedWidget::showTab(int index) m_stack->setCurrentIndex(index); } + m_previousIndex = m_currentIndex; + m_currentIndex = index; + // This is slot connected to ComboTabBar::currentChanged // We must send the signal even with invalid index (-1) emit currentChanged(index); @@ -205,6 +220,12 @@ int TabStackedWidget::insertTab(int index, QWidget* w, const QString &label, boo index = m_stack->insertWidget(index, w); m_tabBar->insertTab(index, QIcon(), label, false); } + + if (m_previousIndex >= index) + ++m_previousIndex; + if (m_currentIndex >= index) + ++m_currentIndex; + QTimer::singleShot(0, this, SLOT(setUpLayout())); return index; @@ -266,6 +287,9 @@ int TabStackedWidget::pinUnPinTab(int index, const QString &title) void TabStackedWidget::removeTab(int index) { if (QWidget* w = m_stack->widget(index)) { + // Select another current tab before remove, so it won't be handled by QTabBar + if (index == currentIndex() && count() > 1) + selectTabOnRemove(); m_stack->removeWidget(w); } } @@ -309,3 +333,37 @@ bool TabStackedWidget::validIndex(int index) const { return (index < m_stack->count() && index >= 0); } + +void TabStackedWidget::selectTabOnRemove() +{ + Q_ASSERT(count() > 1); + + int index = -1; + + switch (m_tabBar->selectionBehaviorOnRemove()) { + case QTabBar::SelectPreviousTab: + if (validIndex(m_previousIndex)) { + index = m_previousIndex; + break; + } + // fallthrough + + case QTabBar::SelectLeftTab: + index = currentIndex() - 1; + if (!validIndex(index)) + index = 1; + break; + + case QTabBar::SelectRightTab: + index = currentIndex() + 1; + if (!validIndex(index)) + index = currentIndex() - 1; + break; + + default: + break; + } + + Q_ASSERT(validIndex(index)); + setCurrentIndex(index); +} diff --git a/src/lib/tabwidget/tabstackedwidget.h b/src/lib/tabwidget/tabstackedwidget.h index 48be6926e..1e1037cc4 100644 --- a/src/lib/tabwidget/tabstackedwidget.h +++ b/src/lib/tabwidget/tabstackedwidget.h @@ -81,11 +81,15 @@ protected: private: bool validIndex(int index) const; + void selectTabOnRemove(); QStackedWidget* m_stack; ComboTabBar* m_tabBar; QVBoxLayout* m_mainLayout; bool m_dirtyTabBar; + + int m_currentIndex; + int m_previousIndex; }; #endif // TABSTACKEDWIDGET_H