Better implementation of empty QSplitter deletion (no longer crashes).

Call it after restore, because sometimes one splitter was left ?-/
This commit is contained in:
mfreiholz
2016-03-08 10:58:56 +01:00
parent 94d8fe3e25
commit 9bbc5e41a3
5 changed files with 57 additions and 15 deletions

View File

@@ -3,21 +3,48 @@
#include <QWidget>
#include <QSplitter>
#include <QLayout>
#include <QVariant>
#include "ads/ContainerWidget.h"
#include "ads/SectionWidget.h"
ADS_NAMESPACE_BEGIN
static bool splitterContainsSectionWidget(QSplitter* splitter)
{
for (int i = 0; i < splitter->count(); ++i)
{
QWidget* w = splitter->widget(i);
QSplitter* sp = qobject_cast<QSplitter*>(w);
SectionWidget* sw = NULL;
if (sp && splitterContainsSectionWidget(sp))
return true;
else if ((sw = qobject_cast<SectionWidget*>(w)) != NULL)
return true;
}
return false;
}
void deleteEmptySplitter(ContainerWidget* container)
{
QList<QSplitter*> splitters = container->findChildren<QSplitter*>();
for (int i = 0; i < splitters.count(); ++i)
bool doAgain = false;
do
{
QSplitter* sp = splitters.at(i);
if (sp->count() == 0)
doAgain = false;
QList<QSplitter*> splitters = container->findChildren<QSplitter*>();
for (int i = 0; i < splitters.count(); ++i)
{
QSplitter* sp = splitters.at(i);
if (!sp->property("ads-splitter").toBool())
continue;
if (sp->count() > 0 && splitterContainsSectionWidget(sp))
continue;
delete splitters[i];
doAgain = true;
break;
}
}
while (doAgain);
}
ContainerWidget* findParentContainerWidget(QWidget* w)

View File

@@ -22,6 +22,7 @@ ADS_NAMESPACE_BEGIN
static QSplitter* newSplitter(Qt::Orientation orientation = Qt::Horizontal, QWidget* parent = 0)
{
QSplitter* s = new QSplitter(orientation, parent);
s->setProperty("ads-splitter", QVariant(true));
s->setChildrenCollapsible(false);
s->setOpaqueResize(false);
return s;
@@ -348,9 +349,9 @@ QByteArray ContainerWidget::saveState() const
{
// Looks like the user has hidden all contents and no more sections
// are available. We can simply write a list of all hidden contents.
out << 0;
out << _hiddenSectionContents.count();
out << 0; // Mode
out << _hiddenSectionContents.count();
QHashIterator<int, HiddenSectionItem> iter(_hiddenSectionContents);
while (iter.hasNext())
{
@@ -360,13 +361,15 @@ QByteArray ContainerWidget::saveState() const
}
else if (_mainLayout->count() == 1)
{
out << 1; // Mode
// There should only be one!
out << 1;
QLayoutItem* li = _mainLayout->itemAt(0);
if (!li->widget())
qFatal("Not a widget in _mainLayout, this shouldn't happen.");
else
saveSectionWidgets(out, li->widget());
// Save sections beginning with the first QSplitter (li->widget()).
saveSectionWidgets(out, li->widget());
// Safe state of hidden contents, which doesn't have an section association
// or the section association points to a no longer existing section.
@@ -563,6 +566,8 @@ bool ContainerWidget::restoreState(const QByteArray& data)
for (int i = 0; i < contentsToHide.count(); ++i)
hideSectionContent(contentsToHide.at(i));
deleteEmptySplitter(this);
qDebug() << "End of restore state" << success;
return success;
}