From a48f4bda777cbceb2d5177a68fb046cfae6e04b8 Mon Sep 17 00:00:00 2001 From: wonder Date: Thu, 14 Jan 2010 17:50:44 +0000 Subject: [PATCH] QgsSymbolV2RenderContext: - use reference to QgsRenderContext instead of pointer - added utility functions for simpler syntax when converting values to output units git-svn-id: http://svn.osgeo.org/qgis/trunk@12756 c8812cc2-4d05-0410-92ff-de0c093fc19c --- python/core/symbology-ng-core.sip | 17 +++++-- .../symbology-ng/qgsfillsymbollayerv2.cpp | 9 +--- .../symbology-ng/qgslinesymbollayerv2.cpp | 46 +++++-------------- .../symbology-ng/qgsmarkersymbollayerv2.cpp | 43 ++++++----------- .../symbology-ng/qgssymbollayerv2utils.cpp | 23 +++------- src/core/symbology-ng/qgssymbollayerv2utils.h | 4 +- src/core/symbology-ng/qgssymbolv2.cpp | 25 +++++++--- src/core/symbology-ng/qgssymbolv2.h | 11 +++-- 8 files changed, 75 insertions(+), 103 deletions(-) diff --git a/python/core/symbology-ng-core.sip b/python/core/symbology-ng-core.sip index 86f17af1a69a..24484e087afe 100644 --- a/python/core/symbology-ng-core.sip +++ b/python/core/symbology-ng-core.sip @@ -444,14 +444,17 @@ class QgsSymbolV2RenderContext %End public: - QgsSymbolV2RenderContext( QgsRenderContext* c, QgsSymbolV2::OutputUnit u); + QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u); ~QgsSymbolV2RenderContext(); - QgsRenderContext* renderContext(); - void setRenderContext( QgsRenderContext* c ); + QgsRenderContext& renderContext(); + //void setRenderContext( QgsRenderContext* c ); QgsSymbolV2::OutputUnit outputUnit() const; void setOutputUnit( QgsSymbolV2::OutputUnit u ); + + double outputLineWidth(double width) const; + double outputPixelSize(double size) const; }; /////////////// @@ -605,6 +608,10 @@ typedef QMap QgsStringMap; ////////// +//typedef QgsSymbolLayerV2 * ( * QgsSymbolLayerV2CreateFunc )( const QgsStringMap& ); +//typedef QgsSymbolLayerV2Widget*( *QgsSymbolLayerV2WidgetFunc )(); + + class QgsSymbolLayerV2Metadata { %TypeHeaderCode @@ -623,8 +630,8 @@ public: QString name() const; QgsSymbolV2::SymbolType type(); - // TODO QgsSymbolLayerV2CreateFunc createFunction() const { return mCreateFunc; } - // TODO QgsSymbolLayerV2WidgetFunc widgetFunction() const { return mWidgetFunc; } + // TODO QgsSymbolLayerV2CreateFunc createFunction() const; + // TODO QgsSymbolLayerV2WidgetFunc widgetFunction() const; }; diff --git a/src/core/symbology-ng/qgsfillsymbollayerv2.cpp b/src/core/symbology-ng/qgsfillsymbollayerv2.cpp index 77b47815a5c9..4c90c26eb550 100644 --- a/src/core/symbology-ng/qgsfillsymbollayerv2.cpp +++ b/src/core/symbology-ng/qgsfillsymbollayerv2.cpp @@ -46,7 +46,7 @@ void QgsSimpleFillSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context mBrush = QBrush( mColor, mBrushStyle ); mPen = QPen( mBorderColor ); mPen.setStyle( mBorderStyle ); - mPen.setWidthF( mBorderWidth * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) ); + mPen.setWidthF( context.outputLineWidth( mBorderWidth ) ); } void QgsSimpleFillSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context ) @@ -55,12 +55,7 @@ void QgsSimpleFillSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context ) void QgsSimpleFillSymbolLayerV2::renderPolygon( const QPolygonF& points, QList* rings, QgsSymbolV2RenderContext& context ) { - QgsRenderContext* rc = context.renderContext(); - if ( !rc ) - { - return; - } - QPainter* p = rc->painter(); + QPainter* p = context.renderContext().painter(); if ( !p ) { return; diff --git a/src/core/symbology-ng/qgslinesymbollayerv2.cpp b/src/core/symbology-ng/qgslinesymbollayerv2.cpp index f6bbd06c33a3..aff4923af535 100644 --- a/src/core/symbology-ng/qgslinesymbollayerv2.cpp +++ b/src/core/symbology-ng/qgslinesymbollayerv2.cpp @@ -50,7 +50,7 @@ QString QgsSimpleLineSymbolLayerV2::layerType() const void QgsSimpleLineSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context ) { mPen.setColor( mColor ); - mPen.setWidthF( mWidth * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) ); + mPen.setWidthF( context.outputLineWidth( mWidth ) ); mPen.setStyle( mPenStyle ); mPen.setJoinStyle( mPenJoinStyle ); mPen.setCapStyle( mPenCapStyle ); @@ -62,12 +62,7 @@ void QgsSimpleLineSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context ) void QgsSimpleLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSymbolV2RenderContext& context ) { - QgsRenderContext* rc = context.renderContext(); - if ( !rc ) - { - return; - } - QPainter* p = rc->painter(); + QPainter* p = context.renderContext().painter(); if ( !p ) { return; @@ -216,11 +211,7 @@ void QgsMarkerLineSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context // if being rotated, it gets initialized with every line segment if ( !mRotateMarker ) { - QgsRenderContext* rc = context.renderContext(); - if ( rc ) - { - mMarker->startRender( *rc ); - } + mMarker->startRender( context.renderContext() ); } } @@ -228,11 +219,7 @@ void QgsMarkerLineSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context ) { if ( !mRotateMarker ) { - QgsRenderContext* rc = context.renderContext(); - if ( rc ) - { - mMarker->stopRender( *rc ); - } + mMarker->stopRender( context.renderContext() ); } } @@ -244,7 +231,7 @@ void QgsMarkerLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSym } else { - QPolygonF points2 = ::offsetLine( points, mOffset * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) ); + QPolygonF points2 = ::offsetLine( points, context.outputLineWidth( mOffset ) ); renderPolylineNoOffset( points2, context ); } } @@ -256,13 +243,9 @@ void QgsMarkerLineSymbolLayerV2::renderPolylineNoOffset( const QPolygonF& points bool first = true; double origAngle = mMarker->angle(); - double painterUnitInterval = mInterval * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ); + double painterUnitInterval = context.outputLineWidth( mInterval ); - QgsRenderContext* rc = context.renderContext(); - if ( !rc ) - { - return; - } + QgsRenderContext& rc = context.renderContext(); for ( int i = 1; i < points.count(); ++i ) { @@ -285,13 +268,13 @@ void QgsMarkerLineSymbolLayerV2::renderPolylineNoOffset( const QPolygonF& points if ( mRotateMarker ) { mMarker->setAngle( origAngle + ( l.angle() * 180 / M_PI ) ); - mMarker->startRender( *rc ); + mMarker->startRender( rc ); } // draw first marker if ( first ) { - mMarker->renderPoint( lastPt, *rc ); + mMarker->renderPoint( lastPt, rc ); first = false; } @@ -301,14 +284,14 @@ void QgsMarkerLineSymbolLayerV2::renderPolylineNoOffset( const QPolygonF& points // "c" is 1 for regular point or in interval (0,1] for begin of line segment lastPt += c * diff; lengthLeft -= painterUnitInterval; - mMarker->renderPoint( lastPt, *rc ); + mMarker->renderPoint( lastPt, rc ); c = 1; // reset c (if wasn't 1 already) } lastPt = pt; if ( mRotateMarker ) - mMarker->stopRender( *rc ); + mMarker->stopRender( rc ); } // restore original rotation @@ -404,12 +387,7 @@ void QgsLineDecorationSymbolLayerV2::renderPolyline( const QPolygonF& points, Qg { // draw arrow at the end of line - QgsRenderContext* rc = context.renderContext(); - if ( !rc ) - { - return; - } - QPainter* p = rc->painter(); + QPainter* p = context.renderContext().painter(); if ( !p ) { return; diff --git a/src/core/symbology-ng/qgsmarkersymbollayerv2.cpp b/src/core/symbology-ng/qgsmarkersymbollayerv2.cpp index efd68d7b4e93..770ce3670dea 100644 --- a/src/core/symbology-ng/qgsmarkersymbollayerv2.cpp +++ b/src/core/symbology-ng/qgsmarkersymbollayerv2.cpp @@ -67,11 +67,11 @@ void QgsSimpleMarkerSymbolLayerV2::startRender( QgsSymbolV2RenderContext& contex { mBrush = QBrush( mColor ); mPen = QPen( mBorderColor ); - mPen.setWidthF( mPen.widthF() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) ); + mPen.setWidthF( context.outputLineWidth( mPen.widthF() ) ); mPolygon.clear(); - double scaledSize = mSize * QgsSymbolLayerV2Utils::pixelSizeScaleFactor( context.renderContext(), context.outputUnit() ); + double scaledSize = context.outputPixelSize( mSize ); double half = scaledSize / 2.0; if ( mName == "rectangle" ) @@ -183,12 +183,8 @@ void QgsSimpleMarkerSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context void QgsSimpleMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context ) { - QgsRenderContext* rc = context.renderContext(); - if ( !rc ) - { - return; - } - QPainter* p = rc->painter(); + QgsRenderContext& rc = context.renderContext(); + QPainter* p = rc.painter(); if ( !p ) { return; @@ -202,9 +198,9 @@ void QgsSimpleMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV //drawMarker(p); //mCache.save("/home/marco/tmp/marker.png", "PNG"); - double s = mCache.width() / context.renderContext()->rasterScaleFactor(); - p->drawImage( QRectF( point.x() - s / 2.0 + mOffset.x() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ), \ - point.y() - s / 2.0 + mOffset.y() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ), \ + double s = mCache.width() / context.renderContext().rasterScaleFactor(); + p->drawImage( QRectF( context.outputLineWidth( point.x() - s / 2.0 + mOffset.x() ), + context.outputLineWidth( point.y() - s / 2.0 + mOffset.y() ), s, s ), mCache ); //p->restore(); } @@ -237,7 +233,7 @@ void QgsSimpleMarkerSymbolLayerV2::drawMarker( QPainter* p, QgsSymbolV2RenderCon } else { - double scaledSize = mSize * QgsSymbolLayerV2Utils::pixelSizeScaleFactor( context.renderContext(), context.outputUnit() ); + double scaledSize = context.outputPixelSize( mSize ); double half = scaledSize / 2.0; // TODO: rotate @@ -300,21 +296,16 @@ QString QgsSvgMarkerSymbolLayerV2::layerType() const void QgsSvgMarkerSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context ) { double pictureSize = 0; - QgsRenderContext* rc = context.renderContext(); - if ( !rc ) - { - return; - } + QgsRenderContext& rc = context.renderContext(); - if ( rc->painter() && rc->painter()->device() ) + if ( rc.painter() && rc.painter()->device() ) { //correct QPictures DPI correction - pictureSize = mSize * QgsSymbolLayerV2Utils::lineWidthScaleFactor( rc, context.outputUnit() ) \ - / rc->painter()->device()->logicalDpiX() * mPicture.logicalDpiX(); + pictureSize = context.outputLineWidth( mSize ) / rc.painter()->device()->logicalDpiX() * mPicture.logicalDpiX(); } else { - pictureSize = mSize * QgsSymbolLayerV2Utils::lineWidthScaleFactor( rc, context.outputUnit() ); + pictureSize = context.outputLineWidth( mSize ); } QRectF rect( QPointF( -pictureSize / 2.0, -pictureSize / 2.0 ), QSizeF( pictureSize, pictureSize ) ); QSvgRenderer renderer( mPath ); @@ -329,19 +320,15 @@ void QgsSvgMarkerSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context ) void QgsSvgMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context ) { - QgsRenderContext* rc = context.renderContext(); - if ( !rc ) - { - return; - } - QPainter* p = rc->painter(); + QPainter* p = context.renderContext().painter(); if ( !p ) { return; } p->save(); - p->translate( point + mOffset * QgsSymbolLayerV2Utils::lineWidthScaleFactor( rc, context.outputUnit() ) ); + QPointF outputOffset = QPointF(context.outputLineWidth( mOffset.x() ), context.outputLineWidth( mOffset.y() ) ); + p->translate( point + outputOffset ); if ( mAngle != 0 ) p->rotate( mAngle ); diff --git a/src/core/symbology-ng/qgssymbollayerv2utils.cpp b/src/core/symbology-ng/qgssymbollayerv2utils.cpp index f7cd1e2c9f74..733b7353b05a 100644 --- a/src/core/symbology-ng/qgssymbollayerv2utils.cpp +++ b/src/core/symbology-ng/qgssymbollayerv2utils.cpp @@ -175,7 +175,7 @@ QIcon QgsSymbolLayerV2Utils::symbolLayerPreviewIcon( QgsSymbolLayerV2* layer, Qg painter.eraseRect( QRect( QPoint( 0, 0 ), size ) ); QgsRenderContext renderContext; renderContext.setPainter( &painter ); - QgsSymbolV2RenderContext symbolContext( &renderContext, u ); + QgsSymbolV2RenderContext symbolContext( renderContext, u ); layer->drawPreviewIcon( symbolContext, size ); painter.end(); return QIcon( pixmap ); @@ -612,20 +612,16 @@ QDomElement QgsSymbolLayerV2Utils::saveColorRamp( QString name, QgsVectorColorRa return rampEl; } -double QgsSymbolLayerV2Utils::lineWidthScaleFactor( QgsRenderContext* c, QgsSymbolV2::OutputUnit u ) +double QgsSymbolLayerV2Utils::lineWidthScaleFactor( QgsRenderContext& c, QgsSymbolV2::OutputUnit u ) { - if ( !c ) - { - return 1.0; - } if ( u == QgsSymbolV2::MM ) { - return c->scaleFactor(); + return c.scaleFactor(); } else //QgsSymbol::MapUnit { - double mup = c->mapToPixel().mapUnitsPerPixel(); + double mup = c.mapToPixel().mapUnitsPerPixel(); if ( mup > 0 ) { return 1.0 / mup; @@ -637,19 +633,14 @@ double QgsSymbolLayerV2Utils::lineWidthScaleFactor( QgsRenderContext* c, QgsSymb } } -double QgsSymbolLayerV2Utils::pixelSizeScaleFactor( QgsRenderContext* c, QgsSymbolV2::OutputUnit u ) +double QgsSymbolLayerV2Utils::pixelSizeScaleFactor( QgsRenderContext& c, QgsSymbolV2::OutputUnit u ) { - if ( !c ) - { - return 1.0; - } - if ( u == QgsSymbolV2::MM ) { - return ( c->scaleFactor() * c->rasterScaleFactor() ); + return ( c.scaleFactor() * c.rasterScaleFactor() ); } else //QgsSymbol::MapUnit { - return c->rasterScaleFactor() / c->mapToPixel().mapUnitsPerPixel(); + return c.rasterScaleFactor() / c.mapToPixel().mapUnitsPerPixel(); } } diff --git a/src/core/symbology-ng/qgssymbollayerv2utils.h b/src/core/symbology-ng/qgssymbollayerv2utils.h index a6b3fd5306bf..69468fd403bc 100644 --- a/src/core/symbology-ng/qgssymbollayerv2utils.h +++ b/src/core/symbology-ng/qgssymbollayerv2utils.h @@ -67,9 +67,9 @@ class CORE_EXPORT QgsSymbolLayerV2Utils static QDomElement saveColorRamp( QString name, QgsVectorColorRampV2* ramp, QDomDocument& doc ); /**Returns the line width scale factor depending on the unit and the paint device*/ - static double lineWidthScaleFactor( QgsRenderContext* c, QgsSymbolV2::OutputUnit u ); + static double lineWidthScaleFactor( QgsRenderContext& c, QgsSymbolV2::OutputUnit u ); /**Returns scale factor painter units -> pixel dimensions*/ - static double pixelSizeScaleFactor( QgsRenderContext* c, QgsSymbolV2::OutputUnit u ); + static double pixelSizeScaleFactor( QgsRenderContext& c, QgsSymbolV2::OutputUnit u ); }; class QPolygonF; diff --git a/src/core/symbology-ng/qgssymbolv2.cpp b/src/core/symbology-ng/qgssymbolv2.cpp index 2b480c8ebd81..0098729227b1 100644 --- a/src/core/symbology-ng/qgssymbolv2.cpp +++ b/src/core/symbology-ng/qgssymbolv2.cpp @@ -126,14 +126,14 @@ bool QgsSymbolV2::changeSymbolLayer( int index, QgsSymbolLayerV2* layer ) void QgsSymbolV2::startRender( QgsRenderContext& context ) { - QgsSymbolV2RenderContext symbolContext( &context, mOutputUnit ); + QgsSymbolV2RenderContext symbolContext( context, mOutputUnit ); for ( QgsSymbolLayerV2List::iterator it = mLayers.begin(); it != mLayers.end(); ++it ) ( *it )->startRender( symbolContext ); } void QgsSymbolV2::stopRender( QgsRenderContext& context ) { - QgsSymbolV2RenderContext symbolContext( &context, mOutputUnit ); + QgsSymbolV2RenderContext symbolContext( context, mOutputUnit ); for ( QgsSymbolLayerV2List::iterator it = mLayers.begin(); it != mLayers.end(); ++it ) ( *it )->stopRender( symbolContext ); } @@ -162,7 +162,7 @@ void QgsSymbolV2::drawPreviewIcon( QPainter* painter, QSize size ) { QgsRenderContext context; context.setPainter( painter ); - QgsSymbolV2RenderContext symbolContext( &context, mOutputUnit ); + QgsSymbolV2RenderContext symbolContext( context, mOutputUnit ); for ( QgsSymbolLayerV2List::iterator it = mLayers.begin(); it != mLayers.end(); ++it ) { ( *it )->drawPreviewIcon( symbolContext, size ); @@ -247,7 +247,8 @@ QgsSymbolLayerV2List QgsSymbolV2::cloneLayers() const //////////////////// -QgsSymbolV2RenderContext::QgsSymbolV2RenderContext( QgsRenderContext* c, QgsSymbolV2::OutputUnit u ): mRenderContext( c ), mOutputUnit( u ) +QgsSymbolV2RenderContext::QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u ) + : mRenderContext( c ), mOutputUnit( u ) { } @@ -257,6 +258,16 @@ QgsSymbolV2RenderContext::~QgsSymbolV2RenderContext() } +double QgsSymbolV2RenderContext::outputLineWidth(double width) const +{ + return width * QgsSymbolLayerV2Utils::lineWidthScaleFactor( mRenderContext, mOutputUnit ); +} + +double QgsSymbolV2RenderContext::outputPixelSize(double size) const +{ + return size * QgsSymbolLayerV2Utils::pixelSizeScaleFactor( mRenderContext, mOutputUnit ); +} + /////////////////// @@ -323,7 +334,7 @@ double QgsMarkerSymbolV2::size() void QgsMarkerSymbolV2::renderPoint( const QPointF& point, QgsRenderContext& context, int layer ) { - QgsSymbolV2RenderContext symbolContext( &context, mOutputUnit ); + QgsSymbolV2RenderContext symbolContext( context, mOutputUnit ); if ( layer != -1 ) { if ( layer >= 0 && layer < mLayers.count() ) @@ -391,7 +402,7 @@ double QgsLineSymbolV2::width() void QgsLineSymbolV2::renderPolyline( const QPolygonF& points, QgsRenderContext& context, int layer ) { - QgsSymbolV2RenderContext symbolContext( &context, mOutputUnit ); + QgsSymbolV2RenderContext symbolContext( context, mOutputUnit ); if ( layer != -1 ) { if ( layer >= 0 && layer < mLayers.count() ) @@ -426,7 +437,7 @@ QgsFillSymbolV2::QgsFillSymbolV2( QgsSymbolLayerV2List layers ) void QgsFillSymbolV2::renderPolygon( const QPolygonF& points, QList* rings, QgsRenderContext& context, int layer ) { - QgsSymbolV2RenderContext symbolContext( &context, mOutputUnit ); + QgsSymbolV2RenderContext symbolContext( context, mOutputUnit ); if ( layer != -1 ) { if ( layer >= 0 && layer < mLayers.count() ) diff --git a/src/core/symbology-ng/qgssymbolv2.h b/src/core/symbology-ng/qgssymbolv2.h index e9dc0870401b..fc3333fca406 100644 --- a/src/core/symbology-ng/qgssymbolv2.h +++ b/src/core/symbology-ng/qgssymbolv2.h @@ -97,17 +97,20 @@ class CORE_EXPORT QgsSymbolV2 class CORE_EXPORT QgsSymbolV2RenderContext { public: - QgsSymbolV2RenderContext( QgsRenderContext* c, QgsSymbolV2::OutputUnit u ); + QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u ); ~QgsSymbolV2RenderContext(); - QgsRenderContext* renderContext() { return mRenderContext; } - void setRenderContext( QgsRenderContext* c ) { mRenderContext = c;} + QgsRenderContext& renderContext() { return mRenderContext; } + //void setRenderContext( QgsRenderContext& c ) { mRenderContext = c;} QgsSymbolV2::OutputUnit outputUnit() const { return mOutputUnit; } void setOutputUnit( QgsSymbolV2::OutputUnit u ) { mOutputUnit = u; } + double outputLineWidth(double width) const; + double outputPixelSize(double size) const; + private: - QgsRenderContext* mRenderContext; + QgsRenderContext& mRenderContext; QgsSymbolV2::OutputUnit mOutputUnit; };