source: flair-src/trunk/tools/FlairGCS/src/DataPlot1D.cpp@ 440

Last change on this file since 440 was 437, checked in by Sanahuja Guillaume, 3 years ago

prepare for graphs buffering

File size: 4.2 KB
RevLine 
[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]14DataPlot1D::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
25DataPlot1D::~DataPlot1D() {
[15]26 visible_widget = NULL; // because otherwise xmlwidget will delete it
[9]27}
28
[269]29void 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
58bool 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
[15]78void 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));
[9]84
[15]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 {
[244]107 fprintf(stderr,"DataPlot1D::BufEvent unknown type %s\n",
[15]108 datas_type.at(i).toLocal8Bit().constData());
[9]109 }
[15]110 }
[9]111
[15]112 step_s = period / 1000.;
113 plot(datas);
[222]114 free(datas);
[9]115}
116
[15]117// context menu
[9]118bool DataPlot1D::mouseEvent(QMouseEvent *event) {
[15]119 if (event->button() == Qt::RightButton) {
120 QMenu *menu = new QMenu("nom", (QwtPlot *)this);
[222]121 QAction *resetX, *resetY, *clear,*action;
[9]122
[222]123 resetX = menu->addAction("reset time view");
124 resetY = menu->addAction("reset y view");
[15]125 menu->addSeparator();
[9]126
[15]127 appendmenu(menu);
[222]128
129 menu->addSeparator();
130 clear = menu->addAction("clear graph");
131
132 action = execmenu((QwtPlot *)this, menu, event->globalPos());
[15]133 delete menu;
[9]134
[222]135 if (action == resetX)
[15]136 resetXView();
[222]137 if (action == resetY)
[15]138 resetYView();
[222]139
140 if (action == clear)
141 clearCurves();
[436]142
[15]143 return true;
144 }
145 return false;
[9]146}
Note: See TracBrowser for help on using the repository browser.