Skip to content

Commit

Permalink
Update north arrow decorator to use a vector graphic (#4519)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed May 8, 2017
1 parent ac7cc12 commit f004fc1
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 23 deletions.
1 change: 1 addition & 0 deletions images/images.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<file>icons/qgis-icon-60x60.png</file>
<file>icons/qbrowser_icon.svg</file>
<file>north_arrows/default.png</file>
<file>north_arrows/default.svg</file>
<file>north_arrows/gpsarrow.svg</file>
<file>north_arrows/gpsarrow2.svg</file>
<file>splash/splash.png</file>
Expand Down
83 changes: 83 additions & 0 deletions images/north_arrows/default.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 21 additions & 15 deletions src/app/qgsdecorationnortharrow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ email : tim@linfiniti.com
#include "qgisapp.h"
#include "qgsbearingutils.h"
#include "qgscoordinatetransform.h"
#include "qgscsexception.h"
#include "qgslogger.h"
#include "qgsmaplayer.h"
#include "qgsproject.h"
#include "qgslogger.h"
#include "qgscsexception.h"
#include "qgssvgcache.h"

// qt includes
#include <QPainter>
#include <QMenu>
#include <QDir>
#include <QFile>
#include <QSvgRenderer>

//non qt includes
#include <cmath>
Expand Down Expand Up @@ -101,14 +103,17 @@ void QgsDecorationNorthArrow::render( const QgsMapSettings &mapSettings, QgsRend
//Large IF statement controlled by enable check box
if ( enabled() )
{
QPixmap myQPixmap; //to store the north arrow image in
QSize size( 64, 64 );
QSvgRenderer svg;

QString myFileNameQString = QStringLiteral( ":/images/north_arrows/default.png" );
const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( QStringLiteral( ":/images/north_arrows/default.svg" ), size.width(), QColor( "#000000" ), QColor( "#FFFFFF" ), 0.2, 1.0 );
svg.load( svgContent );

if ( myQPixmap.load( myFileNameQString ) )
if ( svg.isValid() )
{
double centerXDouble = myQPixmap.width() / 2.0;
double centerYDouble = myQPixmap.height() / 2.0;
double centerXDouble = size.width() / 2.0;
double centerYDouble = size.width() / 2.0;

//save the current canvas rotation
context.painter()->save();
//
Expand Down Expand Up @@ -161,8 +166,8 @@ void QgsDecorationNorthArrow::render( const QgsMapSettings &mapSettings, QgsRend
break;

case QgsUnitTypes::RenderPercentage:
myXOffset = ( ( myWidth - myQPixmap.width() ) / 100. ) * mMarginHorizontal;
myYOffset = ( ( myHeight - myQPixmap.height() ) / 100. ) * mMarginVertical;
myXOffset = ( ( myWidth - size.width() ) / 100. ) * mMarginHorizontal;
myYOffset = ( ( myHeight - size.width() ) / 100. ) * mMarginVertical;
break;

default: // Use default of top left
Expand All @@ -172,17 +177,17 @@ void QgsDecorationNorthArrow::render( const QgsMapSettings &mapSettings, QgsRend
switch ( mPlacement )
{
case BottomLeft:
context.painter()->translate( myXOffset, myHeight - myYOffset - myQPixmap.height() );
context.painter()->translate( myXOffset, myHeight - myYOffset - size.width() );
break;
case TopLeft:
context.painter()->translate( myXOffset, myYOffset );
break;
case TopRight:
context.painter()->translate( myWidth - myXOffset - myQPixmap.width(), myYOffset );
context.painter()->translate( myWidth - myXOffset - size.width(), myYOffset );
break;
case BottomRight:
context.painter()->translate( myWidth - myXOffset - myQPixmap.width(),
myHeight - myYOffset - myQPixmap.height() );
context.painter()->translate( myWidth - myXOffset - size.width(),
myHeight - myYOffset - size.width() );
break;
default:
{
Expand All @@ -193,8 +198,9 @@ void QgsDecorationNorthArrow::render( const QgsMapSettings &mapSettings, QgsRend
//rotate the canvas by the north arrow rotation amount
context.painter()->rotate( mRotationInt );
//Now we can actually do the drawing, and draw a smooth north arrow even when rotated
context.painter()->setRenderHint( QPainter::SmoothPixmapTransform );
context.painter()->drawPixmap( xShift, yShift, myQPixmap );

context.painter()->translate( xShift, yShift );
svg.render( context.painter(), QRectF( 0, 0, size.width(), size.height() ) );

//unrotate the canvas again
context.painter()->restore();
Expand Down
23 changes: 15 additions & 8 deletions src/app/qgsdecorationnortharrowdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
#include "qgslogger.h"
#include "qgshelp.h"
#include "qgssettings.h"
#include "qgssvgcache.h"

#include <QPainter>
#include <cmath>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QSvgRenderer>

QgsDecorationNorthArrowDialog::QgsDecorationNorthArrowDialog( QgsDecorationNorthArrow &deco, QWidget *parent )
: QDialog( parent )
Expand Down Expand Up @@ -104,20 +106,24 @@ void QgsDecorationNorthArrowDialog::apply()

void QgsDecorationNorthArrowDialog::rotatePixmap( int rotationInt )
{
QPixmap myQPixmap;
QString myFileNameQString = QStringLiteral( ":/images/north_arrows/default.png" );
// QgsDebugMsg(QString("Trying to load %1").arg(myFileNameQString));
if ( myQPixmap.load( myFileNameQString ) )
QSize size( 64, 64 );
QSvgRenderer svg;

const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( QStringLiteral( ":/images/north_arrows/default.svg" ), size.width(), QColor( "#000000" ), QColor( "#FFFFFF" ), 0.2, 1.0 );
svg.load( svgContent );

if ( svg.isValid() )
{
QPixmap myPainterPixmap( myQPixmap.height(), myQPixmap.width() );
QPixmap myPainterPixmap( size.height(), size.width() );
myPainterPixmap.fill();

QPainter myQPainter;
myQPainter.begin( &myPainterPixmap );

myQPainter.setRenderHint( QPainter::SmoothPixmapTransform );

double centerXDouble = myQPixmap.width() / 2.0;
double centerYDouble = myQPixmap.height() / 2.0;
double centerXDouble = size.width() / 2.0;
double centerYDouble = size.height() / 2.0;
//save the current canvas rotation
myQPainter.save();
//myQPainter.translate( (int)centerXDouble, (int)centerYDouble );
Expand All @@ -138,7 +144,8 @@ void QgsDecorationNorthArrowDialog::rotatePixmap( int rotationInt )
) - centerYDouble );

//draw the pixmap in the proper position
myQPainter.drawPixmap( xShift, yShift, myQPixmap );
myQPainter.translate( xShift, yShift );
svg.render( &myQPainter, QRectF( 0, 0, size.width(), size.height() ) );

//unrotate the canvas again
myQPainter.restore();
Expand Down

0 comments on commit f004fc1

Please sign in to comment.