PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/app/qgsmaptoolmeasureangle.cpp

http://github.com/qgis/Quantum-GIS
C++ | 186 lines | 144 code | 25 blank | 17 comment | 18 complexity | 2da4f48b96491cc4691296b14a6c2788 MD5 | raw file
Possible License(s): LGPL-2.0, GPL-3.0, GPL-2.0, CC-BY-SA-3.0, MIT, 0BSD, BSD-3-Clause
  1. /***************************************************************************
  2. qgsmaptoolmeasureangle.cpp
  3. --------------------------
  4. begin : December 2009
  5. copyright : (C) 2009 by Marco Hugentobler
  6. email : marco at hugis dot net
  7. ***************************************************************************
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. ***************************************************************************/
  15. #include "qgsmaptoolmeasureangle.h"
  16. #include "qgsdisplayangle.h"
  17. #include "qgsdistancearea.h"
  18. #include "qgslogger.h"
  19. #include "qgsmapcanvas.h"
  20. #include "qgsmaptopixel.h"
  21. #include "qgsproject.h"
  22. #include "qgsrubberband.h"
  23. #include "qgssnappingutils.h"
  24. #include "qgssettings.h"
  25. #include "qgssnapindicator.h"
  26. #include "qgsmapmouseevent.h"
  27. #include <cmath>
  28. QgsMapToolMeasureAngle::QgsMapToolMeasureAngle( QgsMapCanvas *canvas )
  29. : QgsMapTool( canvas )
  30. , mSnapIndicator( new QgsSnapIndicator( canvas ) )
  31. {
  32. mToolName = tr( "Measure angle" );
  33. connect( canvas, &QgsMapCanvas::destinationCrsChanged,
  34. this, &QgsMapToolMeasureAngle::updateSettings );
  35. }
  36. QgsMapToolMeasureAngle::~QgsMapToolMeasureAngle()
  37. {
  38. stopMeasuring();
  39. }
  40. void QgsMapToolMeasureAngle::canvasMoveEvent( QgsMapMouseEvent *e )
  41. {
  42. QgsPointXY point = e->snapPoint();
  43. mSnapIndicator->setMatch( e->mapPointMatch() );
  44. if ( !mRubberBand || mAnglePoints.empty() || mAnglePoints.size() > 2 )
  45. {
  46. return;
  47. }
  48. mRubberBand->movePoint( point );
  49. if ( mAnglePoints.size() == 2 )
  50. {
  51. if ( !mResultDisplay->isVisible() )
  52. {
  53. mResultDisplay->move( e->pos() - QPoint( 100, 100 ) );
  54. mResultDisplay->show();
  55. }
  56. //angle calculation
  57. double azimuthOne = mDa.bearing( mAnglePoints.at( 1 ), mAnglePoints.at( 0 ) );
  58. double azimuthTwo = mDa.bearing( mAnglePoints.at( 1 ), point );
  59. double resultAngle = azimuthTwo - azimuthOne;
  60. QgsDebugMsg( QString::number( std::fabs( resultAngle ) ) );
  61. QgsDebugMsg( QString::number( M_PI ) );
  62. if ( std::fabs( resultAngle ) > M_PI )
  63. {
  64. if ( resultAngle < 0 )
  65. {
  66. resultAngle = M_PI + ( resultAngle + M_PI );
  67. }
  68. else
  69. {
  70. resultAngle = -M_PI + ( resultAngle - M_PI );
  71. }
  72. }
  73. mResultDisplay->setValueInRadians( resultAngle );
  74. }
  75. }
  76. void QgsMapToolMeasureAngle::canvasReleaseEvent( QgsMapMouseEvent *e )
  77. {
  78. //add points until we have three
  79. if ( mAnglePoints.size() == 3 )
  80. {
  81. mAnglePoints.clear();
  82. }
  83. if ( mAnglePoints.empty() )
  84. {
  85. if ( !mResultDisplay )
  86. {
  87. mResultDisplay = new QgsDisplayAngle( this );
  88. mResultDisplay->setWindowFlags( mResultDisplay->windowFlags() | Qt::Tool );
  89. connect( mResultDisplay, &QDialog::rejected, this, &QgsMapToolMeasureAngle::stopMeasuring );
  90. }
  91. configureDistanceArea();
  92. createRubberBand();
  93. }
  94. if ( mAnglePoints.size() < 3 )
  95. {
  96. QgsPointXY newPoint = e->snapPoint();
  97. mAnglePoints.push_back( newPoint );
  98. mRubberBand->addPoint( newPoint );
  99. }
  100. }
  101. void QgsMapToolMeasureAngle::stopMeasuring()
  102. {
  103. delete mRubberBand;
  104. mRubberBand = nullptr;
  105. delete mResultDisplay;
  106. mResultDisplay = nullptr;
  107. mAnglePoints.clear();
  108. }
  109. void QgsMapToolMeasureAngle::activate()
  110. {
  111. QgsMapTool::activate();
  112. }
  113. void QgsMapToolMeasureAngle::deactivate()
  114. {
  115. mSnapIndicator->setMatch( QgsPointLocator::Match() );
  116. stopMeasuring();
  117. QgsMapTool::deactivate();
  118. }
  119. void QgsMapToolMeasureAngle::createRubberBand()
  120. {
  121. delete mRubberBand;
  122. mRubberBand = new QgsRubberBand( mCanvas, QgsWkbTypes::LineGeometry );
  123. QgsSettings settings;
  124. int myRed = settings.value( QStringLiteral( "qgis/default_measure_color_red" ), 180 ).toInt();
  125. int myGreen = settings.value( QStringLiteral( "qgis/default_measure_color_green" ), 180 ).toInt();
  126. int myBlue = settings.value( QStringLiteral( "qgis/default_measure_color_blue" ), 180 ).toInt();
  127. mRubberBand->setColor( QColor( myRed, myGreen, myBlue, 100 ) );
  128. mRubberBand->setWidth( 3 );
  129. }
  130. void QgsMapToolMeasureAngle::updateSettings()
  131. {
  132. if ( mAnglePoints.size() != 3 )
  133. return;
  134. if ( !mResultDisplay )
  135. return;
  136. configureDistanceArea();
  137. //angle calculation
  138. double azimuthOne = mDa.bearing( mAnglePoints.at( 1 ), mAnglePoints.at( 0 ) );
  139. double azimuthTwo = mDa.bearing( mAnglePoints.at( 1 ), mAnglePoints.at( 2 ) );
  140. double resultAngle = azimuthTwo - azimuthOne;
  141. QgsDebugMsg( QString::number( std::fabs( resultAngle ) ) );
  142. QgsDebugMsg( QString::number( M_PI ) );
  143. if ( std::fabs( resultAngle ) > M_PI )
  144. {
  145. if ( resultAngle < 0 )
  146. {
  147. resultAngle = M_PI + ( resultAngle + M_PI );
  148. }
  149. else
  150. {
  151. resultAngle = -M_PI + ( resultAngle - M_PI );
  152. }
  153. }
  154. mResultDisplay->setValueInRadians( resultAngle );
  155. }
  156. void QgsMapToolMeasureAngle::configureDistanceArea()
  157. {
  158. QString ellipsoidId = QgsProject::instance()->ellipsoid();
  159. mDa.setSourceCrs( mCanvas->mapSettings().destinationCrs(), QgsProject::instance()->transformContext() );
  160. mDa.setEllipsoid( ellipsoidId );
  161. }