Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update decorators to avoid relying on map canvas when rendering (#4388)
  • Loading branch information
nirvn committed Apr 21, 2017
1 parent db848a3 commit e0d20e5
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 245 deletions.
1 change: 1 addition & 0 deletions python/core/core.sip
Expand Up @@ -82,6 +82,7 @@
%Include qgslegendsettings.sip
%Include qgslegendstyle.sip
%Include qgslogger.sip
%Include qgsmapdecoration.sip
%Include qgsmaphittest.sip
%Include qgsmaplayer.sip
%Include qgsmaplayerdependency.sip
Expand Down
46 changes: 46 additions & 0 deletions python/core/qgsmapdecoration.sip
@@ -0,0 +1,46 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsmapdecoration.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsMapDecoration
{
%Docstring
Interface for map decorations.

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsmapdecoration.h"
%End
public:

QgsMapDecoration();
%Docstring
Constructor for QgsMapDecoration.
%End

virtual ~QgsMapDecoration();

virtual void render( const QgsMapSettings &mapSettings, QgsRenderContext &context ) = 0;
%Docstring
Renders a map decoration.
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsmapdecoration.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/

5 changes: 4 additions & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -3484,9 +3484,12 @@ void QgisApp::createDecorations()

void QgisApp::renderDecorationItems( QPainter *p )
{
QgsRenderContext context = QgsRenderContext::fromMapSettings( mMapCanvas->mapSettings() );
context.setPainter( p );

Q_FOREACH ( QgsDecorationItem *item, mDecorationItems )
{
item->render( p );
item->render( mMapCanvas->mapSettings(), context );
}
}

Expand Down
19 changes: 10 additions & 9 deletions src/app/qgsdecorationcopyright.cpp
Expand Up @@ -91,14 +91,15 @@ void QgsDecorationCopyright::run()
}


void QgsDecorationCopyright::render( QPainter *theQPainter )
void QgsDecorationCopyright::render( const QgsMapSettings &mapSettings, QgsRenderContext &context )
{
Q_UNUSED( mapSettings );
//Large IF statement to enable/disable copyright label
if ( enabled() )
{
// need width/height of paint device
int myHeight = theQPainter->device()->height();
int myWidth = theQPainter->device()->width();
int myHeight = context.painter()->device()->height();
int myWidth = context.painter()->device()->width();

QTextDocument text;
text.setDefaultFont( mQFont );
Expand All @@ -115,8 +116,8 @@ void QgsDecorationCopyright::render( QPainter *theQPainter )
{
case QgsUnitTypes::RenderMillimeters:
{
int myPixelsInchX = theQPainter->device()->logicalDpiX();
int myPixelsInchY = theQPainter->device()->logicalDpiY();
int myPixelsInchX = context.painter()->device()->logicalDpiX();
int myPixelsInchY = context.painter()->device()->logicalDpiY();
myXOffset = myPixelsInchX * INCHES_TO_MM * mMarginHorizontal;
myYOffset = myPixelsInchY * INCHES_TO_MM * mMarginVertical;
break;
Expand Down Expand Up @@ -156,11 +157,11 @@ void QgsDecorationCopyright::render( QPainter *theQPainter )
}

//Paint label to canvas
QMatrix worldMatrix = theQPainter->worldMatrix();
theQPainter->translate( myXOffset, myYOffset );
text.drawContents( theQPainter );
QMatrix worldMatrix = context.painter()->worldMatrix();
context.painter()->translate( myXOffset, myYOffset );
text.drawContents( context.painter() );
// Put things back how they were
theQPainter->setWorldMatrix( worldMatrix );
context.painter()->setWorldMatrix( worldMatrix );
}
}

2 changes: 1 addition & 1 deletion src/app/qgsdecorationcopyright.h
Expand Up @@ -47,7 +47,7 @@ class APP_EXPORT QgsDecorationCopyright : public QgsDecorationItem
//! Show the dialog box
void run() override;
//! render the copyright label
void render( QPainter * ) override;
void render( const QgsMapSettings &mapSettings, QgsRenderContext &context ) override;

private:
//! This is the font that will be used for the copyright label
Expand Down
58 changes: 21 additions & 37 deletions src/app/qgsdecorationgrid.cpp
Expand Up @@ -208,17 +208,17 @@ void QgsDecorationGrid::run()
}
}

