25
25
#include " qgsproject.h"
26
26
#include " qgssettings.h"
27
27
#include " qgsvectorlayer.h"
28
+ #include " testqgsmaptoolutils.h"
28
29
29
30
30
31
bool operator ==( const QgsGeometry &g1, const QgsGeometry &g2 )
@@ -45,25 +46,6 @@ namespace QTest
45
46
}
46
47
}
47
48
48
- static QSet<QgsFeatureId> _existingFeatureIds ( QgsVectorLayer *layer )
49
- {
50
- QSet<QgsFeatureId> fids;
51
- QgsFeature f;
52
- QgsFeatureIterator it = layer->getFeatures ();
53
- while ( it.nextFeature ( f ) )
54
- fids << f.id ();
55
- return fids;
56
- }
57
-
58
- static QgsFeatureId _newFeatureId ( QgsVectorLayer *layer, QSet<QgsFeatureId> oldFids )
59
- {
60
- QSet<QgsFeatureId> newFids = _existingFeatureIds ( layer );
61
- QSet<QgsFeatureId> diffFids = newFids.subtract ( oldFids );
62
- Q_ASSERT ( diffFids.count () == 1 );
63
- return *diffFids.constBegin ();
64
- }
65
-
66
-
67
49
68
50
/* *
69
51
* \ingroup UnitTests
@@ -84,46 +66,6 @@ class TestQgsMapToolAddFeature : public QObject
84
66
void testTracingWithOffset ();
85
67
void testZ ();
86
68
87
- private:
88
- QPoint mapToScreen ( double mapX, double mapY )
89
- {
90
- QgsPointXY pt = mCanvas ->mapSettings ().mapToPixel ().transform ( mapX, mapY );
91
- return QPoint ( std::round ( pt.x () ), std::round ( pt.y () ) );
92
- }
93
-
94
- void mouseMove ( double mapX, double mapY )
95
- {
96
- QgsMapMouseEvent e ( mCanvas , QEvent::MouseMove, mapToScreen ( mapX, mapY ) );
97
- mCaptureTool ->cadCanvasMoveEvent ( &e );
98
- }
99
-
100
- void mousePress ( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() )
101
- {
102
- QgsMapMouseEvent e1 ( mCanvas , QEvent::MouseButtonPress, mapToScreen ( mapX, mapY ), button, button, stateKey );
103
- mCaptureTool ->cadCanvasPressEvent ( &e1 );
104
- }
105
-
106
- void mouseRelease ( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() )
107
- {
108
- QgsMapMouseEvent e2 ( mCanvas , QEvent::MouseButtonRelease, mapToScreen ( mapX, mapY ), button, Qt::MouseButton (), stateKey );
109
- mCaptureTool ->cadCanvasReleaseEvent ( &e2 );
110
- }
111
-
112
- void mouseClick ( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() )
113
- {
114
- mousePress ( mapX, mapY, button, stateKey );
115
- mouseRelease ( mapX, mapY, button, stateKey );
116
- }
117
-
118
- void keyClick ( int key )
119
- {
120
- QKeyEvent e1 ( QEvent::KeyPress, key, Qt::KeyboardModifiers () );
121
- mCaptureTool ->keyPressEvent ( &e1 );
122
-
123
- QKeyEvent e2 ( QEvent::KeyRelease, key, Qt::KeyboardModifiers () );
124
- mCaptureTool ->keyReleaseEvent ( &e2 );
125
- }
126
-
127
69
private:
128
70
QgisApp *mQgisApp = nullptr ;
129
71
QgsMapCanvas *mCanvas = nullptr ;
@@ -227,15 +169,17 @@ void TestQgsMapToolAddFeature::cleanupTestCase()
227
169
228
170
void TestQgsMapToolAddFeature::testNoTracing ()
229
171
{
172
+ TestQgsMapToolAdvancedDigitizingUtils utils ( mCaptureTool );
173
+
230
174
// tracing not enabled - will be straight line
231
175
232
- QSet<QgsFeatureId> oldFids = _existingFeatureIds ( mLayerLine );
176
+ QSet<QgsFeatureId> oldFids = utils. existingFeatureIds ( );
233
177
234
- mouseClick ( 1 , 1 , Qt::LeftButton );
235
- mouseClick ( 3 , 2 , Qt::LeftButton );
236
- mouseClick ( 3 , 2 , Qt::RightButton );
178
+ utils. mouseClick ( 1 , 1 , Qt::LeftButton );
179
+ utils. mouseClick ( 3 , 2 , Qt::LeftButton );
180
+ utils. mouseClick ( 3 , 2 , Qt::RightButton );
237
181
238
- QgsFeatureId newFid = _newFeatureId ( mLayerLine , oldFids );
182
+ QgsFeatureId newFid = utils. newFeatureId ( oldFids );
239
183
240
184
QCOMPARE ( mLayerLine ->undoStack ()->index (), 2 );
241
185
QCOMPARE ( mLayerLine ->getFeature ( newFid ).geometry (), QgsGeometry::fromWkt ( " LINESTRING(1 1, 3 2)" ) );
@@ -246,17 +190,19 @@ void TestQgsMapToolAddFeature::testNoTracing()
246
190
247
191
void TestQgsMapToolAddFeature::testTracing ()
248
192
{
193
+ TestQgsMapToolAdvancedDigitizingUtils utils ( mCaptureTool );
194
+
249
195
// tracing enabled - same clicks - now following line
250
196
251
197
mEnableTracingAction ->setChecked ( true );
252
198
253
- QSet<QgsFeatureId> oldFids = _existingFeatureIds ( mLayerLine );
199
+ QSet<QgsFeatureId> oldFids = utils. existingFeatureIds ( );
254
200
255
- mouseClick ( 1 , 1 , Qt::LeftButton );
256
- mouseClick ( 3 , 2 , Qt::LeftButton );
257
- mouseClick ( 3 , 2 , Qt::RightButton );
201
+ utils. mouseClick ( 1 , 1 , Qt::LeftButton );
202
+ utils. mouseClick ( 3 , 2 , Qt::LeftButton );
203
+ utils. mouseClick ( 3 , 2 , Qt::RightButton );
258
204
259
- QgsFeatureId newFid = _newFeatureId ( mLayerLine , oldFids );
205
+ QgsFeatureId newFid = utils. newFeatureId ( oldFids );
260
206
261
207
QCOMPARE ( mLayerLine ->undoStack ()->index (), 2 );
262
208
QCOMPARE ( mLayerLine ->getFeature ( newFid ).geometry (), QgsGeometry::fromWkt ( " LINESTRING(1 1, 2 1, 3 2)" ) );
@@ -268,13 +214,13 @@ void TestQgsMapToolAddFeature::testTracing()
268
214
269
215
// tracing enabled - combined with first and last segments that are not traced
270
216
271
- mouseClick ( 0 , 2 , Qt::LeftButton );
272
- mouseClick ( 1 , 1 , Qt::LeftButton );
273
- mouseClick ( 3 , 2 , Qt::LeftButton );
274
- mouseClick ( 4 , 1 , Qt::LeftButton );
275
- mouseClick ( 4 , 1 , Qt::RightButton );
217
+ utils. mouseClick ( 0 , 2 , Qt::LeftButton );
218
+ utils. mouseClick ( 1 , 1 , Qt::LeftButton );
219
+ utils. mouseClick ( 3 , 2 , Qt::LeftButton );
220
+ utils. mouseClick ( 4 , 1 , Qt::LeftButton );
221
+ utils. mouseClick ( 4 , 1 , Qt::RightButton );
276
222
277
- QgsFeatureId newFid2 = _newFeatureId ( mLayerLine , oldFids );
223
+ QgsFeatureId newFid2 = utils. newFeatureId ( oldFids );
278
224
279
225
QCOMPARE ( mLayerLine ->undoStack ()->index (), 2 );
280
226
QCOMPARE ( mLayerLine ->getFeature ( newFid2 ).geometry (), QgsGeometry::fromWkt ( " LINESTRING(0 2, 1 1, 2 1, 3 2, 4 1)" ) );
@@ -289,18 +235,20 @@ void TestQgsMapToolAddFeature::testTracing()
289
235
290
236
void TestQgsMapToolAddFeature::testTracingWithOffset ()
291
237
{
238
+ TestQgsMapToolAdvancedDigitizingUtils utils ( mCaptureTool );
239
+
292
240
// tracing enabled + offset enabled
293
241
294
242
mEnableTracingAction ->setChecked ( true );
295
243
mTracer ->setOffset ( 0.1 );
296
244
297
- QSet<QgsFeatureId> oldFids = _existingFeatureIds ( mLayerLine );
245
+ QSet<QgsFeatureId> oldFids = utils. existingFeatureIds ( );
298
246
299
- mouseClick ( 2 , 1 , Qt::LeftButton );
300
- mouseClick ( 1 , 2 , Qt::LeftButton );
301
- mouseClick ( 1 , 2 , Qt::RightButton );
247
+ utils. mouseClick ( 2 , 1 , Qt::LeftButton );
248
+ utils. mouseClick ( 1 , 2 , Qt::LeftButton );
249
+ utils. mouseClick ( 1 , 2 , Qt::RightButton );
302
250
303
- QgsFeatureId newFid = _newFeatureId ( mLayerLine , oldFids );
251
+ QgsFeatureId newFid = utils. newFeatureId ( oldFids );
304
252
305
253
QCOMPARE ( mLayerLine ->undoStack ()->index (), 2 );
306
254
@@ -319,11 +267,11 @@ void TestQgsMapToolAddFeature::testTracingWithOffset()
319
267
// use negative offset
320
268
mTracer ->setOffset ( -0.1 );
321
269
322
- mouseClick ( 2 , 1 , Qt::LeftButton );
323
- mouseClick ( 1 , 2 , Qt::LeftButton );
324
- mouseClick ( 1 , 2 , Qt::RightButton );
270
+ utils. mouseClick ( 2 , 1 , Qt::LeftButton );
271
+ utils. mouseClick ( 1 , 2 , Qt::LeftButton );
272
+ utils. mouseClick ( 1 , 2 , Qt::RightButton );
325
273
326
- QgsFeatureId newFid2 = _newFeatureId ( mLayerLine , oldFids );
274
+ QgsFeatureId newFid2 = utils. newFeatureId ( oldFids );
327
275
328
276
QgsGeometry g2 = mLayerLine ->getFeature ( newFid2 ).geometry ();
329
277
QgsPolylineXY poly2 = g2.asPolyline ();
@@ -336,13 +284,13 @@ void TestQgsMapToolAddFeature::testTracingWithOffset()
336
284
337
285
// tracing enabled + offset enabled - combined with first and last segments that are not traced
338
286
339
- mouseClick ( 3 , 0 , Qt::LeftButton );
340
- mouseClick ( 2 , 1 , Qt::LeftButton );
341
- mouseClick ( 1 , 2 , Qt::LeftButton );
342
- mouseClick ( 0 , 1 , Qt::LeftButton );
343
- mouseClick ( 0 , 1 , Qt::RightButton );
287
+ utils. mouseClick ( 3 , 0 , Qt::LeftButton );
288
+ utils. mouseClick ( 2 , 1 , Qt::LeftButton );
289
+ utils. mouseClick ( 1 , 2 , Qt::LeftButton );
290
+ utils. mouseClick ( 0 , 1 , Qt::LeftButton );
291
+ utils. mouseClick ( 0 , 1 , Qt::RightButton );
344
292
345
- QgsFeatureId newFid3 = _newFeatureId ( mLayerLine , oldFids );
293
+ QgsFeatureId newFid3 = utils. newFeatureId ( oldFids );
346
294
347
295
QCOMPARE ( mLayerLine ->undoStack ()->index (), 2 );
348
296
QgsGeometry g3 = mLayerLine ->getFeature ( newFid3 ).geometry ();
@@ -365,18 +313,20 @@ void TestQgsMapToolAddFeature::testTracingWithOffset()
365
313
366
314
void TestQgsMapToolAddFeature::testZ ()
367
315
{
316
+ TestQgsMapToolAdvancedDigitizingUtils utils ( mCaptureTool );
317
+
368
318
mCanvas ->setCurrentLayer ( mLayerLineZ );
369
319
370
320
// test with default Z value = 333
371
321
QgsSettings ().setValue ( QStringLiteral ( " /qgis/digitizing/default_z_value" ), 333 );
372
322
373
- QSet<QgsFeatureId> oldFids = _existingFeatureIds ( mLayerLineZ );
374
- mouseClick ( 4 , 0 , Qt::LeftButton );
375
- mouseClick ( 5 , 0 , Qt::LeftButton );
376
- mouseClick ( 5 , 1 , Qt::LeftButton );
377
- mouseClick ( 4 , 1 , Qt::LeftButton );
378
- mouseClick ( 4 , 1 , Qt::RightButton );
379
- QgsFeatureId newFid = _newFeatureId ( mLayerLineZ , oldFids );
323
+ QSet<QgsFeatureId> oldFids = utils. existingFeatureIds ( );
324
+ utils. mouseClick ( 4 , 0 , Qt::LeftButton );
325
+ utils. mouseClick ( 5 , 0 , Qt::LeftButton );
326
+ utils. mouseClick ( 5 , 1 , Qt::LeftButton );
327
+ utils. mouseClick ( 4 , 1 , Qt::LeftButton );
328
+ utils. mouseClick ( 4 , 1 , Qt::RightButton );
329
+ QgsFeatureId newFid = utils. newFeatureId ( oldFids );
380
330
381
331
QString wkt = " LineStringZ (4 0 333, 5 0 333, 5 1 333, 4 1 333)" ;
382
332
QCOMPARE ( mLayerLineZ ->getFeature ( newFid ).geometry (), QgsGeometry::fromWkt ( wkt ) );
@@ -386,13 +336,13 @@ void TestQgsMapToolAddFeature::testZ()
386
336
// test with default Z value = 222
387
337
QgsSettings ().setValue ( QStringLiteral ( " /qgis/digitizing/default_z_value" ), 222 );
388
338
389
- oldFids = _existingFeatureIds ( mLayerLineZ );
390
- mouseClick ( 4 , 0 , Qt::LeftButton );
391
- mouseClick ( 5 , 0 , Qt::LeftButton );
392
- mouseClick ( 5 , 1 , Qt::LeftButton );
393
- mouseClick ( 4 , 1 , Qt::LeftButton );
394
- mouseClick ( 4 , 1 , Qt::RightButton );
395
- newFid = _newFeatureId ( mLayerLineZ , oldFids );
339
+ oldFids = utils. existingFeatureIds ( );
340
+ utils. mouseClick ( 4 , 0 , Qt::LeftButton );
341
+ utils. mouseClick ( 5 , 0 , Qt::LeftButton );
342
+ utils. mouseClick ( 5 , 1 , Qt::LeftButton );
343
+ utils. mouseClick ( 4 , 1 , Qt::LeftButton );
344
+ utils. mouseClick ( 4 , 1 , Qt::RightButton );
345
+ newFid = utils. newFeatureId ( oldFids );
396
346
397
347
wkt = " LineStringZ (4 0 222, 5 0 222, 5 1 222, 4 1 222)" ;
398
348
QCOMPARE ( mLayerLineZ ->getFeature ( newFid ).geometry (), QgsGeometry::fromWkt ( wkt ) );
0 commit comments