diff --git a/demo/MainWindow.cpp b/demo/MainWindow.cpp index 55bb6ef..9503ceb 100644 --- a/demo/MainWindow.cpp +++ b/demo/MainWindow.cpp @@ -470,12 +470,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::factory()); // 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 c073791..7cb20b2 100644 --- a/src/AutoHideDockContainer.cpp +++ b/src/AutoHideDockContainer.cpp @@ -124,6 +124,21 @@ struct AutoHideDockContainerPrivate */ AutoHideDockContainerPrivate(CAutoHideDockContainer *_public); + /** + * Convenience function to ease access to dock manager components factory + */ + QSharedPointer componentsFactory() const + { + if (!DockWidget || !DockWidget->dockManager()) + { + return CDockComponentsFactory::factory(); + } + else + { + return DockWidget->dockManager()->componentsFactory(); + } + } + /** * Convenience function to get a dock widget area */ @@ -199,7 +214,7 @@ CAutoHideDockContainer::CAutoHideDockContainer(CDockWidget* DockWidget, SideBarL { 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 ebc0074..9470682 100644 --- a/src/DockAreaTitleBar.cpp +++ b/src/DockAreaTitleBar.cpp @@ -114,6 +114,14 @@ struct DockAreaTitleBarPrivate return DockArea->dockManager(); } + /** + * Convenience function for access to dock manager components factory + */ + QSharedPointer componentsFactory() const + { + return dockManager()->componentsFactory(); + } + /** * Returns true if the given config flag is set * Convenience function to ease config flag testing diff --git a/src/DockAreaWidget.cpp b/src/DockAreaWidget.cpp index b3828d3..cba0650 100644 --- a/src/DockAreaWidget.cpp +++ b/src/DockAreaWidget.cpp @@ -269,6 +269,14 @@ struct DockAreaWidgetPrivate */ DockAreaWidgetPrivate(CDockAreaWidget* _public); + /** + * Convencience function to ease components factory access + */ + QSharedPointer componentsFactory() const + { + return DockManager->componentsFactory(); + } + /** * Creates the layout for top area with tabs and close button */ diff --git a/src/DockComponentsFactory.cpp b/src/DockComponentsFactory.cpp index 05259d7..874bf11 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; //============================================================================ @@ -52,9 +53,13 @@ CDockAreaTitleBar* CDockComponentsFactory::createDockAreaTitleBar(CDockAreaWidge //============================================================================ -const CDockComponentsFactory* CDockComponentsFactory::factory() +QSharedPointer CDockComponentsFactory::factory() { - return DefaultFactory.get(); + if (!DefaultFactory) + { + DefaultFactory.reset(new CDockComponentsFactory()); + } + return DefaultFactory; } @@ -70,6 +75,7 @@ void CDockComponentsFactory::resetDefaultFactory() { DefaultFactory.reset(new CDockComponentsFactory()); } + } // namespace ads //--------------------------------------------------------------------------- diff --git a/src/DockComponentsFactory.h b/src/DockComponentsFactory.h index 0d2b7d4..28d15a6 100644 --- a/src/DockComponentsFactory.h +++ b/src/DockComponentsFactory.h @@ -66,9 +66,11 @@ public: virtual CDockAreaTitleBar* createDockAreaTitleBar(CDockAreaWidget* DockArea) const; /** - * Returns the default components factory + * This returns the default dock components factory instance. + * If no components factory is assigned to a specifc dock manager, this + * global factory instance will be used. */ - static const CDockComponentsFactory* factory(); + static QSharedPointer factory(); /** * Sets a new default factory for creation of GUI elements. @@ -82,15 +84,6 @@ public: static void resetDefaultFactory(); }; - -/** - * 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 e7aabb3..2834455 100644 --- a/src/DockManager.cpp +++ b/src/DockManager.cpp @@ -60,6 +60,8 @@ #include "DockAreaTitleBar.h" #include "DockFocusController.h" #include "DockSplitter.h" +#include "DockComponentsFactory.h" + #if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) #include "linux/FloatingWidgetTitleBar.h" @@ -125,6 +127,7 @@ struct DockManagerPrivate QSize ToolBarIconSizeDocked = QSize(16, 16); QSize ToolBarIconSizeFloating = QSize(24, 24); CDockWidget::DockWidgetFeatures LockedDockWidgetFeatures; + QSharedPointer ComponentFactory {ads::CDockComponentsFactory::factory()}; /** * Private data constructor @@ -581,6 +584,28 @@ CDockManager::~CDockManager() delete d; } + +//============================================================================ +QSharedPointer CDockManager::componentsFactory() const +{ + return d->ComponentFactory; +} + + +//============================================================================ +void CDockManager::setComponentsFactory(ads::CDockComponentsFactory* factory) +{ + setComponentsFactory(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 291ab68..4ac6ae4 100644 --- a/src/DockManager.h +++ b/src/DockManager.h @@ -290,6 +290,24 @@ public: */ virtual ~CDockManager() override; + /** + * Returns the dock manager specific factory for creating components of + * fock widgets + */ + QSharedPointer componentsFactory() const; + + /** + * Sets a custom factory for creating components of dock widgets. + * The pointer is stored internally into a shared pointer so you should not + * delete the given factory object as long as it is used by the dock manager. + */ + void setComponentsFactory(ads::CDockComponentsFactory* Factory); + + /** + * Sets a custom factory for creating components of dock widgets. + */ + void setComponentsFactory(QSharedPointer); + /** * This function returns the global configuration flags */ diff --git a/src/DockWidget.cpp b/src/DockWidget.cpp index 4bb8ea4..23b493d 100644 --- a/src/DockWidget.cpp +++ b/src/DockWidget.cpp @@ -102,6 +102,14 @@ struct DockWidgetPrivate */ DockWidgetPrivate(CDockWidget* _public); + /** + * Convenience function to ease components factory access + */ + QSharedPointer componentsFactory() const + { + return DockManager ? DockManager->componentsFactory() : CDockComponentsFactory::factory(); + } + /** * Show dock widget */ @@ -358,9 +366,17 @@ void DockWidgetPrivate::setToolBarStyleFromDockManager() //============================================================================ CDockWidget::CDockWidget(const QString &title, QWidget *parent) : - QFrame(parent), - d(new DockWidgetPrivate(this)) + CDockWidget(nullptr, title, parent) { +} + + +//============================================================================ +CDockWidget::CDockWidget(CDockManager *manager, const QString &title, QWidget* parent) + : QFrame(parent), + d(new DockWidgetPrivate(this)) +{ + d->DockManager = manager; d->Layout = new QBoxLayout(QBoxLayout::TopToBottom); d->Layout->setContentsMargins(0, 0, 0, 0); d->Layout->setSpacing(0); @@ -368,7 +384,7 @@ CDockWidget::CDockWidget(const QString &title, QWidget *parent) : setWindowTitle(title); setObjectName(title); - d->TabWidget = componentsFactory()->createDockWidgetTab(this); + d->TabWidget = d->componentsFactory()->createDockWidgetTab(this); d->ToggleViewAction = new QAction(title, this); d->ToggleViewAction->setCheckable(true); @@ -382,6 +398,7 @@ CDockWidget::CDockWidget(const QString &title, QWidget *parent) : } } + //============================================================================ CDockWidget::~CDockWidget() { diff --git a/src/DockWidget.h b/src/DockWidget.h index 0d6b88c..3dc1a9a 100644 --- a/src/DockWidget.h +++ b/src/DockWidget.h @@ -256,8 +256,18 @@ public: * during runtime, you need to set a unique object name explicitly * by calling setObjectName() after construction. * Use the layoutFlags to configure the layout of the dock widget. + * \note If you would like to use custom TabWidget implementations, you need + * to use the constructor with the CDockManager argument. */ - CDockWidget(const QString &title, QWidget* parent = nullptr); + explicit CDockWidget(const QString &title, QWidget* parent = nullptr); + + /** + * Creates a dock widget and assigns the dock manager that manages this + * widget. + * This allows the dock widget to use the componentsFactory() of the dock + * manager in the constructot to create its components. + */ + CDockWidget(CDockManager *manager, const QString &title, QWidget* parent = nullptr); /** * Virtual Destructor