2 # Copyright © 2009- The Spyder Development Team
3 # Licensed under the terms of the MIT License
6 Compatibility functions
16 from .QtWidgets
import QFileDialog
21 def is_text_string(obj
):
22 """Return True if `obj` is a text string, False if it is anything else,
24 return isinstance(obj
, str)
27 def to_text_string(obj
, encoding
=None):
28 """Convert `obj` to (unicode) text string"""
31 if isinstance(obj
, str):
32 # In case this function is not used properly, this could happen
35 return str(obj
, encoding
)
38 # =============================================================================
39 # QVariant conversion utilities
40 # =============================================================================
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)"""
51 def from_qvariant(qobj
=None, pytype
=None): # analysis:ignore
52 """Convert QVariant object to Python object
53 This is a transitional function from PyQt API #1 (QVariant exist)
54 to PyQt API #2 and Pyside (QVariant does not exist)"""
58 # =============================================================================
59 # Wrappers around QFileDialog static methods
60 # =============================================================================
61 def getexistingdirectory(
65 options
=QFileDialog
.ShowDirsOnly
,
67 """Wrapper around QtGui.QFileDialog.getExistingDirectory static method
68 Compatible with PyQt >=v4.4 (API #1 and #2) and PySide >=v1.0"""
69 # Calling QFileDialog static method
70 if sys
.platform
== "win32":
71 # On Windows platforms: redirect standard outputs
72 _temp1
, _temp2
= sys
.stdout
, sys
.stderr
73 sys
.stdout
, sys
.stderr
= None, None
75 result
= QFileDialog
.getExistingDirectory(
82 if sys
.platform
== "win32":
83 # On Windows platforms: restore standard outputs
84 sys
.stdout
, sys
.stderr
= _temp1
, _temp2
85 if not is_text_string(result
):
87 result
= to_text_string(result
)
91 def _qfiledialog_wrapper(
101 options
= QFileDialog
.Option(0)
103 func
= getattr(QFileDialog
, attr
)
105 # Calling QFileDialog static method
106 if sys
.platform
== "win32":
107 # On Windows platforms: redirect standard outputs
108 _temp1
, _temp2
= sys
.stdout
, sys
.stderr
109 sys
.stdout
, sys
.stderr
= None, None
110 result
= func(parent
, caption
, basedir
, filters
, selectedfilter
, options
)
111 if sys
.platform
== "win32":
112 # On Windows platforms: restore standard outputs
113 sys
.stdout
, sys
.stderr
= _temp1
, _temp2
115 output
, selectedfilter
= result
117 # Always returns the tuple (output, selectedfilter)
118 return output
, selectedfilter
129 """Wrapper around QtGui.QFileDialog.getOpenFileName static method
130 Returns a tuple (filename, selectedfilter) -- when dialog box is canceled,
131 returns a tuple of empty strings
132 Compatible with PyQt >=v4.4 (API #1 and #2) and PySide >=v1.0"""
133 return _qfiledialog_wrapper(
139 selectedfilter
=selectedfilter
,
144 def getopenfilenames(
152 """Wrapper around QtGui.QFileDialog.getOpenFileNames static method
153 Returns a tuple (filenames, selectedfilter) -- when dialog box is canceled,
154 returns a tuple (empty list, empty string)
155 Compatible with PyQt >=v4.4 (API #1 and #2) and PySide >=v1.0"""
156 return _qfiledialog_wrapper(
162 selectedfilter
=selectedfilter
,
175 """Wrapper around QtGui.QFileDialog.getSaveFileName static method
176 Returns a tuple (filename, selectedfilter) -- when dialog box is canceled,
177 returns a tuple of empty strings
178 Compatible with PyQt >=v4.4 (API #1 and #2) and PySide >=v1.0"""
179 return _qfiledialog_wrapper(
185 selectedfilter
=selectedfilter
,
190 # =============================================================================
192 """Wrapper around sip.isdeleted and shiboken.isValid which tests whether
193 an object is currently alive."""
197 return not sip
.isdeleted(obj
)
198 if PYSIDE2
or PYSIDE6
:
199 from . import shiboken
201 return shiboken
.isValid(obj
)