git-cola v2.7
[git-cola.git] / extras / qtpy / qtpy / __init__.py
blobb6392f5dd4617c80c85fbf826ddefd85e3e337e0
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 The shim will automatically select the first available API (PyQt5, PyQt4 and
14 finally PySide).
16 You can force the use of one specific bindings (e.g. if your application is
17 using one specific bindings and you need to use library that use QtPy) by
18 setting up the ``QT_API`` environment variable.
20 PyQt5
21 =====
23 For PyQt5, you don't have to set anything as it will be used automatically::
25 >>> from qtpy import QtGui, QtWidgets, QtCore
26 >>> print(QtWidgets.QWidget)
29 PyQt4
30 =====
32 Set the ``QT_API`` environment variable to 'pyqt' before importing any python
33 package::
35 >>> import os
36 >>> os.environ['QT_API'] = 'pyqt'
37 >>> from qtpy import QtGui, QtWidgets, QtCore
38 >>> print(QtWidgets.QWidget)
40 PySide
41 ======
43 Set the QT_API environment variable to 'pyside' before importing other
44 packages::
46 >>> import os
47 >>> os.environ['QT_API'] = 'pyside'
48 >>> from qtpy import QtGui, QtWidgets, QtCore
49 >>> print(QtWidgets.QWidget)
51 """
53 import os
55 # Version of QtPy
56 from qtpy._version import __version__
58 #: Qt API environment variable name
59 QT_API = 'QT_API'
60 #: names of the expected PyQt5 api
61 PYQT5_API = ['pyqt5']
62 #: names of the expected PyQt4 api
63 PYQT4_API = [
64 'pyqt', # name used in IPython.qt
65 'pyqt4' # pyqode.qt original name
67 #: names of the expected PySide api
68 PYSIDE_API = ['pyside']
70 os.environ.setdefault(QT_API, 'pyqt5')
71 API = os.environ[QT_API]
72 assert API in (PYQT5_API + PYQT4_API + PYSIDE_API)
74 is_old_pyqt = is_pyqt46 = False
75 PYQT5 = True
76 PYQT4 = PYSIDE = False
79 class PythonQtError(Exception):
80 """Error raise if no bindings could be selected"""
81 pass
84 if API in PYQT5_API:
85 try:
86 from PyQt5.Qt import PYQT_VERSION_STR as PYQT_VERSION # analysis:ignore
87 from PyQt5.Qt import QT_VERSION_STR as QT_VERSION # analysis:ignore
88 PYSIDE_VERSION = None
89 except ImportError:
90 API = os.environ['QT_API'] = 'pyqt'
92 if API in PYQT4_API:
93 try:
94 import sip
95 try:
96 sip.setapi('QString', 2)
97 sip.setapi('QVariant', 2)
98 sip.setapi('QDate', 2)
99 sip.setapi('QDateTime', 2)
100 sip.setapi('QTextStream', 2)
101 sip.setapi('QTime', 2)
102 sip.setapi('QUrl', 2)
103 except AttributeError:
104 # PyQt < v4.6
105 pass
106 from PyQt4.Qt import PYQT_VERSION_STR as PYQT_VERSION # analysis:ignore
107 from PyQt4.Qt import QT_VERSION_STR as QT_VERSION # analysis:ignore
108 PYSIDE_VERSION = None
109 PYQT5 = False
110 PYQT4 = True
111 except ImportError:
112 API = os.environ['QT_API'] = 'pyside'
113 else:
114 is_old_pyqt = PYQT_VERSION.startswith(('4.4', '4.5', '4.6', '4.7'))
115 is_pyqt46 = PYQT_VERSION.startswith('4.6')
117 if API in PYSIDE_API:
118 try:
119 from PySide import __version__ as PYSIDE_VERSION # analysis:ignore
120 from PySide.QtCore import __version__ as QT_VERSION # analysis:ignore
121 PYQT_VERSION = None
122 PYQT5 = False
123 PYSIDE = True
124 except ImportError:
125 raise PythonQtError('No Qt bindings could be found')
127 API_NAME = {'pyqt5': 'PyQt5', 'pyqt': 'PyQt4', 'pyqt4': 'PyQt4',
128 'pyside': 'PySide'}[API]
129 if PYQT4:
130 import sip
131 try:
132 API_NAME += (" (API v{0})".format(sip.getapi('QString')))
133 except AttributeError:
134 pass