1 /***************************************************************************
2      testqgsmaptoolutils.h
3      ---------------------
4     Date                 : January 2018
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
9  ***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include "qgstest.h"
19 
20 #include "qgisapp.h"
21 #include "qgsgeometry.h"
22 #include "qgsmapcanvas.h"
23 #include "qgsmapmouseevent.h"
24 #include "qgssnappingutils.h"
25 #include "qgsmaptooladvanceddigitizing.h"
26 
27 /**
28  * \ingroup UnitTests
29  */
30 class TestQgsMapToolAdvancedDigitizingUtils
31 {
32   public:
TestQgsMapToolAdvancedDigitizingUtils(QgsMapToolAdvancedDigitizing * mapTool)33     TestQgsMapToolAdvancedDigitizingUtils( QgsMapToolAdvancedDigitizing *mapTool )
34       : mMapTool( mapTool )
35     {
36     }
37 
existingFeatureIds()38     QSet<QgsFeatureId> existingFeatureIds()
39     {
40       QSet<QgsFeatureId> fids;
41       QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mMapTool->canvas()->currentLayer() );
42 
43       if ( vl )
44       {
45         QgsFeature f;
46         QgsFeatureIterator it = vl->getFeatures();
47         while ( it.nextFeature( f ) )
48           fids << f.id();
49       }
50 
51       return fids;
52     }
53 
54     QgsFeatureId newFeatureId( QSet<QgsFeatureId> oldFids = QSet<QgsFeatureId>() )
55     {
56       QSet<QgsFeatureId> newFids = existingFeatureIds();
57       QSet<QgsFeatureId> diffFids = newFids.subtract( oldFids );
58       Q_ASSERT( diffFids.count() == 1 );
59       return *diffFids.constBegin();
60     }
61 
mapToScreen(double mapX,double mapY)62     QPoint mapToScreen( double mapX, double mapY )
63     {
64       QgsPointXY pt = mMapTool->canvas()->mapSettings().mapToPixel().transform( mapX, mapY );
65       return QPoint( std::round( pt.x() ), std::round( pt.y() ) );
66     }
67 
mouseMove(double mapX,double mapY)68     void mouseMove( double mapX, double mapY )
69     {
70       QgsMapMouseEvent e( mMapTool->canvas(), QEvent::MouseMove, mapToScreen( mapX, mapY ) );
71       mMapTool->cadCanvasMoveEvent( &e );
72     }
73 
74     void mousePress( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), bool snap = false )
75     {
76       QgsMapMouseEvent e1( mMapTool->canvas(), QEvent::MouseButtonPress, mapToScreen( mapX, mapY ), button, button, stateKey );
77 
78       if ( snap )
79         e1.snapPoint();
80 
81       mMapTool->cadCanvasPressEvent( &e1 );
82     }
83 
84     void mouseRelease( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), bool snap = false )
85     {
86       QgsMapMouseEvent e2( mMapTool->canvas(), QEvent::MouseButtonRelease, mapToScreen( mapX, mapY ), button, Qt::MouseButton(), stateKey );
87 
88       if ( snap )
89         e2.snapPoint();
90 
91       mMapTool->cadCanvasReleaseEvent( &e2 );
92     }
93 
94     void mouseClick( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), bool snap = false )
95     {
96       mousePress( mapX, mapY, button, stateKey, snap );
97       mouseRelease( mapX, mapY, button, stateKey, snap );
98     }
99 
100     void keyClick( int key, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() )
101     {
102       QKeyEvent e1( QEvent::KeyPress, key, stateKey );
103       mMapTool->keyPressEvent( &e1 );
104 
105       QKeyEvent e2( QEvent::KeyRelease, key, stateKey );
106       mMapTool->keyReleaseEvent( &e2 );
107     }
108 
109   private:
110     QgsMapToolAdvancedDigitizing *mMapTool = nullptr;
111 };
112 
113 /**
114  * \ingroup UnitTests
115  */
116 class TestQgsMapToolUtils
117 {
118   public:
TestQgsMapToolUtils(QgsMapTool * mapTool)119     TestQgsMapToolUtils( QgsMapTool *mapTool )
120       : mMapTool( mapTool )
121     {
122     }
123 
existingFeatureIds()124     QSet<QgsFeatureId> existingFeatureIds()
125     {
126       QSet<QgsFeatureId> fids;
127       QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mMapTool->canvas()->currentLayer() );
128 
129       if ( vl )
130       {
131         QgsFeature f;
132         QgsFeatureIterator it = vl->getFeatures();
133         while ( it.nextFeature( f ) )
134           fids << f.id();
135       }
136 
137       return fids;
138     }
139 
140     QgsFeatureId newFeatureId( QSet<QgsFeatureId> oldFids = QSet<QgsFeatureId>() )
141     {
142       QSet<QgsFeatureId> newFids = existingFeatureIds();
143       QSet<QgsFeatureId> diffFids = newFids.subtract( oldFids );
144       Q_ASSERT( diffFids.count() == 1 );
145       return *diffFids.constBegin();
146     }
147 
mapToScreen(double mapX,double mapY)148     QPoint mapToScreen( double mapX, double mapY )
149     {
150       QgsPointXY pt = mMapTool->canvas()->mapSettings().mapToPixel().transform( mapX, mapY );
151       return QPoint( std::round( pt.x() ), std::round( pt.y() ) );
152     }
153 
mouseMove(double mapX,double mapY)154     void mouseMove( double mapX, double mapY )
155     {
156       QgsMapMouseEvent e( mMapTool->canvas(), QEvent::MouseMove, mapToScreen( mapX, mapY ) );
157       mMapTool->canvasMoveEvent( &e );
158     }
159 
160     void mousePress( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), bool snap = false )
161     {
162       QgsMapMouseEvent e1( mMapTool->canvas(), QEvent::MouseButtonPress, mapToScreen( mapX, mapY ), button, button, stateKey );
163 
164       if ( snap )
165         e1.snapPoint();
166 
167       mMapTool->canvasPressEvent( &e1 );
168     }
169 
170     void mouseRelease( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), bool snap = false )
171     {
172       QgsMapMouseEvent e2( mMapTool->canvas(), QEvent::MouseButtonRelease, mapToScreen( mapX, mapY ), button, Qt::MouseButton(), stateKey );
173 
174       if ( snap )
175         e2.snapPoint();
176 
177       mMapTool->canvasReleaseEvent( &e2 );
178     }
179 
180     void mouseClick( double mapX, double mapY, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), bool snap = false )
181     {
182       mousePress( mapX, mapY, button, stateKey, snap );
183       mouseRelease( mapX, mapY, button, stateKey, snap );
184     }
185 
186     void keyClick( int key, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers() )
187     {
188       QKeyEvent e1( QEvent::KeyPress, key, stateKey );
189       mMapTool->keyPressEvent( &e1 );
190 
191       QKeyEvent e2( QEvent::KeyRelease, key, stateKey );
192       mMapTool->keyReleaseEvent( &e2 );
193     }
194 
195   private:
196     QgsMapTool *mMapTool = nullptr;
197 };
198 
199