Skip to content
Permalink
Browse files
[dxf] Fix allowing attribute selection for DXF layer name
Fixes #42575
  • Loading branch information
nyalldawson committed Jun 3, 2021
1 parent dc9502c commit 66c6ef96bcd4bcce4fa06cfc4ea62c87ac2898c9
Showing with 29 additions and 16 deletions.
  1. +26 −16 src/app/qgsdxfexportdialog.cpp
  2. +3 −0 src/app/qgsdxfexportdialog.h
@@ -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;

@@ -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;

@@ -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 )
{
@@ -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

0 comments on commit 66c6ef9

Please sign in to comment.