QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 }
QgsException
Definition: qgsexception.h:34
QgsCoordinateTransformContext
Definition: qgscoordinatetransformcontext.h:57
QgsBearingUtils::bearingTrueNorth
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...
Definition: qgsbearingutils.cpp:25
QgsLayoutItemMap::extentChanged
void extentChanged()
Emitted when the map's extent changes.
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
qgslayoutnortharrowhandler.h
QgsLayoutNorthArrowHandler::linkedMap
QgsLayoutItemMap * linkedMap() const
Returns the linked rotation map, if set.
Definition: qgslayoutnortharrowhandler.cpp:107
QgsLayoutNorthArrowHandler::TrueNorth
@ TrueNorth
Align to true north.
Definition: qgslayoutnortharrowhandler.h:41
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsLayoutNorthArrowHandler::QgsLayoutNorthArrowHandler
QgsLayoutNorthArrowHandler(QObject *parent)
Constructor for QgsLayoutNorthArrowHandler, with the specified parent object.
Definition: qgslayoutnortharrowhandler.cpp:24
QgsException::what
QString what() const
Definition: qgsexception.h:48
QgsLayoutNorthArrowHandler::setNorthOffset
void setNorthOffset(double offset)
Sets the offset added to the arrows's rotation from a map's North.
Definition: qgslayoutnortharrowhandler.cpp:118
QgsLayoutNorthArrowHandler::setLinkedMap
void setLinkedMap(QgsLayoutItemMap *map)
Sets the linked map item.
Definition: qgslayoutnortharrowhandler.cpp:81
QgsLayoutNorthArrowHandler::arrowRotationChanged
void arrowRotationChanged(double newRotation)
Emitted on arrow rotation change.
QgsLayoutNorthArrowHandler::setNorthMode
void setNorthMode(NorthMode mode)
Sets the mode used to calculate the arrow rotation.
Definition: qgslayoutnortharrowhandler.cpp:112
qgslayout.h
QgsLayoutItem::rotationChanged
void rotationChanged(double newRotation)
Emitted on item rotation change.
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsPointXY
Definition: qgspointxy.h:43
QgsLayoutNorthArrowHandler::NorthMode
NorthMode
Method for syncing rotation to a map's North direction.
Definition: qgslayoutnortharrowhandler.h:38
QgsLayoutItemMap
Layout graphical items for displaying a map.
Definition: qgslayoutitemmap.h:39
QgsLayoutItemMap::mapRotationChanged
void mapRotationChanged(double newRotation)
Emitted when the map's rotation changes.
qgsbearingutils.h
qgslogger.h
QgsLayoutNorthArrowHandler::GridNorth
@ GridNorth
Align to grid north.
Definition: qgslayoutnortharrowhandler.h:40
qgslayoutitemmap.h