UI/BrowserModel.py

changeset 233
9d2677615a3d
parent 232
0ee8be384de2
child 405
374066392929
child 792
a13346916170
equal deleted inserted replaced
232:0ee8be384de2 233:9d2677615a3d
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 = {} 49 self.watchedItems = {}
50 self.watcher = QFileSystemWatcher(self) 50 self.watcher = QFileSystemWatcher(self)
51 self.watcher.directoryChanged.connect(self.__directoryChanged) 51 self.watcher.directoryChanged.connect(self.directoryChanged)
52 52
53 self.__populateModel() 53 self.__populateModel()
54 54
55 def columnCount(self, parent=QModelIndex()): 55 def columnCount(self, parent=QModelIndex()):
56 """ 56 """
249 if dirName != "" and \ 249 if dirName != "" and \
250 not dirName.startswith("//") and \ 250 not dirName.startswith("//") and \
251 not dirName.startswith("\\\\"): 251 not dirName.startswith("\\\\"):
252 if dirName not in self.watcher.directories(): 252 if dirName not in self.watcher.directories():
253 self.watcher.addPath(dirName) 253 self.watcher.addPath(dirName)
254 if dirName in self.watchedItems and \ 254 if dirName in self.watchedItems:
255 itm not in self.watchedItems[dirName]: 255 if itm not in self.watchedItems[dirName]:
256 self.watchedItems[dirName].append(itm) 256 self.watchedItems[dirName].append(itm)
257 else: 257 else:
258 self.watchedItems[dirName] = [itm] 258 self.watchedItems[dirName] = [itm]
259 259
260 def _removeWatchedItem(self, itm): 260 def _removeWatchedItem(self, itm):
261 """ 261 """
263 263
264 @param itm item to be removed (BrowserDirectoryItem) 264 @param itm item to be removed (BrowserDirectoryItem)
265 """ 265 """
266 if isinstance(itm, BrowserDirectoryItem): 266 if isinstance(itm, BrowserDirectoryItem):
267 dirName = itm.dirName() 267 dirName = itm.dirName()
268 if dirName in self.watchedItems and \ 268 if dirName in self.watchedItems:
269 itm in self.watchedItems[dirName]: 269 if itm in self.watchedItems[dirName]:
270 self.watchedItems[dirName].remove(itm) 270 self.watchedItems[dirName].remove(itm)
271 if len(self.watchedItems[dirName]) == 0: 271 if len(self.watchedItems[dirName]) == 0:
272 del self.watchedItems[dirName] 272 del self.watchedItems[dirName]
273 self.watcher.removePath(dirName) 273 self.watcher.removePath(dirName)
274 274
275 def __directoryChanged(self, path): 275 def directoryChanged(self, path):
276 """ 276 """
277 Private slot to handle the directoryChanged signal of the watcher. 277 Public slot to handle the directoryChanged signal of the watcher.
278 278
279 @param path path of the directory (string) 279 @param path path of the directory (string)
280 """ 280 """
281 if path not in self.watchedItems: 281 if path not in self.watchedItems:
282 # just ignore the situation we don't have a reference to the item 282 # just ignore the situation we don't have a reference to the item
283 return 283 return
284 284
285 for itm in self.watchedItems[path]: 285 for itm in self.watchedItems[path]:
286 if itm.isPopulated(): 286 oldCnt = itm.childCount()
287 oldCnt = itm.childCount() 287
288 qdir = QDir(itm.dirName())
289
290 entryInfoList = qdir.entryInfoList(
291 QDir.Filters(QDir.AllEntries | QDir.NoDotAndDotDot))
292
293 # step 1: check for new entries
294 children = itm.children()
295 for f in entryInfoList:
296 fpath = f.absoluteFilePath()
297 childFound = False
298 for child in children:
299 if child.name() == fpath:
300 childFound = True
301 children.remove(child)
302 break
303 if childFound:
304 continue
288 305
289 qdir = QDir(itm.dirName()) 306 cnt = itm.childCount()
290 307 self.beginInsertRows(self.createIndex(itm.row(), 0, itm),
291 entryInfoList = qdir.entryInfoList( 308 cnt, cnt)
292 QDir.Filters(QDir.AllEntries | QDir.NoDotAndDotDot)) 309 if f.isDir():
293 310 node = BrowserDirectoryItem(itm,
294 # step 1: check for new entries 311 Utilities.toNativeSeparators(f.absoluteFilePath()),
295 for f in entryInfoList: 312 False)
296 fpath = f.absoluteFilePath() 313 else:
297 childFound = False 314 node = BrowserFileItem(itm,
298 for child in itm.children(): 315 Utilities.toNativeSeparators(f.absoluteFilePath()))
299 if child.name() == fpath: 316 self._addItem(node, itm)
300 childFound = True 317 self.endInsertRows()
318
319 # step 2: check for removed entries
320 if len(entryInfoList) != itm.childCount():
321 for row in range(oldCnt - 1, -1, -1):
322 child = itm.child(row)
323 entryFound = False
324 for f in entryInfoList:
325 if f.absoluteFilePath() == child.name():
326 entryFound = True
327 entryInfoList.remove(f)
301 break 328 break
302 if childFound: 329 if entryFound:
303 continue 330 continue
304 331
305 cnt = itm.childCount() 332 self._removeWatchedItem(child)
306 self.beginInsertRows(self.createIndex(itm.row(), 0, itm), 333 self.beginRemoveRows(self.createIndex(itm.row(), 0, itm),
307 cnt, cnt) 334 row, row)
308 if f.isDir(): 335 itm.removeChild(child)
309 node = BrowserDirectoryItem(itm, 336 self.endRemoveRows()
310 Utilities.toNativeSeparators(f.absoluteFilePath()),
311 False)
312 self._addWatchedItem(node)
313 else:
314 node = BrowserFileItem(itm,
315 Utilities.toNativeSeparators(f.absoluteFilePath()))
316 self._addItem(node, itm)
317 self.endInsertRows()
318
319 # step 2: check for removed entries
320 if len(entryInfoList) != itm.childCount():
321 for row in range(oldCnt - 1, -1, -1):
322 child = itm.child(row)
323 entryFound = False
324 for f in entryInfoList:
325 if f.absoluteFilePath() == child.name():
326 entryFound = True
327 break
328 if entryFound:
329 continue
330
331 self.beginRemoveRows(self.createIndex(itm.row(), 0, itm),
332 row, row)
333 itm.removeChild(child)
334 self.endRemoveRows()
335 337
336 def __populateModel(self): 338 def __populateModel(self):
337 """ 339 """
338 Private method to populate the browser model. 340 Private method to populate the browser model.
339 """ 341 """
348 for d in QDir.drives(): 350 for d in QDir.drives():
349 self.toplevelDirs.append(Utilities.toNativeSeparators(\ 351 self.toplevelDirs.append(Utilities.toNativeSeparators(\
350 d.absoluteFilePath())) 352 d.absoluteFilePath()))
351 353
352 for d in self.toplevelDirs: 354 for d in self.toplevelDirs:
353 self._addItem(BrowserDirectoryItem(self.rootItem, d), self.rootItem) 355 itm = BrowserDirectoryItem(self.rootItem, d)
354 self.watcher.addPath(d) 356 self._addItem(itm, self.rootItem)
355 357
356 def programChange(self, dirname): 358 def programChange(self, dirname):
357 """ 359 """
358 Public method to change the entry for the directory of file being debugged. 360 Public method to change the entry for the directory of file being debugged.
359 361
371 self.progDir = None 373 self.progDir = None
372 374
373 itm = BrowserDirectoryItem(self.rootItem, dirname) 375 itm = BrowserDirectoryItem(self.rootItem, dirname)
374 self.addItem(itm) 376 self.addItem(itm)
375 self.progDir = itm 377 self.progDir = itm
376 self._addWatchedItem(itm)
377 378
378 def addTopLevelDir(self, dirname): 379 def addTopLevelDir(self, dirname):
379 """ 380 """
380 Public method to add a new toplevel directory. 381 Public method to add a new toplevel directory.
381 382
383 """ 384 """
384 if dirname not in self.toplevelDirs: 385 if dirname not in self.toplevelDirs:
385 itm = BrowserDirectoryItem(self.rootItem, dirname) 386 itm = BrowserDirectoryItem(self.rootItem, dirname)
386 self.addItem(itm) 387 self.addItem(itm)
387 self.toplevelDirs.append(itm.dirName()) 388 self.toplevelDirs.append(itm.dirName())
388 self._addWatchedItem(itm)
389 389
390 def removeToplevelDir(self, index): 390 def removeToplevelDir(self, index):
391 """ 391 """
392 Public method to remove a toplevel directory. 392 Public method to remove a toplevel directory.
393 393
462 Public method to populate a directory item's subtree. 462 Public method to populate a directory item's subtree.
463 463
464 @param parentItem reference to the directory item to be populated 464 @param parentItem reference to the directory item to be populated
465 @param repopulate flag indicating a repopulation (boolean) 465 @param repopulate flag indicating a repopulation (boolean)
466 """ 466 """
467 self._addWatchedItem(parentItem)
468
467 qdir = QDir(parentItem.dirName()) 469 qdir = QDir(parentItem.dirName())
468 470
469 entryInfoList = \ 471 entryInfoList = \
470 qdir.entryInfoList(QDir.Filters(QDir.AllEntries | QDir.NoDotAndDotDot)) 472 qdir.entryInfoList(QDir.Filters(QDir.AllEntries | QDir.NoDotAndDotDot))
471 if len(entryInfoList) > 0: 473 if len(entryInfoList) > 0:
475 for f in entryInfoList: 477 for f in entryInfoList:
476 if f.isDir(): 478 if f.isDir():
477 node = BrowserDirectoryItem(parentItem, 479 node = BrowserDirectoryItem(parentItem,
478 Utilities.toNativeSeparators(f.absoluteFilePath()), 480 Utilities.toNativeSeparators(f.absoluteFilePath()),
479 False) 481 False)
480 self._addWatchedItem(node)
481 else: 482 else:
482 node = BrowserFileItem(parentItem, 483 node = BrowserFileItem(parentItem,
483 Utilities.toNativeSeparators(f.absoluteFilePath())) 484 Utilities.toNativeSeparators(f.absoluteFilePath()))
484 self._addItem(node, parentItem) 485 self._addItem(node, parentItem)
485 if repopulate: 486 if repopulate:
500 if p == '': 501 if p == '':
501 p = os.getcwd() 502 p = os.getcwd()
502 503
503 node = BrowserDirectoryItem(parentItem, p) 504 node = BrowserDirectoryItem(parentItem, p)
504 self._addItem(node, parentItem) 505 self._addItem(node, parentItem)
505 self._addWatchedItem(node)
506 if repopulate: 506 if repopulate:
507 self.endInsertRows() 507 self.endInsertRows()
508 508
509 def populateFileItem(self, parentItem, repopulate = False): 509 def populateFileItem(self, parentItem, repopulate = False):
510 """ 510 """

eric ide

mercurial