Skip to content
Permalink
Browse files

Fix dxf export for label layers and add unit test

  • Loading branch information
mhugent committed Feb 13, 2018
1 parent ec86fcb commit 5c3a7554de2c1e4aa1847d87d6fb334dfaa6009d
Showing with 56 additions and 33 deletions.
  1. +3 −6 src/core/dxf/qgsdxfexport.cpp
  2. +53 −27 tests/src/core/testqgsdxfexport.cpp
@@ -1071,12 +1071,9 @@ void QgsDxfExport::writeEntities()
else
{
QgsSymbolList symbolList = renderer->symbolsForFeature( fet, ctx );
if ( symbolList.empty() )
{
continue;
}
bool hasSymbology = symbolList.size() > 0;

if ( mSymbologyExport == QgsDxfExport::SymbolLayerSymbology ) // symbol layer symbology, but layer does not use symbol levels
if ( hasSymbology && mSymbologyExport == QgsDxfExport::SymbolLayerSymbology ) // symbol layer symbology, but layer does not use symbol levels
{
QgsSymbolList::iterator symbolIt = symbolList.begin();
for ( ; symbolIt != symbolList.end(); ++symbolIt )
@@ -1088,7 +1085,7 @@ void QgsDxfExport::writeEntities()
}
}
}
else
else if ( hasSymbology )
{
// take first symbollayer from first symbol
QgsSymbol *s = symbolList.first();
@@ -20,6 +20,7 @@
#include "qgsproject.h"
#include "qgsvectorlayer.h"
#include "qgsfontutils.h"
#include "qgsnullsymbolrenderer.h"
#include "qgstextrenderer.h"
#include "qgspallabeling.h"
#include "qgslabelingengine.h"
@@ -41,10 +42,12 @@ class TestQgsDxfExport : public QObject
void testLines();
void testPolygons();
void testMtext();
void testMTextNoSymbology(); //tests if label export works if layer has vector renderer type 'no symbols'
void testText();

private:
QgsVectorLayer *mPointLayer = nullptr;
QgsVectorLayer *mPointLayerNoSymbols = nullptr;
QgsVectorLayer *mLineLayer = nullptr;
QgsVectorLayer *mPolygonLayer = nullptr;

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

bool fileContainsText( const QString &path, const QString &text ) const;
bool testMtext( QgsVectorLayer *vlayer, const QString &tempFileName ) const;
};

void TestQgsDxfExport::initTestCase()
@@ -75,6 +79,10 @@ void TestQgsDxfExport::init()
mPointLayer = new QgsVectorLayer( filename, QStringLiteral( "points" ), QStringLiteral( "ogr" ) );
QVERIFY( mPointLayer->isValid() );
QgsProject::instance()->addMapLayer( mPointLayer );
mPointLayerNoSymbols = new QgsVectorLayer( filename, QStringLiteral( "points" ), QStringLiteral( "ogr" ) );
QVERIFY( mPointLayerNoSymbols->isValid() );
mPointLayerNoSymbols->setRenderer( new QgsNullSymbolRenderer() );
QgsProject::instance()->addMapLayer( mPointLayerNoSymbols );
filename = QStringLiteral( TEST_DATA_DIR ) + "/lines.shp";
mLineLayer = new QgsVectorLayer( filename, QStringLiteral( "lines" ), QStringLiteral( "ogr" ) );
QVERIFY( mLineLayer->isValid() );
@@ -176,6 +184,16 @@ void TestQgsDxfExport::testPolygons()
}

void TestQgsDxfExport::testMtext()
{
QVERIFY( testMtext( mPointLayer, QStringLiteral( "mtext_dxf" ) ) );
}

void TestQgsDxfExport::testMTextNoSymbology()
{
QVERIFY( testMtext( mPointLayerNoSymbols, QStringLiteral( "text_no_symbology_dxf" ) ) );
}

