Skip to content

Commit aad29b8

Browse files
author
wonder
committedJan 14, 2010
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/qgis@12756 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

8 files changed

+75
-103
lines changed

8 files changed

+75
-103
lines changed
 

‎python/core/symbology-ng-core.sip

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,17 @@ class QgsSymbolV2RenderContext
444444
%End
445445

446446
public:
447-
QgsSymbolV2RenderContext( QgsRenderContext* c, QgsSymbolV2::OutputUnit u);
447+
QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u);
448448
~QgsSymbolV2RenderContext();
449449

450-
QgsRenderContext* renderContext();
451-
void setRenderContext( QgsRenderContext* c );
450+
QgsRenderContext& renderContext();
451+
//void setRenderContext( QgsRenderContext* c );
452452

453453
QgsSymbolV2::OutputUnit outputUnit() const;
454454
void setOutputUnit( QgsSymbolV2::OutputUnit u );
455+
456+
double outputLineWidth(double width) const;
457+
double outputPixelSize(double size) const;
455458
};
456459

457460
///////////////
@@ -605,6 +608,10 @@ typedef QMap<QString, QString> QgsStringMap;
605608

606609
//////////
607610

611+
//typedef QgsSymbolLayerV2 * ( * QgsSymbolLayerV2CreateFunc )( const QgsStringMap& );
612+
//typedef QgsSymbolLayerV2Widget*( *QgsSymbolLayerV2WidgetFunc )();
613+
614+
608615
class QgsSymbolLayerV2Metadata
609616
{
610617
%TypeHeaderCode
@@ -623,8 +630,8 @@ public:
623630

624631
QString name() const;
625632
QgsSymbolV2::SymbolType type();
626-
// TODO QgsSymbolLayerV2CreateFunc createFunction() const { return mCreateFunc; }
627-
// TODO QgsSymbolLayerV2WidgetFunc widgetFunction() const { return mWidgetFunc; }
633+
// TODO QgsSymbolLayerV2CreateFunc createFunction() const;
634+
// TODO QgsSymbolLayerV2WidgetFunc widgetFunction() const;
628635

629636
};
630637

‎src/core/symbology-ng/qgsfillsymbollayerv2.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void QgsSimpleFillSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context
4646
mBrush = QBrush( mColor, mBrushStyle );
4747
mPen = QPen( mBorderColor );
4848
mPen.setStyle( mBorderStyle );
49-
mPen.setWidthF( mBorderWidth * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) );
49+
mPen.setWidthF( context.outputLineWidth( mBorderWidth ) );
5050
}
5151

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

5656
void QgsSimpleFillSymbolLayerV2::renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context )
5757
{
58-
QgsRenderContext* rc = context.renderContext();
59-
if ( !rc )
60-
{
61-
return;
62-
}
63-
QPainter* p = rc->painter();
58+
QPainter* p = context.renderContext().painter();
6459
if ( !p )
6560
{
6661
return;

‎src/core/symbology-ng/qgslinesymbollayerv2.cpp

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ QString QgsSimpleLineSymbolLayerV2::layerType() const
5050
void QgsSimpleLineSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
5151
{
5252
mPen.setColor( mColor );
53-
mPen.setWidthF( mWidth * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) );
53+
mPen.setWidthF( context.outputLineWidth( mWidth ) );
5454
mPen.setStyle( mPenStyle );
5555
mPen.setJoinStyle( mPenJoinStyle );
5656
mPen.setCapStyle( mPenCapStyle );
@@ -62,12 +62,7 @@ void QgsSimpleLineSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
6262

6363
void QgsSimpleLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSymbolV2RenderContext& context )
6464
{
65-
QgsRenderContext* rc = context.renderContext();
66-
if ( !rc )
67-
{
68-
return;
69-
}
70-
QPainter* p = rc->painter();
65+
QPainter* p = context.renderContext().painter();
7166
if ( !p )
7267
{
7368
return;
@@ -216,23 +211,15 @@ void QgsMarkerLineSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context
216211
// if being rotated, it gets initialized with every line segment
217212
if ( !mRotateMarker )
218213
{
219-
QgsRenderContext* rc = context.renderContext();
220-
if ( rc )
221-
{
222-
mMarker->startRender( *rc );
223-
}
214+
mMarker->startRender( context.renderContext() );
224215
}
225216
}
226217

227218
void QgsMarkerLineSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
228219
{
229220
if ( !mRotateMarker )
230221
{
231-
QgsRenderContext* rc = context.renderContext();
232-
if ( rc )
233-
{
234-
mMarker->stopRender( *rc );
235-
}
222+
mMarker->stopRender( context.renderContext() );
236223
}
237224
}
238225

@@ -244,7 +231,7 @@ void QgsMarkerLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSym
244231
}
245232
else
246233
{
247-
QPolygonF points2 = ::offsetLine( points, mOffset * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) );
234+
QPolygonF points2 = ::offsetLine( points, context.outputLineWidth( mOffset ) );
248235
renderPolylineNoOffset( points2, context );
249236
}
250237
}
@@ -256,13 +243,9 @@ void QgsMarkerLineSymbolLayerV2::renderPolylineNoOffset( const QPolygonF& points
256243
bool first = true;
257244
double origAngle = mMarker->angle();
258245

259-
double painterUnitInterval = mInterval * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() );
246+
double painterUnitInterval = context.outputLineWidth( mInterval );
260247

