QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgslayoutnortharrowhandler.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutnortharrowhandler.cpp
3 -------------------
4begin : April 2020
5copyright : (C) 2020 by Nyall Dawson
6email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "moc_qgslayoutnortharrowhandler.cpp"
20#include "qgslayoutitemmap.h"
21#include "qgslayout.h"
22#include "qgsbearingutils.h"
23#include "qgslogger.h"
24
26 : QObject( parent )
27{
28
29}
30
31void QgsLayoutNorthArrowHandler::disconnectMap( QgsLayoutItemMap *map )
32{
33 if ( map )
34 {
35 disconnect( map, &QgsLayoutItemMap::mapRotationChanged, this, &QgsLayoutNorthArrowHandler::updateMapRotation );
36 disconnect( map, &QgsLayoutItemMap::rotationChanged, this, &QgsLayoutNorthArrowHandler::updateMapRotation );
37 disconnect( map, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutNorthArrowHandler::updateMapRotation );
38 }
39}
40
41void QgsLayoutNorthArrowHandler::updateMapRotation()
42{
43 if ( !mRotationMap )
44 return;
45
46 // take map rotation
47 double rotation = mRotationMap->mapRotation() + mRotationMap->rotation();
48
49 // handle true north
50 switch ( mNorthMode )
51 {
52 case GridNorth:
53 break; // nothing to do
54
55 case TrueNorth:
56 {
57 const QgsPointXY center = mRotationMap->extent().center();
58 const QgsCoordinateReferenceSystem crs = mRotationMap->crs();
59 const QgsCoordinateTransformContext transformContext = mRotationMap->layout()->project()->transformContext();
60
61 try
62 {
63 const double bearing = QgsBearingUtils::bearingTrueNorth( crs, transformContext, center );
64 rotation += bearing;
65 }
66 catch ( QgsException &e )
67 {
68 Q_UNUSED( e )
69 QgsDebugError( QStringLiteral( "Caught exception %1" ).arg( e.what() ) );
70 }
71 break;
72 }
73 }
74
75 rotation += mNorthOffset;
76 const double oldRotation = mArrowRotation;
77 mArrowRotation = ( rotation > 360.0 ) ? rotation - 360.0 : rotation ;
78 if ( mArrowRotation != oldRotation )
79 emit arrowRotationChanged( mArrowRotation );
80}
81
83{
84 if ( mRotationMap )
85 {
86 disconnectMap( mRotationMap );
87 }
88
89 if ( !map ) //disable rotation from map
90 {
91 mRotationMap = nullptr;
92 if ( mArrowRotation != 0 )
93 {
94 mArrowRotation = 0;
95 emit arrowRotationChanged( mArrowRotation );
96 }
97 }
98 else
99 {
100 connect( map, &QgsLayoutItemMap::mapRotationChanged, this, &QgsLayoutNorthArrowHandler::updateMapRotation );
101 connect( map, &QgsLayoutItemMap::rotationChanged, this, &QgsLayoutNorthArrowHandler::updateMapRotation );
102 connect( map, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutNorthArrowHandler::updateMapRotation );
103 mRotationMap = map;
104 updateMapRotation();
105 }
106}
107
109{
110 return mRotationMap;
111}
112
114{
115 mNorthMode = mode;
116 updateMapRotation();
117}
118
120{
121 mNorthOffset = offset;
122 updateMapRotation();
123}
static Q_INVOKABLE double bearingTrueNorth(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &transformContext, const QgsPointXY &point)
Returns the direction to true north from a specified point and for a specified coordinate reference s...
This class represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Defines a QGIS exception class.
QString what() const
Layout graphical items for displaying a map.
void extentChanged()
Emitted when the map's extent changes.
void mapRotationChanged(double newRotation)
Emitted when the map's rotation changes.
void rotationChanged(double newRotation)
Emitted on item rotation change.
void setNorthOffset(double offset)
Sets the offset added to the arrows's rotation from a map's North.
QgsLayoutItemMap * linkedMap() const
Returns the linked rotation map, if set.
void arrowRotationChanged(double newRotation)
Emitted on arrow rotation change.
NorthMode
Method for syncing rotation to a map's North direction.
void setNorthMode(NorthMode mode)
Sets the mode used to calculate the arrow rotation.
void setLinkedMap(QgsLayoutItemMap *map)
Sets the linked map item.
QgsLayoutNorthArrowHandler(QObject *parent)
Constructor for QgsLayoutNorthArrowHandler, with the specified parent object.
A class to represent a 2D point.
Definition qgspointxy.h:60
#define QgsDebugError(str)
Definition qgslogger.h:38
const QgsCoordinateReferenceSystem & crs