UI/BrowserModel.py

changeset 232
0ee8be384de2
parent 231
2e4eb047ae93
child 233
9d2677615a3d
equal deleted inserted replaced
231:2e4eb047ae93 232:0ee8be384de2
244 244
245 @param itm item to be watched (BrowserDirectoryItem) 245 @param itm item to be watched (BrowserDirectoryItem)
246 """ 246 """
247 if isinstance(itm, BrowserDirectoryItem): 247 if isinstance(itm, BrowserDirectoryItem):
248 dirName = itm.dirName() 248 dirName = itm.dirName()
249 if dirName not in self.watcher.directories() and \ 249 if dirName != "" and \
250 dirName != "" and \
251 not dirName.startswith("//") and \ 250 not dirName.startswith("//") and \
252 not dirName.startswith("\\\\"): 251 not dirName.startswith("\\\\"):
253 self.watcher.addPath(dirName) 252 if dirName not in self.watcher.directories():
254 self.watchedItems[dirName] = itm 253 self.watcher.addPath(dirName)
254 if dirName in self.watchedItems and \
255 itm not in self.watchedItems[dirName]:
256 self.watchedItems[dirName].append(itm)
257 else:
258 self.watchedItems[dirName] = [itm]
255 259
256 def _removeWatchedItem(self, itm): 260 def _removeWatchedItem(self, itm):
257 """ 261 """
258 Protected method to remove a watched item. 262 Protected method to remove a watched item.
259 263
260 @param itm item to be removed (BrowserDirectoryItem) 264 @param itm item to be removed (BrowserDirectoryItem)
261 """ 265 """
262 if isinstance(itm, BrowserDirectoryItem): 266 if isinstance(itm, BrowserDirectoryItem):
263 dirName = itm.dirName() 267 dirName = itm.dirName()
264 self.watcher.removePath(dirName) 268 if dirName in self.watchedItems and \
265 try: 269 itm in self.watchedItems[dirName]:
270 self.watchedItems[dirName].remove(itm)
271 if len(self.watchedItems[dirName]) == 0:
266 del self.watchedItems[dirName] 272 del self.watchedItems[dirName]
267 except KeyError: 273 self.watcher.removePath(dirName)
268 pass
269 274
270 def __directoryChanged(self, path): 275 def __directoryChanged(self, path):
271 """ 276 """
272 Private slot to handle the directoryChanged signal of the watcher. 277 Private slot to handle the directoryChanged signal of the watcher.
273 278
275 """ 280 """
276 if path not in self.watchedItems: 281 if path not in self.watchedItems:
277 # 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
278 return 283 return
279 284
280 itm = self.watchedItems[path] 285 for itm in self.watchedItems[path]:
281 if itm.isPopulated(): 286 if itm.isPopulated():
282 oldCnt = itm.childCount() 287 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 288
300 cnt = itm.childCount() 289 qdir = QDir(itm.dirName())
301 self.beginInsertRows(self.createIndex(itm.row(), 0, itm), 290
302 cnt, cnt) 291 entryInfoList = qdir.entryInfoList(
303 if f.isDir(): 292 QDir.Filters(QDir.AllEntries | QDir.NoDotAndDotDot))
304 node = BrowserDirectoryItem(itm, 293
305 Utilities.toNativeSeparators(f.absoluteFilePath()), 294 # step 1: check for new entries
306 False) 295 for f in entryInfoList:
307 self._addWatchedItem(node) 296 fpath = f.absoluteFilePath()
308 else: 297 childFound = False
309 node = BrowserFileItem(itm, 298 for child in itm.children():
310 Utilities.toNativeSeparators(f.absoluteFilePath())) 299 if child.name() == fpath:
311 self._addItem(node, itm) 300 childFound = True
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 301 break
323 if entryFound: 302 if childFound:
324 continue 303 continue
325 304
326 self.beginRemoveRows(self.createIndex(itm.row(), 0, itm), 305 cnt = itm.childCount()
327 row, row) 306 self.beginInsertRows(self.createIndex(itm.row(), 0, itm),
328 itm.removeChild(child) 307 cnt, cnt)
329 self.endRemoveRows() 308 if f.isDir():
309 node = BrowserDirectoryItem(itm,
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()
330 335
331 def __populateModel(self): 336 def __populateModel(self):
332 """ 337 """
333 Private method to populate the browser model. 338 Private method to populate the browser model.
334 """ 339 """

eric ide

mercurial