1 # -*- coding: utf-8 -*-
3 # Copyright © 2009- The Spyder Development Team
4 # Licensed under the terms of the MIT License
7 Compatibility functions
10 from __future__
import print_function
15 from .QtWidgets
import QFileDialog
16 from .py3compat
import is_text_string
, to_text_string
, TEXT_TYPES
19 # =============================================================================
20 # QVariant conversion utilities
21 # =============================================================================
26 PYQT_API_1
= sip
.getapi('QVariant') == 1 # PyQt API #1
27 except AttributeError:
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)"""
37 from PyQt4
.QtCore
import QVariant
38 return QVariant(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)"""
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:
55 return qobj
.toInt()[0]
56 elif convfunc
is float:
57 return qobj
.toDouble()[0]
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)"""
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)"""
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
90 result
= QFileDialog
.getExistingDirectory(parent
, caption
, basedir
,
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
):
98 result
= to_text_string(result
)
102 def _qfiledialog_wrapper(attr
, parent
=None, caption
='', basedir
='',
103 filters
='', selectedfilter
='', options
=None):
105 options
= QFileDialog
.Options(0)
107 # PyQt <v4.6 (API #1)
108 from .QtCore
import QString
110 # PySide or PyQt >=v4.6
111 QString
= None # analysis:ignore
112 tuple_returned
= True
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
129 result
= func(parent
, caption
, basedir
,
130 filters
, selectedfilter
, options
)
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
)
138 if sys
.platform
== "win32":
139 # On Windows platforms: restore standard outputs
140 sys
.stdout
, sys
.stderr
= _temp1
, _temp2
144 # PySide or PyQt >=v4.6
145 output
, selectedfilter
= result
147 # PyQt <v4.6 (API #1)
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
):
154 output
= to_text_string(output
)
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
,
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
,
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
,