browser: use fresh models

This commit is contained in:
Robert Jördens 2016-04-06 22:15:51 +08:00
parent 9685771275
commit 46273455c5

View File

@ -1,4 +1,5 @@
import logging
import os
import h5py
from PyQt5 import QtCore, QtWidgets, QtGui
@ -25,51 +26,22 @@ class ResultIconProvider(QtWidgets.QFileIconProvider):
return QtGui.QIcon(pix)
class DirsOnly(QtCore.QSortFilterProxyModel):
def filterAcceptsRow(self, row, parent):
m = self.sourceModel()
if not m.isDir(m.index(row, 0, parent)):
return False
return QtCore.QSortFilterProxyModel.filterAcceptsRow(self, row, parent)
class FilesOnly(QtCore.QSortFilterProxyModel):
_root_idx = None
def filterAcceptsRow(self, row, parent):
if self._root_idx is not None:
model = self.sourceModel()
idx = model.index(row, 0, parent)
if idx == self._root_idx:
return True
if model.isDir(idx):
print("false", model.filePath(idx),
model.filePath(self._root_idx))
return False
return QtCore.QSortFilterProxyModel.filterAcceptsRow(self, row, parent)
def setRootIndex(self, idx):
self._root_idx = idx
class ResultsBrowser(QtWidgets.QSplitter):
def __init__(self, datasets):
def __init__(self, datasets, root=None):
QtWidgets.QSplitter.__init__(self)
if root is None:
root = QtCore.QDir.currentPath()
self.datasets = datasets
self.model = QtWidgets.QFileSystemModel()
self.model.setRootPath(QtCore.QDir.currentPath())
self.model.setNameFilters(["*.h5"])
self.model.setNameFilterDisables(False)
self.model.setIconProvider(ResultIconProvider())
self.rt_model = QtWidgets.QFileSystemModel()
self.rt_model.setFilter(QtCore.QDir.NoDotAndDotDot |
QtCore.QDir.AllDirs)
self.rt = QtWidgets.QTreeView()
self.rt_model = DirsOnly()
self.rt_model.setSourceModel(self.model)
self.rt.setModel(self.rt_model)
self.rt.setRootIndex(self.rt_model.mapFromSource(self.model.index(
QtCore.QDir.currentPath())))
self.rt.setRootIndex(self.rt_model.setRootPath(root))
self.rt.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.rt.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
self.rt.selectionModel().currentChanged.connect(
@ -78,37 +50,34 @@ class ResultsBrowser(QtWidgets.QSplitter):
self.addWidget(self.rt)
self.rl = QtWidgets.QListView()
self.rl_model = FilesOnly()
self.rl_model.setSourceModel(self.model)
self.rl.setModel(self.rl_model)
self.rl.setViewMode(QtWidgets.QListView.IconMode)
l = QtGui.QFontMetrics(self.font()).lineSpacing()
self.rl.setIconSize(QtCore.QSize(20*l, 15*l))
self.rl.setFlow(QtWidgets.QListView.LeftToRight)
self.rl.setWrapping(True)
self.rl.selectionModel().currentChanged.connect(
self.list_current_changed)
self.tree_current_changed(self.rt.currentIndex(), None)
self.addWidget(self.rl)
def showEvent(self, ev):
if hasattr(self, "_shown"):
return
self._shown = True
self.rt.hideColumn(1)
self.rt.hideColumn(2)
self.rt.hideColumn(3)
self.rt.scrollTo(self.rt.selectionModel().currentIndex())
def tree_current_changed(self, current, previous):
i = self.rt_model.mapToSource(current)
self.rl_model.setRootIndex(i)
j = self.rl_model.mapFromSource(i)
print("root", self.model.filePath(i),
i.isValid())
self.rl.setRootIndex(j)
path = self.rt_model.filePath(current)
# we need to create a new model here. the old one remembers side effect
self.rl_model = QtWidgets.QFileSystemModel()
self.rl_model.setFilter(QtCore.QDir.Files)
self.rl_model.setNameFilters(["*.h5"])
self.rl_model.setNameFilterDisables(False)
self.rl_model.setIconProvider(ResultIconProvider())
self.rl.setModel(self.rl_model)
self.rl.setRootIndex(self.rl_model.setRootPath(path))
self.rl.selectionModel().currentChanged.connect(
self.list_current_changed)
def list_current_changed(self, current, previous):
info = self.model.fileInfo(self.rl_model.mapToSource(current))
info = self.rl_model.fileInfo(current)
logger.info("opening %s", info.filePath())
if not (info.isFile() and info.isReadable() and
info.suffix() == "h5"):
@ -123,16 +92,16 @@ class ResultsBrowser(QtWidgets.QSplitter):
self.datasets.init(rd)
def select(self, path):
idx = self.rt_model.mapFromSource(self.model.index(path))
self.rt.scrollTo(idx)
idx = self.rt_model.index(os.path.dirname(path))
self.rt.expand(idx)
self.rt.selectionModel().setCurrentIndex(
idx, QtCore.QItemSelectionModel.ClearAndSelect)
self.rt.scrollTo(idx)
self.rt.setCurrentIndex(idx)
# rl root is signaled
self.rl.setCurrentIndex(self.rl_model.index(path))
def save_state(self):
return {
"selected": self.model.filePath(self.rt_model.mapToSource(
self.rt.selectionModel().currentIndex())),
"selected": self.rl_model.filePath(self.rt.currentIndex()),
"header": bytes(self.rt.header().saveState()),
"splitter": bytes(self.saveState()),
}