QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgsprocessingaggregatewidgets.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingaggregatewidgets.cpp
3 ---------------------
4 Date : June 2020
5 Copyright : (C) 2020 by 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
20
21#include <QBoxLayout>
22#include <QLineEdit>
23#include <QMessageBox>
24#include <QPushButton>
25#include <QStandardItemModel>
26#include <QToolButton>
27#include <QTableView>
28#include <mutex>
29
30
31//
32// QgsAggregateMappingModel
33//
34
36 QObject *parent )
37 : QAbstractTableModel( parent )
38 , mExpressionContextGenerator( new QgsFieldMappingModel::ExpressionContextGenerator( sourceFields ) )
39{
41}
42
43QVariant QgsAggregateMappingModel::headerData( int section, Qt::Orientation orientation, int role ) const
44{
45 if ( role == Qt::DisplayRole )
46 {
47 switch ( orientation )
48 {
49 case Qt::Horizontal:
50 {
51 switch ( static_cast<ColumnDataIndex>( section ) )
52 {
54 {
55 return tr( "Source Expression" );
56 }
58 {
59 return tr( "Aggregate Function" );
60 }
62 {
63 return tr( "Delimiter" );
64 }
66 {
67 return tr( "Name" );
68 }
70 {
71 return tr( "Type" );
72 }
74 {
75 return tr( "Length" );
76 }
78 {
79 return tr( "Precision" );
80 }
81 }
82 break;
83 }
84 case Qt::Vertical:
85 {
86 return section;
87 }
88 }
89 }
90 return QVariant();
91}
92
94{
95 return mSourceFields;
96}
97
98int QgsAggregateMappingModel::rowCount( const QModelIndex &parent ) const
99{
100 if ( parent.isValid() )
101 return 0;
102 return mMapping.count();
103}
104
105int QgsAggregateMappingModel::columnCount( const QModelIndex &parent ) const
106{
107 if ( parent.isValid() )
108 return 0;
109 return 7;
110}
111
112QVariant QgsAggregateMappingModel::data( const QModelIndex &index, int role ) const
113{
114 if ( index.isValid() )
115 {
116 const ColumnDataIndex col { static_cast<ColumnDataIndex>( index.column() ) };
117 const Aggregate &agg { mMapping.at( index.row() ) };
118
119 switch ( role )
120 {
121 case Qt::DisplayRole:
122 case Qt::EditRole:
123 {
124 switch ( col )
125 {
127 {
128 return agg.source;
129 }
131 {
132 return agg.aggregate;
133 }
135 {
136 return agg.delimiter;
137 }
139 {
140 return agg.field.displayName();
141 }
143 {
144 return agg.field.typeName();
145 }
147 {
148 return agg.field.length();
149 }
151 {
152 return agg.field.precision();
153 }
154 }
155 break;
156 }
157 }
158 }
159 return QVariant();
160}
161
162Qt::ItemFlags QgsAggregateMappingModel::flags( const QModelIndex &index ) const
163{
164 if ( index.isValid() )
165 {
166 return Qt::ItemFlags( Qt::ItemIsSelectable |
167 Qt::ItemIsEditable |
168 Qt::ItemIsEnabled );
169 }
170 return Qt::ItemFlags();
171}
172
173bool QgsAggregateMappingModel::setData( const QModelIndex &index, const QVariant &value, int role )
174{
175 if ( index.isValid() )
176 {
177 if ( role == Qt::EditRole )
178 {
179 Aggregate &f = mMapping[index.row()];
180 switch ( static_cast<ColumnDataIndex>( index.column() ) )
181 {
183 {
184 const QgsExpression exp { value.toString() };
185 f.source = exp;
186 break;
187 }
189 {
190 f.aggregate = value.toString();
191 break;
192 }
194 {
195 f.delimiter = value.toString();
196 break;
197 }
199 {
200 f.field.setName( value.toString() );
201 break;
202 }
204 {
205 QgsFieldMappingModel::setFieldTypeFromName( f.field, value.toString() );
206 break;
207 }
209 {
210 bool ok;
211 const int length { value.toInt( &ok ) };
212 if ( ok )
213 f.field.setLength( length );
214 break;
215 }
217 {
218 bool ok;
219 const int precision { value.toInt( &ok ) };
220 if ( ok )
222 break;
223 }
224 }
225 emit dataChanged( index, index );
226 }
227 return true;
228 }
229 else
230 {
231 return false;
232 }
233}
234
235
236bool QgsAggregateMappingModel::moveUpOrDown( const QModelIndex &index, bool up )
237{
238 if ( ! index.isValid() && index.model() == this )
239 return false;
240
241 // Always swap down
242 const int row { up ? index.row() - 1 : index.row() };
243 // Range checking
244 if ( row < 0 || row + 1 >= rowCount( QModelIndex() ) )
245 {
246 return false;
247 }
248 beginMoveRows( QModelIndex( ), row, row, QModelIndex(), row + 2 );
249 mMapping.swapItemsAt( row, row + 1 );
250 endMoveRows();
251 return true;
252}
253
255{
256 mSourceFields = sourceFields;
257 if ( mExpressionContextGenerator )
258 mExpressionContextGenerator->setSourceFields( mSourceFields );
259
260 const QStringList usedFields;
261 beginResetModel();
262 mMapping.clear();
263
264 for ( const QgsField &f : sourceFields )
265 {
266 Aggregate aggregate;
267 aggregate.field = f;
268 aggregate.field.setTypeName( QgsFieldMappingModel::qgsFieldToTypeName( f ) );
269 aggregate.source = QgsExpression::quotedColumnRef( f.name() );
270
271 if ( f.isNumeric() )
272 aggregate.aggregate = QStringLiteral( "sum" );
273 else if ( f.type() == QVariant::String || ( f.type() == QVariant::List && f.subType() == QVariant::String ) )
274 aggregate.aggregate = QStringLiteral( "concatenate" );
275
276 aggregate.delimiter = ',';
277
278 mMapping.push_back( aggregate );
279 }
280 endResetModel();
281}
282
284{
285 return mExpressionContextGenerator.get();
286}
287
289{
290 mExpressionContextGenerator->setBaseExpressionContextGenerator( generator );
291}
292
293QList<QgsAggregateMappingModel::Aggregate> QgsAggregateMappingModel::mapping() const
294{
295 return mMapping;
296}
297
298void QgsAggregateMappingModel::setMapping( const QList<QgsAggregateMappingModel::Aggregate> &mapping )
299{
300 beginResetModel();
301 mMapping = mapping;
302 for ( auto &agg : mMapping )
303 {
304 agg.field.setTypeName( QgsFieldMappingModel::qgsFieldToTypeName( agg.field ) );
305 }
306 endResetModel();
307}
308
309void QgsAggregateMappingModel::appendField( const QgsField &field, const QString &source, const QString &aggregate )
310{
311 const int lastRow { rowCount( QModelIndex( ) ) };
312 beginInsertRows( QModelIndex(), lastRow, lastRow );
313 Aggregate agg;
314 agg.field = field;
315 agg.field.setTypeName( QgsFieldMappingModel::qgsFieldToTypeName( field ) );
316 agg.source = source;
317 agg.aggregate = aggregate;
318 agg.delimiter = ',';
319 mMapping.push_back( agg );
320 endInsertRows( );
321}
322
323bool QgsAggregateMappingModel::removeField( const QModelIndex &index )
324{
325 if ( index.isValid() && index.model() == this && index.row() < rowCount( QModelIndex() ) )
326 {
327 beginRemoveRows( QModelIndex(), index.row(), index.row() );
328 mMapping.removeAt( index.row() );
329 endRemoveRows();
330 return true;
331 }
332 else
333 {
334 return false;
335 }
336}
337
338bool QgsAggregateMappingModel::moveUp( const QModelIndex &index )
339{
340 return moveUpOrDown( index );
341}
342
343bool QgsAggregateMappingModel::moveDown( const QModelIndex &index )
344{
345 return moveUpOrDown( index, false );
346}
347
348
349//
350// QgsAggregateMappingWidget
351//
352
354 const QgsFields &sourceFields )
355 : QgsPanelWidget( parent )
356{
357 QVBoxLayout *verticalLayout = new QVBoxLayout();
358 verticalLayout->setContentsMargins( 0, 0, 0, 0 );
359 mTableView = new QTableView();
360 verticalLayout->addWidget( mTableView );
361 setLayout( verticalLayout );
362
363 mModel = new QgsAggregateMappingModel( sourceFields, this );
364 mTableView->setModel( mModel );
365 mTableView->setItemDelegateForColumn( static_cast<int>( QgsAggregateMappingModel::ColumnDataIndex::SourceExpression ), new QgsFieldMappingExpressionDelegate( this ) );
366 mTableView->setItemDelegateForColumn( static_cast<int>( QgsAggregateMappingModel::ColumnDataIndex::Aggregate ), new QgsAggregateMappingDelegate( mTableView ) );
367 mTableView->setItemDelegateForColumn( static_cast<int>( QgsAggregateMappingModel::ColumnDataIndex::DestinationType ), new QgsFieldMappingTypeDelegate( mTableView ) );
368 updateColumns();
369 // Make sure columns are updated when rows are added
370 connect( mModel, &QgsAggregateMappingModel::rowsInserted, this, [ = ] { updateColumns(); } );
371 connect( mModel, &QgsAggregateMappingModel::modelReset, this, [ = ] { updateColumns(); } );
372 connect( mModel, &QgsAggregateMappingModel::dataChanged, this, &QgsAggregateMappingWidget::changed );
373 connect( mModel, &QgsAggregateMappingModel::rowsInserted, this, &QgsAggregateMappingWidget::changed );
374 connect( mModel, &QgsAggregateMappingModel::rowsRemoved, this, &QgsAggregateMappingWidget::changed );
375 connect( mModel, &QgsAggregateMappingModel::modelReset, this, &QgsAggregateMappingWidget::changed );
376}
377
379{
380 return qobject_cast<QgsAggregateMappingModel *>( mModel );
381}
382
383QList<QgsAggregateMappingModel::Aggregate> QgsAggregateMappingWidget::mapping() const
384{
385 return model()->mapping();
386}
387
388void QgsAggregateMappingWidget::setMapping( const QList<QgsAggregateMappingModel::Aggregate> &mapping )
389{
391}
392
394{
395 return mTableView->selectionModel();
396}
397
399{
400 model()->setSourceFields( sourceFields );
401}
402
404{
405 mSourceLayer = layer;
406}
407
409{
410 return mSourceLayer;
411}
412
413void QgsAggregateMappingWidget::scrollTo( const QModelIndex &index ) const
414{
415 mTableView->scrollTo( index );
416}
417
419{
421}
422
423void QgsAggregateMappingWidget::appendField( const QgsField &field, const QString &source, const QString &aggregate )
424{
425 model()->appendField( field, source, aggregate );
426}
427
429{
430 if ( ! mTableView->selectionModel()->hasSelection() )
431 return false;
432
433 std::list<int> rowsToRemove { selectedRows() };
434 rowsToRemove.reverse();
435 for ( const int row : rowsToRemove )
436 {
437 if ( ! model()->removeField( model()->index( row, 0, QModelIndex() ) ) )
438 {
439 return false;
440 }
441 }
442 return true;
443}
444
446{
447 if ( ! mTableView->selectionModel()->hasSelection() )
448 return false;
449
450 const std::list<int> rowsToMoveUp { selectedRows() };
451 for ( const int row : rowsToMoveUp )
452 {
453 if ( ! model()->moveUp( model()->index( row, 0, QModelIndex() ) ) )
454 {
455 return false;
456 }
457 }
458 return true;
459}
460
462{
463 if ( ! mTableView->selectionModel()->hasSelection() )
464 return false;
465
466 std::list<int> rowsToMoveDown { selectedRows() };
467 rowsToMoveDown.reverse();
468 for ( const int row : rowsToMoveDown )
469 {
470 if ( ! model()->moveDown( model()->index( row, 0, QModelIndex() ) ) )
471 {
472 return false;
473 }
474 }
475 return true;
476}
477
478void QgsAggregateMappingWidget::updateColumns()
479{
480 for ( int i = 0; i < mModel->rowCount(); ++i )
481 {
482 mTableView->openPersistentEditor( mModel->index( i, static_cast<int>( QgsAggregateMappingModel::ColumnDataIndex::SourceExpression ) ) );
483 mTableView->openPersistentEditor( mModel->index( i, static_cast<int>( QgsAggregateMappingModel::ColumnDataIndex::DestinationType ) ) );
484 mTableView->openPersistentEditor( mModel->index( i, static_cast<int>( QgsAggregateMappingModel::ColumnDataIndex::Aggregate ) ) );
485 }
486
487 for ( int i = 0; i < mModel->columnCount(); ++i )
488 {
489 mTableView->resizeColumnToContents( i );
490 }
491}
492
493std::list<int> QgsAggregateMappingWidget::selectedRows()
494{
495 std::list<int> rows;
496 if ( mTableView->selectionModel()->hasSelection() )
497 {
498 const QModelIndexList constSelection { mTableView->selectionModel()->selectedIndexes() };
499 for ( const QModelIndex &index : constSelection )
500 {
501 rows.push_back( index.row() );
502 }
503 rows.sort();
504 rows.unique();
505 }
506 return rows;
507}
508
509
511
512//
513// AggregateDelegate
514//
515
516QgsAggregateMappingDelegate::QgsAggregateMappingDelegate( QObject *parent )
517 : QStyledItemDelegate( parent )
518{
519}
520
521QWidget *QgsAggregateMappingDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex & ) const
522{
523 Q_UNUSED( option )
524 QComboBox *editor = new QComboBox( parent );
525
526 const QStringList aggregateList { aggregates() };
527 int i = 0;
528 for ( const QString &aggregate : aggregateList )
529 {
530 editor->addItem( aggregate );
531 editor->setItemData( i, aggregate, Qt::UserRole );
532 ++i;
533 }
534
535 connect( editor,
536 qOverload<int >( &QComboBox::currentIndexChanged ),
537 this,
538 [ = ]( int currentIndex )
539 {
540 Q_UNUSED( currentIndex )
541 const_cast< QgsAggregateMappingDelegate *>( this )->emit commitData( editor );
542 } );
543
544 return editor;
545}
546
547void QgsAggregateMappingDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
548{
549 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
550 if ( ! editorWidget )
551 return;
552
553 const QVariant value = index.model()->data( index, Qt::EditRole );
554 editorWidget->setCurrentIndex( editorWidget->findData( value ) );
555}
556
557void QgsAggregateMappingDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
558{
559 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
560 if ( ! editorWidget )
561 return;
562
563 const QVariant currentValue = editorWidget->currentData( );
564 model->setData( index, currentValue, Qt::EditRole );
565}
566
567const QStringList QgsAggregateMappingDelegate::aggregates()
568{
569 static QStringList sAggregates;
570 static std::once_flag initialized;
571 std::call_once( initialized, [ = ]( )
572 {
573 sAggregates << QStringLiteral( "first_value" )
574 << QStringLiteral( "last_value" );
575
576 const QList<QgsExpressionFunction *> functions = QgsExpression::Functions();
577 for ( const QgsExpressionFunction *function : functions )
578 {
579 if ( !function || function->isDeprecated() || function->name().isEmpty() || function->name().at( 0 ) == '_' )
580 continue;
581
582 if ( function->groups().contains( QLatin1String( "Aggregates" ) ) )
583 {
584 if ( function->name() == QLatin1String( "aggregate" )
585 || function->name() == QLatin1String( "relation_aggregate" ) )
586 continue;
587
588 sAggregates.append( function->name() );
589 }
590
591 std::sort( sAggregates.begin(), sAggregates.end() );
592 }
593 } );
594
595 return sAggregates;
596}
597
The QgsAggregateMappingModel holds mapping information for defining sets of aggregates of fields from...
QgsFields sourceFields() const
Returns a list of source fields.
void appendField(const QgsField &field, const QString &source=QString(), const QString &aggregate=QString())
Appends a new field to the model, with an optional source and aggregate.
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
ColumnDataIndex
The ColumnDataIndex enum represents the column index for the view.
@ DestinationPrecision
Destination field precision.
@ DestinationName
Destination field name.
@ DestinationType
Destination field type string.
@ DestinationLength
Destination field length.
Qt::ItemFlags flags(const QModelIndex &index) const override
bool removeField(const QModelIndex &index)
Removes the field at index from the model, returns true on success.
bool moveUp(const QModelIndex &index)
Moves down the field at index.
QVariant data(const QModelIndex &index, int role) const override
QList< QgsAggregateMappingModel::Aggregate > mapping() const
Returns a list of Aggregate objects representing the current status of the model.
bool setData(const QModelIndex &index, const QVariant &value, int role) override
QgsExpressionContextGenerator * contextGenerator() const
Returns the context generator with the source fields.
bool moveDown(const QModelIndex &index)
Moves up the field at index.
void setSourceFields(const QgsFields &sourceFields)
Set source fields to sourceFields.
void setMapping(const QList< QgsAggregateMappingModel::Aggregate > &mapping)
Sets the mapping to show in the model.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void setBaseExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Sets the base expression context generator, which will generate the expression contexts for expressio...
QgsAggregateMappingModel(const QgsFields &sourceFields=QgsFields(), QObject *parent=nullptr)
Constructs a QgsAggregateMappingModel from a set of sourceFields.
QList< QgsAggregateMappingModel::Aggregate > mapping() const
Returns a list of Aggregate objects representing the current status of the underlying mapping model.
void changed()
Emitted when the aggregates defined in the widget are changed.
void setMapping(const QList< QgsAggregateMappingModel::Aggregate > &mapping)
Sets the mapping to show in the model.
void appendField(const QgsField &field, const QString &source=QString(), const QString &aggregate=QString())
Appends a new field to the model, with an optional source and aggregate.
QgsVectorLayer * sourceLayer()
Returns the source layer for use when generating expression previews.
QgsAggregateMappingModel * model() const
Returns the underlying mapping model.
void setSourceFields(const QgsFields &sourceFields)
Set source fields of the underlying mapping model to sourceFields.
bool moveSelectedFieldsDown()
Moves down currently selected field.
QgsAggregateMappingWidget(QWidget *parent=nullptr, const QgsFields &sourceFields=QgsFields())
Constructs a QgsAggregateMappingWidget from a set of sourceFields.
void registerExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
QItemSelectionModel * selectionModel()
Returns the selection model.
bool moveSelectedFieldsUp()
Moves up currently selected field.
void scrollTo(const QModelIndex &index) const
Scroll the fields view to index.
bool removeSelectedFields()
Removes the currently selected field from the model.
void setSourceLayer(QgsVectorLayer *layer)
Sets a source layer to use when generating expression previews in the widget.
Abstract interface for generating an expression context.
A abstract base class for defining QgsExpression functions.
Class for parsing and evaluation of expressions (formerly called "search strings").
static const QList< QgsExpressionFunction * > & Functions()
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
The QgsFieldMappingModel holds mapping information for mapping from one set of QgsFields to another,...
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
void setPrecision(int precision)
Set the field precision.
Definition: qgsfield.cpp:200
void setName(const QString &name)
Set the field name.
Definition: qgsfield.cpp:176
void setLength(int len)
Set the field length.
Definition: qgsfield.cpp:196
void setTypeName(const QString &typeName)
Set the field type.
Definition: qgsfield.cpp:191
Container of fields for a vector layer.
Definition: qgsfields.h:45
Base class for any widget that can be shown as a inline panel.
Represents a vector layer which manages a vector based data sets.
const QgsField & field
Definition: qgsfield.h:463
int precision
The Aggregate struct holds information about an aggregate column.
QString source
The source expression used as the input for the aggregate calculation.
QgsField field
The field in its current status (it might have been renamed)