1 /***************************************************************************
2      testqgsmaptoolmovefeature.cpp
3      --------------------------------
4     Date                 : August 2019
5     Copyright            : (C) 2019 by Loïc Bartoletti
6     Email                : loic.bartoletti@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 "qgsadvanceddigitizingdockwidget.h"
20 #include "qgsgeometry.h"
21 #include "qgsmapcanvas.h"
22 #include "qgsmapcanvassnappingutils.h"
23 #include "qgssnappingconfig.h"
24 #include "qgssnappingutils.h"
25 #include "qgsmaptoolmovefeature.h"
26 #include "qgsproject.h"
27 #include "qgssettings.h"
28 #include "qgsvectorlayer.h"
29 #include "qgsmapmouseevent.h"
30 #include "testqgsmaptoolutils.h"
31 
32 
33 /**
34  * \ingroup UnitTests
35  * This is a unit test for the vertex tool
36  */
37 class TestQgsMapToolMoveFeature: public QObject
38 {
39     Q_OBJECT
40   public:
41     TestQgsMapToolMoveFeature();
42 
43   private slots:
44     void initTestCase();// will be called before the first testfunction is executed.
45     void cleanupTestCase();// will be called after the last testfunction was executed.
46 
47     void testMoveFeature();
48     void testTopologicalMoveFeature();
49 
50   private:
51     QgisApp *mQgisApp = nullptr;
52     QgsMapCanvas *mCanvas = nullptr;
53     QgsMapToolMoveFeature *mCaptureTool = nullptr;
54     QgsVectorLayer *mLayerBase = nullptr;
55 };
56 
57 TestQgsMapToolMoveFeature::TestQgsMapToolMoveFeature() = default;
58 
59 
60 //runs before all tests
initTestCase()61 void TestQgsMapToolMoveFeature::initTestCase()
62 {
63   qDebug() << "TestMapToolCapture::initTestCase()";
64   // init QGIS's paths - true means that all path will be inited from prefix
65   QgsApplication::init();
66   QgsApplication::initQgis();
67 
68   // Set up the QSettings environment
69   QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
70   QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
71   QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );
72 
73   mQgisApp = new QgisApp();
74 
75   mCanvas = new QgsMapCanvas();
76 
77   mCanvas->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3946" ) ) );
78 
79   mCanvas->setFrameStyle( QFrame::NoFrame );
80   mCanvas->resize( 512, 512 );
81   mCanvas->setExtent( QgsRectangle( 0, 0, 8, 8 ) );
82   mCanvas->show(); // to make the canvas resize
83   mCanvas->hide();
84 
85   // make testing layers
86   mLayerBase = new QgsVectorLayer( QStringLiteral( "Polygon?crs=EPSG:3946" ), QStringLiteral( "baselayer" ), QStringLiteral( "memory" ) );
87   QVERIFY( mLayerBase->isValid() );
88   QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mLayerBase );
89 
90   mLayerBase->startEditing();
91   QString wkt1 = "Polygon ((0 0, 0 1, 1 1, 1 0))";
92   QgsFeature f1;
93   f1.setGeometry( QgsGeometry::fromWkt( wkt1 ) );
94   QString wkt2 = "Polygon ((2 0, 2 5, 3 5, 3 0))";
95   QgsFeature f2;
96   f2.setGeometry( QgsGeometry::fromWkt( wkt2 ) );
97 
98   QgsFeatureList flist;
99   flist << f1 << f2;
100   mLayerBase->dataProvider()->addFeatures( flist );
101   QCOMPARE( mLayerBase->featureCount(), ( long )2 );
102   QCOMPARE( mLayerBase->getFeature( 1 ).geometry().asWkt(), wkt1 );
103   QCOMPARE( mLayerBase->getFeature( 2 ).geometry().asWkt(), wkt2 );
104 
105   QgsSnappingConfig cfg = mCanvas->snappingUtils()->config();
106   cfg.setMode( QgsSnappingConfig::AllLayers );
107   cfg.setTolerance( 1 );
108   cfg.setTypeFlag( static_cast<QgsSnappingConfig::SnappingTypeFlag>( QgsSnappingConfig::VertexFlag | QgsSnappingConfig::SegmentFlag ) );
109   cfg.setEnabled( true );
110   mCanvas->snappingUtils()->setConfig( cfg );
111 
112   mCanvas->setLayers( QList<QgsMapLayer *>() << mLayerBase );
113   mCanvas->setCurrentLayer( mLayerBase );
114 
115   // create the tool
116   mCaptureTool = new QgsMapToolMoveFeature( mCanvas, QgsMapToolMoveFeature::Move );
117   mCanvas->setMapTool( mCaptureTool );
118 
119   QCOMPARE( mCanvas->mapSettings().outputSize(), QSize( 512, 512 ) );
120   QCOMPARE( mCanvas->mapSettings().visibleExtent(), QgsRectangle( 0, 0, 8, 8 ) );
121 }
122 
123 //runs after all tests
cleanupTestCase()124 void TestQgsMapToolMoveFeature::cleanupTestCase()
125 {
126   delete mCaptureTool;
127   delete mCanvas;
128   QgsApplication::exitQgis();
129 }
130 
testMoveFeature()131 void TestQgsMapToolMoveFeature::testMoveFeature()
132 {
133   TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool );
134 
135   utils.mouseClick( 1, 1, Qt::LeftButton, Qt::KeyboardModifiers(), true );
136   utils.mouseClick( 2, 1, Qt::LeftButton, Qt::KeyboardModifiers(), true );
137 
138   QString wkt1 = "Polygon ((1 0, 1 1, 2 1, 2 0))";
139   QCOMPARE( mLayerBase->getFeature( 1 ).geometry().asWkt(), wkt1 );
140   QString wkt2 = "Polygon ((2 0, 2 5, 3 5, 3 0))";
141   QCOMPARE( mLayerBase->getFeature( 2 ).geometry().asWkt(), wkt2 );
142 
143   mLayerBase->undoStack()->undo();
144 }
145 
testTopologicalMoveFeature()146 void TestQgsMapToolMoveFeature::testTopologicalMoveFeature()
147 {
148   bool topologicalEditing = QgsProject::instance()->topologicalEditing();
149   QgsProject::instance()->setTopologicalEditing( true );
150 
151   TestQgsMapToolAdvancedDigitizingUtils utils( mCaptureTool );
152 
153   utils.mouseClick( 1, 1, Qt::LeftButton, Qt::KeyboardModifiers(), true );
154   utils.mouseClick( 2, 1, Qt::LeftButton, Qt::KeyboardModifiers(), true );
155 
156   QString wkt1 = "Polygon ((1 0, 1 1, 2 1, 2 0))";
157   QCOMPARE( mLayerBase->getFeature( 1 ).geometry().asWkt(), wkt1 );
158   QString wkt2 = "Polygon ((2 0, 2 1, 2 5, 3 5, 3 0))";
159   QCOMPARE( mLayerBase->getFeature( 2 ).geometry().asWkt(), wkt2 );
160 
161   mLayerBase->undoStack()->undo();
162 
163   QgsProject::instance()->setTopologicalEditing( topologicalEditing );
164 }
165 
166 QGSTEST_MAIN( TestQgsMapToolMoveFeature )
167 #include "testqgsmaptoolmovefeature.moc"
168