261-
QgsRenderContext* rc = context.renderContext();
262-
if ( !rc )
263-
{
264-
return;
265-
}
248+
QgsRenderContext& rc = context.renderContext();
266249

267250
for ( int i = 1; i < points.count(); ++i )
268251
{
@@ -285,13 +268,13 @@ void QgsMarkerLineSymbolLayerV2::renderPolylineNoOffset( const QPolygonF& points
285268
if ( mRotateMarker )
286269
{
287270
mMarker->setAngle( origAngle + ( l.angle() * 180 / M_PI ) );
288-
mMarker->startRender( *rc );
271+
mMarker->startRender( rc );
289272
}
290273

291274
// draw first marker
292275
if ( first )
293276
{
294-
mMarker->renderPoint( lastPt, *rc );
277+
mMarker->renderPoint( lastPt, rc );
295278
first = false;
296279
}
297280

@@ -301,14 +284,14 @@ void QgsMarkerLineSymbolLayerV2::renderPolylineNoOffset( const QPolygonF& points
301284
// "c" is 1 for regular point or in interval (0,1] for begin of line segment
302285
lastPt += c * diff;
303286
lengthLeft -= painterUnitInterval;
304-
mMarker->renderPoint( lastPt, *rc );
287+
mMarker->renderPoint( lastPt, rc );
305288
c = 1; // reset c (if wasn't 1 already)
306289
}
307290

308291
lastPt = pt;
309292

310293
if ( mRotateMarker )
311-
mMarker->stopRender( *rc );
294+
mMarker->stopRender( rc );
312295
}
313296

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

407-
QgsRenderContext* rc = context.renderContext();
408-
if ( !rc )
409-
{
410-
return;
411-
}
412-
QPainter* p = rc->painter();
390+
QPainter* p = context.renderContext().painter();
413391
if ( !p )
414392
{
415393
return;

‎src/core/symbology-ng/qgsmarkersymbollayerv2.cpp

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ void QgsSimpleMarkerSymbolLayerV2::startRender( QgsSymbolV2RenderContext& contex
6767
{
6868
mBrush = QBrush( mColor );
6969
mPen = QPen( mBorderColor );
70-
mPen.setWidthF( mPen.widthF() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) );
70+
mPen.setWidthF( context.outputLineWidth( mPen.widthF() ) );
7171

7272
mPolygon.clear();
7373

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

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

184184
void QgsSimpleMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context )
185185
{
186-
QgsRenderContext* rc = context.renderContext();
187-
if ( !rc )
188-
{
189-
return;
190-
}
191-
QPainter* p = rc->painter();
186+
QgsRenderContext& rc = context.renderContext();
187+
QPainter* p = rc.painter();
192188
if ( !p )
193189
{
194190
return;
@@ -202,9 +198,9 @@ void QgsSimpleMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV
202198

203199
//drawMarker(p);
204200
//mCache.save("/home/marco/tmp/marker.png", "PNG");
205-
double s = mCache.width() / context.renderContext()->rasterScaleFactor();
206-
p->drawImage( QRectF( point.x() - s / 2.0 + mOffset.x() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ), \
207-
point.y() - s / 2.0 + mOffset.y() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ), \
201+
double s = mCache.width() / context.renderContext().rasterScaleFactor();
202+
p->drawImage( QRectF( context.outputLineWidth( point.x() - s / 2.0 + mOffset.x() ),
203+
context.outputLineWidth( point.y() - s / 2.0 + mOffset.y() ),
208204
s, s ), mCache );
209205
//p->restore();
210206
}
@@ -237,7 +233,7 @@ void QgsSimpleMarkerSymbolLayerV2::drawMarker( QPainter* p, QgsSymbolV2RenderCon
237233
}
238234
else
239235
{
240-
double scaledSize = mSize * QgsSymbolLayerV2Utils::pixelSizeScaleFactor( context.renderContext(), context.outputUnit() );
236+
double scaledSize = context.outputPixelSize( mSize );
241237
double half = scaledSize / 2.0;
242238
// TODO: rotate
243239

@@ -300,21 +296,16 @@ QString QgsSvgMarkerSymbolLayerV2::layerType() const
300296
void QgsSvgMarkerSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
301297
{
302298
double pictureSize = 0;
303-
QgsRenderContext* rc = context.renderContext();
304-
if ( !rc )
305-
{
306-
return;
307-
}
299+
QgsRenderContext& rc = context.renderContext();
308300

309-
if ( rc->painter() && rc->painter()->device() )
301+
if ( rc.painter() && rc.painter()->device() )
310302
{
311303
//correct QPictures DPI correction
312-
pictureSize = mSize * QgsSymbolLayerV2Utils::lineWidthScaleFactor( rc, context.outputUnit() ) \
313-
/ rc->painter()->device()->logicalDpiX() * mPicture.logicalDpiX();
304+
pictureSize = context.outputLineWidth( mSize ) / rc.painter()->device()->logicalDpiX() * mPicture.logicalDpiX();
314305
}
315306
else
316307
{
317-
pictureSize = mSize * QgsSymbolLayerV2Utils::lineWidthScaleFactor( rc, context.outputUnit() );
308+
pictureSize = context.outputLineWidth( mSize );
318309
}
319310
QRectF rect( QPointF( -pictureSize / 2.0, -pictureSize / 2.0 ), QSizeF( pictureSize, pictureSize ) );
320311
QSvgRenderer renderer( mPath );
@@ -329,19 +320,15 @@ void QgsSvgMarkerSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
329320

330321
void QgsSvgMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context )
331322
{
332-
QgsRenderContext* rc = context.renderContext();
333-
if ( !rc )
334-
{
335-
return;
336-
}
337-
QPainter* p = rc->painter();
323+
QPainter* p = context.renderContext().painter();
338324
if ( !p )
339325
{
340326
return;
341327
}
342328

343329
p->save();
344-
p->translate( point + mOffset * QgsSymbolLayerV2Utils::lineWidthScaleFactor( rc, context.outputUnit() ) );
330+
QPointF outputOffset = QPointF(context.outputLineWidth( mOffset.x() ), context.outputLineWidth( mOffset.y() ) );
331+
p->translate( point + outputOffset );
345332

346333
if ( mAngle != 0 )
347334
p->rotate( mAngle );

‎src/core/symbology-ng/qgssymbollayerv2utils.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ QIcon QgsSymbolLayerV2Utils::symbolLayerPreviewIcon( QgsSymbolLayerV2* layer, Qg
175175
painter.eraseRect( QRect( QPoint( 0, 0 ), size ) );
176176
QgsRenderContext renderContext;
177177
renderContext.setPainter( &painter );
178-
QgsSymbolV2RenderContext symbolContext( &renderContext, u );
178+
QgsSymbolV2RenderContext symbolContext( renderContext, u );
179179
layer->drawPreviewIcon( symbolContext, size );
180180
painter.end();
181181
return QIcon( pixmap );
@@ -612,20 +612,16 @@ QDomElement QgsSymbolLayerV2Utils::saveColorRamp( QString name, QgsVectorColorRa
612612
return rampEl;
613613
}
614614

615-
double QgsSymbolLayerV2Utils::lineWidthScaleFactor( QgsRenderContext* c, QgsSymbolV2::OutputUnit u )
615+
double QgsSymbolLayerV2Utils::lineWidthScaleFactor( QgsRenderContext& c, QgsSymbolV2::OutputUnit u )
616616
{
617-
if ( !c )
618-
{
619-
return 1.0;
620-
}
621617

622618
if ( u == QgsSymbolV2::MM )
623619
{
624-
return c->scaleFactor();
620+
return c.scaleFactor();
625621
}
626622
else //QgsSymbol::MapUnit
627623
{
628-
double mup = c->mapToPixel().mapUnitsPerPixel();
624+
double mup = c.mapToPixel().mapUnitsPerPixel();
629625
if ( mup > 0 )
630626
{
631627
return 1.0 / mup;
@@ -637,19 +633,14 @@ double QgsSymbolLayerV2Utils::lineWidthScaleFactor( QgsRenderContext* c, QgsSymb
637633
}
638634
}
639635

640-
double QgsSymbolLayerV2Utils::pixelSizeScaleFactor( QgsRenderContext* c, QgsSymbolV2::OutputUnit u )
636+
double QgsSymbolLayerV2Utils::pixelSizeScaleFactor( QgsRenderContext& c, QgsSymbolV2::OutputUnit u )
641637
{
642-
if ( !c )
643-
{
644-
return 1.0;
645-
}
646-
647638
if ( u == QgsSymbolV2::MM )
648639
{
649-
return ( c->scaleFactor() * c->rasterScaleFactor() );
640+
return ( c.scaleFactor() * c.rasterScaleFactor() );
650641
}
651642
else //QgsSymbol::MapUnit
652643
{
653-
return c->rasterScaleFactor() / c->mapToPixel().mapUnitsPerPixel();
644+
return c.rasterScaleFactor() / c.mapToPixel().mapUnitsPerPixel();
654645
}
655646
}

‎src/core/symbology-ng/qgssymbollayerv2utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
6767
static QDomElement saveColorRamp( QString name, QgsVectorColorRampV2* ramp, QDomDocument& doc );
6868

6969
/**Returns the line width scale factor depending on the unit and the paint device*/
70-
static double lineWidthScaleFactor( QgsRenderContext* c, QgsSymbolV2::OutputUnit u );
70+
static double lineWidthScaleFactor( QgsRenderContext& c, QgsSymbolV2::OutputUnit u );
7171
/**Returns scale factor painter units -> pixel dimensions*/
72-
static double pixelSizeScaleFactor( QgsRenderContext* c, QgsSymbolV2::OutputUnit u );
72+
static double pixelSizeScaleFactor( QgsRenderContext& c, QgsSymbolV2::OutputUnit u );
7373
};
7474

7575
class QPolygonF;

‎src/core/symbology-ng/qgssymbolv2.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ bool QgsSymbolV2::changeSymbolLayer( int index, QgsSymbolLayerV2* layer )
126126

127127
void QgsSymbolV2::startRender( QgsRenderContext& context )
128128
{
129-
QgsSymbolV2RenderContext symbolContext( &context, mOutputUnit );
129+
QgsSymbolV2RenderContext symbolContext( context, mOutputUnit );
130130
for ( QgsSymbolLayerV2List::iterator it = mLayers.begin(); it != mLayers.end(); ++it )
131131
( *it )->startRender( symbolContext );
132132
}
133133

134134
void QgsSymbolV2::stopRender( QgsRenderContext& context )
135135
{
136-
QgsSymbolV2RenderContext symbolContext( &context, mOutputUnit );
136+
QgsSymbolV2RenderContext symbolContext( context, mOutputUnit );
137137
for ( QgsSymbolLayerV2List::iterator it = mLayers.begin(); it != mLayers.end(); ++it )
138138
( *it )->stopRender( symbolContext );
139139
}
@@ -162,7 +162,7 @@ void QgsSymbolV2::drawPreviewIcon( QPainter* painter, QSize size )
162162
{
163163
QgsRenderContext context;
164164
context.setPainter( painter );
165-
QgsSymbolV2RenderContext symbolContext( &context, mOutputUnit );
165+
QgsSymbolV2RenderContext symbolContext( context, mOutputUnit );
166166
for ( QgsSymbolLayerV2List::iterator it = mLayers.begin(); it != mLayers.end(); ++it )
167167
{
168168
( *it )->drawPreviewIcon( symbolContext, size );
@@ -247,7 +247,8 @@ QgsSymbolLayerV2List QgsSymbolV2::cloneLayers() const
247247

248248
////////////////////
249249

250-
QgsSymbolV2RenderContext::QgsSymbolV2RenderContext( QgsRenderContext* c, QgsSymbolV2::OutputUnit u ): mRenderContext( c ), mOutputUnit( u )
250+
QgsSymbolV2RenderContext::QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u )
251+
: mRenderContext( c ), mOutputUnit( u )
251252
{
252253

253254
}
@@ -257,6 +258,16 @@ QgsSymbolV2RenderContext::~QgsSymbolV2RenderContext()
257258

258259
}
259260

261+
double QgsSymbolV2RenderContext::outputLineWidth(double width) const
262+
{
263+
return width * QgsSymbolLayerV2Utils::lineWidthScaleFactor( mRenderContext, mOutputUnit );
264+
}
265+
266+
double QgsSymbolV2RenderContext::outputPixelSize(double size) const
267+
{
268+
return size * QgsSymbolLayerV2Utils::pixelSizeScaleFactor( mRenderContext, mOutputUnit );
269+
}
270+
260271

261272
///////////////////
262273

@@ -323,7 +334,7 @@ double QgsMarkerSymbolV2::size()
323334

324335
void QgsMarkerSymbolV2::renderPoint( const QPointF& point, QgsRenderContext& context, int layer )
325336
{
326-
QgsSymbolV2RenderContext symbolContext( &context, mOutputUnit );
337+
QgsSymbolV2RenderContext symbolContext( context, mOutputUnit );
327338
if ( layer != -1 )
328339
{
329340
if ( layer >= 0 && layer < mLayers.count() )
@@ -391,7 +402,7 @@ double QgsLineSymbolV2::width()
391402

392403
void QgsLineSymbolV2::renderPolyline( const QPolygonF& points, QgsRenderContext& context, int layer )
393404
{
394-
QgsSymbolV2RenderContext symbolContext( &context, mOutputUnit );
405+
QgsSymbolV2RenderContext symbolContext( context, mOutputUnit );
395406
if ( layer != -1 )
396407
{
397408
if ( layer >= 0 && layer < mLayers.count() )
@@ -426,7 +437,7 @@ QgsFillSymbolV2::QgsFillSymbolV2( QgsSymbolLayerV2List layers )
426437

427438
void QgsFillSymbolV2::renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsRenderContext& context, int layer )
428439
{
429-
QgsSymbolV2RenderContext symbolContext( &context, mOutputUnit );
440+
QgsSymbolV2RenderContext symbolContext( context, mOutputUnit );
430441
if ( layer != -1 )
431442
{
432443
if ( layer >= 0 && layer < mLayers.count() )

‎src/core/symbology-ng/qgssymbolv2.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,20 @@ class CORE_EXPORT QgsSymbolV2
9797
class CORE_EXPORT QgsSymbolV2RenderContext
9898
{
9999
public:
100-
QgsSymbolV2RenderContext( QgsRenderContext* c, QgsSymbolV2::OutputUnit u );
100+
QgsSymbolV2RenderContext( QgsRenderContext& c, QgsSymbolV2::OutputUnit u );
101101
~QgsSymbolV2RenderContext();
102102

103-
QgsRenderContext* renderContext() { return mRenderContext; }
104-
void setRenderContext( QgsRenderContext* c ) { mRenderContext = c;}
103+
QgsRenderContext& renderContext() { return mRenderContext; }
104+
//void setRenderContext( QgsRenderContext& c ) { mRenderContext = c;}
105105

106106
QgsSymbolV2::OutputUnit outputUnit() const { return mOutputUnit; }
107107
void setOutputUnit( QgsSymbolV2::OutputUnit u ) { mOutputUnit = u; }
108108

109+
double outputLineWidth(double width) const;
110+
double outputPixelSize(double size) const;
111+
109112
private:
110-
QgsRenderContext* mRenderContext;
113+
QgsRenderContext& mRenderContext;
111114
QgsSymbolV2::OutputUnit mOutputUnit;
112115
};
113116

0 commit comments

Comments
 (0)
Please sign in to comment.