src/eric7/EricWidgets/EricPathPickerDialog.py

branch
eric7
changeset 9238
a7cbf3d61498
parent 9221
bf71ee032bb4
child 9322
3f0fe9a79aa1
diff -r 03c714bd4ebf -r a7cbf3d61498 src/eric7/EricWidgets/EricPathPickerDialog.py
--- a/src/eric7/EricWidgets/EricPathPickerDialog.py	Sat Jul 16 18:09:30 2022 +0200
+++ b/src/eric7/EricWidgets/EricPathPickerDialog.py	Sat Jul 16 18:14:30 2022 +0200
@@ -75,24 +75,6 @@
         """
         self.__pathPicker.setMode(mode)
 
-    def setPickerPath(self, path):
-        """
-        Public method to set the path of the path picker.
-
-        @param path path to be set
-        @type str
-        """
-        self.__pathPicker.setPath(path)
-
-    def setDefaultDirectory(self, directory):
-        """
-        Public method to set the default directory of the path picker.
-
-        @param directory default directory
-        @type str
-        """
-        self.__pathPicker.setDefaultDirectory(directory)
-
     def setPickerFilters(self, filters):
         """
         Public method to set the filters of the path picker.
@@ -104,23 +86,50 @@
         """
         self.__pathPicker.setFilters(filters)
 
-    def getPath(self):
+    def setPickerPath(self, fpath):
+        """
+        Public method to set the path of the path picker.
+
+        @param fpath path to be set
+        @type str or pathlib.Path
+        """
+        self.__pathPicker.setText(str(fpath))
+
+    def setDefaultDirectory(self, directory):
         """
-        Public method to get the current path.
+        Public method to set the default directory of the path picker.
+
+        @param directory default directory
+        @type str or pathlib.Path
+        """
+        self.__pathPicker.setDefaultDirectory(str(directory))
+
+    def getText(self):
+        """
+        Public method to get the current path as text.
 
         @return current path
         @rtype str
         """
+        return self.__pathPicker.text()
+
+    def getPath(self):
+        """
+        Public method to get the current path as a pathlib.Path object.
+
+        @return current path
+        @rtype pathlib.Path
+        """
         return self.__pathPicker.path()
 
 
-def getPath(
+def getStrPath(
     parent,
     title,
     label,
     mode=EricPathPickerModes.OPEN_FILE_MODE,
-    path="",
-    defaultDirectory="",
+    strPath=None,
+    defaultDirectory=None,
     filters=None,
 ):
     """
@@ -132,15 +141,15 @@
     @type str
     @param label text to be shown above the path picker
     @type str
-    @param mode mode of the path picker
-    @type EricPathPickerModes
-    @param path initial path to be shown
-    @type str
+    @param mode mode of the path picker (defaults to EricPathPickerModes.OPEN_FILE_MODE)
+    @type EricPathPickerModes (optional)
+    @param strPath initial path to be shown (defaults to None)
+    @type str (optional)
     @param defaultDirectory default directory of the path picker selection
-        dialog
-    @type str
-    @param filters list of file filters
-    @type list of str
+        dialog (defaults to None)
+    @type str (optional)
+    @param filters list of file filters (defaults to None)
+    @type list of str (optional)
     @return tuple containing the entered path and a flag indicating that the
         user pressed the OK button
     @rtype tuple of (str, bool)
@@ -152,8 +161,8 @@
     if label:
         dlg.setLabelText(label)
     dlg.setPickerMode(mode)
-    if path:
-        dlg.setPickerPath(path)
+    if strPath:
+        dlg.setPickerPath(strPath)
     if defaultDirectory:
         dlg.setDefaultDirectory(defaultDirectory)
     if filters is not None and len(filters) > 0:
@@ -162,10 +171,67 @@
     # step 2: show the dialog and get the result
     if dlg.exec() == QDialog.DialogCode.Accepted:
         ok = True
-        path = dlg.getPath().strip()
+        fpath = dlg.getText().strip()
     else:
         ok = False
-        path = ""
+        fpath = ""
 
     # step 3: return the result
-    return path, ok
+    return fpath, ok
+
+
+def getPath(
+    parent,
+    title,
+    label,
+    mode=EricPathPickerModes.OPEN_FILE_MODE,
+    pathlibPath=None,
+    defaultDirectory=None,
+    filters=None,
+):
+    """
+    Function to get a file or directory path from the user.
+
+    @param parent reference to the parent widget
+    @type QWidget
+    @param title title of the dialog
+    @type str
+    @param label text to be shown above the path picker
+    @type str
+    @param mode mode of the path picker (defaults to EricPathPickerModes.OPEN_FILE_MODE)
+    @type EricPathPickerModes (optional)
+    @param pathlibPath initial path to be shown (defaults to None)
+    @type pathlib.Path (optional)
+    @param defaultDirectory default directory of the path picker selection
+        dialog (defaults to None)
+    @type pathlib.Path (optional)
+    @param filters list of file filters (defaults to None)
+    @type list of str (optional)
+    @return tuple containing the entered path and a flag indicating that the
+        user pressed the OK button
+    @rtype tuple of (pathlib.Path, bool)
+    """
+    # step 1: setup of the dialog
+    dlg = EricPathPickerDialog(parent)
+    if title:
+        dlg.setTitle(title)
+    if label:
+        dlg.setLabelText(label)
+    dlg.setPickerMode(mode)
+    if pathlibPath:
+        dlg.setPickerPath(pathlibPath)
+    if defaultDirectory:
+        dlg.setDefaultDirectory(defaultDirectory)
+    if filters is not None and len(filters) > 0:
+        dlg.setPickerFilters(";;".join(filters))
+
+    # step 2: show the dialog and get the result
+    if dlg.exec() == QDialog.DialogCode.Accepted:
+        ok = True
+        fpath = dlg.getText().strip()
+    else:
+        ok = False
+        fpath = ""
+
+    # step 3: return the result
+    return fpath, ok

eric ide

mercurial