Skip to content

Commit e9d4829

Browse files
author
mhugent
committed
[FEATURE]: choice between mm and map units for new symbology. Scaling to use new symbology in print composer as well
git-svn-id: http://svn.osgeo.org/qgis/trunk@12755 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 897b784 commit e9d4829

23 files changed

+462
-149
lines changed

python/core/symbology-ng-core.sip

+37-9
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,14 @@ public:
350350

351351
virtual QString layerType() const = 0;
352352

353-
virtual void startRender(QgsRenderContext& context) = 0;
354-
virtual void stopRender(QgsRenderContext& context) = 0;
353+
virtual void startRender(QgsSymbolV2RenderContext& context) = 0;
354+
virtual void stopRender(QgsSymbolV2RenderContext& context) = 0;
355355

356356
virtual QgsSymbolLayerV2* clone() const = 0 /Factory/;
357357

358358
virtual QgsStringMap properties() const = 0;
359359

360-
virtual void drawPreviewIcon(QPainter* painter, QSize size) = 0;
360+
virtual void drawPreviewIcon(QgsSymbolV2RenderContext& context, QSize size) = 0;
361361

362362
virtual QgsSymbolV2* subSymbol();
363363
virtual bool setSubSymbol(QgsSymbolV2* symbol /Transfer/);
@@ -385,9 +385,9 @@ class QgsMarkerSymbolLayerV2 : QgsSymbolLayerV2
385385
%End
386386

387387
public:
388-
virtual void renderPoint(const QPointF& point, QgsRenderContext& context) = 0;
388+
virtual void renderPoint(const QPointF& point, QgsSymbolV2RenderContext& context) = 0;
389389

390-
void drawPreviewIcon(QPainter* painter, QSize size);
390+
void drawPreviewIcon(QgsSymbolV2RenderContext& context, QSize size);
391391

392392
void setAngle(double angle);
393393
double angle() const;
@@ -407,12 +407,12 @@ class QgsLineSymbolLayerV2 : QgsSymbolLayerV2
407407
%End
408408

409409
public:
410-
virtual void renderPolyline(const QPolygonF& points, QgsRenderContext& context) = 0;
410+
virtual void renderPolyline(const QPolygonF& points, QgsSymbolV2RenderContext& context) = 0;
411411

412412
void setWidth(double width);
413413
double width() const;
414414

415-
void drawPreviewIcon(QPainter* painter, QSize size);
415+
void drawPreviewIcon(QgsSymbolV2RenderContext& context, QSize size);
416416

417417
protected:
418418
QgsLineSymbolLayerV2(bool locked = false);
@@ -426,15 +426,34 @@ class QgsFillSymbolLayerV2 : QgsSymbolLayerV2
426426
%End
427427

428428
public:
429-
virtual void renderPolygon(const QPolygonF& points, QList<QPolygonF>* rings, QgsRenderContext& context) = 0;
429+
virtual void renderPolygon(const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context) = 0;
430430

431-
void drawPreviewIcon(QPainter* painter, QSize size);
431+
void drawPreviewIcon(QgsSymbolV2RenderContext& context, QSize size);
432432

433433
protected:
434434
QgsFillSymbolLayerV2(bool locked = false);
435435
};
436436

437437

438+
///////////////
439+
440+
class QgsSymbolV2RenderContext
441+
{
442+
%TypeHeaderCode
443+
#include <qgssymbolv2.h>
444+
%End
445+
446+
public:
447+
QgsSymbolV2RenderContext( QgsRenderContext* c, QgsSymbolV2::OutputUnit u);
448+
~QgsSymbolV2RenderContext();
449+
450+
QgsRenderContext* renderContext();
451+
void setRenderContext( QgsRenderContext* c );
452+
453+
QgsSymbolV2::OutputUnit outputUnit() const;
454+
void setOutputUnit( QgsSymbolV2::OutputUnit u );
455+
};
456+
438457
///////////////
439458

440459

@@ -458,6 +477,12 @@ class QgsSymbolV2
458477

459478
public:
460479

480+
enum OutputUnit
481+
{
482+
MM,
483+
MapUnit
484+
};
485+
461486
enum SymbolType
462487
{
463488
Marker,
@@ -508,6 +533,9 @@ public:
508533

509534
virtual QgsSymbolV2* clone() const = 0 /Factory/;
510535

536+
OutputUnit outputUnit() const;
537+
void setOutputUnit( OutputUnit u );
538+
511539
protected:
512540
QgsSymbolV2(SymbolType type, QgsSymbolLayerV2List layers /Transfer/); // can't be instantiated
513541

src/core/symbology-ng/qgsfillsymbollayerv2.cpp

+15-5
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,31 @@ QString QgsSimpleFillSymbolLayerV2::layerType() const
4141
return "SimpleFill";
4242
}
4343

