Seamly2D
Code documentation
vpropertyset.cpp
Go to the documentation of this file.
1 /************************************************************************
2  **
3  ** @file vpropertyset.cpp
4  ** @author hedgeware <internal(at)hedgeware.net>
5  ** @date
6  **
7  ** @brief
8  ** @copyright
9  ** All rights reserved. This program and the accompanying materials
10  ** are made available under the terms of the GNU Lesser General Public License
11  ** (LGPL) version 2.1 which accompanies this distribution, and is available at
12  ** http://www.gnu.org/licenses/lgpl-2.1.html
13  **
14  ** This library is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  ** Lesser General Public License for more details.
18  **
19  *************************************************************************/
20 
21 #include "vpropertyset.h"
22 
23 #include <stddef.h>
24 #include <QList>
25 #include <QMap>
26 
27 #include "vproperty.h"
28 #include "vpropertyset_p.h"
29 
30 
32  : d_ptr(new VPropertySetPrivate())
33 {
34 }
35 
36 
38 {
39  // Delete all the properties
40  clear(true);
41 
42  delete d_ptr;
43 }
44 
45 bool VPE::VPropertySet::addProperty(VProperty *property, const QString &id, const QString &parentid)
46 {
47  // Check if the property to add is not a null pointer
48  if (!property)
49  {
50  return false;
51  }
52 
53  VProperty* tmpParent = parentid.isEmpty() ? nullptr : getProperty(parentid);
54  return addProperty(property, id, tmpParent);
55 }
56 
57 bool VPE::VPropertySet::addProperty(VProperty *property, const QString &id, VProperty *parent_property)
58 {
59  // Check if the property to add is not a null pointer
60  if (!property)
61  {
62  return false;
63  }
64 
65  QString tmpOldID = getPropertyID(property);
66  if (!tmpOldID.isEmpty())
67  {
68  d_ptr->Properties.remove(tmpOldID);
69  }
70 
71  if (parent_property)
72  {
73  parent_property->addChild(property);
74  }
75  else
76  {
77  d_ptr->RootProperties.append(property);
78  if (property->getParent())
79  {
80  property->getParent()->removeChild(property);
81  }
82  }
83 
84  if (!id.isEmpty())
85  {
86  d_ptr->Properties.insert(id, property);
87  }
88 
89  return true;
90 }
91 
93 {
94  if (!property)
95  {
96  return false;
97  }
98 
99  return hasProperty(property, nullptr);
100 }
101 
103 {
104  return d_ptr->Properties.value(id, nullptr);
105 }
106 
108 {
109  VProperty* tmpProp = getProperty(id);
110  removeProperty(tmpProp, false);
111 
112  // Return the property
113  return tmpProp;
114 }
115 
116 void VPE::VPropertySet::removeProperty(const QString &id)
117 {
118  VProperty* tmpProp = takeProperty(id);
119  delete tmpProp;
120 }
121 
122 void VPE::VPropertySet::removeProperty(VProperty* prop, bool delete_property)
123 {
124  // Remove all the children
125  removePropertyFromSet(prop);
126 
127  // Remove from parent and optionally delete
128  prop->setParent(nullptr);
129 
130  if (delete_property)
131  {
132  delete prop;
133  }
134 }
135 
137 {
138  return d_ptr->Properties.count();
139 }
140 
141 void VPE::VPropertySet::clear(bool delete_properties)
142 {
143  d_ptr->Properties.clear();
144  while (!d_ptr->RootProperties.isEmpty())
145  {
146  VProperty* tmpProp = d_ptr->RootProperties.takeLast();
147  if (tmpProp != nullptr && delete_properties)
148  {
149  delete tmpProp;
150  }
151  }
152 }
153 
154 QString VPE::VPropertySet::getPropertyID(const VProperty *prop, bool look_for_parent_id) const
155 {
156 // QString tmpResult;
157  const VProperty* tmpCurrentProp = prop;
158 
159  while (tmpCurrentProp && (look_for_parent_id || prop == tmpCurrentProp) /*&& tmpResult.isEmpty()*/)
160  {
161 
162  // todo: The following code doesn't work, because .key() doesn't accept a const VProperty* pointer ...
163  //tmpResult = d_ptr->Properties.key(tmpCurrentProp, QString());
164 
165  // ... which is why we need the code below
166  for (QMap<QString, VProperty*>::const_iterator i = d_ptr->Properties.constBegin();
167  i != d_ptr->Properties.constEnd(); ++i)
168  {
169  if (tmpCurrentProp == (*i))
170  {
171  return i.key();
172  }
173  }
174 
175  tmpCurrentProp = tmpCurrentProp->getParent();
176  }
177 
178 // return tmpResult;
179  return QString();
180 }
181 
182 // cppcheck-suppress unusedFunction
184 {
185  return d_ptr->Properties;
186 }
187 
189 {
190  return d_ptr->RootProperties;
191 }
192 
194 {
195  return d_ptr->RootProperties.value(row, nullptr);
196 }
197 
199 {
200  return d_ptr->RootProperties.count();
201 }
202 
204 {
205  VPropertySet* tmpResult = new VPropertySet();
206 
207  foreach(VProperty* tmpProperty, d_ptr->RootProperties)
208  cloneProperty(tmpProperty, nullptr, tmpResult);
209 
210 
211  return tmpResult;
212 }
213 
214 bool VPE::VPropertySet::hasProperty(VProperty *property, VProperty *parent) const
215 {
216  if (!property)
217  {
218  return false;
219  }
220 
221  const QList<VProperty*>& tmpChildrenList = (parent != nullptr ? parent->getChildren() : d_ptr->RootProperties);
222  foreach(VProperty* tmpProp, tmpChildrenList)
223  {
224  if (!tmpProp)
225  {
226  continue;
227  }
228  else if (tmpProp == property || hasProperty(property, tmpProp))
229  {
230  return true;
231  }
232  }
233 
234  return false;
235 }
236 
237 void VPE::VPropertySet::cloneProperty(VProperty* property_to_clone, VProperty *parent_property,
238  VPropertySet *output_set) const
239 {
240  if (!output_set || !property_to_clone || !hasProperty(property_to_clone))
241  {
242  return;
243  }
244 
245  QString tmpID = getPropertyID(property_to_clone, false);
246 
247  // We want to clone the children ourselves (because of the IDs)
248  VProperty* tmpNewProperty = property_to_clone->clone(false);
249 
250  output_set->addProperty(tmpNewProperty, tmpID, parent_property);
251  for (int i = 0; i < property_to_clone->getRowCount(); ++i)
252  {
253  cloneProperty(property_to_clone->getChild(i), tmpNewProperty, output_set);
254  }
255 }
256 
258 {
259  // Remove all the children
260  foreach(VProperty* tmpChild, prop->getChildren())
261  removeProperty(tmpChild);
262 
263 
264  QList<QString> tmpKeys = d_ptr->Properties.keys(prop);
265  foreach(const QString& tmpID, tmpKeys)
266  d_ptr->Properties.remove(tmpID);
267 
268  // Remove from list
269  d_ptr->RootProperties.removeAll(prop);
270 }
VPropertySet is a simple class for managing a set of properties. If you don't need all the Model-func...
Definition: vpropertyset.h:48
virtual void removeProperty(const QString &id)
Removes a property from the set and deletes it.
Q_REQUIRED_RESULT VPropertySet * clone() const
Clones the property set.
virtual void clear(bool delete_properties=true)
Clears the set and (optionally) deletes all properties.
virtual ~VPropertySet()
Destructor.
VProperty * getRootProperty(int row) const
Returns the root property in a certain row.
virtual bool addProperty(VProperty *property, const QString &id, const QString &parentid)
Adds the property to the model and attaches it to the parentid. Note that if the property has a paren...
virtual int count() const
Returns the number of properties with in ID that are directly accessable by getProperty()
VPropertySet()
Default constructor, creating an empty property set.
virtual QString getPropertyID(const VProperty *prop, bool look_for_parent_id=true) const
Returns the ID of the property within the set The concept of property IDs is, that the object that ma...
virtual void removePropertyFromSet(VProperty *prop)
Recursivly removes a property's child properties from the set, but not from the parent.
virtual bool hasProperty(VProperty *property) const
Checks whether a property belongs to this set and returns the result.
virtual VProperty * getProperty(const QString &id) const
Gets a property by it's ID.
void cloneProperty(VProperty *property_to_clone, VProperty *parent_property, VPropertySet *output_set) const
Clones a property into another property set.
int getRootPropertyCount() const
Returns the number of independent properties.
const QMap< QString, VProperty * > & getPropertiesMap() const
Returns a const reference to the map of properties.
const QList< VProperty * > & getRootProperties() const
Returns a const reference to the list of root properties.
virtual VProperty * takeProperty(const QString &id)
Removes a property from the set and returns it.
virtual Q_REQUIRED_RESULT VProperty * clone(bool include_children=true, VProperty *container=nullptr) const
Clones this property.
Definition: vproperty.cpp:372
virtual int getRowCount() const
Gets the number of children.
Definition: vproperty.cpp:245
virtual VProperty * getParent() const
Gets the parent of this property.
Definition: vproperty.cpp:251
virtual int addChild(VProperty *child)
Adds a child to this property.
Definition: vproperty.cpp:278
virtual VProperty * getChild(int row) const
Returns the child at a certain row.
Definition: vproperty.cpp:234
virtual void setParent(VProperty *parent)
Sets the parent of this property.
Definition: vproperty.cpp:257
virtual QList< VProperty * > & getChildren()
Returns a reference to the list of children.
Definition: vproperty.cpp:222