33from qgis .PyQt .QtTest import QTest
44from qgis .PyQt .QtCore import Qt , QEvent , QPoint
55from qgis .PyQt .QtWidgets import QPushButton
6- from qgis .gui import QgsMapCanvas , QgsMapMouseEvent
6+ from qgis .gui import QgsMapCanvas , QgsMapMouseEvent , QgsRubberBand
77from qgis .core import (
88 QgsCoordinateReferenceSystem ,
99 QgsRectangle ,
1818
1919@pytest .mark .filterwarnings ("ignore:.*imp module is deprecated.*" )
2020class TestGui (unittest .TestCase ):
21- def test_ORStoolsDialog (self ):
21+ def test_without_live_preview (self ):
2222 from ORStools .gui .ORStoolsDialog import ORStoolsDialog
2323 from ORStools .utils import maptools
2424
@@ -35,32 +35,193 @@ def test_ORStoolsDialog(self):
3535 self .assertTrue (dlg .isVisible ())
3636
3737 map_button : QPushButton = dlg .routing_fromline_map
38- # click 'routing_fromline_map'
38+
39+ # click green add vertices button
3940 QTest .mouseClick (map_button , Qt .LeftButton )
4041 self .assertFalse (dlg .isVisible ())
4142 self .assertIsInstance (CANVAS .mapTool (), maptools .LineTool )
4243
43- map_dclick = QgsMapMouseEvent (
44+ # click on canvas at [0, 0]
45+ dlg .line_tool .canvasReleaseEvent (self .map_release (0 , 0 , Qt .LeftButton ))
46+
47+ dlg .line_tool .canvasReleaseEvent (self .map_release (5 , 5 , Qt .LeftButton ))
48+
49+ self .assertEqual (dlg .routing_fromline_list .count (), 2 )
50+
51+ len_rubber_band = len (dlg .rubber_band .asGeometry ().asPolyline ())
52+ print (dlg .rubber_band .asGeometry ().asPolyline ())
53+ self .assertEqual (len_rubber_band , 2 )
54+
55+ # doubleclick on canvas at [5, 5]
56+ dlg .line_tool .canvasDoubleClickEvent (self .map_dclick (5 , 5 , Qt .LeftButton ))
57+ self .assertTrue (dlg .isVisible ())
58+
59+ # Check first item of list widget
60+ self .assertEqual (dlg .routing_fromline_list .item (0 ).text (), "Point 0: -0.187575, 56.516620" )
61+
62+ # Check rubber band has only 2 vertices
63+ self .assertEqual (dlg .routing_fromline_list .count (), 2 )
64+ self .assertEqual (type (dlg .rubber_band ), QgsRubberBand )
65+ len_rubber_band = len (dlg .rubber_band .asGeometry ().asPolyline ())
66+ self .assertEqual (len_rubber_band , 2 )
67+
68+ def test_with_live_preview (self ):
69+ """
70+ Tests basic adding and removing of points to the QListWidget and associated rubber bands.
71+ """
72+ from ORStools .gui .ORStoolsDialog import ORStoolsDialog
73+ from ORStools .utils import maptools
74+
75+ CRS = QgsCoordinateReferenceSystem .fromEpsgId (3857 )
76+ CANVAS .setExtent (QgsRectangle (- 13732628.1 , 6181790.0 , - 13728426.7 , 6179205.3 ))
77+ CANVAS .setDestinationCrs (CRS )
78+ CANVAS .setFrameStyle (0 )
79+ CANVAS .resize (600 , 400 )
80+ self .assertEqual (CANVAS .width (), 600 )
81+ self .assertEqual (CANVAS .height (), 400 )
82+
83+ dlg = ORStoolsDialog (IFACE )
84+ dlg .open ()
85+ self .assertTrue (dlg .isVisible ())
86+
87+ # Toggle live preview
88+ dlg .toggle_preview .toggle ()
89+ self .assertTrue (dlg .toggle_preview .isChecked ())
90+
91+ # click 'routing_fromline_map'
92+ QTest .mouseClick (dlg .routing_fromline_map , Qt .LeftButton )
93+ self .assertFalse (dlg .isVisible ())
94+ self .assertIsInstance (CANVAS .mapTool (), maptools .LineTool )
95+
96+ # click on canvas at [0, 0]
97+ dlg .line_tool .canvasReleaseEvent (self .map_release (0 , 0 , Qt .LeftButton ))
98+ # click on canvas at [5, 5]
99+ dlg .line_tool .canvasReleaseEvent (self .map_release (5 , 5 , Qt .LeftButton ))
100+
101+ self .assertEqual (
102+ dlg .routing_fromline_list .item (0 ).text (), "Point 0: -123.384059, 48.448463"
103+ )
104+
105+ # Check that the live preview rubber band has more than two vertices
106+ self .assertEqual (type (dlg .rubber_band ), QgsRubberBand )
107+ len_rubber_band = len (dlg .rubber_band .asGeometry ().asPolyline ())
108+ self .assertTrue (len_rubber_band > 2 )
109+
110+ # Right click and thus show dlg
111+ dlg .line_tool .canvasReleaseEvent (self .map_release (0 , 5 , Qt .RightButton ))
112+ self .assertTrue (dlg .isVisible ())
113+ # Test that right click doesn't create a point
114+ self .assertEqual (dlg .routing_fromline_list .count (), 2 )
115+
116+ # click on canvas at [10, 10]
117+ # Check that the click with an open dlg doesn't create an entry
118+ dlg .line_tool .canvasReleaseEvent (self .map_release (10 , 10 , Qt .LeftButton ))
119+ self .assertEqual (dlg .routing_fromline_list .count (), 2 )
120+
121+ # Disable live preview
122+ dlg .toggle_preview .toggle ()
123+ self .assertFalse (dlg .toggle_preview .isChecked ())
124+
125+ # Check rubber band has only 2 vertices
126+ self .assertEqual (dlg .routing_fromline_list .count (), 2 )
127+ self .assertEqual (type (dlg .rubber_band ), QgsRubberBand )
128+ len_rubber_band = len (dlg .rubber_band .asGeometry ().asPolyline ())
129+ self .assertEqual (len_rubber_band , 2 )
130+
131+ # Click Add Vertices again
132+ QTest .mouseClick (dlg .routing_fromline_map , Qt .LeftButton )
133+ self .assertFalse (dlg .isVisible ())
134+
135+ # continue digitization
136+ # click on canvas at [10, 5]
137+ dlg .line_tool .canvasReleaseEvent (self .map_release (10 , 5 , Qt .LeftButton ))
138+ self .assertEqual (dlg .routing_fromline_list .count (), 3 )
139+
140+ # Double click and thus show dlg
141+ dlg .line_tool .canvasDoubleClickEvent (self .map_dclick (0 , 5 , Qt .LeftButton ))
142+ self .assertTrue (dlg .isVisible ())
143+
144+ # clear list widget and check that it's empty
145+ QTest .mouseClick (dlg .routing_fromline_clear , Qt .LeftButton )
146+ self .assertEqual (dlg .routing_fromline_list .count (), 0 )
147+ # Check that the rubber band is empty
148+ self .assertEqual (type (dlg .rubber_band ), QgsRubberBand )
149+ self .assertTrue (dlg .rubber_band .asGeometry ().isNull ())
150+
151+ def test_drag_drop_with_live_preview (self ):
152+ from ORStools .gui .ORStoolsDialog import ORStoolsDialog
153+ from ORStools .utils import maptools
154+
155+ CRS = QgsCoordinateReferenceSystem .fromEpsgId (3857 )
156+ CANVAS .setExtent (QgsRectangle (- 13732628.1 , 6181790.0 , - 13728426.7 , 6179205.3 ))
157+ CANVAS .setDestinationCrs (CRS )
158+ CANVAS .setFrameStyle (0 )
159+ CANVAS .resize (600 , 400 )
160+ self .assertEqual (CANVAS .width (), 600 )
161+ self .assertEqual (CANVAS .height (), 400 )
162+
163+ dlg = ORStoolsDialog (IFACE )
164+ dlg .open ()
165+ self .assertTrue (dlg .isVisible ())
166+
167+ # click 'routing_fromline_map'
168+ QTest .mouseClick (dlg .routing_fromline_map , Qt .LeftButton )
169+ self .assertFalse (dlg .isVisible ())
170+ self .assertIsInstance (CANVAS .mapTool (), maptools .LineTool )
171+
172+ # Add some points to the list
173+ dlg .line_tool .canvasReleaseEvent (self .map_release (100 , 5 , Qt .LeftButton ))
174+ dlg .line_tool .canvasReleaseEvent (self .map_release (10 , 50 , Qt .LeftButton ))
175+ dlg .line_tool .canvasReleaseEvent (self .map_release (100 , 50 , Qt .LeftButton ))
176+
177+ # Add point to be dragged
178+ dlg .line_tool .canvasReleaseEvent (self .map_release (10 , 5 , Qt .LeftButton ))
179+ self .assertEqual (dlg .routing_fromline_list .count (), 4 )
180+ self .assertEqual (
181+ dlg .routing_fromline_list .item (3 ).text (), "Point 3: -123.375767, 48.445713"
182+ )
183+
184+ # Press at previous position
185+ dlg .line_tool .canvasPressEvent (self .map_press (11 , 5 , Qt .LeftButton ))
186+
187+ # Release somewhere else
188+ dlg .line_tool .canvasReleaseEvent (self .map_release (50 , 10 , Qt .LeftButton ))
189+ self .assertEqual (dlg .routing_fromline_list .count (), 4 )
190+ # Check that the coordinates of the point at the same position in the list has changed
191+ self .assertEqual (
192+ dlg .routing_fromline_list .item (3 ).text (), "Point 3: -123.342597, 48.442962"
193+ )
194+
195+ # Check that the rubber band is not empty
196+ self .assertEqual (type (dlg .rubber_band ), QgsRubberBand )
197+ self .assertFalse (dlg .rubber_band .asGeometry ().isNull ())
198+
199+ def map_release (self , x , y , side ):
200+ return QgsMapMouseEvent (
44201 CANVAS ,
45- QEvent .MouseButtonDblClick ,
46- QPoint (5 , 5 ), # Relative to the canvas' dimensions
47- Qt . LeftButton ,
48- Qt . LeftButton ,
202+ QEvent .MouseButtonRelease ,
203+ QPoint (x , y ), # Relative to the canvas' dimensions
204+ side ,
205+ side ,
49206 Qt .NoModifier ,
50207 )
51208
52- map_click = QgsMapMouseEvent (
209+ def map_press (self , x , y , side ):
210+ return QgsMapMouseEvent (
53211 CANVAS ,
54- QEvent .MouseButtonRelease ,
55- QPoint (0 , 0 ), # Relative to the canvas' dimensions
56- Qt . LeftButton ,
57- Qt . LeftButton ,
212+ QEvent .MouseButtonPress ,
213+ QPoint (x , y ), # Relative to the canvas' dimensions
214+ side ,
215+ side ,
58216 Qt .NoModifier ,
59217 )
60- # click on canvas at [0, 0]
61- dlg .line_tool .canvasReleaseEvent (map_click )
62- # doubleclick on canvas at [5, 5]
63- dlg .line_tool .canvasDoubleClickEvent (map_dclick )
64218
65- self .assertTrue (dlg .isVisible ())
66- self .assertEqual (dlg .routing_fromline_list .item (0 ).text (), "Point 0: -0.187575, 56.516620" )
219+ def map_dclick (self , x , y , side ):
220+ return QgsMapMouseEvent (
221+ CANVAS ,
222+ QEvent .MouseButtonDblClick ,
223+ QPoint (x , y ), # Relative to the canvas' dimensions
224+ side ,
225+ side ,
226+ Qt .NoModifier ,
227+ )
0 commit comments