1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
import os
from qtpy.QtCore import QEventLoop, Qt, QUrl
from qtpy.QtWebChannel import QWebChannel
from qtpy.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
class MapWidget(QWebEngineView):
"""
The MapWidget class is a QWebEngineView that houses the leaflet map.
Since it is a QWidget, it can be added to any QLayout.
"""
@property
def page(self):
return self._page
@property
def channel(self):
return self._channel
def __init__(self, use_file_absolute_path: bool = True, alternative_base_path: str=""):
super().__init__()
if use_file_absolute_path or len(alternative_base_path) == 0:
self.base_path = os.path.dirname(os.path.abspath(__file__))
else:
self.base_path = alternative_base_path
self._page = QWebEnginePage()
self.setPage(self._page)
self._channel = QWebChannel()
self._page.setWebChannel(self._channel)
self._loadPage()
self.setContextMenuPolicy(Qt.NoContextMenu)
def _get_page_path(self):
return os.path.join(self.base_path, 'web', 'map.html')
def _loadPage(self):
html_path = self._get_page_path()
# QEventLoop is used to make the page loading behave syncronously
init_loop = QEventLoop()
self._page.loadFinished.connect(init_loop.quit)
self._page.load(QUrl().fromLocalFile(html_path))
init_loop.exec_()
|