QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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
20#include "qgsbearingutils.h"
21#include "qgslayout.h"
22#include "qgslayoutitemmap.h"
23#include "qgslogger.h"
24
25#include "moc_qgslayoutnortharrowhandler.cpp"
26
28 : QObject( parent )
29{
30
31}
32
33void QgsLayoutNorthArrowHandler::disconnectMap( QgsLayoutItemMap *map )
34{
35 if ( map )
36 {
37 disconnect( map, &QgsLayoutItemMap::mapRotationChanged, this, &QgsLayoutNorthArrowHandler::updateMapRotation );
38 disconnect( map, &QgsLayoutItemMap::rotationChanged, this, &QgsLayoutNorthArrowHandler::updateMapRotation );
39 disconnect( map, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutNorthArrowHandler::updateMapRotation );
40 }
41}
42
43void QgsLayoutNorthArrowHandler::updateMapRotation()
44{
45 if ( !mRotationMap )
46 return;
47
48 // take map rotation
49 double rotation = mRotationMap->mapRotation() + mRotationMap->rotation();
50
51 // handle true north
52 switch ( mNorthMode )
53 {
54 case GridNorth:
55 break; // nothing to do
56
57 case TrueNorth:
58 {
59 const QgsPointXY center = mRotationMap->extent().center();
60 const QgsCoordinateReferenceSystem crs = mRotationMap->crs();
61 const QgsCoordinateTransformContext transformContext = mRotationMap->layout()->project()->transformContext();
62
63 try
64 {
65 const double bearing = QgsBearingUtils::bearingTrueNorth( crs, transformContext, center );
66 rotation += bearing;
67 }
68 catch ( QgsException &e )
69 {
70 Q_UNUSED( e )
71 QgsDebugError( QStringLiteral( "Caught exception %1" ).arg( e.what() ) );
72 }
73 break;
74 }
75 }
76
77 rotation += mNorthOffset;
78 const double oldRotation = mArrowRotation;
79 mArrowRotation = ( rotation > 360.0 ) ? rotation - 360.0 : rotation ;
80 if ( mArrowRotation != oldRotation )
81 emit arrowRotationChanged( mArrowRotation );
82}
83
85{
86 if ( mRotationMap )
87 {
88 disconnectMap( mRotationMap );
89 }
90
91 if ( !map ) //disable rotation from map
92 {
93 mRotationMap = nullptr;
94 if ( mArrowRotation != 0 )
95 {
96 mArrowRotation = 0;
97 emit arrowRotationChanged( mArrowRotation );
98 }
99 }
100 else
101 {
102 connect( map, &QgsLayoutItemMap::mapRotationChanged, this, &QgsLayoutNorthArrowHandler::updateMapRotation );
103 connect( map, &QgsLayoutItemMap::rotationChanged, this, &QgsLayoutNorthArrowHandler::updateMapRotation );
104 connect( map, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutNorthArrowHandler::updateMapRotation );
105 mRotationMap = map;
106 updateMapRotation();
107 }
108}
109
111{
112 return mRotationMap;
113}
114
116{
117 mNorthMode = mode;
118 updateMapRotation();
119}
120
122{
123 mNorthOffset = offset;
124 updateMapRotation();
125}
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...
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.
#define QgsDebugError(str)
Definition qgslogger.h:57