Properly implemented save and restore with central widget

This commit is contained in:
Uwe Kindler 2020-09-21 10:51:02 +02:00
parent 1c261515db
commit c370875128
2 changed files with 39 additions and 4 deletions

View File

@ -279,6 +279,24 @@ bool DockManagerPrivate::restoreStateFromXml(const QByteArray &state, int versi
int DockContainers = s.attributes().value("Containers").toInt();
#endif
ADS_PRINT(DockContainers);
const auto CentralWidgetAttribute = s.attributes().value("CentralWidget");
// If we have a central widget but a state without central widget, then
// something is wrong.
if (CentralWidget && CentralWidgetAttribute.isEmpty())
{
qWarning() << "Dock manager has central widget but saved state does not have central widget.";
return false;
}
// If the object name of the central widget does not match the name of the
// saved central widget, the something is wrong
if (CentralWidget->objectName() != CentralWidgetAttribute.toString())
{
qWarning() << "Object name of central widget does not match name of central widget in saved state.";
return false;
}
int DockContainerCount = 0;
while (s.readNextStartElement())
{
@ -405,7 +423,6 @@ bool DockManagerPrivate::restoreState(const QByteArray& State, int version)
return false;
}
CentralWidget = nullptr;
// Hide updates of floating widgets from use
hideFloatingWidgets();
markDockWidgetsDirty();
@ -419,6 +436,7 @@ bool DockManagerPrivate::restoreState(const QByteArray& State, int version)
restoreDockWidgetsOpenState();
restoreDockAreasIndices();
emitTopLevelEvents();
_this->dumpLayout();
return true;
}
@ -636,6 +654,10 @@ QByteArray CDockManager::saveState(int version) const
s.writeAttribute("Version", QString::number(CurrentVersion));
s.writeAttribute("UserVersion", QString::number(version));
s.writeAttribute("Containers", QString::number(d->Containers.count()));
if (d->CentralWidget)
{
s.writeAttribute("CentralWidget", d->CentralWidget->objectName());
}
for (auto Container : d->Containers)
{
Container->saveState(s);
@ -904,10 +926,20 @@ CDockAreaWidget* CDockManager::setCentralWidget(CDockWidget* widget)
return nullptr;
}
// Setting a new central widget is now allowed if there is alread a central
// widget
// Setting a new central widget is now allowed if there is already a central
// widget or if there are already other dock widgets
if (d->CentralWidget)
{
qWarning("Setting a central widget not possible because there is already a central widget.");
return nullptr;
}
// Setting a central widget is now allowed if there are already other
// dock widgets.
if (!d->DockWidgetsMap.isEmpty())
{
qWarning("Setting a central widget not possible - the central widget need to be the first "
"dock widget that is added to the dock manager.");
return nullptr;
}

View File

@ -398,9 +398,12 @@ public:
* movable, floatable or closable and the titlebar of the central
* dock area is not visible.
* If the given widget could be set as central widget, the function returns
* the created cok area. If the widget could not be set, because there
* the created dock area. If the widget could not be set, because there
* is already a central widget, this function returns a nullptr.
* To clear the central widget, pass a nullptr to the function.
* \note Setting a central widget is only possible if no other dock widgets
* have been registered before. That means, this function should be the
* first function that you call before you add other dock widgets.
* \retval != 0 The dock area that contains the central widget
* \retval nullptr Indicates that the given widget can not be set as central
* widget because there is already a central widget.