From 25e0cff0489e61ebedab7ae901a51cde9a2ae86e Mon Sep 17 00:00:00 2001 From: Federico Fuga Date: Tue, 5 Nov 2024 19:28:26 +0100 Subject: [PATCH] Remove DockComponentsFactory singleton. Allow Factory to be per-dock-manager customizable --- demo/MainWindow.cpp | 4 ++-- src/AutoHideDockContainer.cpp | 14 ++++++++++---- src/DockAreaTitleBar.cpp | 13 +++++++++---- src/DockAreaWidget.cpp | 10 +++++++--- src/DockComponentsFactory.cpp | 26 ++++++++------------------ src/DockComponentsFactory.h | 25 +------------------------ src/DockManager.cpp | 19 ++++++++++++++++++- src/DockManager.h | 6 ++++++ src/DockWidget.cpp | 15 ++++++++++----- src/DockWidget.h | 4 +++- 10 files changed, 74 insertions(+), 62 deletions(-) diff --git a/demo/MainWindow.cpp b/demo/MainWindow.cpp index cde5492..272016e 100644 --- a/demo/MainWindow.cpp +++ b/demo/MainWindow.cpp @@ -471,12 +471,12 @@ void MainWindowPrivate::createContent() appendFeaturStringToWindowTitle(FileSystemWidget); // Test custom factory - we inject a help button into the title bar - ads::CDockComponentsFactory::setFactory(new CCustomComponentsFactory()); + DockManager->setComponentsFactory(new CCustomComponentsFactory()); auto TopDockArea = DockManager->addDockWidget(ads::TopDockWidgetArea, FileSystemWidget); // Uncomment the next line if you would like to test the // HideSingleWidgetTitleBar functionality // TopDockArea->setDockAreaFlag(ads::CDockAreaWidget::HideSingleWidgetTitleBar, true); - ads::CDockComponentsFactory::resetDefaultFactory(); + DockManager->setComponentsFactory(ads::CDockComponentsFactory::defaultFactory()); // We create a calendar widget and clear all flags to prevent the dock area // from closing diff --git a/src/AutoHideDockContainer.cpp b/src/AutoHideDockContainer.cpp index 44af9e2..0a88eb7 100644 --- a/src/AutoHideDockContainer.cpp +++ b/src/AutoHideDockContainer.cpp @@ -109,6 +109,7 @@ int resizeHandleLayoutPosition(SideBarLocation Area) */ struct AutoHideDockContainerPrivate { + CDockManager *DockManager; CAutoHideDockContainer* _this; CDockAreaWidget* DockArea{nullptr}; CDockWidget* DockWidget{nullptr}; @@ -122,7 +123,11 @@ struct AutoHideDockContainerPrivate /** * Private data constructor */ - AutoHideDockContainerPrivate(CAutoHideDockContainer *_public); + AutoHideDockContainerPrivate(CDockManager *manager, CAutoHideDockContainer *_public); + + QSharedPointer componentsFactory() const { + return DockManager->componentsFactory(); + } /** * Convenience function to get a dock widget area @@ -178,8 +183,9 @@ struct AutoHideDockContainerPrivate //============================================================================ AutoHideDockContainerPrivate::AutoHideDockContainerPrivate( + CDockManager *manager, CAutoHideDockContainer *_public) : - _this(_public) + DockManager(manager), _this(_public) { } @@ -195,11 +201,11 @@ CDockContainerWidget* CAutoHideDockContainer::dockContainer() const //============================================================================ CAutoHideDockContainer::CAutoHideDockContainer(CDockWidget* DockWidget, SideBarLocation area, CDockContainerWidget* parent) : Super(parent), - d(new AutoHideDockContainerPrivate(this)) + d(new AutoHideDockContainerPrivate(DockWidget->dockManager(), this)) { hide(); // auto hide dock container is initially always hidden d->SideTabBarArea = area; - d->SideTab = componentsFactory()->createDockWidgetSideTab(nullptr); + d->SideTab = d->componentsFactory()->createDockWidgetSideTab(nullptr); connect(d->SideTab, &CAutoHideTab::pressed, this, &CAutoHideDockContainer::toggleCollapseState); d->DockArea = new CDockAreaWidget(DockWidget->dockManager(), parent); d->DockArea->setObjectName("autoHideDockArea"); diff --git a/src/DockAreaTitleBar.cpp b/src/DockAreaTitleBar.cpp index 32a1d1a..7517505 100644 --- a/src/DockAreaTitleBar.cpp +++ b/src/DockAreaTitleBar.cpp @@ -66,6 +66,7 @@ namespace ads */ struct DockAreaTitleBarPrivate { + CDockManager *DockManager; CDockAreaTitleBar* _this; QPointer TabsMenuButton; QPointer AutoHideButton; @@ -88,7 +89,11 @@ struct DockAreaTitleBarPrivate /** * Private data constructor */ - DockAreaTitleBarPrivate(CDockAreaTitleBar* _public); + DockAreaTitleBarPrivate(CDockManager *manager, CDockAreaTitleBar* _public); + + QSharedPointer componentsFactory() const { + return DockManager->componentsFactory(); + } /** * Creates the title bar close and menu buttons @@ -165,8 +170,8 @@ struct DockAreaTitleBarPrivate };// struct DockAreaTitleBarPrivate //============================================================================ -DockAreaTitleBarPrivate::DockAreaTitleBarPrivate(CDockAreaTitleBar* _public) : - _this(_public) +DockAreaTitleBarPrivate::DockAreaTitleBarPrivate(CDockManager *manager, CDockAreaTitleBar* _public) : + DockManager(manager), _this(_public) { } @@ -331,7 +336,7 @@ void DockAreaTitleBarPrivate::startFloating(const QPoint& Offset) //============================================================================ CDockAreaTitleBar::CDockAreaTitleBar(CDockAreaWidget* parent) : QFrame(parent), - d(new DockAreaTitleBarPrivate(this)) + d(new DockAreaTitleBarPrivate(parent->dockManager(), this)) { d->DockArea = parent; diff --git a/src/DockAreaWidget.cpp b/src/DockAreaWidget.cpp index b3828d3..c4f5338 100644 --- a/src/DockAreaWidget.cpp +++ b/src/DockAreaWidget.cpp @@ -264,6 +264,10 @@ struct DockAreaWidgetPrivate QSize MinSizeHint; CDockAreaWidget::DockAreaFlags Flags{CDockAreaWidget::DefaultFlags}; + QSharedPointer componentsFactory() const { + return DockManager->componentsFactory(); + } + /** * Private data constructor */ @@ -371,10 +375,10 @@ void DockAreaWidgetPrivate::updateTitleBarButtonStates() if (_this->isAutoHide()) { - if (CDockManager::testAutoHideConfigFlag(CDockManager::AutoHideHasCloseButton)) - { + if (CDockManager::testAutoHideConfigFlag(CDockManager::AutoHideHasCloseButton)) + { TitleBar->button(TitleBarButtonClose)->setEnabled( - _this->features().testFlag(CDockWidget::DockWidgetClosable)); + _this->features().testFlag(CDockWidget::DockWidgetClosable)); } } else diff --git a/src/DockComponentsFactory.cpp b/src/DockComponentsFactory.cpp index 05259d7..03d64ef 100644 --- a/src/DockComponentsFactory.cpp +++ b/src/DockComponentsFactory.cpp @@ -21,7 +21,8 @@ namespace ads { -static std::unique_ptr DefaultFactory(new CDockComponentsFactory()); + +static QSharedPointer DefaultFactory; //============================================================================ @@ -50,26 +51,15 @@ CDockAreaTitleBar* CDockComponentsFactory::createDockAreaTitleBar(CDockAreaWidge return new CDockAreaTitleBar(DockArea); } - -//============================================================================ -const CDockComponentsFactory* CDockComponentsFactory::factory() +QSharedPointer + CDockComponentsFactory::defaultFactory() { - return DefaultFactory.get(); + if (!DefaultFactory) { + DefaultFactory.reset(new CDockComponentsFactory()); + } + return DefaultFactory; } - -//============================================================================ -void CDockComponentsFactory::setFactory(CDockComponentsFactory* Factory) -{ - DefaultFactory.reset(Factory); -} - - -//============================================================================ -void CDockComponentsFactory::resetDefaultFactory() -{ - DefaultFactory.reset(new CDockComponentsFactory()); -} } // namespace ads //--------------------------------------------------------------------------- diff --git a/src/DockComponentsFactory.h b/src/DockComponentsFactory.h index 0d2b7d4..4e744f8 100644 --- a/src/DockComponentsFactory.h +++ b/src/DockComponentsFactory.h @@ -65,32 +65,9 @@ public: */ virtual CDockAreaTitleBar* createDockAreaTitleBar(CDockAreaWidget* DockArea) const; - /** - * Returns the default components factory - */ - static const CDockComponentsFactory* factory(); - - /** - * Sets a new default factory for creation of GUI elements. - * This function takes ownership of the given Factory. - */ - static void setFactory(CDockComponentsFactory* Factory); - - /** - * Resets the current factory to the - */ - static void resetDefaultFactory(); + static QSharedPointer defaultFactory(); }; - -/** - * Convenience function to ease factory instance access - */ -inline const CDockComponentsFactory* componentsFactory() -{ - return CDockComponentsFactory::factory(); -} - } // namespace ads //--------------------------------------------------------------------------- diff --git a/src/DockManager.cpp b/src/DockManager.cpp index 06ed8cd..0d94c02 100644 --- a/src/DockManager.cpp +++ b/src/DockManager.cpp @@ -31,6 +31,7 @@ #include #include "DockWidgetTab.h" #include "DockManager.h" +#include "DockComponentsFactory.h" #include #include @@ -124,6 +125,8 @@ struct DockManagerPrivate QSize ToolBarIconSizeFloating = QSize(24, 24); CDockWidget::DockWidgetFeatures LockedDockWidgetFeatures; + QSharedPointer componentFactory {ads::CDockComponentsFactory::defaultFactory()}; + /** * Private data constructor */ @@ -190,7 +193,6 @@ struct DockManagerPrivate DockManagerPrivate::DockManagerPrivate(CDockManager* _public) : _this(_public) { - } @@ -579,6 +581,21 @@ CDockManager::~CDockManager() delete d; } + QSharedPointer CDockManager::componentsFactory() const +{ + return d->componentFactory; +} + +void CDockManager::setComponentsFactory(ads::CDockComponentsFactory* factory) +{ + d->componentFactory = QSharedPointer(factory); +} + +void CDockManager::setComponentsFactory(QSharedPointer factory) +{ + d->componentFactory = factory; +} + //============================================================================ #if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) bool CDockManager::eventFilter(QObject *obj, QEvent *e) diff --git a/src/DockManager.h b/src/DockManager.h index dcb2356..c7fc990 100644 --- a/src/DockManager.h +++ b/src/DockManager.h @@ -277,6 +277,12 @@ public: */ virtual ~CDockManager() override; + QSharedPointer componentsFactory() const; + + void setComponentsFactory(ads::CDockComponentsFactory *); + + void setComponentsFactory(QSharedPointer); + /** * This function returns the global configuration flags */ diff --git a/src/DockWidget.cpp b/src/DockWidget.cpp index 4bb8ea4..4f1f9af 100644 --- a/src/DockWidget.cpp +++ b/src/DockWidget.cpp @@ -80,7 +80,7 @@ struct DockWidgetPrivate QWidget* Widget = nullptr; CDockWidgetTab* TabWidget = nullptr; CDockWidget::DockWidgetFeatures Features = CDockWidget::DefaultDockWidgetFeatures; - QPointer DockManager; + QPointer DockManager {nullptr}; QPointer DockArea; QAction* ToggleViewAction = nullptr; bool Closed = false; @@ -357,9 +357,9 @@ void DockWidgetPrivate::setToolBarStyleFromDockManager() //============================================================================ -CDockWidget::CDockWidget(const QString &title, QWidget *parent) : - QFrame(parent), - d(new DockWidgetPrivate(this)) +CDockWidget::CDockWidget(CDockManager* manager, const QString& title, + QWidget* parent) +: QFrame(parent), d(new DockWidgetPrivate(this)) { d->Layout = new QBoxLayout(QBoxLayout::TopToBottom); d->Layout->setContentsMargins(0, 0, 0, 0); @@ -368,7 +368,8 @@ CDockWidget::CDockWidget(const QString &title, QWidget *parent) : setWindowTitle(title); setObjectName(title); - d->TabWidget = componentsFactory()->createDockWidgetTab(this); + auto factory = manager ? manager->componentsFactory() : CDockComponentsFactory::defaultFactory(); + d->TabWidget = factory->createDockWidgetTab(this); d->ToggleViewAction = new QAction(title, this); d->ToggleViewAction->setCheckable(true); @@ -382,6 +383,10 @@ CDockWidget::CDockWidget(const QString &title, QWidget *parent) : } } +CDockWidget::CDockWidget(const QString &title, QWidget *parent) : + CDockWidget(nullptr, title, parent) +{} + //============================================================================ CDockWidget::~CDockWidget() { diff --git a/src/DockWidget.h b/src/DockWidget.h index 0d6b88c..5670f23 100644 --- a/src/DockWidget.h +++ b/src/DockWidget.h @@ -257,7 +257,9 @@ public: * by calling setObjectName() after construction. * Use the layoutFlags to configure the layout of the dock widget. */ - CDockWidget(const QString &title, QWidget* parent = nullptr); + explicit CDockWidget(const QString &title, QWidget* parent = nullptr); + + CDockWidget(CDockManager *manager, const QString &title, QWidget* parent = nullptr); /** * Virtual Destructor