Skip to content

Commit 99c6875

Browse files
committed
Better conversion of QPainterPath to dxf
1 parent 5be7a94 commit 99c6875

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/core/dxf/qgsdxfpaintengine.cpp

+51-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ void QgsDxfPaintEngine::updateState( const QPaintEngineState& state )
6666

6767
void QgsDxfPaintEngine::drawPolygon( const QPointF* points, int pointCount, PolygonDrawMode mode )
6868
{
69+
Q_UNUSED( mode );
6970
if ( !mDxf || !mPaintDevice )
7071
{
7172
return;
@@ -77,7 +78,8 @@ void QgsDxfPaintEngine::drawPolygon( const QPointF* points, int pointCount, Poly
7778
polyline[i] = toDxfCoordinates( points[i] );
7879
}
7980

80-
mDxf->writePolyline( polyline, mLayer, "CONTINUOUS", currentPenColor(), currentWidth(), mode != QPaintEngine::PolylineMode );
81+
bool closed = ( pointCount > 3 && points[0] == points[pointCount - 1] );
82+
mDxf->writePolyline( polyline, mLayer, "CONTINUOUS", currentPenColor(), currentWidth(), closed );
8183
}
8284

8385
void QgsDxfPaintEngine::drawRects( const QRectF* rects, int rectCount )
@@ -116,12 +118,59 @@ void QgsDxfPaintEngine::drawEllipse( const QRectF& rect )
116118

117119
void QgsDxfPaintEngine::drawPath( const QPainterPath& path )
118120
{
119-
QList<QPolygonF> polygonList = path.toFillPolygons();
121+
/*QList<QPolygonF> polygonList = path.toFillPolygons();
120122
QList<QPolygonF>::const_iterator pIt = polygonList.constBegin();
121123
for ( ; pIt != polygonList.constEnd(); ++pIt )
122124
{
123125
drawPolygon( pIt->constData(), pIt->size(), pIt->isClosed() ? QPaintEngine::OddEvenMode : QPaintEngine::PolylineMode );
126+
}*/
127+
128+
int pathLength = path.elementCount();
129+
for ( int i = 0; i < pathLength; ++i )
130+
{
131+
const QPainterPath::Element& pathElem = path.elementAt( i );
132+
if ( pathElem.isMoveTo() )
133+
{
134+
moveTo( pathElem.x, pathElem.y );
135+
}
136+
else if ( pathElem.isLineTo() )
137+
{
138+
lineTo( pathElem.x, pathElem.y );
139+
}
140+
else if ( pathElem.isCurveTo() )
141+
{
142+
curveTo( pathElem.x, pathElem.y );
143+
}
144+
}
145+
endPolygon();
146+
}
147+
148+
void QgsDxfPaintEngine::moveTo( double dx, double dy )
149+
{
150+
if ( mCurrentPolygon.size() < 0 )
151+
{
152+
endPolygon();
153+
}
154+
mCurrentPolygon.append( QPointF( dx, dy ) );
155+
}
156+
157+
void QgsDxfPaintEngine::lineTo( double dx, double dy )
158+
{
159+
mCurrentPolygon.append( QPointF( dx, dy ) );
160+
}
161+
162+
void QgsDxfPaintEngine::curveTo( double dx, double dy )
163+
{
164+
mCurrentPolygon.append( QPointF( dx, dy ) ); //todo...
165+
}
166+
167+
void QgsDxfPaintEngine::endPolygon()
168+
{
169+
if ( mCurrentPolygon.size() > 1 )
170+
{
171+
drawPolygon( mCurrentPolygon.constData(), mCurrentPolygon.size(), QPaintEngine::OddEvenMode );
124172
}
173+
mCurrentPolygon.clear();
125174
}
126175

127176
void QgsDxfPaintEngine::drawLines( const QLineF* lines, int lineCount )

src/core/dxf/qgsdxfpaintengine.h

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class CORE_EXPORT QgsDxfPaintEngine: public QPaintEngine
4848

4949
void setShift( const QPointF& shift ) { mShift = shift; }
5050

51+
void moveTo( double dx, double dy );
52+
void lineTo( double dx, double dy );
53+
void curveTo( double dx, double dy );
54+
void endPolygon();
55+
5156
private:
5257
const QgsDxfPaintDevice* mPaintDevice;
5358
QgsDxfExport* mDxf;
@@ -57,6 +62,7 @@ class CORE_EXPORT QgsDxfPaintEngine: public QPaintEngine
5762
QPen mPen;
5863
QString mLayer;
5964
QPointF mShift;
65+
QPolygonF mCurrentPolygon;
6066

6167
QgsPoint toDxfCoordinates( const QPointF& pt ) const;
6268
int currentPenColor() const;

0 commit comments

Comments
 (0)