src/eric7/EricWidgets/EricFileDialog.py

branch
eric7
changeset 9239
3c605ab5a8c7
parent 9221
bf71ee032bb4
child 9413
80c06d472826
--- a/src/eric7/EricWidgets/EricFileDialog.py	Sat Jul 16 18:14:30 2022 +0200
+++ b/src/eric7/EricWidgets/EricFileDialog.py	Sun Jul 17 12:21:39 2022 +0200
@@ -7,6 +7,8 @@
 Module implementing alternative functions for the QFileDialog static methods.
 """
 
+import pathlib
+
 from PyQt6.QtWidgets import QFileDialog
 
 import Globals
@@ -44,6 +46,11 @@
         return filterStr
 
 
+###########################################################################
+## String based interface
+###########################################################################
+
+
 def getOpenFileName(parent=None, caption="", directory="", filterStr="", options=None):
     """
     Module function to get the name of a file for opening it.
@@ -180,7 +187,7 @@
 
 def getSaveFileName(parent=None, caption="", directory="", filterStr="", options=None):
     """
-    Module function to get the name of a file for saving it.
+    Module function to get the name of a file for saving.
 
     @param parent parent widget of the dialog
     @type QWidget
@@ -206,8 +213,8 @@
     parent=None, caption="", directory="", filterStr="", initialFilter="", options=None
 ):
     """
-    Module function to get the name of a file for saving it and the selected
-    file name filter.
+    Module function to get the name of a file for saving and the selected file name
+    filter.
 
     @param parent parent widget of the dialog
     @type QWidget
@@ -252,3 +259,225 @@
     if options is None:
         options = QFileDialog.Option(0)
     return QFileDialog.getExistingDirectory(parent, caption, directory, options)
