eric7/EricWidgets/EricPathPicker.py

branch
eric7
changeset 9152
8a68afaf1ba2
parent 9024
640778fcd474
diff -r 8c5296fe3056 -r 8a68afaf1ba2 eric7/EricWidgets/EricPathPicker.py
--- a/eric7/EricWidgets/EricPathPicker.py	Wed Jun 15 09:44:07 2022 +0200
+++ b/eric7/EricWidgets/EricPathPicker.py	Thu Jun 16 18:28:59 2022 +0200
@@ -9,8 +9,9 @@
 
 import enum
 import os
+import pathlib
 
-from PyQt6.QtCore import pyqtSignal, Qt, QFileInfo, QCoreApplication, QDir
+from PyQt6.QtCore import pyqtSignal, Qt, QCoreApplication, QDir
 from PyQt6.QtWidgets import (
     QWidget, QHBoxLayout, QToolButton, QSizePolicy, QLineEdit, QComboBox
 )
@@ -120,15 +121,15 @@
         
         self.__button.setEnabled(self.__mode != EricPathPickerModes.NO_MODE)
     
-    def __pathEdited(self, path):
+    def __pathEdited(self, fpath):
         """
         Private slot handling editing of the path.
         
-        @param path current text of the path line edit
+        @param fpath current text of the path line edit
         @type str
         """
         if self._completer and not self._completer.popup().isVisible():
-            self._completer.setRootPath(QDir.toNativeSeparators(path))
+            self._completer.setRootPath(QDir.toNativeSeparators(fpath))
     
     def setMode(self, mode):
         """
@@ -236,11 +237,11 @@
         else:
             return self._editor.currentText()
     
-    def setText(self, path, toNative=True):
+    def setText(self, fpath, toNative=True):
         """
         Public method to set the current path.
         
-        @param path path to be set
+        @param fpath path to be set
         @type str
         @param toNative flag indicating to convert the path into
             a native format
@@ -250,13 +251,13 @@
             EricPathPickerModes.OPEN_FILES_MODE,
             EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE,
         ):
-            self._setEditorText(path)
+            self._setEditorText(fpath)
         else:
             if toNative:
-                path = QDir.toNativeSeparators(path)
-            self._setEditorText(path)
+                fpath = QDir.toNativeSeparators(fpath)
+            self._setEditorText(fpath)
             if self._completer:
-                self._completer.setRootPath(path)
+                self._completer.setRootPath(fpath)
     
     def text(self, toNative=True):
         """
@@ -274,8 +275,8 @@
         ):
             if toNative:
                 return ";".join(
-                    [QDir.toNativeSeparators(path)
-                     for path in self._editorText().split(";")])
+                    [QDir.toNativeSeparators(fpath)
+                     for fpath in self._editorText().split(";")])
             else:
                 return self._editorText()
         else:
@@ -285,17 +286,17 @@
             else:
                 return os.path.expanduser(self._editorText())
     
-    def setEditText(self, path, toNative=True):
+    def setEditText(self, fpath, toNative=True):
         """
         Public method to set the current path.
         
-        @param path path to be set
+        @param fpath path to be set
         @type str
         @param toNative flag indicating to convert the path into
             a native format
         @type bool
         """
-        self.setText(path, toNative=toNative)
+        self.setText(fpath, toNative=toNative)
     
     def currentText(self, toNative=True):
         """
@@ -309,17 +310,17 @@
         """
         return self.text(toNative=toNative)
     
-    def setPath(self, path, toNative=True):
+    def setPath(self, fpath, toNative=True):
         """
         Public method to set the current path.
         
-        @param path path to be set
+        @param fpath path to be set
         @type str
         @param toNative flag indicating to convert the path into
             a native format
         @type bool
         """
-        self.setText(path, toNative=toNative)
+        self.setText(fpath, toNative=toNative)
     
     def path(self, toNative=True):
         """
@@ -541,82 +542,80 @@
         directory = QDir.fromNativeSeparators(directory)
         
         if self.__mode == EricPathPickerModes.OPEN_FILE_MODE:
