test_vertexmodel.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /***************************************************************************
  2. test_vertexmodel.h
  3. -----------------------
  4. begin : Aug 2018
  5. copyright : (C) 2018 by Denis Rouzaud
  6. email : denis@opengis.ch
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #define CATCH_CONFIG_MAIN
  17. #include "qgsquickmapsettings.h"
  18. #include "vertexmodel.h"
  19. #include "qgsgeometry.h"
  20. #include "catch2.h"
  21. #include <qgsapplication.h>
  22. #include <qgsgeometry.h>
  23. #include <qgslinestring.h>
  24. #include <qgsmessagelog.h>
  25. #include <qgspoint.h>
  26. #include <qgspointxy.h>
  27. class VertexModelTest
  28. {
  29. public:
  30. static void setCurrentVertex( std::unique_ptr<VertexModel> &model, int newVertex, bool forceUpdate = false )
  31. {
  32. model->setCurrentVertex( newVertex, forceUpdate );
  33. }
  34. };
  35. TEST_CASE( "VertexModel" )
  36. {
  37. std::unique_ptr<VertexModel> model = std::make_unique<VertexModel> ();
  38. QgsLineString *lineString = new QgsLineString( QVector<QgsPoint>() << QgsPoint( 0, 0 ) << QgsPoint( 2, 2 ) << QgsPoint( 4, 4 ) );
  39. QgsGeometry lineGeometry( lineString );
  40. QgsGeometry polygonGeometry = QgsGeometry::fromPolygonXY( QVector<QVector<QgsPointXY>>()
  41. << ( QVector<QgsPointXY>()
  42. << QgsPointXY( 0, 0 )
  43. << QgsPointXY( 2, 0 )
  44. << QgsPointXY( 2, 2 )
  45. << QgsPointXY( 0, 2 )
  46. << QgsPointXY( 0, 0 ) ) );
  47. QgsGeometry ringPolygonGeometry = QgsGeometry::fromPolygonXY( QVector<QVector<QgsPointXY>>()
  48. << ( QVector<QgsPointXY>()
  49. << QgsPointXY( 0, 0 )
  50. << QgsPointXY( 4, 0 )
  51. << QgsPointXY( 4, 4 )
  52. << QgsPointXY( 0, 4 )
  53. << QgsPointXY( 0, 0 ) )
  54. << ( QVector<QgsPointXY>()
  55. << QgsPointXY( 1, 1 )
  56. << QgsPointXY( 3, 1 )
  57. << QgsPointXY( 3, 3 )
  58. << QgsPointXY( 1, 3 )
  59. << QgsPointXY( 1, 1 ) ) );
  60. QgsGeometry point2056Geometry = QgsGeometry::fromPointXY( QgsPointXY( 2500000, 1200000 ) );
  61. QgsGeometry line2056Geometry = QgsGeometry::fromPolylineXY( { QgsPointXY( 2500001, 1200001 ), QgsPointXY( 2500002, 1200002 ), QgsPointXY( 2500004, 1200004 ) } );
  62. SECTION( "Candidates" )
  63. {
  64. model->setGeometry( lineGeometry );
  65. REQUIRE( model->vertexCount() == 7 );
  66. REQUIRE( model->vertices().at( 0 ).point == QgsPoint( -.5, -.5 ) );
  67. REQUIRE( model->vertices().at( 2 ).point == QgsPoint( 1, 1 ) );
  68. REQUIRE( model->vertices().at( 4 ).point == QgsPoint( 3, 3 ) );
  69. REQUIRE( model->vertices().at( 6 ).point == QgsPoint( 4.5, 4.5 ) );
  70. model->setGeometry( polygonGeometry );
  71. REQUIRE( model->vertexCount() == 8 );
  72. REQUIRE( model->vertices().at( 0 ).point == QgsPoint( 1, 0 ) );
  73. REQUIRE( model->vertices().at( 1 ).point == QgsPoint( 2, 0 ) );
  74. REQUIRE( model->vertices().at( 2 ).point == QgsPoint( 2, 1 ) );
  75. REQUIRE( model->vertices().at( 3 ).point == QgsPoint( 2, 2 ) );
  76. REQUIRE( model->vertices().at( 4 ).point == QgsPoint( 1, 2 ) );
  77. REQUIRE( model->vertices().at( 5 ).point == QgsPoint( 0, 2 ) );
  78. REQUIRE( model->vertices().at( 6 ).point == QgsPoint( 0, 1 ) );
  79. REQUIRE( model->vertices().at( 7 ).point == QgsPoint( 0, 0 ) );
  80. model->setGeometry( ringPolygonGeometry );
  81. REQUIRE( model->vertexCount() == 16 );
  82. REQUIRE( model->vertices().at( 0 ).point == QgsPoint( 2, 0 ) );
  83. REQUIRE( model->vertices().at( 1 ).point == QgsPoint( 4, 0 ) );
  84. REQUIRE( model->vertices().at( 2 ).point == QgsPoint( 4, 2 ) );
  85. REQUIRE( model->vertices().at( 3 ).point == QgsPoint( 4, 4 ) );
  86. REQUIRE( model->vertices().at( 4 ).point == QgsPoint( 2, 4 ) );
  87. REQUIRE( model->vertices().at( 5 ).point == QgsPoint( 0, 4 ) );
  88. REQUIRE( model->vertices().at( 6 ).point == QgsPoint( 0, 2 ) );
  89. REQUIRE( model->vertices().at( 7 ).point == QgsPoint( 0, 0 ) );
  90. REQUIRE( model->vertices().at( 8 ).point == QgsPoint( 2, 1 ) );
  91. REQUIRE( model->vertices().at( 9 ).point == QgsPoint( 3, 1 ) );
  92. REQUIRE( model->vertices().at( 10 ).point == QgsPoint( 3, 2 ) );
  93. REQUIRE( model->vertices().at( 11 ).point == QgsPoint( 3, 3 ) );
  94. REQUIRE( model->vertices().at( 12 ).point == QgsPoint( 2, 3 ) );
  95. REQUIRE( model->vertices().at( 13 ).point == QgsPoint( 1, 3 ) );
  96. REQUIRE( model->vertices().at( 14 ).point == QgsPoint( 1, 2 ) );
  97. REQUIRE( model->vertices().at( 15 ).point == QgsPoint( 1, 1 ) );
  98. }
  99. SECTION( "CanRemoveVertex" )
  100. {
  101. // line
  102. model->setGeometry( lineGeometry );
  103. REQUIRE( !model->canRemoveVertex() );
  104. model->setEditingMode( VertexModel::EditVertex );
  105. REQUIRE( model->canRemoveVertex() );
  106. model->setEditingMode( VertexModel::NoEditing );
  107. REQUIRE( model->editingMode() == VertexModel::NoEditing );
  108. VertexModelTest::setCurrentVertex( model, 1 );
  109. REQUIRE( model->editingMode() == VertexModel::EditVertex );
  110. REQUIRE( model->vertexCount() == 7 );
  111. model->removeCurrentVertex();
  112. REQUIRE( model->vertexCount() == 5 );
  113. REQUIRE( !model->canRemoveVertex() );
  114. // polygon
  115. model->setGeometry( polygonGeometry );
  116. REQUIRE( model->editingMode() == VertexModel::NoEditing );
  117. REQUIRE( !model->canRemoveVertex() );
  118. VertexModelTest::setCurrentVertex( model, 1 );
  119. REQUIRE( model->vertexCount() == 8 );
  120. model->removeCurrentVertex();
  121. REQUIRE( model->vertexCount() == 6 );
  122. REQUIRE( !model->canRemoveVertex() );
  123. }
  124. SECTION( "AddVertex" )
  125. {
  126. model->setGeometry( polygonGeometry );
  127. REQUIRE( model->vertexCount() == 8 );
  128. model->setEditingMode( VertexModel::AddVertex );
  129. VertexModelTest::setCurrentVertex( model, 0 );
  130. model->setCurrentPoint( QgsPoint( -3, 0 ) );
  131. REQUIRE( model->vertexCount() == 10 );
  132. model->setGeometry( lineGeometry );
  133. model->setEditingMode( VertexModel::AddVertex );
  134. VertexModelTest::setCurrentVertex( model, 2 );
  135. REQUIRE( model->currentVertexIndex() == 2 );
  136. REQUIRE( model->canPreviousVertex() );
  137. model->previous();
  138. REQUIRE( !model->canPreviousVertex() );
  139. REQUIRE( model->currentVertexIndex() == 0 );
  140. model->next();
  141. REQUIRE( model->currentVertexIndex() == 2 );
  142. model->setGeometry( lineGeometry );
  143. model->setEditingMode( VertexModel::AddVertex );
  144. VertexModelTest::setCurrentVertex( model, 0 );
  145. REQUIRE( model->currentVertexIndex() == 0 );
  146. REQUIRE( model->currentPoint() == QgsPoint( -.5, -.5 ) );
  147. model->next();
  148. REQUIRE( model->currentVertexIndex() == 2 );
  149. REQUIRE( model->currentPoint() == QgsPoint( 1, 1 ) );
  150. model->next();
  151. REQUIRE( model->currentVertexIndex() == 4 );
  152. REQUIRE( model->canNextVertex() );
  153. model->next();
  154. REQUIRE( model->currentVertexIndex() == 6 );
  155. REQUIRE( !model->canNextVertex() );
  156. model->previous();
  157. REQUIRE( model->currentVertexIndex() == 4 );
  158. }
  159. SECTION( "EditingMode" )
  160. {
  161. model->setGeometry( ringPolygonGeometry );
  162. REQUIRE( model->editingMode() == VertexModel::NoEditing );
  163. REQUIRE( model->currentVertexIndex() == -1 );
  164. model->setEditingMode( VertexModel::AddVertex );
  165. REQUIRE( model->currentVertexIndex() == 0 );
  166. REQUIRE( model->vertices().at( 0 ).currentVertex == true );
  167. model->setCurrentPoint( QgsPoint( 1, 0 ) );
  168. REQUIRE( model->currentVertexIndex() == 1 );
  169. REQUIRE( model->currentPoint() == QgsPoint( 1, 0 ) );
  170. REQUIRE( model->vertices().at( 1 ).point == QgsPoint( 1, 0 ) );
  171. }
  172. SECTION( "Transform" )
  173. {
  174. QgsQuickMapSettings mapSettings;
  175. mapSettings.setDestinationCrs( QgsCoordinateReferenceSystem::fromEpsgId( 4326 ) );
  176. model->setMapSettings( &mapSettings );
  177. REQUIRE( model->mapSettings()->destinationCrs().authid() == QStringLiteral( "EPSG:4326" ) );
  178. model->setGeometry( point2056Geometry );
  179. model->setCrs( QgsCoordinateReferenceSystem::fromEpsgId( 2056 ) );
  180. const auto &point = model->vertex( 0 ).point;
  181. REQUIRE( point.y() == Approx( 46.9435 ).epsilon( 0.001 ) );
  182. REQUIRE( point.x() == Approx( 6.12514 ).epsilon( 0.001 ) );
  183. }
  184. SECTION( "SelectVertexAtPosition" )
  185. {
  186. QgsQuickMapSettings mapSettings;
  187. mapSettings.setDestinationCrs( QgsCoordinateReferenceSystem::fromEpsgId( 4326 ) );
  188. // mapSettings.setExtent( );
  189. model->setMapSettings( &mapSettings );
  190. model->setCrs( QgsCoordinateReferenceSystem::fromEpsgId( 2056 ) );
  191. model->setGeometry( line2056Geometry );
  192. REQUIRE( model->currentVertexIndex() == -1 );
  193. model->selectVertexAtPosition( QgsPoint( 6.12515656, 46.943546146 ), 10 );
  194. REQUIRE( model->currentVertexIndex() == 1 );
  195. REQUIRE( model->editingMode() == VertexModel::EditVertex );
  196. model->setEditingMode( VertexModel::AddVertex );
  197. REQUIRE( model->currentVertexIndex() == 2 );
  198. // selecting a candidate will make it a vertex
  199. REQUIRE( model->vertices().count() == 7 );
  200. model->selectVertexAtPosition( QgsPoint( 6.12515333, 46.94354385 ), 10 );
  201. REQUIRE( model->editingMode() == VertexModel::EditVertex );
  202. REQUIRE( model->vertices().count() == 9 );
  203. }
  204. }