UI/BrowserModel.py

changeset 231
2e4eb047ae93
parent 103
59137afca666
child 232
0ee8be384de2
equal deleted inserted replaced
230:2cde09c26384 231:2e4eb047ae93
44 44
45 rootData = QApplication.translate("BrowserModel", "Name") 45 rootData = QApplication.translate("BrowserModel", "Name")
46 self.rootItem = BrowserItem(None, rootData) 46 self.rootItem = BrowserItem(None, rootData)
47 47
48 self.progDir = None 48 self.progDir = None
49 self.watchedItems = {}
50 self.watcher = QFileSystemWatcher(self)
51 self.watcher.directoryChanged.connect(self.__directoryChanged)
49 52
50 self.__populateModel() 53 self.__populateModel()
51 54
52 def columnCount(self, parent=QModelIndex()): 55 def columnCount(self, parent=QModelIndex()):
53 """ 56 """
233 if not index.isValid(): 236 if not index.isValid():
234 return None 237 return None
235 238
236 return index.internalPointer() 239 return index.internalPointer()
237 240
241 def _addWatchedItem(self, itm):
242 """
243 Protected method to watch an item.
244
245 @param itm item to be watched (BrowserDirectoryItem)
246 """
247 if isinstance(itm, BrowserDirectoryItem):
248 dirName = itm.dirName()
249 if dirName not in self.watcher.directories() and \
250 dirName != "" and \
251 not dirName.startswith("//") and \
252 not dirName.startswith("\\\\"):
253 self.watcher.addPath(dirName)
254 self.watchedItems[dirName] = itm
255
256 def _removeWatchedItem(self, itm):
257 """
258 Protected method to remove a watched item.
259
260 @param itm item to be removed (BrowserDirectoryItem)
261 """
262 if isinstance(itm, BrowserDirectoryItem):
263 dirName = itm.dirName()
264 self.watcher.removePath(dirName)
265 try:
266 del self.watchedItems[dirName]
267 except KeyError:
268 pass
269
270 def __directoryChanged(self, path):
271 """
272 Private slot to handle the directoryChanged signal of the watcher.
273
274 @param path path of the directory (string)
275 """
276 if path not in self.watchedItems:
277 # just ignore the situation we don't have a reference to the item
278 return
279
280 itm = self.watchedItems[path]
281 if itm.isPopulated():
282 oldCnt = itm.childCount()
283
284 qdir = QDir(itm.dirName())
285
286 entryInfoList = \
287 qdir.entryInfoList(QDir.Filters(QDir.AllEntries | QDir.NoDotAndDotDot))
288
289 # step 1: check for new entries
290 for f in entryInfoList:
291 fpath = f.absoluteFilePath()
292 childFound = False
293 for child in itm.children():
294 if child.name() == fpath:
295 childFound = True
296 break
297 if childFound:
298 continue
299
300 cnt = itm.childCount()
301 self.beginInsertRows(self.createIndex(itm.row(), 0, itm),
302 cnt, cnt)
303 if f.isDir():
304 node = BrowserDirectoryItem(itm,
305 Utilities.toNativeSeparators(f.absoluteFilePath()),
306 False)
307 self._addWatchedItem(node)
308 else:
309 node = BrowserFileItem(itm,
310 Utilities.toNativeSeparators(f.absoluteFilePath()))
311 self._addItem(node, itm)
312 self.endInsertRows()
313
314 # step 2: check for removed entries
315 if len(entryInfoList) != itm.childCount():
316 for row in range(oldCnt - 1, -1, -1):
317 child = itm.child(row)
318 entryFound = False
319 for f in entryInfoList:
320 if f.absoluteFilePath() == child.name():
321 entryFound = True
322 break
323 if entryFound:
324 continue
325
326 self.beginRemoveRows(self.createIndex(itm.row(), 0, itm),
327 row, row)
328 itm.removeChild(child)
329 self.endRemoveRows()
330
238 def __populateModel(self): 331 def __populateModel(self):
239 """ 332 """
240 Private method to populate the browser model. 333 Private method to populate the browser model.
241 """ 334 """
242 self._addItem(BrowserSysPathItem(self.rootItem), self.rootItem) 335 self._addItem(BrowserSysPathItem(self.rootItem), self.rootItem)
251 self.toplevelDirs.append(Utilities.toNativeSeparators(\ 344 self.toplevelDirs.append(Utilities.toNativeSeparators(\
252 d.absoluteFilePath())) 345 d.absoluteFilePath()))
253 346
254 for d in self.toplevelDirs: 347 for d in self.toplevelDirs:
255 self._addItem(BrowserDirectoryItem(self.rootItem, d), self.rootItem) 348 self._addItem(BrowserDirectoryItem(self.rootItem, d), self.rootItem)
349 self.watcher.addPath(d)
256 350
257 def programChange(self, dirname): 351 def programChange(self, dirname):
258 """ 352 """
259 Public method to change the entry for the directory of file being debugged. 353 Public method to change the entry for the directory of file being debugged.
260 354
261 @param dirname name of the directory containg the file (string) 355 @param dirname name of the directory containing the file (string)
262 """ 356 """
263 if self.progDir: 357 if self.progDir:
264 if dirname == self.progDir.dirName(): 358 if dirname == self.progDir.dirName():
265 return 359 return
266 360
267 # remove old entry 361 # remove old entry
362 self._removeWatchedItem(self.progDir)
268 self.beginRemoveRows(QModelIndex(), self.progDir.row(), self.progDir.row()) 363 self.beginRemoveRows(QModelIndex(), self.progDir.row(), self.progDir.row())
269 self.rootItem.removeChild(self.progDir) 364 self.rootItem.removeChild(self.progDir)
270 self.endRemoveRows() 365 self.endRemoveRows()
271 self.progDir = None 366 self.progDir = None
272 367
273 itm = BrowserDirectoryItem(self.rootItem, dirname) 368 itm = BrowserDirectoryItem(self.rootItem, dirname)
274 self.addItem(itm) 369 self.addItem(itm)
275 self.progDir = itm 370 self.progDir = itm
371 self._addWatchedItem(itm)
276 372
277 def addTopLevelDir(self, dirname): 373 def addTopLevelDir(self, dirname):
278 """ 374 """
279 Public method to add a new toplevel directory. 375 Public method to add a new toplevel directory.
280 376
282 """ 378 """
283 if dirname not in self.toplevelDirs: 379 if dirname not in self.toplevelDirs:
284 itm = BrowserDirectoryItem(self.rootItem, dirname) 380 itm = BrowserDirectoryItem(self.rootItem, dirname)
285 self.addItem(itm) 381 self.addItem(itm)
286 self.toplevelDirs.append(itm.dirName()) 382 self.toplevelDirs.append(itm.dirName())
383 self._addWatchedItem(itm)
287 384
288 def removeToplevelDir(self, index): 385 def removeToplevelDir(self, index):
289 """ 386 """
290 Public method to remove a toplevel directory. 387 Public method to remove a toplevel directory.
291 388
298 self.beginRemoveRows(index.parent(), index.row(), index.row()) 395 self.beginRemoveRows(index.parent(), index.row(), index.row())
299 self.rootItem.removeChild(item) 396 self.rootItem.removeChild(item)
300 self.endRemoveRows() 397 self.endRemoveRows()
301 398
302 self.toplevelDirs.remove(item.dirName()) 399 self.toplevelDirs.remove(item.dirName())
400 self._removeWatchedItem(item)
303 401
304 def saveToplevelDirs(self): 402 def saveToplevelDirs(self):
305 """ 403 """
306 Public slot to save the toplevel directories. 404 Public slot to save the toplevel directories.
307 """ 405 """
372 for f in entryInfoList: 470 for f in entryInfoList:
373 if f.isDir(): 471 if f.isDir():
374 node = BrowserDirectoryItem(parentItem, 472 node = BrowserDirectoryItem(parentItem,
375 Utilities.toNativeSeparators(f.absoluteFilePath()), 473 Utilities.toNativeSeparators(f.absoluteFilePath()),
376 False) 474 False)
475 self._addWatchedItem(node)
377 else: 476 else:
378 node = BrowserFileItem(parentItem, 477 node = BrowserFileItem(parentItem,
379 Utilities.toNativeSeparators(f.absoluteFilePath())) 478 Utilities.toNativeSeparators(f.absoluteFilePath()))
380 self._addItem(node, parentItem) 479 self._addItem(node, parentItem)
381 if repopulate: 480 if repopulate:
396 if p == '': 495 if p == '':
397 p = os.getcwd() 496 p = os.getcwd()
398 497
399 node = BrowserDirectoryItem(parentItem, p) 498 node = BrowserDirectoryItem(parentItem, p)
400 self._addItem(node, parentItem) 499 self._addItem(node, parentItem)
500 self._addWatchedItem(node)
401 if repopulate: 501 if repopulate:
402 self.endInsertRows() 502 self.endInsertRows()
403 503
404 def populateFileItem(self, parentItem, repopulate = False): 504 def populateFileItem(self, parentItem, repopulate = False):
405 """ 505 """
769 def dirName(self): 869 def dirName(self):
770 """ 870 """
771 Public method returning the directory name. 871 Public method returning the directory name.
772 872
773 @return directory name (string) 873 @return directory name (string)
874 """
875 return self._dirName
876
877 def name(self):
878 """
879 Public method to return the name of the item.
880
881 @return name of the item (string)
774 """ 882 """
775 return self._dirName 883 return self._dirName
776 884
777 def lessThan(self, other, column, order): 885 def lessThan(self, other, column, order):
778 """ 886 """
901 1009
902 @return filename (string) 1010 @return filename (string)
903 """ 1011 """
904 return self._filename 1012 return self._filename
905 1013
1014 def name(self):
1015 """
1016 Public method to return the name of the item.
1017
1018 @return name of the item (string)
1019 """
1020 return self._filename
1021
906 def fileExt(self): 1022 def fileExt(self):
907 """ 1023 """
908 Public method returning the file extension. 1024 Public method returning the file extension.
909 1025
910 @return file extension (string) 1026 @return file extension (string)

eric ide

mercurial