|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing alternative functions for the QFileDialog static methods. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtWidgets import QFileDialog |
|
11 |
|
12 import Globals |
|
13 |
|
14 Option = QFileDialog.Option |
|
15 |
|
16 ShowDirsOnly = QFileDialog.Option.ShowDirsOnly |
|
17 DontResolveSymlinks = QFileDialog.Option.DontResolveSymlinks |
|
18 DontConfirmOverwrite = QFileDialog.Option.DontConfirmOverwrite |
|
19 DontUseNativeDialog = QFileDialog.Option.DontUseNativeDialog |
|
20 ReadOnly = QFileDialog.Option.ReadOnly |
|
21 HideNameFilterDetails = QFileDialog.Option.HideNameFilterDetails |
|
22 DontUseCustomDirectoryIcons = QFileDialog.Option.DontUseCustomDirectoryIcons |
|
23 |
|
24 |
|
25 def __reorderFilter(filterStr, initialFilter=""): |
|
26 """ |
|
27 Private function to reorder the file filter to cope with a KDE issue |
|
28 introduced by distributor's usage of KDE file dialogs. |
|
29 |
|
30 @param filterStr Qt file filter |
|
31 @type str |
|
32 @param initialFilter initial filter |
|
33 @type str |
|
34 @return the rearranged Qt file filter |
|
35 @rtype str |
|
36 """ |
|
37 if initialFilter and not Globals.isMacPlatform(): |
|
38 fileFilters = filterStr.split(';;') |
|
39 if len(fileFilters) < 10 and initialFilter in fileFilters: |
|
40 fileFilters.remove(initialFilter) |
|
41 fileFilters.insert(0, initialFilter) |
|
42 return ";;".join(fileFilters) |
|
43 else: |
|
44 return filterStr |
|
45 |
|
46 |
|
47 def getOpenFileName(parent=None, caption="", directory="", |
|
48 filterStr="", options=None): |
|
49 """ |
|
50 Module function to get the name of a file for opening it. |
|
51 |
|
52 @param parent parent widget of the dialog |
|
53 @type QWidget |
|
54 @param caption window title of the dialog |
|
55 @type str |
|
56 @param directory working directory of the dialog |
|
57 @type str |
|
58 @param filterStr filter string for the dialog |
|
59 @type str |
|
60 @param options various options for the dialog |
|
61 @type QFileDialog.Options |
|
62 @return name of file to be opened |
|
63 @rtype str |
|
64 """ |
|
65 if options is None: |
|
66 options = QFileDialog.Option(0) |
|
67 return QFileDialog.getOpenFileName( |
|
68 parent, caption, directory, filterStr, "", options)[0] |
|
69 |
|
70 |
|
71 def getOpenFileNameAndFilter(parent=None, caption="", directory="", |
|
72 filterStr="", initialFilter="", |
|
73 options=None): |
|
74 """ |
|
75 Module function to get the name of a file for opening it and the selected |
|
76 file name filter. |
|
77 |
|
78 @param parent parent widget of the dialog |
|
79 @type QWidget |
|
80 @param caption window title of the dialog |
|
81 @type str |
|
82 @param directory working directory of the dialog |
|
83 @type str |
|
84 @param filterStr filter string for the dialog |
|
85 @type str |
|
86 @param initialFilter initial filter for the dialog |
|
87 @type str |
|
88 @param options various options for the dialog |
|
89 @type QFileDialog.Options |
|
90 @return name of file to be opened and selected filter |
|
91 @rtype tuple of (str, str) |
|
92 """ |
|
93 if options is None: |
|
94 options = QFileDialog.Option(0) |
|
95 newfilter = __reorderFilter(filterStr, initialFilter) |
|
96 return QFileDialog.getOpenFileName( |
|
97 parent, caption, directory, newfilter, initialFilter, options) |
|
98 |
|
99 |
|
100 def getOpenFileNames(parent=None, caption="", directory="", |
|
101 filterStr="", options=None): |
|
102 """ |
|
103 Module function to get a list of names of files for opening. |
|
104 |
|
105 @param parent parent widget of the dialog |
|
106 @type QWidget |
|
107 @param caption window title of the dialog |
|
108 @type str |
|
109 @param directory working directory of the dialog |
|
110 @type str |
|
111 @param filterStr filter string for the dialog |
|
112 @type str |
|
113 @param options various options for the dialog |
|
114 @type QFileDialog.Options |
|
115 @return list of file names to be opened |
|
116 @rtype list of str |
|
117 """ |
|
118 if options is None: |
|
119 options = QFileDialog.Option(0) |
|
120 return QFileDialog.getOpenFileNames( |
|
121 parent, caption, directory, filterStr, "", options)[0] |
|
122 |
|
123 |
|
124 def getOpenFileNamesAndFilter(parent=None, caption="", directory="", |
|
125 filterStr="", initialFilter="", |
|
126 options=None): |
|
127 """ |
|
128 Module function to get a list of names of files for opening and the |
|
129 selected file name filter. |
|
130 |
|
131 @param parent parent widget of the dialog |
|
132 @type QWidget |
|
133 @param caption window title of the dialog |
|
134 @type str |
|
135 @param directory working directory of the dialog |
|
136 @type str |
|
137 @param filterStr filter string for the dialog |
|
138 @type str |
|
139 @param initialFilter initial filter for the dialog |
|
140 @type str |
|
141 @param options various options for the dialog |
|
142 @type QFileDialog.Options |
|
143 @return list of file names to be opened and selected filter |
|
144 @rtype tuple of (list of str, str) |
|
145 """ |
|
146 if options is None: |
|
147 options = QFileDialog.Option(0) |
|
148 newfilter = __reorderFilter(filterStr, initialFilter) |
|
149 return QFileDialog.getOpenFileNames( |
|
150 parent, caption, directory, newfilter, initialFilter, options) |
|
151 |
|
152 |
|
153 def getOpenFileAndDirNames(parent=None, caption="", directory="", |
|
154 filterStr="", options=None): |
|
155 """ |
|
156 Module function to get the names of files and directories for opening. |
|
157 |
|
158 @param parent parent widget of the dialog |
|
159 @type QWidget |
|
160 @param caption window title of the dialog |
|
161 @type str |
|
162 @param directory working directory of the dialog |
|
163 @type str |
|
164 @param filterStr filter string for the dialog |
|
165 @type str |
|
166 @param options various options for the dialog |
|
167 @type QFileDialog.Options |
|
168 @return names of the selected files and folders |
|
169 @rtype list of str |
|
170 """ |
|
171 from .EricDirFileDialog import EricDirFileDialog |
|
172 return EricDirFileDialog.getOpenFileAndDirNames( |
|
173 parent, caption, directory, filterStr, options |
|
174 ) |
|
175 |
|
176 |
|
177 def getSaveFileName(parent=None, caption="", directory="", |
|
178 filterStr="", options=None): |
|
179 """ |
|
180 Module function to get the name of a file for saving it. |
|
181 |
|
182 @param parent parent widget of the dialog |
|
183 @type QWidget |
|
184 @param caption window title of the dialog |
|
185 @type str |
|
186 @param directory working directory of the dialog |
|
187 @type str |
|
188 @param filterStr filter string for the dialog |
|
189 @type str |
|
190 @param options various options for the dialog |
|
191 @type QFileDialog.Options |
|
192 @return name of file to be saved |
|
193 @rtype str |
|
194 """ |
|
195 if options is None: |
|
196 options = QFileDialog.Option(0) |
|
197 return QFileDialog.getSaveFileName( |
|
198 parent, caption, directory, filterStr, "", options)[0] |
|
199 |
|
200 |
|
201 def getSaveFileNameAndFilter(parent=None, caption="", directory="", |
|
202 filterStr="", initialFilter="", |
|
203 options=None): |
|
204 """ |
|
205 Module function to get the name of a file for saving it and the selected |
|
206 file name filter. |
|
207 |
|
208 @param parent parent widget of the dialog |
|
209 @type QWidget |
|
210 @param caption window title of the dialog |
|
211 @type str |
|
212 @param directory working directory of the dialog |
|
213 @type str |
|
214 @param filterStr filter string for the dialog |
|
215 @type str |
|
216 @param initialFilter initial filter for the dialog |
|
217 @type str |
|
218 @param options various options for the dialog |
|
219 @type QFileDialog.Options |
|
220 @return name of file to be saved and selected filte |
|
221 @rtype tuple of (str, str) |
|
222 """ |
|
223 if options is None: |
|
224 options = QFileDialog.Option(0) |
|
225 newfilter = __reorderFilter(filterStr, initialFilter) |
|
226 return QFileDialog.getSaveFileName( |
|
227 parent, caption, directory, newfilter, initialFilter, options) |
|
228 |
|
229 |
|
230 def getExistingDirectory(parent=None, caption="", |
|
231 directory="", |
|
232 options=QFileDialog.Option.ShowDirsOnly): |
|
233 """ |
|
234 Module function to get the name of a directory. |
|
235 |
|
236 @param parent parent widget of the dialog |
|
237 @type QWidget |
|
238 @param caption window title of the dialog |
|
239 @type str |
|
240 @param directory working directory of the dialog |
|
241 @type str |
|
242 @param options various options for the dialog |
|
243 @type QFileDialog.Options |
|
244 @return name of selected directory |
|
245 @rtype str |
|
246 """ |
|
247 if options is None: |
|
248 options = QFileDialog.Option(0) |
|
249 return QFileDialog.getExistingDirectory(parent, caption, directory, |
|
250 options) |