Skip to content

Commit fcad776

Browse files
Add factory method to Dock manager as an alternative method to create DockWidget, supporting custom factory
1 parent 1bec423 commit fcad776

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

demo/MainWindow.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ struct MainWindowPrivate
236236
m->setRootPath(QDir::currentPath());
237237
w->setModel(m);
238238
w->setRootIndex(m->index(QDir::currentPath()));
239-
ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Filesystem %1")
239+
ads::CDockWidget* DockWidget = DockManager->createDockWidget(QString("Filesystem %1")
240240
.arg(FileSystemCount++));
241241
DockWidget->setWidget(w);
242242
DockWidget->setIcon(svgIcon(":/adsdemo/images/folder_open.svg"));
@@ -257,7 +257,7 @@ struct MainWindowPrivate
257257
{
258258
static int CalendarCount = 0;
259259
QCalendarWidget* w = new QCalendarWidget();
260-
ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Calendar %1").arg(CalendarCount++));
260+
ads::CDockWidget* DockWidget = DockManager->createDockWidget(QString("Calendar %1").arg(CalendarCount++));
261261
// The following lines are for testing the setWidget() and takeWidget()
262262
// functionality
263263
DockWidget->setWidget(w);
@@ -302,7 +302,7 @@ struct MainWindowPrivate
302302
.arg(LabelCount)
303303
.arg(QTime::currentTime().toString("hh:mm:ss:zzz")));
304304

305-
ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Label %1").arg(LabelCount++));
305+
ads::CDockWidget* DockWidget = DockManager->createDockWidget(QString("Label %1").arg(LabelCount++));
306306
DockWidget->setWidget(l);
307307
DockWidget->setIcon(svgIcon(":/adsdemo/images/font_download.svg"));
308308
ui.menuView->addAction(DockWidget->toggleViewAction());
@@ -320,7 +320,7 @@ struct MainWindowPrivate
320320
w->setPlaceholderText("This is an editor. If you close the editor, it will be "
321321
"deleted. Enter your text here.");
322322
w->setStyleSheet("border: none");
323-
ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Editor %1").arg(EditorCount++));
323+
ads::CDockWidget* DockWidget = DockManager->createDockWidget(QString("Editor %1").arg(EditorCount++));
324324
DockWidget->setWidget(w);
325325
DockWidget->setIcon(svgIcon(":/adsdemo/images/edit.svg"));
326326
DockWidget->setFeature(ads::CDockWidget::CustomCloseHandling, true);
@@ -363,7 +363,7 @@ struct MainWindowPrivate
363363

