00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "EventViewer.h"
00025 #include "../common/types.h"
00026 #include <vector>
00027 #include <QtDebug>
00028
00029 #ifdef HAVE_QWT
00030
00031 #include <qwt_data.h>
00032
00033 namespace Aseba
00034 {
00037
00038 class EventDataWrapper : public QwtData
00039 {
00040 private:
00041 std::vector<double>& _x;
00042 std::vector<sint16>& _y;
00043
00044 public:
00045 EventDataWrapper(std::vector<double>& _x, std::vector<sint16>& _y) :
00046 _x(_x),
00047 _y(_y)
00048 { }
00049 virtual QwtData * copy () const { return new EventDataWrapper(*this); }
00050 virtual size_t size () const { return _x.size(); }
00051 virtual double x (size_t i) const { return _x[i]; }
00052 virtual double y (size_t i) const { return (double)_y[i]; }
00053 };
00054
00055 EventViewer::EventViewer(unsigned eventId, const QString& eventName, unsigned eventVariablesCount, MainWindow::EventViewers* eventsViewers) :
00056 eventId(eventId),
00057 eventsViewers(eventsViewers),
00058 values(eventVariablesCount),
00059 startingTime(QTime::currentTime())
00060 {
00061 setCanvasBackground(Qt::white);
00062 setAxisTitle(xBottom, tr("Time (seconds)"));
00063 setAxisTitle(yLeft, tr("Values"));
00064
00065 QwtLegend *legend = new QwtLegend;
00066
00067 insertLegend(legend, QwtPlot::BottomLegend);
00068
00069 for (size_t i = 0; i < values.size(); i++)
00070 {
00071 QwtPlotCurve *curve = new QwtPlotCurve(QString("%0").arg(i));
00072 curve->setData(EventDataWrapper(timeStamps, values[i]));
00073 curve->attach(this);
00074 curve->setPen(QColor::fromHsv((i * 360) / values.size(), 255, 100));
00075 }
00076
00077 eventsViewers->insert(eventId, this);
00078 }
00079
00080 EventViewer::~EventViewer()
00081 {
00082 if (eventsViewers)
00083 eventsViewers->remove(eventId, this);
00084 }
00085
00086 void EventViewer::addData(const VariablesDataVector& data)
00087 {
00088 double elapsedTime = (double)startingTime.msecsTo(QTime::currentTime()) / 1000.;
00089 timeStamps.push_back(elapsedTime);
00090 for (size_t i = 0; i < values.size(); i++)
00091 {
00092 if (i < data.size())
00093 {
00094 values[i].push_back(data[i]);
00095 }
00096 else
00097 {
00098 values[i].push_back(0);
00099 }
00100 }
00101 replot();
00102 }
00103
00105 };
00106
00107 #endif // HAVE_QWT