Skip to content

Commit 916e4ba

Browse files
committed
Add some tests for circular string map tools
1 parent 47ba3a0 commit 916e4ba

7 files changed

+240
-3
lines changed

src/app/qgisapp.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,7 @@ QgisApp::QgisApp()
13111311
mMapCanvas->freeze();
13121312
mLayerTreeView = new QgsLayerTreeView( this );
13131313
mUndoWidget = new QgsUndoWidget( nullptr, mMapCanvas );
1314+
mUserInputDockWidget = new QgsUserInputDockWidget( this );
13141315
mInfoBar = new QgsMessageBar( centralWidget() );
13151316
mAdvancedDigitizingDockWidget = new QgsAdvancedDigitizingDockWidget( mMapCanvas, this );
13161317
mPanelMenu = new QMenu( this );

src/app/qgsmaptooladdcircularstring.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
#define QGSMAPTOOLADDCIRCULARSTRING_H
1818

1919
#include "qgsmaptoolcapture.h"
20+
#include "qgis_app.h"
2021

2122
class QgsGeometryRubberBand;
2223

23-
class QgsMapToolAddCircularString: public QgsMapToolCapture
24+
class APP_EXPORT QgsMapToolAddCircularString: public QgsMapToolCapture
2425
{
2526
Q_OBJECT
2627
public:

src/app/qgsmaptoolcircularstringcurvepoint.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
#define QGSMAPTOOLCIRCULARSTRINGCURVEPOINT_H
1919

2020
#include "qgsmaptooladdcircularstring.h"
21+
#include "qgis_app.h"
2122

22-
class QgsMapToolCircularStringCurvePoint: public QgsMapToolAddCircularString
23+
class APP_EXPORT QgsMapToolCircularStringCurvePoint: public QgsMapToolAddCircularString
2324
{
2425
Q_OBJECT
2526

src/app/qgsmaptoolcircularstringradius.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919

2020
#include "qgsmaptooladdcircularstring.h"
2121
#include "qgspoint.h"
22+
#include "qgis_app.h"
2223

2324
class QDoubleSpinBox;
2425

25-
class QgsMapToolCircularStringRadius: public QgsMapToolAddCircularString
26+
class APP_EXPORT QgsMapToolCircularStringRadius: public QgsMapToolAddCircularString
2627
{
2728
Q_OBJECT
2829
public:

tests/src/app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ ADD_QGIS_TEST(maptooladdfeature testqgsmaptooladdfeature.cpp)
9999
ADD_QGIS_TEST(maptoolidentifyaction testqgsmaptoolidentifyaction.cpp)
100100
ADD_QGIS_TEST(maptoolselect testqgsmaptoolselect.cpp)
101101
ADD_QGIS_TEST(maptoolreshape testqgsmaptoolreshape.cpp)
102+
ADD_QGIS_TEST(maptoolcircularstringtest testqgsmaptoolcircularstring.cpp)
102103
ADD_QGIS_TEST(measuretool testqgsmeasuretool.cpp)
103104
ADD_QGIS_TEST(vertextool testqgsvertextool.cpp)
104105
ADD_QGIS_TEST(vectorlayersaveasdialogtest testqgsvectorlayersaveasdialog.cpp)
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/***************************************************************************
2+
testqgsmaptoolcircularstring.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+
25+
#include "testqgsmaptoolutils.h"
26+
#include "qgsmaptoolcircularstringcurvepoint.h"
27+
#include "qgsmaptoolcircularstringradius.h"
28+
29+
30+
class TestQgsMapToolCircularString : public QObject
31+
{
32+
Q_OBJECT
33+
34+
public:
35+
TestQgsMapToolCircularString();
36+
37+
private slots:
38+
void initTestCase();
39+
void cleanupTestCase();
40+
41+
void testAddCircularStringCurvePoint();
42+
void testAddCircularStringRadius();
43+
44+
private:
45+
QgisApp *mQgisApp = nullptr;
46+
QgsMapToolCapture *mParentTool = nullptr;
47+
QgsMapCanvas *mCanvas = nullptr;
48+
QgsVectorLayer *mLayer = nullptr;
49+
};
50+
51+
TestQgsMapToolCircularString::TestQgsMapToolCircularString() = default;
52+
53+
54+
//runs before all tests
55+
void TestQgsMapToolCircularString::initTestCase()
56+
{
57+
QgsApplication::init();
58+
QgsApplication::initQgis();
59+
60+
mQgisApp = new QgisApp();
61+
62+
mCanvas = new QgsMapCanvas();
63+
mCanvas->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:27700" ) ) );
64+
65+
// make testing layers
66+
mLayer = new QgsVectorLayer( QStringLiteral( "LineStringZ?crs=EPSG:27700" ), QStringLiteral( "layer line Z" ), QStringLiteral( "memory" ) );
67+
QVERIFY( mLayer->isValid() );
68+
QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayer );
69+
70+
// set layers in canvas
71+
mCanvas->setLayers( QList<QgsMapLayer *>() << mLayer );
72+
mCanvas->setCurrentLayer( mLayer );
73+
74+
mParentTool = new QgsMapToolAddFeature( mCanvas, QgsMapToolCapture::CaptureLine );
75+
}
76+
77+
void TestQgsMapToolCircularString::cleanupTestCase()
78+
{
79+
QgsApplication::exitQgis();
80+
}
81+
82+
void TestQgsMapToolCircularString::testAddCircularStringCurvePoint()
83+
{
84+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 333 );
85+
mLayer->startEditing();
86+
87+
QgsMapToolCircularStringCurvePoint mapTool( mParentTool, mCanvas );
88+
mCanvas->setMapTool( &mapTool );
89+
90+
TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
91+
utils.mouseClick( 0, 0, Qt::LeftButton );
92+
utils.mouseClick( 1, 1, Qt::LeftButton );
93+
utils.mouseClick( 0, 2, Qt::LeftButton );
94+
utils.mouseClick( 0, 2, Qt::RightButton );
95+
QgsFeatureId newFid = utils.newFeatureId();
96+
97+
QCOMPARE( mLayer->featureCount(), ( long )1 );
98+
QgsFeature f = mLayer->getFeature( newFid );
99+
100+
QString wkt = "CompoundCurveZ (CircularStringZ (0 0 333, 1 1 333, 0 2 333))";
101+
QCOMPARE( f.geometry().asWkt(), wkt );
102+
103+
mLayer->rollBack();
104+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
105+
}
106+
107+
void TestQgsMapToolCircularString::testAddCircularStringRadius()
108+
{
109+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 111 );
110+
mLayer->startEditing();
111+
112+
QgsMapToolCircularStringRadius mapTool( mParentTool, mCanvas );
113+
mCanvas->setMapTool( &mapTool );
114+
115+
TestQgsMapToolAdvancedDigitizingUtils utils( &mapTool );
116+
utils.mouseClick( 0, 0, Qt::LeftButton );
117+
utils.mouseClick( 1, 1, Qt::LeftButton );
118+
utils.mouseClick( 0, 2, Qt::LeftButton );
119+
utils.mouseClick( 0, 2, Qt::RightButton );
120+
QgsFeatureId newFid = utils.newFeatureId();
121+
122+
QCOMPARE( mLayer->featureCount(), ( long )1 );
123+
QgsFeature f = mLayer->getFeature( newFid );
124+
125+
QString wkt = "CompoundCurveZ (CircularStringZ (0 0 111, 0.17912878474779187 0.82087121525220819 111, 1 1 111))";
126+
QCOMPARE( f.geometry().asWkt(), wkt );
127+
128+
mLayer->rollBack();
129+
QgsSettings().setValue( QStringLiteral( "/qgis/digitizing/default_z_value" ), 0 );
130+
}
131+
132+
QGSTEST_MAIN( TestQgsMapToolCircularString )
133+
#include "testqgsmaptoolcircularstring.moc"

tests/src/app/testqgsmaptoolutils.h

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/***************************************************************************
2+
testqgsmaptoolutils.h
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 "qgsvectorlayer.h"
22+
23+
/**
24+
* \ingroup UnitTests
25+
*/
26+
class TestQgsMapToolAdvancedDigitizingUtils
27+
{
28+
public:
29+
TestQgsMapToolAdvancedDigitizingUtils( QgsMapToolAdvancedDigitizing *mapTool )
30+
: mMapTool( mapTool )
31+
{
32+
}
33+
34+
QSet<QgsFeatureId> existingFeatureIds()
35+
{
36+
QSet<QgsFeatureId> fids;
37+
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mMapTool->canvas()->currentLayer() );
38+
39+
if ( vl )
40+
{
41+
QgsFeature f;
42+
QgsFeatureIterator it = vl->getFeatures();
43+
while ( it.nextFeature( f ) )
44+
fids << f.id();
45+
}
46+
47+
return fids;
48+
}
49+
50+
QgsFeatureId newFeatureId( QSet<QgsFeatureId> oldFids = QSet<QgsFeatureId>() )
51+
{
52+
QSet<QgsFeatureId> newFids = existingFeatureIds();
53+
QSet<QgsFeatureId> diffFids = newFids.subtract( oldFids );
54+
Q_ASSERT( diffFids.count() == 1 );
55+
return *diffFids.constBegin();
56+
}
57+
58+
QPoint mapToScreen( double mapX, double mapY )
59+
{
60+
QgsPointXY pt = mMapTool->canvas()->mapSettings().mapToPixel().transform( mapX, mapY );
61+
return QPoint( std::round( pt.x() ), std::round( pt.y() ) );
62+
}
63+
64+
void mouseMove( double mapX, double mapY )
65+
{
66+
QgsMapMouseEvent e( mMapTool->canvas(), QEvent::MouseMove, mapToScreen( mapX, mapY ) );
67+
mMapTool->cadCanvasMoveEvent( &e );
68+
}
69+
70+
void mousePress( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() )
71+
{
72+
QgsMapMouseEvent e1( mMapTool->canvas(), QEvent::MouseButtonPress, mapToScreen( mapX, mapY ), button, button, stateKey );
73+
mMapTool->cadCanvasPressEvent( &e1 );
74+
}
75+
76+
void mouseRelease( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() )
77+
{
78+
QgsMapMouseEvent e2( mMapTool->canvas(), QEvent::MouseButtonRelease, mapToScreen( mapX, mapY ), button, Qt::MouseButton(), stateKey );
79+
mMapTool->cadCanvasReleaseEvent( &e2 );
80+
}
81+
82+
void mouseClick( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() )
83+
{
84+
mousePress( mapX, mapY, button, stateKey );
85+
mouseRelease( mapX, mapY, button, stateKey );
86+
}
87+
88+
void keyClick( int key )
89+
{
90+
QKeyEvent e1( QEvent::KeyPress, key, Qt::KeyboardModifiers() );
91+
mMapTool->keyPressEvent( &e1 );
92+
93+
QKeyEvent e2( QEvent::KeyRelease, key, Qt::KeyboardModifiers() );
94+
mMapTool->keyReleaseEvent( &e2 );
95+
}
96+
97+
private:
98+
QgsMapToolAdvancedDigitizing *mMapTool = nullptr;
99+
};

0 commit comments

Comments
 (0)