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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
# -----------------------------------------------------------------------------
# Copyright © 2014-2015 Colin Duquesnoy
# Copyright © 2009- The Spyder Development Team
#
# Licensed under the terms of the MIT License
# (see LICENSE.txt for details)
# -----------------------------------------------------------------------------
"""Provides QtGui classes and functions."""
from functools import partialmethod
from . import PYQT5, PYQT6, PYSIDE2, PYSIDE6
from . import QT_VERSION as _qt_version
from . import parse
from ._utils import (
getattr_missing_optional_dep,
possibly_static_exec,
set_shortcut,
set_shortcuts,
)
_missing_optional_names = {}
_QTOPENGL_NAMES = {
"QOpenGLBuffer",
"QOpenGLContext",
"QOpenGLContextGroup",
"QOpenGLDebugLogger",
"QOpenGLDebugMessage",
"QOpenGLFramebufferObject",
"QOpenGLFramebufferObjectFormat",
"QOpenGLPixelTransferOptions",
"QOpenGLShader",
"QOpenGLShaderProgram",
"QOpenGLTexture",
"QOpenGLTextureBlitter",
"QOpenGLVersionProfile",
"QOpenGLVertexArrayObject",
"QOpenGLWindow",
}
def __getattr__(name):
"""Custom getattr to chain and wrap errors due to missing optional deps."""
raise getattr_missing_optional_dep(
name,
module_name=__name__,
optional_names=_missing_optional_names,
)
if PYQT5:
from PyQt5.QtGui import *
# Backport items moved to QtGui in Qt6
from PyQt5.QtWidgets import (
QAction,
QActionGroup,
QFileSystemModel,
QShortcut,
QUndoCommand,
)
elif PYQT6:
from PyQt6 import QtGui
from PyQt6.QtGui import *
# Attempt to import QOpenGL* classes, but if that fails,
# don't raise an exception until the name is explicitly accessed.
# See https://github.com/spyder-ide/qtpy/pull/387/
try:
from PyQt6.QtOpenGL import *
except ImportError as error:
for name in _QTOPENGL_NAMES:
_missing_optional_names[name] = {
"name": "PyQt6.QtOpenGL",
"missing_package": "pyopengl",
"import_error": error,
}
QFontMetrics.width = lambda self, *args, **kwargs: self.horizontalAdvance(
*args,
**kwargs,
)
QFontMetricsF.width = lambda self, *args, **kwargs: self.horizontalAdvance(
*args,
**kwargs,
)
# Map missing/renamed methods
QDrag.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
QGuiApplication.exec_ = lambda *args, **kwargs: possibly_static_exec(
QGuiApplication,
*args,
**kwargs,
)
QTextDocument.print_ = lambda self, *args, **kwargs: self.print(
*args,
**kwargs,
)
# Allow unscoped access for enums inside the QtGui module
from .enums_compat import promote_enums
promote_enums(QtGui)
del QtGui
elif PYSIDE2:
from PySide2.QtGui import *
# Backport items moved to QtGui in Qt6
from PySide2.QtWidgets import (
QAction,
QActionGroup,
QFileSystemModel,
QShortcut,
QUndoCommand,
)
if hasattr(QFontMetrics, "horizontalAdvance"):
# Needed to prevent raising a DeprecationWarning when using QFontMetrics.width
QFontMetrics.width = (
lambda self, *args, **kwargs: self.horizontalAdvance(
*args,
**kwargs,
)
)
elif PYSIDE6:
from PySide6.QtGui import *
# Attempt to import QOpenGL* classes, but if that fails,
# don't raise an exception until the name is explicitly accessed.
# See https://github.com/spyder-ide/qtpy/pull/387/
try:
from PySide6.QtOpenGL import *
except ImportError as error:
for name in _QTOPENGL_NAMES:
_missing_optional_names[name] = {
"name": "PySide6.QtOpenGL",
"missing_package": "pyopengl",
"import_error": error,
}
# Backport `QFileSystemModel` moved to QtGui in Qt6
from PySide6.QtWidgets import QFileSystemModel
QFontMetrics.width = lambda self, *args, **kwargs: self.horizontalAdvance(
*args,
**kwargs,
)
QFontMetricsF.width = lambda self, *args, **kwargs: self.horizontalAdvance(
*args,
**kwargs,
)
# Map DeprecationWarning methods
QDrag.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
QGuiApplication.exec_ = lambda *args, **kwargs: possibly_static_exec(
QGuiApplication,
*args,
**kwargs,
)
if PYSIDE2 or PYSIDE6:
# PySide{2,6} do not accept the `mode` keyword argument in
# QTextCursor.movePosition() even though it is a valid optional argument
# as per C++ API. Fix this by monkeypatching.
#
# Notes:
#
# * The `mode` argument is called `arg__2` in PySide{2,6} as per
# QTextCursor.movePosition.__doc__ and __signature__. Using `arg__2` as
# keyword argument works as intended, so does using a positional
# argument. Tested with PySide2 5.15.0, 5.15.2.1 and 5.15.3 and PySide6
# 6.3.0; older version, down to PySide 1, are probably affected as well [1].
#
# * PySide2 5.15.0 and 5.15.2.1 silently ignore invalid keyword arguments,
# i.e. passing the `mode` keyword argument has no effect and doesn`t
# raise an exception. Older versions, down to PySide 1, are probably
# affected as well [1]. At least PySide2 5.15.3 and PySide6 6.3.0 raise an
# exception when `mode` or any other invalid keyword argument is passed.
#
# [1] https://bugreports.qt.io/browse/PYSIDE-185
movePosition = QTextCursor.movePosition
def movePositionPatched(
self,
operation: QTextCursor.MoveOperation,
mode: QTextCursor.MoveMode = QTextCursor.MoveAnchor,
n: int = 1,
) -> bool:
return movePosition(self, operation, mode, n)
QTextCursor.movePosition = movePositionPatched
if PYQT5 or PYSIDE2:
# Part of the fix for https://github.com/spyder-ide/qtpy/issues/394
from qtpy.QtCore import QPointF as __QPointF
QNativeGestureEvent.x = lambda self: self.localPos().toPoint().x()
QNativeGestureEvent.y = lambda self: self.localPos().toPoint().y()
QNativeGestureEvent.position = lambda self: self.localPos()
QNativeGestureEvent.globalX = lambda self: self.globalPos().x()
QNativeGestureEvent.globalY = lambda self: self.globalPos().y()
QNativeGestureEvent.globalPosition = lambda self: __QPointF(
float(self.globalPos().x()),
float(self.globalPos().y()),
)
QEnterEvent.position = lambda self: self.localPos()
QEnterEvent.globalPosition = lambda self: __QPointF(
float(self.globalX()),
float(self.globalY()),
)
QTabletEvent.position = lambda self: self.posF()
QTabletEvent.globalPosition = lambda self: self.globalPosF()
QHoverEvent.x = lambda self: self.pos().x()
QHoverEvent.y = lambda self: self.pos().y()
QHoverEvent.position = lambda self: self.posF()
# No `QHoverEvent.globalPosition`, `QHoverEvent.globalX`,
# nor `QHoverEvent.globalY` in the Qt5 docs.
QMouseEvent.position = lambda self: self.localPos()
QMouseEvent.globalPosition = lambda self: __QPointF(
float(self.globalX()),
float(self.globalY()),
)
# Follow similar approach for `QDropEvent` and child classes
QDropEvent.position = lambda self: self.posF()
if PYQT6 or PYSIDE6:
# Part of the fix for https://github.com/spyder-ide/qtpy/issues/394
for _class in (
QNativeGestureEvent,
QEnterEvent,
QTabletEvent,
QHoverEvent,
QMouseEvent,
):
for _obsolete_function in (
"pos",
"x",
"y",
"globalPos",
"globalX",
"globalY",
):
if hasattr(_class, _obsolete_function):
delattr(_class, _obsolete_function)
QSinglePointEvent.pos = lambda self: self.position().toPoint()
QSinglePointEvent.posF = lambda self: self.position()
QSinglePointEvent.localPos = lambda self: self.position()
QSinglePointEvent.x = lambda self: self.position().toPoint().x()
QSinglePointEvent.y = lambda self: self.position().toPoint().y()
QSinglePointEvent.globalPos = lambda self: self.globalPosition().toPoint()
QSinglePointEvent.globalX = (
lambda self: self.globalPosition().toPoint().x()
)
QSinglePointEvent.globalY = (
lambda self: self.globalPosition().toPoint().y()
)
# Follow similar approach for `QDropEvent` and child classes
QDropEvent.pos = lambda self: self.position().toPoint()
QDropEvent.posF = lambda self: self.position()
# Make `QAction.setShortcut` and `QAction.setShortcuts` compatible with Qt>=6.4
if PYQT5 or PYSIDE2 or parse(_qt_version) < parse("6.4"):
class _QAction(QAction):
old_set_shortcut = QAction.setShortcut
old_set_shortcuts = QAction.setShortcuts
def setShortcut(self, shortcut):
return set_shortcut(
self,
shortcut,
old_set_shortcut=_QAction.old_set_shortcut,
)
def setShortcuts(self, shortcuts):
return set_shortcuts(
self,
shortcuts,
old_set_shortcuts=_QAction.old_set_shortcuts,
)
_action_set_shortcut = partialmethod(
set_shortcut,
old_set_shortcut=QAction.setShortcut,
)
_action_set_shortcuts = partialmethod(
set_shortcuts,
old_set_shortcuts=QAction.setShortcuts,
)
QAction.setShortcut = _action_set_shortcut
QAction.setShortcuts = _action_set_shortcuts
# Despite the two previous lines!
if (
QAction.setShortcut is not _action_set_shortcut
or QAction.setShortcuts is not _action_set_shortcuts
):
QAction = _QAction
|