Skip to content

Commit 59b17f2

Browse files
authored
Merge pull request #6321 from 3nids/dxf_addlayers
[dxf] use a struct instead of QPair for layers
2 parents 94b644e + 2a7aeaf commit 59b17f2

File tree

9 files changed

+69
-84
lines changed

9 files changed

+69
-84
lines changed

python/core/conversions.sip

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,65 +1683,6 @@ template<int, TYPE2*>
16831683
%End
16841684
};
16851685

1686-
%MappedType QList < QPair< QgsVectorLayer *, int > >
1687-
{
1688-
%TypeHeaderCode
1689-
#include <QPair>
1690-
#include <QList>
1691-
%End
1692-
1693-
%ConvertFromTypeCode
1694-
//convert map to a python dictionary
1695-
PyObject *d;
1696-
1697-
if ((d = PyList_New( sipCpp->size() )) == NULL)
1698-
return NULL;
1699-
1700-
for ( int i = 0; i<sipCpp->size(); i++ )
1701-
{
1702-
PyObject *p;
1703-
if ((p = PyList_New(2) ) == NULL)
1704-
{
1705-
Py_DECREF(d);
1706-
return NULL;
1707-
}
1708-
1709-
PyObject *t1obj = sipConvertFromNewType(sipCpp->at(i).first, sipType_QgsVectorLayer, sipTransferObj);
1710-
PyObject *t2obj = PyLong_FromLong( (long) sipCpp->at(i).second );
1711-
PyList_SetItem( p, 0, t1obj );
1712-
PyList_SetItem( p, 1, t2obj );
1713-
1714-
PyList_SetItem( d, i, p );
1715-
}
1716-
1717-
return d;
1718-
%End
1719-
1720-
%ConvertToTypeCode
1721-
Py_ssize_t i = 0;
1722-
1723-
QList < QPair< QgsVectorLayer *, int > > *qm = new QList< QPair< QgsVectorLayer *, int > >;
1724-
1725-
for ( i = 0; i < PyList_GET_SIZE(sipPy); i++ )
1726-
{
1727-
int state;
1728-
1729-
PyObject *sipPair = PyList_GetItem( sipPy, i );
1730-
PyObject *sipLayer = PyList_GetItem( sipPair, 0 );
1731-
PyObject *sipIdx = PyList_GetItem( sipPair, 1 );
1732-
1733-
QgsVectorLayer *t1 = reinterpret_cast<QgsVectorLayer *>(sipConvertToType(sipLayer, sipType_QgsVectorLayer, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr));
1734-
int idx = PyLong_AsLongLong(sipIdx);
1735-
qm->append( qMakePair<QgsVectorLayer *, int>( t1, idx ) );
1736-
sipReleaseType(t1, sipType_QgsVectorLayer, state);
1737-
}
1738-
1739-
*sipCppPtr = qm;
1740-
1741-
return sipGetState(sipTransferObj);
1742-
%End
1743-
};
1744-
17451686
%MappedType QMap< QPair< QString, QString>, QPair< int, int > >
17461687
{
17471688
%TypeHeaderCode

python/core/dxf/qgsdxfexport.sip.in

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ class QgsDxfExport
2020
#include "qgsdxfexport.h"
2121
%End
2222
public:
23+
24+
struct DxfLayer
25+
{
26+
DxfLayer( QgsVectorLayer *vl, int layerOutputAttributeIndex = -1 );
27+
28+
QgsVectorLayer *layer() const;
29+
%Docstring
30+
Return the layer
31+
%End
32+
33+
int layerOutputAttributeIndex() const;
34+
%Docstring
35+
Return the attribute index used to split into multiple layers.
36+
The attribute value is used for layer names.
37+
%End
38+
39+
};
40+
2341
enum SymbologyExport
2442
{
2543
NoSymbology,
@@ -64,7 +82,7 @@ Returns the export flags.
6482
.. seealso:: :py:func:`setFlags`
6583
%End
6684

67-
void addLayers( const QList< QPair<QgsVectorLayer *, int > > &layers );
85+
void addLayers( const QList< QgsDxfExport::DxfLayer > &layers );
6886
%Docstring
6987
Add layers to export
7088

src/app/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,15 +1357,15 @@ int main( int argc, char *argv[] )
13571357
dxfExport.setExtent( dxfExtent );
13581358

13591359
QStringList layerIds;
1360-
QList< QPair<QgsVectorLayer *, int > > layers;
1360+
QList< QgsDxfExport::DxfLayer > layers;
13611361
if ( !dxfMapTheme.isEmpty() )
13621362
{
13631363
Q_FOREACH ( QgsMapLayer *layer, QgsProject::instance()->mapThemeCollection()->mapThemeVisibleLayers( dxfMapTheme ) )
13641364
{
13651365
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
13661366
if ( !vl )
13671367
continue;
1368-
layers << qMakePair<QgsVectorLayer *, int>( vl, -1 );
1368+
layers << QgsDxfExport::DxfLayer( vl );
13691369
layerIds << vl->id();
13701370
}
13711371
}
@@ -1376,7 +1376,7 @@ int main( int argc, char *argv[] )
13761376
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( ml );
13771377
if ( !vl )
13781378
continue;
1379-
layers << qMakePair<QgsVectorLayer *, int>( vl, -1 );
1379+
layers << QgsDxfExport::DxfLayer( vl );
13801380
layerIds << vl->id();
13811381
}
13821382
}

