Skip to content

Commit c370875

Browse files
Properly implemented save and restore with central widget
1 parent 1c26151 commit c370875

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/DockManager.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,24 @@ bool DockManagerPrivate::restoreStateFromXml(const QByteArray &state, int versi
279279
int DockContainers = s.attributes().value("Containers").toInt();
280280
#endif
281281
ADS_PRINT(DockContainers);
282+
283+
const auto CentralWidgetAttribute = s.attributes().value("CentralWidget");
284+
// If we have a central widget but a state without central widget, then
285+
// something is wrong.
286+
if (CentralWidget && CentralWidgetAttribute.isEmpty())
287+
{
288+
qWarning() << "Dock manager has central widget but saved state does not have central widget.";
289+
return false;
290+
}
291+
292+
// If the object name of the central widget does not match the name of the
293+
// saved central widget, the something is wrong
294+
if (CentralWidget->objectName() != CentralWidgetAttribute.toString())
295+
{
296+
qWarning() << "Object name of central widget does not match name of central widget in saved state.";
297+
return false;
298+
}
299+
282300
int DockContainerCount = 0;
283301
while (s.readNextStartElement())
284302
{
@@ -405,7 +423,6 @@ bool DockManagerPrivate::restoreState(const QByteArray& State, int version)
405423
return false;
406424
}
407425

408-
CentralWidget = nullptr;
409426
// Hide updates of floating widgets from use
410427
hideFloatingWidgets();
411428
markDockWidgetsDirty();
@@ -419,6 +436,7 @@ bool DockManagerPrivate::restoreState(const QByteArray& State, int version)
419436
restoreDockWidgetsOpenState();
420437
restoreDockAreasIndices();
421438
emitTopLevelEvents();
439+
_this->dumpLayout();
422440

423441
return true;
424442
}
@@ -636,6 +654,10 @@ QByteArray CDockManager::saveState(int version) const
636654
s.writeAttribute("Version", QString::number(CurrentVersion));
637655
s.writeAttribute("UserVersion", QString::number(version));
638656
s.writeAttribute("Containers", QString::number(d->Containers.count()));
657+
if (d->CentralWidget)
658+
{
659+
s.writeAttribute("CentralWidget", d->CentralWidget->objectName());
660+
}
639661
for (auto Container : d->Containers)
640662
{
641663
Container->saveState(s);
@@ -904,10 +926,20 @@ CDockAreaWidget* CDockManager::setCentralWidget(CDockWidget* widget)
904926
return nullptr;
905927
}
906928

907-
// Setting a new central widget is now allowed if there is alread a central
908-
// widget
929+
// Setting a new central widget is now allowed if there is already a central
930+
// widget or if there are already other dock widgets
909931
if (d->CentralWidget)
910932
{
933+
qWarning("Setting a central widget not possible because there is already a central widget.");
934+
return nullptr;
935+
}
936+
937+
// Setting a central widget is now allowed if there are already other
938+
// dock widgets.
939+
if (!d->DockWidgetsMap.isEmpty())
940+
{
941+
qWarning("Setting a central widget not possible - the central widget need to be the first "
942+
"dock widget that is added to the dock manager.");
911943
return nullptr;
912944
}
913945

src/DockManager.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,12 @@ class ADS_EXPORT CDockManager : public CDockContainerWidget
398398
* movable, floatable or closable and the titlebar of the central
399399
* dock area is not visible.
400400
* If the given widget could be set as central widget, the function returns
401-
* the created cok area. If the widget could not be set, because there
401+
* the created dock area. If the widget could not be set, because there
402402
* is already a central widget, this function returns a nullptr.
403403
* To clear the central widget, pass a nullptr to the function.
404+
* \note Setting a central widget is only possible if no other dock widgets
405+
* have been registered before. That means, this function should be the
406+
* first function that you call before you add other dock widgets.
404407
* \retval != 0 The dock area that contains the central widget
405408
* \retval nullptr Indicates that the given widget can not be set as central
406409
* widget because there is already a central widget.

0 commit comments

Comments
 (0)