+
+
+###########################################################################
+## pathlib.Path based interface
+###########################################################################
+
+
+def getOpenFilePath(parent=None, caption="", directory="", filterStr="", options=None):
+    """
+    Module function to get the path of a file for opening it.
+
+    @param parent parent widget of the dialog
+    @type QWidget
+    @param caption window title of the dialog
+    @type str
+    @param directory working directory of the dialog
+    @type str or pathlib.Path
+    @param filterStr filter string for the dialog
+    @type str
+    @param options various options for the dialog
+    @type QFileDialog.Options
+    @return path of file to be opened
+    @rtype pathlib.Path
+    """
+    if options is None:
+        options = QFileDialog.Option(0)
+    filename = QFileDialog.getOpenFileName(
+        parent, caption, str(directory), filterStr, "", options
+    )[0]
+    return pathlib.Path(filename)
+
+
+def getOpenFilePathAndFilter(
+    parent=None, caption="", directory="", filterStr="", initialFilter="", options=None
+):
+    """
+    Module function to get the path of a file for opening it and the selected
+    file name filter.
+
+    @param parent parent widget of the dialog
+    @type QWidget
+    @param caption window title of the dialog
+    @type str
+    @param directory working directory of the dialog
+    @type str or pathlib.Path
+    @param filterStr filter string for the dialog
+    @type str
+    @param initialFilter initial filter for the dialog
+    @type str
+    @param options various options for the dialog
+    @type QFileDialog.Options
+    @return path of file to be opened and selected filter
+    @rtype tuple of (pathlib.Path, str)
+    """
+    if options is None:
+        options = QFileDialog.Option(0)
+    newfilter = __reorderFilter(filterStr, initialFilter)
+    filename, selectedFilter = QFileDialog.getOpenFileName(
+        parent, caption, str(directory), newfilter, initialFilter, options
+    )
+    return pathlib.Path(filename), selectedFilter
+
+
+def getOpenFilePaths(parent=None, caption="", directory="", filterStr="", options=None):
+    """
+    Module function to get a list of paths of files for opening.
+
+    @param parent parent widget of the dialog
+    @type QWidget
+    @param caption window title of the dialog
+    @type str
+    @param directory working directory of the dialog
+    @type str or pathlib.Path
+    @param filterStr filter string for the dialog
+    @type str
+    @param options various options for the dialog
+    @type QFileDialog.Options
+    @return list of file paths to be opened
+    @rtype list of pathlib.Path
+    """
+    if options is None:
+        options = QFileDialog.Option(0)
+    filenames = QFileDialog.getOpenFileNames(
+        parent, caption, str(directory), filterStr, "", options
+    )[0]
+    return [pathlib.Path(f) for f in filenames]
+
+
+def getOpenFilPathsAndFilter(
+    parent=None, caption="", directory="", filterStr="", initialFilter="", options=None
+):
+    """
+    Module function to get a list of paths of files for opening and the
+    selected file name filter.
+
+    @param parent parent widget of the dialog
+    @type QWidget
+    @param caption window title of the dialog
+    @type str
+    @param directory working directory of the dialog
+    @type str or pathlib.Path
+    @param filterStr filter string for the dialog
+    @type str
+    @param initialFilter initial filter for the dialog
+    @type str
+    @param options various options for the dialog
+    @type QFileDialog.Options
+    @return list of file paths to be opened and selected filter
+    @rtype tuple of (list of pathlib.Path, str)
+    """
+    if options is None:
+        options = QFileDialog.Option(0)
+    newfilter = __reorderFilter(filterStr, initialFilter)
+    filenames, selectedFilter = QFileDialog.getOpenFileNames(
+        parent, caption, str(directory), newfilter, initialFilter, options
+    )
+    return [pathlib.Path(f) for f in filenames], selectedFilter
+
+
+def getOpenFileAndDirPaths(
+    parent=None, caption="", directory="", filterStr="", options=None
+):
+    """
+    Module function to get the paths of files and directories for opening.
+
+    @param parent parent widget of the dialog
+    @type QWidget
+    @param caption window title of the dialog
+    @type str
+    @param directory working directory of the dialog
+    @type str or pathlib.Path
+    @param filterStr filter string for the dialog
+    @type str
+    @param options various options for the dialog
+    @type QFileDialog.Options
+    @return paths of the selected files and folders
+    @rtype list of pathlib.Path
+    """
+    from .EricDirFileDialog import EricDirFileDialog
+
+    return EricDirFileDialog.getOpenFileAndDirPaths(
+        parent, caption, directory, filterStr, options
+    )
+
+
+def getSaveFilePath(parent=None, caption="", directory="", filterStr="", options=None):
+    """
+    Module function to get the path of a file for saving.
+
+    @param parent parent widget of the dialog
+    @type QWidget
+    @param caption window title of the dialog
+    @type str
+    @param directory working directory of the dialog
+    @type str or pathlib.Path
+    @param filterStr filter string for the dialog
+    @type str
+    @param options various options for the dialog
+    @type QFileDialog.Options
+    @return path of file to be saved
+    @rtype pathlib.Path
+    """
+    if options is None:
+        options = QFileDialog.Option(0)
+    filename = QFileDialog.getSaveFileName(
+        parent, caption, str(directory), filterStr, "", options
+    )[0]
+    return pathlib.Path(filename)
+
+
+def getSaveFilePathAndFilter(
+    parent=None, caption="", directory="", filterStr="", initialFilter="", options=None
+):
+    """
+    Module function to get the path of a file for saving and the selected
+    file name filter.
+
+    @param parent parent widget of the dialog
+    @type QWidget
+    @param caption window title of the dialog
+    @type str
+    @param directory working directory of the dialog
+    @type str or pathlib.Path
+    @param filterStr filter string for the dialog
+    @type str
+    @param initialFilter initial filter for the dialog
+    @type str
+    @param options various options for the dialog
+    @type QFileDialog.Options
+    @return path of file to be saved and selected filte
+    @rtype tuple of (pathlib.Path, str)
+    """
+    if options is None:
+        options = QFileDialog.Option(0)
+    newfilter = __reorderFilter(filterStr, initialFilter)
+    filename, selectedFilter = QFileDialog.getSaveFileName(
+        parent, caption, directory, newfilter, initialFilter, options
+    )
+    return pathlib.Path(filename), selectedFilter
+
+
+def getExistingDirectoryPath(
+    parent=None, caption="", directory="", options=QFileDialog.Option.ShowDirsOnly
+):
+    """
+    Module function to get the path of a directory.
+
+    @param parent parent widget of the dialog
+    @type QWidget
+    @param caption window title of the dialog
+    @type str
+    @param directory working directory of the dialog
+    @type str or pathlib.Path
+    @param options various options for the dialog
+    @type QFileDialog.Options
+    @return path of selected directory
+    @rtype pathlib.Path
+    """
+    if options is None:
+        options = QFileDialog.Option(0)
+    dirname = QFileDialog.getExistingDirectory(parent, caption, str(directory), options)
+    return pathlib.Path(dirname)

eric ide

mercurial