Skip to content

Commit ef3eee2

Browse files
committedDec 5, 2013
Linestyles for Qt pen styles
1 parent 14de6e6 commit ef3eee2

File tree

2 files changed

+137
-23
lines changed

2 files changed

+137
-23
lines changed
 

‎src/core/dxf/qgsdxfexport.cpp

Lines changed: 128 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgspoint.h"
2121
#include "qgsrendererv2.h"
2222
#include "qgssymbollayerv2.h"
23+
#include "qgsfillsymbollayerv2.h"
2324
#include "qgslinesymbollayerv2.h"
2425
#include "qgsvectorlayer.h"
2526
#include <QIODevice>
@@ -427,18 +428,10 @@ void QgsDxfExport::writeTables()
427428
mLineStyles.clear();
428429
writeGroup( 0, "TABLE" );
429430
writeGroup( 2, "LTYPE" );
430-
writeGroup( 70, nLineTypes( slList ) + 1 );
431+
writeGroup( 70, nLineTypes( slList ) + 5 );
431432

432-
//add continuous style as default
433-
writeGroup( 0, "LTYPE" );
434-
writeGroup( 2, "CONTINUOUS" );
435-
writeGroup( 70, 64 );
436-
writeGroup( 3, "Defaultstyle" );
437-
writeGroup( 72, 65 );
438-
writeGroup( 73, 0 );
439-
writeGroup( 40, 0.0 );
440-
441-
//add symbol layer linestyles
433+
writeDefaultLinestyles();
434+
//add custom linestyles
442435
QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2*> >::const_iterator slIt = slList.constBegin();
443436
for ( ; slIt != slList.constEnd(); ++slIt )
444437
{
@@ -829,15 +822,7 @@ void QgsDxfExport::addFeature( const QgsFeature& fet, const QString& layer, cons
829822
{
830823
int c = colorFromSymbolLayer( symbolLayer );
831824
double width = widthFromSymbolLayer( symbolLayer );
832-
QString lineStyleName = "CONTINUOUS";
833-
QHash< const QgsSymbolLayerV2*, QString >::const_iterator lineTypeIt = mLineStyles.find( symbolLayer );
834-
if ( lineTypeIt != mLineStyles.constEnd() )
835-
{
836-
lineStyleName = lineTypeIt.value();
837-
}
838-
839-
//todo: write point symbols as blocks
840-
825+
QString lineStyleName = lineStyleFromSymbolLayer( symbolLayer );
841826
QGis::WkbType geometryType = geom->wkbType();
842827

843828
//single point
@@ -922,7 +907,7 @@ int QgsDxfExport::colorFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer )
922907
return closestColorMatch( c.rgba() );
923908
}
924909

925-
double QgsDxfExport::widthFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer )
910+
double QgsDxfExport::widthFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer ) const
926911
{
927912
//line symbol layer has width and width units
928913
if ( symbolLayer && symbolLayer->type() == QgsSymbolV2::Line )
@@ -938,6 +923,36 @@ double QgsDxfExport::widthFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer )
938923
//mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits )
939924
}
940925

926+
QString QgsDxfExport::lineStyleFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer )
927+
{
928+
QString lineStyleName = "CONTINUOUS";
929+
if ( !symbolLayer )
930+
{
931+
return lineStyleName;
932+
}
933+
934+
QHash< const QgsSymbolLayerV2*, QString >::const_iterator lineTypeIt = mLineStyles.find( symbolLayer );
935+
if ( lineTypeIt != mLineStyles.constEnd() )
936+
{
937+
lineStyleName = lineTypeIt.value();
938+
}
939+
else
940+
{
941+
//simple line and simple fill have pen style member
942+
if ( symbolLayer->layerType() == "SimpleLine" )
943+
{
944+
const QgsSimpleLineSymbolLayerV2* sl = static_cast< const QgsSimpleLineSymbolLayerV2* >( symbolLayer );
945+
return lineNameFromPenStyle( sl->penStyle() );
946+
}
947+
else if ( symbolLayer->layerType() == "SimpleFill" )
948+
{
949+
const QgsSimpleFillSymbolLayerV2* sf = static_cast< const QgsSimpleFillSymbolLayerV2* >( symbolLayer );
950+
return lineNameFromPenStyle( sf->borderStyle() );
951+
}
952+
}
953+
return lineStyleName;
954+
}
955+
941956
int QgsDxfExport::closestColorMatch( QRgb pixel )
942957
{
943958
int idx = 0;
@@ -1070,6 +1085,48 @@ QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2* > > QgsDxfExport::symbolLayers()
10701085
return symbolLayers;
10711086
}
10721087

