1 /***************************************************************************
2      testqgsmaptoolsplitparts.cpp
3      ------------------------
4     Date                 : February 2020
5     Copyright            : (C) 2020 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 "qgsgeometry.h"
20 #include "qgsmapcanvas.h"
21 #include "qgssettings.h"
22 #include "qgsvectorlayer.h"
23 #include "qgsmaptoolsplitparts.h"
24 #include "qgsgeometryutils.h"
25 
26 #include "testqgsmaptoolutils.h"
27 
28 class TestQgsMapToolSplitParts : public QObject
29 {
30     Q_OBJECT
31 
32   public:
33     TestQgsMapToolSplitParts();
34 
35   private slots:
36     void initTestCase();
37     void cleanupTestCase();
38 
39     void testSplitMultiLineString();
40 
41   private:
42     QPoint mapToPoint( double x, double y );
43     QgisApp *mQgisApp = nullptr;
44     QgsMapCanvas *mCanvas = nullptr;
45     QgsVectorLayer *mMultiLineStringLayer = nullptr;
46     QgsFeature lineF1, lineF2;
47 };
48 
49 TestQgsMapToolSplitParts::TestQgsMapToolSplitParts() = default;
50 
51 
52 //runs before all tests
initTestCase()53 void TestQgsMapToolSplitParts::initTestCase()
54 {
55   QgsApplication::init();
56   QgsApplication::initQgis();
57 
58   mQgisApp = new QgisApp();
59 
60   mCanvas = new QgsMapCanvas();
61   mCanvas->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3946" ) ) );
62 
63   // make testing layers
64   mMultiLineStringLayer = new QgsVectorLayer( QStringLiteral( "MultiLineString?crs=EPSG:3946" ), QStringLiteral( "layer multiline" ), QStringLiteral( "memory" ) );
65   QVERIFY( mMultiLineStringLayer->isValid() );
66   mMultiLineStringLayer->startEditing();
67   lineF1.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "MultiLineString ((0 0, 10 0))" ) ) );
68   lineF2.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "MultiLineString ((0 5, 10 5),(10 5, 15 5))" ) ) );
69   mMultiLineStringLayer->addFeature( lineF1 );
70   mMultiLineStringLayer->addFeature( lineF2 );
71 
72   mCanvas->setFrameStyle( QFrame::NoFrame );
73   mCanvas->resize( 50, 50 );
74   mCanvas->setExtent( QgsRectangle( 0, 0, 10, 10 ) );
75   mCanvas->show(); // to make the canvas resize
76   mCanvas->hide();
77   // Disable flaky tests on windows...
78   // QCOMPARE( mCanvas->mapSettings().outputSize(), QSize( 50, 50 ) );
79   // QCOMPARE( mCanvas->mapSettings().visibleExtent(), QgsRectangle( 0, 0, 10, 10 ) );
80 
81   QgsProject::instance()->addMapLayers( QList<QgsMapLayer *>() << mMultiLineStringLayer );
82 
83   // set layers in canvas
84   mCanvas->setLayers( QList<QgsMapLayer *>() << mMultiLineStringLayer );
85 }
86 
cleanupTestCase()87 void TestQgsMapToolSplitParts::cleanupTestCase()
88 {
89   QgsApplication::exitQgis();
90 }
91 
mapToPoint(double x,double y)92 QPoint TestQgsMapToolSplitParts::mapToPoint( double x, double y )
93 {
94 
95   QgsPointXY mapPoint = mCanvas->mapSettings().mapToPixel().transform( x, y );
96 
97   return QPoint( std::round( mapPoint.x() ), std::round( mapPoint.y() ) );
98 }
99 
testSplitMultiLineString()100 void TestQgsMapToolSplitParts::testSplitMultiLineString()
101 {
102 
103   mCanvas->setCurrentLayer( mMultiLineStringLayer );
104   QgsMapToolSplitParts *mapTool = new QgsMapToolSplitParts( mCanvas ) ;
105   mCanvas->setMapTool( mapTool );
106 
107   std::unique_ptr< QgsMapMouseEvent > event( new QgsMapMouseEvent(
108         mCanvas,
109         QEvent::MouseButtonRelease,
110         mapToPoint( 4, 7 ),
111         Qt::LeftButton
112       ) );
113   mapTool->cadCanvasReleaseEvent( event.get() );
114   event.reset( new QgsMapMouseEvent(
115                  mCanvas,
116                  QEvent::MouseButtonRelease,
117                  mapToPoint( 4, -1 ),
118                  Qt::LeftButton
119                ) );
120   mapTool->cadCanvasReleaseEvent( event.get() );
121 
122   event.reset( new QgsMapMouseEvent(
123                  mCanvas,
124                  QEvent::MouseButtonRelease,
125                  mapToPoint( 4, -1 ),
126                  Qt::RightButton
127                ) );
128   mapTool->cadCanvasReleaseEvent( event.get() );
129 
130 
131   QCOMPARE( mMultiLineStringLayer->featureCount(), ( long )2 );
132   QCOMPARE( mMultiLineStringLayer->getFeature( lineF1.id() ).geometry().asWkt(), QStringLiteral( "MultiLineString ((0 0, 4 0),(4 0, 10 0))" ) );
133   QCOMPARE( mMultiLineStringLayer->getFeature( lineF2.id() ).geometry().asWkt(), QStringLiteral( "MultiLineString ((0 5, 4 5),(4 5, 10 5),(10 5, 15 5))" ) );
134 
135   mMultiLineStringLayer->rollBack();
136 }
137 
138 QGSTEST_MAIN( TestQgsMapToolSplitParts )
139 #include "testqgsmaptoolsplitparts.moc"
140