BranchesWidget: sort order toggle button
[git-cola.git] / qtpy / __init__.py
blob9da0b7d5a67c40f175bb5108f24b2aa072795c74
1 # -*- coding: utf-8 -*-
3 # Copyright © 2009- The Spyder Development Team
4 # Copyright © 2014-2015 Colin Duquesnoy
6 # Licensed under the terms of the MIT License
7 # (see LICENSE.txt for details)
9 """
10 **QtPy** is a shim over the various Python Qt bindings. It is used to write
11 Qt binding indenpendent libraries or applications.
13 If one of the APIs has already been imported, then it will be used.
15 Otherwise, the shim will automatically select the first available API (PyQt5,
16 PySide2, PyQt4 and finally PySide); in that case, you can force the use of one
17 specific bindings (e.g. if your application is using one specific bindings and
18 you need to use library that use QtPy) by setting up the ``QT_API`` environment
19 variable.
21 PyQt5
22 =====
24 For PyQt5, you don't have to set anything as it will be used automatically::
26 >>> from qtpy import QtGui, QtWidgets, QtCore
27 >>> print(QtWidgets.QWidget)
30 PySide2
31 ======
33 Set the QT_API environment variable to 'pyside2' before importing other
34 packages::
36 >>> import os
37 >>> os.environ['QT_API'] = 'pyside2'
38 >>> from qtpy import QtGui, QtWidgets, QtCore
39 >>> print(QtWidgets.QWidget)
41 PyQt4
42 =====
44 Set the ``QT_API`` environment variable to 'pyqt' before importing any python
45 package::
47 >>> import os
48 >>> os.environ['QT_API'] = 'pyqt'
49 >>> from qtpy import QtGui, QtWidgets, QtCore
50 >>> print(QtWidgets.QWidget)
52 PySide
53 ======
55 Set the QT_API environment variable to 'pyside' before importing other
56 packages::
58 >>> import os
59 >>> os.environ['QT_API'] = 'pyside'
60 >>> from qtpy import QtGui, QtWidgets, QtCore
61 >>> print(QtWidgets.QWidget)
63 """
65 import os
66 import sys
67 import warnings
69 # Version of QtPy
70 from ._version import __version__
73 class PythonQtError(RuntimeError):
74 """Error raise if no bindings could be selected."""
75 pass
78 class PythonQtWarning(Warning):
79 """Warning if some features are not implemented in a binding."""
80 pass
83 # Qt API environment variable name
84 QT_API = 'QT_API'
86 # Names of the expected PyQt5 api
87 PYQT5_API = ['pyqt5']
89 # Names of the expected PyQt4 api
90 PYQT4_API = [
91 'pyqt', # name used in IPython.qt
92 'pyqt4' # pyqode.qt original name
95 # Names of the expected PySide api
96 PYSIDE_API = ['pyside']
98 # Names of the expected PySide2 api
99 PYSIDE2_API = ['pyside2']
101 # Setting a default value for QT_API
102 os.environ.setdefault(QT_API, 'pyqt5')
104 API = os.environ[QT_API].lower()
105 initial_api = API
106 assert API in (PYQT5_API + PYQT4_API + PYSIDE_API + PYSIDE2_API)
108 is_old_pyqt = is_pyqt46 = False
109 PYQT5 = True
110 PYQT4 = PYSIDE = PYSIDE2 = False
113 if 'PyQt5' in sys.modules:
114 API = 'pyqt5'
115 elif 'PySide2' in sys.modules:
116 API = 'pyside2'
117 elif 'PyQt4' in sys.modules:
118 API = 'pyqt4'
119 elif 'PySide' in sys.modules:
120 API = 'pyside'
123 if API in PYQT5_API:
124 try:
125 from PyQt5.QtCore import PYQT_VERSION_STR as PYQT_VERSION # analysis:ignore
126 from PyQt5.QtCore import QT_VERSION_STR as QT_VERSION # analysis:ignore
127 PYSIDE_VERSION = None
128 except ImportError:
129 API = os.environ['QT_API'] = 'pyside2'
131 if API in PYSIDE2_API:
132 try:
133 from PySide2 import __version__ as PYSIDE_VERSION # analysis:ignore
134 from PySide2.QtCore import __version__ as QT_VERSION # analysis:ignore
136 PYQT_VERSION = None
137 PYQT5 = False
138 PYSIDE2 = True
139 except ImportError:
140 API = os.environ['QT_API'] = 'pyqt'
142 if API in PYQT4_API:
143 try:
144 import sip
145 try:
146 sip.setapi('QString', 2)
147 sip.setapi('QVariant', 2)
148 sip.setapi('QDate', 2)
149 sip.setapi('QDateTime', 2)
150 sip.setapi('QTextStream', 2)
151 sip.setapi('QTime', 2)
152 sip.setapi('QUrl', 2)
153 except (AttributeError, ValueError):
154 # PyQt < v4.6
155 pass
156 from PyQt4.Qt import PYQT_VERSION_STR as PYQT_VERSION # analysis:ignore
157 from PyQt4.Qt import QT_VERSION_STR as QT_VERSION # analysis:ignore
158 PYSIDE_VERSION = None
159 PYQT5 = False
160 PYQT4 = True
161 except ImportError:
162 API = os.environ['QT_API'] = 'pyside'
163 else:
164 is_old_pyqt = PYQT_VERSION.startswith(('4.4', '4.5', '4.6', '4.7'))
165 is_pyqt46 = PYQT_VERSION.startswith('4.6')
167 if API in PYSIDE_API:
168 try:
169 from PySide import __version__ as PYSIDE_VERSION # analysis:ignore
170 from PySide.QtCore import __version__ as QT_VERSION # analysis:ignore
171 PYQT_VERSION = None
172 PYQT5 = PYSIDE2 = False
173 PYSIDE = True
174 except ImportError:
175 raise PythonQtError('No Qt bindings could be found')
177 API_NAME = {'pyqt5': 'PyQt5', 'pyqt': 'PyQt4', 'pyqt4': 'PyQt4',
178 'pyside': 'PySide', 'pyside2':'PySide2'}[API]
180 if PYQT4:
181 import sip
182 try:
183 API_NAME += (" (API v{0})".format(sip.getapi('QString')))
184 except AttributeError:
185 pass