Skip to content

Commit b3c2bd7

Browse files
committed
dxf export:
* fix support for data-defined properties in SVG export * remove drawRects and let it fallback to drawPath and drawPolygon * close arcs
1 parent b6e059c commit b3c2bd7

File tree

7 files changed

+92
-48
lines changed

7 files changed

+92
-48
lines changed

python/core/symbology-ng/qgssvgcache.sip

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ class QgsSvgCache : QObject
8787
/**Get image data*/
8888
QByteArray getImageData( const QString &path ) const;
8989

90+
/**Get SVG content*/
91+
const QByteArray& svgContent( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
92+
double widthScaleFactor, double rasterScaleFactor );
93+
9094
signals:
9195
/** Emit a signal to be caught by qgisapp and display a msg on status bar */
9296
void statusChanged( const QString& theStatusQString );

src/core/dxf/qgsdxfexport.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,8 @@ void QgsDxfExport::writeBlocks()
859859
writeGroup( 3, block );
860860
writeGroup( 1, "" );
861861

862-
ml->writeDxf( *this, mapUnitScaleFactor( mSymbologyScaleDenominator, ml->sizeUnit(), mMapUnits ), "0", &ctx, 0 ); // maplayer 0 -> block receives layer from INSERT statement
862+
// maplayer 0 -> block receives layer from INSERT statement
863+
ml->writeDxf( *this, mapUnitScaleFactor( mSymbologyScaleDenominator, ml->sizeUnit(), mMapUnits ), "0", &ctx, 0 );
863864

864865
writeGroup( 0, "ENDBLK" );
865866
writeHandle();

src/core/dxf/qgsdxfpaintengine.cpp

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void QgsDxfPaintEngine::drawPolygon( const QPointF *points, int pointCount, Poly
8484

8585
if ( mode == QPaintEngine::PolylineMode )
8686
{
87-
if ( mPen.style() != Qt::NoPen )
87+
if ( mPen.style() != Qt::NoPen && mPen.brush().style() != Qt::NoBrush )
8888
mDxf->writePolyline( polygon[0], mLayer, "CONTINUOUS", mPen.color(), currentWidth() );
8989
}
9090
else
@@ -94,25 +94,6 @@ void QgsDxfPaintEngine::drawPolygon( const QPointF *points, int pointCount, Poly
9494
}
9595
}
9696

