return TupledListModel
-class FileSystemModel(QtCore.QAbstractListModel):
- """
- Wrapper around QtGui.QFileSystemModel
- """
-
- FILEINFOS = [
- "fileName",
- "isDir",
- "filePath",
- "completeSuffix",
- "baseName",
- ]
-
- EXTINFOS = [
- "type",
- ]
-
- ALLINFOS = FILEINFOS + EXTINFOS
-
- def __init__(self, model, path):
- QtCore.QAbstractListModel.__init__(self)
- self._path = path
-
- self._model = model
- self._rootIndex = self._model.index(self._path)
-
- self._child = None
- self.setRoleNames(dict(enumerate(self.ALLINFOS)))
- self._model.directoryLoaded.connect(self._on_directory_loaded)
-
- childChanged = qt_compat.Signal(QtCore.QObject)
-
- def _child(self):
- assert self._child is not None
- return self._child
-
- child = qt_compat.Property(QtCore.QObject, _child, notify=childChanged)
-
- backendChanged = qt_compat.Signal()
-
- def _parent(self):
- finfo = self._model.fileInfo(self._rootIndex)
- return finfo.fileName()
-
- parent = qt_compat.Property(str, _parent, notify=backendChanged)
-
- @qt_compat.Slot(str)
- def browse_to(self, path):
- if self._child is None:
- self._child = FileSystemModel(self._model, path)
- else:
- self._child.switch_to(path)
- self.childChanged.emit()
- return self._child
-
- @qt_compat.Slot(str)
- def switch_to(self, path):
- with scoped_model_reset(self):
- self._path = path
- self._rootIndex = self._model.index(self._path)
- self.backendChanged.emit()
-
- def __len__(self):
- return self._model.rowCount(self._rootIndex)
-
- def __getitem__(self, key):
- return self._model.index(key, 0, self._rootIndex)
-
- def __iter__(self):
- return (self[i] for i in xrange(len(self)))
-
- def rowCount(self, parent=QtCore.QModelIndex()):
- return len(self)
-
- def data(self, index, role):
- if index.isValid() and 0 <= role and role < len(self.ALLINFOS):
- internalIndex = self._translate_index(index)
- info = self._model.fileInfo(internalIndex)
- if role < len(self.FILEINFOS):
- field = self.FILEINFOS[role]
- value = getattr(info, field)()
- else:
- role -= len(self.FILEINFOS)
- field = self.EXTINFOS[role]
- if field == "type":
- return self._model.type(internalIndex)
- else:
- raise NotImplementedError("Out of range that was already checked")
- return value
- return None
-
- def _on_directory_loaded(self, path):
- if self._path == path:
- self.backendChanged.emit()
- self.reset()
-
- def _translate_index(self, externalIndex):
- internalIndex = self._model.index(externalIndex.row(), 0, self._rootIndex)
- return internalIndex
-
-
@contextlib.contextmanager
def scoped_model_reset(model):
model.beginResetModel()