Skip to content

Commit 5c3a755

Browse files
committed
Fix dxf export for label layers and add unit test
1 parent ec86fcb commit 5c3a755

File tree

2 files changed

+56
-33
lines changed

2 files changed

+56
-33
lines changed

src/core/dxf/qgsdxfexport.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -1071,12 +1071,9 @@ void QgsDxfExport::writeEntities()
10711071
else
10721072
{
10731073
QgsSymbolList symbolList = renderer->symbolsForFeature( fet, ctx );
1074-
if ( symbolList.empty() )
1075-
{
1076-
continue;
1077-
}
1074+
bool hasSymbology = symbolList.size() > 0;
10781075

1079-
if ( mSymbologyExport == QgsDxfExport::SymbolLayerSymbology ) // symbol layer symbology, but layer does not use symbol levels
1076+
if ( hasSymbology && mSymbologyExport == QgsDxfExport::SymbolLayerSymbology ) // symbol layer symbology, but layer does not use symbol levels
10801077
{
10811078
QgsSymbolList::iterator symbolIt = symbolList.begin();
10821079
for ( ; symbolIt != symbolList.end(); ++symbolIt )
@@ -1088,7 +1085,7 @@ void QgsDxfExport::writeEntities()
10881085
}
10891086
}
10901087
}
1091-
else
1088+
else if ( hasSymbology )
10921089
{
10931090
// take first symbollayer from first symbol
10941091
QgsSymbol *s = symbolList.first();

tests/src/core/testqgsdxfexport.cpp

+53-27
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgsproject.h"
2121
#include "qgsvectorlayer.h"
2222
#include "qgsfontutils.h"
23+
#include "qgsnullsymbolrenderer.h"
2324
#include "qgstextrenderer.h"
2425
#include "qgspallabeling.h"
2526
#include "qgslabelingengine.h"
@@ -41,10 +42,12 @@ class TestQgsDxfExport : public QObject
4142
void testLines();
4243
void testPolygons();
4344
void testMtext();
45+
void testMTextNoSymbology(); //tests if label export works if layer has vector renderer type 'no symbols'
4446
void testText();
4547

4648
private:
4749
QgsVectorLayer *mPointLayer = nullptr;
50+
QgsVectorLayer *mPointLayerNoSymbols = nullptr;
4851
QgsVectorLayer *mLineLayer = nullptr;
4952
QgsVectorLayer *mPolygonLayer = nullptr;
5053

@@ -54,6 +57,7 @@ class TestQgsDxfExport : public QObject
5457
QString getTempFileName( const QString &file ) const;
5558

5659
bool fileContainsText( const QString &path, const QString &text ) const;
60+
bool testMtext( QgsVectorLayer *vlayer, const QString &tempFileName ) const;
5761
};
5862

5963
void TestQgsDxfExport::initTestCase()
@@ -75,6 +79,10 @@ void TestQgsDxfExport::init()
7579
mPointLayer = new QgsVectorLayer( filename, QStringLiteral( "points" ), QStringLiteral( "ogr" ) );
7680
QVERIFY( mPointLayer->isValid() );
7781
QgsProject::instance()->addMapLayer( mPointLayer );
82+
mPointLayerNoSymbols = new QgsVectorLayer( filename, QStringLiteral( "points" ), QStringLiteral( "ogr" ) );
83+
QVERIFY( mPointLayerNoSymbols->isValid() );
84+
mPointLayerNoSymbols->setRenderer( new QgsNullSymbolRenderer() );
85+
QgsProject::instance()->addMapLayer( mPointLayerNoSymbols );
7886
filename = QStringLiteral( TEST_DATA_DIR ) + "/lines.shp";
7987
mLineLayer = new QgsVectorLayer( filename, QStringLiteral( "lines" ), QStringLiteral( "ogr" ) );
8088
QVERIFY( mLineLayer->isValid() );
@@ -176,6 +184,16 @@ void TestQgsDxfExport::testPolygons()
176184
}
177185

