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

0001 /***************************************************************************
0002     File                 : ImportFileWidget.h
0003     Project              : LabPlot
0004     Description          : import file data widget
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2009-2017 by Stefan Gerlach (stefan.gerlach@uni-konstanz.de)
0007     Copyright            : (C) 2009-2019 Alexander Semke (alexander.semke@web.de)
0008     Copyright            : (C) 2017-2018 Fabian Kristof (fkristofszabolcs@gmail.com)
0009     Copyright            : (C) 2018-2019 Kovacs Ferencz (kferike98@gmail.com)
0010 
0011  ***************************************************************************/
0012 
0013 /***************************************************************************
0014  *                                                                         *
0015  *  This program is free software; you can redistribute it and/or modify   *
0016  *  it under the terms of the GNU General Public License as published by   *
0017  *  the Free Software Foundation; either version 2 of the License, or      *
0018  *  (at your option) any later version.                                    *
0019  *                                                                         *
0020  *  This program is distributed in the hope that it will be useful,        *
0021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0023  *  GNU General Public License for more details.                           *
0024  *                                                                         *
0025  *   You should have received a copy of the GNU General Public License     *
0026  *   along with this program; if not, write to the Free Software           *
0027  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0028  *   Boston, MA  02110-1301  USA                                           *
0029  *                                                                         *
0030  ***************************************************************************/
0031 #ifndef IMPORTFILEWIDGET_H
0032 #define IMPORTFILEWIDGET_H
0033 
0034 #include "ui_importfilewidget.h"
0035 #include "backend/datasources/LiveDataSource.h"
0036 #include <memory>
0037 
0038 #ifdef HAVE_MQTT
0039 #include "backend/datasources/MQTTClient.h"
0040 class MQTTSubscriptionWidget;
0041 #endif
0042 
0043 #include <QVector>
0044 
0045 class AbstractFileFilter;
0046 class AsciiOptionsWidget;
0047 class BinaryOptionsWidget;
0048 class HDF5OptionsWidget;
0049 class ImageOptionsWidget;
0050 class NetCDFOptionsWidget;
0051 class FITSOptionsWidget;
0052 class JsonOptionsWidget;
0053 class ROOTOptionsWidget;
0054 class QTableWidget;
0055 class QCompleter;
0056 class QTimer;
0057 class QTreeWidgetItem;
0058 class QStringList;
0059 class KUrlComboBox;
0060 
0061 class ImportFileWidget : public QWidget {
0062     Q_OBJECT
0063 
0064 public:
0065     explicit ImportFileWidget(QWidget*, bool liveDataSource, const QString& fileName = QString());
0066     ~ImportFileWidget() override;
0067 
0068     void showOptions(bool);
0069     void saveSettings(LiveDataSource*) const;
0070     void loadSettings();
0071     AbstractFileFilter::FileType currentFileType() const;
0072     LiveDataSource::SourceType currentSourceType() const;
0073     AbstractFileFilter* currentFileFilter() const;
0074     QString fileName() const;
0075     QString selectedObject() const;
0076     bool isFileEmpty() const;
0077     const QStringList selectedHDF5Names() const;
0078     const QStringList selectedNetCDFNames() const;
0079     const QStringList selectedFITSExtensions() const;
0080     const QStringList selectedROOTNames() const;
0081     void showAsciiHeaderOptions(bool);
0082     void showJsonModel(bool);
0083 
0084     QString host() const;
0085     QString port() const;
0086     QString serialPort() const;
0087     int baudRate() const;
0088 
0089 private:
0090     Ui::ImportFileWidget ui;
0091     void setMQTTVisible(bool);
0092     void updateContent(const QString&);
0093     void initOptionsWidget();
0094     void initSlots();
0095 
0096     std::unique_ptr<AsciiOptionsWidget> m_asciiOptionsWidget;
0097     std::unique_ptr<BinaryOptionsWidget> m_binaryOptionsWidget;
0098     std::unique_ptr<HDF5OptionsWidget> m_hdf5OptionsWidget;
0099     std::unique_ptr<ImageOptionsWidget> m_imageOptionsWidget;
0100     std::unique_ptr<NetCDFOptionsWidget> m_netcdfOptionsWidget;
0101     std::unique_ptr<FITSOptionsWidget> m_fitsOptionsWidget;
0102     std::unique_ptr<JsonOptionsWidget> m_jsonOptionsWidget;
0103     std::unique_ptr<ROOTOptionsWidget> m_rootOptionsWidget;
0104 
0105     mutable std::unique_ptr<AbstractFileFilter> m_currentFilter;
0106 
0107     QTableWidget* m_twPreview{nullptr};
0108     KUrlComboBox* m_cbFileName{nullptr};
0109     const QString& m_fileName;
0110     bool m_fileEmpty{false};
0111     bool m_liveDataSource;
0112     bool m_suppressRefresh{false};
0113 
0114 private slots:
0115     void fileNameChanged(const QString&);
0116     void fileTypeChanged(int = 0);
0117 
0118     void sourceTypeChanged(int);
0119     void updateTypeChanged(int);
0120     void readingTypeChanged(int);
0121 
0122     void saveFilter();
0123     void manageFilters();
0124     void filterChanged(int);
0125     void selectFile();
0126     void fileInfoDialog();
0127     void refreshPreview();
0128 
0129 signals:
0130     void fileNameChanged();
0131     void sourceTypeChanged();
0132     void hostChanged();
0133     void portChanged();
0134     void checkedFitsTableToMatrix(const bool enable);
0135     void error(const QString&);
0136 
0137     friend class HDF5OptionsWidget; // to access refreshPreview()
0138     friend class NetCDFOptionsWidget;   // to access refreshPreview() and others
0139     friend class FITSOptionsWidget;
0140     friend class JsonOptionsWidget;
0141     friend class ROOTOptionsWidget; // to access refreshPreview() and others
0142 
0143 #ifdef HAVE_MQTT
0144 private:
0145     QMqttClient* m_client{nullptr};
0146     QVector<QMqttSubscription*> m_mqttSubscriptions;
0147     QTimer* m_connectTimeoutTimer{nullptr};
0148     QMap<QMqttTopicName, QMqttMessage> m_lastMessage;
0149     QVector<QString> m_subscribedTopicNames;
0150     QVector<QString> m_addedTopics;
0151     QString m_configPath;
0152     bool m_initialisingMQTT{false};
0153     MQTTClient::MQTTWill m_willSettings;
0154     MQTTSubscriptionWidget* m_subscriptionWidget{nullptr};
0155 
0156     void disconnectMqttConnection();
0157 
0158 public:
0159     void saveMQTTSettings(MQTTClient*) const;
0160     bool isMqttValid();
0161 
0162 signals:
0163     void newTopic(const QString&);
0164     void subscriptionsChanged();
0165     void checkFileType();
0166     void updateSubscriptionTree(const QVector<QString>&);
0167     void MQTTClearTopics();
0168 
0169 private slots:
0170     void mqttConnectionChanged();
0171     void onMqttConnect();
0172     void subscribeTopic(const QString&, uint);
0173     void unsubscribeTopic(const QString&, QVector<QTreeWidgetItem*>);
0174     void mqttMessageReceived(const QByteArray&, const QMqttTopicName&);
0175     void mqttSubscriptionMessageReceived(const QMqttMessage& );
0176     void onMqttDisconnect();
0177     void mqttErrorChanged(QMqttClient::ClientError);
0178     void mqttConnectTimeout();
0179     void showMQTTConnectionManager();
0180     void readMQTTConnections();
0181     void showWillSettings();
0182     void enableWill(bool);
0183 #endif
0184 };
0185 
0186 #endif