Seamly2D
Code documentation
vproperty.h
Go to the documentation of this file.
1 /************************************************************************
2  **
3  ** @file vproperty.h
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 #ifndef VPROPERTY_H
22 #define VPROPERTY_H
23 
24 #include <qcompilerdetection.h>
25 #include <QAbstractItemDelegate>
26 #include <QEvent>
27 #include <QMap>
28 #include <QMetaObject>
29 #include <QObject>
30 #include <QSharedPointer>
31 #include <QString>
32 #include <QStringList>
33 #include <QStyleOptionViewItem>
34 #include <QVariant>
35 #include <Qt>
36 #include <QtGlobal>
37 
38 template <typename T> class QList;
39 
40 namespace VPE
41 {
42 
43 enum class Property : char{Simple, Complex, Label};
44 
45 static const int MyCustomEventType = 1099;
46 
47 class UserChangeEvent : public QEvent
48 {
49 public:
50  UserChangeEvent() : QEvent(static_cast<QEvent::Type>(MyCustomEventType)) {}
51  virtual ~UserChangeEvent() Q_DECL_OVERRIDE;
52 };
53 
54 class VPropertyPrivate;
55 
56 class VProperty : public QObject
57 {
58  Q_OBJECT
59 public:
61  {
62  DPC_Name = 0,
63  DPC_Data
64  };
65 
66  //! Standard constructor, takes a name and a parent property as argument
67  explicit VProperty(const QString &name, QVariant::Type type = QVariant::String);
68 
69  //! Destructor
70  virtual ~VProperty() Q_DECL_OVERRIDE;
71 
72  //! Returns a string containing the type of the property
73  virtual QString type() const;
74 
75  //! Get the data how it should be displayed
76  virtual QVariant data (int column = DPC_Name, int role = Qt::DisplayRole) const;
77 
78  //! This is used by the model to set the data
79  //! \param data The data to set
80  //! \param role The role. Default is Qt::EditRole
81  //! \return Returns true, if the data was changed, false if not.
82  virtual bool setData (const QVariant &data, int role = Qt::EditRole);
83 
84  //! This is called by the delegate when the property value is being drawn.
85  //! The standard implementation doesn't do anything.
86  //! If you reimplement this in a sub property, make sure to return true or the delegate will draw the item.
87  virtual bool paint(QPainter*, const QStyleOptionViewItem&, const QModelIndex&, const QAbstractItemDelegate*) const;
88 
89  //! Returns an editor widget, or NULL if it doesn't supply one
90  //! \param parent The widget to which the editor will be added as a child
91  //! \options Render options
92  //! \delegate A pointer to the QAbstractItemDelegate requesting the editor. This can be used to connect signals and
93  //! slots.
94  virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem&, const QAbstractItemDelegate*);
95 
96  //! Sets the property's data to the editor (returns false, if the standard delegate should do that)
97  virtual bool setEditorData(QWidget *editor);
98 
99  //! Gets the data from the widget
100  virtual QVariant getEditorData(const QWidget *editor) const;
101 
102  //! Returns item flags
103  virtual Qt::ItemFlags flags(int column = DPC_Name) const;
104 
105  //! Sets the value of the property
106  virtual void setValue(const QVariant &value);
107 
108  //! Returns the value of the property as a QVariant
109  virtual QVariant getValue() const;
110 
111  //! Serializes the value to a string
112  virtual QString serialize() const;
113 
114  //! Deserializes the value from a string
115  virtual void deserialize(const QString &value);
116 
117  // The following functions are experimental and not yet implemented.
118  /*//! Returns a pointer to the data stored and handled by this property. In most cases this function shouldn't be
119  //! used.
120  //! \return Returns a void pointer to the data. Not all properties have to support this. By default, this
121  //! implementation returns a NULL pointer.
122  virtual void *getDataPointer();
123 
124  //! Sets the data.
125  //! \return Returns a void pointer to the data. Not all properties have to support this. By default, this
126  //! implementation returns a NULL pointer.
127  virtual bool setDataPointer(void *pointer);*/
128 
129  //! Sets the name of the property
130  virtual void setName(const QString &name);
131 
132  //! Gets the name of the property
133  virtual QString getName() const;
134 
135  //! Sets the name of the property
136  virtual void setDescription(const QString &desc);
137 
138  //! Gets the name of the property
139  virtual QString getDescription() const;
140 
141  //! Adds a child to this property
142  virtual int addChild(VProperty *child);
143 
144  //! Returns a reference to the list of children
145  virtual QList<VProperty*> &getChildren();
146 
147  //! Returns a reference to the list of children
148  virtual const QList<VProperty*> &getChildren() const;
149 
150  //! Returns the child at a certain row
151  virtual VProperty *getChild(int row) const;
152 
153  //! Gets the number of children
154  virtual int getRowCount() const;
155 
156  //! Gets the parent of this property
157  virtual VProperty *getParent() const;
158 
159  //! Sets the parent of this property
160  virtual void setParent(VProperty *parent);
161 
162  //! Removes a child from the children list, doesn't delete the child!
163  virtual void removeChild(VProperty *child);
164 
165  //! Returns the row the child has
166  virtual int getChildRow(VProperty *child) const;
167 
168  //! Returns whether the views have to update the parent of this property if it changes
169  virtual bool getUpdateParent() const;
170 
171  //! Returns whether the views have to update the children of this property if it changes
172  virtual bool getUpdateChildren() const;
173 
174  //! Sets whether the views should update Parents or children after this property changes
175  virtual void setUpdateBehaviour(bool update_parent, bool update_children);
176 
177  //! Sets the settings by calling the overloaded setSetting(const QString &key, const QVariant &value) for each item
178  //! in the map.
179  virtual void setSettings(const QMap<QString, QVariant> &settings);
180 
181  //! Get the settings.
182  virtual QMap<QString, QVariant> getSettings() const;
183 
184  //! Sets the settings. This function has to be implemented in a subclass in order to have an effect
185  virtual void setSetting(const QString &key, const QVariant &value);
186 
187  //! Get the settings. This function has to be implemented in a subclass in order to have an effect
188  virtual QVariant getSetting(const QString &key) const;
189 
190  //! Returns the list of keys of the property's settings
191  virtual QStringList getSettingKeys() const;
192 
193  //! Clones this property
194  //! \param include_children Indicates whether to also clone the children
195  //! \param container If a property is being passed here, no new VProperty is being created but instead it is tried
196  //! to fill all the data into container. This can also be used when subclassing this function.
197  //! \return Returns the newly created property (or container, if it was not NULL)
198  Q_REQUIRED_RESULT virtual VProperty *clone(bool include_children = true, VProperty *container = nullptr) const;
199 
200  Property propertyType() const;
201  void setPropertyType(const Property &type);
202 
203  virtual void updateParent(const QVariant &value);
204 public slots:
205  virtual void childValueChanged(const QVariant &value, int typeForParent);
206 signals:
207  void childChanged(const QVariant &value, int typeForParent);
208 
209 protected:
210  //! Protected constructor
211  explicit VProperty(VPropertyPrivate *d);
212 
213  //! The protected structure holding the member variables (to assure binary compatibility)
215 
216 private:
217  // Provide access functions for the d_ptr
218  Q_DECLARE_PRIVATE(VProperty)
219  Q_DISABLE_COPY(VProperty)
220 };
221 
222 }
223 
224 #endif // VPROPERTY_H
virtual ~UserChangeEvent() Q_DECL_OVERRIDE
Definition: vproperty.cpp:416
static const int MyCustomEventType
Definition: vproperty.h:45
Property
Definition: vproperty.h:43