27#ifdef ENABLE_MODELTEST
34 const QMap<QString, QString> &expressions )
37 QVBoxLayout *verticalLayout =
new QVBoxLayout();
38 verticalLayout->setContentsMargins( 0, 0, 0, 0 );
39 mTableView =
new QTableView();
40 verticalLayout->addWidget( mTableView );
41 setLayout( verticalLayout );
45#ifdef ENABLE_MODELTEST
46 new ModelTest( mModel,
this );
49 mTableView->setModel( mModel );
54 connect( mModel, &QgsFieldMappingModel::rowsInserted,
this, [ = ] { updateColumns(); } );
55 connect( mModel, &QgsFieldMappingModel::modelReset,
this, [ = ] { updateColumns(); } );
64 qobject_cast<QgsFieldMappingModel *>( mModel )->setDestinationEditable( editable );
70 return qobject_cast<QgsFieldMappingModel *>( mModel )->destinationEditable();
75 return qobject_cast<QgsFieldMappingModel *>( mModel );
95 return mTableView->selectionModel();
105 mSourceLayer = layer;
120 mTableView->scrollTo( index );
135 if ( ! mTableView->selectionModel()->hasSelection() )
138 std::list<int> rowsToRemove { selectedRows() };
139 rowsToRemove.reverse();
140 for (
const int row : rowsToRemove )
142 if ( !
model()->removeField(
model()->index( row, 0, QModelIndex() ) ) )
152 if ( ! mTableView->selectionModel()->hasSelection() )
155 const std::list<int> rowsToMoveUp { selectedRows() };
156 for (
const int row : rowsToMoveUp )
158 if ( !
model()->moveUp(
model()->index( row, 0, QModelIndex() ) ) )
168 if ( ! mTableView->selectionModel()->hasSelection() )
171 std::list<int> rowsToMoveDown { selectedRows() };
172 rowsToMoveDown.reverse();
173 for (
const int row : rowsToMoveDown )
175 if ( !
model()->moveDown(
model()->index( row, 0, QModelIndex() ) ) )
183void QgsFieldMappingWidget::updateColumns()
185 for (
int i = 0; i < mModel->rowCount(); ++i )
191 for (
int i = 0; i < mModel->columnCount(); ++i )
193 mTableView->resizeColumnToContents( i );
197std::list<int> QgsFieldMappingWidget::selectedRows()
200 if ( mTableView->selectionModel()->hasSelection() )
202 const QModelIndexList constSelection { mTableView->selectionModel()->selectedIndexes() };
203 for (
const QModelIndex &index : constSelection )
205 rows.push_back( index.row() );
219QgsFieldMappingExpressionDelegate::QgsFieldMappingExpressionDelegate( QObject *parent )
220 : QStyledItemDelegate( parent )
224void QgsFieldMappingExpressionDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
227 if ( ! editorWidget )
232 const QString currentValue { editorWidget->
currentField( &isExpression, &isValid ) };
235 model->setData( index, currentValue, Qt::EditRole );
243void QgsFieldMappingExpressionDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
246 if ( ! editorWidget )
249 const QVariant value = index.model()->data( index, Qt::EditRole );
250 editorWidget->
setField( value.toString() );
253QWidget *QgsFieldMappingExpressionDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
257 editor->setAutoFillBackground(
true );
259 if (
const QgsFieldMappingModel *model = qobject_cast<const QgsFieldMappingModel *>( index.model() ) )
262 editor->
setFields( model->sourceFields() );
267 editor->
setFields( model->sourceFields() );
274 if (
QgsFieldMappingWidget *mappingWidget = qobject_cast< QgsFieldMappingWidget *>( QgsFieldMappingExpressionDelegate::parent() ) )
276 if ( mappingWidget->sourceLayer() )
277 editor->
setLayer( mappingWidget->sourceLayer() );
279 else if (
QgsAggregateMappingWidget *aggregateWidget = qobject_cast< QgsAggregateMappingWidget *>( QgsFieldMappingExpressionDelegate::parent() ) )
281 if ( aggregateWidget->sourceLayer() )
282 editor->
setLayer( aggregateWidget->sourceLayer() );
285 editor->
setField( index.model()->data( index, Qt::DisplayRole ).toString() );
289 [ = ](
const QString & fieldName,
bool isValid )
291 Q_UNUSED( fieldName )
293 const_cast< QgsFieldMappingExpressionDelegate *
>( this )->emit commitData( editor );
303QgsFieldMappingTypeDelegate::QgsFieldMappingTypeDelegate( QObject *parent )
304 : QStyledItemDelegate( parent )
308QWidget *QgsFieldMappingTypeDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
311 QComboBox *editor =
new QComboBox( parent );
314 for (
int i = 0; i < typeList.size(); i++ )
316 editor->addItem(
QgsFields::iconForFieldType( typeList[i].mType, typeList[i].mSubType, typeList[i].mTypeName ), typeList[i].mTypeDesc );
317 editor->setItemData( i, typeList[i].mTypeName, Qt::UserRole );
320 const QgsFieldMappingModel *model { qobject_cast<const QgsFieldMappingModel *>( index.model() ) };
322 if ( model && !model->destinationEditable() )
324 editor->setEnabled(
false );
329 qOverload<int >( &QComboBox::currentIndexChanged ),
331 [ = ](
int currentIndex )
333 Q_UNUSED( currentIndex )
334 const_cast< QgsFieldMappingTypeDelegate *
>( this )->emit commitData( editor );
340void QgsFieldMappingTypeDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
342 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
343 if ( ! editorWidget )
346 const QVariant value = index.model()->
data( index, Qt::EditRole );
347 editorWidget->setCurrentIndex( editorWidget->findData( value ) );
350void QgsFieldMappingTypeDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
352 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
353 if ( ! editorWidget )
356 const QVariant currentValue = editorWidget->currentData( );
357 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 type string.
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...
static const QList< QgsVectorDataProvider::NativeType > supportedDataTypes()
Returns a static list of supported data types.
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.
static QIcon iconForFieldType(QVariant::Type type, QVariant::Type subType=QVariant::Type::Invalid, const QString &typeString=QString())
Returns an icon corresponding to a field type.
Represents a vector layer which manages a vector based data sets.