diff --git a/src/silx/app/view/Viewer.py b/src/silx/app/view/Viewer.py index 5ff81e8644..a88b13ada3 100644 --- a/src/silx/app/view/Viewer.py +++ b/src/silx/app/view/Viewer.py @@ -75,7 +75,20 @@ def __init__(self, parent=None, settings=None): self.__dialogState = None self.__customNxDataItem = None - self.__treeview = silx.gui.hdf5.Hdf5TreeView(self) + + # Custom the model to be able to manage the life cycle of the files + self.__treeModelSorted = silx.gui.hdf5.NexusSortFilterProxyModel(self) + self.__treeModelSorted.sort(0, qt.Qt.AscendingOrder) + self.__treeModelSorted.setSortCaseSensitivity(qt.Qt.CaseInsensitive) + + treeModel = silx.gui.hdf5.Hdf5TreeModel(self.__treeModelSorted, ownFiles=False) + treeModel.sigH5pyObjectLoaded.connect(self.__h5FileLoaded) + treeModel.sigH5pyObjectRemoved.connect(self.__h5FileRemoved) + treeModel.sigH5pyObjectSynchronized.connect(self.__h5FileSynchonized) + treeModel.setDatasetDragEnabled(True) + self.__treeModelSorted.setSourceModel(treeModel) + + self.__treeview = silx.gui.hdf5.Hdf5TreeView(self, model=self.__treeModelSorted) self.__treeview.setExpandsOnDoubleClick(False) """Silx HDF5 TreeView""" @@ -85,21 +98,6 @@ def __init__(self, parent=None, settings=None): self.__displayIt = None self.__treeWindow = self.__createTreeWindow(self.__treeview) - - # Custom the model to be able to manage the life cycle of the files - treeModel = silx.gui.hdf5.Hdf5TreeModel(self.__treeview, ownFiles=False) - treeModel.sigH5pyObjectLoaded.connect(self.__h5FileLoaded) - treeModel.sigH5pyObjectRemoved.connect(self.__h5FileRemoved) - treeModel.sigH5pyObjectSynchronized.connect(self.__h5FileSynchonized) - treeModel.setDatasetDragEnabled(True) - self.__treeModelSorted = silx.gui.hdf5.NexusSortFilterProxyModel( - self.__treeview - ) - self.__treeModelSorted.setSourceModel(treeModel) - self.__treeModelSorted.sort(0, qt.Qt.AscendingOrder) - self.__treeModelSorted.setSortCaseSensitivity(qt.Qt.CaseInsensitive) - - self.__treeview.setModel(self.__treeModelSorted) rightPanel.addWidget(self.__treeWindow) self.__customNxdata = CustomNxdataWidget(self) diff --git a/src/silx/gui/dialog/AbstractDataFileDialog.py b/src/silx/gui/dialog/AbstractDataFileDialog.py index c2d3580c5e..c6f3f06745 100644 --- a/src/silx/gui/dialog/AbstractDataFileDialog.py +++ b/src/silx/gui/dialog/AbstractDataFileDialog.py @@ -586,7 +586,7 @@ def _init(self): self.__fileModel.setReadOnly(True) self.__fileModel.directoryLoaded.connect(self.__directoryLoaded) - self.__dataModel = Hdf5TreeModel(self) + self.__dataModel = Hdf5TreeModel(self, ownFiles=False) self.__createWidgets() self.__initLayout() diff --git a/src/silx/gui/hdf5/Hdf5TreeModel.py b/src/silx/gui/hdf5/Hdf5TreeModel.py index fc0f468eb4..f4118138cd 100644 --- a/src/silx/gui/hdf5/Hdf5TreeModel.py +++ b/src/silx/gui/hdf5/Hdf5TreeModel.py @@ -237,14 +237,15 @@ def __init__(self, parent=None, ownFiles=True): """Store the list of files opened by the model itself.""" # FIXME: It should be managed one by one by Hdf5Item itself - # It is not possible to override the QObject destructor nor - # to access to the content of the Python object with the `destroyed` - # signal cause the Python method was already removed with the QWidget, - # while the QObject still exists. - # We use a static method plus explicit references to objects to - # release. The callback do not use any ref to self. - onDestroy = functools.partial(self._closeFileList, self.__openedFiles) - self.destroyed.connect(onDestroy) + if self.__ownFiles: + # It is not possible to override the QObject destructor nor + # to access to the content of the Python object with the `destroyed` + # signal cause the Python method was already removed with the QWidget, + # while the QObject still exists. + # We use a static method plus explicit references to objects to + # release. The callback do not use any ref to self. + onDestroy = functools.partial(self._closeFileList, self.__openedFiles) + self.destroyed.connect(onDestroy) @staticmethod def _closeFileList(fileList): diff --git a/src/silx/gui/hdf5/Hdf5TreeView.py b/src/silx/gui/hdf5/Hdf5TreeView.py index 3c94b8a465..815f9fca08 100644 --- a/src/silx/gui/hdf5/Hdf5TreeView.py +++ b/src/silx/gui/hdf5/Hdf5TreeView.py @@ -58,7 +58,11 @@ class Hdf5TreeView(qt.QTreeView): to the selected objects. """ - def __init__(self, parent=None): + def __init__( + self, + parent: qt.QWidget | None = None, + model: Hdf5TreeModel | qt.QAbstractProxyModel | None = None, + ): """ Constructor @@ -66,7 +70,8 @@ def __init__(self, parent=None): """ qt.QTreeView.__init__(self, parent) - model = self.createDefaultModel() + if model is None: + model = self.createDefaultModel() self.setModel(model) self.setHeader(Hdf5HeaderView(qt.Qt.Horizontal, self))