garden: use 4-space indents
[git-cola.git] / qtpy / QtCore.py
blobdc0f9981fb9adc9d199c5882bd88bd7093e4652e
1 # -----------------------------------------------------------------------------
2 # Copyright © 2014-2015 Colin Duquesnoy
3 # Copyright © 2009- The Spyder Development Team
5 # Licensed under the terms of the MIT License
6 # (see LICENSE.txt for details)
7 # -----------------------------------------------------------------------------
9 """Provides QtCore classes and functions."""
10 import contextlib
11 from typing import TYPE_CHECKING
13 from . import PYQT5, PYQT6, PYSIDE2, PYSIDE6
14 from . import QT_VERSION as _qt_version
15 from . import parse
16 from ._utils import possibly_static_exec, possibly_static_exec_
18 if PYQT5:
19 from PyQt5.QtCore import *
20 from PyQt5.QtCore import pyqtBoundSignal as SignalInstance
21 from PyQt5.QtCore import pyqtProperty as Property
22 from PyQt5.QtCore import pyqtSignal as Signal
23 from PyQt5.QtCore import pyqtSlot as Slot
25 try:
26 from PyQt5.QtCore import Q_ENUM as QEnum
28 del Q_ENUM
29 except ImportError: # fallback for Qt5.9
30 from PyQt5.QtCore import Q_ENUMS as QEnum
32 del Q_ENUMS
33 from PyQt5.QtCore import QT_VERSION_STR as __version__
35 # Those are imported from `import *`
36 del pyqtSignal, pyqtBoundSignal, pyqtSlot, pyqtProperty, QT_VERSION_STR
38 elif PYQT6:
39 from PyQt6 import QtCore
40 from PyQt6.QtCore import *
41 from PyQt6.QtCore import QT_VERSION_STR as __version__
42 from PyQt6.QtCore import pyqtBoundSignal as SignalInstance
43 from PyQt6.QtCore import pyqtEnum as QEnum
44 from PyQt6.QtCore import pyqtProperty as Property
45 from PyQt6.QtCore import pyqtSignal as Signal
46 from PyQt6.QtCore import pyqtSlot as Slot
48 # For issue #311
49 # Seems like there is an error with sip. Without first
50 # trying to import `PyQt6.QtGui.Qt`, some functions like
51 # `PyQt6.QtCore.Qt.mightBeRichText` are missing.
52 if not TYPE_CHECKING:
53 with contextlib.suppress(ImportError):
54 from PyQt6.QtGui import Qt
56 # Map missing methods
57 QCoreApplication.exec_ = lambda *args, **kwargs: possibly_static_exec(
58 QCoreApplication,
59 *args,
60 **kwargs,
62 QEventLoop.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
63 QThread.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
65 # Those are imported from `import *`
66 del (
67 pyqtSignal,
68 pyqtBoundSignal,
69 pyqtSlot,
70 pyqtProperty,
71 pyqtEnum,
72 QT_VERSION_STR,
75 # Allow unscoped access for enums inside the QtCore module
76 from .enums_compat import promote_enums
78 promote_enums(QtCore)
79 del QtCore
81 # Alias deprecated ItemDataRole enum values removed in Qt6
82 Qt.BackgroundColorRole = (
83 Qt.ItemDataRole.BackgroundColorRole
84 ) = Qt.BackgroundRole
85 Qt.TextColorRole = Qt.ItemDataRole.TextColorRole = Qt.ForegroundRole
87 # Alias for MiddleButton removed in PyQt6 but available in PyQt5, PySide2 and PySide6
88 Qt.MidButton = Qt.MiddleButton
90 # Add removed definition for `Qt.ItemFlags` as an alias of `Qt.ItemFlag`
91 # passing as default value 0 in the same way PySide6 6.5+ does.
92 # Note that for PyQt5 and PySide2 those definitions are two different classes
93 # (one is the flag definition and the other the enum definition)
94 Qt.ItemFlags = lambda value=0: Qt.ItemFlag(value)
96 elif PYSIDE2:
97 import PySide2.QtCore
98 from PySide2.QtCore import *
100 __version__ = PySide2.QtCore.__version__
102 # Missing QtGui utility functions on Qt
103 if getattr(Qt, "mightBeRichText", None) is None:
104 try:
105 from PySide2.QtGui import Qt as guiQt
107 Qt.mightBeRichText = guiQt.mightBeRichText
108 del guiQt
109 except ImportError:
110 # Fails with PySide2 5.12.0
111 pass
113 QCoreApplication.exec = lambda *args, **kwargs: possibly_static_exec_(
114 QCoreApplication,
115 *args,
116 **kwargs,
118 QEventLoop.exec = lambda self, *args, **kwargs: self.exec_(*args, **kwargs)
119 QThread.exec = lambda self, *args, **kwargs: self.exec_(*args, **kwargs)
120 QTextStreamManipulator.exec = lambda self, *args, **kwargs: self.exec_(
121 *args,
122 **kwargs,
125 elif PYSIDE6:
126 import PySide6.QtCore
127 from PySide6.QtCore import *
129 __version__ = PySide6.QtCore.__version__
131 # Missing QtGui utility functions on Qt
132 if getattr(Qt, "mightBeRichText", None) is None:
133 from PySide6.QtGui import Qt as guiQt
135 Qt.mightBeRichText = guiQt.mightBeRichText
136 del guiQt
138 # Alias deprecated ItemDataRole enum values removed in Qt6
139 Qt.BackgroundColorRole = (
140 Qt.ItemDataRole.BackgroundColorRole
141 ) = Qt.BackgroundRole
142 Qt.TextColorRole = Qt.ItemDataRole.TextColorRole = Qt.ForegroundRole
143 Qt.MidButton = Qt.MiddleButton
145 # Map DeprecationWarning methods
146 QCoreApplication.exec_ = lambda *args, **kwargs: possibly_static_exec(
147 QCoreApplication,
148 *args,
149 **kwargs,
151 QEventLoop.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
152 QThread.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
153 QTextStreamManipulator.exec_ = lambda self, *args, **kwargs: self.exec(
154 *args,
155 **kwargs,
158 # Passing as default value 0 in the same way PySide6 6.3.2 does for the `Qt.ItemFlags` definition.
159 if parse(_qt_version) > parse("6.3"):
160 Qt.ItemFlags = lambda value=0: Qt.ItemFlag(value)
162 # For issue #153 and updated for issue #305
163 if PYQT5 or PYQT6:
164 QDate.toPython = lambda self, *args, **kwargs: self.toPyDate(
165 *args,
166 **kwargs,
168 QDateTime.toPython = lambda self, *args, **kwargs: self.toPyDateTime(
169 *args,
170 **kwargs,
172 QTime.toPython = lambda self, *args, **kwargs: self.toPyTime(
173 *args,
174 **kwargs,
176 if PYSIDE2 or PYSIDE6:
177 QDate.toPyDate = lambda self, *args, **kwargs: self.toPython(
178 *args,
179 **kwargs,
181 QDateTime.toPyDateTime = lambda self, *args, **kwargs: self.toPython(
182 *args,
183 **kwargs,
185 QTime.toPyTime = lambda self, *args, **kwargs: self.toPython(
186 *args,
187 **kwargs,
190 # Mirror https://github.com/spyder-ide/qtpy/pull/393
191 if PYQT5 or PYSIDE2:
192 QLibraryInfo.path = QLibraryInfo.location
193 QLibraryInfo.LibraryPath = QLibraryInfo.LibraryLocation
194 if PYQT6 or PYSIDE6:
195 QLibraryInfo.location = QLibraryInfo.path
196 QLibraryInfo.LibraryLocation = QLibraryInfo.LibraryPath