Skip to content

Commit de05ddd

Browse files
Renamed RecreateContentsWidgetOnCloseAndOpen into DeleteContentOnClose
1 parent 03a8eaa commit de05ddd

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

examples/deleteonclose/main.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,32 @@ int main(int argc, char *argv[])
4141
now->widget()->setFocus();
4242
});
4343

44-
QAction *action = new QAction("New Delete Dock Widget On Close", &w);
44+
QAction *action = new QAction("New [DockWidgetDeleteOnClose]", &w);
4545
w.menuBar()->addAction(action);
4646

4747
int i = 0;
4848
QObject::connect(action, &QAction::triggered, [&]() {
49-
auto dw = new ads::CDockWidget(QStringLiteral("test doc %1").arg(i++), &w);
49+
auto dw = new ads::CDockWidget(QStringLiteral("test %1 [DockWidgetDeleteOnClose]").arg(i++), &w);
5050
auto editor = new QTextEdit(QStringLiteral("lorem ipsum..."), dw);
5151
dw->setWidget(editor);
5252
dw->setFeature(ads::CDockWidget::DockWidgetDeleteOnClose, true);
5353
auto area = dockManager->addDockWidgetTab(ads::CenterDockWidgetArea, dw);
5454
qDebug() << "doc dock widget created!" << dw << area;
5555
});
5656

