-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsdecorationnortharrow.cpp
323 lines (276 loc) · 10.8 KB
/
qgsdecorationnortharrow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/***************************************************************************
plugin.cpp
Import tool for various worldmap analysis output files
Functions:
-------------------
begin : Jan 21, 2004
copyright : (C) 2004 by Tim Sutton
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
// includes
#include "qgsdecorationnortharrow.h"
#include "qgsdecorationnortharrowdialog.h"
#include "qgisapp.h"
#include "qgscoordinatetransform.h"
#include "qgsmaplayer.h"
#include "qgsproject.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgscsexception.h"
// qt includes
#include <QPainter>
#include <QMenu>
#include <QDir>
#include <QFile>
//non qt includes
#include <cmath>
#include <cassert>
// const double QgsNorthArrowPlugin::DEG2RAD = 0.0174532925199433;
const double QgsDecorationNorthArrow::TOL = 1e-8;
/**
* Constructor for the plugin. The plugin is passed a pointer to the main app
* and an interface object that provides access to exposed functions in QGIS.
* @param qgis Pointer to the QGIS main window
* @param _qI Pointer to the QGIS interface object
*/
QgsDecorationNorthArrow::QgsDecorationNorthArrow( QObject *parent )
: QgsDecorationItem( parent )
, mRotationInt( 0 )
, mAutomatic( true )
, mMarginHorizontal( 0 )
, mMarginVertical( 0 )
{
mPlacement = BottomLeft;
mMarginUnit = QgsUnitTypes::RenderMillimeters;
setName( "North Arrow" );
projectRead();
}
QgsDecorationNorthArrow::~QgsDecorationNorthArrow()
{
}
void QgsDecorationNorthArrow::projectRead()
{
QgsDecorationItem::projectRead();
mRotationInt = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/Rotation" ), 0 );
mAutomatic = QgsProject::instance()->readBoolEntry( mNameConfig, QStringLiteral( "/Automatic" ), true );
mMarginHorizontal = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/MarginH" ), 0 );
mMarginVertical = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/MarginV" ), 0 );
}
void QgsDecorationNorthArrow::saveToProject()
{
QgsDecorationItem::saveToProject();
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Rotation" ), mRotationInt );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Automatic" ), mAutomatic );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/MarginH" ), mMarginHorizontal );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/MarginV" ), mMarginVertical );
}
// Slot called when the buffer menu item is activated
void QgsDecorationNorthArrow::run()
{
QgsDecorationNorthArrowDialog dlg( *this, QgisApp::instance() );
dlg.exec();
}
void QgsDecorationNorthArrow::render( QPainter *theQPainter )
{
//Large IF statement controlled by enable check box
if ( enabled() )
{
QPixmap myQPixmap; //to store the north arrow image in
QString myFileNameQString = QStringLiteral( ":/images/north_arrows/default.png" );
if ( myQPixmap.load( myFileNameQString ) )
{
double centerXDouble = myQPixmap.width() / 2.0;
double centerYDouble = myQPixmap.height() / 2.0;
//save the current canvas rotation
theQPainter->save();
//
//work out how to shift the image so that it rotates
// properly about its center
//(x cos a + y sin a - x, -x sin a + y cos a - y)
//
// could move this call to somewhere else so that it is only
// called when the projection or map extent changes
if ( mAutomatic )
calculateNorthDirection();
double myRadiansDouble = mRotationInt * M_PI / 180.0;
int xShift = static_cast<int>( (
( centerXDouble * cos( myRadiansDouble ) ) +
( centerYDouble * sin( myRadiansDouble ) )
) - centerXDouble );
int yShift = static_cast<int>( (
( -centerXDouble * sin( myRadiansDouble ) ) +
( centerYDouble * cos( myRadiansDouble ) )
) - centerYDouble );
// need width/height of paint device
int myHeight = theQPainter->device()->height();
int myWidth = theQPainter->device()->width();
//QgsDebugMsg("Rendering north arrow at " + mPlacementLabels.at(mPlacementIndex));
// Set margin according to selected units
int myXOffset = 0;
int myYOffset = 0;
switch ( mMarginUnit )
{
case QgsUnitTypes::RenderMillimeters:
{
int myPixelsInchX = theQPainter->device()->logicalDpiX();
int myPixelsInchY = theQPainter->device()->logicalDpiY();
myXOffset = myPixelsInchX * INCHES_TO_MM * mMarginHorizontal;
myYOffset = myPixelsInchY * INCHES_TO_MM * mMarginVertical;
break;
}
case QgsUnitTypes::RenderPixels:
myXOffset = mMarginHorizontal - 5; // Minus 5 to shift tight into corner
myYOffset = mMarginVertical - 5;
break;
case QgsUnitTypes::RenderPercentage:
myXOffset = ( ( myWidth - myQPixmap.width() ) / 100. ) * mMarginHorizontal;
myYOffset = ( ( myHeight - myQPixmap.height() ) / 100. ) * mMarginVertical;
break;
default: // Use default of top left
break;
}
//Determine placement of label from form combo box
switch ( mPlacement )
{
case BottomLeft:
theQPainter->translate( myXOffset, myHeight - myYOffset - myQPixmap.height() );
break;
case TopLeft:
theQPainter->translate( myXOffset, myYOffset );
break;
case TopRight:
theQPainter->translate( myWidth - myXOffset - myQPixmap.width(), myYOffset );
break;
case BottomRight:
theQPainter->translate( myWidth - myXOffset - myQPixmap.width(),
myHeight - myYOffset - myQPixmap.height() );
break;
default:
{
//QgsDebugMsg("Unable to determine where to put north arrow so defaulting to top left");
}
}
//rotate the canvas by the north arrow rotation amount
theQPainter->rotate( mRotationInt );
//Now we can actually do the drawing, and draw a smooth north arrow even when rotated
theQPainter->setRenderHint( QPainter::SmoothPixmapTransform );
theQPainter->drawPixmap( xShift, yShift, myQPixmap );
//unrotate the canvas again
theQPainter->restore();
}
else
{
QFont myQFont( QStringLiteral( "time" ), 12, QFont::Bold );
theQPainter->setFont( myQFont );
theQPainter->setPen( Qt::black );
theQPainter->drawText( 10, 20, tr( "North arrow pixmap not found" ) );
}
}
}
bool QgsDecorationNorthArrow::calculateNorthDirection()
{
QgsMapCanvas *mapCanvas = QgisApp::instance()->mapCanvas();
bool goodDirn = false;
// Get the shown extent...
QgsRectangle canvasExtent = mapCanvas->extent();
// ... and all layers extent, ...
QgsRectangle fullExtent = mapCanvas->fullExtent();
// ... and combine
QgsRectangle extent = canvasExtent.intersect( & fullExtent );
// If no layers are added or shown, we can't get any direction
if ( mapCanvas->layerCount() > 0 && ! extent.isEmpty() )
{
QgsCoordinateReferenceSystem outputCRS = mapCanvas->mapSettings().destinationCrs();
if ( outputCRS.isValid() && !outputCRS.isGeographic() )
{
// Use a geographic CRS to get lat/long to work out direction
QgsCoordinateReferenceSystem ourCRS = QgsCoordinateReferenceSystem::fromOgcWmsCrs( GEO_EPSG_CRS_AUTHID );
assert( ourCRS.isValid() );
QgsCoordinateTransform transform( outputCRS, ourCRS );
QgsPoint p1( extent.center() );
// A point a bit above p1. XXX assumes that y increases up!!
// May need to involve the maptopixel transform if this proves
// to be a problem.
QgsPoint p2( p1.x(), p1.y() + extent.height() * 0.25 );
// project p1 and p2 to geographic coords
try
{
p1 = transform.transform( p1 );
p2 = transform.transform( p2 );
}
catch ( QgsCsException &e )
{
Q_UNUSED( e );
// just give up
QgsDebugMsg( "North Arrow: Transformation error, quitting" );
return false;
}
// Work out the value of the initial heading one takes to go
// from point p1 to point p2. The north direction is then that
// many degrees anti-clockwise or vertical.
// Take some care to not divide by zero, etc, and ensure that we
// get sensible results for all possible values for p1 and p2.
goodDirn = true;
double angle = 0.0;
// convert to radians for the equations below
p1.multiply( M_PI / 180.0 );
p2.multiply( M_PI / 180.0 );
double y = sin( p2.x() - p1.x() ) * cos( p2.y() );
double x = cos( p1.y() ) * sin( p2.y() ) -
sin( p1.y() ) * cos( p2.y() ) * cos( p2.x() - p1.x() );
// Use TOL to decide if the quotient is big enough.
// Both x and y can be very small, if heavily zoomed
// For small y/x, we set directly angle 0. Not sure
// if this is needed.
if ( y > 0.0 )
{
if ( x > 0.0 && ( y / x ) > TOL )
angle = atan( y / x );
else if ( x < 0.0 && ( y / x ) < -TOL )
angle = M_PI - atan( -y / x );
else
angle = 0.5 * M_PI;
}
else if ( y < 0.0 )
{
if ( x > 0.0 && ( y / x ) < -TOL )
angle = -atan( -y / x );
else if ( x < 0.0 && ( y / x ) > TOL )
angle = atan( y / x ) - M_PI;
else
angle = 1.5 * M_PI;
}
else
{
if ( x > TOL )
angle = 0.0;
else if ( x < -TOL )
angle = M_PI;
else
{
angle = 0.0; // p1 = p2
goodDirn = false;
}
}
// And set the angle of the north arrow. Perhaps do something
// different if goodDirn = false.
mRotationInt = qRound( fmod( 360.0 - angle * 180.0 / M_PI, 360.0 ) );
}
else
{
// For geographic CRS and for when there are no layers, set the
// direction back to the default
mRotationInt = 0;
}
}
mRotationInt += mapCanvas->mapSettings().rotation();
return goodDirn;
}