browser: handle only known errors, cleanup

This commit is contained in:
Robert Jördens 2016-04-06 15:45:13 +08:00
parent 506b5ec4b0
commit 762e0400c0
1 changed files with 32 additions and 14 deletions

View File

@ -1,4 +1,5 @@
import logging import logging
import threading
import h5py import h5py
from PyQt5 import QtCore, QtWidgets, QtGui from PyQt5 import QtCore, QtWidgets, QtGui
@ -6,17 +7,26 @@ from PyQt5 import QtCore, QtWidgets, QtGui
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class ResultError(Exception):
pass
class ResultIconProvider(QtWidgets.QFileIconProvider): class ResultIconProvider(QtWidgets.QFileIconProvider):
def icon(self, info): def icon(self, info):
if not (info.isFile() and info.isReadable() and info.suffix() == "h5"):
return QtWidgets.QFileIconProvider.icon(self, info)
try: try:
if not (info.isFile() and info.isReadable() and
info.suffix() == "h5"):
raise ResultError
with h5py.File(info.filePath(), "r") as f: with h5py.File(info.filePath(), "r") as f:
if "thumbnail" not in f:
raise ResultError
d = f["thumbnail"] d = f["thumbnail"]
if "extension" not in d.attrs:
raise ResultError
img = QtGui.QImage.fromData(d.value, d.attrs["extension"]) img = QtGui.QImage.fromData(d.value, d.attrs["extension"])
pix = QtGui.QPixmap.fromImage(img) pix = QtGui.QPixmap.fromImage(img)
return QtGui.QIcon(pix) return QtGui.QIcon(pix)
except: except ResultError:
return QtWidgets.QFileIconProvider.icon(self, info) return QtWidgets.QFileIconProvider.icon(self, info)
@ -30,15 +40,16 @@ class ResultsBrowser(QtWidgets.QSplitter):
self.rt_model.setRootPath(QtCore.QDir.currentPath()) self.rt_model.setRootPath(QtCore.QDir.currentPath())
self.rt_model.setNameFilters(["*.h5"]) self.rt_model.setNameFilters(["*.h5"])
self.rt_model.setNameFilterDisables(False) self.rt_model.setNameFilterDisables(False)
self.rt_model.setIconProvider(ResultIconProvider()) self.icon_provider = ResultIconProvider()
self.rt_model.setIconProvider(self.icon_provider)
self.rt = QtWidgets.QTreeView() self.rt = QtWidgets.QTreeView()
self.rt.setModel(self.rt_model) self.rt.setModel(self.rt_model)
self.rt.setRootIndex(self.rt_model.index(QtCore.QDir.currentPath())) self.rt.setRootIndex(self.rt_model.index(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().selectionChanged.connect( self.rt.selectionModel().currentChanged.connect(
self.selection_changed) self.current_changed)
self.rt.setRootIsDecorated(False) self.rt.setRootIsDecorated(False)
self.addWidget(self.rt) self.addWidget(self.rt)
@ -49,6 +60,8 @@ class ResultsBrowser(QtWidgets.QSplitter):
self.rl.setRootIndex(self.rt.rootIndex()) self.rl.setRootIndex(self.rt.rootIndex())
l = QtGui.QFontMetrics(self.font()).lineSpacing() l = QtGui.QFontMetrics(self.font()).lineSpacing()
self.rl.setIconSize(QtCore.QSize(20*l, 20*l)) self.rl.setIconSize(QtCore.QSize(20*l, 20*l))
self.rl.setWrapping(True)
self.rl.setFlow(QtWidgets.QListView.LeftToRight)
self.addWidget(self.rl) self.addWidget(self.rl)
def showEvent(self, ev): def showEvent(self, ev):
@ -60,20 +73,25 @@ class ResultsBrowser(QtWidgets.QSplitter):
self.rt.hideColumn(3) self.rt.hideColumn(3)
self.rt.scrollTo(self.rt.selectionModel().currentIndex()) self.rt.scrollTo(self.rt.selectionModel().currentIndex())
def selection_changed(self, selected, deselected): def cleanup(self):
indexes = selected.indexes() pass
if not indexes:
return def current_changed(self, current, previous):
path = self.rt_model.filePath(indexes[0]) info = self.rt_model.fileInfo(current)
logger.info("opening %s", path) logger.info("opening %s", info.filePath())
try: try:
with h5py.File(path, "r") as f: if not (info.isFile() and info.isReadable() and
info.suffix() == "h5"):
raise ResultError
with h5py.File(info.filePath(), "r") as f:
rd = {} rd = {}
if "datasets" not in f:
raise ResultError
group = f["datasets"] group = f["datasets"]
for k in group: for k in group:
rd[k] = True, group[k].value rd[k] = True, group[k].value
self.datasets.init(rd) self.datasets.init(rd)
except: except ResultError:
pass pass
def select(self, path): def select(self, path):