class OpenlayersRenderer(QgsMapLayerRenderer): def __init__(self, layer, context, webPage, layerType): """ Initialize the object. This function is still run in the GUI thread. Should refrain from doing any heavy work. """ QgsMapLayerRenderer.__init__(self, layer.id()) self.context = context self.controller = OpenlayersController(None, context, webPage, layerType) self.loop = None def render(self): """ do the rendering. This function is called in the worker thread """ debug("[WORKER THREAD] Calling request() asynchronously", 3) QMetaObject.invokeMethod(self.controller, "request") # setup a timer that checks whether the rendering has not been stopped # in the meanwhile timer = QTimer() timer.setInterval(50) timer.timeout.connect(self.onTimeout) timer.start() debug("[WORKER THREAD] Waiting for the async request to complete", 3) self.loop = QEventLoop() self.controller.finished.connect(self.loop.exit) self.loop.exec_() debug("[WORKER THREAD] Async request finished", 3) painter = self.context.painter() painter.drawImage(0, 0, self.controller.img) return True def onTimeout(self): """ periodically check whether the rendering should not be stopped """ if self.context.renderingStopped(): debug("[WORKER THREAD] Cancelling rendering", 3) self.loop.exit()
def _forcibly_terminate_loop(loop: QtCore.QEventLoop): log("Forcibly ending event loop...") loop.exit(1)