forked from AG_QGIS/Plugin_SN_Basis
394 lines
10 KiB
Python
394 lines
10 KiB
Python
|
|
"""
|
|||
|
|
sn_basis/functions/qt_wrapper.py – zentrale Qt-Abstraktion (PyQt5 / PyQt6 / Mock)
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
from typing import Optional, Type, Any
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
# Qt-Symbole (werden dynamisch gesetzt)
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
|
|||
|
|
QDockWidget: Type[Any]
|
|||
|
|
QMessageBox: Type[Any]
|
|||
|
|
QFileDialog: Type[Any]
|
|||
|
|
QEventLoop: Type[Any]
|
|||
|
|
QUrl: Type[Any]
|
|||
|
|
QNetworkRequest: Type[Any]
|
|||
|
|
QNetworkReply: Type[Any]
|
|||
|
|
QCoreApplication: Type[Any]
|
|||
|
|
|
|||
|
|
QWidget: Type[Any]
|
|||
|
|
QGridLayout: Type[Any]
|
|||
|
|
QLabel: Type[Any]
|
|||
|
|
QLineEdit: Type[Any]
|
|||
|
|
QGroupBox: Type[Any]
|
|||
|
|
QVBoxLayout: Type[Any]
|
|||
|
|
QPushButton: Type[Any]
|
|||
|
|
QAction: Type[Any]
|
|||
|
|
QMenu: Type[Any]
|
|||
|
|
QToolBar: Type[Any]
|
|||
|
|
QActionGroup: Type[Any]
|
|||
|
|
QTabWidget: type
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
YES: Optional[Any] = None
|
|||
|
|
NO: Optional[Any] = None
|
|||
|
|
CANCEL: Optional[Any] = None
|
|||
|
|
ICON_QUESTION: Optional[Any] = None
|
|||
|
|
|
|||
|
|
QT_VERSION = 0 # 0 = Mock, 5 = PyQt5, 6 = PyQt6
|
|||
|
|
|
|||
|
|
|
|||
|
|
def exec_dialog(dialog: Any) -> Any:
|
|||
|
|
raise NotImplementedError
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
# Versuch: PyQt6
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
from qgis.PyQt.QtWidgets import ( # type: ignore
|
|||
|
|
QMessageBox as _QMessageBox,# type: ignore
|
|||
|
|
QFileDialog as _QFileDialog,# type: ignore
|
|||
|
|
QWidget as _QWidget,# type: ignore
|
|||
|
|
QGridLayout as _QGridLayout,# type: ignore
|
|||
|
|
QLabel as _QLabel,# type: ignore
|
|||
|
|
QLineEdit as _QLineEdit,# type: ignore
|
|||
|
|
QGroupBox as _QGroupBox,# type: ignore
|
|||
|
|
QVBoxLayout as _QVBoxLayout,# type: ignore
|
|||
|
|
QPushButton as _QPushButton,# type: ignore
|
|||
|
|
QAction as _QAction,
|
|||
|
|
QMenu as _QMenu,# type: ignore
|
|||
|
|
QToolBar as _QToolBar,# type: ignore
|
|||
|
|
QActionGroup as _QActionGroup,# type: ignore
|
|||
|
|
QDockWidget as _QDockWidget,# type: ignore
|
|||
|
|
QTabWidget as _QTabWidget,# type: ignore
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
from qgis.PyQt.QtCore import ( # type: ignore
|
|||
|
|
QEventLoop as _QEventLoop,# type: ignore
|
|||
|
|
QUrl as _QUrl,# type: ignore
|
|||
|
|
QCoreApplication as _QCoreApplication,# type: ignore
|
|||
|
|
)
|
|||
|
|
from qgis.PyQt.QtNetwork import ( # type: ignore
|
|||
|
|
QNetworkRequest as _QNetworkRequest,# type: ignore
|
|||
|
|
QNetworkReply as _QNetworkReply,# type: ignore
|
|||
|
|
)
|
|||
|
|
QT_VERSION = 6
|
|||
|
|
QMessageBox = _QMessageBox
|
|||
|
|
QFileDialog = _QFileDialog
|
|||
|
|
QEventLoop = _QEventLoop
|
|||
|
|
QUrl = _QUrl
|
|||
|
|
QNetworkRequest = _QNetworkRequest
|
|||
|
|
QNetworkReply = _QNetworkReply
|
|||
|
|
QCoreApplication = _QCoreApplication
|
|||
|
|
QDockWidget = _QDockWidget
|
|||
|
|
QWidget = _QWidget
|
|||
|
|
QGridLayout = _QGridLayout
|
|||
|
|
QLabel = _QLabel
|
|||
|
|
QLineEdit = _QLineEdit
|
|||
|
|
QGroupBox = _QGroupBox
|
|||
|
|
QVBoxLayout = _QVBoxLayout
|
|||
|
|
QPushButton = _QPushButton
|
|||
|
|
QAction = _QAction
|
|||
|
|
QMenu = _QMenu
|
|||
|
|
QToolBar = _QToolBar
|
|||
|
|
QActionGroup = _QActionGroup
|
|||
|
|
QTabWidget = _QTabWidget
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
YES = QMessageBox.StandardButton.Yes
|
|||
|
|
NO = QMessageBox.StandardButton.No
|
|||
|
|
CANCEL = QMessageBox.StandardButton.Cancel
|
|||
|
|
ICON_QUESTION = QMessageBox.Icon.Question
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
def exec_dialog(dialog: Any) -> Any:
|
|||
|
|
return dialog.exec()
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
# Versuch: PyQt5
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
|
|||
|
|
except Exception:
|
|||
|
|
try:
|
|||
|
|
from PyQt5.QtWidgets import (
|
|||
|
|
QMessageBox as _QMessageBox,
|
|||
|
|
QFileDialog as _QFileDialog,
|
|||
|
|
QWidget as _QWidget,
|
|||
|
|
QGridLayout as _QGridLayout,
|
|||
|
|
QLabel as _QLabel,
|
|||
|
|
QLineEdit as _QLineEdit,
|
|||
|
|
QGroupBox as _QGroupBox,
|
|||
|
|
QVBoxLayout as _QVBoxLayout,
|
|||
|
|
QPushButton as _QPushButton,
|
|||
|
|
QAction as _QAction,
|
|||
|
|
QMenu as _QMenu,
|
|||
|
|
QToolBar as _QToolBar,
|
|||
|
|
QActionGroup as _QActionGroup,
|
|||
|
|
QDockWidget as _QDockWidget,
|
|||
|
|
QTabWidget as _QTabWidget,
|
|||
|
|
|
|||
|
|
)
|
|||
|
|
from PyQt5.QtCore import (
|
|||
|
|
QEventLoop as _QEventLoop,
|
|||
|
|
QUrl as _QUrl,
|
|||
|
|
QCoreApplication as _QCoreApplication,
|
|||
|
|
)
|
|||
|
|
from PyQt5.QtNetwork import (
|
|||
|
|
QNetworkRequest as _QNetworkRequest,
|
|||
|
|
QNetworkReply as _QNetworkReply,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
QMessageBox = _QMessageBox
|
|||
|
|
QFileDialog = _QFileDialog
|
|||
|
|
QEventLoop = _QEventLoop
|
|||
|
|
QUrl = _QUrl
|
|||
|
|
QNetworkRequest = _QNetworkRequest
|
|||
|
|
QNetworkReply = _QNetworkReply
|
|||
|
|
QCoreApplication = _QCoreApplication
|
|||
|
|
QDockWidget = _QDockWidget
|
|||
|
|
|
|||
|
|
|
|||
|
|
QWidget = _QWidget
|
|||
|
|
QGridLayout = _QGridLayout
|
|||
|
|
QLabel = _QLabel
|
|||
|
|
QLineEdit = _QLineEdit
|
|||
|
|
QGroupBox = _QGroupBox
|
|||
|
|
QVBoxLayout = _QVBoxLayout
|
|||
|
|
QPushButton = _QPushButton
|
|||
|
|
QAction = _QAction
|
|||
|
|
QMenu = _QMenu
|
|||
|
|
QToolBar = _QToolBar
|
|||
|
|
QActionGroup = _QActionGroup
|
|||
|
|
QTabWidget = _QTabWidget
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
YES = QMessageBox.Yes
|
|||
|
|
NO = QMessageBox.No
|
|||
|
|
CANCEL = QMessageBox.Cancel
|
|||
|
|
ICON_QUESTION = QMessageBox.Question
|
|||
|
|
|
|||
|
|
QT_VERSION = 5
|
|||
|
|
|
|||
|
|
def exec_dialog(dialog: Any) -> Any:
|
|||
|
|
return dialog.exec_()
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
# Mock-Modus
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
|
|||
|
|
except Exception:
|
|||
|
|
QT_VERSION = 0
|
|||
|
|
|
|||
|
|
class FakeEnum(int):
|
|||
|
|
def __or__(self, other: "FakeEnum") -> "FakeEnum":
|
|||
|
|
return FakeEnum(int(self) | int(other))
|
|||
|
|
|
|||
|
|
YES = FakeEnum(1)
|
|||
|
|
NO = FakeEnum(2)
|
|||
|
|
CANCEL = FakeEnum(4)
|
|||
|
|
ICON_QUESTION = FakeEnum(8)
|
|||
|
|
|
|||
|
|
class _MockQMessageBox:
|
|||
|
|
Yes = YES
|
|||
|
|
No = NO
|
|||
|
|
Cancel = CANCEL
|
|||
|
|
Question = ICON_QUESTION
|
|||
|
|
|
|||
|
|
QMessageBox = _MockQMessageBox
|
|||
|
|
|
|||
|
|
class _MockQFileDialog:
|
|||
|
|
@staticmethod
|
|||
|
|
def getOpenFileName(*args, **kwargs):
|
|||
|
|
return ("", "")
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def getSaveFileName(*args, **kwargs):
|
|||
|
|
return ("", "")
|
|||
|
|
|
|||
|
|
QFileDialog = _MockQFileDialog
|
|||
|
|
|
|||
|
|
class _MockQEventLoop:
|
|||
|
|
def exec(self) -> int:
|
|||
|
|
return 0
|
|||
|
|
|
|||
|
|
def quit(self) -> None:
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
QEventLoop = _MockQEventLoop
|
|||
|
|
|
|||
|
|
class _MockQUrl(str):
|
|||
|
|
def isValid(self) -> bool:
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
QUrl = _MockQUrl
|
|||
|
|
|
|||
|
|
class _MockQNetworkRequest:
|
|||
|
|
def __init__(self, url: Any):
|
|||
|
|
self.url = url
|
|||
|
|
|
|||
|
|
QNetworkRequest = _MockQNetworkRequest
|
|||
|
|
|
|||
|
|
class _MockQNetworkReply:
|
|||
|
|
def error(self) -> int:
|
|||
|
|
return 0
|
|||
|
|
|
|||
|
|
def errorString(self) -> str:
|
|||
|
|
return ""
|
|||
|
|
|
|||
|
|
def readAll(self) -> bytes:
|
|||
|
|
return b""
|
|||
|
|
|
|||
|
|
def deleteLater(self) -> None:
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
QNetworkReply = _MockQNetworkReply
|
|||
|
|
|
|||
|
|
class _MockWidget:
|
|||
|
|
def __init__(self, *args, **kwargs):
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
class _MockLayout:
|
|||
|
|
def addWidget(self, *args, **kwargs):
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
def addLayout(self, *args, **kwargs):
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
def addStretch(self, *args, **kwargs):
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
class _MockLabel:
|
|||
|
|
def __init__(self, text: str = ""):
|
|||
|
|
self._text = text
|
|||
|
|
|
|||
|
|
class _MockLineEdit:
|
|||
|
|
def __init__(self, *args, **kwargs):
|
|||
|
|
self._text = ""
|
|||
|
|
|
|||
|
|
def text(self) -> str:
|
|||
|
|
return self._text
|
|||
|
|
|
|||
|
|
def setText(self, value: str) -> None:
|
|||
|
|
self._text = value
|
|||
|
|
|
|||
|
|
class _MockButton:
|
|||
|
|
def __init__(self, *args, **kwargs):
|
|||
|
|
self.clicked = lambda *a, **k: None
|
|||
|
|
|
|||
|
|
QWidget = _MockWidget
|
|||
|
|
QGridLayout = _MockLayout
|
|||
|
|
QLabel = _MockLabel
|
|||
|
|
QLineEdit = _MockLineEdit
|
|||
|
|
QGroupBox = _MockWidget
|
|||
|
|
QVBoxLayout = _MockLayout
|
|||
|
|
QPushButton = _MockButton
|
|||
|
|
|
|||
|
|
class _MockQCoreApplication:
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
QCoreApplication = _MockQCoreApplication
|
|||
|
|
|
|||
|
|
|
|||
|
|
class _MockQDockWidget(_MockWidget):
|
|||
|
|
def __init__(self, *args, **kwargs):
|
|||
|
|
super().__init__(*args, **kwargs)
|
|||
|
|
self._object_name = ""
|
|||
|
|
|
|||
|
|
def setObjectName(self, name: str) -> None:
|
|||
|
|
self._object_name = name
|
|||
|
|
|
|||
|
|
def objectName(self) -> str:
|
|||
|
|
return self._object_name
|
|||
|
|
|
|||
|
|
def show(self) -> None:
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
def deleteLater(self) -> None:
|
|||
|
|
pass
|
|||
|
|
QDockWidget = _MockQDockWidget
|
|||
|
|
class _MockAction:
|
|||
|
|
def __init__(self, *args, **kwargs):
|
|||
|
|
self._checked = False
|
|||
|
|
self.triggered = lambda *a, **k: None
|
|||
|
|
|
|||
|
|
def setToolTip(self, text: str) -> None:
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
def setCheckable(self, value: bool) -> None:
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
def setChecked(self, value: bool) -> None:
|
|||
|
|
self._checked = value
|
|||
|
|
|
|||
|
|
|
|||
|
|
class _MockMenu:
|
|||
|
|
def __init__(self, *args, **kwargs):
|
|||
|
|
self._actions = []
|
|||
|
|
|
|||
|
|
def addAction(self, action):
|
|||
|
|
self._actions.append(action)
|
|||
|
|
|
|||
|
|
def removeAction(self, action):
|
|||
|
|
if action in self._actions:
|
|||
|
|
self._actions.remove(action)
|
|||
|
|
|
|||
|
|
def clear(self):
|
|||
|
|
self._actions.clear()
|
|||
|
|
|
|||
|
|
def menuAction(self):
|
|||
|
|
return self
|
|||
|
|
|
|||
|
|
|
|||
|
|
class _MockToolBar:
|
|||
|
|
def __init__(self, *args, **kwargs):
|
|||
|
|
self._actions = []
|
|||
|
|
|
|||
|
|
def setObjectName(self, name: str) -> None:
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
def addAction(self, action):
|
|||
|
|
self._actions.append(action)
|
|||
|
|
|
|||
|
|
def removeAction(self, action):
|
|||
|
|
if action in self._actions:
|
|||
|
|
self._actions.remove(action)
|
|||
|
|
|
|||
|
|
def clear(self):
|
|||
|
|
self._actions.clear()
|
|||
|
|
|
|||
|
|
|
|||
|
|
class _MockActionGroup:
|
|||
|
|
def __init__(self, *args, **kwargs):
|
|||
|
|
self._actions = []
|
|||
|
|
|
|||
|
|
def setExclusive(self, value: bool) -> None:
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
def addAction(self, action):
|
|||
|
|
self._actions.append(action)
|
|||
|
|
QAction = _MockAction
|
|||
|
|
QMenu = _MockMenu
|
|||
|
|
QToolBar = _MockToolBar
|
|||
|
|
QActionGroup = _MockActionGroup
|
|||
|
|
|
|||
|
|
|
|||
|
|
def exec_dialog(dialog: Any) -> Any:
|
|||
|
|
return YES
|
|||
|
|
class _MockTabWidget:
|
|||
|
|
def __init__(self, *args, **kwargs):
|
|||
|
|
self._tabs = []
|
|||
|
|
|
|||
|
|
def addTab(self, widget, title: str):
|
|||
|
|
self._tabs.append((widget, title))
|
|||
|
|
QTabWidget = _MockTabWidget
|
|||
|
|
|