doc: add Axel to the credits
[git-cola.git] / qtpy / compat.py
blobf579454895829f2d5e3a990a32a290dba0e5f499
1 # -*- coding: utf-8 -*-
3 # Copyright © 2009- The Spyder Development Team
4 # Licensed under the terms of the MIT License
6 """
7 Compatibility functions
8 """
10 from __future__ import print_function
11 import sys
12 import collections
14 from . import PYQT4
15 from .QtWidgets import QFileDialog
16 from .py3compat import is_text_string, to_text_string, TEXT_TYPES
19 # =============================================================================
20 # QVariant conversion utilities
21 # =============================================================================
22 PYQT_API_1 = False
23 if PYQT4:
24 import sip
25 try:
26 PYQT_API_1 = sip.getapi('QVariant') == 1 # PyQt API #1
27 except AttributeError:
28 # PyQt <v4.6
29 PYQT_API_1 = True
31 def to_qvariant(pyobj=None):
32 """Convert Python object to QVariant
33 This is a transitional function from PyQt API #1 (QVariant exist)
34 to PyQt API #2 and Pyside (QVariant does not exist)"""
35 if PYQT_API_1:
36 # PyQt API #1
37 from PyQt4.QtCore import QVariant
38 return QVariant(pyobj)
39 else:
40 # PyQt API #2
41 return pyobj
43 def from_qvariant(qobj=None, convfunc=None):
44 """Convert QVariant object to Python object
45 This is a transitional function from PyQt API #1 (QVariant exist)
46 to PyQt API #2 and Pyside (QVariant does not exist)"""
47 if PYQT_API_1:
48 # PyQt API #1
49 assert isinstance(convfunc, collections.Callable)
50 if convfunc in TEXT_TYPES or convfunc is to_text_string:
51 return convfunc(qobj.toString())
52 elif convfunc is bool:
53 return qobj.toBool()
54 elif convfunc is int:
55 return qobj.toInt()[0]
56 elif convfunc is float:
57 return qobj.toDouble()[0]
58 else:
59 return convfunc(qobj)
60 else:
61 # PyQt API #2
62 return qobj
63 else:
64 def to_qvariant(obj=None): # analysis:ignore
65 """Convert Python object to QVariant
66 This is a transitional function from PyQt API#1 (QVariant exist)
67 to PyQt API#2 and Pyside (QVariant does not exist)"""
68 return obj
70 def from_qvariant(qobj=None, pytype=None): # analysis:ignore
71 """Convert QVariant object to Python object
72 This is a transitional function from PyQt API #1 (QVariant exist)
73 to PyQt API #2 and Pyside (QVariant does not exist)"""
74 return qobj
77 # =============================================================================
78 # Wrappers around QFileDialog static methods
79 # =============================================================================
80 def getexistingdirectory(parent=None, caption='', basedir='',
81 options=QFileDialog.ShowDirsOnly):
82 """Wrapper around QtGui.QFileDialog.getExistingDirectory static method
83 Compatible with PyQt >=v4.4 (API #1 and #2) and PySide >=v1.0"""
84 # Calling QFileDialog static method
85 if sys.platform == "win32":
86 # On Windows platforms: redirect standard outputs
87 _temp1, _temp2 = sys.stdout, sys.stderr
88 sys.stdout, sys.stderr = None, None
89 try:
90 result = QFileDialog.getExistingDirectory(parent, caption, basedir,
91 options)
92 finally:
93 if sys.platform == "win32":
94 # On Windows platforms: restore standard outputs
95 sys.stdout, sys.stderr = _temp1, _temp2
96 if not is_text_string(result):
97 # PyQt API #1
98 result = to_text_string(result)
99 return result
102 def _qfiledialog_wrapper(attr, parent=None, caption='', basedir='',
103 filters='', selectedfilter='', options=None):
104 if options is None:
105 options = QFileDialog.Options(0)
106 try:
107 # PyQt <v4.6 (API #1)
108 from .QtCore import QString
109 except ImportError:
110 # PySide or PyQt >=v4.6
111 QString = None # analysis:ignore
112 tuple_returned = True
113 try:
114 # PyQt >=v4.6
115 func = getattr(QFileDialog, attr+'AndFilter')
116 except AttributeError:
117 # PySide or PyQt <v4.6
118 func = getattr(QFileDialog, attr)
119 if QString is not None:
120 selectedfilter = QString()
121 tuple_returned = False
123 # Calling QFileDialog static method
124 if sys.platform == "win32":
125 # On Windows platforms: redirect standard outputs
126 _temp1, _temp2 = sys.stdout, sys.stderr
127 sys.stdout, sys.stderr = None, None
128 try:
129 result = func(parent, caption, basedir,
130 filters, selectedfilter, options)
131 except TypeError:
132 # The selectedfilter option (`initialFilter` in Qt) has only been
133 # introduced in Jan. 2010 for PyQt v4.7, that's why we handle here
134 # the TypeError exception which will be raised with PyQt v4.6
135 # (see Issue 960 for more details)
136 result = func(parent, caption, basedir, filters, options)
137 finally:
138 if sys.platform == "win32":
139 # On Windows platforms: restore standard outputs
140 sys.stdout, sys.stderr = _temp1, _temp2
142 # Processing output
143 if tuple_returned:
144 # PySide or PyQt >=v4.6
145 output, selectedfilter = result
146 else:
147 # PyQt <v4.6 (API #1)
148 output = result
149 if QString is not None:
150 # PyQt API #1: conversions needed from QString/QStringList
151 selectedfilter = to_text_string(selectedfilter)
152 if isinstance(output, QString):
153 # Single filename
154 output = to_text_string(output)
155 else:
156 # List of filenames
157 output = [to_text_string(fname) for fname in output]
159 # Always returns the tuple (output, selectedfilter)
160 return output, selectedfilter
163 def getopenfilename(parent=None, caption='', basedir='', filters='',
164 selectedfilter='', options=None):
165 """Wrapper around QtGui.QFileDialog.getOpenFileName static method
166 Returns a tuple (filename, selectedfilter) -- when dialog box is canceled,
167 returns a tuple of empty strings
168 Compatible with PyQt >=v4.4 (API #1 and #2) and PySide >=v1.0"""
169 return _qfiledialog_wrapper('getOpenFileName', parent=parent,
170 caption=caption, basedir=basedir,
171 filters=filters, selectedfilter=selectedfilter,
172 options=options)
175 def getopenfilenames(parent=None, caption='', basedir='', filters='',
176 selectedfilter='', options=None):
177 """Wrapper around QtGui.QFileDialog.getOpenFileNames static method
178 Returns a tuple (filenames, selectedfilter) -- when dialog box is canceled,
179 returns a tuple (empty list, empty string)
180 Compatible with PyQt >=v4.4 (API #1 and #2) and PySide >=v1.0"""
181 return _qfiledialog_wrapper('getOpenFileNames', parent=parent,
182 caption=caption, basedir=basedir,
183 filters=filters, selectedfilter=selectedfilter,
184 options=options)
187 def getsavefilename(parent=None, caption='', basedir='', filters='',
188 selectedfilter='', options=None):
189 """Wrapper around QtGui.QFileDialog.getSaveFileName static method
190 Returns a tuple (filename, selectedfilter) -- when dialog box is canceled,
191 returns a tuple of empty strings
192 Compatible with PyQt >=v4.4 (API #1 and #2) and PySide >=v1.0"""
193 return _qfiledialog_wrapper('getSaveFileName', parent=parent,
194 caption=caption, basedir=basedir,
195 filters=filters, selectedfilter=selectedfilter,
196 options=options)