2 # Copyright © 2009- The Spyder Development Team
3 # Licensed under the terms of the MIT License
6 Compatibility functions
17 from .QtWidgets
import QFileDialog
23 def is_text_string(obj
):
24 """Return True if `obj` is a text string, False if it is anything else,
26 return isinstance(obj
, str)
29 def to_text_string(obj
, encoding
=None):
30 """Convert `obj` to (unicode) text string"""
33 elif isinstance(obj
, str):
34 # In case this function is not used properly, this could happen
37 return str(obj
, encoding
)
40 # =============================================================================
41 # QVariant conversion utilities
42 # =============================================================================
44 def to_qvariant(obj
=None): # analysis:ignore
45 """Convert Python object to QVariant
46 This is a transitional function from PyQt API#1 (QVariant exist)
47 to PyQt API#2 and Pyside (QVariant does not exist)"""
50 def from_qvariant(qobj
=None, pytype
=None): # analysis:ignore
51 """Convert QVariant object to Python object
52 This is a transitional function from PyQt API #1 (QVariant exist)
53 to PyQt API #2 and Pyside (QVariant does not exist)"""
57 # =============================================================================
58 # Wrappers around QFileDialog static methods
59 # =============================================================================
60 def getexistingdirectory(parent
=None, caption
='', basedir
='',
61 options
=QFileDialog
.ShowDirsOnly
):
62 """Wrapper around QtGui.QFileDialog.getExistingDirectory static method
63 Compatible with PyQt >=v4.4 (API #1 and #2) and PySide >=v1.0"""
64 # Calling QFileDialog static method
65 if sys
.platform
== "win32":
66 # On Windows platforms: redirect standard outputs
67 _temp1
, _temp2
= sys
.stdout
, sys
.stderr
68 sys
.stdout
, sys
.stderr
= None, None
70 result
= QFileDialog
.getExistingDirectory(parent
, caption
, basedir
,
73 if sys
.platform
== "win32":
74 # On Windows platforms: restore standard outputs
75 sys
.stdout
, sys
.stderr
= _temp1
, _temp2
76 if not is_text_string(result
):
78 result
= to_text_string(result
)
82 def _qfiledialog_wrapper(attr
, parent
=None, caption
='', basedir
='',
83 filters
='', selectedfilter
='', options
=None):
85 options
= QFileDialog
.Option(0)
87 func
= getattr(QFileDialog
, attr
)
89 # Calling QFileDialog static method
90 if sys
.platform
== "win32":
91 # On Windows platforms: redirect standard outputs
92 _temp1
, _temp2
= sys
.stdout
, sys
.stderr
93 sys
.stdout
, sys
.stderr
= None, None
94 result
= func(parent
, caption
, basedir
, filters
, selectedfilter
, options
)
95 if sys
.platform
== "win32":
96 # On Windows platforms: restore standard outputs
97 sys
.stdout
, sys
.stderr
= _temp1
, _temp2
99 output
, selectedfilter
= result
101 # Always returns the tuple (output, selectedfilter)
102 return output
, selectedfilter
105 def getopenfilename(parent
=None, caption
='', basedir
='', filters
='',
106 selectedfilter
='', options
=None):
107 """Wrapper around QtGui.QFileDialog.getOpenFileName static method
108 Returns a tuple (filename, selectedfilter) -- when dialog box is canceled,
109 returns a tuple of empty strings
110 Compatible with PyQt >=v4.4 (API #1 and #2) and PySide >=v1.0"""
111 return _qfiledialog_wrapper('getOpenFileName', parent
=parent
,
112 caption
=caption
, basedir
=basedir
,
113 filters
=filters
, selectedfilter
=selectedfilter
,
117 def getopenfilenames(parent
=None, caption
='', basedir
='', filters
='',
118 selectedfilter
='', options
=None):
119 """Wrapper around QtGui.QFileDialog.getOpenFileNames static method
120 Returns a tuple (filenames, selectedfilter) -- when dialog box is canceled,
121 returns a tuple (empty list, empty string)
122 Compatible with PyQt >=v4.4 (API #1 and #2) and PySide >=v1.0"""
123 return _qfiledialog_wrapper('getOpenFileNames', parent
=parent
,
124 caption
=caption
, basedir
=basedir
,
125 filters
=filters
, selectedfilter
=selectedfilter
,
129 def getsavefilename(parent
=None, caption
='', basedir
='', filters
='',
130 selectedfilter
='', options
=None):
131 """Wrapper around QtGui.QFileDialog.getSaveFileName static method
132 Returns a tuple (filename, selectedfilter) -- when dialog box is canceled,
133 returns a tuple of empty strings
134 Compatible with PyQt >=v4.4 (API #1 and #2) and PySide >=v1.0"""
135 return _qfiledialog_wrapper('getSaveFileName', parent
=parent
,
136 caption
=caption
, basedir
=basedir
,
137 filters
=filters
, selectedfilter
=selectedfilter
,
140 # =============================================================================
142 """Wrapper around sip.isdeleted and shiboken.isValid which tests whether
143 an object is currently alive."""
146 return not sip
.isdeleted(object)
147 elif PYSIDE2
or PYSIDE6
:
148 from . import shiboken
149 return shiboken
.isValid(object)