18#include "moc_qgsfieldmappingwidget.cpp"
24#include "QItemSelectionModel"
29#ifdef ENABLE_MODELTEST
36 const QMap<QString, QString> &expressions )
39 QVBoxLayout *verticalLayout =
new QVBoxLayout();
40 verticalLayout->setContentsMargins( 0, 0, 0, 0 );
41 mTableView =
new QTableView();
42 verticalLayout->addWidget( mTableView );
43 setLayout( verticalLayout );
47#ifdef ENABLE_MODELTEST
48 new ModelTest( mModel,
this );
51 mTableView->setModel( mModel );
56 connect( mModel, &QgsFieldMappingModel::rowsInserted,
this, [ = ] { updateColumns(); } );
57 connect( mModel, &QgsFieldMappingModel::modelReset,
this, [ = ] { updateColumns(); } );
66 qobject_cast<QgsFieldMappingModel *>( mModel )->setDestinationEditable( editable );
72 return qobject_cast<QgsFieldMappingModel *>( mModel )->destinationEditable();
77 return qobject_cast<QgsFieldMappingModel *>( mModel );
97 return mTableView->selectionModel();
107 mSourceLayer = layer;
122 mTableView->scrollTo( index );
137 if ( ! mTableView->selectionModel()->hasSelection() )
140 std::list<int> rowsToRemove { selectedRows() };
141 rowsToRemove.reverse();
142 for (
const int row : rowsToRemove )
154 for (
int i = 0; i < mTableView->model()->rowCount(); ++i )
156 for (
int j = 0; j < mTableView->model()->columnCount(); j++ )
158 QModelIndex index = mTableView->model()->index( i, j );
159 mTableView->selectionModel()->select( index, QItemSelectionModel::Toggle );
166 if ( ! mTableView->selectionModel()->hasSelection() )
169 const std::list<int> rowsToMoveUp { selectedRows() };
170 for (
const int row : rowsToMoveUp )
182 if ( ! mTableView->selectionModel()->hasSelection() )
185 std::list<int> rowsToMoveDown { selectedRows() };
186 rowsToMoveDown.reverse();
187 for (
const int row : rowsToMoveDown )
197void QgsFieldMappingWidget::updateColumns()
199 for (
int i = 0; i < mModel->rowCount(); ++i )
205 for (
int i = 0; i < mModel->columnCount(); ++i )
207 mTableView->resizeColumnToContents( i );
211std::list<int> QgsFieldMappingWidget::selectedRows()
214 if ( mTableView->selectionModel()->hasSelection() )
216 const QModelIndexList constSelection { mTableView->selectionModel()->selectedIndexes() };
217 for (
const QModelIndex &index : constSelection )
219 rows.push_back( index.row() );
233QgsFieldMappingExpressionDelegate::QgsFieldMappingExpressionDelegate( QObject *parent )
234 : QStyledItemDelegate( parent )
238void QgsFieldMappingExpressionDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
241 if ( ! editorWidget )
246 const QString currentValue { editorWidget->
currentField( &isExpression, &isValid ) };
249 model->setData( index, currentValue, Qt::EditRole );
257void QgsFieldMappingExpressionDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
260 if ( ! editorWidget )
263 const QVariant value = index.model()->data( index, Qt::EditRole );
264 editorWidget->
setField( value.toString() );
267QWidget *QgsFieldMappingExpressionDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
271 editor->setAutoFillBackground(
true );
273 if (
const QgsFieldMappingModel *model = qobject_cast<const QgsFieldMappingModel *>( index.model() ) )
276 editor->
setFields( model->sourceFields() );
281 editor->
setFields( model->sourceFields() );
288 if (
QgsFieldMappingWidget *mappingWidget = qobject_cast< QgsFieldMappingWidget *>( QgsFieldMappingExpressionDelegate::parent() ) )
290 if ( mappingWidget->sourceLayer() )
291 editor->
setLayer( mappingWidget->sourceLayer() );
293 else if (
QgsAggregateMappingWidget *aggregateWidget = qobject_cast< QgsAggregateMappingWidget *>( QgsFieldMappingExpressionDelegate::parent() ) )
295 if ( aggregateWidget->sourceLayer() )
296 editor->
setLayer( aggregateWidget->sourceLayer() );
299 editor->
setField( index.model()->data( index, Qt::DisplayRole ).toString() );
303 [ = ](
const QString & fieldName )
305 Q_UNUSED( fieldName )
306 const_cast< QgsFieldMappingExpressionDelegate *
>( this )->emit commitData( editor );
316QgsFieldMappingTypeDelegate::QgsFieldMappingTypeDelegate( QObject *parent )
317 : QStyledItemDelegate( parent )
321QWidget *QgsFieldMappingTypeDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
324 QComboBox *editor =
new QComboBox( parent );
327 for (
int i = 0; i < typeList.size(); i++ )
329 editor->addItem(
QgsFields::iconForFieldType( typeList[i].mType, typeList[i].mSubType, typeList[i].mTypeName ), typeList[i].mTypeDesc );
330 editor->setItemData( i, typeList[i].mTypeName, Qt::UserRole );
333 const QgsFieldMappingModel *model { qobject_cast<const QgsFieldMappingModel *>( index.model() ) };
335 if ( model && !model->destinationEditable() )
337 editor->setEnabled(
false );
342 qOverload<int >( &QComboBox::currentIndexChanged ),
344 [ = ](
int currentIndex )
346 Q_UNUSED( currentIndex )
347 const_cast< QgsFieldMappingTypeDelegate *
>( this )->emit commitData( editor );
353void QgsFieldMappingTypeDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
355 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
356 if ( ! editorWidget )
359 const QVariant value = index.model()->
data( index, Qt::EditRole );
360 editorWidget->setCurrentIndex( editorWidget->findData( value ) );
363void QgsFieldMappingTypeDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
365 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
366 if ( ! editorWidget )
369 const QVariant currentValue = editorWidget->currentData( );
370 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.