25#include <QStandardItemModel>
37 : QAbstractTableModel( parent )
38 , mExpressionContextGenerator( new
QgsFieldMappingModel::ExpressionContextGenerator( sourceFields ) )
45 if ( role == Qt::DisplayRole )
47 switch ( orientation )
55 return tr(
"Source Expression" );
59 return tr(
"Aggregate Function" );
63 return tr(
"Delimiter" );
75 return tr(
"Length" );
79 return tr(
"Precision" );
100 if ( parent.isValid() )
102 return mMapping.count();
107 if ( parent.isValid() )
114 if ( index.isValid() )
117 const Aggregate &agg { mMapping.at( index.row() ) };
121 case Qt::DisplayRole:
132 return agg.aggregate;
136 return agg.delimiter;
140 return agg.field.displayName();
144 return agg.field.typeName();
148 return agg.field.length();
152 return agg.field.precision();
164 if ( index.isValid() )
166 return Qt::ItemFlags( Qt::ItemIsSelectable |
170 return Qt::ItemFlags();
175 if ( index.isValid() )
177 if ( role == Qt::EditRole )
205 QgsFieldMappingModel::setFieldTypeFromName( f.
field, value.toString() );
211 const int length { value.toInt( &ok ) };
219 const int precision { value.toInt( &ok ) };
225 emit dataChanged( index, index );
236bool QgsAggregateMappingModel::moveUpOrDown(
const QModelIndex &index,
bool up )
238 if ( ! index.isValid() && index.model() ==
this )
242 const int row { up ? index.row() - 1 : index.row() };
244 if ( row < 0 || row + 1 >=
rowCount( QModelIndex() ) )
248 beginMoveRows( QModelIndex( ), row, row, QModelIndex(), row + 2 );
249 mMapping.swapItemsAt( row, row + 1 );
257 if ( mExpressionContextGenerator )
258 mExpressionContextGenerator->setSourceFields( mSourceFields );
260 const QStringList usedFields;
268 aggregate.
field.
setTypeName( QgsFieldMappingModel::qgsFieldToTypeName( f ) );
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" );
278 mMapping.push_back( aggregate );
285 return mExpressionContextGenerator.get();
290 mExpressionContextGenerator->setBaseExpressionContextGenerator( generator );
302 for (
auto &agg : mMapping )
304 agg.field.setTypeName( QgsFieldMappingModel::qgsFieldToTypeName( agg.field ) );
311 const int lastRow {
rowCount( QModelIndex( ) ) };
312 beginInsertRows( QModelIndex(), lastRow, lastRow );
319 mMapping.push_back( agg );
325 if ( index.isValid() && index.model() ==
this && index.row() <
rowCount( QModelIndex() ) )
327 beginRemoveRows( QModelIndex(), index.row(), index.row() );
328 mMapping.removeAt( index.row() );
340 return moveUpOrDown( index );
345 return moveUpOrDown( index,
false );
357 QVBoxLayout *verticalLayout =
new QVBoxLayout();
358 verticalLayout->setContentsMargins( 0, 0, 0, 0 );
359 mTableView =
new QTableView();
360 verticalLayout->addWidget( mTableView );
361 setLayout( verticalLayout );
364 mTableView->setModel( mModel );
370 connect( mModel, &QgsAggregateMappingModel::rowsInserted,
this, [ = ] { updateColumns(); } );
371 connect( mModel, &QgsAggregateMappingModel::modelReset,
this, [ = ] { updateColumns(); } );
380 return qobject_cast<QgsAggregateMappingModel *>( mModel );
395 return mTableView->selectionModel();
405 mSourceLayer = layer;
415 mTableView->scrollTo( index );
430 if ( ! mTableView->selectionModel()->hasSelection() )
433 std::list<int> rowsToRemove { selectedRows() };
434 rowsToRemove.reverse();
435 for (
const int row : rowsToRemove )
437 if ( !
model()->removeField(
model()->index( row, 0, QModelIndex() ) ) )
447 if ( ! mTableView->selectionModel()->hasSelection() )
450 const std::list<int> rowsToMoveUp { selectedRows() };
451 for (
const int row : rowsToMoveUp )
453 if ( !
model()->moveUp(
model()->index( row, 0, QModelIndex() ) ) )
463 if ( ! mTableView->selectionModel()->hasSelection() )
466 std::list<int> rowsToMoveDown { selectedRows() };
467 rowsToMoveDown.reverse();
468 for (
const int row : rowsToMoveDown )
470 if ( !
model()->moveDown(
model()->index( row, 0, QModelIndex() ) ) )
478void QgsAggregateMappingWidget::updateColumns()
480 for (
int i = 0; i < mModel->rowCount(); ++i )
487 for (
int i = 0; i < mModel->columnCount(); ++i )
489 mTableView->resizeColumnToContents( i );
493std::list<int> QgsAggregateMappingWidget::selectedRows()
496 if ( mTableView->selectionModel()->hasSelection() )
498 const QModelIndexList constSelection { mTableView->selectionModel()->selectedIndexes() };
499 for (
const QModelIndex &index : constSelection )
501 rows.push_back( index.row() );
516QgsAggregateMappingDelegate::QgsAggregateMappingDelegate( QObject *parent )
517 : QStyledItemDelegate( parent )
521QWidget *QgsAggregateMappingDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex & )
const
524 QComboBox *editor =
new QComboBox( parent );
526 const QStringList aggregateList { aggregates() };
528 for (
const QString &aggregate : aggregateList )
530 editor->addItem( aggregate );
531 editor->setItemData( i, aggregate, Qt::UserRole );
536 qOverload<int >( &QComboBox::currentIndexChanged ),
538 [ = ](
int currentIndex )
540 Q_UNUSED( currentIndex )
541 const_cast< QgsAggregateMappingDelegate *
>( this )->emit commitData( editor );
547void QgsAggregateMappingDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
549 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
550 if ( ! editorWidget )
553 const QVariant value = index.model()->data( index, Qt::EditRole );
554 editorWidget->setCurrentIndex( editorWidget->findData( value ) );
557void QgsAggregateMappingDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
559 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
560 if ( ! editorWidget )
563 const QVariant currentValue = editorWidget->currentData( );
564 model->setData( index, currentValue, Qt::EditRole );
567const QStringList QgsAggregateMappingDelegate::aggregates()
569 static QStringList sAggregates;
570 static std::once_flag initialized;
571 std::call_once( initialized, [ = ]( )
573 sAggregates << QStringLiteral(
"first_value" )
574 << QStringLiteral(
"last_value" );
579 if ( !function || function->isDeprecated() || function->name().isEmpty() || function->name().at( 0 ) ==
'_' )
582 if ( function->groups().contains( QLatin1String(
"Aggregates" ) ) )
584 if ( function->name() == QLatin1String(
"aggregate" )
585 || function->name() == QLatin1String(
"relation_aggregate" ) )
588 sAggregates.append( function->name() );
591 std::sort( sAggregates.begin(), sAggregates.end() );
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.
@ Aggregate
Aggregate name.
@ SourceExpression
Expression.
@ 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.
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.
void setPrecision(int precision)
Set the field precision.
void setName(const QString &name)
Set the field name.
void setLength(int len)
Set the field length.
void setTypeName(const QString &typeName)
Set the field type.
Container of fields for a vector layer.
Represents a vector layer which manages a vector based data sets.
The Aggregate struct holds information about an aggregate column.
QString source
The source expression used as the input for the aggregate calculation.
QString delimiter
Delimiter string.
QString aggregate
Aggregate name.
QgsField field
The field in its current status (it might have been renamed)