Skip to content

Commit

Permalink
[dxf] use a struct instead of QPair for layers
Browse files Browse the repository at this point in the history
there was a crash in Python in QgsDxfExport.addLayers due to a bad conversion
  • Loading branch information
3nids committed Feb 12, 2018
1 parent 9e79d8a commit 31e93ae
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 23 deletions.
14 changes: 13 additions & 1 deletion python/core/dxf/qgsdxfexport.sip.in
Expand Up @@ -20,6 +20,18 @@ class QgsDxfExport
#include "qgsdxfexport.h"
%End
public:

struct DxfLayer
{
DxfLayer( QgsVectorLayer *vl, int layerOutputAttributeIndex = -1 );

QgsVectorLayer *layer() const;
int layerOutputAttributeIndex() const;

QgsVectorLayer *mLayer;
int mLayerOutputAttributeIndex;
};

enum SymbologyExport
{
NoSymbology,
Expand Down Expand Up @@ -64,7 +76,7 @@ Returns the export flags.
.. seealso:: :py:func:`setFlags`
%End

void addLayers( const QList< QPair<QgsVectorLayer *, int > > &layers );
void addLayers( const QList< QgsDxfExport::DxfLayer > &layers );
%Docstring
Add layers to export

Expand Down
6 changes: 3 additions & 3 deletions src/app/main.cpp
Expand Up @@ -1357,15 +1357,15 @@ int main( int argc, char *argv[] )
dxfExport.setExtent( dxfExtent );

QStringList layerIds;
QList< QPair<QgsVectorLayer *, int > > layers;
QList< QgsDxfExport::DxfLayer > layers;
if ( !dxfMapTheme.isEmpty() )
{
Q_FOREACH ( QgsMapLayer *layer, QgsProject::instance()->mapThemeCollection()->mapThemeVisibleLayers( dxfMapTheme ) )
{
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
if ( !vl )
continue;
layers << qMakePair<QgsVectorLayer *, int>( vl, -1 );
layers << QgsDxfExport::DxfLayer( vl );
layerIds << vl->id();
}
}
Expand All @@ -1376,7 +1376,7 @@ int main( int argc, char *argv[] )
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( ml );
if ( !vl )
continue;
layers << qMakePair<QgsVectorLayer *, int>( vl, -1 );
layers << QgsDxfExport::DxfLayer( vl );
layerIds << vl->id();
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/app/qgsdxfexportdialog.cpp
Expand Up @@ -276,9 +276,9 @@ bool QgsVectorLayerAndAttributeModel::setData( const QModelIndex &index, const Q
}


QList< QPair<QgsVectorLayer *, int> > QgsVectorLayerAndAttributeModel::layers() const
QList< QgsDxfExport::DxfLayer > QgsVectorLayerAndAttributeModel::layers() const
{
QList< QPair<QgsVectorLayer *, int> > layers;
QList< QgsDxfExport::DxfLayer > layers;
QHash< QString, int > layerIdx;

Q_FOREACH ( const QModelIndex &idx, mCheckedLeafs )
Expand All @@ -293,7 +293,7 @@ QList< QPair<QgsVectorLayer *, int> > QgsVectorLayerAndAttributeModel::layers()
if ( !layerIdx.contains( vl->id() ) )
{
layerIdx.insert( vl->id(), layers.size() );
layers << qMakePair<QgsVectorLayer *, int>( vl, mAttributeIdx.value( vl, -1 ) );
layers << QgsDxfExport::DxfLayer( vl, mAttributeIdx.value( vl, -1 ) );
}
}
}
Expand All @@ -304,12 +304,12 @@ QList< QPair<QgsVectorLayer *, int> > QgsVectorLayerAndAttributeModel::layers()
if ( !layerIdx.contains( vl->id() ) )
{
layerIdx.insert( vl->id(), layers.size() );
layers << qMakePair<QgsVectorLayer *, int>( vl, mAttributeIdx.value( vl, -1 ) );
layers << QgsDxfExport::DxfLayer( vl, mAttributeIdx.value( vl, -1 ) );
}
}
}

QList< QPair<QgsVectorLayer *, int> > layersInROrder;
QList< QgsDxfExport::DxfLayer > layersInROrder;

QList<QgsMapLayer *> layerOrder = mRootNode->layerOrder();

Expand Down Expand Up @@ -549,7 +549,7 @@ void QgsDxfExportDialog::deSelectAll()
}


QList< QPair<QgsVectorLayer *, int> > QgsDxfExportDialog::layers() const
QList< QgsDxfExport::DxfLayer > QgsDxfExportDialog::layers() const
{
const QgsVectorLayerAndAttributeModel *model = dynamic_cast< const QgsVectorLayerAndAttributeModel *>( mTreeView->model() );
Q_ASSERT( model );
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsdxfexportdialog.h
Expand Up @@ -53,7 +53,7 @@ class QgsVectorLayerAndAttributeModel : public QgsLayerTreeModel
Qt::ItemFlags flags( const QModelIndex &index ) const override;
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;

QList< QPair<QgsVectorLayer *, int> > layers() const;
QList< QgsDxfExport::DxfLayer > layers() const;

QgsVectorLayer *vectorLayer( const QModelIndex &index ) const;
int attributeIndex( const QgsVectorLayer *vl ) const;
Expand All @@ -79,7 +79,7 @@ class QgsDxfExportDialog : public QDialog, private Ui::QgsDxfExportDialogBase
QgsDxfExportDialog( QWidget *parent = nullptr, Qt::WindowFlags f = nullptr );
~QgsDxfExportDialog() override;

QList< QPair<QgsVectorLayer *, int> > layers() const;
QList< QgsDxfExport::DxfLayer > layers() const;

double symbologyScale() const;
QgsDxfExport::SymbologyExport symbologyMode() const;
Expand Down
10 changes: 5 additions & 5 deletions src/core/dxf/qgsdxfexport.cpp
Expand Up @@ -398,18 +398,18 @@ QgsDxfExport::Flags QgsDxfExport::flags() const
return mFlags;
}