97-
void QgsDxfPaintEngine::drawRects( const QRectF* rects, int rectCount )
98-
{
99-
if ( !mDxf || !mPaintDevice || !rects || mBrush.style() == Qt::NoBrush )
100-
return;
101-
102-
for ( int i = 0; i < rectCount; ++i )
103-
{
104-
double left = rects[i].left();
105-
double right = rects[i].right();
106-
double top = rects[i].top();
107-
double bottom = rects[i].bottom();
108-
QgsPoint pt1 = toDxfCoordinates( QPointF( left, bottom ) );
109-
QgsPoint pt2 = toDxfCoordinates( QPointF( right, bottom ) );
110-
QgsPoint pt3 = toDxfCoordinates( QPointF( left, top ) );
111-
QgsPoint pt4 = toDxfCoordinates( QPointF( right, top ) );
112-
mDxf->writeSolid( mLayer, mBrush.color(), pt1, pt2, pt3, pt4 );
113-
}
114-
}
115-
11697
void QgsDxfPaintEngine::drawPath( const QPainterPath& path )
11798
{
11899
int pathLength = path.elementCount();
@@ -171,10 +152,8 @@ void QgsDxfPaintEngine::endPolygon()
171152
{
172153
if ( mCurrentPolygon.size() > 1 )
173154
{
174-
#if 0
175155
if ( mPen.style() != Qt::NoPen )
176156
drawPolygon( mCurrentPolygon.constData(), mCurrentPolygon.size(), QPaintEngine::PolylineMode );
177-
#endif
178157

179158
mPolygon.resize( mPolygon.size() + 1 );
180159
setRing( mPolygon[ mPolygon.size() - 1 ], mCurrentPolygon.constData(), mCurrentPolygon.size() );
@@ -196,7 +175,7 @@ void QgsDxfPaintEngine::endCurve()
196175
if ( mCurrentCurve.size() >= 3 )
197176
{
198177
double t = 0.05;
199-
for ( int i = 1; i < 20; ++i ) //approximate curve with 20 segments
178+
for ( int i = 1; i <= 20; ++i ) //approximate curve with 20 segments
200179
{
201180
mCurrentPolygon.append( bezierPoint( mCurrentCurve, t ) );
202181
t += 0.05;

src/core/dxf/qgsdxfpaintengine.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class CORE_EXPORT QgsDxfPaintEngine: public QPaintEngine
3939
void drawPixmap( const QRectF& r, const QPixmap& pm, const QRectF& sr ) override;
4040

4141
void drawPolygon( const QPointF * points, int pointCount, PolygonDrawMode mode ) override;
42-
void drawRects( const QRectF * rects, int rectCount ) override;
4342
void drawPath( const QPainterPath& path ) override;
4443
void drawLines( const QLineF* lines, int lineCount ) override;
4544

src/core/symbology-ng/qgsmarkersymbollayerv2.cpp

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,15 +1549,6 @@ bool QgsSvgMarkerSymbolLayerV2::writeDxf( QgsDxfExport& e, double mmMapUnitScale
15491549
Q_UNUSED( layerName );
15501550
Q_UNUSED( shift ); //todo...
15511551

1552-
QSvgRenderer r( mPath );
1553-
if ( !r.isValid() )
1554-
{
1555-
return false;
1556-
}
1557-
1558-
QgsDxfPaintDevice pd( &e );
1559-
pd.setDrawingSize( QSizeF( r.defaultSize() ) );
1560-
15611552
//size
15621553
double size = mSize;
15631554

@@ -1616,6 +1607,50 @@ bool QgsSvgMarkerSymbolLayerV2::writeDxf( QgsDxfExport& e, double mmMapUnitScale
16161607
if ( angle )
16171608
outputOffset = _rotatedOffset( outputOffset, angle );
16181609

1610+
QString path = mPath;
1611+
if ( hasDataDefinedProperty( QgsSymbolLayerV2::EXPR_NAME ) )
1612+
{
1613+
path = evaluateDataDefinedProperty( QgsSymbolLayerV2::EXPR_NAME, f, mPath ).toString();
1614+
}
1615+
1616+
double outlineWidth = mOutlineWidth;
1617+
if ( hasDataDefinedProperty( QgsSymbolLayerV2::EXPR_OUTLINE_WIDTH ) )
1618+
{
1619+
outlineWidth = evaluateDataDefinedProperty( QgsSymbolLayerV2::EXPR_OUTLINE_WIDTH, f, mOutlineWidth ).toDouble();
1620+
}
1621+
1622+
QColor fillColor = mFillColor;
1623+
if ( hasDataDefinedProperty( QgsSymbolLayerV2::EXPR_FILL ) )
1624+
{
1625+
QString colorString = evaluateDataDefinedProperty( QgsSymbolLayerV2::EXPR_FILL, f, QVariant(), &ok ).toString();
1626+
if ( ok )
1627+
fillColor = QgsSymbolLayerV2Utils::decodeColor( colorString );
1628+
}
1629+
1630+
QColor outlineColor = mOutlineColor;
1631+
if ( hasDataDefinedProperty( QgsSymbolLayerV2::EXPR_OUTLINE ) )
1632+
{
1633+
QString colorString = evaluateDataDefinedProperty( QgsSymbolLayerV2::EXPR_OUTLINE, f, QVariant(), &ok ).toString();
1634+
if ( ok )
1635+
outlineColor = QgsSymbolLayerV2Utils::decodeColor( colorString );
1636+
}
1637+
1638+
const QByteArray &svgContent = QgsSvgCache::instance()->svgContent( path, size, fillColor, outlineColor, outlineWidth,
1639+
context->renderContext().scaleFactor(),
1640+
context->renderContext().rasterScaleFactor() );
1641+
1642+
//if current entry image is 0: cache image for entry
1643+
// checks to see if image will fit into cache
1644+
//update stats for memory usage
1645+
QSvgRenderer r( svgContent );
1646+
if ( !r.isValid() )
1647+
{
1648+
return false;
1649+
}
1650+
1651+
QgsDxfPaintDevice pd( &e );
1652+
pd.setDrawingSize( QSizeF( r.defaultSize() ) );
1653+
16191654
QPainter p;
16201655
p.begin( &pd );
16211656
if ( !qgsDoubleNear( angle, 0.0 ) )

src/core/symbology-ng/qgssvgcache.cpp

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,33 @@
3636
#include <QNetworkReply>
3737
#include <QNetworkRequest>
3838

39-
QgsSvgCacheEntry::QgsSvgCacheEntry(): file( QString() ), size( 0.0 ), outlineWidth( 0 ), widthScaleFactor( 1.0 ), rasterScaleFactor( 1.0 ), fill( Qt::black ),
40-
outline( Qt::black ), image( 0 ), picture( 0 ), nextEntry( 0 ), previousEntry( 0 )
39+
QgsSvgCacheEntry::QgsSvgCacheEntry()
40+
: file( QString() )
41+
, size( 0.0 )
42+
, outlineWidth( 0 )
43+
, widthScaleFactor( 1.0 )
44+
, rasterScaleFactor( 1.0 )
45+
, fill( Qt::black )
46+
, outline( Qt::black )
47+
, image( 0 )
48+
, picture( 0 )
49+
, nextEntry( 0 )
50+
, previousEntry( 0 )
4151
{
4252
}
4353

44-
QgsSvgCacheEntry::QgsSvgCacheEntry( const QString& f, double s, double ow, double wsf, double rsf, const QColor& fi, const QColor& ou ): file( f ), size( s ), outlineWidth( ow ),
45-
widthScaleFactor( wsf ), rasterScaleFactor( rsf ), fill( fi ), outline( ou ), image( 0 ), picture( 0 ), nextEntry( 0 ), previousEntry( 0 )
54+
QgsSvgCacheEntry::QgsSvgCacheEntry( const QString& f, double s, double ow, double wsf, double rsf, const QColor& fi, const QColor& ou )
55+
: file( f )
56+
, size( s )
57+
, outlineWidth( ow )
58+
, widthScaleFactor( wsf )
59+
, rasterScaleFactor( rsf )
60+
, fill( fi )
61+
, outline( ou )
62+
, image( 0 )
63+
, picture( 0 )
64+
, nextEntry( 0 )
65+
, previousEntry( 0 )
4666
{
4767
}
4868

@@ -55,8 +75,8 @@ QgsSvgCacheEntry::~QgsSvgCacheEntry()
5575

5676
bool QgsSvgCacheEntry::operator==( const QgsSvgCacheEntry& other ) const
5777
{
58-
return ( other.file == file && other.size == size && other.outlineWidth == outlineWidth && other.widthScaleFactor == widthScaleFactor
59-
&& other.rasterScaleFactor == rasterScaleFactor && other.fill == fill && other.outline == outline );
78+
return other.file == file && other.size == size && other.outlineWidth == outlineWidth && other.widthScaleFactor == widthScaleFactor
79+
&& other.rasterScaleFactor == rasterScaleFactor && other.fill == fill && other.outline == outline;
6080
}
6181

6282
int QgsSvgCacheEntry::dataSize() const
@@ -73,14 +93,6 @@ int QgsSvgCacheEntry::dataSize() const
7393
return size;
7494
}
7595

76-
QString file;
77-
double size;
78-
double outlineWidth;
79-
double widthScaleFactor;
80-
double rasterScaleFactor;
81-
QColor fill;
82-
QColor outline;
83-
8496
QgsSvgCache* QgsSvgCache::instance()
8597
{
8698
static QgsSvgCache mInstance;
@@ -169,6 +181,16 @@ const QPicture& QgsSvgCache::svgAsPicture( const QString& file, double size, con
169181
return *( currentEntry->picture );
170182
}
171183

184+
const QByteArray& QgsSvgCache::svgContent( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
185+
double widthScaleFactor, double rasterScaleFactor )
186+
{
187+
QMutexLocker locker( &mMutex );
188+
189+
QgsSvgCacheEntry *currentEntry = cacheEntry( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );
190+
191+
return currentEntry->svgContent;
192+
}
193+
172194
QgsSvgCacheEntry* QgsSvgCache::insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
173195
double widthScaleFactor, double rasterScaleFactor )
174196
{

src/core/symbology-ng/qgssvgcache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ class CORE_EXPORT QgsSvgCache : public QObject
112112
/**Get image data*/
113113
QByteArray getImageData( const QString &path ) const;
114114

115+
/**Get SVG content*/
116+
const QByteArray& svgContent( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
117+
double widthScaleFactor, double rasterScaleFactor );
118+
115119
signals:
116120
/** Emit a signal to be caught by qgisapp and display a msg on status bar */
117121
void statusChanged( const QString& theStatusQString );

0 commit comments

Comments
 (0)