void QgsDecorationGrid::render( QPainter *p )
void QgsDecorationGrid::render( const QgsMapSettings &mapSettings, QgsRenderContext &context )
{
if ( ! mEnabled )
return;

// p->setPen( mGridPen );

QList< QPair< qreal, QLineF > > verticalLines;
yGridLines( verticalLines );
yGridLines( mapSettings, verticalLines );
QList< QPair< qreal, QLineF > > horizontalLines;
xGridLines( horizontalLines );
xGridLines( mapSettings, horizontalLines );
//QgsDebugMsg( QString("grid has %1 vertical and %2 horizontal lines").arg( verticalLines.size() ).arg( horizontalLines.size() ) );

QList< QPair< qreal, QLineF > >::const_iterator vIt = verticalLines.constBegin();
Expand All @@ -230,13 +230,11 @@ void QgsDecorationGrid::render( QPainter *p )
if ( ! mLineSymbol )
return;

QgsRenderContext context = QgsRenderContext::fromMapSettings( QgisApp::instance()->mapCanvas()->mapSettings() );
context.setPainter( p );
mLineSymbol->startRender( context );

for ( ; vIt != verticalLines.constEnd(); ++vIt )
{
// p->drawLine( vIt->second );
// context.painter()->drawLine( vIt->second );
// need to convert QLineF to QPolygonF ...
QVector<QPointF> poly;
poly << vIt->second.p1() << vIt->second.p2();
Expand All @@ -245,7 +243,7 @@ void QgsDecorationGrid::render( QPainter *p )

for ( ; hIt != horizontalLines.constEnd(); ++hIt )
{
// p->drawLine( hIt->second );
// context.painter()->drawLine( hIt->second );
// need to convert QLineF to QPolygonF ...
QVector<QPointF> poly;
poly << hIt->second.p1() << hIt->second.p2();
Expand All @@ -262,7 +260,7 @@ void QgsDecorationGrid::render( QPainter *p )
{
//start mark
crossEnd1 = QgsSymbolLayerUtils::pointOnLineWithDistance( vIt->second.p1(), vIt->second.p2(), mCrossLength );
p->drawLine( vIt->second.p1(), crossEnd1 );
context.painter()->drawLine( vIt->second.p1(), crossEnd1 );

//test for intersection with every horizontal line
hIt = horizontalLines.constBegin();
Expand All @@ -272,20 +270,20 @@ void QgsDecorationGrid::render( QPainter *p )
{
crossEnd1 = QgsSymbolLayerUtils::pointOnLineWithDistance( intersectionPoint, vIt->second.p1(), mCrossLength );
crossEnd2 = QgsSymbolLayerUtils::pointOnLineWithDistance( intersectionPoint, vIt->second.p2(), mCrossLength );
p->drawLine( crossEnd1, crossEnd2 );
context.painter()->drawLine( crossEnd1, crossEnd2 );
}
}
//end mark
QPointF crossEnd2 = QgsSymbolLayerUtils::pointOnLineWithDistance( vIt->second.p2(), vIt->second.p1(), mCrossLength );
p->drawLine( vIt->second.p2(), crossEnd2 );
context.painter()->drawLine( vIt->second.p2(), crossEnd2 );
}

hIt = horizontalLines.constBegin();
for ( ; hIt != horizontalLines.constEnd(); ++hIt )
{
//start mark
crossEnd1 = QgsSymbolLayerUtils::pointOnLineWithDistance( hIt->second.p1(), hIt->second.p2(), mCrossLength );
p->drawLine( hIt->second.p1(), crossEnd1 );
context.painter()->drawLine( hIt->second.p1(), crossEnd1 );

vIt = verticalLines.constBegin();
for ( ; vIt != verticalLines.constEnd(); ++vIt )
Expand All @@ -294,12 +292,12 @@ void QgsDecorationGrid::render( QPainter *p )
{
crossEnd1 = QgsSymbolLayerUtils::pointOnLineWithDistance( intersectionPoint, hIt->second.p1(), mCrossLength );
crossEnd2 = QgsSymbolLayerUtils::pointOnLineWithDistance( intersectionPoint, hIt->second.p2(), mCrossLength );
p->drawLine( crossEnd1, crossEnd2 );
context.painter()->drawLine( crossEnd1, crossEnd2 );
}
}
//end mark
crossEnd1 = QgsSymbolLayerUtils::pointOnLineWithDistance( hIt->second.p2(), hIt->second.p1(), mCrossLength );
p->drawLine( hIt->second.p2(), crossEnd1 );
context.painter()->drawLine( hIt->second.p2(), crossEnd1 );
}
}
#endif
Expand All @@ -308,8 +306,6 @@ void QgsDecorationGrid::render( QPainter *p )
if ( ! mMarkerSymbol )
return;