src/app/qgsdxfexportdialog.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,9 @@ bool QgsVectorLayerAndAttributeModel::setData( const QModelIndex &index, const Q
276276
}
277277

278278

279-
QList< QPair<QgsVectorLayer *, int> > QgsVectorLayerAndAttributeModel::layers() const
279+
QList< QgsDxfExport::DxfLayer > QgsVectorLayerAndAttributeModel::layers() const
280280
{
281-
QList< QPair<QgsVectorLayer *, int> > layers;
281+
QList< QgsDxfExport::DxfLayer > layers;
282282
QHash< QString, int > layerIdx;
283283

284284
Q_FOREACH ( const QModelIndex &idx, mCheckedLeafs )
@@ -293,7 +293,7 @@ QList< QPair<QgsVectorLayer *, int> > QgsVectorLayerAndAttributeModel::layers()
293293
if ( !layerIdx.contains( vl->id() ) )
294294
{
295295
layerIdx.insert( vl->id(), layers.size() );
296-
layers << qMakePair<QgsVectorLayer *, int>( vl, mAttributeIdx.value( vl, -1 ) );
296+
layers << QgsDxfExport::DxfLayer( vl, mAttributeIdx.value( vl, -1 ) );
297297
}
298298
}
299299
}
@@ -304,12 +304,12 @@ QList< QPair<QgsVectorLayer *, int> > QgsVectorLayerAndAttributeModel::layers()
304304
if ( !layerIdx.contains( vl->id() ) )
305305
{
306306
layerIdx.insert( vl->id(), layers.size() );
307-
layers << qMakePair<QgsVectorLayer *, int>( vl, mAttributeIdx.value( vl, -1 ) );
307+
layers << QgsDxfExport::DxfLayer( vl, mAttributeIdx.value( vl, -1 ) );
308308
}
309309
}
310310
}
311311

312-
QList< QPair<QgsVectorLayer *, int> > layersInROrder;
312+
QList< QgsDxfExport::DxfLayer > layersInROrder;
313313

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

@@ -549,7 +549,7 @@ void QgsDxfExportDialog::deSelectAll()
549549
}
550550

551551

552-
QList< QPair<QgsVectorLayer *, int> > QgsDxfExportDialog::layers() const
552+
QList< QgsDxfExport::DxfLayer > QgsDxfExportDialog::layers() const
553553
{
554554
const QgsVectorLayerAndAttributeModel *model = dynamic_cast< const QgsVectorLayerAndAttributeModel *>( mTreeView->model() );
555555
Q_ASSERT( model );

src/app/qgsdxfexportdialog.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class QgsVectorLayerAndAttributeModel : public QgsLayerTreeModel
5353
Qt::ItemFlags flags( const QModelIndex &index ) const override;
5454
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
5555

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

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

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

8484
double symbologyScale() const;
8585
QgsDxfExport::SymbologyExport symbologyMode() const;

src/core/dxf/qgsdxfexport.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,18 +398,18 @@ QgsDxfExport::Flags QgsDxfExport::flags() const
398398
return mFlags;
399399
}
400400

