1 /***************************************************************************
2     testqgsmaptooledit.cpp
3      --------------------------------------
4     Date                 : September 2021
5     Copyright            : (C) 2021 Nyall Dawson
6     Email                : nyall dot dawson at gmail dot 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 #include <QCoreApplication>
16 
17 #include "qgstest.h"
18 #include "qgsguiutils.h"
19 #include "qgsmaptoolcapture.h"
20 #include "qgsapplication.h"
21 #include "qgsmapcanvas.h"
22 #include "qgslogger.h"
23 #include "qgsannotationlayer.h"
24 #include "qgsadvanceddigitizingdockwidget.h"
25 
26 class TestQgsMapToolCapture : public QObject
27 {
28     Q_OBJECT
29   public:
30     TestQgsMapToolCapture() = default;
31 
32   private slots:
33     void initTestCase(); // will be called before the first testfunction is executed.
34     void cleanupTestCase(); // will be called after the last testfunction was executed.
35     void init(); // will be called before each testfunction is executed.
36     void cleanup(); // will be called after every testfunction.
37 
38     void addVertexNonVectorLayer();
39     void addVertexNonVectorLayerTransform();
40 
41 };
42 
initTestCase()43 void TestQgsMapToolCapture::initTestCase()
44 {
45   QgsApplication::init();
46   QgsApplication::initQgis();
47   QgsApplication::showSettings();
48 }
49 
cleanupTestCase()50 void TestQgsMapToolCapture::cleanupTestCase()
51 {
52   QgsApplication::exitQgis();
53 }
54 
init()55 void TestQgsMapToolCapture::init()
56 {
57 }
58 
cleanup()59 void TestQgsMapToolCapture::cleanup()
60 {
61 }
62 
addVertexNonVectorLayer()63 void TestQgsMapToolCapture::addVertexNonVectorLayer()
64 {
65   QgsProject::instance()->clear();
66   QgsMapCanvas canvas;
67   canvas.setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
68   canvas.setFrameStyle( QFrame::NoFrame );
69   canvas.resize( 600, 600 );
70   canvas.setExtent( QgsRectangle( 0, 0, 10, 10 ) );
71   canvas.show(); // to make the canvas resize
72 
73   QgsAnnotationLayer *layer = new QgsAnnotationLayer( QStringLiteral( "test" ), QgsAnnotationLayer::LayerOptions( QgsProject::instance()->transformContext() ) );
74   QVERIFY( layer->isValid() );
75   QgsProject::instance()->addMapLayers( { layer } );
76 
77   canvas.setLayers( { layer } );
78   canvas.setCurrentLayer( layer );
79 
80   QgsAdvancedDigitizingDockWidget cadDock( &canvas );
81   QgsMapToolCapture tool( &canvas, &cadDock, QgsMapToolCapture::CaptureLine );
82   canvas.setMapTool( &tool );
83 
84   // even though we don't have a vector layer selected, adding vertices should still be allowed
85   QCOMPARE( tool.addVertex( QgsPoint( 5, 5 ), QgsPointLocator::Match() ), 0 );
86 
87   QCOMPARE( tool.captureCurve()->asWkt(), QStringLiteral( "CompoundCurve ((5 5))" ) );
88 
89   // the nextPoint method must also accept non vector layers
90   QgsPoint layerPoint;
91   QCOMPARE( tool.nextPoint( QgsPoint( 5, 6 ), layerPoint ), 0 );
92   QCOMPARE( layerPoint.x(), 5.0 );
93   QCOMPARE( layerPoint.y(), 6.0 );
94 
95 }
96 
addVertexNonVectorLayerTransform()97 void TestQgsMapToolCapture::addVertexNonVectorLayerTransform()
98 {
99   QgsProject::instance()->clear();
100   QgsMapCanvas canvas;
101   canvas.setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
102   canvas.setFrameStyle( QFrame::NoFrame );
103   canvas.resize( 600, 600 );
104   canvas.setExtent( QgsRectangle( 0, 0, 10, 10 ) );
105   canvas.show(); // to make the canvas resize
106 
107   QgsAnnotationLayer *layer = new QgsAnnotationLayer( QStringLiteral( "test" ), QgsAnnotationLayer::LayerOptions( QgsProject::instance()->transformContext() ) );
108   layer->setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ) );
109   QVERIFY( layer->isValid() );
110   QgsProject::instance()->addMapLayers( { layer } );
111 
112   canvas.setLayers( { layer } );
113   canvas.setCurrentLayer( layer );
114 
115   QgsAdvancedDigitizingDockWidget cadDock( &canvas );
116   QgsMapToolCapture tool( &canvas, &cadDock, QgsMapToolCapture::CaptureLine );
117   canvas.setMapTool( &tool );
118 
119   // even though we don't have a vector layer selected, adding vertices should still be allowed
120   QCOMPARE( tool.addVertex( QgsPoint( 5, 5 ), QgsPointLocator::Match() ), 0 );
121 
122   QCOMPARE( tool.captureCurve()->asWkt( 0 ), QStringLiteral( "CompoundCurve ((556597 557305))" ) );
123 
124   // the nextPoint method must also accept non vector layers
125   QgsPoint layerPoint;
126   QCOMPARE( tool.nextPoint( QgsPoint( 5, 6 ), layerPoint ), 0 );
127   QGSCOMPARENEAR( layerPoint.x(), 556597, 10 );
128   QGSCOMPARENEAR( layerPoint.y(), 669141, 10 );
129 }
130 
131 QGSTEST_MAIN( TestQgsMapToolCapture )
132 #include "testqgsmaptoolcapture.moc"
133