364364
auto Result = w->loadFile(FileName);
365365
qDebug() << "loadFile result: " << Result;
366-
ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Image Viewer %1").arg(ImageViewerCount++));
366+
ads::CDockWidget* DockWidget = DockManager->createDockWidget(QString("Image Viewer %1").arg(ImageViewerCount++));
367367
DockWidget->setIcon(svgIcon(":/adsdemo/images/photo.svg"));
368368
DockWidget->setWidget(w,ads:: CDockWidget::ForceNoScrollArea);
369369
auto ToolBar = DockWidget->createDefaultToolBar();
@@ -378,7 +378,7 @@ struct MainWindowPrivate
378378
{
379379
static int TableCount = 0;
380380
auto w = new CMinSizeTableWidget();
381-
ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Table %1").arg(TableCount++));
381+
ads::CDockWidget* DockWidget = DockManager->createDockWidget(QString("Table %1").arg(TableCount++));
382382
static int colCount = 5;
383383
static int rowCount = 30;
384384
w->setColumnCount(colCount);
@@ -417,7 +417,7 @@ struct MainWindowPrivate
417417
ads::CDockWidget *createQQuickWidget()
418418
{
419419
QQuickWidget *widget = new QQuickWidget();
420-
ads::CDockWidget *dockWidget = new ads::CDockWidget("Quick");
420+
ads::CDockWidget *dockWidget = DockManager->createDockWidget("Quick");
421421
dockWidget->setWidget(widget);
422422
return dockWidget;
423423
}
@@ -432,7 +432,7 @@ struct MainWindowPrivate
432432
{
433433
static int ActiveXCount = 0;
434434
QAxWidget* w = new QAxWidget("{6bf52a52-394a-11d3-b153-00c04f79faa6}", parent);
435-
ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Active X %1").arg(ActiveXCount++));
435+
ads::CDockWidget* DockWidget = DockManager->createDockWidget(QString("Active X %1").arg(ActiveXCount++));
436436
DockWidget->setWidget(w);
437437
ui.menuView->addAction(DockWidget->toggleViewAction());
438438
return DockWidget;

src/DockManager.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,13 @@ CDockManager::~CDockManager()
585585
}
586586

587587

588+
//============================================================================
589+
CDockWidget* CDockManager::createDockWidget(const QString &title, QWidget* parent)
590+
{
591+
return new CDockWidget(this, title, parent);
592+
}
593+
594+
588595
//============================================================================
589596
QSharedPointer<ads::CDockComponentsFactory> CDockManager::componentsFactory() const
590597
{

src/DockManager.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,21 @@ public Q_SLOTS:
290290
*/
291291
virtual ~CDockManager() override;
292292

293+
/**
294+
* Creates a new dock widget with the specified title and optional parent
295+
* widget.
296+
*
297+
* The new dock widget will be managed by the dock manager, and its lifetime
298+
* will be tied to the dock manager. If a parent widget is provided, the dock
299+
* widget will be created as a child of the parent widget. If no parent widget
300+
* is provided, the dock widget will be created as a top-level widget.
301+
*
302+
* @param title The title of the dock widget.
303+
* @param parent The parent widget, if any. Defaults to nullptr.
304+
* @return Returns a pointer to the created CDockWidget.
305+
*/
306+
CDockWidget *createDockWidget(const QString &title, QWidget* parent = nullptr);
307+
293308
/**
294309
* Returns the dock manager specific factory for creating components of
295310
* fock widgets

src/DockWidget.h

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,30 @@ private Q_SLOTS:
259259
* \note If you would like to use custom TabWidget implementations, you need
260260
* to use the constructor with the CDockManager argument.
261261
*/
262-
explicit CDockWidget(const QString &title, QWidget* parent = nullptr);
262+
Q_DECL_DEPRECATED explicit CDockWidget(const QString &title, QWidget* parent = nullptr);
263263

264264
/**
265-
* Creates a dock widget and assigns the dock manager that manages this
266-
* widget.
267-
* This allows the dock widget to use the componentsFactory() of the dock
268-
* manager in the constructot to create its components.
265+
* This constructor creates a dock widget for the given dock manager with the
266+
* provided title.
267+
*
268+
* @param manager Pointer to the dock manager that owns the dock widget.
269+
* @param title The title is the text that is shown in the window title when
270+
* the dock widget is floating and it is the title that is shown in the
271+
* titlebar or the tab of this dock widget if it is tabified.
272+
* @param parent Pointer to the parent widget, defaults to nullptr.
273+
*
274+
* @note The object name of the dock widget is also set to the title. The
275+
* object name is required by the dock manager to properly save and restore
276+
* the state of the dock widget. That means, the title needs to be unique. If
277+
* the title is not unique or if you would like to change the title during
278+
* runtime, you need to set a unique object name explicitly by calling
279+
* setObjectName() after construction. Use the layoutFlags to configure the
280+
* layout of the dock widget.
281+
*
282+
* @note this constructor is preferred over the two argument version, especially
283+
* when custom factories are in use. Indeed, it will use the Dock Manager factory,
284+
* and not the default factory. For this reason, the original constructor should
285+
* be deprecated in favour of this version.
269286
*/
270287
CDockWidget(CDockManager *manager, const QString &title, QWidget* parent = nullptr);
271288

0 commit comments

Comments
 (0)