44-
void QgsSimpleFillSymbolLayerV2::startRender( QgsRenderContext& context )
44+
void QgsSimpleFillSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
4545
{
4646
mBrush = QBrush( mColor, mBrushStyle );
4747
mPen = QPen( mBorderColor );
4848
mPen.setStyle( mBorderStyle );
49-
mPen.setWidthF( mBorderWidth );
49+
mPen.setWidthF( mBorderWidth * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) );
5050
}
5151

52-
void QgsSimpleFillSymbolLayerV2::stopRender( QgsRenderContext& context )
52+
void QgsSimpleFillSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
5353
{
5454
}
5555

56-
void QgsSimpleFillSymbolLayerV2::renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsRenderContext& context )
56+
void QgsSimpleFillSymbolLayerV2::renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context )
5757
{
58-
QPainter* p = context.painter();
58+
QgsRenderContext* rc = context.renderContext();
59+
if ( !rc )
60+
{
61+
return;
62+
}
63+
QPainter* p = rc->painter();
64+
if ( !p )
65+
{
66+
return;
67+
}
68+
5969
p->setBrush( mBrush );
6070
p->setPen( mPen );
6171

src/core/symbology-ng/qgsfillsymbollayerv2.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ class CORE_EXPORT QgsSimpleFillSymbolLayerV2 : public QgsFillSymbolLayerV2
3030

3131
QString layerType() const;
3232

33-
void startRender( QgsRenderContext& context );
33+
void startRender( QgsSymbolV2RenderContext& context );
3434

35-
void stopRender( QgsRenderContext& context );
35+
void stopRender( QgsSymbolV2RenderContext& context );
3636

37-
void renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsRenderContext& context );
37+
void renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context );
3838

3939
QgsStringMap properties() const;
4040

src/core/symbology-ng/qgslinesymbollayerv2.cpp

+70-28
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,40 @@ QString QgsSimpleLineSymbolLayerV2::layerType() const
4747
}
4848

4949

50-
void QgsSimpleLineSymbolLayerV2::startRender( QgsRenderContext& context )
50+
void QgsSimpleLineSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
5151
{
5252
mPen.setColor( mColor );
53-
mPen.setWidth( mWidth );
53+
mPen.setWidthF( mWidth * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) );
5454
mPen.setStyle( mPenStyle );
5555
mPen.setJoinStyle( mPenJoinStyle );
5656
mPen.setCapStyle( mPenCapStyle );
5757
}
5858

59-
void QgsSimpleLineSymbolLayerV2::stopRender( QgsRenderContext& context )
59+
void QgsSimpleLineSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
6060
{
6161
}
6262

63-
void QgsSimpleLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsRenderContext& context )
63+
void QgsSimpleLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSymbolV2RenderContext& context )
6464
{
65-
context.painter()->setPen( mPen );
65+
QgsRenderContext* rc = context.renderContext();
66+
if ( !rc )
67+
{
68+
return;
69+
}
70+
QPainter* p = rc->painter();
71+
if ( !p )
72+
{
73+
return;
74+
}
75+
76+
p->setPen( mPen );
6677
if ( mOffset == 0 )
6778
{
68-
context.painter()->drawPolyline( points );
79+
p->drawPolyline( points );
6980
}
7081
else
7182
{
72-
context.painter()->drawPolyline( ::offsetLine( points, mOffset ) );
83+
p->drawPolyline( ::offsetLine( points, mOffset ) );
7384
}
7485
}
7586

@@ -200,39 +211,59 @@ void QgsMarkerLineSymbolLayerV2::setColor( QColor color )
200211
mColor = color;
201212
}
202213

203-
void QgsMarkerLineSymbolLayerV2::startRender( QgsRenderContext& context )
214+
void QgsMarkerLineSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
204215
{
205216
// if being rotated, it gets initialized with every line segment
206217
if ( !mRotateMarker )
207-
mMarker->startRender( context );
218+
{
219+
QgsRenderContext* rc = context.renderContext();
220+
if ( rc )
221+
{
222+
mMarker->startRender( *rc );
223+
}
224+
}
208225
}
209226

210-
void QgsMarkerLineSymbolLayerV2::stopRender( QgsRenderContext& context )
227+
void QgsMarkerLineSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
211228
{
212229
if ( !mRotateMarker )
213-
mMarker->stopRender( context );
230+
{
231+
QgsRenderContext* rc = context.renderContext();
232+
if ( rc )
233+
{
234+
mMarker->stopRender( *rc );
235+
}
236+
}
214237
}
215238

216-
void QgsMarkerLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsRenderContext& context )
239+
void QgsMarkerLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSymbolV2RenderContext& context )
217240
{
218241
if ( mOffset == 0 )
219242
{
220243
renderPolylineNoOffset( points, context );
221244
}
222245
else
223246
{
224-
QPolygonF points2 = ::offsetLine( points, mOffset );
247+
QPolygonF points2 = ::offsetLine( points, mOffset * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) );
225248
renderPolylineNoOffset( points2, context );
226249
}
227250
}
228251

