Beispiel #1
0
 virtual bool read(ConnectionReader& connection) {
     double now = Time::now();
     Bottle b;
     bool ok = b.read(connection);
     if (ok) {
         mutex.wait();
         ct++;
         if (ct>1) {
             double dt = now - lastTime;
             period.add(dt);
         }
         lastTime = now;
         printf("Period is %g +/- %g (%d)\n",
                period.mean(),
                period.deviation(),
                ct);
         mutex.post();
     }
     return ok;
 }
void Graph::paintEvent(QPaintEvent *e)
{
    VtlWidget::paintEvent(e);
    QPainter p(this);
    Interval iv = currentInterval();

    p.setTransform(wtr);

    if(!noScale)
    {
        p.setPen(cText);
        QRect rs(0, topScale ? h:-lh, w, lh);
        p.fillRect(rs, cScale);
        drawTextEx(p, rs, Qt::AlignLeft|Qt::AlignVCenter,
            QDateTime::fromTime_t(iv.t1).toString(timeFormat), reverse, 1);
        drawTextEx(p, rs, Qt::AlignRight|Qt::AlignVCenter,
            QDateTime::fromTime_t(iv.t2).toString(timeFormat), reverse, 1);
    }

    p.fillRect(0, 0, w, h, cBack);

    p.setPen(cGrid);
    for(int i = 0; i <= nTimeLines; i++) { int x = i*w/nTimeLines; p.drawLine(x, 0, x, h); }
    for(int i = 0; i <= nValLines; i++) { int y = i*h/nValLines; p.drawLine(0, y, w, y); }

    int scaleX = w + sh + 1;
    int *curScaleWidth = scaleWidth.begin();
    QRect rScales(scaleX, 0, scalesWidth, h);
    p.fillRect(rScales, cScale);

    p.setClipRect(QRectF(-1, -1, w + 2 + sh, h + 2));

    for(QList<Plot>::iterator plot = plots.begin(); plot != plots.end(); ++plot)
    {
        if(!plot->sourcer()) continue;
        const RingBuf<Stat> &st = stats[plot->sourcer()];
        Stat total;
        for(RingBuf<Stat>::citerator s = st.begin(); s != st.end(); ++s) total.add(*s);
        //int pc = plot->precission();
        float s1 = plot->minScale(), s2 = plot->maxScale(), ds = s2 - s1;
        if(plot->autoScale())
        {
            float as1 = total.min, as2 = total.max, ads = as2 - as1;
            if(ads)
            {
                ds *= 0.01f*plot->autoScalePercent();
                if(ads < ds)
                {
                    float as = 0.5*(as1 + as2), k = ds/ads;
                    as1 = as + k*(as1 - as);
                    as2 = as + k*(as2 - as);
                }
                //float t = pow(10, pc - 2);
                s1 = qMax(s1, floorf(/*t*/as1)/*t*/);
                s2 = qMin(s2, ceilf(/*t*/as2)/*t*/);
                ds = s2 - s1;
            }
        }
        if(ds) switch(md)
        {
        case Average:
            {
                QVector<QPointF> points;
                int x = 0;
                float y = NAN;
                p.setPen(plot->color);
                for(RingBuf<Stat>::citerator s = st.begin(); s != st.end(); ++s, ++x)
                {
                    if(s->n)
                    {
                        if(y == y) points << QPointF(x, y); else points.clear();
                        y = (h - 1)*(s->sum/s->n - s1)/ds;
                        points << QPointF(x, y);
                    }
                    else if(s != st.end() - 1)
                    {
                        p.drawPolyline(points);
                        y = NAN;
                    }
                }
                if(plot->sourcer()->getReliability() == 1)
                {
                    y = (h - 1)*(plot->sourcer()->value() - s1)/ds;
                    points << QPointF(w, y);
                    p.setBrush(plot->color);
                    p.drawPolygon(QPolygonF() << QPointF(w, y) <<
                        QPointF(w + sh, y + 2) << QPointF(w + sh, y - 2));
                }
                p.drawPolyline(points);
            }
            break;
        case MinMax:
            {
                QVector<QPointF> lines;
                int x = 0;
                float y1 = NAN, y2 = NAN;
                p.setPen(plot->color);
                for(RingBuf<Stat>::citerator s = st.begin(); s != st.end(); ++s, ++x)
                {
                    if(s->n)
                    {
                        float cy1 = (h - 1)*(s->min - s1)/ds;
                        float cy2 = (h - 1)*(s->max - s1)/ds;
                        if(y2 < cy1) cy1 = y2;
                        if(y1 > cy2) cy2 = y1;
                        y1 = cy1; y2 = cy2;
                        lines << QPointF(x, y1) << QPointF(x, y2);
                    }
                    else if(s != st.end() - 1)
                    {
                        p.drawLines(lines);
                        lines.clear();
                        y1 = y2 = NAN;
                    }
                }
                if(plot->sourcer()->getReliability() == 1)
                {
                    float y = (h - 1)*(plot->sourcer()->value() - s1)/ds;
                    p.setBrush(plot->color);
                    p.drawPolygon(QPolygonF() << QPointF(w, y) <<
                        QPointF(w + sh, y + 2) << QPointF(w + sh, y - 2));
                }
                p.drawLines(lines);
            }
            break;
        }

        if(plot->showScale())
        {
            p.setClipping(0);
            p.fillRect(scaleX, 0, sh, h, plot->color);
            for(int j = 0; j <= nValLines; j++)
            {
                int y = j*h/nValLines;
                float s = s1 + ds*j/nValLines;
                p.setPen(cText);
                QRect rs = QRect(scaleX + sh + 1, y - lh, *curScaleWidth, lh*2)&rScales;
                drawTextEx(p, rs, Qt::AlignVCenter|Qt::AlignLeft, valueToString(s), reverse, 1);
            }
            scaleX += *curScaleWidth++;
            p.setClipping(1);
        }
    }
}