diff options
Diffstat (limited to 'silx/gui/data/Hdf5TableView.py')
-rw-r--r-- | silx/gui/data/Hdf5TableView.py | 68 |
1 files changed, 56 insertions, 12 deletions
diff --git a/silx/gui/data/Hdf5TableView.py b/silx/gui/data/Hdf5TableView.py index d7c33f3..57d6f7b 100644 --- a/silx/gui/data/Hdf5TableView.py +++ b/silx/gui/data/Hdf5TableView.py @@ -1,7 +1,7 @@ # coding: utf-8 # /*########################################################################## # -# Copyright (c) 2017 European Synchrotron Radiation Facility +# Copyright (c) 2017-2020 European Synchrotron Radiation Facility # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -37,6 +37,7 @@ import functools import os.path import logging import h5py +import numpy from silx.gui import qt import silx.io @@ -265,7 +266,7 @@ class Hdf5TableModel(HierarchicalTableView.HierarchicalTableModel): return cell.span() elif role == self.IsHeaderRole: return cell.isHeader() - elif role == qt.Qt.DisplayRole: + elif role in (qt.Qt.DisplayRole, qt.Qt.EditRole): value = cell.value() if callable(value): try: @@ -287,12 +288,6 @@ class Hdf5TableModel(HierarchicalTableView.HierarchicalTableModel): return cell.data(role) return None - def flags(self, index): - """QAbstractTableModel method to inform the view whether data - is editable or not. - """ - return qt.QAbstractTableModel.flags(self, index) - def isSupportedObject(self, h5pyObject): """ Returns true if the provided object can be modelized using this model. @@ -349,6 +344,16 @@ class Hdf5TableModel(HierarchicalTableView.HierarchicalTableModel): shape = self.__hdf5Formatter.humanReadableShape(dataset) return u"%s = %s" % (shape, size) + def __formatChunks(self, dataset): + """Format the shape""" + chunks = dataset.chunks + if chunks is None: + return "" + shape = " \u00D7 ".join([str(i) for i in chunks]) + sizes = numpy.product(chunks) + text = "%s = %s" % (shape, sizes) + return text + def __initProperties(self): """Initialize the list of available properties according to the defined h5py-like object.""" @@ -418,7 +423,7 @@ class Hdf5TableModel(HierarchicalTableView.HierarchicalTableModel): if hasattr(obj, "shape"): self.__data.addHeaderValueRow("shape", self.__formatShape) if hasattr(obj, "chunks") and obj.chunks is not None: - self.__data.addHeaderValueRow("chunks", lambda x: x.chunks) + self.__data.addHeaderValueRow("chunks", self.__formatChunks) # relative to compression # h5py expose compression, compression_opts but are not initialized @@ -438,8 +443,8 @@ class Hdf5TableModel(HierarchicalTableView.HierarchicalTableModel): self.__data.addRow(pos, hdf5id, name, options, availability) for index in range(dcpl.get_nfilters()): filterId, name, options = self.__getFilterInfo(obj, index) - pos = _CellData(value=index) - hdf5id = _CellData(value=filterId) + pos = _CellData(value=str(index)) + hdf5id = _CellData(value=str(filterId)) name = _CellData(value=name) options = _CellData(value=options) availability = _CellFilterAvailableData(filterId=filterId) @@ -517,12 +522,42 @@ class Hdf5TableModel(HierarchicalTableView.HierarchicalTableModel): self.reset() +class Hdf5TableItemDelegate(HierarchicalTableView.HierarchicalItemDelegate): + """Item delegate the :class:`Hdf5TableView` with read-only text editor""" + + def createEditor(self, parent, option, index): + """See :meth:`QStyledItemDelegate.createEditor`""" + editor = super().createEditor(parent, option, index) + if isinstance(editor, qt.QLineEdit): + editor.setReadOnly(True) + editor.deselect() + editor.textChanged.connect(self.__textChanged, qt.Qt.QueuedConnection) + self.installEventFilter(editor) + return editor + + def __textChanged(self, text): + sender = self.sender() + if sender is not None: + sender.deselect() + + def eventFilter(self, watched, event): + eventType = event.type() + if eventType == qt.QEvent.FocusIn: + watched.selectAll() + qt.QTimer.singleShot(0, watched.selectAll) + elif eventType == qt.QEvent.FocusOut: + watched.deselect() + return super().eventFilter(watched, event) + + class Hdf5TableView(HierarchicalTableView.HierarchicalTableView): """A widget to display metadata about a HDF5 node using a table.""" def __init__(self, parent=None): super(Hdf5TableView, self).__init__(parent) self.setModel(Hdf5TableModel(self)) + self.setItemDelegate(Hdf5TableItemDelegate(self)) + self.setSelectionMode(qt.QAbstractItemView.NoSelection) def isSupportedData(self, data): """ @@ -538,7 +573,9 @@ class Hdf5TableView(HierarchicalTableView.HierarchicalTableView): `silx.gui.hdf5.H5Node` which is needed to display some local path information. """ - self.model().setObject(data) + model = self.model() + + model.setObject(data) header = self.horizontalHeader() if qt.qVersion() < "5.0": setResizeMode = header.setResizeMode @@ -550,3 +587,10 @@ class Hdf5TableView(HierarchicalTableView.HierarchicalTableView): setResizeMode(3, qt.QHeaderView.ResizeToContents) setResizeMode(4, qt.QHeaderView.ResizeToContents) header.setStretchLastSection(False) + + for row in range(model.rowCount()): + for column in range(model.columnCount()): + index = model.index(row, column) + if (index.isValid() and index.data( + HierarchicalTableView.HierarchicalTableModel.IsHeaderRole) is False): + self.openPersistentEditor(index) |