hotkeys: make Cmd-M minimize the main window
[git-cola.git] / qtpy / compat.py
blob4c6d428f9af314ca11b85969dcc71b073010f359
2 # Copyright © 2009- The Spyder Development Team
3 # Licensed under the terms of the MIT License
5 """
6 Compatibility functions
7 """
8 import sys
10 from . import (
11 PYQT5,
12 PYQT6,
13 PYSIDE2,
14 PYSIDE6,
16 from .QtWidgets import QFileDialog
18 TEXT_TYPES = (str,)
21 def is_text_string(obj):
22 """Return True if `obj` is a text string, False if it is anything else,
23 like binary data."""
24 return isinstance(obj, str)
27 def to_text_string(obj, encoding=None):
28 """Convert `obj` to (unicode) text string"""
29 if encoding is None:
30 return str(obj)
31 if isinstance(obj, str):
32 # In case this function is not used properly, this could happen
33 return obj
35 return str(obj, encoding)
38 # =============================================================================
39 # QVariant conversion utilities
40 # =============================================================================
41 PYQT_API_1 = False
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)"""
48 return obj
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)"""
55 return qobj
58 # =============================================================================
59 # Wrappers around QFileDialog static methods
60 # =============================================================================
61 def getexistingdirectory(
62 parent=None,
63 caption="",
64 basedir="",
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
74 try:
75 result = QFileDialog.getExistingDirectory(
76 parent,
77 caption,
78 basedir,
79 options,
81 finally:
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):
86 # PyQt API #1
87 result = to_text_string(result)
88 return result
91 def _qfiledialog_wrapper(
92 attr,
93 parent=None,
94 caption="",
95 basedir="",
96 filters="",
97 selectedfilter="",
98 options=None,
100 if options is None:
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
121 def getopenfilename(
122 parent=None,
123 caption="",
124 basedir="",
125 filters="",
126 selectedfilter="",
127 options=None,
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(
134 "getOpenFileName",
135 parent=parent,
136 caption=caption,
137 basedir=basedir,
138 filters=filters,
139 selectedfilter=selectedfilter,
140 options=options,
144 def getopenfilenames(
145 parent=None,
146 caption="",
147 basedir="",
148 filters="",
149 selectedfilter="",
150 options=None,
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(
157 "getOpenFileNames",
158 parent=parent,
159 caption=caption,
160 basedir=basedir,
161 filters=filters,
162 selectedfilter=selectedfilter,
163 options=options,
167 def getsavefilename(
168 parent=None,
169 caption="",
170 basedir="",
171 filters="",
172 selectedfilter="",
173 options=None,
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(
180 "getSaveFileName",
181 parent=parent,
182 caption=caption,
183 basedir=basedir,
184 filters=filters,
185 selectedfilter=selectedfilter,
186 options=options,
190 # =============================================================================
191 def isalive(obj):
192 """Wrapper around sip.isdeleted and shiboken.isValid which tests whether
193 an object is currently alive."""
194 if PYQT5 or PYQT6:
195 from . import sip
197 return not sip.isdeleted(obj)
198 if PYSIDE2 or PYSIDE6:
199 from . import shiboken
201 return shiboken.isValid(obj)
202 return None