Seamly2D
Code documentation
vproperty.cpp
Go to the documentation of this file.
1 /************************************************************************
2  **
3  ** @file vproperty.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 "vproperty.h"
22 
23 #include <QByteArray>
24 #include <QFlags>
25 #include <QItemEditorFactory>
26 #include <QLineEdit>
27 #include <QList>
28 #include <QMetaProperty>
29 #include <QObject>
30 #include <QStandardItemEditorCreator>
31 #include <QWidget>
32 
33 #include "vproperty_p.h"
34 
35 //! Standard constructor, takes a name and a parent property as argument
36 VPE::VProperty::VProperty(const QString &name, QVariant::Type type)
37  : QObject()
38  , d_ptr(new VPropertyPrivate(name, type))
39 {
40 
41 }
42 
44  : d_ptr(d)
45 {
46 }
47 
48 
50 {
51  setParent(nullptr);
52 
53  while (!d_ptr->Children.isEmpty())
54  {
55  VProperty *tmpChild = d_ptr->Children.takeLast();
56  delete tmpChild;
57  }
58 
59  delete d_ptr;
60 }
61 
62 QString VPE::VProperty::type() const
63 {
64  return "string";
65 }
66 
67 //! Get the data how it should be displayed
68 QVariant VPE::VProperty::data (int column, int role) const
69 {
70  if (column == DPC_Name && Qt::DisplayRole == role)
71  {
72  return QVariant(d_ptr->Name);
73  }
74  else if (column == DPC_Data && (Qt::DisplayRole == role || Qt::EditRole == role))
75  {
76  return d_ptr->VariantValue;
77  }
78  else if (Qt::ToolTipRole == role)
79  {
80  return QVariant(d_ptr->Description);
81  }
82  else
83  return QVariant();
84 }
85 
86 bool VPE::VProperty::setData(const QVariant &data, int role)
87 {
88  bool tmpResult = false;
89  if (Qt::EditRole == role)
90  {
91  tmpResult = (d_ptr->VariantValue != data);
92  setValue(data);
93  }
94 
95  return tmpResult;
96 }
97 
98 bool VPE::VProperty::paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &,
99  const QAbstractItemDelegate *) const
100 {
101  return false;
102 }
103 
104 //! Returns an editor widget, or NULL if it doesn't supply one
105 QWidget *VPE::VProperty::createEditor(QWidget *parent, const QStyleOptionViewItem &,
106  const QAbstractItemDelegate *)
107 {
108  QItemEditorFactory *factory = new QItemEditorFactory;
109  QItemEditorCreatorBase *lineCreator = new QStandardItemEditorCreator<QLineEdit>();
110  factory->registerEditor(QVariant::String, lineCreator);
111  QItemEditorFactory::setDefaultFactory(factory);
112 
113  d_ptr->editor = factory->createEditor(static_cast<int>(d_ptr->PropertyVariantType), parent);
114  return d_ptr->editor;
115 }
116 
117 bool VPE::VProperty::setEditorData(QWidget *editor)
118 {
119  if (!editor)
120  {
121  return false;
122  }
123 
124  QByteArray n = editor->metaObject()->userProperty().name();
125 
126  if (!n.isEmpty())
127  {
128  editor->blockSignals(true);
129  editor->setProperty(n, d_ptr->VariantValue);
130  editor->blockSignals(false);
131  return true;
132  }
133 
134  return false;
135 }
136 
137 //! Gets the data from the widget
138 QVariant VPE::VProperty::getEditorData(const QWidget *editor) const
139 {
140  if (!editor)
141  {
142  return QVariant();
143  }
144 
145  QByteArray n = editor->metaObject()->userProperty().name();
146 
147  if (!n.isEmpty())
148  {
149  return editor->property(n);
150  }
151  else
152  return QVariant();
153 }
154 
155 //! Returns item flags
156 Qt::ItemFlags VPE::VProperty::flags(int column) const
157 {
158  if (column == DPC_Name)
159  {
160  return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
161  }
162  else if (column == DPC_Data)
163  {
164  return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable;
165  }
166  else
167  return Qt::NoItemFlags;
168 }
169 
170 
171 void VPE::VProperty::setValue(const QVariant &value)
172 {
173  d_ptr->VariantValue = value;
174  d_ptr->VariantValue.convert(static_cast<int>(d_ptr->PropertyVariantType));
175  if (d_ptr->editor != nullptr)
176  {
177  setEditorData(d_ptr->editor);
178  }
179 }
180 
181 QVariant VPE::VProperty::getValue() const
182 {
183  return d_ptr->VariantValue;
184 }
185 
186 // cppcheck-suppress unusedFunction
188 {
189  return getValue().toString();
190 }
191 
192 void VPE::VProperty::deserialize(const QString &value)
193 {
194  setValue(QVariant(value));
195 }
196 
197 
198 void VPE::VProperty::setName(const QString &name)
199 {
200  d_ptr->Name = name;
201 }
202 
203 
204 QString VPE::VProperty::getName() const
205 {
206  return d_ptr->Name;
207 }
208 
209 
210 void VPE::VProperty::setDescription(const QString &desc)
211 {
212  d_ptr->Description = desc;
213 }
214 
215 
217 {
218  return d_ptr->Description;
219 }
220 
221 //! Returns a reference to the list of children
223 {
224  return d_ptr->Children;
225 }
226 
227 //! Returns a reference to the list of children
229 {
230  return d_ptr->Children;
231 }
232 
233 //! Returns the child at a certain row
235 {
236  if (row >= 0 && row < getRowCount())
237  {
238  return d_ptr->Children.at(row);
239  }
240  else
241  return nullptr;
242 }
243 
244 //! Gets the number of children
246 {
247  return d_ptr->Children.count();
248 }
249 
250 //! Gets the parent of this property
252 {
253  return d_ptr->Parent;
254 }
255 
256 //! Sets the parent of this property
258 {
259  if (d_ptr->Parent == parent)
260  {
261  return;
262  }
263 
264  VProperty *oldParent = d_ptr->Parent;
265  d_ptr->Parent = parent;
266 
267  if (oldParent)
268  {
269  oldParent->removeChild(this);
270  }
271 
272  if (d_ptr->Parent && d_ptr->Parent->getChildRow(this) == -1)
273  {
274  d_ptr->Parent->addChild(this);
275  }
276 }
277 
279 {
280  if (child && child->getParent() != this)
281  {
282  child->setParent(this);
283  }
284 
285  if (!d_ptr->Children.contains(child) && child != nullptr)
286  {
287  d_ptr->Children.push_back(child);
288  return d_ptr->Children.count()-1;
289  }
290  else
291  {
292  return d_ptr->Children.indexOf(child);
293  }
294 }
295 
296 //! Removes a child from the children list
298 {
299  d_ptr->Children.removeAll(child);
300 
301  if (child && child->getParent() == this)
302  {
303  child->setParent(nullptr);
304  }
305 }
306 
307 //! Returns the row the child has
309 {
310  return d_ptr->Children.indexOf(child);
311 }
312 
313 //! Returns whether the views have to update the parent of this property if it changes
315 {
316  return d_ptr->updateParent;
317 }
318 
319 //! Returns whether the views have to update the children of this property if it changes
321 {
322  return d_ptr->UpdateChildren;
323 }
324 
325 //! Sets whether the views should update Parents or children after this property changes
326 void VPE::VProperty::setUpdateBehaviour(bool update_parent, bool update_children)
327 {
328  d_ptr->updateParent = update_parent;
329  d_ptr->UpdateChildren = update_children;
330 }
331 
332 
334 {
335  QMap<QString, QVariant>::const_iterator tmpIterator = settings.constBegin();
336  for (; tmpIterator != settings.constEnd(); ++tmpIterator)
337  {
338  setSetting(tmpIterator.key(), tmpIterator.value());
339  }
340 }
341 
343 {
344  QMap<QString, QVariant> tmpResult;
345 
346  QStringList tmpKeyList = getSettingKeys();
347  foreach(const QString &tmpKey, tmpKeyList)
348  tmpResult.insert(tmpKey, getSetting(tmpKey));
349 
350  return tmpResult;
351 }
352 
353 void VPE::VProperty::setSetting(const QString &key, const QVariant &value)
354 {
355  Q_UNUSED(key)
356  Q_UNUSED(value)
357  // Not needed in the Standard property
358 }
359 
360 QVariant VPE::VProperty::getSetting(const QString &key) const
361 {
362  // Not needed in the Standard property
363  Q_UNUSED(key)
364  return QVariant();
365 }
366 
368 {
369  return QStringList();
370 }
371 
372 VPE::VProperty *VPE::VProperty::clone(bool include_children, VProperty *container) const
373 {
374  if (!container)
375  {
376  container = new VProperty(getName(), d_ptr->PropertyVariantType);
377  }
378 
379  container->setName(getName());
380  container->setDescription(getDescription());
381  container->setValue(getValue());
382  container->setSettings(getSettings());
383  container->setUpdateBehaviour(getUpdateParent(), getUpdateChildren());
384  container->setPropertyType(propertyType());
385 
386  if (include_children)
387  {
388  foreach(VProperty *tmpChild, d_ptr->Children)
389  container->addChild(tmpChild->clone(true));
390  }
391 
392  return container;
393 }
394 
396 {
397  return d_ptr->type;
398 }
399 
401 {
402  d_ptr->type = type;
403 }
404 
405 void VPE::VProperty::updateParent(const QVariant &value)
406 {
407  Q_UNUSED(value)
408 }
409 
410 void VPE::VProperty::childValueChanged(const QVariant &value, int typeForParent)
411 {
412  Q_UNUSED(value)
413  Q_UNUSED(typeForParent)
414 }
415 
417 {}
418 
420 {}
virtual ~UserChangeEvent() Q_DECL_OVERRIDE
Definition: vproperty.cpp:416
virtual ~VPropertyPrivate()
Definition: vproperty.cpp:419
virtual Q_REQUIRED_RESULT VProperty * clone(bool include_children=true, VProperty *container=nullptr) const
Clones this property.
Definition: vproperty.cpp:372
virtual void setUpdateBehaviour(bool update_parent, bool update_children)
Sets whether the views should update Parents or children after this property changes.
Definition: vproperty.cpp:326
virtual Qt::ItemFlags flags(int column=DPC_Name) const
Returns item flags.
Definition: vproperty.cpp:156
virtual bool setData(const QVariant &data, int role=Qt::EditRole)
This is used by the model to set the data.
Definition: vproperty.cpp:86
virtual QVariant data(int column=DPC_Name, int role=Qt::DisplayRole) const
Get the data how it should be displayed.
Definition: vproperty.cpp:68
virtual bool getUpdateParent() const
Returns whether the views have to update the parent of this property if it changes.
Definition: vproperty.cpp:314
virtual void setSetting(const QString &key, const QVariant &value)
Sets the settings. This function has to be implemented in a subclass in order to have an effect.
Definition: vproperty.cpp:353
virtual QVariant getValue() const
Returns the value of the property as a QVariant.
Definition: vproperty.cpp:181
virtual int getRowCount() const
Gets the number of children.
Definition: vproperty.cpp:245
virtual QStringList getSettingKeys() const
Returns the list of keys of the property's settings.
Definition: vproperty.cpp:367
virtual void removeChild(VProperty *child)
Removes a child from the children list, doesn't delete the child!
Definition: vproperty.cpp:297
virtual QString getName() const
Gets the name of the property.
Definition: vproperty.cpp:204
virtual void setDescription(const QString &desc)
Sets the name of the property.
Definition: vproperty.cpp:210
virtual bool setEditorData(QWidget *editor)
Sets the property's data to the editor (returns false, if the standard delegate should do that)
Definition: vproperty.cpp:117
virtual void setValue(const QVariant &value)
Sets the value of the property.
Definition: vproperty.cpp:171
virtual QString getDescription() const
Gets the name of the property.
Definition: vproperty.cpp:216
void setPropertyType(const Property &type)
Definition: vproperty.cpp:400
virtual QString serialize() const
Serializes the value to a string.
Definition: vproperty.cpp:187
virtual VProperty * getParent() const
Gets the parent of this property.
Definition: vproperty.cpp:251
virtual QVariant getEditorData(const QWidget *editor) const
Gets the data from the widget.
Definition: vproperty.cpp:138
virtual int addChild(VProperty *child)
Adds a child to this property.
Definition: vproperty.cpp:278
virtual void childValueChanged(const QVariant &value, int typeForParent)
Definition: vproperty.cpp:410
virtual int getChildRow(VProperty *child) const
Returns the row the child has.
Definition: vproperty.cpp:308
virtual void updateParent(const QVariant &value)
Definition: vproperty.cpp:405
virtual bool paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &, const QAbstractItemDelegate *) const
This is called by the delegate when the property value is being drawn. The standard implementation do...
Definition: vproperty.cpp:98
virtual QMap< QString, QVariant > getSettings() const
Get the settings.
Definition: vproperty.cpp:342
virtual QVariant getSetting(const QString &key) const
Get the settings. This function has to be implemented in a subclass in order to have an effect.
Definition: vproperty.cpp:360
Property propertyType() const
Definition: vproperty.cpp:395
virtual void setSettings(const QMap< QString, QVariant > &settings)
Sets the settings by calling the overloaded setSetting(const QString &key, const QVariant &value) for...
Definition: vproperty.cpp:333
virtual VProperty * getChild(int row) const
Returns the child at a certain row.
Definition: vproperty.cpp:234
virtual bool getUpdateChildren() const
Returns whether the views have to update the children of this property if it changes.
Definition: vproperty.cpp:320
VProperty(const QString &name, QVariant::Type type=QVariant::String)
Standard constructor, takes a name and a parent property as argument.
Definition: vproperty.cpp:36
virtual ~VProperty() Q_DECL_OVERRIDE
Destructor.
Definition: vproperty.cpp:49
virtual void setParent(VProperty *parent)
Sets the parent of this property.
Definition: vproperty.cpp:257
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &, const QAbstractItemDelegate *)
Returns an editor widget, or NULL if it doesn't supply one.
Definition: vproperty.cpp:105
virtual void setName(const QString &name)
Sets the name of the property.
Definition: vproperty.cpp:198
virtual QList< VProperty * > & getChildren()
Returns a reference to the list of children.
Definition: vproperty.cpp:222
virtual QString type() const
Returns a string containing the type of the property.
Definition: vproperty.cpp:62
virtual void deserialize(const QString &value)
Deserializes the value from a string.
Definition: vproperty.cpp:192
Property
Definition: vproperty.h:43