Skip to content
Permalink
Browse files

[dxf] use a struct instead of QPair for layers

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 31e93aecae272f4845f5c321f04e71a8a9c5ec57
@@ -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,
@@ -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

@@ -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();
}
}
@@ -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();
}
}
@@ -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 )
@@ -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 ) );
}
}
}
@@ -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();

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

0 comments on commit 31e93ae

Please sign in to comment.
You can’t perform that action at this time.