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