Changeset - 2175b10b9f1e
[Not reviewed]
default
0 2 0
Hasan Yavuz ÖZDERYA - 6 years ago 2019-12-29 14:17:39
hy@ozderya.net
some refactoring for value tracker
2 files changed with 55 insertions and 36 deletions:
0 comments (0 inline, 0 general)
src/zoomer.cpp
Show inline comments
 
@@ -119,70 +119,88 @@ void Zoomer::drawTracker(QPainter* paint
 
    else if (dispChannels.length())
 
    {
 
        drawValues(painter);
 
    }
 
}
 

	
 
QList<const StreamChannel*> Zoomer::visChannels() const
 
{
 
    QList<const StreamChannel*> result;
 

	
 
    for (unsigned ci = 0; ci < (unsigned) dispChannels.length(); ci++)
 
    {
 
        if (dispChannels[ci]->visible())
 
            result.append(dispChannels[ci]);
 
    }
 

	
 
    return result;
 
}
 

	
 
void Zoomer::drawValues(QPainter* painter) const
 
{
 
    struct ChannelValue
 
    {
 
        const StreamChannel* ch;
 
        double value;
 
    };
 

	
 
    auto tpos = trackerPosition();
 
    if (tpos.x() < 0) return;   // cursor not on window
 

	
 
    painter->save();
 
    // find Y values for current cursor X position
 
    double x = invTransform(tpos).x();
 
    auto channels = visChannels();
 
    QList<ChannelValue> values;
 
    for (auto ch : channels)
 
    {
 
        double value = ch->findValue(x);
 
        if (!std::isnan(value))
 
            values.append({ch, value});
 
    }
 

	
 
    double x = invTransform(tpos).x();
 
    auto values = findValues(x);
 
    // TODO should keep?
 
    if (values.isEmpty())
 
    {
 
        qDebug() << "no value to draw??";
 
        return;
 
    }
 

	
 
    // TODO layout
 

	
 
    painter->save();
 

	
 
    // draw vertical line
 
    auto linePen = rubberBandPen();
 
    linePen.setStyle(Qt::DotLine);
 
    painter->setPen(linePen);
 
    const QRect pRect = pickArea().boundingRect().toRect();
 
    int px = tpos.x();
 
    painter->drawLine(px, pRect.top(), px, pRect.bottom());
 

	
 
    // draw sample values
 
    for (int ci = 0; ci < values.size(); ci++)
 
    for (auto value : values)
 
    {
 
        if (!dispChannels[ci]->visible()) continue;
 
        double val = value.value;
 
        auto ch = value.ch;
 

	
 
        double val = values[ci];
 
        if (!std::isnan(val))
 
        {
 
            auto p = transform(QPointF(x, val));
 
        auto point = transform(QPointF(x, val));
 

	
 
            painter->setBrush(dispChannels[ci]->color());
 
            painter->setPen(Qt::NoPen);
 
            painter->drawEllipse(p, VALUE_POINT_DIAM, VALUE_POINT_DIAM);
 
        painter->setBrush(ch->color());
 
        painter->setPen(Qt::NoPen);
 
        painter->drawEllipse(point, VALUE_POINT_DIAM, VALUE_POINT_DIAM);
 

	
 
            painter->setPen(rubberBandPen());
 
            // We give a very small (1x1) rectangle but disable clipping
 
            painter->drawText(QRectF(p.x() + VALUE_TEXT_MARGIN, p.y(), 1, 1),
 
                              Qt::AlignVCenter | Qt::TextDontClip,
 
                              QString("%1").arg(val));
 
        }
 
        painter->setPen(rubberBandPen());
 
        // We give a very small (1x1) rectangle but disable clipping
 
        painter->drawText(QRectF(point.x() + VALUE_TEXT_MARGIN, point.y(), 1, 1),
 
                          Qt::AlignVCenter | Qt::TextDontClip,
 
                          QString("%1").arg(val));
 
    }
 

	
 
    painter->restore();
 
}
 

	
 
QVector<double> Zoomer::findValues(double x) const
 
{
 
    unsigned nc = dispChannels.length();
 
    QVector<double> result(nc);
 
    for (unsigned ci = 0; ci < nc; ci++)
 
    {
 
        if (dispChannels[ci]->visible())
 
        {
 
            result[ci] = dispChannels[ci]->findValue(x);
 
        }
 

	
 
    }
 
    return result;
 
}
 

	
 
QRect Zoomer::trackerRect(const QFont& font) const
 
{
 
    if (isActive())
 
    {
 
        return QwtPlotZoomer::trackerRect(font);
 
    }
src/zoomer.h
Show inline comments
 
/*
 
  Copyright © 2018 Hasan Yavuz Özderya
 
  Copyright © 2019 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
@@ -18,12 +18,13 @@
 
*/
 

	
 
#ifndef ZOOMER_H
 
#define ZOOMER_H
 

	
 
#include <QVector>
 
#include <QList>
 
#include <QRect>
 

	
 
#include "scrollzoomer.h"
 
#include "streamchannel.h"
 

	
 
class Zoomer : public ScrollZoomer
 
@@ -61,15 +62,15 @@ protected:
 
private:
 
    bool is_panning;
 
    QPointF pan_point;
 
    /// displayed channels for value tracking
 
    QVector<const StreamChannel*> dispChannels;
 

	
 
    /// Get a list of visible channels
 
    QList<const StreamChannel*> visChannels() const;
 
    /// Draw sample values
 
    void drawValues(QPainter* painter) const;
 
    /// Find sample values for given X value
 
    QVector<double> findValues(double x) const;
 
    /// Returns trackerRect for value tracker
 
    QRect valueTrackerRect(const QFont& font) const;
 
};
 

	
 
#endif // ZOOMER_H
0 comments (0 inline, 0 general)