Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 71 additions & 8 deletions orgui/app/QScanSelector.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

from silx.gui import qt

from silx.gui.hdf5.Hdf5TreeModel import Hdf5TreeModel
from silx.gui import icons
from silx.gui.plot.AlphaSlider import NamedImageAlphaSlider
# from silx.gui.widgets import HorizontalSliderWithBrowser
Expand Down Expand Up @@ -107,7 +106,7 @@ def __init__(self, parentmainwindow, parent=None):
self.hdfTreeView = silx.gui.hdf5.Hdf5TreeView(self)
self.hdfTreeView.setSortingEnabled(True)
self.hdfTreeView.addContextMenuCallback(self.nexus_treeview_callback)
self.hdf5model = Hdf5TreeModel(self.hdfTreeView, ownFiles=True)
self.hdf5model = qutils.RobustHdf5TreeModel(self.hdfTreeView, ownFiles=True)
self.hdfTreeView.setModel(self.hdf5model)
self.hdfTreeView.setExpandsOnDoubleClick(False)
self.hdf5model.setFileMoveEnabled(True)
Expand Down Expand Up @@ -1139,15 +1138,32 @@ def _onCloseFile(self):
objects = list(self.hdfTreeView.selectedH5Nodes())
if len(objects) > 0:
obj = objects[0]
self.hdf5model.removeH5pyObject(obj.file)
try:
self.hdf5model.removeH5pyObject(obj.file)
except Exception as e:
# reading a file on a disconnected drive raises in h5py, which
# must not terminate orGUI, see issue #23.
logger.error(
"Cannot close the data file properly.",
exc_info=True,
extra={
"title": "Cannot close file",
"description": f"Cannot close the data file properly:\n{e}\n"
"The file might be corrupted. If it is still listed, "
"use 'refresh file' to rebuild the file list.",
"show_dialog": True,
"dialog_level": logging.WARNING,
"parent": self,
},
)

def nexus_treeview_callback(self, event):
objects = list(event.source().selectedH5Nodes())
if len(objects) > 0:
obj = objects[0] # for single selection
menu = event.menu()
action = qt.QAction("Refresh", menu)
action.triggered.connect(lambda: self.hdf5model.synchronizeH5pyObject(obj))
action.triggered.connect(lambda: self._synchronizeH5pyObject(obj))
# menu.addAction(action)
# if obj.ntype is h5py.Dataset:
action = qt.QAction("display data", menu)
Expand All @@ -1162,7 +1178,24 @@ def _onRefreshFileOld(self):
objects = list(self.hdfTreeView.selectedH5Nodes())
if len(objects) > 0:
obj = objects[0]
self._synchronizeH5pyObject(obj)

def _synchronizeH5pyObject(self, obj):
"""Reload a data file in the tree view and report errors."""
try:
self.hdf5model.synchronizeH5pyObject(obj)
except Exception as e:
logger.error(
"Cannot refresh the data file.",
exc_info=True,
extra={
"title": "Cannot refresh file",
"description": f"Cannot refresh the data file:\n{e}",
"show_dialog": True,
"dialog_level": logging.WARNING,
"parent": self,
},
)

def __getRelativePath(self, model, rootIndex, index):
"""Returns a relative path from an index to his rootIndex.
Expand All @@ -1185,11 +1218,28 @@ def __getRelativePath(self, model, rootIndex, index):

def _onRefreshFile(self):
qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor)
try:
self._refreshFileTreeView()
except Exception as e:
# rebuilding the tree view reads all open files, which fails if
# one of them became unavailable.
logger.error(
"Cannot refresh the file tree view.",
exc_info=True,
extra={
"title": "Cannot refresh file",
"description": f"Cannot refresh the file tree view:\n{e}",
"show_dialog": True,
"parent": self,
},
)
finally:
qt.QApplication.restoreOverrideCursor()

def _refreshFileTreeView(self):
selection = self.hdfTreeView.selectionModel()
indexes = selection.selectedIndexes()
if indexes == []:
qt.QApplication.restoreOverrideCursor()
qt.QMessageBox.warning(
self, "No file selected", "Cannot refresh file: No file selected."
)
Expand Down Expand Up @@ -1247,17 +1297,30 @@ def _onRefreshFile(self):

modelnew = self.hdfTreeView.findHdf5TreeModel()
for h5, filename in h5files:
modelnew.insertFile(filename, 0)
try:
modelnew.insertFile(filename, 0)
except Exception as e:
logger.error(
"Cannot reload the data file %s.",
filename,
exc_info=True,
extra={
"title": "Cannot reload file",
"description": f"Cannot reload the data file {filename}:\n{e}",
"show_dialog": True,
"dialog_level": logging.WARNING,
"parent": self,
},
)

# self.__expandNodesFromPaths(self.hdfTreeView, index, paths)
# self.hdf5model.appendFile(filename)
qt.QApplication.restoreOverrideCursor()

def createTreeView(self):
self.hdfTreeView = silx.gui.hdf5.Hdf5TreeView(self)
self.hdfTreeView.setSortingEnabled(True)
self.hdfTreeView.addContextMenuCallback(self.nexus_treeview_callback)
self.hdf5model = Hdf5TreeModel(self.hdfTreeView, ownFiles=True)
self.hdf5model = qutils.RobustHdf5TreeModel(self.hdfTreeView, ownFiles=True)
self.hdfTreeView.setModel(self.hdf5model)
self.hdfTreeView.setExpandsOnDoubleClick(False)
self.hdf5model.setFileMoveEnabled(True)
Expand Down
Loading