Skip to content

Commit

Permalink
libappfw|GuiWidget: Manually saving/restoring widget tree state
Browse files Browse the repository at this point in the history
Sometimes it is beneficial to save/restore state at other times
in addition to widget de/initialization.
  • Loading branch information
skyjake committed Apr 13, 2014
1 parent 2f04361 commit 65faca6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
12 changes: 12 additions & 0 deletions doomsday/libappfw/include/de/framework/guiwidget.h
Expand Up @@ -261,6 +261,18 @@ class LIBAPPFW_PUBLIC GuiWidget : public QObject, public Widget
*/
void enableStateSerialization(bool enabled = true);

/**
* Save the state of the widget and all its children (those who support state
* serialization).
*/
void saveState();

/**
* Restore the state of the widget and all its children (those who support state
* serialization).
*/
void restoreState();

// Events.
void initialize();
void deinitialize();
Expand Down
38 changes: 34 additions & 4 deletions doomsday/libappfw/src/guiwidget.cpp
Expand Up @@ -321,7 +321,6 @@ DENG2_PIMPL(GuiWidget)

void restoreState()
{
if(!stateSerializationEnabled) return;
try
{
if(IPersistent *po = self.maybeAs<IPersistent>())
Expand All @@ -339,7 +338,6 @@ DENG2_PIMPL(GuiWidget)

void saveState()
{
if(!stateSerializationEnabled) return;
try
{
if(IPersistent *po = self.maybeAs<IPersistent>())
Expand Down Expand Up @@ -548,6 +546,32 @@ void GuiWidget::enableStateSerialization(bool enabled)
d->stateSerializationEnabled = enabled;
}

void GuiWidget::saveState()
{
d->saveState();

foreach(Widget *child, childWidgets())
{
if(GuiWidget *widget = child->maybeAs<GuiWidget>())
{
widget->saveState();
}
}
}

void GuiWidget::restoreState()
{
d->restoreState();

foreach(Widget *child, childWidgets())
{
if(GuiWidget *widget = child->maybeAs<GuiWidget>())
{
widget->restoreState();
}
}
}

void GuiWidget::initialize()
{
if(d->inited) return;
Expand All @@ -557,7 +581,10 @@ void GuiWidget::initialize()
d->inited = true;
glInit();

d->restoreState();
if(d->stateSerializationEnabled)
{
d->restoreState();
}
}
catch(Error const &er)
{
Expand All @@ -572,7 +599,10 @@ void GuiWidget::deinitialize()

try
{
d->saveState();
if(d->stateSerializationEnabled)
{
d->saveState();
}

d->inited = false;
d->deinitBlur();
Expand Down

0 comments on commit 65faca6

Please sign in to comment.