forked from M-Labs/artiq
1
0
Fork 0
artiq/artiq/browser/results.py

102 lines
3.5 KiB
Python
Raw Normal View History

2016-04-04 23:37:51 +08:00
import logging
2016-04-05 15:51:04 +08:00
import h5py
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)
class ResultsBrowser(QtWidgets.QSplitter):
2016-04-05 15:51:04 +08:00
def __init__(self, datasets):
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)
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()
self.rl.setViewMode(QtWidgets.QListView.IconMode)
2016-04-05 16:05:53 +08:00
self.rl.setModel(self.rt_model)
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 = {}
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(
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()),
"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))
splitter = state.get("splitter")
if splitter:
self.restoreState(QtCore.QByteArray(splitter))