diff options
Diffstat (limited to 'silx/gui/dialog/GroupDialog.py')
-rw-r--r-- | silx/gui/dialog/GroupDialog.py | 161 |
1 files changed, 107 insertions, 54 deletions
diff --git a/silx/gui/dialog/GroupDialog.py b/silx/gui/dialog/GroupDialog.py index 71235d2..217a03c 100644 --- a/silx/gui/dialog/GroupDialog.py +++ b/silx/gui/dialog/GroupDialog.py @@ -26,9 +26,7 @@ tree. .. autoclass:: GroupDialog - :show-inheritance: - :members: - + :members: addFile, addGroup, getSelectedDataUrl, setMode """ from silx.gui import qt @@ -41,31 +39,18 @@ __license__ = "MIT" __date__ = "22/03/2018" -class GroupDialog(qt.QDialog): - """This :class:`QDialog` uses a :class:`silx.gui.hdf5.Hdf5TreeView` to - provide a HDF5 group selection dialog. - - The information identifying the selected node is provided as a - :class:`silx.io.url.DataUrl`. - - Example: - - .. code-block:: python - - dialog = GroupDialog() - dialog.addFile(filepath1) - dialog.addFile(filepath2) +class _Hdf5ItemSelectionDialog(qt.QDialog): + SaveMode = 1 + """Mode used to set the HDF5 item selection dialog to *save* mode. + This adds a text field to type in a new item name.""" - if dialog.exec_(): - print("File path: %s" % dialog.getSelectedDataUrl().file_path()) - print("HDF5 group path : %s " % dialog.getSelectedDataUrl().data_path()) - else: - print("Operation cancelled :(") + LoadMode = 2 + """Mode used to set the HDF5 item selection dialog to *load* mode. + Only existing items of the HDF5 file can be selected in this mode.""" - """ def __init__(self, parent=None): qt.QDialog.__init__(self, parent) - self.setWindowTitle("HDF5 group selection") + self.setWindowTitle("HDF5 item selection") self._tree = Hdf5TreeView(self) self._tree.setSelectionMode(qt.QAbstractItemView.SingleSelection) @@ -76,25 +61,26 @@ class GroupDialog(qt.QDialog): self._model = self._tree.findHdf5TreeModel() self._header = self._tree.header() - self._header.setSections([self._model.NAME_COLUMN, - self._model.NODE_COLUMN, - self._model.LINK_COLUMN]) - _labelSubgroup = qt.QLabel(self) - _labelSubgroup.setText("Subgroup name (optional)") - self._lineEditSubgroup = qt.QLineEdit(self) - self._lineEditSubgroup.setToolTip( - "Specify the name of a new subgroup " + self._newItemWidget = qt.QWidget(self) + newItemLayout = qt.QVBoxLayout(self._newItemWidget) + self._labelNewItem = qt.QLabel(self._newItemWidget) + self._labelNewItem.setText("Create new item in selected group (optional):") + self._lineEditNewItem = qt.QLineEdit(self._newItemWidget) + self._lineEditNewItem.setToolTip( + "Specify the name of a new item " "to be created in the selected group.") - self._lineEditSubgroup.textChanged.connect( - self._onSubgroupNameChange) + self._lineEditNewItem.textChanged.connect( + self._onNewItemNameChange) + newItemLayout.addWidget(self._labelNewItem) + newItemLayout.addWidget(self._lineEditNewItem) _labelSelectionTitle = qt.QLabel(self) _labelSelectionTitle.setText("Current selection") self._labelSelection = qt.QLabel(self) self._labelSelection.setStyleSheet("color: gray") self._labelSelection.setWordWrap(True) - self._labelSelection.setText("Select a group") + self._labelSelection.setText("Select an item") buttonBox = qt.QDialogButtonBox() self._okButton = buttonBox.addButton(qt.QDialogButtonBox.Ok) @@ -106,8 +92,7 @@ class GroupDialog(qt.QDialog): vlayout = qt.QVBoxLayout(self) vlayout.addWidget(self._tree) - vlayout.addWidget(_labelSubgroup) - vlayout.addWidget(self._lineEditSubgroup) + vlayout.addWidget(self._newItemWidget) vlayout.addWidget(_labelSelectionTitle) vlayout.addWidget(self._labelSelection) vlayout.addWidget(buttonBox) @@ -117,6 +102,30 @@ class GroupDialog(qt.QDialog): self._selectedUrl = None + def _onSelectionChange(self, old, new): + self._updateUrl() + + def _onNewItemNameChange(self, text): + self._updateUrl() + + def _onActivation(self, idx): + # double-click or enter press + self.accept() + + def setMode(self, mode): + """Set dialog mode DatasetDialog.SaveMode or DatasetDialog.LoadMode + + :param mode: DatasetDialog.SaveMode or DatasetDialog.LoadMode + """ + if mode == self.LoadMode: + # hide "Create new item" field + self._lineEditNewItem.clear() + self._newItemWidget.hide() + elif mode == self.SaveMode: + self._newItemWidget.show() + else: + raise ValueError("Invalid DatasetDialog mode %s" % mode) + def addFile(self, path): """Add a HDF5 file to the tree. All groups it contains will be selectable in the dialog. @@ -133,22 +142,75 @@ class GroupDialog(qt.QDialog): """ self._model.insertH5pyObject(group) + def _updateUrl(self): + nodes = list(self._tree.selectedH5Nodes()) + subgroupName = self._lineEditNewItem.text() + if nodes: + node = nodes[0] + data_path = node.local_name + if subgroupName.lstrip("/"): + if not data_path.endswith("/"): + data_path += "/" + data_path += subgroupName.lstrip("/") + self._selectedUrl = DataUrl(file_path=node.local_filename, + data_path=data_path) + self._okButton.setEnabled(True) + self._labelSelection.setText( + self._selectedUrl.path()) + + def getSelectedDataUrl(self): + """Return a :class:`DataUrl` with a file path and a data path. + Return None if the dialog was cancelled. + + :return: :class:`silx.io.url.DataUrl` object pointing to the + selected HDF5 item. + """ + return self._selectedUrl + + +class GroupDialog(_Hdf5ItemSelectionDialog): + """This :class:`QDialog` uses a :class:`silx.gui.hdf5.Hdf5TreeView` to + provide a HDF5 group selection dialog. + + The information identifying the selected node is provided as a + :class:`silx.io.url.DataUrl`. + + Example: + + .. code-block:: python + + dialog = GroupDialog() + dialog.addFile(filepath1) + dialog.addFile(filepath2) + + if dialog.exec_(): + print("File path: %s" % dialog.getSelectedDataUrl().file_path()) + print("HDF5 group path : %s " % dialog.getSelectedDataUrl().data_path()) + else: + print("Operation cancelled :(") + + """ + def __init__(self, parent=None): + _Hdf5ItemSelectionDialog.__init__(self, parent) + + # customization for groups + self.setWindowTitle("HDF5 group selection") + + self._header.setSections([self._model.NAME_COLUMN, + self._model.NODE_COLUMN, + self._model.LINK_COLUMN]) + def _onActivation(self, idx): - # double-click or enter press + # double-click or enter press: filter for groups nodes = list(self._tree.selectedH5Nodes()) node = nodes[0] if silx.io.is_group(node.h5py_object): self.accept() - def _onSelectionChange(self, old, new): - self._updateUrl() - - def _onSubgroupNameChange(self, text): - self._updateUrl() - def _updateUrl(self): + # overloaded to filter for groups nodes = list(self._tree.selectedH5Nodes()) - subgroupName = self._lineEditSubgroup.text() + subgroupName = self._lineEditNewItem.text() if nodes: node = nodes[0] if silx.io.is_group(node.h5py_object): @@ -166,12 +228,3 @@ class GroupDialog(qt.QDialog): self._selectedUrl = None self._okButton.setEnabled(False) self._labelSelection.setText("Select a group") - - def getSelectedDataUrl(self): - """Return a :class:`DataUrl` with a file path and a data path. - Return None if the dialog was cancelled. - - :return: :class:`silx.io.url.DataUrl` object pointing to the - selected group. - """ - return self._selectedUrl |