1 /***************************************************************************
2     testqgsmaptoolrectangle.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 "qgssettingsregistrycore.h"
22 #include "qgsvectorlayer.h"
23 #include "qgsmaptooladdfeature.h"
24 #include "qgsgeometryutils.h"
25 
26 #include "testqgsmaptoolutils.h"
27 #include "qgsmaptoolrectanglecenter.h"
28 #include "qgsmaptoolrectangleextent.h"
29 #include "qgsmaptoolrectangle3points.h"
30 
31 
32 class TestQgsMapToolRectangle : public QObject
33 {
34     Q_OBJECT
35 
36   public:
37     TestQgsMapToolRectangle();
38 
39   private slots:
40     void initTestCase();
41     void cleanupTestCase();
42 
43     void testRectangleFromCenter();
44     void testRectangleFromCenterWithDeletedVertex();
45     void testRectangleFromExtent();
46     void testRectangleFromExtentWithDeletedVertex();
47     void testRectangleFrom3PointsDistance();
48     void testRectangleFrom3PointsDistanceWithDeletedVertex();
49     void testRectangleFrom3PointsProjected();
50     void testRectangleFrom3PointsProjectedWithDeletedVertex();
51 
52   private:
53     QgisApp *mQgisApp = nullptr;
54     QgsMapToolCapture *mParentTool = nullptr;
55     QgsMapCanvas *mCanvas = nullptr;
56     QgsVectorLayer *mLayer = nullptr;
57 };
58 
59 TestQgsMapToolRectangle::TestQgsMapToolRectangle() = default;
60 
61 
62 //runs before all tests
initTestCase()63 void TestQgsMapToolRectangle::initTestCase()
64 {
65   QgsApplication::init();
66   QgsApplication::initQgis();
67 
68   mQgisApp = new QgisApp();
69 
70   mCanvas = new QgsMapCanvas();
71   mCanvas->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:27700" ) ) );
72 
73   // make testing layers
74   mLayer = new QgsVectorLayer( QStringLiteral( "LineStringZ?crs=EPSG:27700" ), QStringLiteral( "layer line Z" ), QStringLiteral( "memory" ) );
75   QVERIFY( mLayer->isValid() );
76   QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayer );
77 
78   // set layers in canvas
79   mCanvas->setLayers( QList<QgsMapLayer *>() << mLayer );
80   mCanvas->setCurrentLayer( mLayer );
81 
82   mParentTool = new QgsMapToolAddFeature( mCanvas, QgsMapToolCapture::CaptureLine );
83 }
84 
cleanupTestCase()85 void TestQgsMapToolRectangle::cleanupTestCase()
86 {
87   QgsApplication::exitQgis();
88 }
89 
testRectangleFromCenter()90 void TestQgsMapToolRectangle::testRectangleFromCenter()
91 {
92   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 333 );
93   mLayer->startEditing();
94 
95   QgsMapToolRectangleCenter mapTool( mParentTool, mCanvas );
96   mCanvas->setMapTool( &mapTool );
97 
98   TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
99   utils.mouseClick( 0, 0, Qt::LeftButton );
100   utils.mouseMove( 2, 1 );
101   utils.mouseClick( 2, 1, Qt::RightButton );
102   const QgsFeatureId newFid = utils.newFeatureId();
103 
104   // QCOMPARE( mLayer->featureCount(), ( long )1 );
105   const QgsFeature f = mLayer->getFeature( newFid );
106 
107   const QString wkt = "LineStringZ (-2 -1 333, -2 1 333, 2 1 333, 2 -1 333, -2 -1 333)";
108   QgsLineString line;
109   line.fromWkt( wkt );
110   QVERIFY( static_cast<QgsLineString *>( f.geometry().get() )->equals( line ) );
111 
112   mLayer->rollBack();
113   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 0 );
114 }
115 
testRectangleFromCenterWithDeletedVertex()116 void TestQgsMapToolRectangle::testRectangleFromCenterWithDeletedVertex()
117 {
118   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 333 );
119   mLayer->startEditing();
120 
121   QgsMapToolRectangleCenter mapTool( mParentTool, mCanvas );
122   mCanvas->setMapTool( &mapTool );
123 
124   TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
125   utils.mouseClick( 4, 1, Qt::LeftButton );
126   utils.keyClick( Qt::Key_Backspace );
127   utils.mouseClick( 0, 0, Qt::LeftButton );
128   utils.mouseMove( 2, 1 );
129   utils.mouseClick( 2, 1, Qt::RightButton );
130   const QgsFeatureId newFid = utils.newFeatureId();
131 
132   // QCOMPARE( mLayer->featureCount(), ( long )1 );
133   const QgsFeature f = mLayer->getFeature( newFid );
134 
135   const QString wkt = "LineStringZ (-2 -1 333, -2 1 333, 2 1 333, 2 -1 333, -2 -1 333)";
136   QgsLineString line;
137   line.fromWkt( wkt );
138   QVERIFY( static_cast<QgsLineString *>( f.geometry().get() )->equals( line ) );
139 
140   mLayer->rollBack();
141   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 0 );
142 }
143 
testRectangleFromExtent()144 void TestQgsMapToolRectangle::testRectangleFromExtent()
145 {
146   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 222 );
147   mLayer->startEditing();
148 
149   QgsMapToolRectangleExtent mapTool( mParentTool, mCanvas );
150   mCanvas->setMapTool( &mapTool );
151 
152   TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
153   utils.mouseClick( 0, 0, Qt::LeftButton );
154   utils.mouseMove( 2, 1 );
155   utils.mouseClick( 2, 1, Qt::RightButton );
156   const QgsFeatureId newFid = utils.newFeatureId();
157 
158   // QCOMPARE( mLayer->featureCount(), ( long )1 );
159   const QgsFeature f = mLayer->getFeature( newFid );
160 
161   const QString wkt = "LineStringZ (0 0 222, 0 1 222, 2 1 222, 2 0 222, 0 0 222)";
162   QgsLineString line;
163   line.fromWkt( wkt );
164   QVERIFY( static_cast<QgsLineString *>( f.geometry().get() )->equals( line ) );
165 
166   mLayer->rollBack();
167   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 0 );
168 }
testRectangleFromExtentWithDeletedVertex()169 void TestQgsMapToolRectangle::testRectangleFromExtentWithDeletedVertex()
170 {
171   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 222 );
172   mLayer->startEditing();
173 
174   QgsMapToolRectangleExtent mapTool( mParentTool, mCanvas );
175   mCanvas->setMapTool( &mapTool );
176 
177   TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
178   utils.mouseClick( 4, 1, Qt::LeftButton );
179   utils.keyClick( Qt::Key_Backspace );
180   utils.mouseClick( 0, 0, Qt::LeftButton );
181   utils.mouseMove( 2, 1 );
182   utils.mouseClick( 2, 1, Qt::RightButton );
183   const QgsFeatureId newFid = utils.newFeatureId();
184 
185   // QCOMPARE( mLayer->featureCount(), ( long )1 );
186   const QgsFeature f = mLayer->getFeature( newFid );
187 
188   const QString wkt = "LineStringZ (0 0 222, 0 1 222, 2 1 222, 2 0 222, 0 0 222)";
189   QgsLineString line;
190   line.fromWkt( wkt );
191   QVERIFY( static_cast<QgsLineString *>( f.geometry().get() )->equals( line ) );
192 
193   mLayer->rollBack();
194   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 0 );
195 }
196 
197 
testRectangleFrom3PointsDistance()198 void TestQgsMapToolRectangle::testRectangleFrom3PointsDistance()
199 {
200   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 111 );
201   mLayer->startEditing();
202 
203   QgsMapToolRectangle3Points mapTool( mParentTool, mCanvas, QgsMapToolRectangle3Points::DistanceMode );
204   mCanvas->setMapTool( &mapTool );
205 
206   TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
207   utils.mouseClick( 0, 0, Qt::LeftButton );
208   utils.mouseMove( 2, 0 );
209   utils.mouseClick( 2, 0, Qt::LeftButton );
210   utils.mouseMove( 2, 1 );
211   utils.mouseClick( 2, 1, Qt::RightButton );
212   const QgsFeatureId newFid = utils.newFeatureId();
213 
214   // QCOMPARE( mLayer->featureCount(), ( long )1 );
215   const QgsFeature f = mLayer->getFeature( newFid );
216 
217   const QString wkt = "LineStringZ (0 0 111, 2 0 111, 2 1 111, 0 1 111, 0 0 111)";
218   QgsLineString line;
219   line.fromWkt( wkt );
220   QVERIFY( static_cast<QgsLineString *>( f.geometry().get() )->equals( line ) );
221 
222   mLayer->rollBack();
223   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 0 );
224 }
testRectangleFrom3PointsDistanceWithDeletedVertex()225 void TestQgsMapToolRectangle::testRectangleFrom3PointsDistanceWithDeletedVertex()
226 {
227   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 111 );
228   mLayer->startEditing();
229 
230   QgsMapToolRectangle3Points mapTool( mParentTool, mCanvas, QgsMapToolRectangle3Points::DistanceMode );
231   mCanvas->setMapTool( &mapTool );
232 
233   TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
234   utils.mouseClick( 0, 0, Qt::LeftButton );
235   utils.mouseMove( 2, 0 );
236   utils.mouseClick( 3, 0, Qt::LeftButton );
237   utils.keyClick( Qt::Key_Backspace );
238   utils.mouseClick( 2, 0, Qt::LeftButton );
239   utils.mouseMove( 2, 1 );
240   utils.mouseClick( 2, 1, Qt::RightButton );
241   const QgsFeatureId newFid = utils.newFeatureId();
242 
243   // QCOMPARE( mLayer->featureCount(), ( long )1 );
244   const QgsFeature f = mLayer->getFeature( newFid );
245 
246   const QString wkt = "LineStringZ (0 0 111, 2 0 111, 2 1 111, 0 1 111, 0 0 111)";
247   QgsLineString line;
248   line.fromWkt( wkt );
249   QVERIFY( static_cast<QgsLineString *>( f.geometry().get() )->equals( line ) );
250 
251   mLayer->rollBack();
252   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 0 );
253 }
254 
testRectangleFrom3PointsProjected()255 void TestQgsMapToolRectangle::testRectangleFrom3PointsProjected()
256 {
257   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 111 );
258   mLayer->startEditing();
259 
260   QgsMapToolRectangle3Points mapTool( mParentTool, mCanvas, QgsMapToolRectangle3Points::ProjectedMode );
261   mCanvas->setMapTool( &mapTool );
262 
263   TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
264   utils.mouseClick( 0, 0, Qt::LeftButton );
265   utils.mouseMove( 2, 0 );
266   utils.mouseClick( 2, 0, Qt::LeftButton );
267   utils.mouseMove( 2, 1 );
268   utils.mouseClick( 2, 1, Qt::RightButton );
269   const QgsFeatureId newFid = utils.newFeatureId();
270 
271   // QCOMPARE( mLayer->featureCount(), ( long )1 );
272   const QgsFeature f = mLayer->getFeature( newFid );
273 
274   const QString wkt = "LineStringZ (0 0 111, 2 0 111, 2 1 111, 0 1 111, 0 0 111)";
275   QgsLineString line;
276   line.fromWkt( wkt );
277   QVERIFY( static_cast<QgsLineString *>( f.geometry().get() )->equals( line ) );
278 
279   mLayer->rollBack();
280   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 0 );
281 }
testRectangleFrom3PointsProjectedWithDeletedVertex()282 void TestQgsMapToolRectangle::testRectangleFrom3PointsProjectedWithDeletedVertex()
283 {
284   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 111 );
285   mLayer->startEditing();
286 
287   QgsMapToolRectangle3Points mapTool( mParentTool, mCanvas, QgsMapToolRectangle3Points::ProjectedMode );
288   mCanvas->setMapTool( &mapTool );
289 
290   TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
291   utils.mouseClick( 0, 0, Qt::LeftButton );
292   utils.mouseMove( 2, 0 );
293   utils.mouseClick( 3, 0, Qt::LeftButton );
294   utils.keyClick( Qt::Key_Backspace );
295   utils.mouseClick( 2, 0, Qt::LeftButton );
296   utils.mouseMove( 2, 1 );
297   utils.mouseClick( 2, 1, Qt::RightButton );
298   const QgsFeatureId newFid = utils.newFeatureId();
299 
300   // QCOMPARE( mLayer->featureCount(), ( long )1 );
301   const QgsFeature f = mLayer->getFeature( newFid );
302 
303   const QString wkt = "LineStringZ (0 0 111, 2 0 111, 2 1 111, 0 1 111, 0 0 111)";
304   QgsLineString line;
305   line.fromWkt( wkt );
306   QVERIFY( static_cast<QgsLineString *>( f.geometry().get() )->equals( line ) );
307 
308   mLayer->rollBack();
309   QgsSettingsRegistryCore::settingsDigitizingDefaultZValue.setValue( 0 );
310 }
311 QGSTEST_MAIN( TestQgsMapToolRectangle )
312 #include "testqgsmaptoolrectangle.moc"
313