Navigation Menu

Skip to content

Commit

Permalink
[dxf] Fix allowing attribute selection for DXF layer name
Browse files Browse the repository at this point in the history
Fixes #42575
  • Loading branch information
nyalldawson committed Jun 3, 2021
1 parent dc9502c commit 66c6ef9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/app/qgsdxfexportdialog.cpp
Expand Up @@ -45,11 +45,7 @@ QWidget *FieldSelectorDelegate::createEditor( QWidget *parent, const QStyleOptio
{
Q_UNUSED( option )

const QgsVectorLayerAndAttributeModel *m = qobject_cast< const QgsVectorLayerAndAttributeModel *>( index.model() );
if ( !m )
return nullptr;

QgsVectorLayer *vl = m->vectorLayer( index );
QgsVectorLayer *vl = indexToLayer( index.model(), index );
if ( !vl )
return nullptr;

Expand All @@ -61,30 +57,22 @@ QWidget *FieldSelectorDelegate::createEditor( QWidget *parent, const QStyleOptio

void FieldSelectorDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
{
const QgsVectorLayerAndAttributeModel *m = qobject_cast< const QgsVectorLayerAndAttributeModel *>( index.model() );
if ( !m )
return;

QgsVectorLayer *vl = m->vectorLayer( index );
QgsVectorLayer *vl = indexToLayer( index.model(), index );
if ( !vl )
return;

QgsFieldComboBox *fcb = qobject_cast<QgsFieldComboBox *>( editor );
if ( !fcb )
return;

int idx = m->attributeIndex( vl );
int idx = attributeIndex( index.model(), vl );
if ( vl->fields().exists( idx ) )
fcb->setField( vl->fields().at( idx ).name() );
}

void FieldSelectorDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
{
QgsVectorLayerAndAttributeModel *m = qobject_cast< QgsVectorLayerAndAttributeModel *>( model );
if ( !m )
return;

QgsVectorLayer *vl = m->vectorLayer( index );
QgsVectorLayer *vl = indexToLayer( index.model(), index );
if ( !vl )
return;

Expand All @@ -95,6 +83,28 @@ void FieldSelectorDelegate::setModelData( QWidget *editor, QAbstractItemModel *m
model->setData( index, vl->fields().lookupField( fcb->currentField() ) );
}

QgsVectorLayer *FieldSelectorDelegate::indexToLayer( const QAbstractItemModel *model, const QModelIndex &index ) const
{
const QgsLayerTreeProxyModel *proxy = qobject_cast< const QgsLayerTreeProxyModel *>( model );
Q_ASSERT( proxy );

const QgsVectorLayerAndAttributeModel *m = qobject_cast< const QgsVectorLayerAndAttributeModel *>( proxy->sourceModel() );
Q_ASSERT( m );

return m->vectorLayer( proxy->mapToSource( index ) );
}

int FieldSelectorDelegate::attributeIndex( const QAbstractItemModel *model, const QgsVectorLayer *vl ) const
{
const QgsLayerTreeProxyModel *proxy = qobject_cast< const QgsLayerTreeProxyModel *>( model );
Q_ASSERT( proxy );

const QgsVectorLayerAndAttributeModel *m = qobject_cast< const QgsVectorLayerAndAttributeModel *>( proxy->sourceModel() );
Q_ASSERT( m );

return m->attributeIndex( vl );
}

QgsVectorLayerAndAttributeModel::QgsVectorLayerAndAttributeModel( QgsLayerTree *rootNode, QObject *parent )
: QgsLayerTreeModel( rootNode, parent )
{
Expand Down
3 changes: 3 additions & 0 deletions src/app/qgsdxfexportdialog.h
Expand Up @@ -39,6 +39,9 @@ class FieldSelectorDelegate : public QItemDelegate
QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
void setEditorData( QWidget *editor, const QModelIndex &index ) const override;
void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const override;
private:
QgsVectorLayer *indexToLayer( const QAbstractItemModel *model, const QModelIndex &index ) const;
int attributeIndex( const QAbstractItemModel *model, const QgsVectorLayer *vl ) const;
};

class QgsVectorLayerAndAttributeModel : public QgsLayerTreeModel
Expand Down

0 comments on commit 66c6ef9

Please sign in to comment.