void TestQgsDxfExport::testText()
{
QgsPalLayerSettings settings;
settings.fieldName = QStringLiteral( "Class" );
@@ -202,20 +220,21 @@ void TestQgsDxfExport::testMtext()
d.setMapSettings( mapSettings );
d.setSymbologyScale( 1000 );
d.setSymbologyExport( QgsDxfExport::FeatureSymbology );
d.setFlags( QgsDxfExport::FlagNoMText );

QString file = getTempFileName( "mtext_dxf" );
QString file = getTempFileName( "text_dxf" );
QFile dxfFile( file );
QCOMPARE( d.writeToFile( &dxfFile, QStringLiteral( "CP1252" ) ), 0 );
dxfFile.close();


QVERIFY( fileContainsText( file, "MTEXT\n"
QVERIFY( fileContainsText( file, "TEXT\n"
" 5\n"
"dd\n"
"100\n"
"AcDbEntity\n"
"100\n"
"AcDbMText\n"
"AcDbText\n"
" 8\n"
"points\n"
"420\n"
@@ -224,21 +243,25 @@ void TestQgsDxfExport::testMtext()
"**no check**\n"
" 20\n"
"**no check**\n"
" 40\n"
"**no check**\n"
" 1\n"
"\\fQGIS Vera Sans|i0|b1;\\H3.81136;Biplane\n"
"Biplane\n"
" 50\n"
"0.0\n"
" 41\n"
"**no check**\n"
" 71\n"
" 7\n"
" 7\n"
"STANDARD\n"
" 0" ) );
"100\n"
"AcDbText" ) );
}

void TestQgsDxfExport::testText()
bool TestQgsDxfExport::testMtext( QgsVectorLayer *vlayer, const QString &tempFileName ) const
{
if ( !vlayer )
{
return false;
}

QgsPalLayerSettings settings;
settings.fieldName = QStringLiteral( "Class" );
QgsTextFormat format;
@@ -247,38 +270,40 @@ void TestQgsDxfExport::testText()
format.setNamedStyle( QStringLiteral( "Bold" ) );
format.setColor( QColor( 200, 0, 200 ) );
settings.setFormat( format );
mPointLayer->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
mPointLayer->setLabelsEnabled( true );
vlayer->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
vlayer->setLabelsEnabled( true );

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

QgsMapSettings mapSettings;
QSize size( 640, 480 );
mapSettings.setOutputSize( size );
mapSettings.setExtent( mPointLayer->extent() );
mapSettings.setLayers( QList<QgsMapLayer *>() << mPointLayer );
mapSettings.setExtent( vlayer->extent() );
mapSettings.setLayers( QList<QgsMapLayer *>() << vlayer );
mapSettings.setOutputDpi( 96 );
mapSettings.setDestinationCrs( mPointLayer->crs() );
mapSettings.setDestinationCrs( vlayer->crs() );

d.setMapSettings( mapSettings );
d.setSymbologyScale( 1000 );
d.setSymbologyExport( QgsDxfExport::FeatureSymbology );
d.setFlags( QgsDxfExport::FlagNoMText );

QString file = getTempFileName( "text_dxf" );
QString file = getTempFileName( tempFileName );
QFile dxfFile( file );
QCOMPARE( d.writeToFile( &dxfFile, QStringLiteral( "CP1252" ) ), 0 );
if ( !d.writeToFile( &dxfFile, QStringLiteral( "CP1252" ) ) == 0 )

This comment has been minimized.

Copy link
@3nids

3nids Feb 14, 2018

Member

@mhugent this causes a warning:
screen shot 2018-02-14 at 13 14 30

{
return false;
}
dxfFile.close();


QVERIFY( fileContainsText( file, "TEXT\n"
return ( fileContainsText( file, "MTEXT\n"
" 5\n"
"dd\n"
"**no check**\n"
"100\n"
"AcDbEntity\n"
"100\n"
"AcDbText\n"
"AcDbMText\n"
" 8\n"
"points\n"
"420\n"
@@ -287,16 +312,17 @@ void TestQgsDxfExport::testText()
"**no check**\n"
" 20\n"
"**no check**\n"
" 40\n"
"**no check**\n"
" 1\n"
"Biplane\n"
"\\fQGIS Vera Sans|i0|b1;\\H3.81136;Biplane\n"
" 50\n"
"0.0\n"
" 41\n"
"**no check**\n"
" 71\n"
" 7\n"
" 7\n"
"STANDARD\n"
"100\n"
"AcDbText" ) );
" 0" ) );
}

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

0 comments on commit 5c3a755

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