1 /***************************************************************************
2     qgsattributeeditorcontext.h
3      --------------------------------------
4     Date                 : 30.7.2013
5     Copyright            : (C) 2013 Matthias Kuhn
6     Email                : matthias at opengis dot 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 QGSATTRIBUTEEDITORCONTEXT_H
17 #define QGSATTRIBUTEEDITORCONTEXT_H
18 
19 #include <QMap>
20 #include <QWidget>
21 #include <QMetaEnum>
22 
23 #include "qgsdistancearea.h"
24 #include "qgsvectorlayertools.h"
25 #include "qgsvectorlayer.h"
26 #include "qgis_gui.h"
27 #include "qgsproject.h"
28 
29 class QgsMapCanvas;
30 class QgsAdvancedDigitizingDockWidget;
31 class QgsMessageBar;
32 
33 /**
34  * \ingroup gui
35  * \brief This class contains context information for attribute editor widgets.
36  * It will be passed to embedded widgets whenever this occurs (e.g. when
37  * showing an embedded form due to relations)
38  */
39 
40 class GUI_EXPORT QgsAttributeEditorContext
41 {
42     Q_GADGET
43 
44   public:
45 
46     //! modes
47     enum Mode
48     {
49       SingleEditMode, //!< Single edit mode, for editing a single feature
50       AddFeatureMode, /*!< Add feature mode, for setting attributes for a new feature. In this mode the dialog will be editable even with an invalid feature and
51       will add a new feature when the form is accepted. */
52       FixAttributeMode, //!< Fix feature mode, for modifying the feature attributes without saving. The updated feature is available via `feature()` after `save()`
53       MultiEditMode, //!< Multi edit mode, for editing fields of multiple features at once
54       SearchMode, //!< Form values are used for searching/filtering the layer
55       AggregateSearchMode, //!< Form is in aggregate search mode, show each widget in this mode \since QGIS 3.0
56       IdentifyMode //!< Identify the feature \since QGIS 3.0
57     };
58     Q_ENUM( Mode )
59 
60     /**
61      * Determines in which direction a relation was resolved.
62      */
63     enum RelationMode
64     {
65       Undefined,  //!< This context is not defined by a relation
66       Multiple,   //!< When showing a list of features (e.g. houses as an embedded form in a district form)
67       Single      //!< When showing a single feature (e.g. district information when looking at the form of a house)
68     };
69 
70     enum FormMode
71     {
72       Embed,            //!< A form was embedded as a widget on another form
73       StandaloneDialog, //!< A form was opened as a new dialog
74       Popup             //!< A widget was opened as a popup (e.g. attribute table editor widget)
75     };
76 
77     //! Constructor for QgsAttributeEditorContext
78     QgsAttributeEditorContext() = default;
79 
QgsAttributeEditorContext(const QgsAttributeEditorContext & parentContext,FormMode formMode)80     QgsAttributeEditorContext( const QgsAttributeEditorContext &parentContext, FormMode formMode )
81       : mParentContext( &parentContext )
82       , mVectorLayerTools( parentContext.mVectorLayerTools )
83       , mMapCanvas( parentContext.mMapCanvas )
84       , mMainMessageBar( parentContext.mMainMessageBar )
85       , mCadDockWidget( parentContext.mCadDockWidget )
86       , mDistanceArea( parentContext.mDistanceArea )
87       , mFormFeature( parentContext.mFormFeature )
88       , mFormMode( formMode )
89     {
90       Q_ASSERT( parentContext.vectorLayerTools() );
91     }
92 
QgsAttributeEditorContext(const QgsAttributeEditorContext & parentContext,const QgsRelation & relation,RelationMode relationMode,FormMode widgetMode)93     QgsAttributeEditorContext( const QgsAttributeEditorContext &parentContext, const QgsRelation &relation, RelationMode relationMode, FormMode widgetMode )
94       : mParentContext( &parentContext )
95       , mVectorLayerTools( parentContext.mVectorLayerTools )
96       , mMapCanvas( parentContext.mMapCanvas )
97       , mMainMessageBar( parentContext.mMainMessageBar )
98       , mCadDockWidget( parentContext.mCadDockWidget )
99       , mDistanceArea( parentContext.mDistanceArea )
100       , mRelation( relation )
101       , mRelationMode( relationMode )
102       , mFormMode( widgetMode )
103     {
104       Q_ASSERT( parentContext.vectorLayerTools() );
105     }
106 
107     /**
108      * Sets distance area object, \a distanceArea, for area/length calculations
109      * \see distanceArea()
110      * \since QGIS 2.2
111      */
setDistanceArea(const QgsDistanceArea & distanceArea)112     inline void setDistanceArea( const QgsDistanceArea &distanceArea )
113     {
114       if ( mLayer )
115       {
116         mDistanceArea = distanceArea;
117         mDistanceArea.setSourceCrs( mLayer->crs(), QgsProject::instance()->transformContext() );
118       }
119     }
120 
121     /**
122      * Returns the distance area object used for area/length calculations.
123      * \see setDistanceArea()
124      * \since QGIS 2.2
125      */
distanceArea()126     inline const QgsDistanceArea &distanceArea() const { return mDistanceArea; }
127 
128     /**
129      * Sets the associated map canvas, \a mapCanvas, (e.g. to zoom to related features).
130      * \see mapCanvas()
131      * \since QGIS 3.2
132      */
setMapCanvas(QgsMapCanvas * mapCanvas)133     inline void setMapCanvas( QgsMapCanvas *mapCanvas ) { mMapCanvas = mapCanvas; }
134 
135     /**
136      * Returns the associated map canvas (e.g. to zoom to related features).
137      * \see setMapCanvas()
138      * \since QGIS 3.2
139      */
mapCanvas()140     inline QgsMapCanvas *mapCanvas() const { return mMapCanvas; }
141 
142     /**
143      * Sets the associated CAD dock widget, \a cadDockWidget, (e.g. to be used in map tools).
144      * \note Unstable API. This method is unstable API and may be modified or removed at any time.
145      * \see cadDockWidget()
146      * \since QGIS 3.10
147      */
148     void setCadDockWidget( QgsAdvancedDigitizingDockWidget *cadDockWidget );
149 
150     /**
151      * Returns the associated CAD dock widget (e.g. to be used in map tools).
152      * \note Unstable API. This method is unstable API and may be modified or removed at any time.
153      * \see setCadDockWidget()
154      * \since QGIS 3.10
155      */
cadDockWidget()156     QgsAdvancedDigitizingDockWidget *cadDockWidget() const { return mCadDockWidget; }
157 
158     /**
159      * Sets the associated vector layer tools.
160      * \param vlTools vector layer tools
161      * \see vectorLayerTools()
162      * \since QGIS 2.2
163      */
setVectorLayerTools(QgsVectorLayerTools * vlTools)164     inline void setVectorLayerTools( QgsVectorLayerTools *vlTools ) { mVectorLayerTools = vlTools; }
165     // TODO QGIS 4.0 - rename vlTools to tools
166 
167     /**
168      * Returns the associated vector layer tools.
169      * \see setVectorLayerTools()
170      * \since QGIS 2.2
171      */
vectorLayerTools()172     inline const QgsVectorLayerTools *vectorLayerTools() const { return mVectorLayerTools; }
173 
174     /**
175      * Set attribute relation and mode
176      * \param relation relation
177      * \param mode relation mode
178      * \see relation()
179      * \see relationMode()
180      * \since QGIS 2.6
181      */
setRelation(const QgsRelation & relation,RelationMode mode)182     inline void setRelation( const QgsRelation &relation, RelationMode mode ) { mRelation = relation; mRelationMode = mode; }
183 
184     /**
185      * Returns the attribute relation.
186      * \see setRelation()
187      * \see relationMode()
188      * \since QGIS 2.6
189      */
relation()190     inline const QgsRelation &relation() const { return mRelation; }
191 
192     /**
193      * Returns the attribute relation mode.
194      * \see setRelation()
195      * \see relation()
196      * \since QGIS 2.6
197      */
relationMode()198     inline RelationMode relationMode() const { return mRelationMode; }
199 
200     /**
201      * Returns the form mode.
202      * \see setFormMode()
203      */
formMode()204     inline FormMode formMode() const { return mFormMode; }
205 
206     /**
207      * Sets the form mode.
208      * \param mode form mode
209      * \see formMode()
210      * \since QGIS 2.16
211      */
setFormMode(FormMode mode)212     inline void setFormMode( FormMode mode ) { mFormMode = mode; }
213 
214     /**
215      * Returns TRUE if the attribute editor should permit use of custom UI forms.
216      * \see setAllowCustomUi()
217      * \since QGIS 2.16
218      */
allowCustomUi()219     bool allowCustomUi() const { return mAllowCustomUi; }
220 
221     /**
222      * Sets whether the attribute editor should permit use of custom UI forms.
223      * \param allow set to TRUE to allow custom UI forms, or FALSE to disable them and use default generated
224      * QGIS forms
225      * \see allowCustomUi()
226      * \since QGIS 2.16
227      */
setAllowCustomUi(bool allow)228     void setAllowCustomUi( bool allow ) { mAllowCustomUi = allow; }
229 
parentContext()230     inline const QgsAttributeEditorContext *parentContext() const { return mParentContext; }
231 
232     /**
233      * Returns current feature from the currently edited form or table row
234      * \see setFormFeature()
235      * \since QGIS 3.2
236      */
formFeature()237     QgsFeature formFeature() const { return mFormFeature; }
238 
239     /**
240      * Set current \a feature for the currently edited form or table row
241      * \see formFeature()
242      * \since QGIS 3.2
243      */
setFormFeature(const QgsFeature & feature)244     void setFormFeature( const QgsFeature &feature ) { mFormFeature = feature ; }
245 
246     /**
247      * Returns the feature of the currently edited parent form in its actual state
248      * \see setParentFormFeature()
249      * \since QGIS 3.14
250      */
parentFormFeature()251     QgsFeature parentFormFeature() const { return mParentFormFeature; }
252 
253     /**
254      * Sets the \a feature of the currently edited parent form
255      * \see parentFormFeature()
256      * \since QGIS 3.14
257      */
setParentFormFeature(const QgsFeature & feature)258     void setParentFormFeature( const QgsFeature &feature ) { mParentFormFeature = feature ; }
259 
260     /**
261      * Returns current attributeFormMode
262      * \since QGIS 3.4
263      */
attributeFormMode()264     Mode attributeFormMode() const { return mAttributeFormMode; }
265 
266     /**
267      * Set \a attributeFormMode for the edited form
268      * \since QGIS 3.4
269      */
setAttributeFormMode(const Mode & attributeFormMode)270     void setAttributeFormMode( const Mode &attributeFormMode ) { mAttributeFormMode = attributeFormMode; }
271 
272     /**
273      * Returns given \a attributeFormMode as string
274      * \since QGIS 3.4
275      */
attributeFormModeString()276     QString attributeFormModeString() const
277     {
278       const QMetaEnum metaEnum( QMetaEnum::fromType<Mode>() );
279       return metaEnum.valueToKey( static_cast<int>( mAttributeFormMode ) );
280     }
281 
282     /**
283      * Set current \a messageBar as the main message bar
284      * \since QGIS 3.12
285      */
setMainMessageBar(QgsMessageBar * messageBar)286     void setMainMessageBar( QgsMessageBar *messageBar ) { mMainMessageBar = messageBar; }
287 
288     /**
289      * Returns the main message bar
290      * \since QGIS 3.12
291      */
mainMessageBar()292     QgsMessageBar *mainMessageBar() { return mMainMessageBar; }
293 
294   private:
295     const QgsAttributeEditorContext *mParentContext = nullptr;
296     QgsVectorLayer *mLayer = nullptr;
297     QgsVectorLayerTools *mVectorLayerTools = nullptr;
298     QgsMapCanvas *mMapCanvas = nullptr;
299     QgsMessageBar *mMainMessageBar = nullptr;
300     QgsAdvancedDigitizingDockWidget *mCadDockWidget = nullptr;
301     QgsDistanceArea mDistanceArea;
302     QgsRelation mRelation;
303     RelationMode mRelationMode = Undefined;
304     //! Store the values of the currently edited form or table row
305     QgsFeature mFormFeature;
306     //! Store the values of the currently edited parent form or table row
307     QgsFeature mParentFormFeature;
308     FormMode mFormMode = Embed;
309     bool mAllowCustomUi = true;
310     Mode mAttributeFormMode = SingleEditMode;
311 };
312 
313 #endif // QGSATTRIBUTEEDITORCONTEXT_H
314