diff --git a/src/KDDockWidgets.h b/src/KDDockWidgets.h index 529f281b..cbf4fe1b 100644 --- a/src/KDDockWidgets.h +++ b/src/KDDockWidgets.h @@ -128,7 +128,8 @@ Q_ENUM_NS(AddingOption) enum class InitialVisibilityOption { StartVisible = 0, ///< The dock widget is made visible when docked - StartHidden ///< Don't show the dock widget when adding it + StartHidden, ///< Don't show the dock widget when adding it + PreserveCurrentTab ///< When adding as tabbed, don't change the current index }; Q_ENUM_NS(InitialVisibilityOption) @@ -176,6 +177,11 @@ struct InitialOption return visibility == InitialVisibilityOption::StartHidden; } + bool preservesCurrentTab() const + { + return visibility == InitialVisibilityOption::PreserveCurrentTab; + } + int preferredLength(Qt::Orientation o) const { return o == Qt::Horizontal ? preferredSize.width() diff --git a/src/controllers/DockWidget.cpp b/src/controllers/DockWidget.cpp index 7cdf3b0e..fbd17592 100644 --- a/src/controllers/DockWidget.cpp +++ b/src/controllers/DockWidget.cpp @@ -336,6 +336,14 @@ int DockWidget::tabIndex() const return 0; } +int DockWidget::currentTabIndex() const +{ + if (Frame *frame = d->frame()) + return frame->currentTabIndex(); + + return 0; +} + void DockWidget::setIcon(const QIcon &icon, IconPlaces places) { if (places & IconPlace::TitleBar) diff --git a/src/controllers/DockWidget.h b/src/controllers/DockWidget.h index 935371d0..e6a29d3f 100644 --- a/src/controllers/DockWidget.h +++ b/src/controllers/DockWidget.h @@ -229,10 +229,17 @@ public: void setAsCurrentTab(); /** - * @brief Returns which tab index this dock widget occupies in the tab widget it's contained in + * @brief Returns the tab index this dock widget occupies + * Note that dock widgets are almost always tabbed, even if you don't see the tab bar. + * A single floating dock widget is still tabbed on a tab widget with a single tab. */ int tabIndex() const; + /** + * @brief Returns the index of the current tab of the tab group this dock widget is in. + */ + int currentTabIndex() const; + /** * @brief Sets an icon to show on title bars and tab bars. * @param places Specifies where the icon will be shown (TitleBar, TabBar, ToggleAction, or All) diff --git a/src/controllers/Frame.cpp b/src/controllers/Frame.cpp index b7f18950..df60c751 100644 --- a/src/controllers/Frame.cpp +++ b/src/controllers/Frame.cpp @@ -292,6 +292,7 @@ void Frame::insertWidget(DockWidget *dockWidget, int index, InitialOption adding if (m_layoutItem) dockWidget->d->addPlaceholderItem(m_layoutItem); + const int originalCurrentIndex = currentIndex(); insertDockWidget(dockWidget, index); if (addingOption.startsHidden()) { @@ -307,6 +308,8 @@ void Frame::insertWidget(DockWidget *dockWidget, int index, InitialOption adding // use that size as the initial suggested size. view()->resize(dockWidget->size()); } + } else if (addingOption.preservesCurrentTab() && originalCurrentIndex != -1) { + setCurrentTabIndex(originalCurrentIndex); } }