diff --git a/src/DockWidget.cpp b/src/DockWidget.cpp index 6320c274..977c66f9 100644 --- a/src/DockWidget.cpp +++ b/src/DockWidget.cpp @@ -273,6 +273,21 @@ bool DockWidget::isTabbed() const } } +bool DockWidget::isCurrentTab() const +{ + if (TabWidget* tabWidget = d->parentTabWidget()) { + return tabWidget->currentIndex() == tabWidget->indexOf(const_cast(this)); + } else { + return true; + } +} + +void DockWidget::setAsCurrentTab() +{ + if (TabWidget* tabWidget = d->parentTabWidget()) + tabWidget->setCurrentWidget(this); +} + bool DockWidget::event(QEvent *e) { if (e->type() == QEvent::ParentChange) { diff --git a/src/DockWidget.h b/src/DockWidget.h index 333f702a..1c7c39f8 100644 --- a/src/DockWidget.h +++ b/src/DockWidget.h @@ -162,6 +162,18 @@ public: */ bool isTabbed() const; + /** + * @brief Returns true if this dock widget is the current one in the tab widget that contains it. + * + * If the dock widget is alone then true is returned, as in this case there will also be a tab widget even though it's invisible. + */ + bool isCurrentTab() const; + + /** + * @brief Makes this dock widget current in its tab group. + */ + void setAsCurrentTab(); + Q_SIGNALS: ///@brief signal emitted when the parent changed void parentChanged(); diff --git a/tests/tst_docks.cpp b/tests/tst_docks.cpp index c4cc8100..ebf36576 100644 --- a/tests/tst_docks.cpp +++ b/tests/tst_docks.cpp @@ -246,6 +246,7 @@ private Q_SLOTS: void tst_addAndReadd(); void tst_placeholderCount(); void tst_availableLengthForOrientation(); + void tst_setAstCurrentTab(); private: void tst_restoreEmpty(); // TODO. Disabled for now, save/restore needs to support placeholders void tst_restoreCrash(); // TODO. Disabled for now, save/restore needs to support placeholders @@ -2192,6 +2193,33 @@ void TestDocks::tst_availableLengthForOrientation() QCOMPARE(availableHeight, layout->contentsHeight() - 2 *Anchor::thickness(true) - Anchor::thickness(false) - dock1MinHeight); } +void TestDocks::tst_setAstCurrentTab() +{ + EnsureTopLevelsDeleted e; + + // Tests DockWidget::setAsCurrentTab() and DockWidget::isCurrentTab() + // 1. a single dock widget is current, by definition + auto dock1 = createDockWidget(QStringLiteral("1"), new QPushButton(QStringLiteral("1"))); + QVERIFY(dock1->isCurrentTab()); + + // 2. Tab dock2 to the group, dock2 is current now + auto dock2 = createDockWidget(QStringLiteral("2"), new QPushButton(QStringLiteral("2"))); + dock1->addDockWidgetAsTab(dock2); + QVERIFY(!dock1->isCurrentTab()); + QVERIFY(dock2->isCurrentTab()); + + // 3. Set dock1 as current + dock1->setAsCurrentTab(); + QVERIFY(dock1->isCurrentTab()); + QVERIFY(!dock2->isCurrentTab()); + + auto fw = qobject_cast(dock1->window()); + QVERIFY(fw); + + delete dock1; delete dock2; + waitForDeleted(fw); +} + // QTest::qWait(50000) QTEST_MAIN(KDDockWidgets::TestDocks)