23 #include <QVBoxLayout>
25 #ifdef ENABLE_MODELTEST
26 #include "modeltest.h"
32 const QMap<QString, QString> &expressions )
35 QVBoxLayout *verticalLayout =
new QVBoxLayout();
36 verticalLayout->setContentsMargins( 0, 0, 0, 0 );
37 mTableView =
new QTableView();
38 verticalLayout->addWidget( mTableView );
39 setLayout( verticalLayout );
43 #ifdef ENABLE_MODELTEST
44 new ModelTest( mModel,
this );
47 mTableView->setModel( mModel );
52 connect( mModel, &QgsFieldMappingModel::rowsInserted,
this, [ = ] { updateColumns(); } );
53 connect( mModel, &QgsFieldMappingModel::modelReset,
this, [ = ] { updateColumns(); } );
62 qobject_cast<QgsFieldMappingModel *>( mModel )->setDestinationEditable( editable );
68 return qobject_cast<QgsFieldMappingModel *>( mModel )->destinationEditable();
73 return qobject_cast<QgsFieldMappingModel *>( mModel );
93 return mTableView->selectionModel();
108 mTableView->scrollTo( index );
123 if ( ! mTableView->selectionModel()->hasSelection() )
126 std::list<int> rowsToRemove { selectedRows() };
127 rowsToRemove.reverse();
128 for (
int row : rowsToRemove )
140 if ( ! mTableView->selectionModel()->hasSelection() )
143 const std::list<int> rowsToMoveUp { selectedRows() };
144 for (
int row : rowsToMoveUp )
156 if ( ! mTableView->selectionModel()->hasSelection() )
159 std::list<int> rowsToMoveDown { selectedRows() };
160 rowsToMoveDown.reverse();
161 for (
int row : rowsToMoveDown )
171 void QgsFieldMappingWidget::updateColumns()
173 for (
int i = 0; i < mModel->rowCount(); ++i )
179 for (
int i = 0; i < mModel->columnCount(); ++i )
181 mTableView->resizeColumnToContents( i );
185 std::list<int> QgsFieldMappingWidget::selectedRows()
188 if ( mTableView->selectionModel()->hasSelection() )
190 const QModelIndexList constSelection { mTableView->selectionModel()->selectedIndexes() };
191 for (
const QModelIndex &index : constSelection )
193 rows.push_back( index.row() );
205 QgsFieldMappingWidget::ExpressionDelegate::ExpressionDelegate( QObject *parent )
206 : QStyledItemDelegate( parent )
210 void QgsFieldMappingWidget::ExpressionDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
213 if ( ! editorWidget )
218 const QString currentValue { editorWidget->
currentField( &isExpression, &isValid ) };
221 model->setData( index, currentValue, Qt::EditRole );
229 void QgsFieldMappingWidget::ExpressionDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
232 if ( ! editorWidget )
235 const QVariant value = index.model()->data( index, Qt::EditRole );
236 editorWidget->
setField( value.toString() );
239 QWidget *QgsFieldMappingWidget::ExpressionDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
243 editor->setAutoFillBackground(
true );
246 if (
const QgsFieldMappingModel *model = qobject_cast<const QgsFieldMappingModel *>( index.model() ) )
249 editor->
setFields( model->sourceFields() );
254 editor->
setFields( model->sourceFields() );
260 editor->
setField( index.model()->data( index, Qt::DisplayRole ).toString() );
264 [ = ](
const QString & fieldName,
bool isValid )
266 Q_UNUSED( fieldName )
268 const_cast< QgsFieldMappingWidget::ExpressionDelegate *
>( this )->emit commitData( editor );
278 QgsFieldMappingWidget::TypeDelegate::TypeDelegate( QObject *parent )
279 : QStyledItemDelegate( parent )
283 QWidget *QgsFieldMappingWidget::TypeDelegate::createEditor( QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index )
const
286 QComboBox *editor =
new QComboBox( parent );
290 for (
auto it = typeList.constBegin(); it != typeList.constEnd(); ++it )
292 editor->addItem( typeList[ it.key() ] );
293 editor->setItemData( i,
static_cast<int>( it.key() ), Qt::UserRole );
297 const QgsFieldMappingModel *model { qobject_cast<const QgsFieldMappingModel *>( index.model() ) };
299 if ( model && !model->destinationEditable() )
301 editor->setEnabled(
false );
306 qgis::overload<int >::of( &QComboBox::currentIndexChanged ),
308 [ = ](
int currentIndex )
310 Q_UNUSED( currentIndex )
311 const_cast< QgsFieldMappingWidget::TypeDelegate *
>( this )->emit commitData( editor );
317 void QgsFieldMappingWidget::TypeDelegate::setEditorData( QWidget *editor,
const QModelIndex &index )
const
319 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
320 if ( ! editorWidget )
323 const QVariant value { index.model()->
data( index, Qt::EditRole ) };
324 editorWidget->setCurrentIndex( editorWidget->findData( value ) );
327 void QgsFieldMappingWidget::TypeDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index )
const
329 QComboBox *editorWidget { qobject_cast<QComboBox *>( editor ) };
330 if ( ! editorWidget )
333 const QVariant currentValue { editorWidget->currentData( ) };
334 model->setData( index, currentValue, Qt::EditRole );