src/eric7/ViewManager/ViewManager.py

branch
eric7
changeset 10377
9f6ffcd1db54
parent 10376
4146ac0fd307
child 10379
6f27d7bcfe56
equal deleted inserted replaced
10376:4146ac0fd307 10377:9f6ffcd1db54
13 import re 13 import re
14 14
15 from PyQt6.Qsci import QsciScintilla 15 from PyQt6.Qsci import QsciScintilla
16 from PyQt6.QtCore import ( 16 from PyQt6.QtCore import (
17 QCoreApplication, 17 QCoreApplication,
18 QFileSystemWatcher,
18 QPoint, 19 QPoint,
19 QSignalMapper, 20 QSignalMapper,
20 Qt, 21 Qt,
21 pyqtSignal, 22 pyqtSignal,
22 pyqtSlot, 23 pyqtSlot,
137 self.apisManager = APIsManager(parent=self) 138 self.apisManager = APIsManager(parent=self)
138 139
139 self.__cooperationClient = None 140 self.__cooperationClient = None
140 141
141 self.__lastFocusWidget = None 142 self.__lastFocusWidget = None
143
144 # initialize the file system watcher
145 self.__watcher = QFileSystemWatcher(self)
146 self.__watcher.fileChanged.connect(self.__watchedFileChanged)
142 147
143 def setReferences(self, ui, dbs): 148 def setReferences(self, ui, dbs):
144 """ 149 """
145 Public method to set some references needed later on. 150 Public method to set some references needed later on.
146 151
5453 # remove the window 5458 # remove the window
5454 editor.parent().aboutToBeClosed() 5459 editor.parent().aboutToBeClosed()
5455 self._removeView(editor) 5460 self._removeView(editor)
5456 self.editors.remove(editor) 5461 self.editors.remove(editor)
5457 5462
5463 # remove the file from the monitor list
5464 self.removeWatchedFilePath(fn)
5465
5458 # send a signal, if it was the last editor for this filename 5466 # send a signal, if it was the last editor for this filename
5459 if fn and self.getOpenEditor(fn) is None: 5467 if fn and self.getOpenEditor(fn) is None:
5460 self.editorClosed.emit(fn) 5468 self.editorClosed.emit(fn)
5461 self.editorClosedEd.emit(editor) 5469 self.editorClosedEd.emit(editor)
5462 5470
5948 self.editors.append(editor) 5956 self.editors.append(editor)
5949 self.__connectEditor(editor) 5957 self.__connectEditor(editor)
5950 self.__editorOpened() 5958 self.__editorOpened()
5951 self.editorOpened.emit(fn) 5959 self.editorOpened.emit(fn)
5952 self.editorOpenedEd.emit(editor) 5960 self.editorOpenedEd.emit(editor)
5961
5962 self.addWatchedFilePath(fn)
5963
5953 newWin = True 5964 newWin = True
5954 5965
5955 if newWin: 5966 if newWin:
5956 self._addView(assembly, fn, addNext=addNext, indexes=indexes) 5967 self._addView(assembly, fn, addNext=addNext, indexes=indexes)
5957 else: 5968 else:
5979 def getOpenEditor(self, fn): 5990 def getOpenEditor(self, fn):
5980 """ 5991 """
5981 Public method to return the editor displaying the given file. 5992 Public method to return the editor displaying the given file.
5982 5993
5983 @param fn filename to look for 5994 @param fn filename to look for
5995 @type str
5984 @return a reference to the editor displaying this file or None, if 5996 @return a reference to the editor displaying this file or None, if
5985 no editor was found 5997 no editor was found
5998 @rtype Editor or None
5986 """ 5999 """
5987 for editor in self.editors: 6000 for editor in self.editors:
5988 if FileSystemUtilities.samepath(fn, editor.getFileName()): 6001 if FileSystemUtilities.samepath(fn, editor.getFileName()):
5989 return editor 6002 return editor
5990 6003
5991 return None 6004 return None
5992 6005
6006 def getOpenEditorList(self, fn):
6007 """
6008 Public method to return a list of all editors displaying the given file.
6009
6010 @param fn filename to look for
6011 @type str
6012 @return list of references to the editors displaying this file
6013 """
6014 return [
6015 ed
6016 for ed in self.editors
6017 if FileSystemUtilities.samepath(fn, ed.getFileName())
6018 ]
6019
5993 def getOpenEditorCount(self, fn): 6020 def getOpenEditorCount(self, fn):
5994 """ 6021 """
5995 Public method to return the count of editors displaying the given file. 6022 Public method to return the count of editors displaying the given file.
5996 6023
5997 @param fn filename to look for 6024 @param fn filename to look for
5998 @return count of editors displaying this file (integer) 6025 @return count of editors displaying this file (integer)
5999 """ 6026 """
6000 count = 0 6027 return len(self.getOpenEditorList(fn))
6001 for editor in self.editors:
6002 if FileSystemUtilities.samepath(fn, editor.getFileName()):
6003 count += 1
6004 return count
6005 6028
6006 def getOpenEditorsForSession(self): 6029 def getOpenEditorsForSession(self):
6007 """ 6030 """
6008 Public method to get a lists of all open editors. 6031 Public method to get a lists of all open editors.
6009 6032
6194 self.editorOpenedEd.emit(editor) 6217 self.editorOpenedEd.emit(editor)
6195 6218
6196 editor.setText(text) 6219 editor.setText(text)
6197 editor.setModified(False) 6220 editor.setModified(False)
6198 editor.clearChangeMarkers() 6221 editor.clearChangeMarkers()
6222
6223 self.addWatchedFilePath(fileName)
6199 6224
6200 def printEditor(self, editor): 6225 def printEditor(self, editor):
6201 """ 6226 """
6202 Public slot to print an editor. 6227 Public slot to print an editor.
6203 6228
7803 @rtype bool 7828 @rtype bool
7804 """ 7829 """
7805 language = editor.getLanguage() 7830 language = editor.getLanguage()
7806 return self.isEditorInfoSupported(language) 7831 return self.isEditorInfoSupported(language)
7807 7832
7833 #######################################################################
7834 ## File system watcher related methods
7835 #######################################################################
7836
7837 @pyqtSlot(str)
7838 def __watchedFileChanged(self, filePath):
7839 """
7840 Private slot handling a file has been modified, renamed or removed.
7841
7842 @param filePath path of the file
7843 @type str
7844 """
7845 editorList = self.getOpenEditorList(filePath)
7846 if editorList:
7847 # read the file for the first view only
7848 editorList.pop(0).checkRereadFile()
7849
7850 # for all other views record the modification time only
7851 for editor in editorList:
7852 editor.recordModificationTime()
7853
7854 def addWatchedFilePath(self, filePath):
7855 """
7856 Public method to add a file to the list of monitored files.
7857
7858 @param filePath path of the file to be added
7859 @type str
7860 """
7861 # TODO: not yet implemented
7862 if filePath:
7863 self.__watcher.addPath(filePath)
7864
7865 def removeWatchedFilePath(self, filePath):
7866 """
7867 Public method to remove a file from the list of monitored files.
7868
7869 @param filePath path of the file to be removed
7870 @type str
7871 """
7872 # TODO: not yet implemented
7873 if self.getOpenEditorCount(filePath) == 0:
7874 self.__watcher.removePath(filePath)
7875
7808 ################################################################## 7876 ##################################################################
7809 ## Below are protected utility methods 7877 ## Below are protected utility methods
7810 ################################################################## 7878 ##################################################################
7811 7879
7812 def _getOpenStartDir(self): 7880 def _getOpenStartDir(self):

eric ide

mercurial