1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %flair:license}
|
---|
5 | #include "DataPlot1D.h"
|
---|
6 | #include "ConnectionLayout.h"
|
---|
7 | #include "Layout.h"
|
---|
8 | #include <QMouseEvent>
|
---|
9 | #include <QMenu>
|
---|
10 | #include <QGridLayout>
|
---|
11 | #include <QTime>
|
---|
12 | #include <qendian.h>
|
---|
13 |
|
---|
14 | DataPlot1D::DataPlot1D(Layout *parent, int row, int col, QString title,
|
---|
15 | float ymin, float ymax, bool enabled, uint16_t period, uint16_t nb_buffering)
|
---|
16 | : ScopeFixedStep(title, ymin, ymax, period / 1000.),
|
---|
17 | DataRemote(title, "DataPlot1D", parent, enabled, period, nb_buffering) {
|
---|
18 |
|
---|
19 | setEnabled(enabled);
|
---|
20 | ResetElapsedTime((float)connectionLayout()->GetStartTime().msecsTo(QTime::currentTime())/1000.);
|
---|
21 | parent->addWidget(this, row, col);
|
---|
22 | visible_widget = this;
|
---|
23 | }
|
---|
24 |
|
---|
25 | DataPlot1D::~DataPlot1D() {
|
---|
26 | visible_widget = NULL; // because otherwise xmlwidget will delete it
|
---|
27 | }
|
---|
28 |
|
---|
29 | void DataPlot1D::XmlEvent(QDomElement *dom) {
|
---|
30 | bool previouslyEnabled=IsEnabled();
|
---|
31 |
|
---|
32 | if (dom->attribute("curve") != "") {
|
---|
33 | QString type = dom->attribute("type");
|
---|
34 | int r = dom->attribute("r").toInt();
|
---|
35 | int g = dom->attribute("g").toInt();
|
---|
36 | int b = dom->attribute("b").toInt();
|
---|
37 | QString name = dom->attribute("curve");
|
---|
38 | addCurve(QPen(QColor(r, g, b, 255)), name);
|
---|
39 | datas_type.append(type);
|
---|
40 | if (type == "float") {
|
---|
41 | receivesize += sizeof(float);
|
---|
42 | } else if (type == "int8_t") {
|
---|
43 | receivesize += sizeof(int8_t);
|
---|
44 | } else if (type == "int16_t") {
|
---|
45 | receivesize += sizeof(int16_t);
|
---|
46 | } else {
|
---|
47 | fprintf(stderr,"MyDataPlot1D::addCurve unknown type %s\n",
|
---|
48 | type.toLocal8Bit().constData());
|
---|
49 | }
|
---|
50 | } else {
|
---|
51 | XmlSetup(dom);
|
---|
52 | }
|
---|
53 |
|
---|
54 | if(previouslyEnabled!=IsEnabled() && IsEnabled())
|
---|
55 | ResetElapsedTime((float)connectionLayout()->GetStartTime().msecsTo(QTime::currentTime())/1000.);
|
---|
56 | }
|
---|
57 |
|
---|
58 | bool DataPlot1D::eventFilter(QObject *o, QEvent *e) {
|
---|
59 | if (o == canvas()) {
|
---|
60 | switch (e->type()) {
|
---|
61 | case QEvent::Resize: {
|
---|
62 | // resolution bug taille widgets:
|
---|
63 | setMaximumHeight(parentWidget()->height() /
|
---|
64 | ((QGridLayout *)(parentWidget()->layout()))->rowCount());
|
---|
65 | break;
|
---|
66 | }
|
---|
67 | case QEvent::MouseButtonPress: {
|
---|
68 | return mouseEvent((QMouseEvent *)e);
|
---|
69 | }
|
---|
70 |
|
---|
71 | default:
|
---|
72 | break;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | return Scope::eventFilter(o, e);
|
---|
76 | }
|
---|
77 |
|
---|
78 | void DataPlot1D::BufEvent(char **buf, int *buf_size, uint16_t period,
|
---|
79 | bool big_endian) {
|
---|
80 | setEnabled(IsEnabled());
|
---|
81 | if (IsEnabled() == false || RefreshRate_ms() != period)
|
---|
82 | return;
|
---|
83 | double *datas = (double *)malloc(datas_type.count() * sizeof(double));
|
---|
84 |
|
---|
85 | for (int i = 0; i < datas_type.count(); i++) {
|
---|
86 | if (datas_type.at(i) == "float") {
|
---|
87 | uint32_t data_raw;
|
---|
88 | float *data = (float *)&data_raw;
|
---|
89 | memcpy((void *)&data_raw, *buf, sizeof(uint32_t));
|
---|
90 | *buf += sizeof(uint32_t);
|
---|
91 | if (big_endian == true)
|
---|
92 | data_raw = qFromBigEndian(data_raw);
|
---|
93 | datas[i] = *data;
|
---|
94 | } else if (datas_type.at(i) == "int8_t") {
|
---|
95 | int8_t data;
|
---|
96 | memcpy((void *)&data, *buf, sizeof(data));
|
---|
97 | *buf += sizeof(data);
|
---|
98 | datas[i] = data;
|
---|
99 | } else if (datas_type.at(i) == "int16_t") {
|
---|
100 | int16_t data;
|
---|
101 | memcpy((void *)&data, *buf, sizeof(data));
|
---|
102 | *buf += sizeof(data);
|
---|
103 | if (big_endian == true)
|
---|
104 | data = qFromBigEndian(data);
|
---|
105 | datas[i] = data;
|
---|
106 | } else {
|
---|
107 | fprintf(stderr,"DataPlot1D::BufEvent unknown type %s\n",
|
---|
108 | datas_type.at(i).toLocal8Bit().constData());
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | step_s = period / 1000.;
|
---|
113 | plot(datas);
|
---|
114 | free(datas);
|
---|
115 | }
|
---|
116 |
|
---|
117 | // context menu
|
---|
118 | bool DataPlot1D::mouseEvent(QMouseEvent *event) {
|
---|
119 | if (event->button() == Qt::RightButton) {
|
---|
120 | QMenu *menu = new QMenu("nom", (QwtPlot *)this);
|
---|
121 | QAction *resetX, *resetY, *clear,*action;
|
---|
122 |
|
---|
123 | resetX = menu->addAction("reset time view");
|
---|
124 | resetY = menu->addAction("reset y view");
|
---|
125 | menu->addSeparator();
|
---|
126 |
|
---|
127 | appendmenu(menu);
|
---|
128 |
|
---|
129 | menu->addSeparator();
|
---|
130 | clear = menu->addAction("clear graph");
|
---|
131 |
|
---|
132 | action = execmenu((QwtPlot *)this, menu, event->globalPos());
|
---|
133 | delete menu;
|
---|
134 |
|
---|
135 | if (action == resetX)
|
---|
136 | resetXView();
|
---|
137 | if (action == resetY)
|
---|
138 | resetYView();
|
---|
139 |
|
---|
140 | if (action == clear)
|
---|
141 | clearCurves();
|
---|
142 |
|
---|
143 | return true;
|
---|
144 | }
|
---|
145 | return false;
|
---|
146 | }
|
---|