1 /***************************************************************************
2  QgsQuickAttributeModel.h
3   --------------------------------------
4   Date                 : 10.12.2014
5   Copyright            : (C) 2014 by Matthias Kuhn
6   Email                : matthias@opengis.ch
7  ***************************************************************************
8  *                                                                         *
9  *   This program is free software; you can redistribute it and/or modify  *
10  *   it under the terms of the GNU General Public License as published by  *
11  *   the Free Software Foundation; either version 2 of the License, or     *
12  *   (at your option) any later version.                                   *
13  *                                                                         *
14  ***************************************************************************/
15 
16 #ifndef QGSQUICKATTRIBUTEMODEL_H
17 #define QGSQUICKATTRIBUTEMODEL_H
18 
19 #include <QAbstractListModel>
20 #include <QVector>
21 
22 #include "qgsfeature.h"
23 
24 #include "qgis_quick.h"
25 #include "qgsquickfeaturelayerpair.h"
26 
27 /**
28  * \ingroup quick
29  *
30  * \brief Basic item model for attributes of QgsFeature associated
31  * from feature layer pair. Each attribute of the feature
32  * gets a row in the model. Also supports CRUD operations
33  * related to layer and feature pair.
34  *
35  * On top of the QgsFeature attributes, support for "remember"
36  * attribute is added. Remember attribute is used for
37  * quick addition of the multiple features to the same layer.
38  * A new feature can use "remembered" attribute values from
39  * the last feature added.
40  *
41  *
42  * \note QML Type: AttributeModel
43  *
44  * \since QGIS 3.4
45  */
46 class QUICK_EXPORT QgsQuickAttributeModel : public QAbstractListModel
47 {
48     Q_OBJECT
49 
50     /**
51      * QgsQuickFeatureLayerPair for the model. Input for attributes model.
52      */
53     Q_PROPERTY( QgsQuickFeatureLayerPair featureLayerPair READ featureLayerPair WRITE setFeatureLayerPair NOTIFY featureLayerPairChanged )
54 
55     /**
56      * Feature roles enum.
57      */
58     Q_ENUMS( FeatureRoles )
59 
60   public:
61     //! Feature roles
62     enum FeatureRoles
63     {
64       AttributeName = Qt::UserRole + 1,  //!< Attribute's display name (the original field name or a custom alias)
65       AttributeValue,                    //!< Value of the feature's attribute
66       Field,                             //!< Field definition (QgsField)
67       RememberAttribute                  //!< Remember attribute value for next feature
68     };
69 
70     //! Creates a new feature attribute model
71     explicit QgsQuickAttributeModel( QObject *parent = nullptr );
72 
73     /**
74      * Creates a new feature attribute model
75      * \param feat Feature set to model
76      * \param parent Parent object.
77      */
78     explicit QgsQuickAttributeModel( const QgsFeature &feat, QObject *parent = nullptr );
79 
80 
81     QHash<int, QByteArray> roleNames() const override;
82     int rowCount( const QModelIndex &parent ) const override;
83     QVariant data( const QModelIndex &index, int role ) const override;
84     bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
85 
86     /**
87      * Commits the edit buffer of this layer.
88      * May change in the future to only commit the changes buffered in this model.
89      *
90      * @return Success of the operation.
91      */
92     Q_INVOKABLE bool save();
93 
94     /**
95      * Deletes the current feature from the layer and commit the changes.
96      * @return Success of the operation.
97      */
98     Q_INVOKABLE bool deleteFeature();
99 
100     /**
101      * Resets the feature to the original values and dismiss any buffered edits.
102      */
103     Q_INVOKABLE void reset();
104 
105     //! Adds feature from featureLayerPair to the layer
106     Q_INVOKABLE void create();
107 
108     /**
109      * Suppress layer's QgsEditFormConfig
110      *
111      * \sa QgsEditFormConfig::suppress
112      */
113     Q_INVOKABLE bool suppressFeatureForm() const;
114 
115     //! Resets remembered attributes
116     Q_INVOKABLE virtual void resetAttributes();
117 
118     //! Gets remembered attributes
119     QVector<bool> rememberedAttributes() const;
120 
121     //!\copydoc QgsQuickAttributeModel::featureLayerPair
122     QgsQuickFeatureLayerPair featureLayerPair() const;
123 
124     //!\copydoc QgsQuickAttributeModel::featureLayerPair
125     void setFeatureLayerPair( const QgsQuickFeatureLayerPair &pair );
126 
127   public slots:
128 
129   signals:
130     //! Feature or layer changed in feature layer pair
131     void featureLayerPairChanged();
132 
133     //! Feature updated, layer is the same
134     void featureChanged();
135 
136     //! Layer changed, feature is the same
137     void layerChanged();
138 
139   protected:
140     //! Commits model changes
141     bool commit();
142     //! Starts editing model
143     bool startEditing();
144 
145     QgsQuickFeatureLayerPair mFeatureLayerPair;
146     QVector<bool> mRememberedAttributes;
147   private:
148     void setFeature( const QgsFeature &feature );
149     void setVectorLayer( QgsVectorLayer *layer );
150 };
151 
152 #endif // QGSQUICKATTRIBUTEMODEL_H
153