src/eric7/ViewManager/ViewManager.py

branch
eric7
changeset 10377
9f6ffcd1db54
parent 10376
4146ac0fd307
child 10379
6f27d7bcfe56
--- a/src/eric7/ViewManager/ViewManager.py	Mon Dec 04 15:21:07 2023 +0100
+++ b/src/eric7/ViewManager/ViewManager.py	Mon Dec 04 19:00:59 2023 +0100
@@ -15,6 +15,7 @@
 from PyQt6.Qsci import QsciScintilla
 from PyQt6.QtCore import (
     QCoreApplication,
+    QFileSystemWatcher,
     QPoint,
     QSignalMapper,
     Qt,
@@ -140,6 +141,10 @@
 
         self.__lastFocusWidget = None
 
+        # initialize the file system watcher
+        self.__watcher = QFileSystemWatcher(self)
+        self.__watcher.fileChanged.connect(self.__watchedFileChanged)
+
     def setReferences(self, ui, dbs):
         """
         Public method to set some references needed later on.
@@ -5455,6 +5460,9 @@
         self._removeView(editor)
         self.editors.remove(editor)
 
+        # remove the file from the monitor list
+        self.removeWatchedFilePath(fn)
+
         # send a signal, if it was the last editor for this filename
         if fn and self.getOpenEditor(fn) is None:
             self.editorClosed.emit(fn)
@@ -5950,6 +5958,9 @@
                 self.__editorOpened()
                 self.editorOpened.emit(fn)
                 self.editorOpenedEd.emit(editor)
+
+                self.addWatchedFilePath(fn)
+
                 newWin = True
 
         if newWin:
@@ -5981,8 +5992,10 @@
         Public method to return the editor displaying the given file.
 
         @param fn filename to look for
+        @type str
         @return a reference to the editor displaying this file or None, if
             no editor was found
+        @rtype Editor or None
         """
         for editor in self.editors:
             if FileSystemUtilities.samepath(fn, editor.getFileName()):
@@ -5990,6 +6003,20 @@
 
         return None
 
+    def getOpenEditorList(self, fn):
+        """
+        Public method to return a list of all editors displaying the given file.
+
+        @param fn filename to look for
+        @type str
+        @return list of references to the editors displaying this file
+        """
+        return [
+            ed
+            for ed in self.editors
+            if FileSystemUtilities.samepath(fn, ed.getFileName())
+        ]
+
     def getOpenEditorCount(self, fn):
         """
         Public method to return the count of editors displaying the given file.
@@ -5997,11 +6024,7 @@
         @param fn filename to look for
         @return count of editors displaying this file (integer)
         """
-        count = 0
-        for editor in self.editors:
-            if FileSystemUtilities.samepath(fn, editor.getFileName()):
-                count += 1
-        return count
+        return len(self.getOpenEditorList(fn))
 
     def getOpenEditorsForSession(self):
         """
@@ -6197,6 +6220,8 @@
         editor.setModified(False)
         editor.clearChangeMarkers()
 
+        self.addWatchedFilePath(fileName)
+
     def printEditor(self, editor):
         """
         Public slot to print an editor.
@@ -7805,6 +7830,49 @@
         language = editor.getLanguage()
         return self.isEditorInfoSupported(language)
 
+    #######################################################################
+    ## File system watcher related methods
+    #######################################################################
+
+    @pyqtSlot(str)
+    def __watchedFileChanged(self, filePath):
+        """
+        Private slot handling a file has been modified, renamed or removed.
+
+        @param filePath path of the file
+        @type str
+        """
+        editorList = self.getOpenEditorList(filePath)
+        if editorList:
+            # read the file for the first view only
+            editorList.pop(0).checkRereadFile()
+
+            # for all other views record the modification time only
+            for editor in editorList:
+                editor.recordModificationTime()
+
+    def addWatchedFilePath(self, filePath):
+        """
+        Public method to add a file to the list of monitored files.
+
+        @param filePath path of the file to be added
+        @type str
+        """
+        # TODO: not yet implemented
+        if filePath:
+            self.__watcher.addPath(filePath)
+
+    def removeWatchedFilePath(self, filePath):
+        """
+        Public method to remove a file from the list of monitored files.
+
+        @param filePath path of the file to be removed
+        @type str
+        """
+        # TODO: not yet implemented
+        if self.getOpenEditorCount(filePath) == 0:
+            self.__watcher.removePath(filePath)
+
     ##################################################################
     ## Below are protected utility methods
     ##################################################################

eric ide

mercurial