2016-04-04 23:37:51 +08:00
|
|
|
import logging
|
|
|
|
|
2016-04-05 15:51:04 +08:00
|
|
|
import h5py
|
2016-04-05 17:17:02 +08:00
|
|
|
from PyQt5 import QtCore, QtWidgets, QtGui
|
2016-04-04 23:37:51 +08:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-04-06 02:01:25 +08:00
|
|
|
class ResultIconProvider(QtWidgets.QFileIconProvider):
|
|
|
|
def icon(self, info):
|
2016-04-06 18:16:40 +08:00
|
|
|
icon = self.hdf5_thumbnail(info)
|
|
|
|
if icon is None:
|
|
|
|
icon = QtWidgets.QFileIconProvider.icon(self, info)
|
|
|
|
return icon
|
|
|
|
|
|
|
|
def hdf5_thumbnail(self, info):
|
|
|
|
if not (info.isFile() and info.isReadable() and
|
|
|
|
info.suffix() == "h5"):
|
|
|
|
return
|
|
|
|
with h5py.File(info.filePath(), "r") as f:
|
|
|
|
if "thumbnail" not in f:
|
|
|
|
return
|
|
|
|
img = QtGui.QImage.fromData(f["thumbnail"].value)
|
|
|
|
pix = QtGui.QPixmap.fromImage(img)
|
|
|
|
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
|
2016-04-06 02:01:25 +08:00
|
|
|
|
|
|
|
|
2016-04-05 17:17:02 +08:00
|
|
|
class ResultsBrowser(QtWidgets.QSplitter):
|
2016-04-05 15:51:04 +08:00
|
|
|
def __init__(self, datasets):
|
2016-04-05 17:17:02 +08:00
|
|
|
QtWidgets.QSplitter.__init__(self)
|
2016-04-05 15:51:04 +08:00
|
|
|
|
|
|
|
self.datasets = datasets
|
|
|
|
|
2016-04-06 18:16:40 +08:00
|
|
|
self.model = QtWidgets.QFileSystemModel()
|
|
|
|
self.model.setRootPath(QtCore.QDir.currentPath())
|
|
|
|
self.model.setNameFilters(["*.h5"])
|
|
|
|
self.model.setNameFilterDisables(False)
|
|
|
|
self.model.setIconProvider(ResultIconProvider())
|
2016-04-04 23:37:51 +08:00
|
|
|
|
|
|
|
self.rt = QtWidgets.QTreeView()
|
2016-04-06 18:16:40 +08:00
|
|
|
self.rt_model = DirsOnly()
|
|
|
|
self.rt_model.setSourceModel(self.model)
|
2016-04-04 23:37:51 +08:00
|
|
|
self.rt.setModel(self.rt_model)
|
2016-04-06 18:16:40 +08:00
|
|
|
self.rt.setRootIndex(self.rt_model.mapFromSource(self.model.index(
|
|
|
|
QtCore.QDir.currentPath())))
|
2016-04-05 15:51:04 +08:00
|
|
|
self.rt.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
|
|
|
|
self.rt.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
|
2016-04-06 15:45:13 +08:00
|
|
|
self.rt.selectionModel().currentChanged.connect(
|
2016-04-06 18:16:40 +08:00
|
|
|
self.tree_current_changed)
|
2016-04-05 17:17:02 +08:00
|
|
|
self.rt.setRootIsDecorated(False)
|
|
|
|
self.addWidget(self.rt)
|
2016-04-05 16:05:53 +08:00
|
|
|
|
|
|
|
self.rl = QtWidgets.QListView()
|
2016-04-06 18:16:40 +08:00
|
|
|
self.rl_model = FilesOnly()
|
|
|
|
self.rl_model.setSourceModel(self.model)
|
|
|
|
self.rl.setModel(self.rl_model)
|
2016-04-05 17:17:02 +08:00
|
|
|
self.rl.setViewMode(QtWidgets.QListView.IconMode)
|
|
|
|
l = QtGui.QFontMetrics(self.font()).lineSpacing()
|
2016-04-06 18:16:40 +08:00
|
|
|
self.rl.setIconSize(QtCore.QSize(20*l, 15*l))
|
2016-04-06 15:45:13 +08:00
|
|
|
self.rl.setFlow(QtWidgets.QListView.LeftToRight)
|
2016-04-06 18:16:40 +08:00
|
|
|
self.rl.setWrapping(True)
|
|
|
|
self.rl.selectionModel().currentChanged.connect(
|
|
|
|
self.list_current_changed)
|
2016-04-05 17:17:02 +08:00
|
|
|
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())
|
2016-04-04 23:37:51 +08:00
|
|
|
|
2016-04-06 18:16:40 +08:00
|
|
|
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)
|
|
|
|
|
|
|
|
def list_current_changed(self, current, previous):
|
|
|
|
info = self.model.fileInfo(self.rl_model.mapToSource(current))
|
2016-04-06 15:45:13 +08:00
|
|
|
logger.info("opening %s", info.filePath())
|
2016-04-06 18:16:40 +08:00
|
|
|
if not (info.isFile() and info.isReadable() and
|
|
|
|
info.suffix() == "h5"):
|
|
|
|
return
|
|
|
|
with h5py.File(info.filePath(), "r") as f:
|
|
|
|
rd = {}
|
|
|
|
if "datasets" not in f:
|
|
|
|
return
|
|
|
|
group = f["datasets"]
|
|
|
|
for k in group:
|
|
|
|
rd[k] = True, group[k].value
|
|
|
|
self.datasets.init(rd)
|
2016-04-04 23:37:51 +08:00
|
|
|
|
|
|
|
def select(self, path):
|
2016-04-06 18:16:40 +08:00
|
|
|
idx = self.rt_model.mapFromSource(self.model.index(path))
|
|
|
|
self.rt.scrollTo(idx)
|
|
|
|
self.rt.expand(idx)
|
2016-04-04 23:37:51 +08:00
|
|
|
self.rt.selectionModel().setCurrentIndex(
|
2016-04-06 18:16:40 +08:00
|
|
|
idx, QtCore.QItemSelectionModel.ClearAndSelect)
|
2016-04-04 23:37:51 +08:00
|
|
|
|
|
|
|
def save_state(self):
|
|
|
|
return {
|
2016-04-06 18:16:40 +08:00
|
|
|
"selected": self.model.filePath(self.rt_model.mapToSource(
|
|
|
|
self.rt.selectionModel().currentIndex())),
|
2016-04-04 23:37:51 +08:00
|
|
|
"header": bytes(self.rt.header().saveState()),
|
2016-04-05 17:17:02 +08:00
|
|
|
"splitter": bytes(self.saveState()),
|
2016-04-04 23:37:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
def restore_state(self, state):
|
|
|
|
selected = state.get("selected")
|
|
|
|
if selected:
|
|
|
|
self.select(selected)
|
|
|
|
header = state.get("header")
|
|
|
|
if header:
|
|
|
|
self.rt.header().restoreState(QtCore.QByteArray(header))
|
2016-04-05 17:17:02 +08:00
|
|
|
splitter = state.get("splitter")
|
|
|
|
if splitter:
|
|
|
|
self.restoreState(QtCore.QByteArray(splitter))
|