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