QgsRenderContext context = QgsRenderContext::fromMapSettings( QgisApp::instance()->mapCanvas()->mapSettings() );
context.setPainter( p );
mMarkerSymbol->startRender( context );

QPointF intersectionPoint;
Expand All @@ -332,7 +328,7 @@ void QgsDecorationGrid::render( QPainter *p )

if ( mShowGridAnnotation )
{
drawCoordinateAnnotations( p, horizontalLines, verticalLines );
drawCoordinateAnnotations( context.painter(), horizontalLines, verticalLines );
}
}

Expand Down Expand Up @@ -511,17 +507,9 @@ void QgsDecorationGrid::drawAnnotation( QPainter *p, QPointF pos, int rotation,
p->restore();
}

const QgsMapCanvas &canvas()
{
return *QgisApp::instance()->mapCanvas();
}

QPolygonF canvasPolygon()
QPolygonF canvasPolygon( const QgsMapSettings &mapSettings )
{
QPolygonF poly;

const QgsMapCanvas &mapCanvas = canvas();
const QgsMapSettings &mapSettings = mapCanvas.mapSettings();
return mapSettings.visiblePolygon();
}

Expand Down Expand Up @@ -553,18 +541,18 @@ bool clipByRect( QLineF &line, const QPolygonF &rect )
return true;
}

QPolygonF canvasExtent()
QPolygonF canvasExtent( const QgsMapSettings &mapSettings )
{
QPolygonF poly;
QgsRectangle extent = canvas().extent();
QgsRectangle extent = mapSettings.visibleExtent();
poly << QPointF( extent.xMinimum(), extent.yMaximum() );
poly << QPointF( extent.xMaximum(), extent.yMaximum() );
poly << QPointF( extent.xMaximum(), extent.yMinimum() );
poly << QPointF( extent.xMinimum(), extent.yMinimum() );
return poly;
}

int QgsDecorationGrid::xGridLines( QList< QPair< qreal, QLineF > > &lines ) const
int QgsDecorationGrid::xGridLines( const QgsMapSettings &mapSettings, QList< QPair< qreal, QLineF > > &lines ) const
{
// prepare horizontal lines
lines.clear();
Expand All @@ -573,8 +561,6 @@ int QgsDecorationGrid::xGridLines( QList< QPair< qreal, QLineF > > &lines ) cons
return 1;
}

const QgsMapCanvas &mapCanvas = canvas();
const QgsMapSettings &mapSettings = mapCanvas.mapSettings();
const QgsMapToPixel &m2p = mapSettings.mapToPixel();

// draw nothing if the distance between grid lines would be less than 1px
Expand All @@ -583,8 +569,8 @@ int QgsDecorationGrid::xGridLines( QList< QPair< qreal, QLineF > > &lines ) cons
if ( mGridIntervalY / mapSettings.mapUnitsPerPixel() < 1 )
return 1;

const QPolygonF &canvasPoly = canvasPolygon();
const QPolygonF &mapPolygon = canvasExtent();
const QPolygonF &canvasPoly = canvasPolygon( mapSettings );
const QPolygonF &mapPolygon = canvasExtent( mapSettings );
const QRectF &mapBoundingRect = mapPolygon.boundingRect();
QLineF lineEast( mapPolygon[2], mapPolygon[1] );
QLineF lineWest( mapPolygon[3], mapPolygon[0] );
Expand All @@ -610,7 +596,7 @@ int QgsDecorationGrid::xGridLines( QList< QPair< qreal, QLineF > > &lines ) cons
return 0;
}

