QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgsattributesformtreeviewindicatorprovider.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsattributesformtreeviewindicatorprovider.cpp
3 ---------------------
4 begin : June 2025
5 copyright : (C) 2025 by Germán Carrillo
6 email : german 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
17
18#include "qgsapplication.h"
19#include "qgsfieldconstraints.h"
20
21#include <QString>
22
23#include "moc_qgsattributesformtreeviewindicatorprovider.cpp"
24
25using namespace Qt::StringLiterals;
26
32
34{
35 // recursively populate indicators
36 for ( int i = indexFrom; i <= indexTo; ++i )
37 {
38 QgsAttributesFormItem *childItem = item->child( i );
39
40 if ( QgsAttributesFormItem::isGroup( childItem ) )
41 {
42 if ( childItem->childCount() > 0 )
43 {
44 onAddedChildren( childItem, 0, childItem->childCount() - 1 );
45 }
46 }
47 else
48 {
49 updateItemIndicator( childItem );
50 }
51 }
52}
53
54std::unique_ptr<QgsAttributesFormTreeViewIndicator> QgsAttributesFormTreeViewIndicatorProvider::newIndicator( QgsAttributesFormItem *item )
55{
56 auto indicator = std::make_unique<QgsAttributesFormTreeViewIndicator>( this );
57 indicator->setIcon( QgsApplication::getThemeIcon( iconName( item ) ) );
58 indicator->setToolTip( tooltipText( item ) );
59 mIndicators.insert( indicator.get() );
60
61 return indicator;
62}
63
65{
66 if ( acceptsItem( item ) )
67 {
68 const QList<QgsAttributesFormTreeViewIndicator *> itemIndicators = mAttributesFormTreeView->indicators( item );
69
70 // maybe the indicator exists already
71 for ( QgsAttributesFormTreeViewIndicator *indicator : itemIndicators )
72 {
73 if ( mIndicators.contains( indicator ) )
74 {
75 indicator->setToolTip( tooltipText( item ) );
76 indicator->setIcon( QgsApplication::getThemeIcon( iconName( item ) ) );
77 return;
78 }
79 }
80
81 // it does not exist: need to create a new one
82 mAttributesFormTreeView->addIndicator( item, newIndicator( item ).release() );
83 }
84 else
85 {
86 removeItemIndicator( item );
87 }
88}
89
90void QgsAttributesFormTreeViewIndicatorProvider::removeItemIndicator( QgsAttributesFormItem *item )
91{
92 const QList<QgsAttributesFormTreeViewIndicator *> itemIndicators = mAttributesFormTreeView->indicators( item );
93
94 // Get rid of the existing indicator
95 for ( QgsAttributesFormTreeViewIndicator *indicator : itemIndicators )
96 {
97 if ( mIndicators.contains( indicator ) )
98 {
99 mIndicators.remove( indicator );
100 mAttributesFormTreeView->removeIndicator( item, indicator );
101 indicator->deleteLater();
102 return;
103 }
104 }
105}
106
111
113{
114 QgsAttributesFormItem *item = mAttributesFormTreeView->sourceModel()->rootItem();
115
116 if ( enabled )
117 {
118 if ( mEnabled )
119 {
120 return; // Already done
121 }
122
123 // Draw indicators for all existing items
124 if ( item->childCount() > 0 )
125 {
126 onAddedChildren( item, 0, item->childCount() - 1 );
127 }
128
129 // Connect
131 mEnabled = true;
132 }
133 else
134 {
135 if ( !mEnabled )
136 {
137 return; // Already done
138 }
139
140 // Disconnect
142
143 // Get rid of all item indicators in the view and in the provider
144 mAttributesFormTreeView->removeAllIndicators();
145 mIndicators.clear();
146 mEnabled = false;
147 }
148}
149
150
155
156bool QgsFieldConstraintIndicatorProvider::acceptsItem( QgsAttributesFormItem *item )
157{
158 if ( item->type() == QgsAttributesFormData::Field )
159 {
161 const QgsFieldConstraints constraints = config.mFieldConstraints;
162
164 return true;
165
167 return true;
168
170 return true;
171 }
172 return false;
173}
174
175QString QgsFieldConstraintIndicatorProvider::iconName( QgsAttributesFormItem *item )
176{
177 const QgsAttributesFormData::FieldConfig config = item->data( QgsAttributesFormModel::ItemFieldConfigRole ).value< QgsAttributesFormData::FieldConfig >();
178 const QgsFieldConstraints constraints = config.mFieldConstraints;
179
180 bool hardConstraint = false;
181
183 {
185 {
186 hardConstraint = true;
187 }
188 }
189
190 if ( !hardConstraint )
191 {
193 {
195 {
196 hardConstraint = true;
197 }
198 }
199 }
200
201 if ( !hardConstraint )
202 {
204 {
206 {
207 hardConstraint = true;
208 }
209 }
210 }
211
212 return hardConstraint ? u"/field_indicators/mIndicatorConstraintHard.svg"_s : u"/field_indicators/mIndicatorConstraint.svg"_s;
213}
214
215QString QgsFieldConstraintIndicatorProvider::tooltipText( QgsAttributesFormItem *item )
216{
217 const QgsAttributesFormData::FieldConfig config = item->data( QgsAttributesFormModel::ItemFieldConfigRole ).value< QgsAttributesFormData::FieldConfig >();
218 const QgsFieldConstraints constraints = config.mFieldConstraints;
219
220 auto addOriginAndStrengthText = [=]( QString name, QgsFieldConstraints::Constraint constraint ) {
221 return u"%1 (%2, %3)"_s.arg( name, constraints.constraintOrigin( constraint ) == QgsFieldConstraints::ConstraintOriginProvider ? tr( "provider" ) : tr( "layer" ), constraints.constraintStrength( constraint ) == QgsFieldConstraints::ConstraintStrengthHard ? tr( "enforced" ) : tr( "unenforced" ) );
222 };
223
224 QString tooltipText;
226 {
227 tooltipText += addOriginAndStrengthText( tr( "Not Null" ), QgsFieldConstraints::ConstraintNotNull );
228 }
229
231 {
232 tooltipText += "\n" + addOriginAndStrengthText( tr( "Unique" ), QgsFieldConstraints::ConstraintUnique );
233 }
234
236 {
237 tooltipText += "\n" + addOriginAndStrengthText( tr( "Expression" ), QgsFieldConstraints::ConstraintExpression );
238 tooltipText += !constraints.constraintDescription().isEmpty() ? u"\n "_s + constraints.constraintDescription() : QString();
239 }
240
241 return tooltipText;
242}
243
244
249
250bool QgsFieldDefaultValueIndicatorProvider::acceptsItem( QgsAttributesFormItem *item )
251{
252 if ( item->type() == QgsAttributesFormData::Field )
253 {
255 return !config.mDefaultValueExpression.isEmpty();
256 }
257 return false;
258}
259
260QString QgsFieldDefaultValueIndicatorProvider::iconName( QgsAttributesFormItem *item )
261{
262 const QgsAttributesFormData::FieldConfig config = item->data( QgsAttributesFormModel::ItemFieldConfigRole ).value< QgsAttributesFormData::FieldConfig >();
263
264 QString iconName = u"/field_indicators/mIndicatorDefaultValue.svg"_s;
265
266 if ( !config.mDefaultValueExpression.isEmpty() )
267 {
268 if ( config.mApplyDefaultValueOnUpdate )
269 {
270 iconName = u"/field_indicators/mIndicatorDefaultValueApplyOnUpdate.svg"_s;
271 }
272 }
273 return iconName;
274}
275
276QString QgsFieldDefaultValueIndicatorProvider::tooltipText( QgsAttributesFormItem *item )
277{
278 const QgsAttributesFormData::FieldConfig config = item->data( QgsAttributesFormModel::ItemFieldConfigRole ).value< QgsAttributesFormData::FieldConfig >();
279 QString text;
280 if ( !config.mDefaultValueExpression.isEmpty() )
281 {
282 text += config.mDefaultValueExpression;
283 text += u"\n(%1)"_s.arg( config.mApplyDefaultValueOnUpdate ? tr( "Apply on update" ) : tr( "Do not apply on update" ) );
284 }
285 return text;
286}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Graphical representation for the attribute drag and drop editor.
void removeIndicator(QgsAttributesFormItem *item, QgsAttributesFormTreeViewIndicator *indicator)
Removes the indicator from the given item.
const QList< QgsAttributesFormTreeViewIndicator * > indicators(const QModelIndex &index) const
Returns the list of indicators associated with a given index.
@ Field
Vector layer field.
Holds parent-child relations as well as item data contained in a QgsAttributesFormModel.
int childCount() const
Returns the number of children items for the given item.
QgsAttributesFormItem * child(int row)
Access the child item located at row position.
static bool isGroup(QgsAttributesFormItem *item)
Returns whether the item is a group.
QgsAttributesFormData::AttributesFormItemType type() const
Returns the type of the item.
QVariant data(int role) const
Returns the data stored in the item, corresponding to the given role.
void addedChildren(QgsAttributesFormItem *item, int indexFrom, int indexTo)
Notifies other objects when children have been added to the item, informing the indices where added c...
@ ItemFieldConfigRole
Prior to QGIS 3.44, this was available as FieldConfigRole.
void setEnabled(bool enabled)
Enables or disables the provider.
void updateItemIndicator(QgsAttributesFormItem *item)
Updates the state of a the indicator for the given item.
void onAddedChildren(QgsAttributesFormItem *item, int indexFrom, int indexTo)
Connects to signals of new items added to the tree.
bool isEnabled() const
Returns whether the provider is enabled or not.
QgsAttributesFormTreeViewIndicatorProvider(QgsAttributesFormBaseView *view)
Constructor for QgsAttributesFormTreeViewIndicatorProvider.
Indicator that can be used in an Attributes Form tree view to display icons next to field items.
QgsFieldConstraintIndicatorProvider(QgsAttributesFormBaseView *view)
Constructor for QgsFieldConstraintIndicatorProvider.
Stores information about constraints which may be present on a field.
@ ConstraintStrengthHard
Constraint must be honored before feature can be accepted.
@ ConstraintOriginNotSet
Constraint is not set.
@ ConstraintOriginProvider
Constraint was set at data provider.
Constraint
Constraints which may be present on a field.
@ ConstraintNotNull
Field may not be null.
@ ConstraintUnique
Field must have a unique value.
@ ConstraintExpression
Field has an expression constraint set. See constraintExpression().
QgsFieldDefaultValueIndicatorProvider(QgsAttributesFormBaseView *view)
Constructor for QgsFieldDefaultValueIndicatorProvider.
Holds the configuration for a field.