From 296c7edbd092be76146946e5cf3f0e344e10678d Mon Sep 17 00:00:00 2001 From: Uwe Kindler Date: Mon, 14 Nov 2022 14:49:58 +0100 Subject: [PATCH] Added support for tab index when inserting dockwidgets into area --- src/DockAreaWidget.cpp | 4 ++++ src/DockContainerWidget.cpp | 10 +++++----- src/DockContainerWidget.h | 2 +- src/DockManager.cpp | 12 ++++++++++-- src/DockManager.h | 10 +++++++++- 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/DockAreaWidget.cpp b/src/DockAreaWidget.cpp index a8d4297..90d21e9 100644 --- a/src/DockAreaWidget.cpp +++ b/src/DockAreaWidget.cpp @@ -416,6 +416,10 @@ void CDockAreaWidget::addDockWidget(CDockWidget* DockWidget) void CDockAreaWidget::insertDockWidget(int index, CDockWidget* DockWidget, bool Activate) { + if (index < 0 || index > d->ContentsLayout->count()) + { + index = d->ContentsLayout->count(); + } d->ContentsLayout->insertWidget(index, DockWidget); DockWidget->setDockArea(this); DockWidget->tabWidget()->setDockAreaWidget(this); diff --git a/src/DockContainerWidget.cpp b/src/DockContainerWidget.cpp index 5451eb3..b96a416 100644 --- a/src/DockContainerWidget.cpp +++ b/src/DockContainerWidget.cpp @@ -153,7 +153,7 @@ public: * Adds dock widget to a existing DockWidgetArea */ CDockAreaWidget* addDockWidgetToDockArea(DockWidgetArea area, CDockWidget* Dockwidget, - CDockAreaWidget* TargetDockArea); + CDockAreaWidget* TargetDockArea, int Index = -1); /** * Add dock area to this container @@ -1228,7 +1228,7 @@ void DockContainerWidgetPrivate::dumpRecursive(int level, QWidget* widget) //============================================================================ CDockAreaWidget* DockContainerWidgetPrivate::addDockWidgetToDockArea(DockWidgetArea area, - CDockWidget* Dockwidget, CDockAreaWidget* TargetDockArea) + CDockWidget* Dockwidget, CDockAreaWidget* TargetDockArea, int Index) { if (CenterDockWidgetArea == area) { @@ -1238,7 +1238,7 @@ CDockAreaWidget* DockContainerWidgetPrivate::addDockWidgetToDockArea(DockWidgetA } CDockAreaWidget* NewDockArea = new CDockAreaWidget(DockManager, _this); - NewDockArea->addDockWidget(Dockwidget); + NewDockArea->insertDockWidget(Index, Dockwidget); auto InsertParam = internal::dockAreaInsertParameters(area); QSplitter* TargetAreaSplitter = internal::findParent(TargetDockArea); @@ -1315,7 +1315,7 @@ CDockContainerWidget::~CDockContainerWidget() //============================================================================ CDockAreaWidget* CDockContainerWidget::addDockWidget(DockWidgetArea area, CDockWidget* Dockwidget, - CDockAreaWidget* DockAreaWidget) + CDockAreaWidget* DockAreaWidget, int Index) { CDockAreaWidget* OldDockArea = Dockwidget->dockAreaWidget(); if (OldDockArea) @@ -1326,7 +1326,7 @@ CDockAreaWidget* CDockContainerWidget::addDockWidget(DockWidgetArea area, CDockW Dockwidget->setDockManager(d->DockManager); if (DockAreaWidget) { - return d->addDockWidgetToDockArea(area, Dockwidget, DockAreaWidget); + return d->addDockWidgetToDockArea(area, Dockwidget, DockAreaWidget, Index); } else { diff --git a/src/DockContainerWidget.h b/src/DockContainerWidget.h index a9ebea4..1420557 100644 --- a/src/DockContainerWidget.h +++ b/src/DockContainerWidget.h @@ -181,7 +181,7 @@ public: * \return Returns the dock area widget that contains the new DockWidget */ CDockAreaWidget* addDockWidget(DockWidgetArea area, CDockWidget* Dockwidget, - CDockAreaWidget* DockAreaWidget = nullptr); + CDockAreaWidget* DockAreaWidget = nullptr, int Index = -1); /** * Removes dockwidget diff --git a/src/DockManager.cpp b/src/DockManager.cpp index 61210d0..2025682 100644 --- a/src/DockManager.cpp +++ b/src/DockManager.cpp @@ -842,11 +842,11 @@ void CDockManager::restoreHiddenFloatingWidgets() //============================================================================ CDockAreaWidget* CDockManager::addDockWidget(DockWidgetArea area, - CDockWidget* Dockwidget, CDockAreaWidget* DockAreaWidget) + CDockWidget* Dockwidget, CDockAreaWidget* DockAreaWidget, int Index) { d->DockWidgetsMap.insert(Dockwidget->objectName(), Dockwidget); auto Container = DockAreaWidget ? DockAreaWidget->dockContainer(): this; - auto AreaOfAddedDockWidget = Container->addDockWidget(area, Dockwidget, DockAreaWidget); + auto AreaOfAddedDockWidget = Container->addDockWidget(area, Dockwidget, DockAreaWidget, Index); Q_EMIT dockWidgetAdded(Dockwidget); return AreaOfAddedDockWidget; } @@ -886,6 +886,14 @@ CDockAreaWidget* CDockManager::addDockWidgetTabToArea(CDockWidget* Dockwidget, } +//============================================================================ +CDockAreaWidget* CDockManager::insertDockWidgetTabIntoArea(CDockWidget* Dockwidget, + CDockAreaWidget* DockAreaWidget, int Index) +{ + return addDockWidget(ads::CenterDockWidgetArea, Dockwidget, DockAreaWidget, Index); +} + + //============================================================================ CDockWidget* CDockManager::findDockWidget(const QString& ObjectName) const { diff --git a/src/DockManager.h b/src/DockManager.h index 2072287..73e05cc 100644 --- a/src/DockManager.h +++ b/src/DockManager.h @@ -281,7 +281,7 @@ public: * \return Returns the dock area widget that contains the new DockWidget */ CDockAreaWidget* addDockWidget(DockWidgetArea area, CDockWidget* Dockwidget, - CDockAreaWidget* DockAreaWidget = nullptr); + CDockAreaWidget* DockAreaWidget = nullptr, int Index = -1); /** * Adds dockwidget into the given container. @@ -308,6 +308,14 @@ public: CDockAreaWidget* addDockWidgetTabToArea(CDockWidget* Dockwidget, CDockAreaWidget* DockAreaWidget); + /** + * This function will inserts the given Dockwidget to the given DockAreaWidget + * as a new tab at the given index. + * If index is out of range, the tab is simply appended. Otherwise it is inserted at the specified position. + */ + CDockAreaWidget* insertDockWidgetTabIntoArea(CDockWidget* Dockwidget, + CDockAreaWidget* DockAreaWidget, int Index = -1); + /** * Adds the given DockWidget floating and returns the created * CFloatingDockContainer instance.