Skip to content
Permalink
Browse files

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
  • Loading branch information
wonder
wonder committed Jan 14, 2010
1 parent e9d4829 commit a48f4bda777cbceb2d5177a68fb046cfae6e04b8
@@ -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<QString, QString> 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;

};

@@ -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<QPolygonF>* rings, QgsSymbolV2RenderContext& context )
{
QgsRenderContext* rc = context.renderContext();
if ( !rc )
{
return;
}
QPainter* p = rc->painter();
QPainter* p = context.renderContext().painter();
if ( !p )
{
return;
@@ -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,23 +211,15 @@ 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() );
}
}

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;
@@ -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 );
@@ -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();
}
}
@@ -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;

0 comments on commit a48f4bd

Please sign in to comment.
You can’t perform that action at this time.