void QgsDxfExport::addLayers( const QList< QPair< QgsVectorLayer *, int > > &layers )
void QgsDxfExport::addLayers( const QList<DxfLayer> &layers )
{
QList<QgsMapLayer *> layerList;

mLayerNameAttribute.clear();

QList< QPair< QgsVectorLayer *, int > >::const_iterator layerIt = layers.constBegin();
QList< DxfLayer >::const_iterator layerIt = layers.constBegin();
for ( ; layerIt != layers.constEnd(); ++layerIt )
{
layerList << layerIt->first;
if ( layerIt->second >= 0 )
mLayerNameAttribute.insert( layerIt->first->id(), layerIt->second );
layerList << layerIt->layer();
if ( layerIt->layerOutputAttributeIndex() >= 0 )
mLayerNameAttribute.insert( layerIt->layer()->id(), layerIt->layerOutputAttributeIndex() );
}

mMapSettings.setLayers( layerList );
Expand Down
21 changes: 20 additions & 1 deletion src/core/dxf/qgsdxfexport.h
Expand Up @@ -51,6 +51,25 @@ namespace pal SIP_SKIP
class CORE_EXPORT QgsDxfExport
{
public:

/**
* Layers and optional attribute index to split
* into multiple layers using attribute value as layer name.
*/
struct DxfLayer
{
DxfLayer( QgsVectorLayer *vl, int layerOutputAttributeIndex = -1 )
: mLayer( vl )
, mLayerOutputAttributeIndex( layerOutputAttributeIndex )
{}

QgsVectorLayer *layer() const {return mLayer;}
int layerOutputAttributeIndex() const {return mLayerOutputAttributeIndex;}

QgsVectorLayer *mLayer;
int mLayerOutputAttributeIndex;
};

enum SymbologyExport
{
NoSymbology = 0, //!< Export only data
Expand Down Expand Up @@ -97,7 +116,7 @@ class CORE_EXPORT QgsDxfExport
* \param layers list of layers and corresponding attribute indexes that determine the layer name (-1 for original layer name or title)
* \see setLayerTitleAsName
*/
void addLayers( const QList< QPair<QgsVectorLayer *, int > > &layers );
void addLayers( const QList< QgsDxfExport::DxfLayer > &layers );

/**
* Export to a dxf file in the given encoding
Expand Down
10 changes: 5 additions & 5 deletions tests/src/core/testqgsdxfexport.cpp
Expand Up @@ -94,7 +94,7 @@ void TestQgsDxfExport::cleanup()
void TestQgsDxfExport::testPoints()
{
QgsDxfExport d;
d.addLayers( QList< QPair< QgsVectorLayer *, int > >() << qMakePair( mPointLayer, -1 ) );
d.addLayers( QList< QgsDxfExport::DxfLayer >() << QgsDxfExport::DxfLayer( mPointLayer ) );

QgsMapSettings mapSettings;
QSize size( 640, 480 );
Expand Down Expand Up @@ -122,7 +122,7 @@ void TestQgsDxfExport::testPoints()
void TestQgsDxfExport::testLines()
{
QgsDxfExport d;
d.addLayers( QList< QPair< QgsVectorLayer *, int > >() << qMakePair( mLineLayer, -1 ) );
d.addLayers( QList< QgsDxfExport::DxfLayer >() << QgsDxfExport::DxfLayer( mLineLayer ) );

QgsMapSettings mapSettings;
QSize size( 640, 480 );
Expand Down Expand Up @@ -150,7 +150,7 @@ void TestQgsDxfExport::testLines()
void TestQgsDxfExport::testPolygons()
{
QgsDxfExport d;
d.addLayers( QList< QPair< QgsVectorLayer *, int > >() << qMakePair( mPolygonLayer, -1 ) );
d.addLayers( QList< QgsDxfExport::DxfLayer >() << QgsDxfExport::DxfLayer( mPolygonLayer ) );

QgsMapSettings mapSettings;
QSize size( 640, 480 );
Expand Down Expand Up @@ -189,7 +189,7 @@ void TestQgsDxfExport::testMtext()
mPointLayer->setLabelsEnabled( true );

QgsDxfExport d;
d.addLayers( QList< QPair< QgsVectorLayer *, int > >() << qMakePair( mPointLayer, -1 ) );
d.addLayers( QList< QgsDxfExport::DxfLayer >() << QgsDxfExport::DxfLayer( mPointLayer ) );

QgsMapSettings mapSettings;
QSize size( 640, 480 );
Expand Down Expand Up @@ -251,7 +251,7 @@ void TestQgsDxfExport::testText()
mPointLayer->setLabelsEnabled( true );

QgsDxfExport d;
d.addLayers( QList< QPair< QgsVectorLayer *, int > >() << qMakePair( mPointLayer, -1 ) );
d.addLayers( QList< QgsDxfExport::DxfLayer >() << QgsDxfExport::DxfLayer( mPointLayer ) );

QgsMapSettings mapSettings;
QSize size( 640, 480 );
Expand Down

0 comments on commit 31e93ae

Please sign in to comment.