Renamed RecreateContentsWidgetOnCloseAndOpen into DeleteContentOnClose

This commit is contained in:
Uwe Kindler 2021-10-14 13:57:06 +02:00
parent 03a8eaa44f
commit de05ddd203
3 changed files with 36 additions and 33 deletions

View File

@ -41,12 +41,12 @@ int main(int argc, char *argv[])
now->widget()->setFocus();
});
QAction *action = new QAction("New Delete Dock Widget On Close", &w);
QAction *action = new QAction("New [DockWidgetDeleteOnClose]", &w);
w.menuBar()->addAction(action);
int i = 0;
QObject::connect(action, &QAction::triggered, [&]() {
auto dw = new ads::CDockWidget(QStringLiteral("test doc %1").arg(i++), &w);
auto dw = new ads::CDockWidget(QStringLiteral("test %1 [DockWidgetDeleteOnClose]").arg(i++), &w);
auto editor = new QTextEdit(QStringLiteral("lorem ipsum..."), dw);
dw->setWidget(editor);
dw->setFeature(ads::CDockWidget::DockWidgetDeleteOnClose, true);
@ -54,18 +54,19 @@ int main(int argc, char *argv[])
qDebug() << "doc dock widget created!" << dw << area;
});
auto dw = new ads::CDockWidget(QStringLiteral("test doc %1").arg(i++), &w);
auto dw = new ads::CDockWidget(QStringLiteral("test %1 [DeleteContentOnClose]").arg(i++), &w);
auto editor = new QTextEdit(QStringLiteral("recreated lorem ipsum......"), dw);
dw->setWidget(editor);
dw->setFeature(ads::CDockWidget::RecreateContentsWidgetOnCloseAndOpen, true);
dw->setWidgetFactory([](QWidget* dw){
dw->setFeature(ads::CDockWidget::DeleteContentOnClose, true);
dw->setWidgetFactory([](QWidget* dw)
{
static int timesRecreated = 0;
return new QTextEdit(QStringLiteral("recreated lorem ipsum... times %1").arg(++timesRecreated), dw);
});
auto area = dockManager->addDockWidgetTab(ads::CenterDockWidgetArea, dw);
qDebug() << "RecreateContentsWidgetOnCloseAndOpen dock widget created!" << dw << area;
qDebug() << "DeleteContentOnClose dock widget created!" << dw << area;
action = new QAction("Toggle Recreate Contents Widget On Close and Open", &w);
action = new QAction("Toggle [DeleteContentOnClose]", &w);
w.menuBar()->addAction(action);
QObject::connect(action, &QAction::triggered, [dw]() {

View File

@ -125,9 +125,9 @@ struct DockWidgetPrivate
void setupScrollArea();
/**
* Sets the widget factory that will be used to recreate and set the contents widget.
* Creates the content widget with the registered widget factory and
* returns true on success.
*/
void setWidgetFactory(CDockWidget::FactoryFunc createWidget, CDockWidget::eInsertMode insertMode);
bool createWidgetFromFactory();
};
// struct DockWidgetPrivate
@ -143,10 +143,13 @@ DockWidgetPrivate::DockWidgetPrivate(CDockWidget* _public) :
//============================================================================
void DockWidgetPrivate::showDockWidget()
{
if (!Widget) {
if(!createWidgetFromFactory()) {
Q_ASSERT(!Features.testFlag(CDockWidget::RecreateContentsWidgetOnCloseAndOpen)
&& "RecreateContentsWidgetOnCloseAndOpen flag was set, but the widget factory is missing or it doesn't return a valid QWidget.");
if (!Widget)
{
if (!createWidgetFromFactory())
{
Q_ASSERT(!Features.testFlag(CDockWidget::DeleteContentOnClose)
&& "DeleteContentOnClose flag was set, but the widget "
"factory is missing or it doesn't return a valid QWidget.");
return;
}
}
@ -189,7 +192,7 @@ void DockWidgetPrivate::hideDockWidget()
TabWidget->hide();
updateParentDockArea();
if (Features.testFlag(CDockWidget::RecreateContentsWidgetOnCloseAndOpen))
if (Features.testFlag(CDockWidget::DeleteContentOnClose))
{
Widget->deleteLater();
Widget = nullptr;
@ -246,19 +249,11 @@ void DockWidgetPrivate::setupScrollArea()
Layout->addWidget(ScrollArea);
}
void DockWidgetPrivate::setWidgetFactory(CDockWidget::FactoryFunc createWidget, CDockWidget::eInsertMode insertMode)
{
if (Factory)
{
delete Factory;
}
Factory = new WidgetFactory { createWidget, insertMode };
}
//============================================================================
bool DockWidgetPrivate::createWidgetFromFactory()
{
if (!Features.testFlag(CDockWidget::RecreateContentsWidgetOnCloseAndOpen))
if (!Features.testFlag(CDockWidget::DeleteContentOnClose))
{
return false;
}
@ -350,8 +345,14 @@ void CDockWidget::setWidget(QWidget* widget, eInsertMode InsertMode)
}
//============================================================================
void CDockWidget::setWidgetFactory(FactoryFunc createWidget, eInsertMode insertMode) {
d->setWidgetFactory(createWidget, insertMode);
void CDockWidget::setWidgetFactory(FactoryFunc createWidget, eInsertMode insertMode)
{
if (d->Factory)
{
delete d->Factory;
}
d->Factory = new DockWidgetPrivate::WidgetFactory { createWidget, insertMode };
}

View File

@ -155,7 +155,7 @@ public:
DockWidgetFocusable = 0x020, ///< if this is enabled, a dock widget can get focus highlighting
DockWidgetForceCloseWithArea = 0x040, ///< dock widget will be closed when the dock area hosting it is closed
NoTab = 0x080, ///< dock widget tab will never be shown if this flag is set
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.
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.
DefaultDockWidgetFeatures = DockWidgetClosable | DockWidgetMovable | DockWidgetFloatable | DockWidgetFocusable,
AllDockWidgetFeatures = DefaultDockWidgetFeatures | DockWidgetDeleteOnClose | CustomCloseHandling,
DockWidgetAlwaysCloseAndDelete = DockWidgetForceCloseWithArea | DockWidgetDeleteOnClose,
@ -272,12 +272,13 @@ public:
void setWidget(QWidget* widget, eInsertMode InsertMode = AutoScrollArea);
/**
* Only used when the flag RecreateContentsWidgetOnCloseAndOpen is set.
* Using the flag and setting a widget factory allows to free the resources of
* the widget of your application while retaining the position the next time you want to
* show your widget, unlike the flag DockWidgetDeleteOnClose which deletes the dock widget itself.
* Since we keep the dock widget, all regular features of ADS should work as normal, including
* saving and restoring the state of the docking system and using perspectives.
* Only used when the feature flag DeleteContentOnClose is set.
* Using the flag and setting a widget factory allows to free the resources
* of the widget of your application while retaining the position the next
* time you want to show your widget, unlike the flag DockWidgetDeleteOnClose
* which deletes the dock widget itself. Since we keep the dock widget, all
* regular features of ADS should work as normal, including saving and
* restoring the state of the docking system and using perspectives.
*/
using FactoryFunc = std::function<QWidget*(QWidget*)>;
void setWidgetFactory(FactoryFunc createWidget, eInsertMode InsertMode = AutoScrollArea);