24 #include <QVBoxLayout>
26 #ifdef ENABLE_MODELTEST
27 #include "modeltest.h"
33 const QMap<QString, QString> &expressions )
36 QVBoxLayout *verticalLayout =
new QVBoxLayout();
37 verticalLayout->setContentsMargins( 0, 0, 0, 0 );
38 mTableView =
new QTableView();
39 verticalLayout->addWidget( mTableView );
40 setLayout( verticalLayout );
44 #ifdef ENABLE_MODELTEST
45 new ModelTest( mModel,
this );
48 mTableView->setModel( mModel );
53 connect( mModel, &QgsFieldMappingModel::rowsInserted,
this, [ = ] { updateColumns(); } );
54 connect( mModel, &QgsFieldMappingModel::modelReset,
this, [ = ] { updateColumns(); } );
63 qobject_cast<QgsFieldMappingModel *>( mModel )->setDestinationEditable( editable );
69 return qobject_cast<QgsFieldMappingModel *>( mModel )->destinationEditable();
74 return qobject_cast<QgsFieldMappingModel *>( mModel );
94 return mTableView->selectionModel();
104 mSourceLayer = layer;
119 mTableView->scrollTo( index );
134 if ( ! mTableView->selectionModel()->hasSelection() )
137 std::list<int> rowsToRemove { selectedRows() };
138 rowsToRemove.reverse();
139 for (
const int row : rowsToRemove )
141 if ( !
model()->removeField(
model()->index( row, 0, QModelIndex() ) ) )
151 if ( ! mTableView->selectionModel()->hasSelection() )
154 const std::list<int> rowsToMoveUp { selectedRows() };
155 for (
const int row : rowsToMoveUp )
157 if ( !
model()->moveUp(
model()->index( row, 0, QModelIndex() ) ) )
167 if ( ! mTableView->selectionModel()->hasSelection() )
170 std::list<int> rowsToMoveDown { selectedRows() };
171 rowsToMoveDown.reverse();
172 for (
const int row : rowsToMoveDown )
174 if ( !
model()->moveDown(
model()->index( row, 0, QModelIndex() ) ) )
182 void QgsFieldMappingWidget::updateColumns()
184 for (
int i = 0; i < mModel->rowCount(); ++i )
190 for (
int i = 0; i < mModel->columnCount(); ++i )
192 mTableView->resizeColumnToContents( i );
196 std::list<int> QgsFieldMappingWidget::selectedRows()
199 if ( mTableView->selectionModel()->hasSelection() )
201 const QModelIndexList constSelection { mTableView->selectionModel()->selectedIndexes() };
202 for (
const QModelIndex &index : constSelection )
204 rows.push_back( index.row() );
216 QgsFieldMappingWidget::ExpressionDelegate::ExpressionDelegate( QObject *parent )
217 : QStyledItemDelegate( parent )
221 void QgsFieldMappingWidget::ExpressionDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
224 if ( ! editorWidget )
229 const QString currentValue { editorWidget->
currentField( &isExpression, &isValid ) };
232 model->setData( index, currentValue, Qt::EditRole );
240 void QgsFieldMappingWidget::ExpressionDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
243 if ( ! editorWidget )
246 const QVariant value = index.model()->data( index, Qt::EditRole );
247 editorWidget->
setField( value.toString() );
250 QWidget *QgsFieldMappingWidget::ExpressionDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
254 editor->setAutoFillBackground(
true );
256 if (
const QgsFieldMappingModel *model = qobject_cast<const QgsFieldMappingModel *>( index.model() ) )
259 editor->
setFields( model->sourceFields() );
264 editor->
setFields( model->sourceFields() );
271 if (
QgsFieldMappingWidget *mappingWidget = qobject_cast< QgsFieldMappingWidget *>( ExpressionDelegate::parent() ) )
273 if ( mappingWidget->sourceLayer() )
274 editor->
setLayer( mappingWidget->sourceLayer() );
276 else if (
QgsAggregateMappingWidget *aggregateWidget = qobject_cast< QgsAggregateMappingWidget *>( ExpressionDelegate::parent() ) )
278 if ( aggregateWidget->sourceLayer() )
279 editor->
setLayer( aggregateWidget->sourceLayer() );
282 editor->
setField( index.model()->data( index, Qt::DisplayRole ).toString() );
286 [ = ](
const QString & fieldName,
bool isValid )
288 Q_UNUSED( fieldName )
290 const_cast< QgsFieldMappingWidget::ExpressionDelegate *
>( this )->emit commitData( editor );
300 QgsFieldMappingWidget::TypeDelegate::TypeDelegate( QObject *parent )
301 : QStyledItemDelegate( parent )
305 QWidget *QgsFieldMappingWidget::TypeDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
308 QComboBox *editor =
new QComboBox( parent );
312 for (
auto it = typeList.constBegin(); it != typeList.constEnd(); ++it )
314 editor->addItem( typeList[ it.key() ] );
315 editor->setItemData( i,
static_cast<int>( it.key() ), Qt::UserRole );
319 const QgsFieldMappingModel *model { qobject_cast<const QgsFieldMappingModel *>( index.model() ) };
321 if ( model && !model->destinationEditable() )
323 editor->setEnabled(
false );
328 qOverload<int >( &QComboBox::currentIndexChanged ),
330 [ = ](
int currentIndex )
332 Q_UNUSED( currentIndex )
333 const_cast< QgsFieldMappingWidget::TypeDelegate *
>( this )->emit commitData( editor );
339 void QgsFieldMappingWidget::TypeDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
341 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
342 if ( ! editorWidget )
345 const QVariant value = index.model()->
data( index, Qt::EditRole );
346 editorWidget->setCurrentIndex( editorWidget->findData( value ) );
349 void QgsFieldMappingWidget::TypeDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
351 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
352 if ( ! editorWidget )
355 const QVariant currentValue = editorWidget->currentData( );
356 model->setData( index, currentValue, Qt::EditRole );
The QgsAggregateMappingModel holds mapping information for defining sets of aggregates of fields from...
Abstract interface for generating an expression context.
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,...
@ SourceExpression
Expression.
@ DestinationType
Destination field QVariant::Type casted to (int)
static const QMap< QVariant::Type, QString > dataTypes()
Returns a static map of supported data types.
void appendField(const QgsField &field, const QString &expression=QString())
Appends a new field to the model, with an optional expression.
QMap< QString, QgsProperty > fieldPropertyMap() const
Returns a map of destination field name to QgsProperty definition for field value,...
QList< QgsFieldMappingModel::Field > mapping() const
Returns a list of Field objects representing the current status of the model.
QVariant data(const QModelIndex &index, int role) const override
void setBaseExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Sets the base expression context generator, which will generate the expression contexts for expressio...
void setDestinationFields(const QgsFields &destinationFields, const QMap< QString, QString > &expressions=QMap< QString, QString >())
Set destination fields to destinationFields, initial values for the expressions can be optionally spe...
void setSourceFields(const QgsFields &sourceFields)
Set source fields to sourceFields.
void setFieldPropertyMap(const QMap< QString, QgsProperty > &map)
Sets a map of destination field name to QgsProperty definition for field value.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
Represents a vector layer which manages a vector based data sets.