401-
void QgsDxfExport::addLayers( const QList< QPair< QgsVectorLayer *, int > > &layers )
401+
void QgsDxfExport::addLayers( const QList<DxfLayer> &layers )
402402
{
403403
QList<QgsMapLayer *> layerList;
404404

405405
mLayerNameAttribute.clear();
406406

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

415415
mMapSettings.setLayers( layerList );

src/core/dxf/qgsdxfexport.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,32 @@ namespace pal SIP_SKIP
5151
class CORE_EXPORT QgsDxfExport
5252
{
5353
public:
54+
55+
/**
56+
* Layers and optional attribute index to split
57+
* into multiple layers using attribute value as layer name.
58+
*/
59+
struct DxfLayer
60+
{
61+
DxfLayer( QgsVectorLayer *vl, int layerOutputAttributeIndex = -1 )
62+
: mLayer( vl )
63+
, mLayerOutputAttributeIndex( layerOutputAttributeIndex )
64+
{}
65+
66+
//! Return the layer
67+
QgsVectorLayer *layer() const {return mLayer;}
68+
69+
/**
70+
* Return the attribute index used to split into multiple layers.
71+
* The attribute value is used for layer names.
72+
*/
73+
int layerOutputAttributeIndex() const {return mLayerOutputAttributeIndex;}
74+
75+
private:
76+
QgsVectorLayer *mLayer = nullptr;
77+
int mLayerOutputAttributeIndex = -1;
78+
};
79+
5480
enum SymbologyExport
5581
{
5682
NoSymbology = 0, //!< Export only data
@@ -97,7 +123,7 @@ class CORE_EXPORT QgsDxfExport
97123
* \param layers list of layers and corresponding attribute indexes that determine the layer name (-1 for original layer name or title)
98124
* \see setLayerTitleAsName
99125
*/
100-
void addLayers( const QList< QPair<QgsVectorLayer *, int > > &layers );
126+
void addLayers( const QList< QgsDxfExport::DxfLayer > &layers );
101127

102128
/**
103129
* Export to a dxf file in the given encoding

src/server/services/wms/qgswmsrenderer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ namespace QgsWms
767767
QStringList wfsLayerIds = QgsServerProjectUtils::wfsLayerIds( *mProject );
768768

769769
// get dxf layers
770-
QList< QPair<QgsVectorLayer *, int > > dxfLayers;
770+
QList< QgsDxfExport::DxfLayer > dxfLayers;
771771
int layerIdx = -1;
772772
Q_FOREACH ( QgsMapLayer *layer, layers )
773773
{
@@ -801,7 +801,7 @@ namespace QgsWms
801801
layerAttribute = vlayer->fields().indexFromName( layerAttributes.at( layerIdx ) );
802802
}
803803

804-
dxfLayers.append( qMakePair( vlayer, layerAttribute ) );
804+
dxfLayers.append( QgsDxfExport::DxfLayer( vlayer, layerAttribute ) );
805805
}
806806

807807
// add layers to dxf

tests/src/core/testqgsdxfexport.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void TestQgsDxfExport::cleanup()
9494
void TestQgsDxfExport::testPoints()
9595
{
9696
QgsDxfExport d;
97-
d.addLayers( QList< QPair< QgsVectorLayer *, int > >() << qMakePair( mPointLayer, -1 ) );
97+
d.addLayers( QList< QgsDxfExport::DxfLayer >() << QgsDxfExport::DxfLayer( mPointLayer ) );
9898

9999
QgsMapSettings mapSettings;
100100
QSize size( 640, 480 );
@@ -122,7 +122,7 @@ void TestQgsDxfExport::testPoints()
122122
void TestQgsDxfExport::testLines()
123123
{
124124
QgsDxfExport d;
125-
d.addLayers( QList< QPair< QgsVectorLayer *, int > >() << qMakePair( mLineLayer, -1 ) );
125+
d.addLayers( QList< QgsDxfExport::DxfLayer >() << QgsDxfExport::DxfLayer( mLineLayer ) );
126126

127127
QgsMapSettings mapSettings;
128128
QSize size( 640, 480 );
@@ -150,7 +150,7 @@ void TestQgsDxfExport::testLines()
150150
void TestQgsDxfExport::testPolygons()
151151
{
152152
QgsDxfExport d;
153-
d.addLayers( QList< QPair< QgsVectorLayer *, int > >() << qMakePair( mPolygonLayer, -1 ) );
153+
d.addLayers( QList< QgsDxfExport::DxfLayer >() << QgsDxfExport::DxfLayer( mPolygonLayer ) );
154154

155155
QgsMapSettings mapSettings;
156156
QSize size( 640, 480 );
@@ -189,7 +189,7 @@ void TestQgsDxfExport::testMtext()
189189
mPointLayer->setLabelsEnabled( true );
190190

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

194194
QgsMapSettings mapSettings;
195195
QSize size( 640, 480 );
@@ -251,7 +251,7 @@ void TestQgsDxfExport::testText()
251251
mPointLayer->setLabelsEnabled( true );
252252

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

256256
QgsMapSettings mapSettings;
257257
QSize size( 640, 480 );

0 commit comments

Comments
 (0)