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):
|
|
|
|
if not (info.isFile() and info.isReadable() and info.suffix() == "h5"):
|
|
|
|
return QtWidgets.QFileIconProvider.icon(self, info)
|
|
|
|
try:
|
|
|
|
with h5py.File(info.filePath(), "r") as f:
|
|
|
|
d = f["thumbnail"]
|
|
|
|
img = QtGui.QImage.fromData(d.value, d.attrs["extension"])
|
|
|
|
pix = QtGui.QPixmap.fromImage(img)
|
|
|
|
return QtGui.QIcon(pix)
|
|
|
|
except:
|
|
|
|
return QtWidgets.QFileIconProvider.icon(self, info)
|
|
|
|
|
|
|
|
|
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-04 23:37:51 +08:00
|
|
|
self.rt_model = QtWidgets.QFileSystemModel()
|
|
|
|
self.rt_model.setRootPath(QtCore.QDir.currentPath())
|
2016-04-05 15:51:04 +08:00
|
|
|
self.rt_model.setNameFilters(["*.h5"])
|
2016-04-04 23:37:51 +08:00
|
|
|
self.rt_model.setNameFilterDisables(False)
|
2016-04-06 02:01:25 +08:00
|
|
|
self.rt_model.setIconProvider(ResultIconProvider())
|
2016-04-04 23:37:51 +08:00
|
|
|
|
|
|
|
self.rt = QtWidgets.QTreeView()
|
|
|
|
self.rt.setModel(self.rt_model)
|
|
|
|
self.rt.setRootIndex(self.rt_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-05 17:17:02 +08:00
|
|
|
self.rt.selectionModel().selectionChanged.connect(
|
|
|
|
self.selection_changed)
|
|
|
|
self.rt.setRootIsDecorated(False)
|
|
|
|
self.addWidget(self.rt)
|
2016-04-05 16:05:53 +08:00
|
|
|
|
|
|
|
self.rl = QtWidgets.QListView()
|
2016-04-05 17:17:02 +08:00
|
|
|
self.rl.setViewMode(QtWidgets.QListView.IconMode)
|
2016-04-05 16:05:53 +08:00
|
|
|
self.rl.setModel(self.rt_model)
|
2016-04-05 17:17:02 +08:00
|
|
|
self.rl.setSelectionModel(self.rt.selectionModel())
|
|
|
|
self.rl.setRootIndex(self.rt.rootIndex())
|
|
|
|
l = QtGui.QFontMetrics(self.font()).lineSpacing()
|
|
|
|
self.rl.setIconSize(QtCore.QSize(20*l, 20*l))
|
|
|
|
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
|
|
|
|
|
|
|
def selection_changed(self, selected, deselected):
|
|
|
|
indexes = selected.indexes()
|
2016-04-05 15:51:04 +08:00
|
|
|
if not indexes:
|
|
|
|
return
|
|
|
|
path = self.rt_model.filePath(indexes[0])
|
|
|
|
logger.info("opening %s", path)
|
|
|
|
try:
|
|
|
|
with h5py.File(path, "r") as f:
|
|
|
|
rd = {}
|
2016-04-05 18:50:19 +08:00
|
|
|
group = f["datasets"]
|
|
|
|
for k in group:
|
|
|
|
rd[k] = True, group[k].value
|
2016-04-05 15:51:04 +08:00
|
|
|
self.datasets.init(rd)
|
|
|
|
except:
|
|
|
|
pass
|
2016-04-04 23:37:51 +08:00
|
|
|
|
|
|
|
def select(self, path):
|
|
|
|
self.rt.selectionModel().setCurrentIndex(
|
2016-04-05 17:17:02 +08:00
|
|
|
self.rt_model.index(path),
|
2016-04-04 23:37:51 +08:00
|
|
|
QtCore.QItemSelectionModel.ClearAndSelect)
|
|
|
|
|
|
|
|
def save_state(self):
|
|
|
|
return {
|
|
|
|
"selected": self.rt_model.filePath(
|
|
|
|
self.rt.selectionModel().currentIndex()),
|
|
|
|
"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))
|