Skip to content

Commit

Permalink
QgsSymbolV2RenderContext:
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
wonder committed Jan 14, 2010
1 parent e9d4829 commit a48f4bd
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 103 deletions.
17 changes: 12 additions & 5 deletions python/core/symbology-ng-core.sip
Expand Up @@ -444,14 +444,17 @@ class QgsSymbolV2RenderContext
%End %End


public: public:
QgsSymbolV2RenderContext( QgsRenderContext* c, QgsSymbolV2::OutputUnit u); QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u);
~QgsSymbolV2RenderContext(); ~QgsSymbolV2RenderContext();


QgsRenderContext* renderContext(); QgsRenderContext& renderContext();
void setRenderContext( QgsRenderContext* c ); //void setRenderContext( QgsRenderContext* c );


QgsSymbolV2::OutputUnit outputUnit() const; QgsSymbolV2::OutputUnit outputUnit() const;
void setOutputUnit( QgsSymbolV2::OutputUnit u ); void setOutputUnit( QgsSymbolV2::OutputUnit u );

double outputLineWidth(double width) const;
double outputPixelSize(double size) const;
}; };


/////////////// ///////////////
Expand Down Expand Up @@ -605,6 +608,10 @@ typedef QMap<QString, QString> QgsStringMap;


////////// //////////


//typedef QgsSymbolLayerV2 * ( * QgsSymbolLayerV2CreateFunc )( const QgsStringMap& );
//typedef QgsSymbolLayerV2Widget*( *QgsSymbolLayerV2WidgetFunc )();


class QgsSymbolLayerV2Metadata class QgsSymbolLayerV2Metadata
{ {
%TypeHeaderCode %TypeHeaderCode
Expand All @@ -623,8 +630,8 @@ public:


QString name() const; QString name() const;
QgsSymbolV2::SymbolType type(); QgsSymbolV2::SymbolType type();
// TODO QgsSymbolLayerV2CreateFunc createFunction() const { return mCreateFunc; } // TODO QgsSymbolLayerV2CreateFunc createFunction() const;
// TODO QgsSymbolLayerV2WidgetFunc widgetFunction() const { return mWidgetFunc; } // TODO QgsSymbolLayerV2WidgetFunc widgetFunction() const;


}; };


Expand Down
9 changes: 2 additions & 7 deletions src/core/symbology-ng/qgsfillsymbollayerv2.cpp
Expand Up @@ -46,7 +46,7 @@ void QgsSimpleFillSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context
mBrush = QBrush( mColor, mBrushStyle ); mBrush = QBrush( mColor, mBrushStyle );
mPen = QPen( mBorderColor ); mPen = QPen( mBorderColor );
mPen.setStyle( mBorderStyle ); mPen.setStyle( mBorderStyle );
mPen.setWidthF( mBorderWidth * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) ); mPen.setWidthF( context.outputLineWidth( mBorderWidth ) );
} }


void QgsSimpleFillSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context ) void QgsSimpleFillSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
Expand All @@ -55,12 +55,7 @@ void QgsSimpleFillSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )


