|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
"""QFileDialog patch.""" |
|
3
|
|
|
|
|
4
|
|
|
import sys |
|
5
|
|
|
|
|
6
|
|
|
from .py3compat import to_text_string, is_text_string |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
def patch_filedialog(QFileDialog): |
|
10
|
|
|
"""Patch QFile Dialog.""" |
|
11
|
|
|
|
|
12
|
|
|
def _qfiledialog_wrapper(attr, parent=None, caption='', basedir='', |
|
13
|
|
|
filters='', selectedfilter='', options=None): |
|
14
|
|
|
if options is None: |
|
15
|
|
|
options = QFileDialog.Options(0) |
|
16
|
|
|
try: |
|
17
|
|
|
# PyQt <v4.6 (API #1) |
|
18
|
|
|
from .QtCore import QString |
|
19
|
|
|
except ImportError: |
|
20
|
|
|
# PySide or PyQt >=v4.6 |
|
21
|
|
|
QString = None # analysis:ignore |
|
22
|
|
|
tuple_returned = True |
|
23
|
|
|
try: |
|
24
|
|
|
# PyQt >=v4.6 |
|
25
|
|
|
func = getattr(QFileDialog, attr+'AndFilter') |
|
26
|
|
|
except AttributeError: |
|
27
|
|
|
# PySide or PyQt <v4.6 |
|
28
|
|
|
func = getattr(QFileDialog, attr) |
|
29
|
|
|
if QString is not None: |
|
30
|
|
|
selectedfilter = QString() |
|
31
|
|
|
tuple_returned = False |
|
32
|
|
|
|
|
33
|
|
|
# Calling QFileDialog static method |
|
34
|
|
|
if sys.platform == "win32": |
|
35
|
|
|
# On Windows platforms: redirect standard outputs |
|
36
|
|
|
_temp1, _temp2 = sys.stdout, sys.stderr |
|
37
|
|
|
sys.stdout, sys.stderr = None, None |
|
38
|
|
|
try: |
|
39
|
|
|
result = func(parent, caption, basedir, |
|
40
|
|
|
filters, selectedfilter, options) |
|
41
|
|
|
except TypeError: |
|
42
|
|
|
# The selectedfilter option (`initialFilter` in Qt) has only been |
|
43
|
|
|
# introduced in Jan. 2010 for PyQt v4.7, that's why we handle here |
|
44
|
|
|
# the TypeError exception which will be raised with PyQt v4.6 |
|
45
|
|
|
# (see Issue 960 for more details) |
|
46
|
|
|
result = func(parent, caption, basedir, filters, options) |
|
47
|
|
|
finally: |
|
48
|
|
|
if sys.platform == "win32": |
|
49
|
|
|
# On Windows platforms: restore standard outputs |
|
50
|
|
|
sys.stdout, sys.stderr = _temp1, _temp2 |
|
51
|
|
|
|
|
52
|
|
|
# Processing output |
|
53
|
|
|
if tuple_returned: |
|
54
|
|
|
# PySide or PyQt >=v4.6 |
|
55
|
|
|
output, selectedfilter = result |
|
56
|
|
|
else: |
|
57
|
|
|
# PyQt <v4.6 (API #1) |
|
58
|
|
|
output = result |
|
59
|
|
|
if QString is not None: |
|
60
|
|
|
# PyQt API #1: conversions needed from QString/QStringList |
|
61
|
|
|
selectedfilter = to_text_string(selectedfilter) |
|
62
|
|
|
if isinstance(output, QString): |
|
63
|
|
|
# Single filename |
|
64
|
|
|
output = to_text_string(output) |
|
65
|
|
|
else: |
|
66
|
|
|
# List of filenames |
|
67
|
|
|
output = [to_text_string(fname) for fname in output] |
|
68
|
|
|
|
|
69
|
|
|
# Always returns the tuple (output, selectedfilter) |
|
70
|
|
|
return output, selectedfilter |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
def _getOpenFileName(parent=None, caption='', basedir='', filters='', |
|
74
|
|
|
selectedfilter='', options=None): |
|
75
|
|
|
""" |
|
76
|
|
|
Wrapper around QtGui.QFileDialog.getOpenFileName static method. |
|
77
|
|
|
|
|
78
|
|
|
Returns a tuple (filename, selectedfilter) -- when dialog box is |
|
79
|
|
|
canceled, returns a tuple of empty strings. |
|
80
|
|
|
|
|
81
|
|
|
Compatible with PyQt >=v4.4 (API #1 and #2) and PySide >=v1.0. |
|
82
|
|
|
""" |
|
83
|
|
|
return _qfiledialog_wrapper('getOpenFileName', |
|
84
|
|
|
parent=parent, |
|
85
|
|
|
caption=caption, |
|
86
|
|
|
basedir=basedir, |
|
87
|
|
|
filters=filters, |
|
88
|
|
|
selectedfilter=selectedfilter, |
|
89
|
|
|
options=options) |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
def _getOpenFileNames(parent=None, caption='', basedir='', filters='', |
|
93
|
|
|
selectedfilter='', options=None): |
|
94
|
|
|
""" |
|
95
|
|
|
Wrapper around QtGui.QFileDialog.getOpenFileNames static method. |
|
96
|
|
|
|
|
97
|
|
|
Returns a tuple (filenames, selectedfilter) -- when dialog box is |
|
98
|
|
|
canceled, returns a tuple (empty list, empty string). |
|
99
|
|
|
|
|
100
|
|
|
Compatible with PyQt >=v4.4 (API #1 and #2) and PySide >=v1.0. |
|
101
|
|
|
""" |
|
102
|
|
|
return _qfiledialog_wrapper('getOpenFileNames', |
|
103
|
|
|
parent=parent, |
|
104
|
|
|
caption=caption, |
|
105
|
|
|
basedir=basedir, |
|
106
|
|
|
filters=filters, |
|
107
|
|
|
selectedfilter=selectedfilter, |
|
108
|
|
|
options=options) |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
def _getSaveFileName(parent=None, caption='', basedir='', filters='', |
|
112
|
|
|
selectedfilter='', options=None): |
|
113
|
|
|
""" |
|
114
|
|
|
Wrapper around QtGui.QFileDialog.getSaveFileName static method. |
|
115
|
|
|
|
|
116
|
|
|
Returns a tuple (filename, selectedfilter) -- when dialog box is |
|
117
|
|
|
canceled, returns a tuple of empty strings. |
|
118
|
|
|
|
|
119
|
|
|
Compatible with PyQt >=v4.4 (API #1 and #2) and PySide >=v1.0. |
|
120
|
|
|
""" |
|
121
|
|
|
return _qfiledialog_wrapper('getSaveFileName', |
|
122
|
|
|
parent=parent, |
|
123
|
|
|
caption=caption, |
|
124
|
|
|
basedir=basedir, |
|
125
|
|
|
filters=filters, |
|
126
|
|
|
selectedfilter=selectedfilter, |
|
127
|
|
|
options=options) |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
def _getExistingDirectory(parent=None, caption='', basedir='', |
|
131
|
|
|
options=QFileDialog.ShowDirsOnly): |
|
132
|
|
|
""" |
|
133
|
|
|
Wrapper around QtGui.QFileDialog.getExistingDirectory static method. |
|
134
|
|
|
|
|
135
|
|
|
Compatible with PyQt >=v4.4 (API #1 and #2) and PySide >=v1.0. |
|
136
|
|
|
""" |
|
137
|
|
|
# Calling QFileDialog static method |
|
138
|
|
|
if sys.platform == "win32": |
|
139
|
|
|
# On Windows platforms: redirect standard outputs |
|
140
|
|
|
_temp1, _temp2 = sys.stdout, sys.stderr |
|
141
|
|
|
sys.stdout, sys.stderr = None, None |
|
142
|
|
|
try: |
|
143
|
|
|
result = QFileDialog.getExistingDirectory(parent, |
|
144
|
|
|
caption, |
|
145
|
|
|
basedir, |
|
146
|
|
|
options) |
|
147
|
|
|
finally: |
|
148
|
|
|
if sys.platform == "win32": |
|
149
|
|
|
# On Windows platforms: restore standard outputs |
|
150
|
|
|
sys.stdout, sys.stderr = _temp1, _temp2 |
|
151
|
|
|
if not is_text_string(result): |
|
152
|
|
|
# PyQt API #1 |
|
153
|
|
|
result = to_text_string(result) |
|
154
|
|
|
return result |
|
155
|
|
|
|
|
156
|
|
|
QFileDialog.getOpenFileName2 = _getOpenFileName |
|
157
|
|
|
QFileDialog.getOpenFileNames2 = _getOpenFileNames |
|
158
|
|
|
QFileDialog.getSaveFileName2 = _getSaveFileName |
|
159
|
|
|
QFileDialog.getExistingDirectory2 = _getExistingDirectory |
|
160
|
|
|
|