QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsattributeformeditorwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsattributeformeditorwidget.cpp
3 -------------------------------
4 Date : March 2016
5 Copyright : (C) 2016 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
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
17#include "moc_qgsattributeformeditorwidget.cpp"
18#include "qgsattributeform.h"
25#include "qgsgui.h"
26#include "qgsvectorlayerutils.h"
27
28#include <QLayout>
29#include <QLabel>
30#include <QStackedWidget>
31
33 : QgsAttributeFormWidget( editorWidget, form )
34 , mWidgetType( widgetType )
35 , mEditorWidget( editorWidget )
36 , mForm( form )
37 , mMultiEditButton( new QgsMultiEditToolButton() )
38 , mBlockValueUpdate( false )
39 , mIsMixed( false )
40 , mIsChanged( false )
41{
42 mConstraintResultLabel = new QLabel( this );
43 mConstraintResultLabel->setObjectName( QStringLiteral( "ConstraintStatus" ) );
44 mConstraintResultLabel->setSizePolicy( QSizePolicy::Fixed, mConstraintResultLabel->sizePolicy().verticalPolicy() );
45
46 mMultiEditButton->setField( mEditorWidget->field() );
47 mAggregateButton = new QgsAggregateToolButton();
48 mAggregateButton->setType( mEditorWidget->field().type() );
49 connect( mAggregateButton, &QgsAggregateToolButton::aggregateChanged, this, &QgsAttributeFormEditorWidget::onAggregateChanged );
50
51 if ( mEditorWidget->widget() )
52 {
53 mEditorWidget->widget()->setObjectName( mEditorWidget->field().name() );
54 }
55
56 connect( mEditorWidget, &QgsEditorWidgetWrapper::valuesChanged, this, &QgsAttributeFormEditorWidget::editorWidgetValuesChanged );
57
58 connect( mMultiEditButton, &QgsMultiEditToolButton::resetFieldValueTriggered, this, &QgsAttributeFormEditorWidget::resetValue );
59 connect( mMultiEditButton, &QgsMultiEditToolButton::setFieldValueTriggered, this, &QgsAttributeFormEditorWidget::setFieldTriggered );
60
61 mMultiEditButton->setField( mEditorWidget->field() );
62
63 updateWidgets();
64}
65
67{
68 //there's a chance these widgets are not currently added to the layout, so have no parent set
69 delete mMultiEditButton;
70}
71
73{
74 Q_ASSERT( !mWidgetType.isEmpty() );
75 const QVariantMap config = mEditorWidget->config();
76 const int fieldIdx = mEditorWidget->fieldIdx();
77
78 QgsSearchWidgetWrapper *sww = QgsGui::editorWidgetRegistry()->createSearchWidget( mWidgetType, layer(), fieldIdx, config, searchWidgetFrame(), context );
80 searchWidgetFrame()->layout()->addWidget( mAggregateButton );
82 {
83 // create secondary widget for between type searches
84 QgsSearchWidgetWrapper *sww2 = QgsGui::editorWidgetRegistry()->createSearchWidget( mWidgetType, layer(), fieldIdx, config, searchWidgetFrame(), context );
86 }
87}
88
89void QgsAttributeFormEditorWidget::setConstraintStatus( const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result )
90{
91 switch ( result )
92 {
94 mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#FF9800\">%1</font>" ).arg( QChar( 0x2718 ) ) );
95 mConstraintResultLabel->setToolTip( description.isEmpty() ? QStringLiteral( "<b>%1</b>: %2" ).arg( constraint, err ) : description );
96 break;
97
99 mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#FFC107\">%1</font>" ).arg( QChar( 0x2718 ) ) );
100 mConstraintResultLabel->setToolTip( description.isEmpty() ? QStringLiteral( "<b>%1</b>: %2" ).arg( constraint, err ) : description );
101 break;
102
104 mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#259B24\">%1</font>" ).arg( QChar( 0x2714 ) ) );
105 mConstraintResultLabel->setToolTip( description );
106 break;
107 }
108}
109
111{
112 mConstraintResultLabel->setHidden( !editable );
113}
114
116{
117 return mEditorWidget;
118}
119
121{
122 if ( mEditorWidget && mixed )
123 mEditorWidget->showIndeterminateState();
124 mMultiEditButton->setIsMixed( mixed );
125 mIsMixed = mixed;
126}
127
129{
130 if ( mEditorWidget )
131 {
132 mPreviousValue = mEditorWidget->value();
133 mPreviousAdditionalValues = mEditorWidget->additionalFieldValues();
134 }
135
136 setIsMixed( false );
137 mMultiEditButton->changesCommitted();
138 mIsChanged = false;
139}
140
141
142void QgsAttributeFormEditorWidget::initialize( const QVariant &initialValue, bool mixedValues, const QVariantList &additionalFieldValues )
143{
144 if ( mEditorWidget )
145 {
146 mBlockValueUpdate = true;
147 mEditorWidget->setValues( initialValue, additionalFieldValues );
148 mBlockValueUpdate = false;
149 }
150 mPreviousValue = initialValue;
151 mPreviousAdditionalValues = additionalFieldValues;
152 setIsMixed( mixedValues );
153 mMultiEditButton->setIsChanged( false );
154 mIsChanged = false;
155 updateWidgets();
156}
157
159{
160 return mEditorWidget->value();
161}
162
163
164void QgsAttributeFormEditorWidget::editorWidgetValuesChanged( const QVariant &value, const QVariantList &additionalFieldValues )
165{
166 if ( mBlockValueUpdate )
167 return;
168
169 mIsChanged = true;
170
171 switch ( mode() )
172 {
173 case DefaultMode:
174 case SearchMode:
176 break;
177 case MultiEditMode:
178 mMultiEditButton->setIsChanged( true );
179 }
180
182 emit valueChanged( value );
184 emit valuesChanged( value, additionalFieldValues );
185}
186
187void QgsAttributeFormEditorWidget::resetValue()
188{
189 mIsChanged = false;
190 mBlockValueUpdate = true;
191 if ( mEditorWidget )
192 mEditorWidget->setValues( mPreviousValue, mPreviousAdditionalValues );
193 mBlockValueUpdate = false;
194
195 switch ( mode() )
196 {
197 case DefaultMode:
198 case SearchMode:
200 break;
201 case MultiEditMode:
202 {
203 mMultiEditButton->setIsChanged( false );
204 if ( mEditorWidget && mIsMixed )
205 mEditorWidget->showIndeterminateState();
206 break;
207 }
208 }
209}
210
211void QgsAttributeFormEditorWidget::setFieldTriggered()
212{
213 mIsChanged = true;
214}
215
216void QgsAttributeFormEditorWidget::onAggregateChanged()
217{
218 const auto constWigets( searchWidgetWrappers() );
219 for ( QgsSearchWidgetWrapper *searchWidget : constWigets )
220 searchWidget->setAggregate( mAggregateButton->aggregate() );
221}
222
223void QgsAttributeFormEditorWidget::updateWidgets()
224{
225 //first update the tool buttons
226 const bool hasMultiEditButton = ( editPage()->layout()->indexOf( mMultiEditButton ) >= 0 );
227
228 bool shouldShowMultiEditButton = false;
229 switch ( mode() )
230 {
234 // in these modes we don't show the multi edit button
235 shouldShowMultiEditButton = false;
236 break;
237
239 {
240 // in multi-edit mode we need to know upfront whether or not to allow add the multiedit buttons
241 // for this field.
242 // if the field is always read only regardless of the feature, no need to dig further. But otherwise
243 // we may need to test editability for the actual selected features...
244 const int fieldIndex = mEditorWidget->fieldIdx();
245 shouldShowMultiEditButton = !QgsVectorLayerUtils::fieldIsReadOnly( layer(), fieldIndex );
246 if ( shouldShowMultiEditButton )
247 {
248 // depending on the field type, the editability of the field may vary feature by feature (e.g. for joined
249 // fields coming from joins without the upsert on edit capabilities).
250 // But this feature-by-feature check is EXPENSIVE!!! (see https://github.com/qgis/QGIS/issues/41366), so
251 // avoid it whenever we can...
252 const bool fieldEditabilityDependsOnFeature = QgsVectorLayerUtils::fieldEditabilityDependsOnFeature( layer(), fieldIndex );
253 if ( fieldEditabilityDependsOnFeature )
254 {
255 QgsFeature feature;
257 while ( it.nextFeature( feature ) )
258 {
259 const bool isEditable = QgsVectorLayerUtils::fieldIsEditable( layer(), fieldIndex, feature );
260 if ( !isEditable )
261 {
262 // as soon as we find one read-only feature for the field, we can break early...
263 shouldShowMultiEditButton = false;
264 break;
265 }
266 }
267 }
268 }
269 }
270 break;
271 }
272
273 if ( hasMultiEditButton && !shouldShowMultiEditButton )
274 {
275 editPage()->layout()->removeWidget( mMultiEditButton );
276 mMultiEditButton->setParent( nullptr );
277 }
278 else if ( !hasMultiEditButton && shouldShowMultiEditButton )
279 {
280 editPage()->layout()->addWidget( mMultiEditButton );
281 }
282
284
285 switch ( mode() )
286 {
287 case DefaultMode:
288 case MultiEditMode:
289 {
290 editPage()->layout()->addWidget( mConstraintResultLabel );
291 break;
292 }
293
295 {
296 mAggregateButton->setVisible( true );
297 break;
298 }
299
300 case SearchMode:
301 {
302 mAggregateButton->setVisible( false );
303 break;
304 }
305 }
306}
Offers a toolbutton to choose between different aggregate functions.
void setType(QMetaType::Type type)
Based on the type of underlying data, some aggregates will be available or not.
void aggregateChanged()
The function name of the selected aggregate has changed.
This class contains context information for attribute editor widgets.
QgsAttributeFormEditorWidget(QgsEditorWidgetWrapper *editorWidget, const QString &widgetType, QgsAttributeForm *form)
Constructor for QgsAttributeFormEditorWidget.
void initialize(const QVariant &initialValue, bool mixedValues=false, const QVariantList &additionalFieldValues=QVariantList())
Resets the widget to an initial value.
void valuesChanged(const QVariant &value, const QVariantList &additionalFieldValues)
Emitted when the widget's value changes.
void changesCommitted()
Called when field values have been committed;.
Q_DECL_DEPRECATED void valueChanged(const QVariant &value)
Emitted when the widget's value changes.
QVariant currentValue() const
Returns the current value of the attached editor widget.
QgsEditorWidgetWrapper * editorWidget() const
Returns the editor widget wrapper.
void setConstraintStatus(const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result)
Set the constraint status for this widget.
void setIsMixed(bool mixed)
Sets whether the widget should be displayed in a "mixed values" mode.
void createSearchWidgetWrappers(const QgsAttributeEditorContext &context=QgsAttributeEditorContext()) override
Creates the search widget wrappers for the widget used when the form is in search mode.
void setConstraintResultVisible(bool editable)
Set the constraint result label visible or invisible according to the layer editable status.
Base class for all widgets shown on a QgsAttributeForm.
void setSearchWidgetWrapper(QgsSearchWidgetWrapper *wrapper)
Sets the search widget wrapper for the widget used when the form is in search mode.
QList< QgsSearchWidgetWrapper * > searchWidgetWrappers()
Returns the search widget wrapper used in this widget.
void setVisiblePageForMode(QgsAttributeFormWidget::Mode mode)
Sets the visible page in the widget to the page matching the specified mode.
void addAdditionalSearchWidgetWrapper(QgsSearchWidgetWrapper *wrapper)
Adds an additional search widget wrapper.
QWidget * searchWidgetFrame()
Returns the widget which should be used as a parent during construction of the search widget wrapper.
QWidget * editPage() const
Returns a pointer to the EDIT page widget.
QgsVectorLayer * layer()
The layer for which this widget and its form is shown.
Mode mode() const
Returns the current mode for the widget.
@ SearchMode
Layer search/filter mode.
@ MultiEditMode
Multi edit mode, both the editor widget and a QgsMultiEditToolButton is shown.
@ DefaultMode
Default mode, only the editor widget is shown.
@ AggregateSearchMode
Embedded in a search form, show additional aggregate function toolbutton.
QgsSearchWidgetWrapper * createSearchWidget(const QString &widgetId, QgsVectorLayer *vl, int fieldIdx, const QVariantMap &config, QWidget *parent, const QgsAttributeEditorContext &context=QgsAttributeEditorContext())
Manages an editor widget Widget and wrapper share the same parent.
virtual QVariant value() const =0
Will be used to access the widget's value.
virtual QVariantList additionalFieldValues() const
Will be used to access the widget's values for potential additional fields handled by the widget.
int fieldIdx() const
Access the field index.
virtual void showIndeterminateState()
Sets the widget to display in an indeterminate "mixed value" state.
void valuesChanged(const QVariant &value, const QVariantList &additionalFieldValues=QVariantList())
Emit this signal, whenever the value changed.
void setValues(const QVariant &value, const QVariantList &additionalValues)
Is called when the value of the widget or additional field values needs to be changed.
QgsField field() const
Access the field.
ConstraintResult
Result of constraint checks.
@ ConstraintResultFailSoft
Widget failed at least one soft (non-enforced) constraint.
@ ConstraintResultPass
Widget passed constraints successfully.
@ ConstraintResultFailHard
Widget failed at least one hard (enforced) constraint.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QMetaType::Type type
Definition qgsfield.h:60
QString name
Definition qgsfield.h:62
static QgsEditorWidgetRegistry * editorWidgetRegistry()
Returns the global editor widget registry, used for managing all known edit widget factories.
Definition qgsgui.cpp:95
A tool button widget which is displayed next to editor widgets in attribute forms,...
void setIsMixed(bool mixed)
Sets whether the associated field contains mixed values.
void changesCommitted()
Called when field values have been changed and field now contains all the same values.
void setIsChanged(bool changed)
Sets whether the associated field has changed.
void resetFieldValueTriggered()
Emitted when the "reset to original values" option is selected.
void setFieldValueTriggered()
Emitted when the "set field value for all features" option is selected.
void setField(const QgsField &field)
Sets the field associated with this button.
Shows a search widget on a filter form.
@ IsNotBetween
Supports searching for values outside of a set range.
@ Between
Supports searches between two values.
virtual FilterFlags supportedFlags() const
Returns filter flags supported by the search widget.
static bool fieldEditabilityDependsOnFeature(const QgsVectorLayer *layer, int fieldIndex)
Returns true if the editability of the field at index fieldIndex from layer may vary feature by featu...
static bool fieldIsEditable(const QgsVectorLayer *layer, int fieldIndex, const QgsFeature &feature)
Tests whether a field is editable for a particular feature.
static bool fieldIsReadOnly(const QgsVectorLayer *layer, int fieldIndex)
QgsFeatureIterator getSelectedFeatures(QgsFeatureRequest request=QgsFeatureRequest()) const
Returns an iterator of the selected features.
QWidget * widget()
Access the widget managed by this wrapper.
QVariant config(const QString &key, const QVariant &defaultVal=QVariant()) const
Use this inside your overridden classes to access the configuration.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6601
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6600