Skip to content

Commit

Permalink
[FEATURE]: choice between mm and map units for new symbology. Scaling…
Browse files Browse the repository at this point in the history
… to use new symbology in print composer as well

git-svn-id: http://svn.osgeo.org/qgis/trunk@12755 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Jan 14, 2010
1 parent 897b784 commit e9d4829
Show file tree
Hide file tree
Showing 23 changed files with 462 additions and 149 deletions.
46 changes: 37 additions & 9 deletions python/core/symbology-ng-core.sip
Expand Up @@ -350,14 +350,14 @@ public:

virtual QString layerType() const = 0;

virtual void startRender(QgsRenderContext& context) = 0;
virtual void stopRender(QgsRenderContext& context) = 0;
virtual void startRender(QgsSymbolV2RenderContext& context) = 0;
virtual void stopRender(QgsSymbolV2RenderContext& context) = 0;

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

virtual QgsStringMap properties() const = 0;

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

virtual QgsSymbolV2* subSymbol();
virtual bool setSubSymbol(QgsSymbolV2* symbol /Transfer/);
Expand Down Expand Up @@ -385,9 +385,9 @@ class QgsMarkerSymbolLayerV2 : QgsSymbolLayerV2
%End

public:
virtual void renderPoint(const QPointF& point, QgsRenderContext& context) = 0;
virtual void renderPoint(const QPointF& point, QgsSymbolV2RenderContext& context) = 0;

void drawPreviewIcon(QPainter* painter, QSize size);
void drawPreviewIcon(QgsSymbolV2RenderContext& context, QSize size);

void setAngle(double angle);
double angle() const;
Expand All @@ -407,12 +407,12 @@ class QgsLineSymbolLayerV2 : QgsSymbolLayerV2
%End

public:
virtual void renderPolyline(const QPolygonF& points, QgsRenderContext& context) = 0;
virtual void renderPolyline(const QPolygonF& points, QgsSymbolV2RenderContext& context) = 0;

void setWidth(double width);
double width() const;

void drawPreviewIcon(QPainter* painter, QSize size);
void drawPreviewIcon(QgsSymbolV2RenderContext& context, QSize size);

protected:
QgsLineSymbolLayerV2(bool locked = false);
Expand All @@ -426,15 +426,34 @@ class QgsFillSymbolLayerV2 : QgsSymbolLayerV2
%End

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

void drawPreviewIcon(QPainter* painter, QSize size);
void drawPreviewIcon(QgsSymbolV2RenderContext& context, QSize size);

protected:
QgsFillSymbolLayerV2(bool locked = false);
};


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

class QgsSymbolV2RenderContext
{
%TypeHeaderCode
#include <qgssymbolv2.h>
%End

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

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

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

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


Expand All @@ -458,6 +477,12 @@ class QgsSymbolV2

public:

enum OutputUnit
{
MM,
MapUnit
};

enum SymbolType
{
Marker,
Expand Down Expand Up @@ -508,6 +533,9 @@ public:

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

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

protected:
QgsSymbolV2(SymbolType type, QgsSymbolLayerV2List layers /Transfer/); // can't be instantiated

Expand Down
20 changes: 15 additions & 5 deletions src/core/symbology-ng/qgsfillsymbollayerv2.cpp
Expand Up @@ -41,21 +41,31 @@ QString QgsSimpleFillSymbolLayerV2::layerType() const
return "SimpleFill";
}

void QgsSimpleFillSymbolLayerV2::startRender( QgsRenderContext& context )
void QgsSimpleFillSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
{
mBrush = QBrush( mColor, mBrushStyle );
mPen = QPen( mBorderColor );
mPen.setStyle( mBorderStyle );
mPen.setWidthF( mBorderWidth );
mPen.setWidthF( mBorderWidth * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) );
}

void QgsSimpleFillSymbolLayerV2::stopRender( QgsRenderContext& context )
void QgsSimpleFillSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
{
}

void QgsSimpleFillSymbolLayerV2::renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsRenderContext& context )
void QgsSimpleFillSymbolLayerV2::renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context )
{
QPainter* p = context.painter();
QgsRenderContext* rc = context.renderContext();
if ( !rc )
{
return;
}
QPainter* p = rc->painter();
if ( !p )
{
return;
}

p->setBrush( mBrush );
p->setPen( mPen );

Expand Down
6 changes: 3 additions & 3 deletions src/core/symbology-ng/qgsfillsymbollayerv2.h
Expand Up @@ -30,11 +30,11 @@ class CORE_EXPORT QgsSimpleFillSymbolLayerV2 : public QgsFillSymbolLayerV2

QString layerType() const;

void startRender( QgsRenderContext& context );
void startRender( QgsSymbolV2RenderContext& context );

void stopRender( QgsRenderContext& context );
void stopRender( QgsSymbolV2RenderContext& context );

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

QgsStringMap properties() const;

Expand Down
98 changes: 70 additions & 28 deletions src/core/symbology-ng/qgslinesymbollayerv2.cpp
Expand Up @@ -47,29 +47,40 @@ QString QgsSimpleLineSymbolLayerV2::layerType() const
}


