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

0001 /***************************************************************************
0002 File                 : NetCDFOptionsWidget.cpp
0003 Project              : LabPlot
0004 Description          : widget providing options for the import of NetCDF 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 "NetCDFOptionsWidget.h"
0028 #include "ImportFileWidget.h"
0029 #include "backend/datasources/filters/NetCDFFilter.h"
0030 #include "backend/lib/macros.h"
0031 
0032 #include <KUrlComboBox>
0033 
0034  /*!
0035     \class NetCDFOptionsWidget
0036     \brief Widget providing options for the import of NetCDF data
0037 
0038     \ingroup kdefrontend
0039  */
0040 NetCDFOptionsWidget::NetCDFOptionsWidget(QWidget* parent, ImportFileWidget* fileWidget)
0041         : QWidget(parent), m_fileWidget(fileWidget) {
0042     ui.setupUi(parent);
0043 
0044     QStringList headers;
0045     headers << i18n("Name") << i18n("Type") << i18n("Properties") << i18n("Values");
0046     ui.twContent->setHeaderLabels(headers);
0047     // type column is hidden
0048     ui.twContent->hideColumn(1);
0049     ui.twContent->setSelectionMode(QAbstractItemView::ExtendedSelection);
0050     ui.twContent->setAlternatingRowColors(true);
0051     ui.twContent->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
0052     ui.twPreview->setEditTriggers(QAbstractItemView::NoEditTriggers);
0053 
0054     ui.bRefreshPreview->setIcon( QIcon::fromTheme("view-refresh") );
0055 
0056     connect(ui.twContent, &QTreeWidget::itemSelectionChanged, this, &NetCDFOptionsWidget::netcdfTreeWidgetSelectionChanged);
0057     connect(ui.bRefreshPreview, &QPushButton::clicked, fileWidget, &ImportFileWidget::refreshPreview);
0058 }
0059 
0060 void NetCDFOptionsWidget::clear() {
0061     ui.twContent->clear();
0062     ui.twPreview->clear();
0063 }
0064 
0065 void NetCDFOptionsWidget::updateContent(NetCDFFilter *filter, const QString& fileName) {
0066     ui.twContent->clear();
0067 
0068     QTreeWidgetItem *rootItem = ui.twContent->invisibleRootItem();
0069     filter->parse(fileName, rootItem);
0070     ui.twContent->insertTopLevelItem(0, rootItem);
0071     ui.twContent->expandAll();
0072     ui.twContent->resizeColumnToContents(0);
0073     ui.twContent->resizeColumnToContents(2);
0074 }
0075 
0076 /*!
0077     updates the selected var name of a NetCDF file when the tree widget item is selected
0078 */
0079 void NetCDFOptionsWidget::netcdfTreeWidgetSelectionChanged() {
0080     DEBUG("netcdfTreeWidgetSelectionChanged()");
0081     QDEBUG("SELECTED ITEMS =" << ui.twContent->selectedItems());
0082 
0083     if (ui.twContent->selectedItems().isEmpty())
0084         return;
0085 
0086     QTreeWidgetItem* item = ui.twContent->selectedItems().first();
0087     if (item->data(1, Qt::DisplayRole).toString() == "variable")
0088         m_fileWidget->refreshPreview();
0089     else if (item->data(1, Qt::DisplayRole).toString().contains("attribute")) {
0090         // reads attributes (only for preview)
0091         auto filter = static_cast<NetCDFFilter*>(m_fileWidget->currentFileFilter());
0092         QString fileName = m_fileWidget->m_cbFileName->currentText();
0093         QString name = item->data(0, Qt::DisplayRole).toString();
0094         QString varName = item->data(1, Qt::DisplayRole).toString().split(' ')[0];
0095         QDEBUG("name =" << name << "varName =" << varName);
0096 
0097         QString importedText = filter->readAttribute(fileName, name, varName);
0098         DEBUG("importedText =" << STDSTRING(importedText));
0099 
0100         QStringList lineStrings = importedText.split('\n');
0101         int rows = lineStrings.size();
0102         ui.twPreview->setRowCount(rows);
0103         ui.twPreview->setColumnCount(0);
0104         for (int i = 0; i < rows; ++i) {
0105             QStringList lineString = lineStrings[i].split(' ');
0106             int cols = lineString.size();
0107             if (ui.twPreview->columnCount() < cols)
0108                 ui.twPreview->setColumnCount(cols);
0109 
0110             for (int j = 0; j < cols; ++j) {
0111                 auto* item = new QTableWidgetItem();
0112                 item->setText(lineString[j]);
0113                 ui.twPreview->setItem(i, j, item);
0114             }
0115         }
0116     } else
0117         DEBUG("non showable object selected in NetCDF tree widget");
0118 }
0119 
0120 /*!
0121     return list of selected NetCDF item names
0122     selects all items if nothing is selected
0123 */
0124 const QStringList NetCDFOptionsWidget::selectedNames() const {
0125     DEBUG("NetCDFOptionsWidget::selectedNames()");
0126     QStringList names;
0127 
0128     if (ui.twContent->selectedItems().size() == 0)
0129         ui.twContent->selectAll();
0130 
0131     for (auto* item : ui.twContent->selectedItems())
0132         names << item->text(0);
0133     QDEBUG("    NetCDFOptionsWidget: selected names =" << names);
0134 
0135     return names;
0136 }