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 (
int row : rowsToRemove )
151 if ( ! mTableView->selectionModel()->hasSelection() )
154 const std::list<int> rowsToMoveUp { selectedRows() };
155 for (
int row : rowsToMoveUp )
167 if ( ! mTableView->selectionModel()->hasSelection() )
170 std::list<int> rowsToMoveDown { selectedRows() };
171 rowsToMoveDown.reverse();
172 for (
int row : rowsToMoveDown )
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 );
257 if (
const QgsFieldMappingModel *model = qobject_cast<const QgsFieldMappingModel *>( index.model() ) )
260 editor->
setFields( model->sourceFields() );
265 editor->
setFields( model->sourceFields() );
272 if (
QgsFieldMappingWidget *mappingWidget = qobject_cast< QgsFieldMappingWidget *>( ExpressionDelegate::parent() ) )
274 if ( mappingWidget->sourceLayer() )
275 editor->
setLayer( mappingWidget->sourceLayer() );
277 else if (
QgsAggregateMappingWidget *aggregateWidget = qobject_cast< QgsAggregateMappingWidget *>( ExpressionDelegate::parent() ) )
279 if ( aggregateWidget->sourceLayer() )
280 editor->
setLayer( aggregateWidget->sourceLayer() );
283 editor->
setField( index.model()->data( index, Qt::DisplayRole ).toString() );
287 [ = ](
const QString & fieldName,
bool isValid )
289 Q_UNUSED( fieldName )
291 const_cast< QgsFieldMappingWidget::ExpressionDelegate *
>( this )->emit commitData( editor );
301 QgsFieldMappingWidget::TypeDelegate::TypeDelegate( QObject *parent )
302 : QStyledItemDelegate( parent )
306 QWidget *QgsFieldMappingWidget::TypeDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
309 QComboBox *editor =
new QComboBox( parent );
313 for (
auto it = typeList.constBegin(); it != typeList.constEnd(); ++it )
315 editor->addItem( typeList[ it.key() ] );
316 editor->setItemData( i,
static_cast<int>( it.key() ), Qt::UserRole );
320 const QgsFieldMappingModel *model { qobject_cast<const QgsFieldMappingModel *>( index.model() ) };
322 if ( model && !model->destinationEditable() )
324 editor->setEnabled(
false );
329 qgis::overload<int >::of( &QComboBox::currentIndexChanged ),
331 [ = ](
int currentIndex )
333 Q_UNUSED( currentIndex )
334 const_cast< QgsFieldMappingWidget::TypeDelegate *
>( this )->emit commitData( editor );
340 void QgsFieldMappingWidget::TypeDelegate::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 ) );
350 void QgsFieldMappingWidget::TypeDelegate::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 );