Skip to content

Commit 9842fcb

Browse files
committed
Add convience method to block while canvas is rendering
NOT to be called from anything but unit tests and standalone scripts!!
1 parent af532ec commit 9842fcb

File tree

4 files changed

+35
-59
lines changed

4 files changed

+35
-59
lines changed

python/gui/qgsmapcanvas.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class QgsMapCanvas : QGraphicsView
3030
bool isCachingEnabled() const;
3131
void clearCache();
3232
void refreshAllLayers();
33+
void waitWhileRendering();
3334
void setParallelRenderingEnabled( bool enabled );
3435
bool isParallelRenderingEnabled() const;
3536
void setMapUpdateInterval( int timeMilliseconds );

src/gui/qgsmapcanvas.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,6 +2016,14 @@ void QgsMapCanvas::refreshAllLayers()
20162016
refresh();
20172017
}
20182018

2019+
void QgsMapCanvas::waitWhileRendering()
2020+
{
2021+
while ( mRefreshScheduled || mJob )
2022+
{
2023+
QgsApplication::processEvents();
2024+
}
2025+
}
2026+
20192027
void QgsMapCanvas::setSegmentationTolerance( double tolerance )
20202028
{
20212029
mSettings.setSegmentationTolerance( tolerance );

src/gui/qgsmapcanvas.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
128128
//! @note added in 2.9
129129
void refreshAllLayers();
130130

131+
/**
132+
* Blocks until the rendering job has finished.
133+
*
134+
* In almost all cases you do NOT want to call this, as it will hang the UI
135+
* until the rendering job is complete. It's included in API solely for
136+
* unit testing and standalone python scripts.
137+
*
138+
* @note added in QGIS 3.0
139+
*/
140+
void waitWhileRendering();
141+
131142
//! Set whether the layers are rendered in parallel or sequentially
132143
//! @note added in 2.4
133144
void setParallelRenderingEnabled( bool enabled );

tests/src/python/test_qgsmapcanvas.py

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,10 @@ def testDeferredUpdate(self):
5858
canvas.setLayers([layer])
5959
canvas.setExtent(QgsRectangle(10, 30, 20, 35))
6060
canvas.show()
61-
6261
# need to wait until first redraw can occur (note that we first need to wait till drawing starts!)
6362
while not canvas.isDrawing():
6463
app.processEvents()
65-
while canvas.isDrawing():
66-
app.processEvents()
67-
64+
canvas.waitWhileRendering()
6865
self.assertTrue(self.canvasImageCheck('empty_canvas', 'empty_canvas', canvas))
6966

7067
# add polygon to layer
@@ -83,10 +80,7 @@ def testDeferredUpdate(self):
8380

8481
# refresh canvas
8582
canvas.refresh()
86-
while not canvas.isDrawing():
87-
app.processEvents()
88-
while canvas.isDrawing():
89-
app.processEvents()
83+
canvas.waitWhileRendering()
9084

9185
# now we expect the canvas check to fail (since they'll be a new polygon rendered over it)
9286
self.assertFalse(self.canvasImageCheck('empty_canvas', 'empty_canvas', canvas))
@@ -110,9 +104,7 @@ def testRefreshOnTimer(self):
110104
# need to wait until first redraw can occur (note that we first need to wait till drawing starts!)
111105
while not canvas.isDrawing():
112106
app.processEvents()
113-
while canvas.isDrawing():
114-
app.processEvents()
115-
107+
canvas.waitWhileRendering()
116108
self.assertTrue(self.canvasImageCheck('empty_canvas', 'empty_canvas', canvas))
117109

118110
# add polygon to layer
@@ -210,9 +202,7 @@ def testMapTheme(self):
210202
# need to wait until first redraw can occur (note that we first need to wait till drawing starts!)
211203
while not canvas.isDrawing():
212204
app.processEvents()
213-
while canvas.isDrawing():
214-
app.processEvents()
215-
205+
canvas.waitWhileRendering()
216206
self.assertTrue(self.canvasImageCheck('theme1', 'theme1', canvas))
217207

218208
# add some styles
@@ -223,18 +213,12 @@ def testMapTheme(self):
223213
layer.styleManager().addStyleFromLayer('style2')
224214

225215
canvas.refresh()
226-
while not canvas.isDrawing():
227-
app.processEvents()
228-
while canvas.isDrawing():
229-
app.processEvents()
216+
canvas.waitWhileRendering()
230217
self.assertTrue(self.canvasImageCheck('theme2', 'theme2', canvas))
231218

232219
layer.styleManager().setCurrentStyle('style1')
233220
canvas.refresh()
234-
while not canvas.isDrawing():
235-
app.processEvents()
236-
while canvas.isDrawing():
237-
app.processEvents()
221+
canvas.waitWhileRendering()
238222
self.assertTrue(self.canvasImageCheck('theme1', 'theme1', canvas))
239223

240224
# ok, so all good with setting/rendering map styles
@@ -258,18 +242,12 @@ def testMapTheme(self):
258242

259243
canvas.setTheme('theme2')
260244
canvas.refresh()
261-
while not canvas.isDrawing():
262-
app.processEvents()
263-
while canvas.isDrawing():
264-
app.processEvents()
245+
canvas.waitWhileRendering()
265246
self.assertTrue(self.canvasImageCheck('theme2', 'theme2', canvas))
266247

267248
canvas.setTheme('theme1')
268249
canvas.refresh()
269-
while not canvas.isDrawing():
270-
app.processEvents()
271-
while canvas.isDrawing():
272-
app.processEvents()
250+
canvas.waitWhileRendering()
273251
self.assertTrue(self.canvasImageCheck('theme1', 'theme1', canvas))
274252

275253
# add another layer
@@ -286,17 +264,11 @@ def testMapTheme(self):
286264

287265
# rerender canvas - should NOT show new layer
288266
canvas.refresh()
289-
while not canvas.isDrawing():
290-
app.processEvents()
291-
while canvas.isDrawing():
292-
app.processEvents()
267+
canvas.waitWhileRendering()
293268
self.assertTrue(self.canvasImageCheck('theme1', 'theme1', canvas))
294269
# test again - this time refresh all layers
295270
canvas.refreshAllLayers()
296-
while not canvas.isDrawing():
297-
app.processEvents()
298-
while canvas.isDrawing():
299-
app.processEvents()
271+
canvas.waitWhileRendering()
300272
self.assertTrue(self.canvasImageCheck('theme1', 'theme1', canvas))
301273

302274
# add layer 2 to theme1
@@ -305,10 +277,7 @@ def testMapTheme(self):
305277
QgsProject.instance().mapThemeCollection().update('theme1', theme1)
306278

307279
canvas.refresh()
308-
while not canvas.isDrawing():
309-
app.processEvents()
310-
while canvas.isDrawing():
311-
app.processEvents()
280+
canvas.waitWhileRendering()
312281
self.assertTrue(self.canvasImageCheck('theme3', 'theme3', canvas))
313282

314283
# change the appearance of an active style
@@ -320,42 +289,29 @@ def testMapTheme(self):
320289
QgsProject.instance().mapThemeCollection().update('theme1', theme1)
321290

322291
canvas.refresh()
323-
while not canvas.isDrawing():
324-
app.processEvents()
325-
while canvas.isDrawing():
326-
app.processEvents()
292+
canvas.waitWhileRendering()
327293
self.assertTrue(self.canvasImageCheck('theme3', 'theme3', canvas))
328294

329295
layer2.styleManager().setCurrentStyle('style4')
330296
sym3 = QgsFillSymbol.createSimple({'color': '#b200b2'})
331297
layer2.renderer().setSymbol(sym3)
332298
canvas.refresh()
333-
334-
while not canvas.isDrawing():
335-
app.processEvents()
336-
while canvas.isDrawing():
337-
app.processEvents()
299+
canvas.waitWhileRendering()
338300
self.assertTrue(self.canvasImageCheck('theme4', 'theme4', canvas))
339301

340302
# try setting layers while a theme is in place
341303
canvas.setLayers([layer])
342304
canvas.refresh()
343305

344306
# should be no change... setLayers should be ignored if canvas is following a theme!
345-
while not canvas.isDrawing():
346-
app.processEvents()
347-
while canvas.isDrawing():
348-
app.processEvents()
307+
canvas.waitWhileRendering()
349308
self.assertTrue(self.canvasImageCheck('theme4', 'theme4', canvas))
350309

351310
# setLayerStyleOverrides while theme is in place
352311
canvas.setLayerStyleOverrides({layer2.id(): 'original'})
353312
# should be no change... setLayerStyleOverrides should be ignored if canvas is following a theme!
354313
canvas.refresh()
355-
while not canvas.isDrawing():
356-
app.processEvents()
357-
while canvas.isDrawing():
358-
app.processEvents()
314+
canvas.waitWhileRendering()
359315
self.assertTrue(self.canvasImageCheck('theme4', 'theme4', canvas))
360316

361317
def canvasImageCheck(self, name, reference_image, canvas):

0 commit comments

Comments
 (0)