-
Notifications
You must be signed in to change notification settings - Fork 9
/
QtGui.py
425 lines (392 loc) · 14.6 KB
/
QtGui.py
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
from warnings import warn
from .QtCore import QT_VERSION_INFO as __QT_VERSION_INFO
from . import _api
# Names imported from Qt4's QtGui module
__Qt4_QtGui = [
'QAbstractTextDocumentLayout',
'QActionEvent',
'QBitmap',
'QBrush',
'QClipboard',
'QCloseEvent',
'QColor',
'QConicalGradient',
'QContextMenuEvent',
'QCursor',
'QDesktopServices',
'QDoubleValidator',
'QDrag',
'QDragEnterEvent',
'QDragLeaveEvent',
'QDragMoveEvent',
'QDropEvent',
'QFileOpenEvent',
'QFocusEvent',
'QFont',
'QFontDatabase',
'QFontInfo',
'QFontMetrics',
'QFontMetricsF',
'QGlyphRun',
'QGradient',
'QHelpEvent',
'QHideEvent',
'QHoverEvent',
'QIcon',
'QIconDragEvent',
'QIconEngine',
'QImage',
'QImageIOHandler',
'QImageReader',
'QImageWriter',
'QInputEvent',
'QInputMethodEvent',
'QIntValidator',
'QKeyEvent',
'QKeySequence',
'QLinearGradient',
'QMatrix2x2',
'QMatrix2x3',
'QMatrix2x4',
'QMatrix3x2',
'QMatrix3x3',
'QMatrix3x4',
'QMatrix4x2',
'QMatrix4x3',
'QMatrix4x4',
'QMouseEvent',
'QMoveEvent',
'QMovie',
'QPaintDevice',
'QPaintEngine',
'QPaintEngineState',
'QPaintEvent',
'QPainter',
'QPainterPath',
'QPainterPathStroker',
'QPalette',
'QPen',
'QPicture',
'QPictureIO',
'QPixmap',
'QPixmapCache',
'QPolygon',
'QPolygonF',
'QQuaternion',
'QRadialGradient',
'QRawFont',
'QRegExpValidator',
'QRegion',
'QResizeEvent',
'QSessionManager',
'QShortcutEvent',
'QShowEvent',
'QStandardItem',
'QStandardItemModel',
'QStaticText',
'QStatusTipEvent',
'QSyntaxHighlighter',
'QTabletEvent',
'QTextBlock',
'QTextBlockFormat',
'QTextBlockGroup',
'QTextBlockUserData',
'QTextCharFormat',
'QTextCursor',
'QTextDocument',
'QTextDocumentFragment',
'QTextDocumentWriter',
'QTextFormat',
'QTextFragment',
'QTextFrame',
'QTextFrameFormat',
'QTextImageFormat',
'QTextInlineObject',
'QTextItem',
'QTextLayout',
'QTextLength',
'QTextLine',
'QTextList',
'QTextListFormat',
'QTextObject',
'QTextObjectInterface',
'QTextOption',
'QTextTable',
'QTextTableCell',
'QTextTableCellFormat',
'QTextTableFormat',
'QTouchEvent',
'QTransform',
'QValidator',
'QVector2D',
'QVector3D',
'QVector4D',
'QWhatsThisClickedEvent',
'QWheelEvent',
'QWindowStateChangeEvent',
'qAlpha',
'qBlue',
'qFuzzyCompare',
'qGray',
'qGreen',
'qIsGray',
'qRed',
'qRgb',
'qRgba'
]
if _api.USED_API == _api.QT_API_PYQT6:
from PyQt6.QtGui import *
# Deprecated QEnterEvent accessors
if not hasattr(QEnterEvent, "pos"):
QEnterEvent.pos = lambda self: self.position().toPoint()
if not hasattr(QEnterEvent, "globalPos"):
QEnterEvent.globalPos = lambda self: self.globalPosition().toPoint()
if not hasattr(QEnterEvent, "x"):
QEnterEvent.x = lambda self: self.position().toPoint().x()
if not hasattr(QEnterEvent, "y"):
QEnterEvent.y = lambda self: self.position().toPoint().y()
if not hasattr(QEnterEvent, "globalX"):
QEnterEvent.globalX = lambda self: self.globalPosition().toPoint().x()
if not hasattr(QEnterEvent, "globalY"):
QEnterEvent.globalY = lambda self: self.globalPosition().toPoint().y()
if not hasattr(QEnterEvent, "localPos"):
QEnterEvent.localPos = lambda self: self.position()
if not hasattr(QEnterEvent, "windowPos"):
QEnterEvent.windowPos = lambda self: self.scenePosition()
if not hasattr(QEnterEvent, "screenPos"):
QEnterEvent.screenPos = lambda self: self.globalPosition()
# Deprecated QMouseEvent accessors
if not hasattr(QMouseEvent, "pos"):
QMouseEvent.pos = lambda self: self.position().toPoint()
if not hasattr(QMouseEvent, "globalPos"):
QMouseEvent.globalPos = lambda self: self.globalPosition().toPoint()
if not hasattr(QMouseEvent, "x"):
QMouseEvent.x = lambda self: self.position().x()
if not hasattr(QMouseEvent, "y"):
QMouseEvent.y = lambda self: self.position().y()
if not hasattr(QMouseEvent, "globalX"):
QMouseEvent.globalX = lambda self: self.globalPosition().x()
if not hasattr(QMouseEvent, "globalY"):
QMouseEvent.globalY = lambda self: self.globalPosition().y()
# Deprecated QDropEvent accessors
if not hasattr(QDropEvent, "pos"):
QDropEvent.pos = lambda self: self.position().toPoint()
if not hasattr(QDropEvent, "posF"):
QDropEvent.posF = lambda self: self.position()
if not hasattr(QDropEvent, "mouseButtons"):
QDropEvent.mouseButtons = lambda self: self.buttons()
if not hasattr(QDropEvent, "keyboardModifiers"):
QDropEvent.keyboardModifiers = lambda self: self.modifiers()
# Deprecated QWheelEvent accessors
if not hasattr(QWheelEvent, "pos"):
QWheelEvent.pos = lambda self: self.position().toPoint()
if not hasattr(QWheelEvent, "posF"):
QWheelEvent.posF = lambda self: self.position()
if not hasattr(QWheelEvent, "globalPos"):
QWheelEvent.globalPos = lambda self: self.globalPosition().toPoint()
if not hasattr(QWheelEvent, "globalPosF"):
QWheelEvent.globalPosF = lambda self: self.globalPosition()
if not hasattr(QWheelEvent, "x"):
QWheelEvent.x = lambda self: self.position().x()
if not hasattr(QWheelEvent, "y"):
QWheelEvent.y = lambda self: self.position().y()
if not hasattr(QWheelEvent, "globalX"):
QWheelEvent.globalX = lambda self: self.globalPosition().x()
if not hasattr(QWheelEvent, "globalY"):
QWheelEvent.globalY = lambda self: self.globalPosition().y()
if not hasattr(QWheelEvent, "mouseButtons"):
QWheelEvent.mouseButtons = lambda self: self.buttons()
if not hasattr(QWheelEvent, "keyboardModifiers"):
QWheelEvent.keyboardModifiers = lambda self: self.modifiers()
elif _api.USED_API == _api.QT_API_PYQT5:
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QUndoCommand, QUndoStack, QUndoGroup
from PyQt5.QtWidgets import QShortcut, QAction, QActionGroup
from PyQt5.QtCore import PYQT_VERSION as _PYQT_VERSION
if _PYQT_VERSION < 0x50c00: # 5.12.0
class WheelEvent(QWheelEvent):
from PyQt5.QtCore import (QPointF as _QPointF,
QPoint as _QPoint,
Qt as _Qt)
_constructor_signature = \
((_QPointF, _QPoint),
(_QPointF, _QPoint),
(_QPoint,),
(_QPoint,),
(_Qt.MouseButtons, _Qt.MouseButton),
(_Qt.KeyboardModifiers, _Qt.KeyboardModifier),
(_Qt.ScrollPhase,),
(bool,),
(_Qt.MouseEventSource,))
def __init__(self, *args):
sig = WheelEvent._constructor_signature
if len(args) == len(sig) and \
all(any(isinstance(a, t) for t in ts)
for a, ts in zip(args, sig)):
angleDelta = args[3]
if abs(angleDelta.x()) > abs(angleDelta.y()):
orientation = 0x1 # horizontal
delta = angleDelta.x()
else:
orientation = 0x2 # vertical
delta = angleDelta.y()
args = args[:4] + \
(delta, orientation) + \
args[4:7] + (args[8], args[7])
super().__init__(*args)
QWheelEvent = WheelEvent
elif _api.USED_API == _api.QT_API_PYQT4:
import PyQt4.QtGui as _QtGui
globals().update(
{name: getattr(_QtGui, name)
for name in __Qt4_QtGui if hasattr(_QtGui, name)}
)
del _QtGui
elif _api.USED_API == _api.QT_API_PYSIDE:
from PySide import QtGui as _QtGui
globals().update(
{name: getattr(_QtGui, name)
for name in __Qt4_QtGui if hasattr(_QtGui, name)}
)
del _QtGui
# Known to be present in PyQt4 but not in PySide:
# QGlyphRun, QRawFont, QStaticText, QTextDocumentWriter
elif _api.USED_API == _api.QT_API_PYSIDE2:
from PySide2.QtGui import *
from PySide2.QtWidgets import QUndoCommand, QUndoStack, QUndoGroup
from PySide2.QtWidgets import QShortcut, QAction, QActionGroup
if _api.USED_API in [_api.QT_API_PYQT4, _api.QT_API_PYSIDE]:
from AnyQt import QtCore as __QtCore
def __QWheelEvent_angleDelta(self):
"""
Qt5 compatible QWheelEvent.angleDelta
Return the delta as an x or y axis aligned QPoint vector
"""
if self.orientation() == __QtCore.Qt.Horizontal:
return __QtCore.QPoint(self.delta(), 0)
else:
return __QtCore.QPoint(0, self.delta())
def __QWheelEvent_pixelDelta(self):
"""
Qt5 compatible QWheelEvent.pixelDelta
Always return a null QPoint. This is acceptable and compatible with
the API (i.e. the pixelDelta is only supported on platforms where
high resolution is available).
"""
return __QtCore.QPoint()
QWheelEvent.angleDelta = __QWheelEvent_angleDelta
QWheelEvent.pixelDelta = __QWheelEvent_pixelDelta
if _api.USED_API == _api.QT_API_PYSIDE2:
from PySide2.QtCore import QRectF as __QRectF
_QPainter_drawPixmapFragments_orig = QPainter.drawPixmapFragments
class __ArgsTypeError(TypeError): pass
def _QPainter_drawPixmapFragments(painter, fragments, *args, **kwargs):
def f1(fragment, size, pixmap=None, hints=QPainter.PixmapFragmentHints()):
# dispatch to original if possible
if isinstance(size, int) and isinstance(pixmap, QPixmap):
_QPainter_drawPixmapFragments_orig(painter, fragment, size, pixmap, hints)
else:
raise __ArgsTypeError
try:
f1(fragments, *args, **kwargs)
return
except __ArgsTypeError:
pass
def f2(fragments, pixmap, hints=QPainter.PixmapFragmentHints()):
if isinstance(pixmap, QPixmap):
return (fragments, pixmap)
else:
raise TypeError
fragments, pixmap = f2(fragments, *args, **kwargs)
# emulate the api
painter.save()
oldtr = painter.worldTransform()
oldopacity = painter.opacity()
for frag in fragments: # type: QPainter.PixmapFragment
tr = QTransform(oldtr)
x, y = frag.x, frag.y
tr.translate(x, y)
tr.rotate(frag.rotation)
painter.setTransform(tr)
painter.setOpacity(oldopacity * frag.opacity)
w = frag.scaleX * frag.width
h = frag.scaleY * frag.height
sourceRect = __QRectF(
frag.sourceLeft, frag.sourceTop, frag.width, frag.height)
painter.drawPixmap(
__QRectF(-0.5 * w, -0.5 * h, w, h), pixmap, sourceRect)
painter.restore()
QPainter.drawPixmapFragments = _QPainter_drawPixmapFragments
if _api.USED_API in (_api.QT_API_PYQT5, _api.QT_API_PYSIDE2):
# PyQt5, PySide2 do not support setPageSize(QPageSize) overload
def QPdfWriter_setPageSize(self, size):
if isinstance(size, QPageSize):
self.setPageSizeMM(size.size(QPageSize.Millimeter))
return self.pageLayout().pageSize().isEquivalentTo(size)
else:
__QPdfWriter_setPageSize(self, size)
__QPdfWriter_setPageSize = QPdfWriter.setPageSize
QPdfWriter.setPageSize = QPdfWriter_setPageSize
del QPdfWriter_setPageSize
if not hasattr(QGuiApplication, 'screenAt'):
def QGuiApplication_screenAt(pos):
visited = set()
for screen in QGuiApplication.screens():
if screen in visited:
continue
# The virtual siblings include the screen itself, so iterate directly
for sibling in screen.virtualSiblings():
if sibling.geometry().contains(pos):
return sibling
visited.add(sibling)
return None
QGuiApplication.screenAt = staticmethod(QGuiApplication_screenAt)
del QGuiApplication_screenAt
# Alias QFontMetrics(F).horizontalAdvance to QFontMetrics(F).width
# when it does not exists
if not hasattr(QFontMetrics, "horizontalAdvance"):
def QFontMetrics_horizontalAdvance(self, *args, **kwargs):
return __QFontMetrics_width(self, *args, **kwargs)
__QFontMetrics_width = QFontMetrics.width
QFontMetrics.horizontalAdvance = QFontMetrics_horizontalAdvance
del QFontMetrics_horizontalAdvance
if not hasattr(QFontMetricsF, "horizontalAdvance"):
def QFontMetricsF_horizontalAdvance(self, *args, **kwargs):
return __QFontMetricsF_width(self, *args, **kwargs)
__QFontMetricsF_width = QFontMetricsF.width
QFontMetricsF.horizontalAdvance = QFontMetricsF_horizontalAdvance
del QFontMetricsF_horizontalAdvance
# Warn on deprecated QFontMetrics.width
def QFontMetrics_width(self, *args, **kwargs):
warn("QFontMetrics(F).width is obsolete. "
"Replace with QFontMetrics(F).horizontalAdvance",
DeprecationWarning, stacklevel=2)
return self.horizontalAdvance(*args, **kwargs)
QFontMetricsF.width = QFontMetrics_width
QFontMetrics.width = QFontMetrics_width
del QFontMetrics_width
if __QT_VERSION_INFO < (6, 0):
class QFontDatabase(QFontDatabase):
def staticwrapper(f):
from functools import wraps
@wraps(f)
def wrapped(*args, **kwargs):
return f(QFontDatabase(), *args, **kwargs)
return staticmethod(wrapped)
bold = staticwrapper(QFontDatabase.bold)
families = staticwrapper(QFontDatabase.families)
font = staticwrapper(QFontDatabase.font)
isBitmapScalable = staticwrapper(QFontDatabase.isBitmapScalable)
isFixedPitch = staticwrapper(QFontDatabase.isFixedPitch)
isPrivateFamily = staticwrapper(QFontDatabase.isPrivateFamily)
isScalable = staticwrapper(QFontDatabase.isScalable)
isSmoothlyScalable = staticwrapper(QFontDatabase.isSmoothlyScalable)
italic = staticwrapper(QFontDatabase.italic)
pointSizes = staticwrapper(QFontDatabase.pointSizes)
smoothSizes = staticwrapper(QFontDatabase.smoothSizes)
styleString = staticwrapper(QFontDatabase.styleString)
styles = staticwrapper(QFontDatabase.styles)
weight = staticwrapper(QFontDatabase.weight)
writingSystems = staticwrapper(QFontDatabase.writingSystems)
del staticwrapper
_api.apply_global_fixes(globals())