From fcad7763ffc0942e87c18665746b5f397fe8816f Mon Sep 17 00:00:00 2001 From: Uwe Kindler Date: Fri, 20 Dec 2024 10:21:57 +0100 Subject: [PATCH] Add factory method to Dock manager as an alternative method to create DockWidget, supporting custom factory --- demo/MainWindow.cpp | 16 ++++++++-------- src/DockManager.cpp | 7 +++++++ src/DockManager.h | 15 +++++++++++++++ src/DockWidget.h | 27 ++++++++++++++++++++++----- 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/demo/MainWindow.cpp b/demo/MainWindow.cpp index 9503ceb..b75276a 100644 --- a/demo/MainWindow.cpp +++ b/demo/MainWindow.cpp @@ -236,7 +236,7 @@ struct MainWindowPrivate m->setRootPath(QDir::currentPath()); w->setModel(m); w->setRootIndex(m->index(QDir::currentPath())); - ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Filesystem %1") + ads::CDockWidget* DockWidget = DockManager->createDockWidget(QString("Filesystem %1") .arg(FileSystemCount++)); DockWidget->setWidget(w); DockWidget->setIcon(svgIcon(":/adsdemo/images/folder_open.svg")); @@ -257,7 +257,7 @@ struct MainWindowPrivate { static int CalendarCount = 0; QCalendarWidget* w = new QCalendarWidget(); - ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Calendar %1").arg(CalendarCount++)); + ads::CDockWidget* DockWidget = DockManager->createDockWidget(QString("Calendar %1").arg(CalendarCount++)); // The following lines are for testing the setWidget() and takeWidget() // functionality DockWidget->setWidget(w); @@ -302,7 +302,7 @@ struct MainWindowPrivate .arg(LabelCount) .arg(QTime::currentTime().toString("hh:mm:ss:zzz"))); - ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Label %1").arg(LabelCount++)); + ads::CDockWidget* DockWidget = DockManager->createDockWidget(QString("Label %1").arg(LabelCount++)); DockWidget->setWidget(l); DockWidget->setIcon(svgIcon(":/adsdemo/images/font_download.svg")); ui.menuView->addAction(DockWidget->toggleViewAction()); @@ -320,7 +320,7 @@ struct MainWindowPrivate w->setPlaceholderText("This is an editor. If you close the editor, it will be " "deleted. Enter your text here."); w->setStyleSheet("border: none"); - ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Editor %1").arg(EditorCount++)); + ads::CDockWidget* DockWidget = DockManager->createDockWidget(QString("Editor %1").arg(EditorCount++)); DockWidget->setWidget(w); DockWidget->setIcon(svgIcon(":/adsdemo/images/edit.svg")); DockWidget->setFeature(ads::CDockWidget::CustomCloseHandling, true); @@ -363,7 +363,7 @@ struct MainWindowPrivate auto Result = w->loadFile(FileName); qDebug() << "loadFile result: " << Result; - ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Image Viewer %1").arg(ImageViewerCount++)); + ads::CDockWidget* DockWidget = DockManager->createDockWidget(QString("Image Viewer %1").arg(ImageViewerCount++)); DockWidget->setIcon(svgIcon(":/adsdemo/images/photo.svg")); DockWidget->setWidget(w,ads:: CDockWidget::ForceNoScrollArea); auto ToolBar = DockWidget->createDefaultToolBar(); @@ -378,7 +378,7 @@ struct MainWindowPrivate { static int TableCount = 0; auto w = new CMinSizeTableWidget(); - ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Table %1").arg(TableCount++)); + ads::CDockWidget* DockWidget = DockManager->createDockWidget(QString("Table %1").arg(TableCount++)); static int colCount = 5; static int rowCount = 30; w->setColumnCount(colCount); @@ -417,7 +417,7 @@ struct MainWindowPrivate ads::CDockWidget *createQQuickWidget() { QQuickWidget *widget = new QQuickWidget(); - ads::CDockWidget *dockWidget = new ads::CDockWidget("Quick"); + ads::CDockWidget *dockWidget = DockManager->createDockWidget("Quick"); dockWidget->setWidget(widget); return dockWidget; } @@ -432,7 +432,7 @@ struct MainWindowPrivate { static int ActiveXCount = 0; QAxWidget* w = new QAxWidget("{6bf52a52-394a-11d3-b153-00c04f79faa6}", parent); - ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Active X %1").arg(ActiveXCount++)); + ads::CDockWidget* DockWidget = DockManager->createDockWidget(QString("Active X %1").arg(ActiveXCount++)); DockWidget->setWidget(w); ui.menuView->addAction(DockWidget->toggleViewAction()); return DockWidget; diff --git a/src/DockManager.cpp b/src/DockManager.cpp index 2834455..6b99334 100644 --- a/src/DockManager.cpp +++ b/src/DockManager.cpp @@ -585,6 +585,13 @@ CDockManager::~CDockManager() } +//============================================================================ +CDockWidget* CDockManager::createDockWidget(const QString &title, QWidget* parent) +{ + return new CDockWidget(this, title, parent); +} + + //============================================================================ QSharedPointer CDockManager::componentsFactory() const { diff --git a/src/DockManager.h b/src/DockManager.h index 4ac6ae4..3eab946 100644 --- a/src/DockManager.h +++ b/src/DockManager.h @@ -290,6 +290,21 @@ public: */ virtual ~CDockManager() override; + /** + * Creates a new dock widget with the specified title and optional parent + * widget. + * + * The new dock widget will be managed by the dock manager, and its lifetime + * will be tied to the dock manager. If a parent widget is provided, the dock + * widget will be created as a child of the parent widget. If no parent widget + * is provided, the dock widget will be created as a top-level widget. + * + * @param title The title of the dock widget. + * @param parent The parent widget, if any. Defaults to nullptr. + * @return Returns a pointer to the created CDockWidget. + */ + CDockWidget *createDockWidget(const QString &title, QWidget* parent = nullptr); + /** * Returns the dock manager specific factory for creating components of * fock widgets diff --git a/src/DockWidget.h b/src/DockWidget.h index 3dc1a9a..04d10f8 100644 --- a/src/DockWidget.h +++ b/src/DockWidget.h @@ -259,13 +259,30 @@ public: * \note If you would like to use custom TabWidget implementations, you need * to use the constructor with the CDockManager argument. */ - explicit CDockWidget(const QString &title, QWidget* parent = nullptr); + Q_DECL_DEPRECATED 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. + * This constructor creates a dock widget for the given dock manager with the + * provided title. + * + * @param manager Pointer to the dock manager that owns the dock widget. + * @param title The title is the text that is shown in the window title when + * the dock widget is floating and it is the title that is shown in the + * titlebar or the tab of this dock widget if it is tabified. + * @param parent Pointer to the parent widget, defaults to nullptr. + * + * @note The object name of the dock widget is also set to the title. The + * object name is required by the dock manager to properly save and restore + * the state of the dock widget. That means, the title needs to be unique. If + * the title is not unique or if you would like to change the title 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 this constructor is preferred over the two argument version, especially + * when custom factories are in use. Indeed, it will use the Dock Manager factory, + * and not the default factory. For this reason, the original constructor should + * be deprecated in favour of this version. */ CDockWidget(CDockManager *manager, const QString &title, QWidget* parent = nullptr);