-            path = EricFileDialog.getOpenFileName(
+            fpath = EricFileDialog.getOpenFileName(
                 self,
                 windowTitle,
                 directory,
                 self.__filters)
-            path = QDir.toNativeSeparators(path)
+            fpath = QDir.toNativeSeparators(fpath)
         elif self.__mode == EricPathPickerModes.OPEN_FILES_MODE:
-            paths = EricFileDialog.getOpenFileNames(
+            fpaths = EricFileDialog.getOpenFileNames(
                 self,
                 windowTitle,
                 directory,
                 self.__filters)
-            path = ";".join([QDir.toNativeSeparators(path)
-                             for path in paths])
+            fpath = ";".join([QDir.toNativeSeparators(fpath)
+                             for fpath in fpaths])
         elif self.__mode == EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE:
-            paths = EricFileDialog.getOpenFileAndDirNames(
+            fpaths = EricFileDialog.getOpenFileAndDirNames(
                 self,
                 windowTitle,
                 directory,
                 self.__filters)
-            path = ";".join([QDir.toNativeSeparators(path)
-                             for path in paths])
+            fpath = ";".join([QDir.toNativeSeparators(fpath)
+                             for fpath in fpaths])
         elif self.__mode == EricPathPickerModes.SAVE_FILE_MODE:
-            path = EricFileDialog.getSaveFileName(
+            fpath = EricFileDialog.getSaveFileName(
                 self,
                 windowTitle,
                 directory,
                 self.__filters,
                 EricFileDialog.DontConfirmOverwrite)
-            path = QDir.toNativeSeparators(path)
+            fpath = QDir.toNativeSeparators(fpath)
         elif (
             self.__mode == EricPathPickerModes.SAVE_FILE_ENSURE_EXTENSION_MODE
         ):
-            path, selectedFilter = EricFileDialog.getSaveFileNameAndFilter(
+            fpath, selectedFilter = EricFileDialog.getSaveFileNameAndFilter(
                 self,
                 windowTitle,
                 directory,
                 self.__filters,
                 None,
                 EricFileDialog.DontConfirmOverwrite)
-            path = QDir.toNativeSeparators(path)
-            if path:
-                ext = QFileInfo(path).suffix()
-                if not ext:
-                    ex = selectedFilter.split("(*")[1].split(")")[0]
-                    if ex:
-                        path += ex
+            fpath = pathlib.Path(fpath)
+            if not fpath.suffix:
+                ex = selectedFilter.split("(*")[1].split(")")[0]
+                if ex:
+                    fpath = fpath.with_suffix(ex)
         elif self.__mode == EricPathPickerModes.SAVE_FILE_OVERWRITE_MODE:
-            path = EricFileDialog.getSaveFileName(
+            fpath = EricFileDialog.getSaveFileName(
                 self,
                 windowTitle,
                 directory,
                 self.__filters)
-            path = QDir.toNativeSeparators(path)
+            fpath = QDir.toNativeSeparators(fpath)
         elif self.__mode == EricPathPickerModes.DIRECTORY_MODE:
-            path = EricFileDialog.getExistingDirectory(
+            fpath = EricFileDialog.getExistingDirectory(
                 self,
                 windowTitle,
                 directory,
                 EricFileDialog.ShowDirsOnly)
-            path = QDir.toNativeSeparators(path)
-            while path.endswith(os.sep):
-                path = path[:-1]
+            fpath = QDir.toNativeSeparators(fpath)
+            while fpath.endswith(os.sep):
+                fpath = fpath[:-1]
         elif self.__mode == EricPathPickerModes.DIRECTORY_SHOW_FILES_MODE:
-            path = EricFileDialog.getExistingDirectory(
+            fpath = EricFileDialog.getExistingDirectory(
                 self,
                 windowTitle,
                 directory,
                 EricFileDialog.DontUseNativeDialog)
-            path = QDir.toNativeSeparators(path)
-            while path.endswith(os.sep):
-                path = path[:-1]
+            fpath = QDir.toNativeSeparators(fpath)
+            while fpath.endswith(os.sep):
+                fpath = fpath[:-1]
         
-        if path:
-            self._setEditorText(path)
-            self.pathSelected.emit(path)
+        if fpath:
+            self._setEditorText(str(fpath))
+            self.pathSelected.emit(str(fpath))
     
     def setReadOnly(self, readOnly):
         """
@@ -656,14 +655,14 @@
         """
         self._editor.addItems(pathsList)
     
-    def addItem(self, path):
+    def addItem(self, fpath):
         """
         Public method to add a paths to the current list.
         
-        @param path path to add
+        @param fpath path to add
         @type str
         """
-        self._editor.addItem(path)
+        self._editor.addItem(fpath)
     
     def setPathsList(self, pathsList):
         """

eric ide

mercurial