26using namespace Qt::StringLiterals;
29 : QPaintEngine( QPaintEngine::AllFeatures )
30 , mPaintDevice( dxfDevice )
48 return QPaintEngine::User;
60 if ( state.state() & QPaintEngine::DirtyTransform )
61 mTransform = state.transform();
63 if ( state.state() & QPaintEngine::DirtyPen )
66 if ( state.state() & QPaintEngine::DirtyBrush )
67 mBrush = state.brush();
69 if ( state.state() & QPaintEngine::DirtyOpacity )
71 mOpacity = state.opacity();
75void QgsDxfPaintEngine::setRing(
QgsPointSequence &polyline,
const QPointF *points,
int pointCount )
78 for (
int i = 0; i < pointCount; ++i )
79 polyline.append( toDxfCoordinates( points[i] ) );
85 if ( !mDxf || !mPaintDevice )
90 setRing( polygon.last(), points, pointCount );
92 if ( mode == QPaintEngine::PolylineMode )
94 if ( mPen.style() != Qt::NoPen && mPen.brush().style() != Qt::NoBrush )
95 mDxf->writePolyline( polygon.at( 0 ), mLayer, u
"CONTINUOUS"_s, penColor(), currentWidth() );
99 if ( mBrush.style() != Qt::NoBrush )
100 mDxf->writePolygon( polygon, mLayer, u
"SOLID"_s, brushColor() );
106 const int pathLength = path.elementCount();
107 for (
int i = 0; i < pathLength; ++i )
109 const QPainterPath::Element &pathElem = path.elementAt( i );
110 if ( pathElem.type == QPainterPath::MoveToElement )
112 moveTo( pathElem.x, pathElem.y );
114 else if ( pathElem.type == QPainterPath::LineToElement )
116 lineTo( pathElem.x, pathElem.y );
118 else if ( pathElem.type == QPainterPath::CurveToElement )
120 curveTo( pathElem.x, pathElem.y );
122 else if ( pathElem.type == QPainterPath::CurveToDataElement )
124 mCurrentCurve.append( QPointF( pathElem.x, pathElem.y ) );
130 if ( !mPolygon.isEmpty() && mBrush.style() != Qt::NoBrush )
131 mDxf->writePolygon( mPolygon, mLayer, u
"SOLID"_s, brushColor() );
136void QgsDxfPaintEngine::moveTo(
double dx,
double dy )
140 mCurrentPolygon.append( QPointF( dx, dy ) );
143void QgsDxfPaintEngine::lineTo(
double dx,
double dy )
146 mCurrentPolygon.append( QPointF( dx, dy ) );
149void QgsDxfPaintEngine::curveTo(
double dx,
double dy )
152 if ( !mCurrentPolygon.isEmpty() )
153 mCurrentCurve.append( mCurrentPolygon.last() );
155 mCurrentCurve.append( QPointF( dx, dy ) );
158void QgsDxfPaintEngine::endPolygon()
160 if ( mCurrentPolygon.size() > 1 )
162 if ( mPen.style() != Qt::NoPen )
163 drawPolygon( mCurrentPolygon.constData(), mCurrentPolygon.size(), QPaintEngine::PolylineMode );
166 setRing( mPolygon.last(), mCurrentPolygon.constData(), mCurrentPolygon.size() );
168 mCurrentPolygon.clear();
171void QgsDxfPaintEngine::endCurve()
173 if ( mCurrentCurve.empty() )
176 if ( mCurrentPolygon.empty() )
178 mCurrentCurve.clear();
182 if ( mCurrentCurve.size() >= 3 )
185 for (
int i = 1; i <= 20; ++i )
187 mCurrentPolygon.append( bezierPoint( mCurrentCurve, t ) );
191 else if ( mCurrentCurve.size() == 2 )
193 mCurrentPolygon.append( mCurrentCurve.at( 1 ) );
195 mCurrentCurve.clear();
200 if ( !mDxf || !mPaintDevice || !lines || mPen.style() == Qt::NoPen )
203 for (
int i = 0; i < lineCount; ++i )
205 mDxf->writeLine( toDxfCoordinates( lines[i].p1() ),
206 toDxfCoordinates( lines[i].p2() ),
207 mLayer, u
"CONTINUOUS"_s, penColor(), currentWidth() );
211QgsPoint QgsDxfPaintEngine::toDxfCoordinates( QPointF pt )
const
213 if ( !mPaintDevice || !mDxf )
216 const QPointF dxfPt = mPaintDevice->
dxfCoordinates( mTransform.map( pt ) ) + mShift;
217 return QgsPoint( dxfPt.x(), dxfPt.y() );
221double QgsDxfPaintEngine::currentWidth()
const
226 return mPen.widthF() * mPaintDevice->widthScaleFactor();
229QPointF QgsDxfPaintEngine::bezierPoint(
const QList<QPointF> &controlPolygon,
double t )
233 const int cPolySize = controlPolygon.size();
236 QList<QPointF>::const_iterator it = controlPolygon.constBegin();
238 for ( ; it != controlPolygon.constEnd(); ++it )
240 bPoly = bernsteinPoly( cPolySize - 1, i, t );
241 x += ( it->x() * bPoly );
242 y += ( it->y() * bPoly );
246 return QPointF( x, y );
249double QgsDxfPaintEngine::bernsteinPoly(
int n,
int i,
double t )
254 return lower( n, i ) * power( t, i ) * power( ( 1 - t ), ( n - i ) );
257int QgsDxfPaintEngine::lower(
int n,
int i )
259 if ( i >= 0 && i <= n )
261 return faculty( n ) / ( faculty( i ) * faculty( n - i ) );
269double QgsDxfPaintEngine::power(
double a,
int b )
274 const double tmp = a;
275 for (
int i = 2; i <= std::abs( b ); i++ )
284int QgsDxfPaintEngine::faculty(
int n )
292 if ( n == 0 || n == 1 )
295 for ( i = n - 1; i >= 2; i-- )
301QColor QgsDxfPaintEngine::penColor()
const
307 QColor
c = mPen.color();
308 c.setAlphaF(
c.alphaF() * mOpacity );
312QColor QgsDxfPaintEngine::brushColor()
const
316 return mBrush.color();
318 QColor
c = mBrush.color();
319 c.setAlphaF(
c.alphaF() * mOpacity );
Exports QGIS layers to the DXF format.
A paint device for drawing into dxf files.
QPointF dxfCoordinates(QPointF pt) const
Converts a point from device coordinates to dxf coordinates.
void drawPath(const QPainterPath &path) override
bool begin(QPaintDevice *pdev) override
QgsDxfPaintEngine(const QgsDxfPaintDevice *dxfDevice, QgsDxfExport *dxf)
void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr) override
void updateState(const QPaintEngineState &state) override
void drawLines(const QLineF *lines, int lineCount) override
QPaintEngine::Type type() const override
void drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode) override
Point geometry type, with support for z-dimension and m-values.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
QVector< QgsPointSequence > QgsRingSequence
QVector< QgsPoint > QgsPointSequence