25 #include <QVBoxLayout>
27 #ifdef ENABLE_MODELTEST
28 #include "modeltest.h"
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 )
152 if ( ! mTableView->selectionModel()->hasSelection() )
155 const std::list<int> rowsToMoveUp { selectedRows() };
156 for (
const int row : rowsToMoveUp )
168 if ( ! mTableView->selectionModel()->hasSelection() )
171 std::list<int> rowsToMoveDown { selectedRows() };
172 rowsToMoveDown.reverse();
173 for (
const int row : rowsToMoveDown )
183 void QgsFieldMappingWidget::updateColumns()
185 for (
int i = 0; i < mModel->rowCount(); ++i )
191 for (
int i = 0; i < mModel->columnCount(); ++i )
193 mTableView->resizeColumnToContents( i );
197 std::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() );
217 QgsFieldMappingWidget::ExpressionDelegate::ExpressionDelegate( QObject *parent )
218 : QStyledItemDelegate( parent )
222 void QgsFieldMappingWidget::ExpressionDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
225 if ( ! editorWidget )
230 const QString currentValue { editorWidget->
currentField( &isExpression, &isValid ) };
233 model->setData( index, currentValue, Qt::EditRole );
241 void QgsFieldMappingWidget::ExpressionDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
244 if ( ! editorWidget )
247 const QVariant value = index.model()->data( index, Qt::EditRole );
248 editorWidget->
setField( value.toString() );
251 QWidget *QgsFieldMappingWidget::ExpressionDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
255 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 );
312 for (
int i = 0; i < typeList.size(); i++ )
315 editor->setItemData( i, typeList[i].mTypeName, Qt::UserRole );
318 const QgsFieldMappingModel *model { qobject_cast<const QgsFieldMappingModel *>( index.model() ) };
320 if ( model && !model->destinationEditable() )
322 editor->setEnabled(
false );
327 qOverload<int >( &QComboBox::currentIndexChanged ),
329 [ = ](
int currentIndex )
331 Q_UNUSED( currentIndex )
332 const_cast< QgsFieldMappingWidget::TypeDelegate *
>( this )->emit commitData( editor );
338 void QgsFieldMappingWidget::TypeDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
340 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
341 if ( ! editorWidget )
344 const QVariant value = index.model()->
data( index, Qt::EditRole );
345 editorWidget->setCurrentIndex( editorWidget->findData( value ) );
348 void QgsFieldMappingWidget::TypeDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
350 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
351 if ( ! editorWidget )
354 const QVariant currentValue = editorWidget->currentData( );
355 model->setData( index, currentValue, Qt::EditRole );