void QgsSimpleLineSymbolLayerV2::startRender( QgsRenderContext& context )
void QgsSimpleLineSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
{
mPen.setColor( mColor );
mPen.setWidth( mWidth );
mPen.setWidthF( mWidth * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) );
mPen.setStyle( mPenStyle );
mPen.setJoinStyle( mPenJoinStyle );
mPen.setCapStyle( mPenCapStyle );
}

void QgsSimpleLineSymbolLayerV2::stopRender( QgsRenderContext& context )
void QgsSimpleLineSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
{
}

void QgsSimpleLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsRenderContext& context )
void QgsSimpleLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSymbolV2RenderContext& context )
{
context.painter()->setPen( mPen );
QgsRenderContext* rc = context.renderContext();
if ( !rc )
{
return;
}
QPainter* p = rc->painter();
if ( !p )
{
return;
}

p->setPen( mPen );
if ( mOffset == 0 )
{
context.painter()->drawPolyline( points );
p->drawPolyline( points );
}
else
{
context.painter()->drawPolyline( ::offsetLine( points, mOffset ) );
p->drawPolyline( ::offsetLine( points, mOffset ) );
}
}

Expand Down Expand Up @@ -200,39 +211,59 @@ void QgsMarkerLineSymbolLayerV2::setColor( QColor color )
mColor = color;
}

void QgsMarkerLineSymbolLayerV2::startRender( QgsRenderContext& context )
void QgsMarkerLineSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
{
// if being rotated, it gets initialized with every line segment
if ( !mRotateMarker )
mMarker->startRender( context );
{
QgsRenderContext* rc = context.renderContext();
if ( rc )
{
mMarker->startRender( *rc );
}
}
}

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

void QgsMarkerLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsRenderContext& context )
void QgsMarkerLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSymbolV2RenderContext& context )
{
if ( mOffset == 0 )
{
renderPolylineNoOffset( points, context );
}
else
{
QPolygonF points2 = ::offsetLine( points, mOffset );
QPolygonF points2 = ::offsetLine( points, mOffset * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), context.outputUnit() ) );
renderPolylineNoOffset( points2, context );
}
}

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

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

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

for ( int i = 1; i < points.count(); ++i )
{
const QPointF& pt = points[i];
Expand All @@ -242,42 +273,42 @@ void QgsMarkerLineSymbolLayerV2::renderPolylineNoOffset( const QPolygonF& points

// for each line, find out dx and dy, and length
MyLine l( lastPt, pt );
QPointF diff = l.diffForInterval( mInterval );
QPointF diff = l.diffForInterval( painterUnitInterval );

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

lengthLeft += l.length();

// rotate marker (if desired)
if ( mRotateMarker )
{
mMarker->setAngle( origAngle + ( l.angle() * 180 / M_PI ) );
mMarker->startRender( context );
mMarker->startRender( *rc );
}

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

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

lastPt = pt;

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

// restore original rotation
Expand Down Expand Up @@ -347,12 +378,12 @@ QString QgsLineDecorationSymbolLayerV2::layerType() const
return "LineDecoration";
}

void QgsLineDecorationSymbolLayerV2::startRender( QgsRenderContext& context )
void QgsLineDecorationSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
{
mPen.setColor( mColor );
}

void QgsLineDecorationSymbolLayerV2::stopRender( QgsRenderContext& context )
void QgsLineDecorationSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
{
}

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

void QgsLineDecorationSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsRenderContext& context )
void QgsLineDecorationSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSymbolV2RenderContext& context )
{
// draw arrow at the end of line

QgsRenderContext* rc = context.renderContext();
if ( !rc )
{
return;
}
QPainter* p = rc->painter();
if ( !p )
{
return;
}

int cnt = points.count();
QPointF p1 = points.at( cnt - 2 );
QPointF p2 = points.at( cnt - 1 );
Expand All @@ -385,9 +427,9 @@ void QgsLineDecorationSymbolLayerV2::renderPolyline( const QPolygonF& points, Qg
QPointF p2_1 = p2 - QPointF( size * cos( angle1 ), size * sin( angle1 ) );
QPointF p2_2 = p2 - QPointF( size * cos( angle2 ), size * sin( angle2 ) );

context.painter()->setPen( mPen );
context.painter()->drawLine( p2, p2_1 );
context.painter()->drawLine( p2, p2_2 );
p->setPen( mPen );
p->drawLine( p2, p2_1 );
p->drawLine( p2, p2_2 );
}

QgsStringMap QgsLineDecorationSymbolLayerV2::properties() const
Expand Down

0 comments on commit e9d4829

Please sign in to comment.