dag: add ability to checkout branches from the DAG viewer
[git-cola.git] / qtpy / compat.py
blob325c0ddb20aee54bedc93cf0efdb43bd89d3c5ca
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,
17 from .QtWidgets import QFileDialog
20 TEXT_TYPES = (str,)
23 def is_text_string(obj):
24 """Return True if `obj` is a text string, False if it is anything else,
25 like binary data."""
26 return isinstance(obj, str)
29 def to_text_string(obj, encoding=None):
30 """Convert `obj` to (unicode) text string"""
31 if encoding is None:
32 return str(obj)
33 elif isinstance(obj, str):
34 # In case this function is not used properly, this could happen
35 return obj
36 else:
37 return str(obj, encoding)
40 # =============================================================================
41 # QVariant conversion utilities
42 # =============================================================================
43 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
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)"""
54 return qobj
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
69 try:
70 result = QFileDialog.getExistingDirectory(parent, caption, basedir,
71 options)
72 finally:
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):
77 # PyQt API #1
78 result = to_text_string(result)
79 return result
82 def _qfiledialog_wrapper(attr, parent=None, caption='', basedir='',
83 filters='', selectedfilter='', options=None):
84 if options is 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,
114 options=options)
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,
126 options=options)
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,
138 options=options)
140 # =============================================================================
141 def isalive(object):
142 """Wrapper around sip.isdeleted and shiboken.isValid which tests whether
143 an object is currently alive."""
144 if PYQT5 or PYQT6:
145 from . import sip
146 return not sip.isdeleted(object)
147 elif PYSIDE2 or PYSIDE6:
148 from . import shiboken
149 return shiboken.isValid(object)