Seamly2D
Code documentation
vpropertymodel.h
Go to the documentation of this file.
1 /************************************************************************
2  **
3  ** @file vpropertymodel.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 VPROPERTYMODEL_H
22 #define VPROPERTYMODEL_H
23 
24 #include <qcompilerdetection.h>
25 #include <QAbstractItemModel>
26 #include <QMap>
27 #include <QMetaObject>
28 #include <QModelIndex>
29 #include <QObject>
30 #include <QString>
31 #include <QVariant>
32 #include <Qt>
33 #include <QtGlobal>
34 
35 #include "vproperty.h"
36 
37 namespace VPE
38 {
39 
40 class VPropertyModelPrivate;
41 class VPropertySet;
42 
43 //! \brief This is the base model for managing all the properties
44 //! and passing them to the view.
45 //!
46 //! When you create your own "proxy models", this is the place to
47 //! start: just subclass VPropertyModel and extend the new class.
48 //! Have a look at existing examples of proxies.
49 //!
50 //! <strong>Note that in this context, the term "proxy model" does not refer
51 //! to VProxyModel as that is another concept.</strong>
52 //! The idea behind "proxy models" in the QtPropertyExplorer framework
53 //! is to provide an convenient interface which takes data as your
54 //! application (or a third-party-library) provides it, and converts this
55 //! data to VProperty-objects, manage them and produce output for the views.
56 //!
57 //! In most cases, you will not need to rewrite the basic functions of
58 //! QAbstractItemModel, as VPropertyModel provides standard implementations
59 //! to work with. Thus, instead of subclassing VPropertyModel, it is also
60 //! possible to use VPropertyModel directly (as it is not an abstract class).
61 //! This might be more convenient in some cases.
62 class VPropertyModel : public QAbstractItemModel
63 {
64  Q_OBJECT
65 public:
66  explicit VPropertyModel(QObject * parent = nullptr);
67  virtual ~VPropertyModel() Q_DECL_OVERRIDE;
68 
69  //! Adds the property to the model and attaches it to the parentid
70  //! \param emitsignals If this is set to false, this function will not call beginInsertRows() and endInsertRows(),
71  //! so it has to be called from a subclass
72  virtual bool addProperty(VProperty* property, const QString& id, const QString& parentid = QString(),
73  bool emitsignals = true);
74 
75  //! Creates a property and adds it to the model
76  virtual VProperty* createProperty(const QString& id, const QString& name, const QString& parentid = QString(),
77  const QVariant& data = QVariant());
78 
79  //! Gets a property by it's ID
80  virtual VProperty* getProperty(const QString& id);
81 
82  //! Returns the item flags for the given index
83  virtual Qt::ItemFlags flags (const QModelIndex& index) const Q_DECL_OVERRIDE;
84 
85  //! Sets the role data for the item at index to value
86  virtual bool setData (const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) Q_DECL_OVERRIDE;
87 
88  //! Returns the model index at row/column
89  virtual QModelIndex index (int row, int column, const QModelIndex & parent = QModelIndex() ) const Q_DECL_OVERRIDE;
90 
91  //! Returns the parent of one model index
92  virtual QModelIndex parent (const QModelIndex& index) const Q_DECL_OVERRIDE;
93 
94  //! Returns the data of an model index
95  virtual QVariant data (const QModelIndex& index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
96 
97  //! Returns the data for the given role and section in the header with the specified orientation.
98  virtual QVariant headerData (int section, Qt::Orientation orientation,
99  int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
100 
101  //! Returns the number of rows
102  virtual int rowCount ( const QModelIndex & parent = QModelIndex() ) const Q_DECL_OVERRIDE;
103 
104  //! Returns the number of columns
105  virtual int columnCount ( const QModelIndex & parent = QModelIndex() ) const Q_DECL_OVERRIDE;
106 
107  //! Gets a property by its ModelIndex
108  //! \param index The modelIndex of the property.
109  //! \return Returns the property with the given index, or NULL if none such property exists
110  virtual VProperty* getProperty(const QModelIndex &index) const;
111 
112  //! Returns the ID of the property within the model
113  //! The concept of property IDs is, that the object that manages the properties
114  //! and not the properties themselves handle the IDs.
115  //! \return Returns the ID under which the property is stored within the model
116  virtual QString getPropertyID(const VProperty* prop) const;
117 
118  //! Returns a const pointer to the property set managed by this model. If you want to manipulate the property set,
119  //! either use the methods provided by the model or use takePropertySet() and setPropertySet().
120  //! \return A constant pointer to the property set or NULL if there currently is none.
121  virtual const VPropertySet* getPropertySet() const;
122 
123  //! Clears the model, deletes the property set managed by this model.
124  //! \param emit_signals Default: true. Set this to false if you want to prevent the model from emmiting the reset
125  //! model signals
126  virtual void clear(bool emit_signals = true);
127 
128  //! Removes the current property set and returns it. If new_property_set is set, the old one will be replaced by the
129  //! new one
130  //! \param new_property_set The new property set to replace the old one with. Default: NULL
131  //! \param emit_signals Default: true. Set this to false if you want to prevent the model from emmiting the reset
132  //! model signals
133  //! \return A constant pointer to the property set or NULL if there currently is none.
134  virtual VPropertySet* takePropertySet(VPropertySet* new_property_set = nullptr, bool emit_signals = true);
135 
136  //! Sets a new property set. The model will take ownership of the property set. The old property set will be
137  //! deleted.
138  //! \param property_set The new property set. Setting this to NULL has the same effect as calling clear.
139  //! \param emit_signals Default: true. Set this to false if you want to prevent the model from emmiting the reset
140  //! model signals
141  virtual void setPropertySet(VPropertySet* property_set, bool emit_signals = true);
142 
143  //! Removes a property from the model and returns it
144  virtual VProperty* takeProperty(const QString& id);
145 
146  //! Removes a property from the model and deletes it
147  virtual void removeProperty(const QString& id);
148 
149 signals:
150  //! This signal is being emitted, when the setData method is being called
152 
153 public slots:
154  //! This function causes the views to update the property
155  void onDataChangedByModel(VProperty* property);
156 
157 protected:
158  //! Gets a property by its ModelIndex
159  virtual QModelIndex getIndexFromProperty(VProperty* property, int column = 0) const;
160 
161  //! Protected constructor passing the private object
162  explicit VPropertyModel(VPropertyModelPrivate* d, QObject* parent = nullptr);
163 
164  //! The model data
166 
167 private:
168  Q_DISABLE_COPY(VPropertyModel)
169 };
170 
171 }
172 
173 #endif // VPROPERTYMODEL_H
This is the base model for managing all the properties and passing them to the view.
virtual QModelIndex getIndexFromProperty(VProperty *property, int column=0) const
Gets a property by its ModelIndex.
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const Q_DECL_OVERRIDE
Returns the number of columns.
VPropertyModelPrivate * d_ptr
The model data.
virtual Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE
Returns the item flags for the given index.
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const Q_DECL_OVERRIDE
Returns the data of an model index.
void onDataChangedByModel(VProperty *property)
This function causes the views to update the property.
virtual ~VPropertyModel() Q_DECL_OVERRIDE
VPropertyModel(QObject *parent=nullptr)
virtual VProperty * createProperty(const QString &id, const QString &name, const QString &parentid=QString(), const QVariant &data=QVariant())
Creates a property and adds it to the model.
virtual QString getPropertyID(const VProperty *prop) const
Returns the ID of the property within the model The concept of property IDs is, that the object that ...
virtual void removeProperty(const QString &id)
Removes a property from the model and deletes it.
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const Q_DECL_OVERRIDE
Returns the data for the given role and section in the header with the specified orientation.
virtual VPropertySet * takePropertySet(VPropertySet *new_property_set=nullptr, bool emit_signals=true)
Removes the current property set and returns it. If new_property_set is set, the old one will be repl...
virtual void setPropertySet(VPropertySet *property_set, bool emit_signals=true)
Sets a new property set. The model will take ownership of the property set. The old property set will...
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const Q_DECL_OVERRIDE
Returns the number of rows.
virtual bool addProperty(VProperty *property, const QString &id, const QString &parentid=QString(), bool emitsignals=true)
Adds the property to the model and attaches it to the parentid.
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const Q_DECL_OVERRIDE
Returns the model index at row/column.
virtual VProperty * takeProperty(const QString &id)
Removes a property from the model and returns it.
virtual VProperty * getProperty(const QString &id)
Gets a property by it's ID.
virtual void clear(bool emit_signals=true)
Clears the model, deletes the property set managed by this model.
void onDataChangedByEditor(VProperty *property)
This signal is being emitted, when the setData method is being called.
virtual QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE
Returns the parent of one model index.
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) Q_DECL_OVERRIDE
Sets the role data for the item at index to value.
virtual const VPropertySet * getPropertySet() const
Returns a const pointer to the property set managed by this model. If you want to manipulate the prop...
VPropertySet is a simple class for managing a set of properties. If you don't need all the Model-func...
Definition: vpropertyset.h:48