src/eric7/EricWidgets/EricFileDialog.py

branch
eric7
changeset 9239
3c605ab5a8c7
parent 9221
bf71ee032bb4
child 9413
80c06d472826
equal deleted inserted replaced
9238:a7cbf3d61498 9239:3c605ab5a8c7
4 # 4 #
5 5
6 """ 6 """
7 Module implementing alternative functions for the QFileDialog static methods. 7 Module implementing alternative functions for the QFileDialog static methods.
8 """ 8 """
9
10 import pathlib
9 11
10 from PyQt6.QtWidgets import QFileDialog 12 from PyQt6.QtWidgets import QFileDialog
11 13
12 import Globals 14 import Globals
13 15
42 return ";;".join(fileFilters) 44 return ";;".join(fileFilters)
43 else: 45 else:
44 return filterStr 46 return filterStr
45 47
46 48
49 ###########################################################################
50 ## String based interface
51 ###########################################################################
52
53
47 def getOpenFileName(parent=None, caption="", directory="", filterStr="", options=None): 54 def getOpenFileName(parent=None, caption="", directory="", filterStr="", options=None):
48 """ 55 """
49 Module function to get the name of a file for opening it. 56 Module function to get the name of a file for opening it.
50 57
51 @param parent parent widget of the dialog 58 @param parent parent widget of the dialog
178 ) 185 )
179 186
180 187
181 def getSaveFileName(parent=None, caption="", directory="", filterStr="", options=None): 188 def getSaveFileName(parent=None, caption="", directory="", filterStr="", options=None):
182 """ 189 """
183 Module function to get the name of a file for saving it. 190 Module function to get the name of a file for saving.
184 191
185 @param parent parent widget of the dialog 192 @param parent parent widget of the dialog
186 @type QWidget 193 @type QWidget
187 @param caption window title of the dialog 194 @param caption window title of the dialog
188 @type str 195 @type str
204 211
205 def getSaveFileNameAndFilter( 212 def getSaveFileNameAndFilter(
206 parent=None, caption="", directory="", filterStr="", initialFilter="", options=None 213 parent=None, caption="", directory="", filterStr="", initialFilter="", options=None
207 ): 214 ):
208 """ 215 """
209 Module function to get the name of a file for saving it and the selected 216 Module function to get the name of a file for saving and the selected file name
210 file name filter. 217 filter.
211 218
212 @param parent parent widget of the dialog 219 @param parent parent widget of the dialog
213 @type QWidget 220 @type QWidget
214 @param caption window title of the dialog 221 @param caption window title of the dialog
215 @type str 222 @type str
250 @rtype str 257 @rtype str
251 """ 258 """
252 if options is None: 259 if options is None:
253 options = QFileDialog.Option(0) 260 options = QFileDialog.Option(0)
254 return QFileDialog.getExistingDirectory(parent, caption, directory, options) 261 return QFileDialog.getExistingDirectory(parent, caption, directory, options)
262
263
264 ###########################################################################
265 ## pathlib.Path based interface
266 ###########################################################################
267
268
269 def getOpenFilePath(parent=None, caption="", directory="", filterStr="", options=None):
270 """
271 Module function to get the path of a file for opening it.
272
273 @param parent parent widget of the dialog
274 @type QWidget
275 @param caption window title of the dialog
276 @type str
277 @param directory working directory of the dialog
278 @type str or pathlib.Path
279 @param filterStr filter string for the dialog
280 @type str
281 @param options various options for the dialog
282 @type QFileDialog.Options
283 @return path of file to be opened
284 @rtype pathlib.Path
285 """
286 if options is None:
287 options = QFileDialog.Option(0)
288 filename = QFileDialog.getOpenFileName(
289 parent, caption, str(directory), filterStr, "", options
290 )[0]
291 return pathlib.Path(filename)
292
293
294 def getOpenFilePathAndFilter(
295 parent=None, caption="", directory="", filterStr="", initialFilter="", options=None
296 ):
297 """
298 Module function to get the path of a file for opening it and the selected
299 file name filter.
300
301 @param parent parent widget of the dialog
302 @type QWidget
303 @param caption window title of the dialog
304 @type str
305 @param directory working directory of the dialog
306 @type str or pathlib.Path
307 @param filterStr filter string for the dialog
308 @type str
309 @param initialFilter initial filter for the dialog
310 @type str
311 @param options various options for the dialog
312 @type QFileDialog.Options
313 @return path of file to be opened and selected filter
314 @rtype tuple of (pathlib.Path, str)
315 """
316 if options is None:
317 options = QFileDialog.Option(0)
318 newfilter = __reorderFilter(filterStr, initialFilter)
319 filename, selectedFilter = QFileDialog.getOpenFileName(
320 parent, caption, str(directory), newfilter, initialFilter, options
321 )
322 return pathlib.Path(filename), selectedFilter
323
324
325 def getOpenFilePaths(parent=None, caption="", directory="", filterStr="", options=None):
326 """
327 Module function to get a list of paths of files for opening.
328
329 @param parent parent widget of the dialog
330 @type QWidget
331 @param caption window title of the dialog
332 @type str
333 @param directory working directory of the dialog
334 @type str or pathlib.Path
335 @param filterStr filter string for the dialog
336 @type str
337 @param options various options for the dialog
338 @type QFileDialog.Options
339 @return list of file paths to be opened
340 @rtype list of pathlib.Path
341 """
342 if options is None:
343 options = QFileDialog.Option(0)
344 filenames = QFileDialog.getOpenFileNames(
345 parent, caption, str(directory), filterStr, "", options
346 )[0]
347 return [pathlib.Path(f) for f in filenames]
348
349
350 def getOpenFilPathsAndFilter(
351 parent=None, caption="", directory="", filterStr="", initialFilter="", options=None
352 ):
353 """
354 Module function to get a list of paths of files for opening and the
355 selected file name filter.
356
357 @param parent parent widget of the dialog
358 @type QWidget
359 @param caption window title of the dialog
360 @type str
361 @param directory working directory of the dialog
362 @type str or pathlib.Path
363 @param filterStr filter string for the dialog
364 @type str
365 @param initialFilter initial filter for the dialog
366 @type str
367 @param options various options for the dialog
368 @type QFileDialog.Options
369 @return list of file paths to be opened and selected filter
370 @rtype tuple of (list of pathlib.Path, str)
371 """
372 if options is None:
373 options = QFileDialog.Option(0)
374 newfilter = __reorderFilter(filterStr, initialFilter)
375 filenames, selectedFilter = QFileDialog.getOpenFileNames(
376 parent, caption, str(directory), newfilter, initialFilter, options
377 )
378 return [pathlib.Path(f) for f in filenames], selectedFilter
379
380
381 def getOpenFileAndDirPaths(
382 parent=None, caption="", directory="", filterStr="", options=None
383 ):
384 """
385 Module function to get the paths of files and directories for opening.
386
387 @param parent parent widget of the dialog
388 @type QWidget
389 @param caption window title of the dialog
390 @type str
391 @param directory working directory of the dialog
392 @type str or pathlib.Path
393 @param filterStr filter string for the dialog
394 @type str
395 @param options various options for the dialog
396 @type QFileDialog.Options
397 @return paths of the selected files and folders
398 @rtype list of pathlib.Path
399 """
400 from .EricDirFileDialog import EricDirFileDialog
401
402 return EricDirFileDialog.getOpenFileAndDirPaths(
403 parent, caption, directory, filterStr, options
404 )
405
406
407 def getSaveFilePath(parent=None, caption="", directory="", filterStr="", options=None):
408 """
409 Module function to get the path of a file for saving.
410
411 @param parent parent widget of the dialog
412 @type QWidget
413 @param caption window title of the dialog
414 @type str
415 @param directory working directory of the dialog
416 @type str or pathlib.Path
417 @param filterStr filter string for the dialog
418 @type str
419 @param options various options for the dialog
420 @type QFileDialog.Options
421 @return path of file to be saved
422 @rtype pathlib.Path
423 """
424 if options is None:
425 options = QFileDialog.Option(0)
426 filename = QFileDialog.getSaveFileName(
427 parent, caption, str(directory), filterStr, "", options
428 )[0]
429 return pathlib.Path(filename)
430
431
432 def getSaveFilePathAndFilter(
433 parent=None, caption="", directory="", filterStr="", initialFilter="", options=None
434 ):
435 """
436 Module function to get the path of a file for saving and the selected
437 file name filter.
438
439 @param parent parent widget of the dialog
440 @type QWidget
441 @param caption window title of the dialog
442 @type str
443 @param directory working directory of the dialog
444 @type str or pathlib.Path
445 @param filterStr filter string for the dialog
446 @type str
447 @param initialFilter initial filter for the dialog
448 @type str
449 @param options various options for the dialog
450 @type QFileDialog.Options
451 @return path of file to be saved and selected filte
452 @rtype tuple of (pathlib.Path, str)
453 """
454 if options is None:
455 options = QFileDialog.Option(0)
456 newfilter = __reorderFilter(filterStr, initialFilter)
457 filename, selectedFilter = QFileDialog.getSaveFileName(
458 parent, caption, directory, newfilter, initialFilter, options
459 )
460 return pathlib.Path(filename), selectedFilter
461
462
463 def getExistingDirectoryPath(
464 parent=None, caption="", directory="", options=QFileDialog.Option.ShowDirsOnly
465 ):
466 """
467 Module function to get the path of a directory.
468
469 @param parent parent widget of the dialog
470 @type QWidget
471 @param caption window title of the dialog
472 @type str
473 @param directory working directory of the dialog
474 @type str or pathlib.Path
475 @param options various options for the dialog
476 @type QFileDialog.Options
477 @return path of selected directory
478 @rtype pathlib.Path
479 """
480 if options is None:
481 options = QFileDialog.Option(0)
482 dirname = QFileDialog.getExistingDirectory(parent, caption, str(directory), options)
483 return pathlib.Path(dirname)

eric ide

mercurial