Refactors: Move save/restore impl into separate methods.

Updates SectionContent API: Its only possible to use it as shared pointer
(RefPtr), Copies not possible.
Handle missing or too many content references in restore procecure.
This commit is contained in:
mfreiholz
2016-02-17 14:42:46 +01:00
parent 1b91fe241b
commit eaff4f3d4e
6 changed files with 155 additions and 53 deletions

View File

@@ -1,5 +1,6 @@
#include "ads/SectionContent.h"
#include <QDebug>
#include <QLabel>
ADS_NAMESPACE_BEGIN
@@ -8,14 +9,42 @@ int SectionContent::NextUid = 1;
QHash<int, SectionContent::WeakPtr> SectionContent::LookupMap;
QHash<QString, SectionContent::WeakPtr> SectionContent::LookupMapByName;
SectionContent::SectionContent(QWidget* title, QWidget* content, const QString& uniqueName) :
_uid(NextUid++),
_uniqueName(uniqueName),
_title(title),
_content(content)
SectionContent::SectionContent() :
_uid(NextUid++)
{
}
SectionContent::RefPtr SectionContent::newSectionContent(const QString& uniqueName, ContainerWidget* container, QWidget* title, QWidget* content)
{
if (uniqueName.isEmpty())
{
qFatal("Can not create SectionContent with empty uniqueName");
return RefPtr();
}
else if (LookupMapByName.contains(uniqueName))
{
qFatal("Can not create SectionContent with already used uniqueName");
return RefPtr();
}
else if (!container || !title || !content)
{
qFatal("Can not create SectionContent with NULL values");
return RefPtr();
}
QSharedPointer<SectionContent> sc(new SectionContent());
sc->_uniqueName = uniqueName;
sc->_container = container;
sc->_title = title;
sc->_content = content;
LookupMap.insert(sc->uid(), sc);
if (!sc->uniqueName().isEmpty())
LookupMapByName.insert(sc->uniqueName(), sc);
return sc;
}
SectionContent::~SectionContent()
{
LookupMap.remove(_uid);
@@ -34,6 +63,11 @@ QString SectionContent::uniqueName() const
return _uniqueName;
}
ContainerWidget* SectionContent::containerWidget() const
{
return _container;
}
QWidget* SectionContent::titleWidget() const
{
return _title;
@@ -44,13 +78,4 @@ QWidget* SectionContent::contentWidget() const
return _content;
}
SectionContent::RefPtr SectionContent::newSectionContent(QWidget* title, QWidget* content, const QString& uniqueName)
{
QSharedPointer<SectionContent> sc(new SectionContent(title, content, uniqueName));
LookupMap.insert(sc->uid(), sc);
if (!sc->uniqueName().isEmpty())
LookupMapByName.insert(sc->uniqueName(), sc);
return sc;
}
ADS_NAMESPACE_END