1 /***************************************************************************
2 testqgsmaptoolregularpolygon.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 "qgsmaptoolregularpolygon2points.h"
28 #include "qgsmaptoolregularpolygoncenterpoint.h"
29 #include "qgsmaptoolregularpolygoncentercorner.h"
30
31
32 class TestQgsMapToolRegularPolygon : public QObject
33 {
34 Q_OBJECT
35
36 public:
37 TestQgsMapToolRegularPolygon();
38
39 private slots:
40 void initTestCase();
41 void cleanupTestCase();
42
43 void testRegularPolygonFrom2Points();
44 void testRegularPolygonFrom2PointsWithDeletedVertex();
45 void testRegularPolygonFromCenterAndPoint();
46 void testRegularPolygonFromCenterAndPointWithDeletedVertex();
47 void testRegularPolygonFromCenterAndCroner();
48 void testRegularPolygonFromCenterAndCronerWithDeletedVertex();
49
50 private:
51 QgisApp *mQgisApp = nullptr;
52 QgsMapToolCapture *mParentTool = nullptr;
53 QgsMapCanvas *mCanvas = nullptr;
54 QgsVectorLayer *mLayer = nullptr;
55 };
56
57 TestQgsMapToolRegularPolygon::TestQgsMapToolRegularPolygon() = default;
58
59
60 //runs before all tests
initTestCase()61 void TestQgsMapToolRegularPolygon::initTestCase()
62 {
63 QgsApplication::init();
64 QgsApplication::initQgis();
65
66 mQgisApp = new QgisApp();
67
68 mCanvas = new QgsMapCanvas();
69 mCanvas->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:27700" ) ) );
70
71 // make testing layers
72 mLayer = new QgsVectorLayer( QStringLiteral( "LineStringZ?crs=EPSG:27700" ), QStringLiteral( "layer line Z" ), QStringLiteral( "memory" ) );
73 QVERIFY( mLayer->isValid() );
74 QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayer );
75
76 // set layers in canvas
77 mCanvas->setLayers( QList<QgsMapLayer *>() << mLayer );
78 mCanvas->setCurrentLayer( mLayer );
79
80 mParentTool = new QgsMapToolAddFeature( mCanvas, QgsMapToolCapture::CaptureLine );
81 }
82
cleanupTestCase()83 void TestQgsMapToolRegularPolygon::cleanupTestCase()
84 {
85 QgsApplication::exitQgis();
86 }
87
testRegularPolygonFrom2Points()88 void TestQgsMapToolRegularPolygon::testRegularPolygonFrom2Points()
89 {
90 QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 333 );
91 mLayer->startEditing();
92
93 QgsMapToolRegularPolygon2Points mapTool( mParentTool, mCanvas );
94 mCanvas->setMapTool( &mapTool );
95
96 TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
97 utils.mouseClick( 0, 0, Qt::LeftButton );
98 utils.mouseMove( 2, 1 );
99 utils.mouseClick( 2, 1, Qt::RightButton );
100 QgsFeatureId newFid = utils.newFeatureId();
101
102 QCOMPARE( mLayer->featureCount(), ( long )1 );
103 QgsFeature f = mLayer->getFeature( newFid );
104
105 QString wkt = "LineStringZ (0 0 333, 2 1 333, 4 0 333, 4 -2 333, 2 -3 333, 0 -2 333, 0 0 333)";
106 QCOMPARE( f.geometry().asWkt( 0 ), wkt );
107
108 mLayer->rollBack();
109 QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
110 }
testRegularPolygonFrom2PointsWithDeletedVertex()111 void TestQgsMapToolRegularPolygon::testRegularPolygonFrom2PointsWithDeletedVertex()
112 {
113 QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 333 );
114 mLayer->startEditing();
115
116 QgsMapToolRegularPolygon2Points mapTool( mParentTool, mCanvas );
117 mCanvas->setMapTool( &mapTool );
118
119 TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
120 utils.mouseClick( 4, 1, Qt::LeftButton );
121 utils.keyClick( Qt::Key_Backspace );
122 utils.mouseClick( 0, 0, Qt::LeftButton );
123 utils.mouseMove( 2, 1 );
124 utils.mouseClick( 2, 1, Qt::RightButton );
125 QgsFeatureId newFid = utils.newFeatureId();
126
127 QCOMPARE( mLayer->featureCount(), ( long )1 );
128 QgsFeature f = mLayer->getFeature( newFid );
129
130 QString wkt = "LineStringZ (0 0 333, 2 1 333, 4 0 333, 4 -2 333, 2 -3 333, 0 -2 333, 0 0 333)";
131 QCOMPARE( f.geometry().asWkt( 0 ), wkt );
132
133 mLayer->rollBack();
134 QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
135 }
136
137
testRegularPolygonFromCenterAndPoint()138 void TestQgsMapToolRegularPolygon::testRegularPolygonFromCenterAndPoint()
139 {
140 QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 222 );
141 mLayer->startEditing();
142
143 QgsMapToolRegularPolygonCenterPoint mapTool( mParentTool, mCanvas );
144 mCanvas->setMapTool( &mapTool );
145
146 TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
147 utils.mouseClick( 0, 0, Qt::LeftButton );
148 utils.mouseMove( 2, 1 );
149 utils.mouseClick( 2, 1, Qt::RightButton );
150 QgsFeatureId newFid = utils.newFeatureId();
151
152 QCOMPARE( mLayer->featureCount(), ( long )1 );
153 QgsFeature f = mLayer->getFeature( newFid );
154
155 QString wkt = "LineStringZ (1 2 222, 3 0 222, 1 -2 222, -1 -2 222, -3 0 222, -1 2 222, 1 2 222)";
156 QCOMPARE( f.geometry().asWkt( 0 ), wkt );
157
158 mLayer->rollBack();
159 QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
160 }
testRegularPolygonFromCenterAndPointWithDeletedVertex()161 void TestQgsMapToolRegularPolygon::testRegularPolygonFromCenterAndPointWithDeletedVertex()
162 {
163 QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 222 );
164 mLayer->startEditing();
165
166 QgsMapToolRegularPolygonCenterPoint mapTool( mParentTool, mCanvas );
167 mCanvas->setMapTool( &mapTool );
168
169 TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
170 utils.mouseClick( 4, 1, Qt::LeftButton );
171 utils.keyClick( Qt::Key_Backspace );
172 utils.mouseClick( 0, 0, Qt::LeftButton );
173 utils.mouseMove( 2, 1 );
174 utils.mouseClick( 2, 1, Qt::RightButton );
175 QgsFeatureId newFid = utils.newFeatureId();
176
177 QCOMPARE( mLayer->featureCount(), ( long )1 );
178 QgsFeature f = mLayer->getFeature( newFid );
179
180 QString wkt = "LineStringZ (1 2 222, 3 0 222, 1 -2 222, -1 -2 222, -3 0 222, -1 2 222, 1 2 222)";
181 QCOMPARE( f.geometry().asWkt( 0 ), wkt );
182
183 mLayer->rollBack();
184 QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
185 }
186
187
testRegularPolygonFromCenterAndCroner()188 void TestQgsMapToolRegularPolygon::testRegularPolygonFromCenterAndCroner()
189 {
190 QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 111 );
191 mLayer->startEditing();
192
193 QgsMapToolRegularPolygonCenterCorner mapTool( mParentTool, mCanvas );
194 mCanvas->setMapTool( &mapTool );
195
196 TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
197 utils.mouseClick( 0, 0, Qt::LeftButton );
198 utils.mouseMove( 2, 1 );
199 utils.mouseClick( 2, 1, Qt::RightButton );
200 QgsFeatureId newFid = utils.newFeatureId();
201
202 QCOMPARE( mLayer->featureCount(), ( long )1 );
203 QgsFeature f = mLayer->getFeature( newFid );
204
205 QString wkt = "LineStringZ (2 1 111, 2 -1 111, 0 -2 111, -2 -1 111, -2 1 111, 0 2 111, 2 1 111)";
206 QCOMPARE( f.geometry().asWkt( 0 ), wkt );
207
208 mLayer->rollBack();
209 QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
210 }
testRegularPolygonFromCenterAndCronerWithDeletedVertex()211 void TestQgsMapToolRegularPolygon::testRegularPolygonFromCenterAndCronerWithDeletedVertex()
212 {
213 QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 111 );
214 mLayer->startEditing();
215
216 QgsMapToolRegularPolygonCenterCorner mapTool( mParentTool, mCanvas );
217 mCanvas->setMapTool( &mapTool );
218
219 TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
220 utils.mouseClick( 4, 1, Qt::LeftButton );
221 utils.keyClick( Qt::Key_Backspace );
222 utils.mouseClick( 0, 0, Qt::LeftButton );
223 utils.mouseMove( 2, 1 );
224 utils.mouseClick( 2, 1, Qt::RightButton );
225 QgsFeatureId newFid = utils.newFeatureId();
226
227 QCOMPARE( mLayer->featureCount(), ( long )1 );
228 QgsFeature f = mLayer->getFeature( newFid );
229
230 QString wkt = "LineStringZ (2 1 111, 2 -1 111, 0 -2 111, -2 -1 111, -2 1 111, 0 2 111, 2 1 111)";
231 QCOMPARE( f.geometry().asWkt( 0 ), wkt );
232
233 mLayer->rollBack();
234 QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
235 }
236
237
238 QGSTEST_MAIN( TestQgsMapToolRegularPolygon )
239 #include "testqgsmaptoolregularpolygon.moc"
240