browser: use fresh models

This commit is contained in:
Robert Jördens 2016-04-06 22:15:51 +08:00
parent 9685771275
commit 46273455c5
1 changed files with 28 additions and 59 deletions

View File

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