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

0001 /***************************************************************************
0002     File                 : FITSOptionsWidget.cpp
0003     Project              : LabPlot
0004     Description          : Widget providing options for the import of FITS data
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2016 Fabian Kristof (fkristofszabolcs@gmail.com)
0007     Copyright            : (C) 2017 Stefan Gerlach (stefan.gerlach@uni.kn)
0008 
0009 ***************************************************************************/
0010 
0011 /***************************************************************************
0012 *                                                                         *
0013 *  This program is free software; you can redistribute it and/or modify   *
0014 *  it under the terms of the GNU General Public License as published by   *
0015 *  the Free Software Foundation; either version 2 of the License, or      *
0016 *  (at your option) any later version.                                    *
0017 *                                                                         *
0018 *  This program is distributed in the hope that it will be useful,        *
0019 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0020 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0021 *  GNU General Public License for more details.                           *
0022 *                                                                         *
0023 *   You should have received a copy of the GNU General Public License     *
0024 *   along with this program; if not, write to the Free Software           *
0025 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0026 *   Boston, MA  02110-1301  USA                                           *
0027 *                                                                         *
0028 ***************************************************************************/
0029 
0030 #include "FITSOptionsWidget.h"
0031 #include "ImportFileWidget.h"
0032 #include "backend/datasources/filters/FITSFilter.h"
0033 #include "backend/lib/macros.h"
0034 
0035 FITSOptionsWidget::FITSOptionsWidget(QWidget* parent, ImportFileWidget* fileWidget) : QWidget(parent), m_fileWidget(fileWidget) {
0036     ui.setupUi(parent);
0037 
0038     ui.twExtensions->headerItem()->setText(0, i18n("Content"));
0039     ui.twExtensions->setSelectionMode(QAbstractItemView::SingleSelection);
0040     ui.twExtensions->setAlternatingRowColors(true);
0041     ui.twExtensions->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
0042     ui.twPreview->setEditTriggers(QAbstractItemView::NoEditTriggers);
0043 
0044     ui.bRefreshPreview->setIcon( QIcon::fromTheme("view-refresh") );
0045 
0046     connect(ui.twExtensions, &QTreeWidget::itemSelectionChanged, this, &FITSOptionsWidget::fitsTreeWidgetSelectionChanged);
0047     connect(ui.bRefreshPreview, &QPushButton::clicked, fileWidget, &ImportFileWidget::refreshPreview);
0048 }
0049 
0050 void FITSOptionsWidget::clear() {
0051     ui.twExtensions->clear();
0052     ui.twPreview->clear();
0053 }
0054 
0055 QString FITSOptionsWidget::currentExtensionName() {
0056     QString name;
0057 
0058     if (ui.twExtensions->currentItem() != nullptr && ui.twExtensions->currentItem()->text(0) != i18n("Primary header"))
0059         name = ui.twExtensions->currentItem()->text(ui.twExtensions->currentColumn());
0060 
0061     return name;
0062 }
0063 
0064 void FITSOptionsWidget::updateContent(FITSFilter* filter, const QString& fileName) {
0065     DEBUG("FITSOptionsWidget::updateContent() file name = " << STDSTRING(fileName));
0066     ui.twExtensions->clear();
0067     filter->parseExtensions(fileName, ui.twExtensions, true);
0068     DEBUG("FITSOptionsWidget::updateContent() DONE");
0069 }
0070 
0071 /*!
0072     updates the selected var name of a NetCDF file when the tree widget item is selected
0073 */
0074 //TODO
0075 void FITSOptionsWidget::fitsTreeWidgetSelectionChanged() {
0076     DEBUG("fitsTreeWidgetSelectionChanges()");
0077     QDEBUG("SELECTED ITEMS =" << ui.twExtensions->selectedItems());
0078 
0079     if (ui.twExtensions->selectedItems().isEmpty())
0080         return;
0081 
0082     QTreeWidgetItem* item = ui.twExtensions->selectedItems().first();
0083     int column = ui.twExtensions->currentColumn();
0084 
0085     WAIT_CURSOR;
0086     const QString& itemText = item->text(column);
0087     QString selectedExtension;
0088     int extType = 0;
0089     if (itemText.contains(QLatin1String("IMAGE #")) ||
0090             itemText.contains(QLatin1String("ASCII_TBL #")) ||
0091             itemText.contains(QLatin1String("BINARY_TBL #")))
0092         extType = 1;
0093     else if (!itemText.compare(i18n("Primary header")))
0094         extType = 2;
0095     if (extType == 0) {
0096         if (item->parent() != nullptr) {
0097             if (item->parent()->parent() != nullptr)
0098                 selectedExtension = item->parent()->parent()->text(0) + QLatin1String("[") + item->text(column) + QLatin1String("]");
0099         }
0100     } else if (extType == 1) {
0101         if (item->parent() != nullptr) {
0102             if (item->parent()->parent() != nullptr) {
0103                 bool ok;
0104                 int hduNum = itemText.rightRef(1).toInt(&ok);
0105                 selectedExtension = item->parent()->parent()->text(0) + QLatin1String("[") + QString::number(hduNum-1) + QLatin1String("]");
0106             }
0107         }
0108     } else {
0109         if (item->parent()->parent() != nullptr)
0110             selectedExtension = item->parent()->parent()->text(column);
0111     }
0112 
0113     if (!selectedExtension.isEmpty()) {
0114         auto filter = static_cast<FITSFilter*>(m_fileWidget->currentFileFilter());
0115         bool readFitsTableToMatrix;
0116         const QVector<QStringList> importedStrings = filter->readChdu(selectedExtension, &readFitsTableToMatrix, ui.sbPreviewLines->value());
0117         emit m_fileWidget->checkedFitsTableToMatrix(readFitsTableToMatrix);
0118 
0119         const int rows = importedStrings.size();
0120         ui.twPreview->clear();
0121 
0122         ui.twPreview->setRowCount(rows);
0123         int colCount = 0;
0124         const int maxColumns = 300;
0125         for (int i = 0; i < rows; ++i) {
0126             QStringList lineString = importedStrings[i];
0127             if (i == 0) {
0128                 colCount = lineString.size() > maxColumns ? maxColumns : lineString.size();
0129                 ui.twPreview->setColumnCount(colCount);
0130             }
0131             colCount = lineString.size() > maxColumns ? maxColumns : lineString.size();
0132 
0133             for (int j = 0; j < colCount; ++j) {
0134                 auto* item = new QTableWidgetItem(lineString[j]);
0135                 ui.twPreview->setItem(i, j, item);
0136             }
0137         }
0138         ui.twPreview->resizeColumnsToContents();
0139     }
0140     RESET_CURSOR;
0141 }
0142 
0143 /*!
0144     return list of selected FITS extension names
0145 */
0146 const QStringList FITSOptionsWidget::selectedExtensions() const {
0147     QStringList names;
0148     for (const auto* item : ui.twExtensions->selectedItems())
0149         names << item->text(0);
0150 
0151     return names;
0152 }
0153 
0154 const QString FITSOptionsWidget::extensionName(bool* ok) {
0155     if (ui.twExtensions->currentItem() != nullptr) {
0156         const QTreeWidgetItem* item = ui.twExtensions->currentItem();
0157         const int currentColumn = ui.twExtensions->currentColumn();
0158         QString itemText = item->text(currentColumn);
0159         int extType = 0;
0160         if (itemText.contains(QLatin1String("IMAGE #")) ||
0161                 itemText.contains(QLatin1String("ASCII_TBL #")) ||
0162                 itemText.contains(QLatin1String("BINARY_TBL #")))
0163             extType = 1;
0164         else if (!itemText.compare(i18n("Primary header")))
0165             extType = 2;
0166 
0167         if (extType == 0) {
0168             if (item->parent() != nullptr && item->parent()->parent() != nullptr)
0169                 return item->parent()->parent()->text(0) + QLatin1String("[")+ item->text(currentColumn) + QLatin1String("]");
0170         } else if (extType == 1) {
0171             if (item->parent() != nullptr && item->parent()->parent() != nullptr) {
0172                 int hduNum = itemText.rightRef(1).toInt(ok);
0173                 return item->parent()->parent()->text(0) + QLatin1String("[") + QString::number(hduNum-1) + QLatin1String("]");
0174             }
0175         } else {
0176             if (item->parent()->parent() != nullptr)
0177                 return item->parent()->parent()->text(currentColumn);
0178         }
0179     }
0180 
0181     return QString();
0182 }