QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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,
79 searchWidgetFrame(), context );
81 searchWidgetFrame()->layout()->addWidget( mAggregateButton );
84 {
85 // create secondary widget for between type searches
86 QgsSearchWidgetWrapper *sww2 = QgsGui::editorWidgetRegistry()->createSearchWidget( mWidgetType, layer(), fieldIdx, config,
87 searchWidgetFrame(), context );
89 }
90}
91
92void QgsAttributeFormEditorWidget::setConstraintStatus( const QString &constraint, const QString &description, const QString &err, QgsEditorWidgetWrapper::ConstraintResult result )
93{
94 switch ( result )
95 {
97 mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#FF9800\">%1</font>" ).arg( QChar( 0x2718 ) ) );
98 mConstraintResultLabel->setToolTip( description.isEmpty() ? QStringLiteral( "<b>%1</b>: %2" ).arg( constraint, err ) : description );
99 break;
100
102 mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#FFC107\">%1</font>" ).arg( QChar( 0x2718 ) ) );
103 mConstraintResultLabel->setToolTip( description.isEmpty() ? QStringLiteral( "<b>%1</b>: %2" ).arg( constraint, err ) : description );
104 break;
105
107 mConstraintResultLabel->setText( QStringLiteral( "<font color=\"#259B24\">%1</font>" ).arg( QChar( 0x2714 ) ) );
108 mConstraintResultLabel->setToolTip( description );
109 break;
110 }
111}
112
114{
115 mConstraintResultLabel->setHidden( !editable );
116}
117
119{
120 return mEditorWidget;
121}
122
124{
125 if ( mEditorWidget && mixed )
126 mEditorWidget->showIndeterminateState();
127 mMultiEditButton->setIsMixed( mixed );
128 mIsMixed = mixed;
129}
130
132{
133 if ( mEditorWidget )
134 {
135 mPreviousValue = mEditorWidget->value();
136 mPreviousAdditionalValues = mEditorWidget->additionalFieldValues();
137 }
138
139 setIsMixed( false );
140 mMultiEditButton->changesCommitted();
141 mIsChanged = false;
142}
143
144
145
146void QgsAttributeFormEditorWidget::initialize( const QVariant &initialValue, bool mixedValues, const QVariantList &additionalFieldValues )
147{
148 if ( mEditorWidget )
149 {
150 mBlockValueUpdate = true;
151 mEditorWidget->setValues( initialValue, additionalFieldValues );
152 mBlockValueUpdate = false;
153 }
154 mPreviousValue = initialValue;
155 mPreviousAdditionalValues = additionalFieldValues;
156 setIsMixed( mixedValues );
157 mMultiEditButton->setIsChanged( false );
158 mIsChanged = false;
159 updateWidgets();
160}
161
163{
164 return mEditorWidget->value();
165}
166
167
168
169void QgsAttributeFormEditorWidget::editorWidgetValuesChanged( const QVariant &value, const QVariantList &additionalFieldValues )
170{
171 if ( mBlockValueUpdate )
172 return;
173
174 mIsChanged = true;
175
176 switch ( mode() )
177 {
178 case DefaultMode:
179 case SearchMode:
181 break;
182 case MultiEditMode:
183 mMultiEditButton->setIsChanged( true );
184 }
185
187 emit valueChanged( value );
189 emit valuesChanged( value, additionalFieldValues );
190}
191
192void QgsAttributeFormEditorWidget::resetValue()
193{
194 mIsChanged = false;
195 mBlockValueUpdate = true;
196 if ( mEditorWidget )
197 mEditorWidget->setValues( mPreviousValue, mPreviousAdditionalValues );
198 mBlockValueUpdate = false;
199
200 switch ( mode() )
201 {
202 case DefaultMode:
203 case SearchMode:
205 break;
206 case MultiEditMode:
207 {
208 mMultiEditButton->setIsChanged( false );
209 if ( mEditorWidget && mIsMixed )
210 mEditorWidget->showIndeterminateState();
211 break;
212 }
213 }
214}
215
216void QgsAttributeFormEditorWidget::setFieldTriggered()
217{
218 mIsChanged = true;
219}
220
221void QgsAttributeFormEditorWidget::onAggregateChanged()
222{
223 const auto constWigets( searchWidgetWrappers() );
224 for ( QgsSearchWidgetWrapper *searchWidget : constWigets )
225 searchWidget->setAggregate( mAggregateButton->aggregate() );
226}
227
228void QgsAttributeFormEditorWidget::updateWidgets()
229{
230 //first update the tool buttons
231 const bool hasMultiEditButton = ( editPage()->layout()->indexOf( mMultiEditButton ) >= 0 );
232
233 bool shouldShowMultiEditButton = false;
234 switch ( mode() )
235 {
239 // in these modes we don't show the multi edit button
240 shouldShowMultiEditButton = false;
241 break;
242
244 {
245 // in multi-edit mode we need to know upfront whether or not to allow add the multiedit buttons
246 // for this field.
247 // if the field is always read only regardless of the feature, no need to dig further. But otherwise
248 // we may need to test editability for the actual selected features...
249 const int fieldIndex = mEditorWidget->fieldIdx();
250 shouldShowMultiEditButton = !QgsVectorLayerUtils::fieldIsReadOnly( layer(), fieldIndex );
251 if ( shouldShowMultiEditButton )
252 {
253 // depending on the field type, the editability of the field may vary feature by feature (e.g. for joined
254 // fields coming from joins without the upsert on edit capabilities).
255 // But this feature-by-feature check is EXPENSIVE!!! (see https://github.com/qgis/QGIS/issues/41366), so
256 // avoid it whenever we can...
257 const bool fieldEditabilityDependsOnFeature = QgsVectorLayerUtils::fieldEditabilityDependsOnFeature( layer(), fieldIndex );
258 if ( fieldEditabilityDependsOnFeature )
259 {
260 QgsFeature feature;
262 while ( it.nextFeature( feature ) )
263 {
264 const bool isEditable = QgsVectorLayerUtils::fieldIsEditable( layer(), fieldIndex, feature );
265 if ( !isEditable )
266 {
267 // as soon as we find one read-only feature for the field, we can break early...
268 shouldShowMultiEditButton = false;
269 break;
270 }
271 }
272 }
273 }
274 }
275 break;
276 }
277
278 if ( hasMultiEditButton && !shouldShowMultiEditButton )
279 {
280 editPage()->layout()->removeWidget( mMultiEditButton );
281 mMultiEditButton->setParent( nullptr );
282 }
283 else if ( !hasMultiEditButton && shouldShowMultiEditButton )
284 {
285 editPage()->layout()->addWidget( mMultiEditButton );
286 }
287
289
290 switch ( mode() )
291 {
292 case DefaultMode:
293 case MultiEditMode:
294 {
295 editPage()->layout()->addWidget( mConstraintResultLabel );
296 break;
297 }
298
300 {
301 mAggregateButton->setVisible( true );
302 break;
303 }
304
305 case SearchMode:
306 {
307 mAggregateButton->setVisible( false );
308 break;
309 }
310 }
311}
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:94
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:6535
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6534