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 |
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: |