forked from AG_QGIS/Plugin_SN_Basis
521 lines
15 KiB
Python
521 lines
15 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
|
||
QToolButton: Type[Any]
|
||
QSizePolicy: Type[Any]
|
||
Qt: Type[Any]
|
||
|
||
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
|
||
QToolButton as _QToolButton,#type:ignore
|
||
QSizePolicy as _QSizePolicy,#type:ignore
|
||
|
||
)
|
||
|
||
|
||
|
||
from qgis.PyQt.QtCore import ( # type: ignore
|
||
QEventLoop as _QEventLoop,# type: ignore
|
||
QUrl as _QUrl,# type: ignore
|
||
QCoreApplication as _QCoreApplication,# type: ignore
|
||
Qt as _Qt#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
|
||
Qt=_Qt
|
||
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
|
||
QToolButton=_QToolButton
|
||
QSizePolicy=_QSizePolicy
|
||
|
||
YES = QMessageBox.StandardButton.Yes
|
||
NO = QMessageBox.StandardButton.No
|
||
CANCEL = QMessageBox.StandardButton.Cancel
|
||
ICON_QUESTION = QMessageBox.Icon.Question
|
||
# ---------------------------------------------------------
|
||
# Qt6 Enum-Aliase (vereinheitlicht)
|
||
# ---------------------------------------------------------
|
||
|
||
ToolButtonTextBesideIcon = Qt.ToolButtonStyle.ToolButtonTextBesideIcon
|
||
ArrowDown = Qt.ArrowType.DownArrow
|
||
ArrowRight = Qt.ArrowType.RightArrow
|
||
# QSizePolicy Enum-Aliase (Qt6)
|
||
SizePolicyPreferred = QSizePolicy.Policy.Preferred
|
||
SizePolicyMaximum = QSizePolicy.Policy.Maximum
|
||
# ---------------------------------------------------------
|
||
# QDockWidget Feature-Aliase (Qt6)
|
||
# ---------------------------------------------------------
|
||
|
||
DockWidgetMovable = QDockWidget.DockWidgetFeature.DockWidgetMovable
|
||
DockWidgetFloatable = QDockWidget.DockWidgetFeature.DockWidgetFloatable
|
||
DockWidgetClosable = QDockWidget.DockWidgetFeature.DockWidgetClosable
|
||
# ---------------------------------------------------------
|
||
# Dock-Area-Aliase (Qt6)
|
||
# ---------------------------------------------------------
|
||
|
||
DockAreaLeft = Qt.DockWidgetArea.LeftDockWidgetArea
|
||
DockAreaRight = Qt.DockWidgetArea.RightDockWidgetArea
|
||
|
||
|
||
|
||
|
||
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,
|
||
QToolButton as _QToolButton,
|
||
QSizePolicy as _QSizePolicy,
|
||
)
|
||
from PyQt5.QtCore import (
|
||
QEventLoop as _QEventLoop,
|
||
QUrl as _QUrl,
|
||
QCoreApplication as _QCoreApplication,
|
||
Qt as _Qt,
|
||
)
|
||
from PyQt5.QtNetwork import (
|
||
QNetworkRequest as _QNetworkRequest,
|
||
QNetworkReply as _QNetworkReply,
|
||
)
|
||
|
||
QMessageBox = _QMessageBox
|
||
QFileDialog = _QFileDialog
|
||
QEventLoop = _QEventLoop
|
||
QUrl = _QUrl
|
||
QNetworkRequest = _QNetworkRequest
|
||
QNetworkReply = _QNetworkReply
|
||
QCoreApplication = _QCoreApplication
|
||
Qt=_Qt
|
||
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
|
||
QToolButton=_QToolButton
|
||
QSizePolicy=_QSizePolicy
|
||
|
||
YES = QMessageBox.Yes
|
||
NO = QMessageBox.No
|
||
CANCEL = QMessageBox.Cancel
|
||
ICON_QUESTION = QMessageBox.Question
|
||
|
||
QT_VERSION = 5
|
||
# ---------------------------------------------------------
|
||
# Qt5 Enum-Aliase (vereinheitlicht)
|
||
# ---------------------------------------------------------
|
||
|
||
ToolButtonTextBesideIcon = Qt.ToolButtonTextBesideIcon
|
||
ArrowDown = Qt.DownArrow
|
||
ArrowRight = Qt.RightArrow
|
||
# QSizePolicy Enum-Aliase (Qt5)
|
||
SizePolicyPreferred = QSizePolicy.Preferred
|
||
SizePolicyMaximum = QSizePolicy.Maximum
|
||
# ---------------------------------------------------------
|
||
# QDockWidget Feature-Aliase (Qt5)
|
||
# ---------------------------------------------------------
|
||
|
||
DockWidgetMovable = QDockWidget.DockWidgetMovable
|
||
DockWidgetFloatable = QDockWidget.DockWidgetFloatable
|
||
DockWidgetClosable = QDockWidget.DockWidgetClosable
|
||
# ---------------------------------------------------------
|
||
# Dock-Area-Aliase (Qt5)
|
||
# ---------------------------------------------------------
|
||
|
||
DockAreaLeft = Qt.LeftDockWidgetArea
|
||
DockAreaRight = Qt.RightDockWidgetArea
|
||
|
||
|
||
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 __init__(self, *args, **kwargs):
|
||
self._widgets = []
|
||
|
||
def addWidget(self, widget):
|
||
self._widgets.append(widget)
|
||
|
||
def addLayout(self, layout):
|
||
pass
|
||
|
||
def addStretch(self, *args, **kwargs):
|
||
pass
|
||
|
||
def setSpacing(self, *args, **kwargs):
|
||
pass
|
||
|
||
def setContentsMargins(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 _MockQt:
|
||
# ToolButtonStyle
|
||
ToolButtonTextBesideIcon = 0
|
||
|
||
# ArrowType
|
||
ArrowDown = 1
|
||
ArrowRight = 2
|
||
|
||
Qt=_MockQt
|
||
ToolButtonTextBesideIcon = Qt.ToolButtonTextBesideIcon
|
||
ArrowDown = Qt.ArrowDown
|
||
ArrowRight = Qt.ArrowRight
|
||
|
||
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
|
||
|
||
class _MockToolButton(_MockWidget):
|
||
def __init__(self, *args, **kwargs):
|
||
super().__init__(*args, **kwargs)
|
||
self._checked = False
|
||
self.toggled = lambda *a, **k: None
|
||
|
||
def setText(self, text: str) -> None:
|
||
pass
|
||
|
||
def setCheckable(self, value: bool) -> None:
|
||
pass
|
||
|
||
def setChecked(self, value: bool) -> None:
|
||
self._checked = value
|
||
|
||
def setToolButtonStyle(self, *args, **kwargs):
|
||
pass
|
||
|
||
def setArrowType(self, *args, **kwargs):
|
||
pass
|
||
|
||
def setStyleSheet(self, *args, **kwargs):
|
||
pass
|
||
|
||
QToolButton=_MockToolButton
|
||
|
||
class _MockQSizePolicy:
|
||
# horizontale Policies
|
||
Fixed = 0
|
||
Minimum = 1
|
||
Maximum = 2
|
||
Preferred = 3
|
||
Expanding = 4
|
||
MinimumExpanding = 5
|
||
Ignored = 6
|
||
|
||
# vertikale Policies (Qt nutzt dieselben Werte)
|
||
def __init__(self, horizontal=None, vertical=None):
|
||
self.horizontal = horizontal
|
||
self.vertical = vertical
|
||
QSizePolicy=_MockQSizePolicy
|
||
SizePolicyPreferred = QSizePolicy.Preferred
|
||
SizePolicyMaximum = QSizePolicy.Maximum
|
||
DockWidgetMovable = 1
|
||
DockWidgetFloatable = 2
|
||
DockWidgetClosable = 4
|
||
DockAreaLeft = 1
|
||
DockAreaRight = 2
|
||
|
||
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
|
||
|