23#include "QItemSelectionModel"
28#ifdef ENABLE_MODELTEST
35 const QMap<QString, QString> &expressions )
38 QVBoxLayout *verticalLayout =
new QVBoxLayout();
39 verticalLayout->setContentsMargins( 0, 0, 0, 0 );
40 mTableView =
new QTableView();
41 verticalLayout->addWidget( mTableView );
42 setLayout( verticalLayout );
46#ifdef ENABLE_MODELTEST
47 new ModelTest( mModel,
this );
50 mTableView->setModel( mModel );
55 connect( mModel, &QgsFieldMappingModel::rowsInserted,
this, [ = ] { updateColumns(); } );
56 connect( mModel, &QgsFieldMappingModel::modelReset,
this, [ = ] { updateColumns(); } );
65 qobject_cast<QgsFieldMappingModel *>( mModel )->setDestinationEditable( editable );
71 return qobject_cast<QgsFieldMappingModel *>( mModel )->destinationEditable();
76 return qobject_cast<QgsFieldMappingModel *>( mModel );
96 return mTableView->selectionModel();
106 mSourceLayer = layer;
121 mTableView->scrollTo( index );
136 if ( ! mTableView->selectionModel()->hasSelection() )
139 std::list<int> rowsToRemove { selectedRows() };
140 rowsToRemove.reverse();
141 for (
const int row : rowsToRemove )
153 for (
int i = 0; i < mTableView->model()->rowCount(); ++i )
155 for (
int j = 0; j < mTableView->model()->columnCount(); j++ )
157 QModelIndex index = mTableView->model()->index( i, j );
158 mTableView->selectionModel()->select( index, QItemSelectionModel::Toggle );
165 if ( ! mTableView->selectionModel()->hasSelection() )
168 const std::list<int> rowsToMoveUp { selectedRows() };
169 for (
const int row : rowsToMoveUp )
181 if ( ! mTableView->selectionModel()->hasSelection() )
184 std::list<int> rowsToMoveDown { selectedRows() };
185 rowsToMoveDown.reverse();
186 for (
const int row : rowsToMoveDown )
196void QgsFieldMappingWidget::updateColumns()
198 for (
int i = 0; i < mModel->rowCount(); ++i )
204 for (
int i = 0; i < mModel->columnCount(); ++i )
206 mTableView->resizeColumnToContents( i );
210std::list<int> QgsFieldMappingWidget::selectedRows()
213 if ( mTableView->selectionModel()->hasSelection() )
215 const QModelIndexList constSelection { mTableView->selectionModel()->selectedIndexes() };
216 for (
const QModelIndex &index : constSelection )
218 rows.push_back( index.row() );
232QgsFieldMappingExpressionDelegate::QgsFieldMappingExpressionDelegate( QObject *parent )
233 : QStyledItemDelegate( parent )
237void QgsFieldMappingExpressionDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
240 if ( ! editorWidget )
245 const QString currentValue { editorWidget->
currentField( &isExpression, &isValid ) };
248 model->setData( index, currentValue, Qt::EditRole );
256void QgsFieldMappingExpressionDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
259 if ( ! editorWidget )
262 const QVariant value = index.model()->data( index, Qt::EditRole );
263 editorWidget->
setField( value.toString() );
266QWidget *QgsFieldMappingExpressionDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
270 editor->setAutoFillBackground(
true );
272 if (
const QgsFieldMappingModel *model = qobject_cast<const QgsFieldMappingModel *>( index.model() ) )
275 editor->
setFields( model->sourceFields() );
280 editor->
setFields( model->sourceFields() );
287 if (
QgsFieldMappingWidget *mappingWidget = qobject_cast< QgsFieldMappingWidget *>( QgsFieldMappingExpressionDelegate::parent() ) )
289 if ( mappingWidget->sourceLayer() )
290 editor->
setLayer( mappingWidget->sourceLayer() );
292 else if (
QgsAggregateMappingWidget *aggregateWidget = qobject_cast< QgsAggregateMappingWidget *>( QgsFieldMappingExpressionDelegate::parent() ) )
294 if ( aggregateWidget->sourceLayer() )
295 editor->
setLayer( aggregateWidget->sourceLayer() );
298 editor->
setField( index.model()->data( index, Qt::DisplayRole ).toString() );
302 [ = ](
const QString & fieldName )
304 Q_UNUSED( fieldName )
305 const_cast< QgsFieldMappingExpressionDelegate *
>( this )->emit commitData( editor );
315QgsFieldMappingTypeDelegate::QgsFieldMappingTypeDelegate( QObject *parent )
316 : QStyledItemDelegate( parent )
320QWidget *QgsFieldMappingTypeDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
323 QComboBox *editor =
new QComboBox( parent );
326 for (
int i = 0; i < typeList.size(); i++ )
328 editor->addItem(
QgsFields::iconForFieldType( typeList[i].mType, typeList[i].mSubType, typeList[i].mTypeName ), typeList[i].mTypeDesc );
329 editor->setItemData( i, typeList[i].mTypeName, Qt::UserRole );
332 const QgsFieldMappingModel *model { qobject_cast<const QgsFieldMappingModel *>( index.model() ) };
334 if ( model && !model->destinationEditable() )
336 editor->setEnabled(
false );
341 qOverload<int >( &QComboBox::currentIndexChanged ),
343 [ = ](
int currentIndex )
345 Q_UNUSED( currentIndex )
346 const_cast< QgsFieldMappingTypeDelegate *
>( this )->emit commitData( editor );
352void QgsFieldMappingTypeDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
354 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
355 if ( ! editorWidget )
358 const QVariant value = index.model()->
data( index, Qt::EditRole );
359 editorWidget->setCurrentIndex( editorWidget->findData( value ) );
362void QgsFieldMappingTypeDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
364 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
365 if ( ! editorWidget )
368 const QVariant currentValue = editorWidget->currentData( );
369 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.
bool removeField(const QModelIndex &index)
Removes the field at index from the model, returns true on success.
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.
bool moveDown(const QModelIndex &index)
Moves up the field at index.
bool moveUp(const QModelIndex &index)
Moves down the field at index.
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(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType, const QString &typeString=QString())
Returns an icon corresponding to a field type.
Represents a vector layer which manages a vector based data sets.