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