From 64c37f29d91b009bc9ecbe2cefb6842c068be1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Carrillo?= Date: Tue, 21 May 2024 10:50:03 -0500 Subject: [PATCH] Use global property to store QtWebKit availability --- swiss_locator/gui/config_dialog.py | 3 ++- swiss_locator/gui/maptip.py | 19 ++++++++----------- swiss_locator/gui/qtwebkit_conf.py | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 swiss_locator/gui/qtwebkit_conf.py diff --git a/swiss_locator/gui/config_dialog.py b/swiss_locator/gui/config_dialog.py index 6e26eed..a1d7a20 100644 --- a/swiss_locator/gui/config_dialog.py +++ b/swiss_locator/gui/config_dialog.py @@ -31,6 +31,7 @@ from ..core.settings import Settings from ..core.language import get_language from ..map_geo_admin.layers import searchable_layers +from .qtwebkit_conf import with_qt_web_kit DialogUi, _ = loadUiType(os.path.join(os.path.dirname(__file__), "../ui/config.ui")) @@ -98,7 +99,7 @@ def __init__(self, parent=None): self.settings = settings self.init_widgets() - if not settings.value("with_qtwebkit"): + if not with_qt_web_kit(): map_tip = self.setting_widget("show_map_tip") map_tip.widget.setEnabled(False) map_tip.widget.setToolTip(self.tr("You need to install QtWebKit to use map tips.")) diff --git a/swiss_locator/gui/maptip.py b/swiss_locator/gui/maptip.py index 57ed4e6..876762e 100644 --- a/swiss_locator/gui/maptip.py +++ b/swiss_locator/gui/maptip.py @@ -22,23 +22,20 @@ """ from PyQt5.QtCore import Qt, QPoint, pyqtSignal -try: - from PyQt5.QtWebKit import QWebSettings - from PyQt5.QtWebKitWidgets import QWebView, QWebPage -except ModuleNotFoundError: - from swiss_locator.core.settings import Settings - settings = Settings() - settings.set_value("show_map_tip", False) - settings.set_value("with_qtwebkit", False) -else: - from swiss_locator.core.settings import Settings - Settings().set_value("with_qtwebkit", True) from PyQt5.QtWidgets import QSizePolicy, QDockWidget from PyQt5.QtGui import QPalette, QDesktopServices, QCloseEvent from qgis.core import Qgis, QgsPointXY, QgsMessageLog from qgis.gui import QgisInterface from swiss_locator import DEBUG +from swiss_locator.gui.qtwebkit_conf import with_qt_web_kit +if with_qt_web_kit(): + from PyQt5.QtWebKit import QWebSettings + from PyQt5.QtWebKitWidgets import QWebView, QWebPage +else: + from swiss_locator.core.settings import Settings + settings = Settings() + settings.set_value("show_map_tip", False) class MapTip(QDockWidget): diff --git a/swiss_locator/gui/qtwebkit_conf.py b/swiss_locator/gui/qtwebkit_conf.py new file mode 100644 index 0000000..0f58c2b --- /dev/null +++ b/swiss_locator/gui/qtwebkit_conf.py @@ -0,0 +1,16 @@ + +_WITH_QTWEBKIT = None + + +def with_qt_web_kit() -> bool: + global _WITH_QTWEBKIT + if _WITH_QTWEBKIT is None: + try: + from PyQt5.QtWebKit import QWebSettings + from PyQt5.QtWebKitWidgets import QWebView, QWebPage + except ModuleNotFoundError: + _WITH_QTWEBKIT = False + else: + _WITH_QTWEBKIT = True + + return _WITH_QTWEBKIT