2014-09-10 17:27:45 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @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"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <QList>
|
|
|
|
#include <QMap>
|
|
|
|
|
|
|
|
#include "vproperty.h"
|
2014-09-10 17:27:45 +02:00
|
|
|
#include "vpropertyset_p.h"
|
|
|
|
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VPropertySet::VPropertySet()
|
2022-08-12 17:50:13 +02:00
|
|
|
// cppcheck-suppress noCopyConstructor
|
|
|
|
// cppcheck-suppress noOperatorEq
|
2014-09-10 17:27:45 +02:00
|
|
|
: d_ptr(new VPropertySetPrivate())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VPropertySet::~VPropertySet()
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
// Delete all the properties
|
2022-08-08 14:25:14 +02:00
|
|
|
VPE::VPropertySet::clear(true);
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
delete d_ptr;
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
bool VPE::VPropertySet::addProperty(VProperty *property, const QString &id, const QString &parentid)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
// Check if the property to add is not a null pointer
|
2014-09-10 19:57:08 +02:00
|
|
|
if (!property)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return false;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2017-07-05 18:35:34 +02:00
|
|
|
VProperty* tmpParent = parentid.isEmpty() ? nullptr : getProperty(parentid);
|
2014-09-10 17:27:45 +02:00
|
|
|
return addProperty(property, id, tmpParent);
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
bool VPE::VPropertySet::addProperty(VProperty *property, const QString &id, VProperty *parent_property)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
// Check if the property to add is not a null pointer
|
2014-09-10 19:57:08 +02:00
|
|
|
if (!property)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return false;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
QString tmpOldID = getPropertyID(property);
|
2014-09-10 19:57:08 +02:00
|
|
|
if (!tmpOldID.isEmpty())
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
d_ptr->Properties.remove(tmpOldID);
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
if (parent_property)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
parent_property->addChild(property);
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
d_ptr->RootProperties.append(property);
|
2014-09-10 19:57:08 +02:00
|
|
|
if (property->getParent())
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
property->getParent()->removeChild(property);
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
if (!id.isEmpty())
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
d_ptr->Properties.insert(id, property);
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
bool VPE::VPropertySet::hasProperty(VProperty *property) const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (!property)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return false;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2017-07-05 18:35:34 +02:00
|
|
|
return hasProperty(property, nullptr);
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VProperty *VPE::VPropertySet::getProperty(const QString &id) const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2017-07-05 18:35:34 +02:00
|
|
|
return d_ptr->Properties.value(id, nullptr);
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VProperty *VPE::VPropertySet::takeProperty(const QString &id)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
VProperty* tmpProp = getProperty(id);
|
|
|
|
removeProperty(tmpProp, false);
|
|
|
|
|
|
|
|
// Return the property
|
|
|
|
return tmpProp;
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VPropertySet::removeProperty(const QString &id)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
VProperty* tmpProp = takeProperty(id);
|
2014-09-14 11:16:59 +02:00
|
|
|
delete tmpProp;
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VPropertySet::removeProperty(VProperty* prop, bool delete_property)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
// Remove all the children
|
|
|
|
removePropertyFromSet(prop);
|
|
|
|
|
|
|
|
// Remove from parent and optionally delete
|
2017-07-05 18:35:34 +02:00
|
|
|
prop->setParent(nullptr);
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2016-12-21 13:20:42 +01:00
|
|
|
if (delete_property)
|
|
|
|
{
|
|
|
|
delete prop;
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
int VPE::VPropertySet::count() const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
return d_ptr->Properties.count();
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VPropertySet::clear(bool delete_properties)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
d_ptr->Properties.clear();
|
2014-09-10 19:57:08 +02:00
|
|
|
while (!d_ptr->RootProperties.isEmpty())
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
VProperty* tmpProp = d_ptr->RootProperties.takeLast();
|
2014-09-10 19:57:08 +02:00
|
|
|
if (tmpProp != nullptr && delete_properties)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
delete tmpProp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
QString VPE::VPropertySet::getPropertyID(const VProperty *prop, bool look_for_parent_id) const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2017-07-06 11:58:26 +02:00
|
|
|
// QString tmpResult;
|
2014-09-10 17:27:45 +02:00
|
|
|
const VProperty* tmpCurrentProp = prop;
|
|
|
|
|
2017-07-06 11:58:26 +02:00
|
|
|
while (tmpCurrentProp && (look_for_parent_id || prop == tmpCurrentProp) /*&& tmpResult.isEmpty()*/)
|
2014-09-10 19:57:08 +02:00
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
// 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))
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return i.key();
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpCurrentProp = tmpCurrentProp->getParent();
|
|
|
|
}
|
|
|
|
|
2017-07-06 11:58:26 +02:00
|
|
|
// return tmpResult;
|
|
|
|
return QString();
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2016-12-21 13:02:55 +01:00
|
|
|
const QMap<QString, VPE::VProperty *> &VPE::VPropertySet::getPropertiesMap() const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
return d_ptr->Properties;
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
const QList<VPE::VProperty *> &VPE::VPropertySet::getRootProperties() const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
return d_ptr->RootProperties;
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VProperty *VPE::VPropertySet::getRootProperty(int row) const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2017-07-05 18:35:34 +02:00
|
|
|
return d_ptr->RootProperties.value(row, nullptr);
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
int VPE::VPropertySet::getRootPropertyCount() const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
return d_ptr->RootProperties.count();
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VPropertySet* VPE::VPropertySet::clone() const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
VPropertySet* tmpResult = new VPropertySet();
|
|
|
|
|
2018-04-03 10:15:58 +02:00
|
|
|
const QList<VProperty*> rootProperties = d_ptr->RootProperties;
|
|
|
|
for (auto tmpProperty : rootProperties)
|
|
|
|
{
|
2017-07-05 18:35:34 +02:00
|
|
|
cloneProperty(tmpProperty, nullptr, tmpResult);
|
2018-04-03 10:15:58 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
return tmpResult;
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
bool VPE::VPropertySet::hasProperty(VProperty *property, VProperty *parent) const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (!property)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return false;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2017-07-05 18:35:34 +02:00
|
|
|
const QList<VProperty*>& tmpChildrenList = (parent != nullptr ? parent->getChildren() : d_ptr->RootProperties);
|
2022-08-08 14:25:14 +02:00
|
|
|
return std::any_of(tmpChildrenList.begin(), tmpChildrenList.end(), [this, property](VProperty* tmpProp)
|
|
|
|
{return tmpProp && (tmpProp == property || hasProperty(property, tmpProp));});
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VPropertySet::cloneProperty(VProperty* property_to_clone, VProperty *parent_property,
|
|
|
|
VPropertySet *output_set) const
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (!output_set || !property_to_clone || !hasProperty(property_to_clone))
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +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);
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
cloneProperty(property_to_clone->getChild(i), tmpNewProperty, output_set);
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VPropertySet::removePropertyFromSet(VProperty *prop)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
// Remove all the children
|
2018-04-03 10:15:58 +02:00
|
|
|
const QList<VPE::VProperty*>& children = prop->getChildren();
|
|
|
|
for (auto tmpChild : children)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
removeProperty(tmpChild);
|
2018-04-03 10:15:58 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2018-04-03 10:15:58 +02:00
|
|
|
const QList<QString> tmpKeys = d_ptr->Properties.keys(prop);
|
|
|
|
for (auto &tmpID : tmpKeys)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
d_ptr->Properties.remove(tmpID);
|
2018-04-03 10:15:58 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
// Remove from list
|
|
|
|
d_ptr->RootProperties.removeAll(prop);
|
|
|
|
}
|