18#include "moc_qgsfieldmappingwidget.cpp"
24#include "QItemSelectionModel"
29#ifdef ENABLE_MODELTEST
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 )
151 for (
int i = 0; i < mTableView->model()->rowCount(); ++i )
153 for (
int j = 0; j < mTableView->model()->columnCount(); j++ )
155 QModelIndex index = mTableView->model()->index( i, j );
156 mTableView->selectionModel()->select( index, QItemSelectionModel::Toggle );
163 if ( !mTableView->selectionModel()->hasSelection() )
166 const std::list<int> rowsToMoveUp { selectedRows() };
167 for (
const int row : rowsToMoveUp )
179 if ( !mTableView->selectionModel()->hasSelection() )
182 std::list<int> rowsToMoveDown { selectedRows() };
183 rowsToMoveDown.reverse();
184 for (
const int row : rowsToMoveDown )
194void QgsFieldMappingWidget::updateColumns()
196 for (
int i = 0; i < mModel->rowCount(); ++i )
202 for (
int i = 0; i < mModel->columnCount(); ++i )
204 mTableView->resizeColumnToContents( i );
208std::list<int> QgsFieldMappingWidget::selectedRows()
211 if ( mTableView->selectionModel()->hasSelection() )
213 const QModelIndexList constSelection { mTableView->selectionModel()->selectedIndexes() };
214 for (
const QModelIndex &index : constSelection )
216 rows.push_back( index.row() );
230QgsFieldMappingExpressionDelegate::QgsFieldMappingExpressionDelegate( QObject *parent )
231 : QStyledItemDelegate( parent )
235void QgsFieldMappingExpressionDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
243 const QString currentValue { editorWidget->
currentField( &isExpression, &isValid ) };
246 model->setData( index, currentValue, Qt::EditRole );
254void QgsFieldMappingExpressionDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
260 const QVariant value = index.model()->data( index, Qt::EditRole );
261 editorWidget->
setField( value.toString() );
264QWidget *QgsFieldMappingExpressionDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
268 editor->setAutoFillBackground(
true );
270 if (
const QgsFieldMappingModel *model = qobject_cast<const QgsFieldMappingModel *>( index.model() ) )
273 editor->
setFields( model->sourceFields() );
278 editor->
setFields( model->sourceFields() );
285 if (
QgsFieldMappingWidget *mappingWidget = qobject_cast<QgsFieldMappingWidget *>( QgsFieldMappingExpressionDelegate::parent() ) )
287 if ( mappingWidget->sourceLayer() )
288 editor->
setLayer( mappingWidget->sourceLayer() );
290 else if (
QgsAggregateMappingWidget *aggregateWidget = qobject_cast<QgsAggregateMappingWidget *>( QgsFieldMappingExpressionDelegate::parent() ) )
292 if ( aggregateWidget->sourceLayer() )
293 editor->
setLayer( aggregateWidget->sourceLayer() );
296 editor->
setField( index.model()->data( index, Qt::DisplayRole ).toString() );
298 Q_UNUSED( fieldName )
299 const_cast<QgsFieldMappingExpressionDelegate *
>( this )->emit commitData( editor );
309QgsFieldMappingTypeDelegate::QgsFieldMappingTypeDelegate( QObject *parent )
310 : QStyledItemDelegate( parent )
314QWidget *QgsFieldMappingTypeDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
317 QComboBox *editor =
new QComboBox( parent );
320 for (
int i = 0; i < typeList.size(); i++ )
322 editor->addItem(
QgsFields::iconForFieldType( typeList[i].mType, typeList[i].mSubType, typeList[i].mTypeName ), typeList[i].mTypeDesc );
323 editor->setItemData( i, typeList[i].mTypeName, Qt::UserRole );
326 const QgsFieldMappingModel *model { qobject_cast<const QgsFieldMappingModel *>( index.model() ) };
328 if ( model && !model->destinationEditable() )
330 editor->setEnabled(
false );
334 connect( editor, qOverload<int>( &QComboBox::currentIndexChanged ),
this, [=](
int currentIndex ) {
335 Q_UNUSED( currentIndex )
336 const_cast<QgsFieldMappingTypeDelegate *
>( this )->emit commitData( editor );
342void QgsFieldMappingTypeDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
344 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
348 const QVariant value = index.model()->
data( index, Qt::EditRole );
349 editorWidget->setCurrentIndex( editorWidget->findData( value ) );
352void QgsFieldMappingTypeDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
354 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
358 const QVariant currentValue = editorWidget->currentData();
359 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.