QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
31
33{
34 // recursively populate indicators
35 for ( int i = indexFrom; i <= indexTo; ++i )
36 {
37 QgsAttributesFormItem *childItem = item->child( i );
38
39 if ( QgsAttributesFormItem::isGroup( childItem ) )
40 {
41 if ( childItem->childCount() > 0 )
42 {
43 onAddedChildren( childItem, 0, childItem->childCount() - 1 );
44 }
45 }
46 else
47 {
48 updateItemIndicator( childItem );
49 }
50 }
51}
52
53std::unique_ptr<QgsAttributesFormTreeViewIndicator> QgsAttributesFormTreeViewIndicatorProvider::newIndicator( QgsAttributesFormItem *item )
54{
55 auto indicator = std::make_unique<QgsAttributesFormTreeViewIndicator>( this );
56 indicator->setIcon( QgsApplication::getThemeIcon( iconName( item ) ) );
57 indicator->setToolTip( tooltipText( item ) );
58 mIndicators.insert( indicator.get() );
59
60 return indicator;
61}
62
64{
65 if ( acceptsItem( item ) )
66 {
67 const QList<QgsAttributesFormTreeViewIndicator *> itemIndicators = mAttributesFormTreeView->indicators( item );
68
69 // maybe the indicator exists already
70 for ( QgsAttributesFormTreeViewIndicator *indicator : itemIndicators )
71 {
72 if ( mIndicators.contains( indicator ) )
73 {
74 indicator->setToolTip( tooltipText( item ) );
75 indicator->setIcon( QgsApplication::getThemeIcon( iconName( item ) ) );
76 return;
77 }
78 }
79
80 // it does not exist: need to create a new one
81 mAttributesFormTreeView->addIndicator( item, newIndicator( item ).release() );
82 }
83 else
84 {
85 removeItemIndicator( item );
86 }
87}
88
89void QgsAttributesFormTreeViewIndicatorProvider::removeItemIndicator( QgsAttributesFormItem *item )
90{
91 const QList<QgsAttributesFormTreeViewIndicator *> itemIndicators = mAttributesFormTreeView->indicators( item );
92
93 // Get rid of the existing indicator
94 for ( QgsAttributesFormTreeViewIndicator *indicator : itemIndicators )
95 {
96 if ( mIndicators.contains( indicator ) )
97 {
98 mIndicators.remove( indicator );
99 mAttributesFormTreeView->removeIndicator( item, indicator );
100 indicator->deleteLater();
101 return;
102 }
103 }
104}
105
110
112{
113 QgsAttributesFormItem *item = mAttributesFormTreeView->sourceModel()->rootItem();
114
115 if ( enabled )
116 {
117 if ( mEnabled )
118 {
119 return; // Already done
120 }
121
122 // Draw indicators for all existing items
123 if ( item->childCount() > 0 )
124 {
125 onAddedChildren( item, 0, item->childCount() - 1 );
126 }
127
128 // Connect
130 mEnabled = true;
131 }
132 else
133 {
134 if ( !mEnabled )
135 {
136 return; // Already done
137 }
138
139 // Disconnect
141
142 // Get rid of all item indicators in the view and in the provider
143 mAttributesFormTreeView->removeAllIndicators();
144 mIndicators.clear();
145 mEnabled = false;
146 }
147}
148
149
153
154bool QgsFieldConstraintIndicatorProvider::acceptsItem( QgsAttributesFormItem *item )
155{
156 if ( item->type() == QgsAttributesFormData::Field )
157 {
159 const QgsFieldConstraints constraints = config.mFieldConstraints;
160
162 return true;
163
165 return true;
166
168 return true;
169 }
170 return false;
171}
172
173QString QgsFieldConstraintIndicatorProvider::iconName( QgsAttributesFormItem *item )
174{
175 const QgsAttributesFormData::FieldConfig config = item->data( QgsAttributesFormModel::ItemFieldConfigRole ).value< QgsAttributesFormData::FieldConfig >();
176 const QgsFieldConstraints constraints = config.mFieldConstraints;
177
178 bool hardConstraint = false;
179
181 {
183 {
184 hardConstraint = true;
185 }
186 }
187
188 if ( !hardConstraint )
189 {
191 {
193 {
194 hardConstraint = true;
195 }
196 }
197 }
198
199 if ( !hardConstraint )
200 {
202 {
204 {
205 hardConstraint = true;
206 }
207 }
208 }
209
210 return hardConstraint ? u"/field_indicators/mIndicatorConstraintHard.svg"_s : u"/field_indicators/mIndicatorConstraint.svg"_s;
211}
212
213QString QgsFieldConstraintIndicatorProvider::tooltipText( QgsAttributesFormItem *item )
214{
215 const QgsAttributesFormData::FieldConfig config = item->data( QgsAttributesFormModel::ItemFieldConfigRole ).value< QgsAttributesFormData::FieldConfig >();
216 const QgsFieldConstraints constraints = config.mFieldConstraints;
217
218 auto addOriginAndStrengthText = [=]( QString name, QgsFieldConstraints::Constraint constraint ) {
219 return u"%1 (%2, %3)"_s.arg(
220 name,
221 constraints.constraintOrigin( constraint ) == QgsFieldConstraints::ConstraintOriginProvider ? tr( "provider" ) : tr( "layer" ),
222 constraints.constraintStrength( constraint ) == QgsFieldConstraints::ConstraintStrengthHard ? tr( "enforced" ) : tr( "unenforced" )
223 );
224 };
225
226 QString tooltipText;
228 {
229 tooltipText += addOriginAndStrengthText( tr( "Not Null" ), QgsFieldConstraints::ConstraintNotNull );
230 }
231
233 {
234 tooltipText += "\n" + addOriginAndStrengthText( tr( "Unique" ), QgsFieldConstraints::ConstraintUnique );
235 }
236
238 {
239 tooltipText += "\n" + addOriginAndStrengthText( tr( "Expression" ), QgsFieldConstraints::ConstraintExpression );
240 tooltipText += !constraints.constraintDescription().isEmpty() ? u"\n "_s + constraints.constraintDescription() : QString();
241 }
242
243 return tooltipText;
244}
245
246
250
251bool QgsFieldDefaultValueIndicatorProvider::acceptsItem( QgsAttributesFormItem *item )
252{
253 if ( item->type() == QgsAttributesFormData::Field )
254 {
256 return !config.mDefaultValueExpression.isEmpty();
257 }
258 return false;
259}
260
261QString QgsFieldDefaultValueIndicatorProvider::iconName( QgsAttributesFormItem *item )
262{
263 const QgsAttributesFormData::FieldConfig config = item->data( QgsAttributesFormModel::ItemFieldConfigRole ).value< QgsAttributesFormData::FieldConfig >();
264
265 QString iconName = u"/field_indicators/mIndicatorDefaultValue.svg"_s;
266
267 if ( !config.mDefaultValueExpression.isEmpty() )
268 {
269 if ( config.mApplyDefaultValueOnUpdate )
270 {
271 iconName = u"/field_indicators/mIndicatorDefaultValueApplyOnUpdate.svg"_s;
272 }
273 }
274 return iconName;
275}
276
277QString QgsFieldDefaultValueIndicatorProvider::tooltipText( QgsAttributesFormItem *item )
278{
279 const QgsAttributesFormData::FieldConfig config = item->data( QgsAttributesFormModel::ItemFieldConfigRole ).value< QgsAttributesFormData::FieldConfig >();
280 QString text;
281 if ( !config.mDefaultValueExpression.isEmpty() )
282 {
283 text += config.mDefaultValueExpression;
284 text += u"\n(%1)"_s.arg( config.mApplyDefaultValueOnUpdate ? tr( "Apply on update" ) : tr( "Do not apply on update" ) );
285 }
286 return text;
287}
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.