1 /***************************************************************************
2      testqgsmaptoolellipse.cpp
3      ------------------------
4     Date                 : January 2018
5     Copyright            : (C) 2018 by Paul Blottiere
6     Email                : paul.blottiere@oslandia.com
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 
16 #include "qgstest.h"
17 
18 #include "qgisapp.h"
19 #include "qgsgeometry.h"
20 #include "qgsmapcanvas.h"
21 #include "qgssettings.h"
22 #include "qgsvectorlayer.h"
23 #include "qgsmaptooladdfeature.h"
24 #include "qgsgeometryutils.h"
25 
26 #include "testqgsmaptoolutils.h"
27 #include "qgsmaptoolellipsecenterpoint.h"
28 #include "qgsmaptoolellipsecenter2points.h"
29 #include "qgsmaptoolellipseextent.h"
30 #include "qgsmaptoolellipsefoci.h"
31 
32 
33 class TestQgsMapToolEllipse : public QObject
34 {
35     Q_OBJECT
36 
37   public:
38     TestQgsMapToolEllipse();
39 
40   private slots:
41     void initTestCase();
42     void cleanupTestCase();
43 
44     void testEllipseFromCenterAndPoint();
45     void testEllipseFromCenterAndPointWithDeletedVertex();
46     void testEllipseFromCenterAnd2Points();
47     void testEllipseFromCenterAnd2PointsWithDeletedVertex();
48     void testEllipseFromExtent();
49     void testEllipseFromExtentWithDeletedVertex();
50     void testEllipseFromFoci();
51     void testEllipseFromFociWithDeletedVertex();
52 
53   private:
54     QgisApp *mQgisApp = nullptr;
55     QgsMapToolCapture *mParentTool = nullptr;
56     QgsMapCanvas *mCanvas = nullptr;
57     QgsVectorLayer *mLayer = nullptr;
58 };
59 
60 TestQgsMapToolEllipse::TestQgsMapToolEllipse() = default;
61 
62 
63 //runs before all tests
initTestCase()64 void TestQgsMapToolEllipse::initTestCase()
65 {
66   QgsApplication::init();
67   QgsApplication::initQgis();
68 
69   mQgisApp = new QgisApp();
70 
71   mCanvas = new QgsMapCanvas();
72   mCanvas->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:27700" ) ) );
73 
74   // make testing layers
75   mLayer = new QgsVectorLayer( QStringLiteral( "LineStringZ?crs=EPSG:27700" ), QStringLiteral( "layer line Z" ), QStringLiteral( "memory" ) );
76   QVERIFY( mLayer->isValid() );
77   QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayer );
78 
79   // set layers in canvas
80   mCanvas->setLayers( QList<QgsMapLayer *>() << mLayer );
81   mCanvas->setCurrentLayer( mLayer );
82 
83   mParentTool = new QgsMapToolAddFeature( mCanvas, QgsMapToolCapture::CaptureLine );
84 }
85 
cleanupTestCase()86 void TestQgsMapToolEllipse::cleanupTestCase()
87 {
88   QgsApplication::exitQgis();
89 }
90 
testEllipseFromCenterAndPoint()91 void TestQgsMapToolEllipse::testEllipseFromCenterAndPoint()
92 {
93   QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 333 );
94   mLayer->startEditing();
95 
96   QgsMapToolEllipseCenterPoint mapTool( mParentTool, mCanvas );
97   mCanvas->setMapTool( &mapTool );
98 
99   TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
100   utils.mouseClick( 0, 0, Qt::LeftButton );
101   utils.mouseMove( 1, -1 );
102   utils.mouseClick( 1, -1, Qt::RightButton );
103   QgsFeatureId newFid = utils.newFeatureId();
104 
105   QCOMPARE( mLayer->featureCount(), ( long )1 );
106   QgsFeature f = mLayer->getFeature( newFid );
107 
108   QString wkt = f.geometry().asWkt().replace( "LineStringZ (", "" ).replace( ")", "" );
109   QgsPointSequence pts = QgsGeometryUtils::pointsFromWKT( wkt, true, false );
110 
111   for ( const QgsPoint &pt : pts )
112   {
113     QCOMPARE( pt.z(), ( double )333 );
114   }
115 
116   mLayer->rollBack();
117   QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
118 }
testEllipseFromCenterAndPointWithDeletedVertex()119 void TestQgsMapToolEllipse::testEllipseFromCenterAndPointWithDeletedVertex()
120 {
121   QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 333 );
122   mLayer->startEditing();
123 
124   QgsMapToolEllipseCenterPoint mapTool( mParentTool, mCanvas );
125   mCanvas->setMapTool( &mapTool );
126 
127   TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
128   utils.mouseClick( 4, 1, Qt::LeftButton );
129   utils.keyClick( Qt::Key_Backspace );
130   utils.mouseClick( 0, 0, Qt::LeftButton );
131   utils.mouseMove( 1, -1 );
132   utils.mouseClick( 1, -1, Qt::RightButton );
133   QgsFeatureId newFid = utils.newFeatureId();
134 
135   QCOMPARE( mLayer->featureCount(), ( long )1 );
136   QgsFeature f = mLayer->getFeature( newFid );
137 
138   QString wkt = f.geometry().asWkt().replace( "LineStringZ (", "" ).replace( ")", "" );
139   QgsPointSequence pts = QgsGeometryUtils::pointsFromWKT( wkt, true, false );
140 
141   for ( const QgsPoint &pt : pts )
142   {
143     QCOMPARE( pt.z(), ( double )333 );
144   }
145 
146   mLayer->rollBack();
147   QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
148 }
149 
150 
testEllipseFromCenterAnd2Points()151 void TestQgsMapToolEllipse::testEllipseFromCenterAnd2Points()
152 {
153   QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 111 );
154   mLayer->startEditing();
155 
156   QgsMapToolEllipseCenter2Points mapTool( mParentTool, mCanvas );
157   mCanvas->setMapTool( &mapTool );
158 
159   TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
160   utils.mouseClick( 0, 0, Qt::LeftButton );
161   utils.mouseClick( 0, 1, Qt::LeftButton );
162   utils.mouseMove( 0, -1 );
163   utils.mouseClick( 0, -1, Qt::RightButton );
164   QgsFeatureId newFid = utils.newFeatureId();
165 
166   QCOMPARE( mLayer->featureCount(), ( long )1 );
167   QgsFeature f = mLayer->getFeature( newFid );
168 
169   QString wkt = f.geometry().asWkt().replace( "LineStringZ (", "" ).replace( ")", "" );
170   QgsPointSequence pts = QgsGeometryUtils::pointsFromWKT( wkt, true, false );
171 
172   for ( const QgsPoint &pt : pts )
173   {
174     QCOMPARE( pt.z(), ( double )111 );
175   }
176 
177   mLayer->rollBack();
178   QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
179 }
180 
testEllipseFromCenterAnd2PointsWithDeletedVertex()181 void TestQgsMapToolEllipse::testEllipseFromCenterAnd2PointsWithDeletedVertex()
182 {
183   QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 111 );
184   mLayer->startEditing();
185 
186   QgsMapToolEllipseCenter2Points mapTool( mParentTool, mCanvas );
187   mCanvas->setMapTool( &mapTool );
188 
189   TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
190   utils.mouseClick( 4, 1, Qt::LeftButton );
191   utils.keyClick( Qt::Key_Backspace );
192   utils.mouseClick( 0, 0, Qt::LeftButton );
193   utils.mouseClick( 0, 1, Qt::LeftButton );
194   utils.mouseMove( 0, -1 );
195   utils.mouseClick( 0, -1, Qt::RightButton );
196   QgsFeatureId newFid = utils.newFeatureId();
197 
198   QCOMPARE( mLayer->featureCount(), ( long )1 );
199   QgsFeature f = mLayer->getFeature( newFid );
200 
201   QString wkt = f.geometry().asWkt().replace( "LineStringZ (", "" ).replace( ")", "" );
202   QgsPointSequence pts = QgsGeometryUtils::pointsFromWKT( wkt, true, false );
203 
204   for ( const QgsPoint &pt : pts )
205   {
206     QCOMPARE( pt.z(), ( double )111 );
207   }
208 
209   mLayer->rollBack();
210   QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
211 }
212 
testEllipseFromExtent()213 void TestQgsMapToolEllipse::testEllipseFromExtent()
214 {
215   QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 222 );
216   mLayer->startEditing();
217 
218   QgsMapToolEllipseExtent mapTool( mParentTool, mCanvas );
219   mCanvas->setMapTool( &mapTool );
220 
221   TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
222   utils.mouseClick( 0, 0, Qt::LeftButton );
223   utils.mouseMove( 2, 2 );
224   utils.mouseClick( 2, 2, Qt::RightButton );
225   QgsFeatureId newFid = utils.newFeatureId();
226 
227   QCOMPARE( mLayer->featureCount(), ( long )1 );
228   QgsFeature f = mLayer->getFeature( newFid );
229 
230   QString wkt = f.geometry().asWkt().replace( "LineStringZ (", "" ).replace( ")", "" );
231   QgsPointSequence pts = QgsGeometryUtils::pointsFromWKT( wkt, true, false );
232 
233   for ( const QgsPoint &pt : pts )
234   {
235     QCOMPARE( pt.z(), ( double )222 );
236   }
237 
238   mLayer->rollBack();
239   QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
240 }
241 
testEllipseFromExtentWithDeletedVertex()242 void TestQgsMapToolEllipse::testEllipseFromExtentWithDeletedVertex()
243 {
244   QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 222 );
245   mLayer->startEditing();
246 
247   QgsMapToolEllipseExtent mapTool( mParentTool, mCanvas );
248   mCanvas->setMapTool( &mapTool );
249 
250   TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
251   utils.mouseClick( 4, 1, Qt::LeftButton );
252   utils.keyClick( Qt::Key_Backspace );
253   utils.mouseClick( 0, 0, Qt::LeftButton );
254   utils.mouseMove( 2, 2 );
255   utils.mouseClick( 2, 2, Qt::RightButton );
256   QgsFeatureId newFid = utils.newFeatureId();
257 
258   QCOMPARE( mLayer->featureCount(), ( long )1 );
259   QgsFeature f = mLayer->getFeature( newFid );
260 
261   QString wkt = f.geometry().asWkt().replace( "LineStringZ (", "" ).replace( ")", "" );
262   QgsPointSequence pts = QgsGeometryUtils::pointsFromWKT( wkt, true, false );
263 
264   for ( const QgsPoint &pt : pts )
265   {
266     QCOMPARE( pt.z(), ( double )222 );
267   }
268 
269   mLayer->rollBack();
270   QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
271 }
272 
testEllipseFromFoci()273 void TestQgsMapToolEllipse::testEllipseFromFoci()
274 {
275   QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 444 );
276   mLayer->startEditing();
277 
278   QgsMapToolEllipseFoci mapTool( mParentTool, mCanvas );
279   mCanvas->setMapTool( &mapTool );
280 
281   TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
282   utils.mouseClick( 0, 0, Qt::LeftButton );
283   utils.mouseClick( 0, 2, Qt::LeftButton );
284   utils.mouseMove( 0, -1 );
285   utils.mouseClick( 0, -1, Qt::RightButton );
286   QgsFeatureId newFid = utils.newFeatureId();
287 
288   QCOMPARE( mLayer->featureCount(), ( long )1 );
289   QgsFeature f = mLayer->getFeature( newFid );
290 
291   QString wkt = f.geometry().asWkt().replace( "LineStringZ (", "" ).replace( ")", "" );
292   QgsPointSequence pts = QgsGeometryUtils::pointsFromWKT( wkt, true, false );
293 
294   for ( const QgsPoint &pt : pts )
295   {
296     QCOMPARE( pt.z(), ( double )444 );
297   }
298 
299   mLayer->rollBack();
300   QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
301 }
302 
testEllipseFromFociWithDeletedVertex()303 void TestQgsMapToolEllipse::testEllipseFromFociWithDeletedVertex()
304 {
305   QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 444 );
306   mLayer->startEditing();
307 
308   QgsMapToolEllipseFoci mapTool( mParentTool, mCanvas );
309   mCanvas->setMapTool( &mapTool );
310 
311   TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
312   utils.mouseClick( 4, 1, Qt::LeftButton );
313   utils.keyClick( Qt::Key_Backspace );
314   utils.mouseClick( 0, 0, Qt::LeftButton );
315   utils.mouseClick( 0, 2, Qt::LeftButton );
316   utils.mouseMove( 0, -1 );
317   utils.mouseClick( 0, -1, Qt::RightButton );
318   QgsFeatureId newFid = utils.newFeatureId();
319 
320   QCOMPARE( mLayer->featureCount(), ( long )1 );
321   QgsFeature f = mLayer->getFeature( newFid );
322 
323   QString wkt = f.geometry().asWkt().replace( "LineStringZ (", "" ).replace( ")", "" );
324   QgsPointSequence pts = QgsGeometryUtils::pointsFromWKT( wkt, true, false );
325 
326   for ( const QgsPoint &pt : pts )
327   {
328     QCOMPARE( pt.z(), ( double )444 );
329   }
330 
331   mLayer->rollBack();
332   QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
333 }
334 
335 QGSTEST_MAIN( TestQgsMapToolEllipse )
336 #include "testqgsmaptoolellipse.moc"
337