From ffed6a9c5fcce9be41e903a531e38524489deea6 Mon Sep 17 00:00:00 2001 From: Uwe Kindler Date: Sun, 14 Jun 2020 10:39:07 +0200 Subject: [PATCH] Merged pull request #201 but made it configurable via config flag --- demo/MainWindow.cpp | 6 +++- src/DockContainerWidget.cpp | 59 ++++++++++++++++++++++--------------- src/DockManager.h | 1 + 3 files changed, 41 insertions(+), 25 deletions(-) diff --git a/demo/MainWindow.cpp b/demo/MainWindow.cpp index 7eff38b..f89352b 100644 --- a/demo/MainWindow.cpp +++ b/demo/MainWindow.cpp @@ -578,6 +578,10 @@ CMainWindow::CMainWindow(QWidget *parent) : // widget that has the focus // CDockManager::setConfigFlag(CDockManager::FocusHighlighting, true); + // uncomment if you would like to enable an equal distribution of the + // available size of a splitter to all contained dock widgets + // CDockManager::setConfigFlag(CDockManager::EqualSplitOnInsertion, true); + // Now create the dock manager and its content d->DockManager = new CDockManager(this); @@ -586,7 +590,7 @@ CMainWindow::CMainWindow(QWidget *parent) : d->createContent(); // Default window geometry - center on screen - resize(1200, 1000); + resize(1280, 720); setGeometry(QStyle::alignedRect( Qt::LeftToRight, Qt::AlignCenter, frameSize(), QGuiApplication::primaryScreen()->availableGeometry() diff --git a/src/DockContainerWidget.cpp b/src/DockContainerWidget.cpp index d126f54..bbba72d 100644 --- a/src/DockContainerWidget.cpp +++ b/src/DockContainerWidget.cpp @@ -147,12 +147,12 @@ public: * Adds dock widget to container and returns the dock area that contains * the inserted dock widget */ - CDockAreaWidget* dockWidgetIntoContainer(DockWidgetArea area, CDockWidget* Dockwidget); + CDockAreaWidget* addDockWidgetToContainer(DockWidgetArea area, CDockWidget* Dockwidget); /** * Adds dock widget to a existing DockWidgetArea */ - CDockAreaWidget* dockWidgetIntoDockArea(DockWidgetArea area, CDockWidget* Dockwidget, + CDockAreaWidget* addDockWidgetToDockArea(DockWidgetArea area, CDockWidget* Dockwidget, CDockAreaWidget* TargetDockArea); /** @@ -304,6 +304,24 @@ public: return s; } + /** + * Ensures equal distribution of the sizes of a splitter if an dock widget + * is inserted from code + */ + void adjustSplitterSizesOnInsertion(QSplitter* Splitter, qreal LastRatio = 1.0) + { + int AreaSize = (Splitter->orientation() == Qt::Horizontal) ? Splitter->width() : Splitter->height(); + auto SplitterSizes = Splitter->sizes(); + + qreal TotRatio = SplitterSizes.size() - 1.0 + LastRatio; + for(int i = 0; i < SplitterSizes.size() -1; i++) + { + SplitterSizes[i] = AreaSize / TotRatio; + } + SplitterSizes.back() = AreaSize * LastRatio / TotRatio; + Splitter->setSizes(SplitterSizes); + } + // private slots: ------------------------------------------------------------ void onDockAreaViewToggled(bool Visible) @@ -313,21 +331,6 @@ public: onVisibleDockAreaCountChanged(); emit _this->dockAreaViewToggled(DockArea, Visible); } - - void onAdjustSplitterSizes(QSplitter* Splitter, qreal LastRatio = 1.0) - { - int AreaSize = (Splitter->orientation() == Qt::Horizontal) ? Splitter->width() : Splitter->height(); - auto SplitterSizes = Splitter->sizes(); - - qreal TotRatio = SplitterSizes.size() - 1.0 + LastRatio; - for( int i=0; isetSizes(SplitterSizes); - } - }; // struct DockContainerWidgetPrivate @@ -1028,7 +1031,7 @@ bool DockContainerWidgetPrivate::restoreChildNodes(CDockingStateReader& s, //============================================================================ -CDockAreaWidget* DockContainerWidgetPrivate::dockWidgetIntoContainer(DockWidgetArea area, +CDockAreaWidget* DockContainerWidgetPrivate::addDockWidgetToContainer(DockWidgetArea area, CDockWidget* Dockwidget) { CDockAreaWidget* NewDockArea = new CDockAreaWidget(DockManager, _this); @@ -1141,7 +1144,7 @@ void DockContainerWidgetPrivate::dumpRecursive(int level, QWidget* widget) //============================================================================ -CDockAreaWidget* DockContainerWidgetPrivate::dockWidgetIntoDockArea(DockWidgetArea area, +CDockAreaWidget* DockContainerWidgetPrivate::addDockWidgetToDockArea(DockWidgetArea area, CDockWidget* Dockwidget, CDockAreaWidget* TargetDockArea) { if (CenterDockWidgetArea == area) @@ -1161,7 +1164,12 @@ CDockAreaWidget* DockContainerWidgetPrivate::dockWidgetIntoDockArea(DockWidgetAr { ADS_PRINT("TargetAreaSplitter->orientation() == InsertParam.orientation()"); TargetAreaSplitter->insertWidget(index + InsertParam.insertOffset(), NewDockArea); - onAdjustSplitterSizes(TargetAreaSplitter); + // do nothing, if flag is not enabled + if (CDockManager::testConfigFlag(CDockManager::EqualSplitOnInsertion)) + { + adjustSplitterSizesOnInsertion(TargetAreaSplitter); + } + } else { @@ -1172,8 +1180,11 @@ CDockAreaWidget* DockContainerWidgetPrivate::dockWidgetIntoDockArea(DockWidgetAr insertWidgetIntoSplitter(NewSplitter, NewDockArea, InsertParam.append()); TargetAreaSplitter->insertWidget(index, NewSplitter); - TargetAreaSplitter->setSizes(TargetAreaSizes); - onAdjustSplitterSizes(NewSplitter); + if (CDockManager::testConfigFlag(CDockManager::EqualSplitOnInsertion)) + { + TargetAreaSplitter->setSizes(TargetAreaSizes); + adjustSplitterSizesOnInsertion(NewSplitter); + } } appendDockAreas({NewDockArea}); @@ -1231,11 +1242,11 @@ CDockAreaWidget* CDockContainerWidget::addDockWidget(DockWidgetArea area, CDockW Dockwidget->setDockManager(d->DockManager); if (DockAreaWidget) { - return d->dockWidgetIntoDockArea(area, Dockwidget, DockAreaWidget); + return d->addDockWidgetToDockArea(area, Dockwidget, DockAreaWidget); } else { - return d->dockWidgetIntoContainer(area, Dockwidget); + return d->addDockWidgetToContainer(area, Dockwidget); } } diff --git a/src/DockManager.h b/src/DockManager.h index 0c500ac..6b5b72c 100644 --- a/src/DockManager.h +++ b/src/DockManager.h @@ -179,6 +179,7 @@ public: HideSingleCentralWidgetTitleBar = 0x100000, //!< If there is only one single visible dock widget in the main dock container (the dock manager) and if this flag is set, then the titlebar of this dock widget will be hidden //!< this only makes sense for non draggable and non floatable widgets and enables the creation of some kind of "central" widget FocusHighlighting = 0x200000, //!< enables styling of focused dock widget tabs or floating widget titlebar + EqualSplitOnInsertion = 0x400000, ///!< if enabled, the space is equally distributed to all widgets in a splitter DefaultDockAreaButtons = DockAreaHasCloseButton | DockAreaHasUndockButton