1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

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.
This commit is contained in:
David Rosca 2015-10-14 21:58:03 +02:00
parent 5024ada696
commit 398232246e
2 changed files with 62 additions and 0 deletions

View File

@ -32,6 +32,8 @@ TabStackedWidget::TabStackedWidget(QWidget* parent)
: QWidget(parent) : QWidget(parent)
, m_stack(0) , m_stack(0)
, m_tabBar(0) , m_tabBar(0)
, m_currentIndex(-1)
, m_previousIndex(-1)
{ {
m_stack = new QStackedWidget(this); m_stack = new QStackedWidget(this);
m_mainLayout = new QVBoxLayout; m_mainLayout = new QVBoxLayout;
@ -92,6 +94,16 @@ void TabStackedWidget::tabWasMoved(int from, int to)
void TabStackedWidget::tabWasRemoved(int index) 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); m_tabBar->removeTab(index);
} }
@ -167,6 +179,9 @@ void TabStackedWidget::showTab(int index)
m_stack->setCurrentIndex(index); m_stack->setCurrentIndex(index);
} }
m_previousIndex = m_currentIndex;
m_currentIndex = index;
// This is slot connected to ComboTabBar::currentChanged // This is slot connected to ComboTabBar::currentChanged
// We must send the signal even with invalid index (-1) // We must send the signal even with invalid index (-1)
emit currentChanged(index); emit currentChanged(index);
@ -205,6 +220,12 @@ int TabStackedWidget::insertTab(int index, QWidget* w, const QString &label, boo
index = m_stack->insertWidget(index, w); index = m_stack->insertWidget(index, w);
m_tabBar->insertTab(index, QIcon(), label, false); 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())); QTimer::singleShot(0, this, SLOT(setUpLayout()));
return index; return index;
@ -266,6 +287,9 @@ int TabStackedWidget::pinUnPinTab(int index, const QString &title)
void TabStackedWidget::removeTab(int index) void TabStackedWidget::removeTab(int index)
{ {
if (QWidget* w = m_stack->widget(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); m_stack->removeWidget(w);
} }
} }
@ -309,3 +333,37 @@ bool TabStackedWidget::validIndex(int index) const
{ {
return (index < m_stack->count() && index >= 0); 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);
}

View File

@ -81,11 +81,15 @@ protected:
private: private:
bool validIndex(int index) const; bool validIndex(int index) const;
void selectTabOnRemove();
QStackedWidget* m_stack; QStackedWidget* m_stack;
ComboTabBar* m_tabBar; ComboTabBar* m_tabBar;
QVBoxLayout* m_mainLayout; QVBoxLayout* m_mainLayout;
bool m_dirtyTabBar; bool m_dirtyTabBar;
int m_currentIndex;
int m_previousIndex;
}; };
#endif // TABSTACKEDWIDGET_H #endif // TABSTACKEDWIDGET_H