57-
auto dw = new ads::CDockWidget(QStringLiteral("test doc %1").arg(i++), &w);
57+
auto dw = new ads::CDockWidget(QStringLiteral("test %1 [DeleteContentOnClose]").arg(i++), &w);
5858
auto editor = new QTextEdit(QStringLiteral("recreated lorem ipsum......"), dw);
5959
dw->setWidget(editor);
60-
dw->setFeature(ads::CDockWidget::RecreateContentsWidgetOnCloseAndOpen, true);
61-
dw->setWidgetFactory([](QWidget* dw){
60+
dw->setFeature(ads::CDockWidget::DeleteContentOnClose, true);
61+
dw->setWidgetFactory([](QWidget* dw)
62+
{
6263
static int timesRecreated = 0;
6364
return new QTextEdit(QStringLiteral("recreated lorem ipsum... times %1").arg(++timesRecreated), dw);
6465
});
6566
auto area = dockManager->addDockWidgetTab(ads::CenterDockWidgetArea, dw);
66-
qDebug() << "RecreateContentsWidgetOnCloseAndOpen dock widget created!" << dw << area;
67+
qDebug() << "DeleteContentOnClose dock widget created!" << dw << area;
6768

68-
action = new QAction("Toggle Recreate Contents Widget On Close and Open", &w);
69+
action = new QAction("Toggle [DeleteContentOnClose]", &w);
6970
w.menuBar()->addAction(action);
7071

7172
QObject::connect(action, &QAction::triggered, [dw]() {

src/DockWidget.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ struct DockWidgetPrivate
125125
void setupScrollArea();
126126

127127
/**
128-
* Sets the widget factory that will be used to recreate and set the contents widget.
128+
* Creates the content widget with the registered widget factory and
129+
* returns true on success.
129130
*/
130-
void setWidgetFactory(CDockWidget::FactoryFunc createWidget, CDockWidget::eInsertMode insertMode);
131131
bool createWidgetFromFactory();
132132
};
133133
// struct DockWidgetPrivate
@@ -143,10 +143,13 @@ DockWidgetPrivate::DockWidgetPrivate(CDockWidget* _public) :
143143
//============================================================================
144144
void DockWidgetPrivate::showDockWidget()
145145
{
146-
if (!Widget) {
147-
if(!createWidgetFromFactory()) {
148-
Q_ASSERT(!Features.testFlag(CDockWidget::RecreateContentsWidgetOnCloseAndOpen)
149-
&& "RecreateContentsWidgetOnCloseAndOpen flag was set, but the widget factory is missing or it doesn't return a valid QWidget.");
146+
if (!Widget)
147+
{
148+
if (!createWidgetFromFactory())
149+
{
150+
Q_ASSERT(!Features.testFlag(CDockWidget::DeleteContentOnClose)
151+
&& "DeleteContentOnClose flag was set, but the widget "
152+
"factory is missing or it doesn't return a valid QWidget.");
150153
return;
151154
}
152155
}
@@ -189,7 +192,7 @@ void DockWidgetPrivate::hideDockWidget()
189192
TabWidget->hide();
190193
updateParentDockArea();
191194

192-
if (Features.testFlag(CDockWidget::RecreateContentsWidgetOnCloseAndOpen))
195+
if (Features.testFlag(CDockWidget::DeleteContentOnClose))
193196
{
194197
Widget->deleteLater();
195198
Widget = nullptr;
@@ -246,19 +249,11 @@ void DockWidgetPrivate::setupScrollArea()
246249
Layout->addWidget(ScrollArea);
247250
}
248251

249-
void DockWidgetPrivate::setWidgetFactory(CDockWidget::FactoryFunc createWidget, CDockWidget::eInsertMode insertMode)
250-
{
251-
if (Factory)
252-
{
253-
delete Factory;
254-
}
255-
256-
Factory = new WidgetFactory { createWidget, insertMode };
257-
}
258252

253+
//============================================================================
259254
bool DockWidgetPrivate::createWidgetFromFactory()
260255
{
261-
if (!Features.testFlag(CDockWidget::RecreateContentsWidgetOnCloseAndOpen))
256+
if (!Features.testFlag(CDockWidget::DeleteContentOnClose))
262257
{
263258
return false;
264259
}
@@ -350,8 +345,14 @@ void CDockWidget::setWidget(QWidget* widget, eInsertMode InsertMode)
350345
}
351346

352347
//============================================================================
353-
void CDockWidget::setWidgetFactory(FactoryFunc createWidget, eInsertMode insertMode) {
354-
d->setWidgetFactory(createWidget, insertMode);
348+
void CDockWidget::setWidgetFactory(FactoryFunc createWidget, eInsertMode insertMode)
349+
{
350+
if (d->Factory)
351+
{
352+
delete d->Factory;
353+
}
354+
355+
d->Factory = new DockWidgetPrivate::WidgetFactory { createWidget, insertMode };
355356
}
356357

357358

src/DockWidget.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ private Q_SLOTS:
155155
DockWidgetFocusable = 0x020, ///< if this is enabled, a dock widget can get focus highlighting
156156
DockWidgetForceCloseWithArea = 0x040, ///< dock widget will be closed when the dock area hosting it is closed
157157
NoTab = 0x080, ///< dock widget tab will never be shown if this flag is set
158-
RecreateContentsWidgetOnCloseAndOpen = 0x100, ///< deletes only the contained widget on close, keeping the dock widget intact and in place. Attempts to rebuild the contents widget on show if there is a widget factory set.
158+
DeleteContentOnClose = 0x100, ///< deletes only the contained widget on close, keeping the dock widget intact and in place. Attempts to rebuild the contents widget on show if there is a widget factory set.
159159
DefaultDockWidgetFeatures = DockWidgetClosable | DockWidgetMovable | DockWidgetFloatable | DockWidgetFocusable,
160160
AllDockWidgetFeatures = DefaultDockWidgetFeatures | DockWidgetDeleteOnClose | CustomCloseHandling,
161161
DockWidgetAlwaysCloseAndDelete = DockWidgetForceCloseWithArea | DockWidgetDeleteOnClose,
@@ -272,12 +272,13 @@ private Q_SLOTS:
272272
void setWidget(QWidget* widget, eInsertMode InsertMode = AutoScrollArea);
273273

274274
/**
275-
* Only used when the flag RecreateContentsWidgetOnCloseAndOpen is set.
276-
* Using the flag and setting a widget factory allows to free the resources of
277-
* the widget of your application while retaining the position the next time you want to
278-
* show your widget, unlike the flag DockWidgetDeleteOnClose which deletes the dock widget itself.
279-
* Since we keep the dock widget, all regular features of ADS should work as normal, including
280-
* saving and restoring the state of the docking system and using perspectives.
275+
* Only used when the feature flag DeleteContentOnClose is set.
276+
* Using the flag and setting a widget factory allows to free the resources
277+
* of the widget of your application while retaining the position the next
278+
* time you want to show your widget, unlike the flag DockWidgetDeleteOnClose
279+
* which deletes the dock widget itself. Since we keep the dock widget, all
280+
* regular features of ADS should work as normal, including saving and
281+
* restoring the state of the docking system and using perspectives.
281282
*/
282283
using FactoryFunc = std::function<QWidget*(QWidget*)>;
283284
void setWidgetFactory(FactoryFunc createWidget, eInsertMode InsertMode = AutoScrollArea);

0 commit comments

Comments
 (0)