diff options
author | Picca Frédéric-Emmanuel <picca@debian.org> | 2018-03-04 10:20:27 +0100 |
---|---|---|
committer | Picca Frédéric-Emmanuel <picca@debian.org> | 2018-03-04 10:20:27 +0100 |
commit | 270d5ddc31c26b62379e3caa9044dd75ccc71847 (patch) | |
tree | 55c5bfc851dfce7172d335cd2405b214323e3caf /silx/gui/hdf5/Hdf5TreeView.py | |
parent | e19c96eff0c310c06c4f268c8b80cb33bd08996f (diff) |
New upstream version 0.7.0+dfsg
Diffstat (limited to 'silx/gui/hdf5/Hdf5TreeView.py')
-rw-r--r-- | silx/gui/hdf5/Hdf5TreeView.py | 113 |
1 files changed, 45 insertions, 68 deletions
diff --git a/silx/gui/hdf5/Hdf5TreeView.py b/silx/gui/hdf5/Hdf5TreeView.py index 0a4198e..78b5c19 100644 --- a/silx/gui/hdf5/Hdf5TreeView.py +++ b/silx/gui/hdf5/Hdf5TreeView.py @@ -25,7 +25,7 @@ __authors__ = ["V. Valls"] __license__ = "MIT" -__date__ = "20/09/2017" +__date__ = "20/02/2018" import logging @@ -114,12 +114,12 @@ class Hdf5TreeView(qt.QTreeView): callback(event) except KeyboardInterrupt: raise - except: + except Exception: # make sure no user callback crash the application _logger.error("Error while calling callback", exc_info=True) pass - if len(menu.children()) > 0: + if not menu.isEmpty(): for action in actions: menu.addAction(action) menu.popup(self.viewport().mapToGlobal(pos)) @@ -194,6 +194,38 @@ class Hdf5TreeView(qt.QTreeView): continue yield _utils.H5Node(item) + def __intermediateModels(self, index): + """Returns intermediate models from the view model to the + model of the index.""" + models = [] + targetModel = index.model() + model = self.model() + while model is not None: + if model is targetModel: + # found + return models + models.append(model) + if isinstance(model, qt.QAbstractProxyModel): + model = model.sourceModel() + else: + break + raise RuntimeError("Model from the requested index is not reachable from this view") + + def mapToModel(self, index): + """Map an index from any model reachable by the view to an index from + the very first model connected to the view. + + :param qt.QModelIndex index: Index from the Hdf5Tree model + :rtype: qt.QModelIndex + :return: Index from the model connected to the view + """ + if not index.isValid(): + return index + models = self.__intermediateModels(index) + for model in reversed(models): + index = model.mapFromSource(index) + return index + def setSelectedH5Node(self, h5Object): """ Select the specified node of the tree using an h5py node. @@ -203,77 +235,22 @@ class Hdf5TreeView(qt.QTreeView): - If the item is not found, the selection do not change. - A none argument allow to deselect everything - :param h5py.Npde h5Object: The node to select + :param h5py.Node h5Object: The node to select """ if h5Object is None: self.setCurrentIndex(qt.QModelIndex()) return - filename = h5Object.file.filename - - # Seach for the right roots - rootIndices = [] - model = self.model() - for index in range(model.rowCount(qt.QModelIndex())): - index = model.index(index, 0, qt.QModelIndex()) - obj = model.data(index, Hdf5TreeModel.H5PY_OBJECT_ROLE) - if obj.file.filename == filename: - # We can have many roots with different subtree of the same - # root - rootIndices.append(index) - - if len(rootIndices) == 0: - # No root found - return - - path = h5Object.name + "/" - path = path.replace("//", "/") - - # Search for the right node - found = False - foundIndices = [] - for _ in range(1000 * len(rootIndices)): - # Avoid too much iterations, in case of recurssive links - if len(foundIndices) == 0: - if len(rootIndices) == 0: - # Nothing found - break - # Start fron a new root - foundIndices.append(rootIndices.pop(0)) - - obj = model.data(index, Hdf5TreeModel.H5PY_OBJECT_ROLE) - p = obj.name + "/" - p = p.replace("//", "/") - if path == p: - found = True - break - - parentIndex = foundIndices[-1] - for index in range(model.rowCount(parentIndex)): - index = model.index(index, 0, parentIndex) - obj = model.data(index, Hdf5TreeModel.H5PY_OBJECT_ROLE) - - p = obj.name + "/" - p = p.replace("//", "/") - if path == p: - foundIndices.append(index) - found = True - break - elif path.startswith(p): - foundIndices.append(index) - break - else: - # Nothing found, start again with another root - foundIndices = [] - - if found: - break - - if found: + model = self.findHdf5TreeModel() + index = model.indexFromH5Object(h5Object) + index = self.mapToModel(index) + if index.isValid(): # Update the GUI - for index in foundIndices[:-1]: - self.expand(index) - self.setCurrentIndex(foundIndices[-1]) + i = index + while i.isValid(): + self.expand(i) + i = i.parent() + self.setCurrentIndex(index) def mousePressEvent(self, event): """Override mousePressEvent to provide a consistante compatible API |