File indexing completed on 2024-05-12 15:27:47

0001 /***************************************************************************
0002     File                 : HDF5OptionsWidget.cpp
0003     Project              : LabPlot
0004     Description          : widget providing options for the import of HDF5 data
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2015-2017 Stefan Gerlach (stefan.gerlach@uni.kn)
0007 ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *  This program is free software; you can redistribute it and/or modify   *
0012  *  it under the terms of the GNU General Public License as published by   *
0013  *  the Free Software Foundation; either version 2 of the License, or      *
0014  *  (at your option) any later version.                                    *
0015  *                                                                         *
0016  *  This program is distributed in the hope that it will be useful,        *
0017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0019  *  GNU General Public License for more details.                           *
0020  *                                                                         *
0021  *   You should have received a copy of the GNU General Public License     *
0022  *   along with this program; if not, write to the Free Software           *
0023  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0024  *   Boston, MA  02110-1301  USA                                           *
0025  *                                                                         *
0026  ***************************************************************************/
0027 #include "HDF5OptionsWidget.h"
0028 #include "ImportFileWidget.h"
0029 #include "backend/datasources/filters/HDF5Filter.h"
0030 #include "backend/lib/macros.h"
0031 
0032  /*!
0033     \class HDF5OptionsWidget
0034     \brief Widget providing options for the import of HDF5 data
0035 
0036     \ingroup kdefrontend
0037  */
0038 HDF5OptionsWidget::HDF5OptionsWidget(QWidget* parent, ImportFileWidget* fileWidget) : QWidget(parent), m_fileWidget(fileWidget) {
0039     ui.setupUi(parent);
0040 
0041     QStringList hdf5headers;
0042     hdf5headers << i18n("Name") << i18n("Link") << i18n("Type") << i18n("Properties") << i18n("Attributes");
0043     ui.twContent->setHeaderLabels(hdf5headers);
0044     ui.twContent->setAlternatingRowColors(true);
0045     // link and type column are hidden
0046     ui.twContent->hideColumn(1);
0047     ui.twContent->hideColumn(2);
0048     ui.twContent->setSelectionMode(QAbstractItemView::ExtendedSelection);
0049     ui.twContent->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
0050     ui.twPreview->setEditTriggers(QAbstractItemView::NoEditTriggers);
0051 
0052     ui.bRefreshPreview->setIcon( QIcon::fromTheme("view-refresh") );
0053 
0054     connect(ui.twContent, &QTreeWidget::itemSelectionChanged, this, &HDF5OptionsWidget::hdf5TreeWidgetSelectionChanged);
0055     connect(ui.bRefreshPreview, &QPushButton::clicked, fileWidget, &ImportFileWidget::refreshPreview);
0056 }
0057 
0058 void HDF5OptionsWidget::clear() {
0059     ui.twContent->clear();
0060     ui.twPreview->clear();
0061 }
0062 
0063 void HDF5OptionsWidget::updateContent(HDF5Filter* filter, const QString& fileName) {
0064     ui.twContent->clear();
0065     QTreeWidgetItem *rootItem = ui.twContent->invisibleRootItem();
0066     filter->parse(fileName, rootItem);
0067 
0068     ui.twContent->insertTopLevelItem(0, rootItem);
0069     ui.twContent->expandAll();
0070     ui.twContent->resizeColumnToContents(0);
0071     ui.twContent->resizeColumnToContents(3);
0072 }
0073 
0074 /*!
0075     updates the selected data set of a HDF5 file when a new tree widget item is selected
0076 */
0077 void HDF5OptionsWidget::hdf5TreeWidgetSelectionChanged() {
0078     DEBUG("hdf5TreeWidgetSelectionChanged()");
0079     auto items = ui.twContent->selectedItems();
0080     QDEBUG("SELECTED ITEMS =" << items);
0081 
0082     if (items.isEmpty())
0083         return;
0084 
0085     QTreeWidgetItem* item = items.first();
0086     if (item->data(2, Qt::DisplayRole).toString() == i18n("data set"))
0087         m_fileWidget->refreshPreview();
0088     else
0089         DEBUG("non data set selected in HDF5 tree widget");
0090 }
0091 
0092 /*!
0093     return list of selected HDF5 item names
0094     selects all items if nothing is selected
0095 */
0096 const QStringList HDF5OptionsWidget::selectedNames() const {
0097     DEBUG("HDF5OptionsWidget::selectedNames()");
0098     QStringList names;
0099 
0100     if (ui.twContent->selectedItems().size() == 0)
0101         ui.twContent->selectAll();
0102 
0103     // the data link is saved in the second column
0104     for (auto* item : ui.twContent->selectedItems())
0105         names << item->text(1);
0106     QDEBUG("    HDF5OptionsWidget: selected names =" << names);
0107 
0108     return names;
0109 }