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 50 insertions and 31 deletions:
0 comments (0 inline, 0 general)
src/zoomer.cpp
Show inline comments
 
@@ -101,106 +101,124 @@ void Zoomer::drawRubberBand(QPainter* pa
 

	
 
QRegion Zoomer::rubberBandMask() const
 
{
 
    const QPolygon pa = selection();
 
    if (pa.count() < 2)
 
    {
 
        return QRegion();
 
    }
 
    const QRect r = QRect(pa.first(), pa.last()).normalized().adjusted(0, 0, 1, 1);
 
    return QRegion(r);
 
}
 

	
 
void Zoomer::drawTracker(QPainter* painter) const
 
{
 
    if (isActive())
 
    {
 
        QwtPlotZoomer::drawTracker(painter);
 
    }
 
    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->setBrush(ch->color());
 
            painter->setPen(Qt::NoPen);
 
            painter->drawEllipse(p, VALUE_POINT_DIAM, VALUE_POINT_DIAM);
 
        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),
 
        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);
 
    }
 
    else
 
    {
 
        return valueTrackerRect(font);
 
    }
 
}
 

	
 
QRect Zoomer::valueTrackerRect(const QFont& font) const
 
{
 
    // TODO: consider using actual tracker values for width calculation
 
    const int textWidth = qCeil(QwtText("-8.8888888").textSize(font).width());
 
    const int width = textWidth + VALUE_POINT_DIAM + VALUE_TEXT_MARGIN;
 
    const int x = trackerPosition().x() - VALUE_POINT_DIAM;
 
    const auto pickRect = pickArea().boundingRect();
 

	
 
    return QRect(x, pickRect.y(), width, pickRect.height());
 
}
 

	
 
void Zoomer::widgetMousePressEvent(QMouseEvent* mouseEvent)
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
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef ZOOMER_H
 
#define ZOOMER_H
 

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

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

	
 
class Zoomer : public ScrollZoomer
 
{
 
    Q_OBJECT
 

	
 
public:
 
    Zoomer(QWidget*, bool doReplot=true);
 
    void zoom(int up);
 
    void zoom(const QRectF&);
 
    /// Set displayed channels for value tracking (can be null)
 
    void setDispChannels(QVector<const StreamChannel*> channels);
 

	
 
signals:
 
    void unzoomed();
 

	
 
protected:
 
    /// Re-implemented to display selection size in the tracker text.
 
    QwtText trackerTextF(const QPointF &pos) const override;
 
    /// Re-implemented for sample value tracker
 
    QRect trackerRect(const QFont&) const override;
 
    /// Re-implemented for alpha background
 
    void drawRubberBand(QPainter* painter) const override;
 
    /// Re-implemented to draw sample values
 
    void drawTracker(QPainter* painter) const override;
 
    /// Re-implemented for alpha background (masking is basically disabled)
 
    QRegion rubberBandMask() const;
 
    /// Overloaded for panning
 
    void widgetMousePressEvent(QMouseEvent* mouseEvent);
 
    /// Overloaded for panning
 
    void widgetMouseReleaseEvent(QMouseEvent* mouseEvent);
 
    /// Overloaded for panning
 
    void widgetMouseMoveEvent(QMouseEvent* mouseEvent);
 

	
 
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)