00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <QtDebug>
00026 #include <QPainter>
00027 #include <QMouseEvent>
00028 #include <QPoint>
00029 #include <QDateTime>
00030
00031 #include <qwt_scale_map.h>
00032 #include <qwt_plot_curve.h>
00033 #include <qwt_symbol.h>
00034
00035 #include "datahandler.h"
00036 #include "simpleplot.h"
00037
00038 SimplePlot::SimplePlot(int linesize,
00039 QPointF lineAxis,
00040 const QDateTime &startstamp,
00041 QWidget *parent)
00042 : QWidget(parent)
00043 {
00044 d_valid = false;
00045 samplesprCurve = linesize;
00046
00047 xMap = new QwtScaleMap;
00048 yMap = new QwtScaleMap;
00049 d_lineaxisbounds = lineAxis;
00050
00051 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
00052 setFocusPolicy(Qt::StrongFocus);
00053 setMouseTracking(true);
00054
00055 d_deltax = d_deltay = 1;
00056
00057 shadowRect.setLeft(0);
00058 shadowRect.setTop(0);
00059 setCursor(QCursor(Qt::BlankCursor));
00060 markerVisible = false;
00061 curvesStartTime = startstamp;
00062 }
00063
00064 SimplePlot::~SimplePlot()
00065 {
00066 delete xMap;
00067 delete yMap;
00068 qDeleteAll(curves);
00069 curves.clear();
00070 }
00071
00072 void SimplePlot::initCurves(int curvecount)
00073 {
00074 if (!d_valid)
00075 {
00076 curveCount = curvecount;
00077 createCurves();
00078 }
00079 else
00080 {
00081 if (curveCount != curvecount)
00082 {
00083
00084 }
00085 }
00086 }
00087
00088 void SimplePlot::setData(const double *x,
00089 const QVector<QVector<double> > &vYData)
00090 {
00091 if (d_valid)
00092 {
00093 for (int i = 0; i < vYData.size(); i++)
00094 {
00095 curves[i]->setRawData(x, vYData[i].constData(), samplesprCurve);
00096 }
00097 shadowRect.setLeft(0);
00098 shadowRect.setTop(0);
00099 update();
00100 }
00101 }
00102
00103 void SimplePlot::createCurves()
00104 {
00105
00106 xMap->setScaleInterval(0.0, 30.0);
00107 yMap->setScaleInterval(d_lineaxisbounds.x(), d_lineaxisbounds.y());
00108
00109 QwtSymbol sym;
00110
00111 sym.setPen(QColor(Qt::black));
00112 sym.setSize(5);
00113
00114 QwtPlotCurve *c = NULL;
00115
00116 for (int i = 0; i < curveCount; i++)
00117 {
00118 c = new QwtPlotCurve;
00119 c->setSymbol(sym);
00120 c->setStyle(QwtPlotCurve::Lines);
00121
00122 curves.push_back(c);
00123 }
00124 d_valid = true;
00125 }
00126
00127 void SimplePlot::drawContents(QPainter *painter)
00128 {
00129 int deltay, i;
00130
00131 QRect r = contentsRect();
00132
00133 deltay = r.height() / curveCount - 1;
00134
00135 d_deltay = deltay;
00136 d_deltax = (double)samplesprCurve / (double)r.width();
00137
00138 markerRect.setWidth((int)(d_samplebounds.y() / d_deltax));
00139 markerRect.setHeight(d_deltay);
00140 shadowRect.setWidth(markerRect.width());
00141 shadowRect.setHeight(markerRect.height());
00142
00143 r.setHeight(deltay);
00144
00145 QDateTime temp = curvesStartTime;
00146 QString format("hh:mm:ss");
00147
00148 painter->drawText(r, Qt::AlignHCenter, QString("CHANNEL 1"));
00149 for (i = 0; i < curveCount; i++)
00150 {
00151 painter->drawText(r,
00152 Qt::AlignLeft | Qt::AlignTop,
00153 temp.toString(format));
00154
00155 if (curves[i]->dataSize() != 0)
00156 {
00157 xMap->setPaintInterval(r.left(), r.right());
00158 yMap->setPaintInterval(r.bottom(), r.top());
00159
00160 curves[i]->draw(painter, *xMap, *yMap, r);
00161
00162 shiftDown(r, deltay);
00163 }
00164 temp = temp.addSecs(DataHandler::secondsprLine);
00165 }
00166 }
00167
00168 void SimplePlot::drawMarker(QPainter *painter)
00169 {
00170 painter->setPen(palette().dark().color());
00171 painter->drawRect(markerRect);
00172 }
00173
00174 void SimplePlot::drawShadow(QPainter *painter)
00175 {
00176 painter->setPen(palette().background().color());
00177 painter->setBrush(Qt::DiagCrossPattern);
00178 painter->drawRect(shadowRect);
00179 }
00180
00181 void SimplePlot::paintEvent(QPaintEvent *event)
00182 {
00183 QWidget::paintEvent(event);
00184
00185 if (d_valid)
00186 {
00187 QPainter painter(this);
00188 painter.setClipRect(contentsRect());
00189 drawContents(&painter);
00190 if (markerVisible)
00191 {
00192 drawMarker(&painter);
00193 }
00194 drawShadow(&painter);
00195 }
00196 }
00197
00198 void SimplePlot::mousePressEvent(QMouseEvent *event)
00199 {
00200 if (event->button() == Qt::LeftButton)
00201 {
00202 int segment = event->y() / d_deltay;
00203 segment = qBound(0, segment, curveCount - 1);
00204 int sample = (int)(event->x() * d_deltax);
00205
00206 qDebug() << "Segment:" << segment << "Sample:" << sample;
00207 sample = qBound(0, sample, samplesprCurve - d_samplebounds.y() - 1);
00208 qDebug() << "qbound sample:" << sample;
00209 int xadjusted = (int)(sample / d_deltax);
00210 QRect r = contentsRect();
00211 shiftDown(r, d_deltay * segment);
00212 shadowRect.setLeft(xadjusted);
00213 shadowRect.setTop(r.top());
00214 emit valueClicked(segment, sample);
00215 update();
00216 }
00217 }
00218
00219 void SimplePlot::mouseMoveEvent(QMouseEvent *event)
00220 {
00221 QRect r = contentsRect();
00222 int segment = event->y() / d_deltay;
00223 segment = qBound(0, segment, curveCount - 1);
00224 shiftDown(r, d_deltay * segment);
00225 int sample = (int)(event->x() * d_deltax);
00226 sample = qBound(0, sample, samplesprCurve - d_samplebounds.y() - 1);
00227 int xadjusted = (int)(sample / d_deltax);
00228 markerRect.setLeft(xadjusted);
00229 markerRect.setTop(r.top());
00230 update();
00231 }
00232
00233 void SimplePlot::enterEvent(QEvent *event)
00234 {
00235 markerVisible = true;
00236 QWidget::enterEvent(event);
00237 update();
00238 }
00239
00240 void SimplePlot::leaveEvent(QEvent *event)
00241 {
00242 markerVisible = false;
00243 QWidget::leaveEvent(event);
00244 update();
00245 }