valentina_old/src/libs/vpropertyexplorer/vpropertyset.cpp

271 lines
6.6 KiB
C++
Raw Normal View History

/************************************************************************
**
** @file vpropertyset.cpp
** @author hedgeware <internal(at)hedgeware.net>
** @date
**
** @brief
** @copyright
** All rights reserved. This program and the accompanying materials
** are made available under the terms of the GNU Lesser General Public License
** (LGPL) version 2.1 which accompanies this distribution, and is available at
** http://www.gnu.org/licenses/lgpl-2.1.html
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
*************************************************************************/
#include "vpropertyset.h"
#include <stddef.h>
#include <QForeachContainer>
#include <QList>
#include <QMap>
#include "vproperty.h"
#include "vpropertyset_p.h"
VPE::VPropertySet::VPropertySet()
: d_ptr(new VPropertySetPrivate())
{
}
VPE::VPropertySet::~VPropertySet()
{
// Delete all the properties
clear(true);
delete d_ptr;
}
bool VPE::VPropertySet::addProperty(VProperty *property, const QString &id, const QString &parentid)
{
// Check if the property to add is not a null pointer
2014-09-10 19:57:08 +02:00
if (!property)
{
return false;
2014-09-10 19:57:08 +02:00
}
VProperty* tmpParent = parentid.isEmpty() ? NULL : getProperty(parentid);
return addProperty(property, id, tmpParent);
}
bool VPE::VPropertySet::addProperty(VProperty *property, const QString &id, VProperty *parent_property)
{
// Check if the property to add is not a null pointer
2014-09-10 19:57:08 +02:00
if (!property)
{
return false;
2014-09-10 19:57:08 +02:00
}
QString tmpOldID = getPropertyID(property);
2014-09-10 19:57:08 +02:00
if (!tmpOldID.isEmpty())
{
d_ptr->Properties.remove(tmpOldID);
2014-09-10 19:57:08 +02:00
}
2014-09-10 19:57:08 +02:00
if (parent_property)
{
parent_property->addChild(property);
2014-09-10 19:57:08 +02:00
}
else
{
d_ptr->RootProperties.append(property);
2014-09-10 19:57:08 +02:00
if (property->getParent())
{
property->getParent()->removeChild(property);
2014-09-10 19:57:08 +02:00
}
}
2014-09-10 19:57:08 +02:00
if (!id.isEmpty())
{
d_ptr->Properties.insert(id, property);
2014-09-10 19:57:08 +02:00
}
return true;
}
bool VPE::VPropertySet::hasProperty(VProperty *property) const
{
2014-09-10 19:57:08 +02:00
if (!property)
{
return false;
2014-09-10 19:57:08 +02:00
}
return hasProperty(property, NULL);
}
VPE::VProperty *VPE::VPropertySet::getProperty(const QString &id) const
{
return d_ptr->Properties.value(id, NULL);
}
VPE::VProperty *VPE::VPropertySet::takeProperty(const QString &id)
{
VProperty* tmpProp = getProperty(id);
removeProperty(tmpProp, false);
// Return the property
return tmpProp;
}
void VPE::VPropertySet::removeProperty(const QString &id)
{
VProperty* tmpProp = takeProperty(id);
2014-09-14 11:16:59 +02:00
delete tmpProp;
}
void VPE::VPropertySet::removeProperty(VProperty* prop, bool delete_property)
{
// Remove all the children
removePropertyFromSet(prop);
// Remove from parent and optionally delete
prop->setParent(NULL);
if (delete_property)
{
delete prop;
}
}
int VPE::VPropertySet::count() const
{
return d_ptr->Properties.count();
}
void VPE::VPropertySet::clear(bool delete_properties)
{
d_ptr->Properties.clear();
2014-09-10 19:57:08 +02:00
while (!d_ptr->RootProperties.isEmpty())
{
VProperty* tmpProp = d_ptr->RootProperties.takeLast();
2014-09-10 19:57:08 +02:00
if (tmpProp != nullptr && delete_properties)
{
delete tmpProp;
}
}
}
QString VPE::VPropertySet::getPropertyID(const VProperty *prop, bool look_for_parent_id) const
{
QString tmpResult;
const VProperty* tmpCurrentProp = prop;
2014-09-10 19:57:08 +02:00
while (tmpCurrentProp && (look_for_parent_id || prop == tmpCurrentProp) && tmpResult.isEmpty())
{
// todo: The following code doesn't work, because .key() doesn't accept a const VProperty* pointer ...
//tmpResult = d_ptr->Properties.key(tmpCurrentProp, QString());
// ... which is why we need the code below
2014-09-10 19:57:08 +02:00
for (QMap<QString, VProperty*>::const_iterator i = d_ptr->Properties.constBegin();
i != d_ptr->Properties.constEnd(); ++i)
{
if (tmpCurrentProp == (*i))
{
return i.key();
2014-09-10 19:57:08 +02:00
}
}
tmpCurrentProp = tmpCurrentProp->getParent();
}
return tmpResult;
}
2015-04-15 14:44:57 +02:00
// cppcheck-suppress unusedFunction
const QMap<QString, VPE::VProperty *> &VPE::VPropertySet::getPropertiesMap() const
{
return d_ptr->Properties;
}
const QList<VPE::VProperty *> &VPE::VPropertySet::getRootProperties() const
{
return d_ptr->RootProperties;
}
VPE::VProperty *VPE::VPropertySet::getRootProperty(int row) const
{
return d_ptr->RootProperties.value(row, NULL);
}
int VPE::VPropertySet::getRootPropertyCount() const
{
return d_ptr->RootProperties.count();
}
VPE::VPropertySet* VPE::VPropertySet::clone() const
{
VPropertySet* tmpResult = new VPropertySet();
foreach(VProperty* tmpProperty, d_ptr->RootProperties)
cloneProperty(tmpProperty, NULL, tmpResult);
return tmpResult;
}
bool VPE::VPropertySet::hasProperty(VProperty *property, VProperty *parent) const
{
2014-09-10 19:57:08 +02:00
if (!property)
{
return false;
2014-09-10 19:57:08 +02:00
}
const QList<VProperty*>& tmpChildrenList = (parent != NULL ? parent->getChildren() : d_ptr->RootProperties);
2014-09-10 19:57:08 +02:00
foreach(VProperty* tmpProp, tmpChildrenList)
{
if (!tmpProp)
{
continue;
2014-09-10 19:57:08 +02:00
}
else if (tmpProp == property || hasProperty(property, tmpProp))
{
return true;
2014-09-10 19:57:08 +02:00
}
}
return false;
}
void VPE::VPropertySet::cloneProperty(VProperty* property_to_clone, VProperty *parent_property,
VPropertySet *output_set) const
{
2014-09-10 19:57:08 +02:00
if (!output_set || !property_to_clone || !hasProperty(property_to_clone))
{
return;
2014-09-10 19:57:08 +02:00
}
QString tmpID = getPropertyID(property_to_clone, false);
2014-09-10 19:57:08 +02:00
// We want to clone the children ourselves (because of the IDs)
VProperty* tmpNewProperty = property_to_clone->clone(false);
output_set->addProperty(tmpNewProperty, tmpID, parent_property);
2014-09-10 19:57:08 +02:00
for (int i = 0; i < property_to_clone->getRowCount(); ++i)
{
cloneProperty(property_to_clone->getChild(i), tmpNewProperty, output_set);
2014-09-10 19:57:08 +02:00
}
}
void VPE::VPropertySet::removePropertyFromSet(VProperty *prop)
{
// Remove all the children
foreach(VProperty* tmpChild, prop->getChildren())
removeProperty(tmpChild);
QList<QString> tmpKeys = d_ptr->Properties.keys(prop);
foreach(const QString& tmpID, tmpKeys)
d_ptr->Properties.remove(tmpID);
// Remove from list
d_ptr->RootProperties.removeAll(prop);
}