178186
void TestQgsDxfExport::testMtext()
187+
{
188+
QVERIFY( testMtext( mPointLayer, QStringLiteral( "mtext_dxf" ) ) );
189+
}
190+
191+
void TestQgsDxfExport::testMTextNoSymbology()
192+
{
193+
QVERIFY( testMtext( mPointLayerNoSymbols, QStringLiteral( "text_no_symbology_dxf" ) ) );
194+
}
195+
196+
void TestQgsDxfExport::testText()
179197
{
180198
QgsPalLayerSettings settings;
181199
settings.fieldName = QStringLiteral( "Class" );
@@ -202,20 +220,21 @@ void TestQgsDxfExport::testMtext()
202220
d.setMapSettings( mapSettings );
203221
d.setSymbologyScale( 1000 );
204222
d.setSymbologyExport( QgsDxfExport::FeatureSymbology );
223+
d.setFlags( QgsDxfExport::FlagNoMText );
205224

206-
QString file = getTempFileName( "mtext_dxf" );
225+
QString file = getTempFileName( "text_dxf" );
207226
QFile dxfFile( file );
208227
QCOMPARE( d.writeToFile( &dxfFile, QStringLiteral( "CP1252" ) ), 0 );
209228
dxfFile.close();
210229

211230

212-
QVERIFY( fileContainsText( file, "MTEXT\n"
231+
QVERIFY( fileContainsText( file, "TEXT\n"
213232
" 5\n"
214233
"dd\n"
215234
"100\n"
216235
"AcDbEntity\n"
217236
"100\n"
218-
"AcDbMText\n"
237+
"AcDbText\n"
219238
" 8\n"
220239
"points\n"
221240
"420\n"
@@ -224,21 +243,25 @@ void TestQgsDxfExport::testMtext()
224243
"**no check**\n"
225244
" 20\n"
226245
"**no check**\n"
246+
" 40\n"
247+
"**no check**\n"
227248
" 1\n"
228-
"\\fQGIS Vera Sans|i0|b1;\\H3.81136;Biplane\n"
249+
"Biplane\n"
229250
" 50\n"
230251
"0.0\n"
231-
" 41\n"
232-
"**no check**\n"
233-
" 71\n"
234-
" 7\n"
235252
" 7\n"
236253
"STANDARD\n"
237-
" 0" ) );
254+
"100\n"
255+
"AcDbText" ) );
238256
}
239257

240-
void TestQgsDxfExport::testText()
258+
bool TestQgsDxfExport::testMtext( QgsVectorLayer *vlayer, const QString &tempFileName ) const
241259
{
260+
if ( !vlayer )
261+
{
262+
return false;
263+
}
264+
242265
QgsPalLayerSettings settings;
243266
settings.fieldName = QStringLiteral( "Class" );
244267
QgsTextFormat format;
@@ -247,38 +270,40 @@ void TestQgsDxfExport::testText()
247270
format.setNamedStyle( QStringLiteral( "Bold" ) );
248271
format.setColor( QColor( 200, 0, 200 ) );
249272
settings.setFormat( format );
250-
mPointLayer->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
251-
mPointLayer->setLabelsEnabled( true );
273+
vlayer->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
274+
vlayer->setLabelsEnabled( true );
252275

253276
QgsDxfExport d;
254-
d.addLayers( QList< QPair< QgsVectorLayer *, int > >() << qMakePair( mPointLayer, -1 ) );
277+
d.addLayers( QList< QPair< QgsVectorLayer *, int > >() << qMakePair( vlayer, -1 ) );
255278

256279
QgsMapSettings mapSettings;
257280
QSize size( 640, 480 );
258281
mapSettings.setOutputSize( size );
259-
mapSettings.setExtent( mPointLayer->extent() );
260-
mapSettings.setLayers( QList<QgsMapLayer *>() << mPointLayer );
282+
mapSettings.setExtent( vlayer->extent() );
283+
mapSettings.setLayers( QList<QgsMapLayer *>() << vlayer );
261284
mapSettings.setOutputDpi( 96 );
262-
mapSettings.setDestinationCrs( mPointLayer->crs() );
285+
mapSettings.setDestinationCrs( vlayer->crs() );
263286

264287
d.setMapSettings( mapSettings );
265288
d.setSymbologyScale( 1000 );
266289
d.setSymbologyExport( QgsDxfExport::FeatureSymbology );
267-
d.setFlags( QgsDxfExport::FlagNoMText );
268290

269-
QString file = getTempFileName( "text_dxf" );
291+
QString file = getTempFileName( tempFileName );
270292
QFile dxfFile( file );
271-
QCOMPARE( d.writeToFile( &dxfFile, QStringLiteral( "CP1252" ) ), 0 );
293+
if ( !d.writeToFile( &dxfFile, QStringLiteral( "CP1252" ) ) == 0 )
294+
{
295+
return false;
296+
}
272297
dxfFile.close();
273298

274299

275-
QVERIFY( fileContainsText( file, "TEXT\n"
300+
return ( fileContainsText( file, "MTEXT\n"
276301
" 5\n"
277-
"dd\n"
302+
"**no check**\n"
278303
"100\n"
279304
"AcDbEntity\n"
280305
"100\n"
281-
"AcDbText\n"
306+
"AcDbMText\n"
282307
" 8\n"
283308
"points\n"
284309
"420\n"
@@ -287,16 +312,17 @@ void TestQgsDxfExport::testText()
287312
"**no check**\n"
288313
" 20\n"
289314
"**no check**\n"
290-
" 40\n"
291-
"**no check**\n"
292315
" 1\n"
293-
"Biplane\n"
316+
"\\fQGIS Vera Sans|i0|b1;\\H3.81136;Biplane\n"
294317
" 50\n"
295318
"0.0\n"
319+
" 41\n"
320+
"**no check**\n"
321+
" 71\n"
322+
" 7\n"
296323
" 7\n"
297324
"STANDARD\n"
298-
"100\n"
299-
"AcDbText" ) );
325+
" 0" ) );
300326
}
301327

302328
bool TestQgsDxfExport::fileContainsText( const QString &path, const QString &text ) const

0 commit comments

Comments
 (0)