Skip to content

Commit

Permalink
[dxf] Insure that the DXF export dialog's output attributes are saved…
Browse files Browse the repository at this point in the history
…/restored properly
  • Loading branch information
nirvn committed Feb 1, 2024
1 parent d600820 commit 42a3d3e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/app/qgsdxfexportdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,83 @@ void QgsVectorLayerAndAttributeModel::applyVisibility( QSet<QString> &visibleLay
}
}

void QgsVectorLayerAndAttributeModel::applyLayersOutputAttribute( const QStringList &encodedLayersOutputAttribute )
{
QMap<QString, int> layersOutputAttribute;
for ( const QString &string : encodedLayersOutputAttribute )
{
QStringList parts = string.split( QLatin1String( "~|<|~|>|~" ) );
if ( parts.size() == 2 )
{
layersOutputAttribute[parts[0]] = parts[1].toInt();
}
}

applyLayersOutputAttribute( layersOutputAttribute, rootGroup() );
}

void QgsVectorLayerAndAttributeModel::applyLayersOutputAttribute( QMap<QString, int> &layersOutputAttribute, QgsLayerTreeNode *node )
{
QgsLayerTreeGroup *group = QgsLayerTree::isGroup( node ) ? QgsLayerTree::toGroup( node ) : nullptr;
if ( !group )
return;

const auto constChildren = node->children();
for ( QgsLayerTreeNode *child : constChildren )
{
if ( QgsLayerTree::isLayer( child ) )
{
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( QgsLayerTree::toLayer( child )->layer() );
if ( vl )
{
if ( layersOutputAttribute.contains( vl->id() ) )
{
mAttributeIdx[vl] = layersOutputAttribute[vl->id()];
qDebug() << vl->id();
qDebug() << mAttributeIdx[vl];
layersOutputAttribute.remove( vl->id() );

QModelIndex idx = node2index( child );
idx = index( idx.row(), 1, idx.parent() );
emit dataChanged( idx, idx, QVector<int>() << Qt::EditRole );
}
}
continue;
}

applyLayersOutputAttribute( layersOutputAttribute, child );
}
}

QStringList QgsVectorLayerAndAttributeModel::encodeLayersOutputAttribute()
{
QMap<QString, int> layersOutputAttribute;
retrieveAllLayersOutputAttribute( rootGroup(), layersOutputAttribute );
QStringList encodedLayersOutputAttribute;
for ( const QString &key : layersOutputAttribute.keys() )
{
encodedLayersOutputAttribute << QStringLiteral( "%1%2%3" ).arg( key, QLatin1String( "~|<|~|>|~" ), QString::number( layersOutputAttribute[key] ) );
}
return encodedLayersOutputAttribute;
}

void QgsVectorLayerAndAttributeModel::retrieveAllLayersOutputAttribute( QgsLayerTreeNode *node, QMap<QString, int> &layersOutputAttribute )
{
if ( QgsLayerTree::isLayer( node ) )
{
QModelIndex idx = node2index( node );
layersOutputAttribute[QgsLayerTree::toLayer( node )->layer()->id()] = data( index( idx.row(), 1, idx.parent() ), Qt::EditRole ).toInt();
}
else if ( QgsLayerTree::isGroup( node ) )
{
const auto constChildren = QgsLayerTree::toGroup( node )->children();
for ( QgsLayerTreeNode *child : constChildren )
{
retrieveAllLayersOutputAttribute( child, layersOutputAttribute );
}
}
}

void QgsVectorLayerAndAttributeModel::retrieveAllLayers( QgsLayerTreeNode *node, QSet<QString> &set )
{
if ( QgsLayerTree::isLayer( node ) )
Expand Down Expand Up @@ -529,6 +606,8 @@ QgsDxfExportDialog::QgsDxfExportDialog( QWidget *parent, Qt::WindowFlags f )

mEncoding->addItems( QgsDxfExport::encodings() );
mEncoding->setCurrentIndex( mEncoding->findText( QgsProject::instance()->readEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfEncoding" ), settings.value( QStringLiteral( "qgis/lastDxfEncoding" ), "CP1252" ).toString() ) ) );

mModel->applyLayersOutputAttribute( QgsProject::instance()->readListEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastLayersOutputAttribute" ) ) );
}


Expand Down Expand Up @@ -664,6 +743,7 @@ void QgsDxfExportDialog::saveSettings()
settings.setValue( QStringLiteral( "qgis/lastDxfUseMText" ), mMTextCheckBox->isChecked() );
settings.setValue( QStringLiteral( "qgis/lastDxfForce2d" ), mForce2d->isChecked() );

QgsProject::instance()->writeEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastLayersOutputAttribute" ), mModel->encodeLayersOutputAttribute() );
QgsProject::instance()->writeEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfSymbologyMode" ), mSymbologyModeComboBox->currentIndex() );
QgsProject::instance()->writeEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastSymbologyExportScale" ), mScaleWidget->scale() != 0 ? 1.0 / mScaleWidget->scale() : 0 );
QgsProject::instance()->writeEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfLayerTitleAsName" ), mLayerTitleAsName->isChecked() );
Expand Down
5 changes: 5 additions & 0 deletions src/app/qgsdxfexportdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class QgsVectorLayerAndAttributeModel : public QgsLayerTreeModel

QList< QgsDxfExport::DxfLayer > layers() const;

QStringList encodeLayersOutputAttribute();
void applyLayersOutputAttribute( const QStringList &encodedLayersOutputAttribute );

QgsVectorLayer *vectorLayer( const QModelIndex &index ) const;
int attributeIndex( const QgsVectorLayer *vl ) const;

Expand All @@ -71,7 +74,9 @@ class QgsVectorLayerAndAttributeModel : public QgsLayerTreeModel
QSet<QModelIndex> mCheckedLeafs;

void applyVisibility( QSet<QString> &visibleLayers, QgsLayerTreeNode *node );
void applyLayersOutputAttribute( QMap<QString, int> &layersOutputAttribute, QgsLayerTreeNode *node );
void retrieveAllLayers( QgsLayerTreeNode *node, QSet<QString> &layers );
void retrieveAllLayersOutputAttribute( QgsLayerTreeNode *node, QMap<QString, int> &layersOutputAttribute );
};

class QgsDxfExportLayerTreeView : public QgsLayerTreeView
Expand Down

0 comments on commit 42a3d3e

Please sign in to comment.