void QgsSimpleFillSymbolLayerV2::renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context ) void QgsSimpleFillSymbolLayerV2::renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context )
{ {
QgsRenderContext* rc = context.renderContext(); QPainter* p = context.renderContext().painter();
if ( !rc )
{
return;
}
QPainter* p = rc->painter();
if ( !p ) if ( !p )
{ {
return; return;
Expand Down
46 changes: 12 additions & 34 deletions src/core/symbology-ng/qgslinesymbollayerv2.cpp
Expand Up @@ -50,7 +50,7 @@ QString QgsSimpleLineSymbolLayerV2::layerType() const
void QgsSimpleLineSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context ) void QgsSimpleLineSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
{ {
mPen.setColor( mColor ); mPen.setColor( mColor );
mPen.setWidthF( mWidth * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) ); mPen.setWidthF( context.outputLineWidth( mWidth ) );
mPen.setStyle( mPenStyle ); mPen.setStyle( mPenStyle );
mPen.setJoinStyle( mPenJoinStyle ); mPen.setJoinStyle( mPenJoinStyle );
mPen.setCapStyle( mPenCapStyle ); mPen.setCapStyle( mPenCapStyle );
Expand All @@ -62,12 +62,7 @@ void QgsSimpleLineSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )


void QgsSimpleLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSymbolV2RenderContext& context ) void QgsSimpleLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSymbolV2RenderContext& context )
{ {
QgsRenderContext* rc = context.renderContext(); QPainter* p = context.renderContext().painter();
if ( !rc )
{
return;
}
QPainter* p = rc->painter();
if ( !p ) if ( !p )
{ {
return; return;
Expand Down Expand Up @@ -216,23 +211,15 @@ void QgsMarkerLineSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context
// if being rotated, it gets initialized with every line segment // if being rotated, it gets initialized with every line segment
if ( !mRotateMarker ) if ( !mRotateMarker )
{ {
QgsRenderContext* rc = context.renderContext(); mMarker->startRender( context.renderContext() );
if ( rc )
{
mMarker->startRender( *rc );
}
} }
} }


void QgsMarkerLineSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context ) void QgsMarkerLineSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
{ {
if ( !mRotateMarker ) if ( !mRotateMarker )
{ {
QgsRenderContext* rc = context.renderContext(); mMarker->stopRender( context.renderContext() );
if ( rc )
{
mMarker->stopRender( *rc );
}
} }
} }


Expand All @@ -244,7 +231,7 @@ void QgsMarkerLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSym
} }
else else
{ {
QPolygonF points2 = ::offsetLine( points, mOffset * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) ); QPolygonF points2 = ::offsetLine( points, context.outputLineWidth( mOffset ) );
renderPolylineNoOffset( points2, context ); renderPolylineNoOffset( points2, context );
} }
} }
Expand All @@ -256,13 +243,9 @@ void QgsMarkerLineSymbolLayerV2::renderPolylineNoOffset( const QPolygonF& points
bool first = true; bool first = true;
double origAngle = mMarker->angle(); double origAngle = mMarker->angle();


double painterUnitInterval = mInterval * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ); double painterUnitInterval = context.outputLineWidth( mInterval );


QgsRenderContext* rc = context.renderContext(); QgsRenderContext& rc = context.renderContext();
if ( !rc )
{
return;
}


for ( int i = 1; i < points.count(); ++i ) for ( int i = 1; i < points.count(); ++i )
{ {
Expand All @@ -285,13 +268,13 @@ void QgsMarkerLineSymbolLayerV2::renderPolylineNoOffset( const QPolygonF& points
if ( mRotateMarker ) if ( mRotateMarker )
{ {
mMarker->setAngle( origAngle + ( l.angle() * 180 / M_PI ) ); mMarker->setAngle( origAngle + ( l.angle() * 180 / M_PI ) );
mMarker->startRender( *rc ); mMarker->startRender( rc );
} }


// draw first marker // draw first marker
if ( first ) if ( first )
{ {
mMarker->renderPoint( lastPt, *rc ); mMarker->renderPoint( lastPt, rc );
first = false; first = false;
} }


Expand All @@ -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 // "c" is 1 for regular point or in interval (0,1] for begin of line segment
lastPt += c * diff; lastPt += c * diff;
lengthLeft -= painterUnitInterval; lengthLeft -= painterUnitInterval;
mMarker->renderPoint( lastPt, *rc ); mMarker->renderPoint( lastPt, rc );
c = 1; // reset c (if wasn't 1 already) c = 1; // reset c (if wasn't 1 already)
} }


lastPt = pt; lastPt = pt;


if ( mRotateMarker ) if ( mRotateMarker )
mMarker->stopRender( *rc ); mMarker->stopRender( rc );
} }