int QgsDecorationGrid::yGridLines( QList< QPair< qreal, QLineF > > &lines ) const
int QgsDecorationGrid::yGridLines( const QgsMapSettings &mapSettings, QList< QPair< qreal, QLineF > > &lines ) const
{
// prepare vertical lines

Expand All @@ -620,8 +606,6 @@ int QgsDecorationGrid::yGridLines( QList< QPair< qreal, QLineF > > &lines ) cons
return 1;
}

const QgsMapCanvas &mapCanvas = canvas();
const QgsMapSettings &mapSettings = mapCanvas.mapSettings();
const QgsMapToPixel &m2p = mapSettings.mapToPixel();

// draw nothing if the distance between grid lines would be less than 1px
Expand All @@ -630,8 +614,8 @@ int QgsDecorationGrid::yGridLines( QList< QPair< qreal, QLineF > > &lines ) cons
if ( mGridIntervalX / mapSettings.mapUnitsPerPixel() < 1 )
return 1;

const QPolygonF &canvasPoly = canvasPolygon();
const QPolygonF &mapPolygon = canvasExtent();
const QPolygonF &canvasPoly = canvasPolygon( mapSettings );
const QPolygonF &mapPolygon = canvasExtent( mapSettings );
QLineF lineSouth( mapPolygon[3], mapPolygon[2] );
QLineF lineNorth( mapPolygon[0], mapPolygon[1] );

Expand Down
7 changes: 4 additions & 3 deletions src/app/qgsdecorationgrid.h
Expand Up @@ -146,7 +146,7 @@ class APP_EXPORT QgsDecorationGrid: public QgsDecorationItem
void saveToProject() override;

//! this does the meaty bit of the work
void render( QPainter * ) override;
void render( const QgsMapSettings &mapSettings, QgsRenderContext &context ) override;
//! Show the dialog box
void run() override;

Expand Down Expand Up @@ -212,11 +212,12 @@ class APP_EXPORT QgsDecorationGrid: public QgsDecorationItem

/** Returns the grid lines with associated coordinate value
\returns 0 in case of success*/
int xGridLines( QList< QPair< qreal, QLineF > > &lines ) const;
int xGridLines( const QgsMapSettings &mapSettings, QList< QPair< qreal, QLineF > > &lines ) const;

/** Returns the grid lines for the y-coordinates. Not vertical in case of rotation
\returns 0 in case of success*/
int yGridLines( QList< QPair< qreal, QLineF > > &lines ) const;
int yGridLines( const QgsMapSettings &mapSettings, QList< QPair< qreal, QLineF > > &lines ) const;

//! Returns the item border of a point (in item coordinates)
Border borderForLineCoord( QPointF point, QPainter *p ) const;

Expand Down
1 change: 1 addition & 0 deletions src/app/qgsdecorationitem.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgisapp.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmapdecoration.h"
#include "qgsmaplayer.h"
#include "qgsmaptopixel.h"
#include "qgspoint.h"
Expand Down
7 changes: 4 additions & 3 deletions src/app/qgsdecorationitem.h
Expand Up @@ -19,16 +19,19 @@
#define QGSDECORATIONITEM_H

#include <QObject>

#include "qgsmapdecoration.h"
#include "qgsunittypes.h"
#include "qgis_app.h"

class QPainter;

#define INCHES_TO_MM 0.0393700787402

class APP_EXPORT QgsDecorationItem: public QObject
class APP_EXPORT QgsDecorationItem : public QObject, public QgsMapDecoration
{
Q_OBJECT

public:

//! Item placements
Expand Down Expand Up @@ -67,8 +70,6 @@ class APP_EXPORT QgsDecorationItem: public QObject
//! save values to the project
virtual void saveToProject();

//! this does the meaty bit of the work
virtual void render( QPainter * ) {}
//! Show the dialog box
virtual void run() {}

Expand Down

0 comments on commit e0d20e5

Please sign in to comment.