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

0001 /***************************************************************************
0002 File                 : MQTTSubscriptionWidget.h
0003 Project              : LabPlot
0004 Description          : manage topics and subscribing
0005 --------------------------------------------------------------------
0006 Copyright            : (C) 2019 by Kovacs Ferencz (kferike98@gmail.com)
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 
0028 #ifndef MQTTSUBSCRIPTIONWIDGET_H
0029 #define MQTTSUBSCRIPTIONWIDGET_H
0030 
0031 #include "ui_mqttsubscriptionwidget.h"
0032 class QMqttSubscription;
0033 
0034 class MQTTSubscriptionWidget : public QWidget {
0035     Q_OBJECT
0036 
0037 public:
0038     explicit MQTTSubscriptionWidget(QWidget* parent = nullptr);
0039     ~MQTTSubscriptionWidget() override;
0040     enum class MQTTParentWidget {
0041         ImportFileWidget,
0042         LiveDataDock
0043     };
0044 
0045     void setTopicList(const QStringList& topicList);
0046     QStringList getTopicList();
0047 
0048     int subscriptionCount();
0049     QTreeWidgetItem* topLevelTopic(int);
0050     QTreeWidgetItem* topLevelSubscription(int);
0051     QTreeWidgetItem* currentItem() const;
0052     void addTopic(QTreeWidgetItem*);
0053     int topicCount();
0054     void setTopicTreeText(const QString&);
0055     void makeVisible(bool);
0056     void testSubscribe(QTreeWidgetItem*);
0057     void testUnsubscribe(QTreeWidgetItem*);
0058 
0059     static bool checkTopicContains(const QString&, const QString&);
0060     static void findSubscriptionLeafChildren(QVector<QTreeWidgetItem*>&, QTreeWidgetItem*);
0061 
0062 signals:
0063     void subscriptionChanged();
0064     void makeSubscription(const QString& name, quint8 QoS);
0065     void MQTTUnsubscribeFromTopic(const QString&, QVector<QTreeWidgetItem*> children);
0066     void removeMQTTSubscription(const QString&);
0067     void addBeforeRemoveSubscription(const QString&, quint8);
0068     void reparentTopic(const QString& topic, const QString& parent);
0069     void enableWill(bool);
0070 
0071 private:
0072     Ui::MQTTSubscriptionWidget ui;
0073     MQTTParentWidget m_parent;
0074     QCompleter* m_subscriptionCompleter{nullptr};
0075     QCompleter* m_topicCompleter{nullptr};
0076     QStringList m_topicList;
0077     bool m_searching{false};
0078     QTimer* m_searchTimer;
0079 
0080     void unsubscribeFromTopic(const QString&);
0081     void manageCommonLevelSubscriptions();
0082     void updateSubscriptionCompleter();
0083 
0084     static void addSubscriptionChildren(QTreeWidgetItem*, QTreeWidgetItem*);
0085     static void restoreSubscriptionChildren(QTreeWidgetItem * topic, QTreeWidgetItem * subscription, const QStringList& list, int level);
0086     static int checkCommonChildCount(int levelIdx, int level, QStringList& namelist, QTreeWidgetItem* currentItem);
0087     static int commonLevelIndex(const QString& first, const QString& second);
0088     static QString checkCommonLevel(const QString&, const QString&);
0089 
0090 private slots:
0091     void mqttAvailableTopicDoubleClicked(QTreeWidgetItem* item, int column);
0092     void mqttSubscribedTopicDoubleClicked(QTreeWidgetItem* item, int column);
0093     void mqttSubscribe();
0094     void mqttUnsubscribe();
0095     void setTopicCompleter(const QString&);
0096     void scrollToTopicTreeItem(const QString& rootName);
0097     void scrollToSubsriptionTreeItem(const QString& rootName);
0098     void topicTimeout();
0099     void updateSubscriptionTree(const QVector<QString>&);
0100     void clearWidgets();
0101     void onDisconnect();
0102 };
0103 
0104 #endif // MQTTSUBSCRIPTIONWIDGET_H