labplot  2.8.2
About: LabPlot is an application for plotting and analysis of 2D and 3D functions and data. It is a complete rewrite of LabPlot1 and lacks in the first release a lot of features available in the predecessor. On the other hand, the GUI and the usability is more superior.
  Fossies Dox: labplot-2.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

LiveDataSource.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : LiveDataSource.h
3  Project : LabPlot
4  Description : File data source
5  --------------------------------------------------------------------
6  Copyright : (C) 2017 Fabian Kristof (fkristofszabolcs@gmail.com)
7  Copyright : (C) 2017-2018 Alexander Semke (alexander.semke@web.de)
8  Copyright : (C) 2018 Stefan Gerlach (stefan.gerlach@uni.kn)
9 
10  ***************************************************************************/
11 
12 /***************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  * This program is distributed in the hope that it will be useful, *
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22  * GNU General Public License for more details. *
23  * *
24  * You should have received a copy of the GNU General Public License *
25  * along with this program; if not, write to the Free Software *
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
27  * Boston, MA 02110-1301 USA *
28  * *
29  ***************************************************************************/
30 #ifndef LIVEDATASOURCE_H
31 #define LIVEDATASOURCE_H
32 
34 #include "backend/matrix/Matrix.h"
35 
36 #include <QLocalSocket>
37 #include <QTimer>
38 #include <QVector>
39 #include <QMap>
40 #ifdef HAVE_QTSERIALPORT
41 #include <QSerialPort>
42 #endif
43 
44 class QString;
45 class AbstractFileFilter;
46 class QFileSystemWatcher;
47 class QAction;
48 class QTcpSocket;
49 class QUdpSocket;
50 
51 class LiveDataSource : public Spreadsheet {
52  Q_OBJECT
53  Q_ENUMS(SourceType)
54  Q_ENUMS(UpdateType)
55  Q_ENUMS(ReadingType)
56 
57 public:
58  enum class SourceType {
59  FileOrPipe = 0, // regular file or pipe
60  NetworkTcpSocket, // TCP socket
61  NetworkUdpSocket, // UDP socket
62  LocalSocket, // local socket
63  SerialPort, // serial port
64  MQTT
65  };
66 
67  enum class UpdateType {
68  TimeInterval = 0, // update periodically using given interval
69  NewData // update when new data is available
70  };
71 
72  enum class ReadingType {
73  ContinuousFixed = 0, // read continuously sampleSize number of samples (lines)
74  FromEnd, // read sampleSize number of samples (lines) from end
75  TillEnd, // read until the end
76  WholeFile // reread whole file
77  };
78 
79  explicit LiveDataSource(const QString& name, bool loading = false);
80  ~LiveDataSource() override;
81 
82  static QStringList supportedBaudRates();
83  static QStringList availablePorts();
84 
87 
88  UpdateType updateType() const;
90 
91  SourceType sourceType() const;
93 
94  ReadingType readingType() const;
96 
97  int sampleSize() const;
98  void setSampleSize(int);
99 
100  void setBytesRead(qint64 bytes);
101  int bytesRead() const;
102 
103  int port() const;
104  void setPort(quint16);
105 
106  bool isPaused() const;
107 
108  void setSerialPort(const QString& name);
109  QString serialPortName() const;
110 
111  QString host() const;
112  void setHost(const QString&);
113 
114  int baudRate() const;
115  void setBaudRate(int);
116 
117  void setUpdateInterval(int);
118  int updateInterval() const;
119 
120  void setKeepNValues(int);
121  int keepNValues() const;
122 
123  void setKeepLastValues(bool);
124  bool keepLastValues() const;
125 
126  void setFileLinked(bool);
127  bool isFileLinked() const;
128 
129  void setUseRelativePath(bool);
130  bool useRelativePath() const;
131 
132  void setFileName(const QString&);
133  QString fileName() const;
134 
135  void setLocalSocketName(const QString&);
136  QString localSocketName() const;
137 
138  void updateNow();
139  void pauseReading();
140  void continueReading();
141 
143  AbstractFileFilter* filter() const;
144 
145  QIcon icon() const override;
146  QMenu* createContextMenu() override;
147  QWidget* view() const override;
148 
149  void save(QXmlStreamWriter*) const override;
150  bool load(XmlStreamReader*, bool preview) override;
151  void finalizeLoad();
152 
153 private:
154  void initActions();
155 
156  QString m_fileName;
157  QString m_dirName;
160  QString m_host;
161 
166 
167  bool m_fileWatched{false};
168  bool m_fileLinked{false};
169  bool m_relativePath{false};
170  bool m_paused{false};
171  bool m_prepared{false};
172  bool m_reading{false};
173  bool m_pending{false};
174 
175  int m_sampleSize{1};
176  int m_keepNValues{0}; // number of values to keep (0 - all)
177  int m_updateInterval{1000};
178  quint16 m_port{1027};
179  int m_baudRate{9600};
180 
181  qint64 m_bytesRead{0};
182 
184 
185  QTimer* m_updateTimer;
186  QTimer* m_watchTimer;
187  QFileSystemWatcher* m_fileSystemWatcher{nullptr};
188 
189  QLocalSocket* m_localSocket{nullptr};
190  QTcpSocket* m_tcpSocket{nullptr};
191  QUdpSocket* m_udpSocket{nullptr};
192 #ifdef HAVE_QTSERIALPORT
193  QSerialPort* m_serialPort{nullptr};
194 #endif
195  QIODevice* m_device{nullptr};
196  QAction* m_plotDataAction{nullptr};
197 
198 public slots:
199  void read();
200  void readOnUpdate();
201 
202 private slots:
203  void plotData();
204  void readyRead();
205 
206  void localSocketError(QLocalSocket::LocalSocketError);
207  void tcpSocketError(QAbstractSocket::SocketError);
208 #ifdef HAVE_QTSERIALPORT
209  void serialPortError(QSerialPort::SerialPortError);
210 #endif
211 };
212 
213 #endif
QString name() const
Interface for the input/output file filters.
Represents data stored in a file. Reading and writing is done with the help of appropriate I/O-filter...
QTimer * m_watchTimer
QUdpSocket * m_udpSocket
void setBytesRead(qint64 bytes)
AbstractFileFilter * m_filter
int keepNValues() const
void setKeepLastValues(bool)
QFileSystemWatcher * m_fileSystemWatcher
void setLocalSocketName(const QString &)
Sets the local socket's server name to name.
QString m_serialPortName
bool useRelativePath() const
void setReadingType(ReadingType)
Sets the source's reading type to readingType.
ReadingType readingType() const
AbstractFileFilter::FileType fileType() const
void setUseRelativePath(bool)
void setUpdateInterval(int)
Sets the source's update interval to interval.
~LiveDataSource() override
bool isFileLinked() const
QString m_localSocketName
void tcpSocketError(QAbstractSocket::SocketError)
int bytesRead() const
QString fileName() const
void setSourceType(SourceType)
Sets the source's type to sourcetype.
static QStringList supportedBaudRates()
Returns a list with the supported baud rates.
QString host() const
QString localSocketName() const
QIODevice * m_device
void setFilter(AbstractFileFilter *)
int updateInterval() const
void setKeepNValues(int)
Sets how many values we should keep when keepLastValues is true.
AbstractFileFilter * filter() const
QWidget * view() const override
Construct a primary view on me.
void save(QXmlStreamWriter *) const override
void setPort(quint16)
Sets the network socket's port to port.
void setFileType(const AbstractFileFilter::FileType)
AbstractFileFilter::FileType m_fileType
void setSerialPort(const QString &name)
Sets the serial port's name to name.
int port() const
LiveDataSource(const QString &name, bool loading=false)
SourceType sourceType() const
bool keepLastValues() const
void updateNow()
Updates this data source at this moment.
void setSampleSize(int)
Sets the sample size to size.
void setHost(const QString &)
Sets the network socket's host.
QMenu * createContextMenu() override
Return AbstractAspect::createContextMenu() plus operations on the primary view.
void localSocketError(QLocalSocket::LocalSocketError)
UpdateType m_updateType
QTcpSocket * m_tcpSocket
QTimer * m_updateTimer
int sampleSize() const
void pauseReading()
Pause the reading of the live data source.
SourceType m_sourceType
QLocalSocket * m_localSocket
bool load(XmlStreamReader *, bool preview) override
void setUpdateType(UpdateType)
Sets the source's update type to updatetype and handles this change.
void setFileName(const QString &)
int baudRate() const
void setBaudRate(int)
Sets the serial port's baud rate.
QString serialPortName() const
bool isPaused() const
static QStringList availablePorts()
Returns a list with the names of the available ports.
void continueReading()
Continue reading from the live data source after it was paused.
QAction * m_plotDataAction
QIcon icon() const override
Return an icon to be used for decorating my views.
void setFileLinked(bool)
ReadingType m_readingType
UpdateType updateType() const
Aspect providing a spreadsheet table with column logic.
Definition: Spreadsheet.h:40
XML stream parser that supports errors as well as warnings. This class also adds line and column numb...