Skip to content

Commit 12392f7

Browse files
committed
Some clean
1 parent 76a57b8 commit 12392f7

File tree

2 files changed

+58
-106
lines changed

2 files changed

+58
-106
lines changed

tests/src/app/testqgsmaptooladdfeature.cpp

Lines changed: 54 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "qgsproject.h"
2626
#include "qgssettings.h"
2727
#include "qgsvectorlayer.h"
28+
#include "testqgsmaptoolutils.h"
2829

2930

3031
bool operator==( const QgsGeometry &g1, const QgsGeometry &g2 )
@@ -45,25 +46,6 @@ namespace QTest
4546
}
4647
}
4748

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-
6749

6850
/**
6951
* \ingroup UnitTests
@@ -84,46 +66,6 @@ class TestQgsMapToolAddFeature : public QObject
8466
void testTracingWithOffset();
8567
void testZ();
8668

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-
12769
private:
12870
QgisApp *mQgisApp = nullptr;
12971
QgsMapCanvas *mCanvas = nullptr;
@@ -227,15 +169,17 @@ void TestQgsMapToolAddFeature::cleanupTestCase()
227169

228170
void TestQgsMapToolAddFeature::testNoTracing()
229171
{
172+
TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool );
173+
230174
// tracing not enabled - will be straight line
231175

232-
QSet<QgsFeatureId> oldFids = _existingFeatureIds( mLayerLine );
176+
QSet<QgsFeatureId> oldFids = utils.existingFeatureIds();
233177

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 );
237181

238-
QgsFeatureId newFid = _newFeatureId( mLayerLine, oldFids );
182+
QgsFeatureId newFid = utils.newFeatureId( oldFids );
239183

240184
QCOMPARE( mLayerLine->undoStack()->index(), 2 );
241185
QCOMPARE( mLayerLine->getFeature( newFid ).geometry(), QgsGeometry::fromWkt( "LINESTRING(1 1, 3 2)" ) );
@@ -246,17 +190,19 @@ void TestQgsMapToolAddFeature::testNoTracing()
246190

247191
void TestQgsMapToolAddFeature::testTracing()
248192
{
193+
TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool );
194+
249195
// tracing enabled - same clicks - now following line
250196

251197
mEnableTracingAction->setChecked( true );
252198

253-
QSet<QgsFeatureId> oldFids = _existingFeatureIds( mLayerLine );
199+
QSet<QgsFeatureId> oldFids = utils.existingFeatureIds();
254200

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 );
258204

259-
QgsFeatureId newFid = _newFeatureId( mLayerLine, oldFids );
205+
QgsFeatureId newFid = utils.newFeatureId( oldFids );
260206

261207
QCOMPARE( mLayerLine->undoStack()->index(), 2 );
262208
QCOMPARE( mLayerLine->getFeature( newFid ).geometry(), QgsGeometry::fromWkt( "LINESTRING(1 1, 2 1, 3 2)" ) );
@@ -268,13 +214,13 @@ void TestQgsMapToolAddFeature::testTracing()
268214

269215
// tracing enabled - combined with first and last segments that are not traced
270216

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 );
276222

277-
QgsFeatureId newFid2 = _newFeatureId( mLayerLine, oldFids );
223+
QgsFeatureId newFid2 = utils.newFeatureId( oldFids );
278224

279225
QCOMPARE( mLayerLine->undoStack()->index(), 2 );
280226
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()
289235

290236
void TestQgsMapToolAddFeature::testTracingWithOffset()
291237
{
238+
TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool );
239+
292240
// tracing enabled + offset enabled
293241

294242
mEnableTracingAction->setChecked( true );
295243
mTracer->setOffset( 0.1 );
296244

297-
QSet<QgsFeatureId> oldFids = _existingFeatureIds( mLayerLine );
245+
QSet<QgsFeatureId> oldFids = utils.existingFeatureIds();
298246

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 );
302250

303-
QgsFeatureId newFid = _newFeatureId( mLayerLine, oldFids );
251+
QgsFeatureId newFid = utils.newFeatureId( oldFids );
304252

305253
QCOMPARE( mLayerLine->undoStack()->index(), 2 );
306254

@@ -319,11 +267,11 @@ void TestQgsMapToolAddFeature::testTracingWithOffset()
319267
// use negative offset
320268
mTracer->setOffset( -0.1 );
321269

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 );
325273

326-
QgsFeatureId newFid2 = _newFeatureId( mLayerLine, oldFids );
274+
QgsFeatureId newFid2 = utils.newFeatureId( oldFids );
327275

328276
QgsGeometry g2 = mLayerLine->getFeature( newFid2 ).geometry();
329277
QgsPolylineXY poly2 = g2.asPolyline();
@@ -336,13 +284,13 @@ void TestQgsMapToolAddFeature::testTracingWithOffset()
336284

337285
// tracing enabled + offset enabled - combined with first and last segments that are not traced
338286

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 );
344292

345-
QgsFeatureId newFid3 = _newFeatureId( mLayerLine, oldFids );
293+
QgsFeatureId newFid3 = utils.newFeatureId( oldFids );
346294

347295
QCOMPARE( mLayerLine->undoStack()->index(), 2 );
348296
QgsGeometry g3 = mLayerLine->getFeature( newFid3 ).geometry();
@@ -365,18 +313,20 @@ void TestQgsMapToolAddFeature::testTracingWithOffset()
365313

366314
void TestQgsMapToolAddFeature::testZ()
367315
{
316+
TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool );
317+
368318
mCanvas->setCurrentLayer( mLayerLineZ );
369319

370320
// test with default Z value = 333
371321
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 333 );
372322

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 );
380330

381331
QString wkt = "LineStringZ (4 0 333, 5 0 333, 5 1 333, 4 1 333)";
382332
QCOMPARE( mLayerLineZ->getFeature( newFid ).geometry(), QgsGeometry::fromWkt( wkt ) );
@@ -386,13 +336,13 @@ void TestQgsMapToolAddFeature::testZ()
386336
// test with default Z value = 222
387337
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 222 );
388338

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 );
396346

397347
wkt = "LineStringZ (4 0 222, 5 0 222, 5 1 222, 4 1 222)";
398348
QCOMPARE( mLayerLineZ->getFeature( newFid ).geometry(), QgsGeometry::fromWkt( wkt ) );

tests/src/app/testqgsmaptoolutils.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
testqgsmaptoolutils.h
33
---------------------
44
Date : January 2018
5-
Copyright : (C) 2018 by Paul Blottiere
6-
Email : paul.blottiere@oslandia.com
5+
Copyright : (C) 2017 by Martin Dobias
6+
(C) 2018 by Paul Blottiere
7+
Email : wonder dot sk at gmail dot com
8+
paul.blottiere@oslandia.com
79
***************************************************************************
810
* *
911
* This program is free software; you can redistribute it and/or modify *

0 commit comments

Comments
 (0)