Skip to content

Commit f004fc1

Browse files
authored
Update north arrow decorator to use a vector graphic (#4519)
1 parent ac7cc12 commit f004fc1

File tree

4 files changed

+120
-23
lines changed

4 files changed

+120
-23
lines changed

images/images.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<file>icons/qgis-icon-60x60.png</file>
4646
<file>icons/qbrowser_icon.svg</file>
4747
<file>north_arrows/default.png</file>
48+
<file>north_arrows/default.svg</file>
4849
<file>north_arrows/gpsarrow.svg</file>
4950
<file>north_arrows/gpsarrow2.svg</file>
5051
<file>splash/splash.png</file>

images/north_arrows/default.svg

Lines changed: 83 additions & 0 deletions
Loading

src/app/qgsdecorationnortharrow.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,18 @@ email : tim@linfiniti.com
2727
#include "qgisapp.h"
2828
#include "qgsbearingutils.h"
2929
#include "qgscoordinatetransform.h"
30+
#include "qgscsexception.h"
31+
#include "qgslogger.h"
3032
#include "qgsmaplayer.h"
3133
#include "qgsproject.h"
32-
#include "qgslogger.h"
33-
#include "qgscsexception.h"
34+
#include "qgssvgcache.h"
3435

3536
// qt includes
3637
#include <QPainter>
3738
#include <QMenu>
3839
#include <QDir>
3940
#include <QFile>
41+
#include <QSvgRenderer>
4042

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

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

108-
if ( myQPixmap.load( myFileNameQString ) )
112+
if ( svg.isValid() )
109113
{
110-
double centerXDouble = myQPixmap.width() / 2.0;
111-
double centerYDouble = myQPixmap.height() / 2.0;
114+
double centerXDouble = size.width() / 2.0;
115+
double centerYDouble = size.width() / 2.0;
116+
112117
//save the current canvas rotation
113118
context.painter()->save();
114119
//
@@ -161,8 +166,8 @@ void QgsDecorationNorthArrow::render( const QgsMapSettings &mapSettings, QgsRend
161166
break;
162167

163168
case QgsUnitTypes::RenderPercentage:
164-
myXOffset = ( ( myWidth - myQPixmap.width() ) / 100. ) * mMarginHorizontal;
165-
myYOffset = ( ( myHeight - myQPixmap.height() ) / 100. ) * mMarginVertical;
169+
myXOffset = ( ( myWidth - size.width() ) / 100. ) * mMarginHorizontal;
170+
myYOffset = ( ( myHeight - size.width() ) / 100. ) * mMarginVertical;
166171
break;
167172

168173
default: // Use default of top left
@@ -172,17 +177,17 @@ void QgsDecorationNorthArrow::render( const QgsMapSettings &mapSettings, QgsRend
172177
switch ( mPlacement )
173178
{
174179
case BottomLeft:
175-
context.painter()->translate( myXOffset, myHeight - myYOffset - myQPixmap.height() );
180+
context.painter()->translate( myXOffset, myHeight - myYOffset - size.width() );
176181
break;
177182
case TopLeft:
178183
context.painter()->translate( myXOffset, myYOffset );
179184
break;
180185
case TopRight:
181-
context.painter()->translate( myWidth - myXOffset - myQPixmap.width(), myYOffset );
186+
context.painter()->translate( myWidth - myXOffset - size.width(), myYOffset );
182187
break;
183188
case BottomRight:
184-
context.painter()->translate( myWidth - myXOffset - myQPixmap.width(),
185-
myHeight - myYOffset - myQPixmap.height() );
189+
context.painter()->translate( myWidth - myXOffset - size.width(),
190+
myHeight - myYOffset - size.width() );
186191
break;
187192
default:
188193
{
@@ -193,8 +198,9 @@ void QgsDecorationNorthArrow::render( const QgsMapSettings &mapSettings, QgsRend
193198
//rotate the canvas by the north arrow rotation amount
194199
context.painter()->rotate( mRotationInt );
195200
//Now we can actually do the drawing, and draw a smooth north arrow even when rotated
196-
context.painter()->setRenderHint( QPainter::SmoothPixmapTransform );
197-
context.painter()->drawPixmap( xShift, yShift, myQPixmap );
201+
202+
context.painter()->translate( xShift, yShift );
203+
svg.render( context.painter(), QRectF( 0, 0, size.width(), size.height() ) );
198204

199205
//unrotate the canvas again
200206
context.painter()->restore();

src/app/qgsdecorationnortharrowdialog.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
#include "qgslogger.h"
1616
#include "qgshelp.h"
1717
#include "qgssettings.h"
18+
#include "qgssvgcache.h"
1819

1920
#include <QPainter>
2021
#include <cmath>
2122
#include <QDialogButtonBox>
2223
#include <QPushButton>
24+
#include <QSvgRenderer>
2325

2426
QgsDecorationNorthArrowDialog::QgsDecorationNorthArrowDialog( QgsDecorationNorthArrow &deco, QWidget *parent )
2527
: QDialog( parent )
@@ -104,20 +106,24 @@ void QgsDecorationNorthArrowDialog::apply()
104106

105107
void QgsDecorationNorthArrowDialog::rotatePixmap( int rotationInt )
106108
{
107-
QPixmap myQPixmap;
108-
QString myFileNameQString = QStringLiteral( ":/images/north_arrows/default.png" );
109-
// QgsDebugMsg(QString("Trying to load %1").arg(myFileNameQString));
110-
if ( myQPixmap.load( myFileNameQString ) )
109+
QSize size( 64, 64 );
110+
QSvgRenderer svg;
111+
112+
const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( QStringLiteral( ":/images/north_arrows/default.svg" ), size.width(), QColor( "#000000" ), QColor( "#FFFFFF" ), 0.2, 1.0 );
113+
svg.load( svgContent );
114+
115+
if ( svg.isValid() )
111116
{
112-
QPixmap myPainterPixmap( myQPixmap.height(), myQPixmap.width() );
117+
QPixmap myPainterPixmap( size.height(), size.width() );
113118
myPainterPixmap.fill();
119+
114120
QPainter myQPainter;
115121
myQPainter.begin( &myPainterPixmap );
116122

117123
myQPainter.setRenderHint( QPainter::SmoothPixmapTransform );
118124

119-
double centerXDouble = myQPixmap.width() / 2.0;
120-
double centerYDouble = myQPixmap.height() / 2.0;
125+
double centerXDouble = size.width() / 2.0;
126+
double centerYDouble = size.height() / 2.0;
121127
//save the current canvas rotation
122128
myQPainter.save();
123129
//myQPainter.translate( (int)centerXDouble, (int)centerYDouble );
@@ -138,7 +144,8 @@ void QgsDecorationNorthArrowDialog::rotatePixmap( int rotationInt )
138144
) - centerYDouble );
139145

140146
//draw the pixmap in the proper position
141-
myQPainter.drawPixmap( xShift, yShift, myQPixmap );
147+
myQPainter.translate( xShift, yShift );
148+
svg.render( &myQPainter, QRectF( 0, 0, size.width(), size.height() ) );
142149

143150
//unrotate the canvas again
144151
myQPainter.restore();

0 commit comments

Comments
 (0)