229-
void QgsMarkerLineSymbolLayerV2::renderPolylineNoOffset( const QPolygonF& points, QgsRenderContext& context )
252+
void QgsMarkerLineSymbolLayerV2::renderPolylineNoOffset( const QPolygonF& points, QgsSymbolV2RenderContext& context )
230253
{
231254
QPointF lastPt = points[0];
232255
double lengthLeft = 0; // how much is left until next marker
233256
bool first = true;
234257
double origAngle = mMarker->angle();
235258

259+
double painterUnitInterval = mInterval * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() );
260+
261+
QgsRenderContext* rc = context.renderContext();
262+
if ( !rc )
263+
{
264+
return;
265+
}
266+
236267
for ( int i = 1; i < points.count(); ++i )
237268
{
238269
const QPointF& pt = points[i];
@@ -242,42 +273,42 @@ void QgsMarkerLineSymbolLayerV2::renderPolylineNoOffset( const QPolygonF& points
242273

243274
// for each line, find out dx and dy, and length
244275
MyLine l( lastPt, pt );
245-
QPointF diff = l.diffForInterval( mInterval );
276+
QPointF diff = l.diffForInterval( painterUnitInterval );
246277

247278
// if there's some length left from previous line
248279
// use only the rest for the first point in new line segment
249-
double c = 1 - lengthLeft / mInterval;
280+
double c = 1 - lengthLeft / painterUnitInterval;
250281

251282
lengthLeft += l.length();
252283

253284
// rotate marker (if desired)
254285
if ( mRotateMarker )
255286
{
256287
mMarker->setAngle( origAngle + ( l.angle() * 180 / M_PI ) );
257-
mMarker->startRender( context );
288+
mMarker->startRender( *rc );
258289
}
259290

260291
// draw first marker
261292
if ( first )
262293
{
263-
mMarker->renderPoint( lastPt, context );
294+
mMarker->renderPoint( lastPt, *rc );
264295
first = false;
265296
}
266297

267298
// while we're not at the end of line segment, draw!
268-
while ( lengthLeft > mInterval )
299+
while ( lengthLeft > painterUnitInterval )
269300
{
270301
// "c" is 1 for regular point or in interval (0,1] for begin of line segment
271302
lastPt += c * diff;
272-
lengthLeft -= mInterval;
273-
mMarker->renderPoint( lastPt, context );
303+
lengthLeft -= painterUnitInterval;
304+
mMarker->renderPoint( lastPt, *rc );
274305
c = 1; // reset c (if wasn't 1 already)
275306
}
276307

277308
lastPt = pt;
278309

279310
if ( mRotateMarker )
280-
mMarker->stopRender( context );
311+
mMarker->stopRender( *rc );
281312
}
282313

283314
// restore original rotation
@@ -347,12 +378,12 @@ QString QgsLineDecorationSymbolLayerV2::layerType() const
347378
return "LineDecoration";
348379
}
349380

350-
void QgsLineDecorationSymbolLayerV2::startRender( QgsRenderContext& context )
381+
void QgsLineDecorationSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
351382
{
352383
mPen.setColor( mColor );
353384
}
354385

355-
void QgsLineDecorationSymbolLayerV2::stopRender( QgsRenderContext& context )
386+
void QgsLineDecorationSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
356387
{
357388
}
358389

@@ -369,10 +400,21 @@ static double _calculateAngle( double x1, double y1, double x2, double y2 )
369400
return atan( t ) + ( y2 >= y1 ? M_PI : 0 ); // atan is positive / negative
370401
}
371402

372-
void QgsLineDecorationSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsRenderContext& context )
403+
void QgsLineDecorationSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSymbolV2RenderContext& context )
373404
{
374405
// draw arrow at the end of line
375406

407+
QgsRenderContext* rc = context.renderContext();
408+
if ( !rc )
409+
{
410+
return;
411+
}
412+
QPainter* p = rc->painter();
413+
if ( !p )
414+
{
415+
return;
416+
}
417+
376418
int cnt = points.count();
377419
QPointF p1 = points.at( cnt - 2 );
378420
QPointF p2 = points.at( cnt - 1 );
@@ -385,9 +427,9 @@ void QgsLineDecorationSymbolLayerV2::renderPolyline( const QPolygonF& points, Qg
385427
QPointF p2_1 = p2 - QPointF( size * cos( angle1 ), size * sin( angle1 ) );
386428
QPointF p2_2 = p2 - QPointF( size * cos( angle2 ), size * sin( angle2 ) );
387429

388-
context.painter()->setPen( mPen );
389-
context.painter()->drawLine( p2, p2_1 );
390-
context.painter()->drawLine( p2, p2_2 );
430+
p->setPen( mPen );
431+
p->drawLine( p2, p2_1 );
432+
p->drawLine( p2, p2_2 );
391433
}
392434

393435
QgsStringMap QgsLineDecorationSymbolLayerV2::properties() const

0 commit comments

Comments
 (0)