1088+
void QgsDxfExport::writeDefaultLinestyles()
1089+
{
1090+
double das = dashSize();
1091+
double dos = dotSize();
1092+
double dss = dashSeparatorSize();
1093+
1094+
//continuous (Qt solid line)
1095+
writeGroup( 0, "LTYPE" );
1096+
writeGroup( 2, "CONTINUOUS" );
1097+
writeGroup( 70, 64 );
1098+
writeGroup( 3, "Defaultstyle" );
1099+
writeGroup( 72, 65 );
1100+
writeGroup( 73, 0 );
1101+
writeGroup( 40, 0.0 );
1102+
1103+
QVector<qreal> dashVector( 2 );
1104+
dashVector[0] = das;
1105+
dashVector[1] = dss;
1106+
writeLinestyle( "DASH", dashVector, QgsSymbolV2::MapUnit );
1107+
1108+
QVector<qreal> dotVector( 2 );
1109+
dotVector[0] = dos;
1110+
dotVector[1] = dss;
1111+
writeLinestyle( "DOT", dotVector, QgsSymbolV2::MapUnit );
1112+
1113+
QVector<qreal> dashDotVector( 4 );
1114+
dashDotVector[0] = das;
1115+
dashDotVector[1] = dss;
1116+
dashDotVector[2] = dos;
1117+
dashDotVector[3] = dss;
1118+
writeLinestyle( "DASHDOT", dashDotVector, QgsSymbolV2::MapUnit );
1119+
1120+
QVector<qreal> dashDotDotVector( 6 );
1121+
dashDotDotVector[0] = das;
1122+
dashDotDotVector[1] = dss;
1123+
dashDotDotVector[2] = dos;
1124+
dashDotDotVector[3] = dss;
1125+
dashDotDotVector[4] = dos;
1126+
dashDotDotVector[5] = dss;
1127+
writeLinestyle( "DASHDOTDOT", dashDotDotVector, QgsSymbolV2::MapUnit );
1128+
}
1129+
10731130
void QgsDxfExport::writeSymbolLayerLinestyle( const QgsSymbolLayerV2* symbolLayer )
10741131
{
10751132
if ( !symbolLayer )
@@ -1155,6 +1212,56 @@ bool QgsDxfExport::hasDataDefinedProperties( const QgsSymbolLayerV2* sl, const Q
11551212
return sl->hasDataDefinedProperties();
11561213
}
11571214

1215+
double QgsDxfExport::dashSize() const
1216+
{
1217+
double size = mSymbologyScaleDenominator * 0.002;
1218+
return sizeToMapUnits( size );
1219+
}
1220+
1221+
double QgsDxfExport::dotSize() const
1222+
{
1223+
double size = mSymbologyScaleDenominator * 0.0006;
1224+
return sizeToMapUnits( size );
1225+
}
1226+
1227+
double QgsDxfExport::dashSeparatorSize() const
1228+
{
1229+
double size = mSymbologyScaleDenominator * 0.0006;
1230+
return sizeToMapUnits( size );
1231+
}
1232+
1233+
double QgsDxfExport::sizeToMapUnits( double s ) const
1234+
{
1235+
double size = s;
1236+
if ( mMapUnits == QGis::Feet )
1237+
{
1238+
size /= 0.3048;
1239+
}
1240+
else if ( mMapUnits == QGis::Degrees )
1241+
{
1242+
size /= 111120;
1243+
}
1244+
return size;
1245+
}
1246+
1247+
QString QgsDxfExport::lineNameFromPenStyle( Qt::PenStyle style )
1248+
{
1249+
switch ( style )
1250+
{
1251+
case Qt::DashLine:
1252+
return "DASH";
1253+
case Qt::DotLine:
1254+
return "DOT";
1255+
case Qt::DashDotLine:
1256+
return "DASHDOT";
1257+
case Qt::DashDotDotLine:
1258+
return "DASHDOTDOT";
1259+
case Qt::SolidLine:
1260+
default:
1261+
return "CONTINUOUS";
1262+
}
1263+
}
1264+
11581265
/******************************************************Test with AC_1018 methods***************************************************************/
11591266

11601267
void QgsDxfExport::writeHeaderAC1018( QTextStream& stream )

‎src/core/dxf/qgsdxfexport.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class QgsDxfExport
108108

109109
void writePoint( const QgsPoint& pt, const QString& layer, const QgsFeature* f, const QgsSymbolLayerV2* symbolLayer, const QgsSymbolV2* symbol );
110110
void writeVertex( const QgsPoint& pt, const QString& layer );
111+
void writeDefaultLinestyles();
111112
void writeSymbolLayerLinestyle( const QgsSymbolLayerV2* symbolLayer );
112113
void writeLinestyle( const QString& styleName, const QVector<qreal>& pattern, QgsSymbolV2::OutputUnit u );
113114

@@ -129,8 +130,9 @@ class QgsDxfExport
129130
double scaleToMapUnits( double value, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits ) const;
130131

131132
//returns dxf palette index from symbol layer color
132-
int colorFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer );
133-
double widthFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer );
133+
static int colorFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer );
134+
double widthFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer ) const;
135+
QString lineStyleFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer );
134136

135137
//functions for dxf palette
136138
static int color_distance( QRgb p1, int index );
@@ -144,6 +146,11 @@ class QgsDxfExport
144146
QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2* > > symbolLayers();
145147
static int nLineTypes( const QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2*> >& symbolLayers );
146148
static bool hasDataDefinedProperties( const QgsSymbolLayerV2* sl, const QgsSymbolV2* symbol );
149+
double dashSize() const;
150+
double dotSize() const;
151+
double dashSeparatorSize() const;
152+
double sizeToMapUnits( double s ) const;
153+
static QString lineNameFromPenStyle( Qt::PenStyle style );
147154
};
148155

149156
#endif // QGSDXFEXPORT_H

0 commit comments

Comments
 (0)
Please sign in to comment.