// restore original rotation // restore original rotation
Expand Down Expand Up @@ -404,12 +387,7 @@ void QgsLineDecorationSymbolLayerV2::renderPolyline( const QPolygonF& points, Qg
{ {
// draw arrow at the end of line // draw arrow at the end of line


QgsRenderContext* rc = context.renderContext(); QPainter* p = context.renderContext().painter();
if ( !rc )
{
return;
}
QPainter* p = rc->painter();
if ( !p ) if ( !p )
{ {
return; return;
Expand Down
43 changes: 15 additions & 28 deletions src/core/symbology-ng/qgsmarkersymbollayerv2.cpp
Expand Up @@ -67,11 +67,11 @@ void QgsSimpleMarkerSymbolLayerV2::startRender( QgsSymbolV2RenderContext& contex
{ {
mBrush = QBrush( mColor ); mBrush = QBrush( mColor );
mPen = QPen( mBorderColor ); mPen = QPen( mBorderColor );
mPen.setWidthF( mPen.widthF() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) ); mPen.setWidthF( context.outputLineWidth( mPen.widthF() ) );


mPolygon.clear(); mPolygon.clear();


double scaledSize = mSize * QgsSymbolLayerV2Utils::pixelSizeScaleFactor( context.renderContext(), context.outputUnit() ); double scaledSize = context.outputPixelSize( mSize );
double half = scaledSize / 2.0; double half = scaledSize / 2.0;


if ( mName == "rectangle" ) if ( mName == "rectangle" )
Expand Down Expand Up @@ -183,12 +183,8 @@ void QgsSimpleMarkerSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context


void QgsSimpleMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context ) void QgsSimpleMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context )
{ {
QgsRenderContext* rc = context.renderContext(); QgsRenderContext& rc = context.renderContext();
if ( !rc ) QPainter* p = rc.painter();
{
return;
}
QPainter* p = rc->painter();
if ( !p ) if ( !p )
{ {
return; return;
Expand All @@ -202,9 +198,9 @@ void QgsSimpleMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV


//drawMarker(p); //drawMarker(p);
//mCache.save("/home/marco/tmp/marker.png", "PNG"); //mCache.save("/home/marco/tmp/marker.png", "PNG");
double s = mCache.width() / context.renderContext()->rasterScaleFactor(); double s = mCache.width() / context.renderContext().rasterScaleFactor();
p->drawImage( QRectF( point.x() - s / 2.0 + mOffset.x() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ), \ p->drawImage( QRectF( context.outputLineWidth( point.x() - s / 2.0 + mOffset.x() ),
point.y() - s / 2.0 + mOffset.y() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ), \ context.outputLineWidth( point.y() - s / 2.0 + mOffset.y() ),
s, s ), mCache ); s, s ), mCache );
//p->restore(); //p->restore();
} }
Expand Down Expand Up @@ -237,7 +233,7 @@ void QgsSimpleMarkerSymbolLayerV2::drawMarker( QPainter* p, QgsSymbolV2RenderCon
} }
else else
{ {
double scaledSize = mSize * QgsSymbolLayerV2Utils::pixelSizeScaleFactor( context.renderContext(), context.outputUnit() ); double scaledSize = context.outputPixelSize( mSize );
double half = scaledSize / 2.0; double half = scaledSize / 2.0;
// TODO: rotate // TODO: rotate


Expand Down Expand Up @@ -300,21 +296,16 @@ QString QgsSvgMarkerSymbolLayerV2::layerType() const
void QgsSvgMarkerSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context ) void QgsSvgMarkerSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
{ {
double pictureSize = 0; double pictureSize = 0;
QgsRenderContext* rc = context.renderContext(); QgsRenderContext& rc = context.renderContext();
if ( !rc )
{
return;
}


if ( rc->painter() && rc->painter()->device() ) if ( rc.painter() && rc.painter()->device() )
{ {
//correct QPictures DPI correction //correct QPictures DPI correction
pictureSize = mSize * QgsSymbolLayerV2Utils::lineWidthScaleFactor( rc, context.outputUnit() ) \ pictureSize = context.outputLineWidth( mSize ) / rc.painter()->device()->logicalDpiX() * mPicture.logicalDpiX();
/ rc->painter()->device()->logicalDpiX() * mPicture.logicalDpiX();
} }
else else
{ {
pictureSize = mSize * QgsSymbolLayerV2Utils::lineWidthScaleFactor( rc, context.outputUnit() ); pictureSize = context.outputLineWidth( mSize );
} }
QRectF rect( QPointF( -pictureSize / 2.0, -pictureSize / 2.0 ), QSizeF( pictureSize, pictureSize ) ); QRectF rect( QPointF( -pictureSize / 2.0, -pictureSize / 2.0 ), QSizeF( pictureSize, pictureSize ) );
QSvgRenderer renderer( mPath ); QSvgRenderer renderer( mPath );
Expand All @@ -329,19 +320,15 @@ void QgsSvgMarkerSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )


void QgsSvgMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context ) void QgsSvgMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context )
{ {
QgsRenderContext* rc = context.renderContext(); QPainter* p = context.renderContext().painter();
if ( !rc )
{
return;
}
QPainter* p = rc->painter();
if ( !p ) if ( !p )
{ {
return; return;
} }


p->save(); 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 ) if ( mAngle != 0 )
p->rotate( mAngle ); p->rotate( mAngle );
Expand Down
23 changes: 7 additions & 16 deletions src/core/symbology-ng/qgssymbollayerv2utils.cpp
Expand Up @@ -175,7 +175,7 @@ QIcon QgsSymbolLayerV2Utils::symbolLayerPreviewIcon( QgsSymbolLayerV2* layer, Qg
painter.eraseRect( QRect( QPoint( 0, 0 ), size ) ); painter.eraseRect( QRect( QPoint( 0, 0 ), size ) );
QgsRenderContext renderContext; QgsRenderContext renderContext;
renderContext.setPainter( &painter ); renderContext.setPainter( &painter );
QgsSymbolV2RenderContext symbolContext( &renderContext, u ); QgsSymbolV2RenderContext symbolContext( renderContext, u );
layer->drawPreviewIcon( symbolContext, size ); layer->drawPreviewIcon( symbolContext, size );
painter.end(); painter.end();
return QIcon( pixmap ); return QIcon( pixmap );
Expand Down Expand Up @@ -612,20 +612,16 @@ QDomElement QgsSymbolLayerV2Utils::saveColorRamp( QString name, QgsVectorColorRa
return rampEl; 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 ) if ( u == QgsSymbolV2::MM )
{ {
return c->scaleFactor(); return c.scaleFactor();
} }
else //QgsSymbol::MapUnit else //QgsSymbol::MapUnit
{ {
double mup = c->mapToPixel().mapUnitsPerPixel(); double mup = c.mapToPixel().mapUnitsPerPixel();
if ( mup > 0 ) if ( mup > 0 )
{ {
return 1.0 / mup; return 1.0 / mup;
Expand All @@ -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 ) if ( u == QgsSymbolV2::MM )
{ {
return ( c->scaleFactor() * c->rasterScaleFactor() ); return ( c.scaleFactor() * c.rasterScaleFactor() );
} }
else //QgsSymbol::MapUnit else //QgsSymbol::MapUnit
{ {
return c->rasterScaleFactor() / c->mapToPixel().mapUnitsPerPixel(); return c.rasterScaleFactor() / c.mapToPixel().mapUnitsPerPixel();
} }
} }
4 changes: 2 additions & 2 deletions src/core/symbology-ng/qgssymbollayerv2utils.h
Expand Up @@ -67,9 +67,9 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
static QDomElement saveColorRamp( QString name, QgsVectorColorRampV2* ramp, QDomDocument& doc ); static QDomElement saveColorRamp( QString name, QgsVectorColorRampV2* ramp, QDomDocument& doc );


/**Returns the line width scale factor depending on the unit and the paint device*/ /**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*/ /**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; class QPolygonF;
Expand Down

0 comments on commit a48f4bd

Please sign in to comment.