Changeset - 262f56d3e063
[Not reviewed]
Hasan Yavuz Ă–ZDERYA - 7 years ago 2018-12-16 13:30:18
hy@ozderya.net
support multi plot and channel visibility for value tracker
7 files changed with 57 insertions and 23 deletions:
0 comments (0 inline, 0 general)
src/plot.cpp
Show inline comments
 
@@ -28,15 +28,15 @@
 
#include "plot.h"
 
#include "utils.h"
 

	
 
static const int SYMBOL_SHOW_AT_WIDTH = 5;
 
static const int SYMBOL_SIZE_MAX = 7;
 

	
 
Plot::Plot(const Stream* stream, QWidget* parent) :
 
Plot::Plot(QWidget* parent) :
 
    QwtPlot(parent),
 
    zoomer(this->canvas(), stream, false),
 
    zoomer(this->canvas(), false),
 
    sZoomer(this, &zoomer)
 
{
 
    isAutoScaled = true;
 
    symbolSize = 0;
 
    numOfSamples = 1;
 
    plotWidth = 1;
 
@@ -88,12 +88,17 @@ Plot::Plot(const Stream* stream, QWidget
 

	
 
Plot::~Plot()
 
{
 
    if (snapshotOverlay != NULL) delete snapshotOverlay;
 
}
 

	
 
void Plot::setDispChannels(QVector<const StreamChannel*> channels)
 
{
 
    zoomer.setDispChannels(channels);
 
}
 

	
 
void Plot::setYAxis(bool autoScaled, double yAxisMin, double yAxisMax)
 
{
 
    this->isAutoScaled = autoScaled;
 

	
 
    if (!autoScaled)
 
    {
src/plot.h
Show inline comments
 
@@ -44,15 +44,18 @@ public:
 
    {
 
        ShowSymbolsAuto,
 
        ShowSymbolsShow,
 
        ShowSymbolsHide
 
    };
 

	
 
    Plot(const Stream* stream, QWidget* parent = 0);
 
    Plot(QWidget* parent = 0);
 
    ~Plot();
 

	
 
    /// Set displayed channels for value tracking (can be null)
 
    void setDispChannels(QVector<const StreamChannel*> channels);
 

	
 
public slots:
 
    void showGrid(bool show = true);
 
    void showMinorGrid(bool show = true);
 
    void showLegend(bool show = true);
 
    void showDemoIndicator(bool show = true);
 
    void showNoChannel(bool show = true);
src/plotmanager.cpp
Show inline comments
 
@@ -51,13 +51,12 @@ PlotManager::PlotManager(QWidget* plotAr
 

	
 
    // add initial curves if any?
 
    for (unsigned int i = 0; i < stream->numChannels(); i++)
 
    {
 
        addCurve(stream->channel(i)->name(), stream->channel(i)->xData(), stream->channel(i)->yData());
 
    }
 

	
 
}
 

	
 
PlotManager::PlotManager(QWidget* plotArea, PlotMenu* menu,
 
                         Snapshot* snapshot, QObject *parent) :
 
    QObject(parent)
 
{
 
@@ -91,14 +90,12 @@ void PlotManager::construct(QWidget* plo
 
    showSymbols = Plot::ShowSymbolsAuto;
 
    emptyPlot = NULL;
 

	
 
    // initalize layout and single widget
 
    isMulti = false;
 
    scrollArea = NULL;
 
    setupLayout(isMulti);
 
    addPlotWidget();
 

	
 
    // connect to  menu
 
    connect(menu, &PlotMenu::symbolShowChanged, this, &PlotManager:: setSymbols);
 

	
 
    connect(&menu->showGridAction, SELECT<bool>::OVERLOAD_OF(&QAction::toggled),
 
            this, &PlotManager::showGrid);
 
@@ -214,14 +211,12 @@ void PlotManager::checkNoVisChannels()
 
        plotWidgets[0]->setVisible(true);
 
    }
 
}
 

	
 
void PlotManager::setMulti(bool enabled)
 
{
 
    if (enabled == isMulti) return;
 

	
 
    isMulti = enabled;
 

	
 
    // detach all curves
 
    for (auto curve : curves)
 
    {
 
        curve->detach();
 
@@ -236,24 +231,32 @@ void PlotManager::setMulti(bool enabled)
 
    // setup new layout
 
    setupLayout(isMulti);
 

	
 
    if (isMulti)
 
    {
 
        // add new widgets and attach
 
        int i = 0;
 
        for (auto curve : curves)
 
        {
 
            auto plot = addPlotWidget();
 
            plot->setVisible(curve->isVisible());
 
            plot->setDispChannels(QVector<const StreamChannel*>(1, _stream->channel(i)));
 
            curve->attach(plot);
 
            i++;
 
        }
 
    }
 
    else
 
    {
 
        // add a single widget
 
        auto plot = addPlotWidget();
 

	
 
        if (_stream != nullptr)
 
        {
 
            plot->setDispChannels(_stream->allChannels());
 
        }
 

	
 
        // attach all curves
 
        for (auto curve : curves)
 
        {
 
            curve->attach(plot);
 
        }
 
    }
 
@@ -302,13 +305,13 @@ void PlotManager::setupLayout(bool multi
 
    layout->setContentsMargins(2,2,2,2);
 
    layout->setSpacing(1);
 
}
 

	
 
Plot* PlotManager::addPlotWidget()
 
{
 
    auto plot = new Plot(_stream);
 
    auto plot = new Plot();
 
    plotWidgets.append(plot);
 
    layout->addWidget(plot);
 

	
 
    plot->darkBackground(_menu->darkBackgroundAction.isChecked());
 
    plot->showGrid(_menu->showGridAction.isChecked());
 
    plot->showMinorGrid(_menu->showMinorGridAction.isChecked());
 
@@ -352,16 +355,18 @@ void PlotManager::_addCurve(QwtPlotCurve
 
    // create the plot for the curve if we are on multi display
 
    Plot* plot;
 
    if (isMulti)
 
    {
 
        // create a new plot widget
 
        plot = addPlotWidget();
 
        plot->setDispChannels(QVector<const StreamChannel*>(1, _stream->channel(index)));
 
    }
 
    else
 
    {
 
        plot = plotWidgets[0];
 
        plot->setDispChannels(_stream->allChannels());
 
    }
 

	
 
    // show the curve
 
    curve->attach(plot);
 
    plot->replot();
 
}
src/stream.cpp
Show inline comments
 
@@ -84,12 +84,22 @@ const StreamChannel* Stream::channel(uns
 

	
 
StreamChannel* Stream::channel(unsigned index)
 
{
 
    return const_cast<StreamChannel*>(static_cast<const Stream&>(*this).channel(index));
 
}
 

	
 
QVector<const StreamChannel*> Stream::allChannels() const
 
{
 
    QVector<const StreamChannel*> result(numChannels());
 
    for (unsigned ci = 0; ci < numChannels(); ci++)
 
    {
 
        result[ci] = channel(ci);
 
    }
 
    return result;
 
}
 

	
 
const ChannelInfoModel* Stream::infoModel() const
 
{
 
    return &_infoModel;
 
}
 

	
 
ChannelInfoModel* Stream::infoModel()
src/stream.h
Show inline comments
 
@@ -54,12 +54,13 @@ public:
 
    bool hasX() const;
 
    unsigned numChannels() const;
 

	
 
    unsigned numSamples() const;
 
    const StreamChannel* channel(unsigned index) const;
 
    StreamChannel* channel(unsigned index);
 
    QVector<const StreamChannel*> allChannels() const;
 
    const ChannelInfoModel* infoModel() const;
 
    ChannelInfoModel* infoModel();
 

	
 
    /// Saves channel information
 
    void saveSettings(QSettings* settings) const;
 
    /// Load channel information
src/zoomer.cpp
Show inline comments
 
@@ -23,17 +23,16 @@
 
#include <QMouseEvent>
 
#include <QtMath>
 

	
 
static const int VALUE_POINT_DIAM = 4;
 
static const int VALUE_TEXT_MARGIN = VALUE_POINT_DIAM + 2;
 

	
 
Zoomer::Zoomer(QWidget* widget, const Stream* stream, bool doReplot) :
 
Zoomer::Zoomer(QWidget* widget, bool doReplot) :
 
    ScrollZoomer(widget)
 
{
 
    is_panning = false;
 
    _stream = stream;
 

	
 
    setTrackerMode(AlwaysOn);
 

	
 
    // set corner widget between the scrollbars with default background color
 
    auto cornerWidget = new QWidget();
 
    auto bgColor = cornerWidget->palette().color(QPalette::Window).name();
 
@@ -60,12 +59,17 @@ void Zoomer::zoom( const QRectF & rect)
 
        this->setZoomBase(false);
 
    }
 

	
 
    ScrollZoomer::zoom(rect);
 
}
 

	
 
void Zoomer::setDispChannels(QVector<const StreamChannel*> channels)
 
{
 
    dispChannels = channels;
 
}
 

	
 
QwtText Zoomer::trackerTextF(const QPointF& pos) const
 
{
 
    QwtText b = ScrollZoomer::trackerTextF(pos);
 

	
 
    const QPolygon pa = selection();
 
    if (!isActive() || pa.count() < 2)
 
@@ -109,13 +113,13 @@ QRegion Zoomer::rubberBandMask() const
 
void Zoomer::drawTracker(QPainter* painter) const
 
{
 
    if (isActive())
 
    {
 
        QwtPlotZoomer::drawTracker(painter);
 
    }
 
    else if (_stream != nullptr && _stream->numChannels())
 
    else if (dispChannels.length())
 
    {
 
        drawValues(painter);
 
    }
 
}
 

	
 
void Zoomer::drawValues(QPainter* painter) const
 
@@ -133,18 +137,20 @@ void Zoomer::drawValues(QPainter* painte
 
    int px = trackerPosition().x();
 
    painter->drawLine(px, pRect.top(), px, pRect.bottom());
 

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

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

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

	
 
            painter->setPen(Qt::white);
 
            // We give a very small (1x1) rectangle but disable clipping
 
            painter->drawText(QRectF(p.x() + VALUE_TEXT_MARGIN, p.y(), 1, 1),
 
@@ -155,22 +161,23 @@ void Zoomer::drawValues(QPainter* painte
 

	
 
    painter->restore();
 
}
 

	
 
QVector<double> Zoomer::findValues(double x) const
 
{
 
    // TODO: process only channel(s) of this plot
 
    unsigned nc = _stream->numChannels();
 
    QVector<double> r(nc);
 
    unsigned nc = dispChannels.length();
 
    QVector<double> result(nc);
 
    for (unsigned ci = 0; ci < nc; ci++)
 
    {
 
        auto chan = _stream->channel(ci);
 
        double val = chan->findValue(x);
 
        r[ci] = val;
 
        if (dispChannels[ci]->visible())
 
        {
 
            result[ci] = dispChannels[ci]->findValue(x);
 
    }
 
    return r;
 

	
 
    }
 
    return result;
 
}
 

	
 
QRect Zoomer::trackerRect(const QFont& font) const
 
{
 
    if (isActive())
 
    {
src/zoomer.h
Show inline comments
 
@@ -21,22 +21,24 @@
 
#define ZOOMER_H
 

	
 
#include <QVector>
 
#include <QRect>
 

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

	
 
class Zoomer : public ScrollZoomer
 
{
 
    Q_OBJECT
 

	
 
public:
 
    Zoomer(QWidget*, const Stream* stream, bool doReplot=true);
 
    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.
 
@@ -56,13 +58,14 @@ protected:
 
    /// Overloaded for panning
 
    void widgetMouseMoveEvent(QMouseEvent* mouseEvent);
 

	
 
private:
 
    bool is_panning;
 
    QPointF pan_point;
 
    const Stream* _stream;
 
    /// displayed channels for value tracking
 
    QVector<const StreamChannel*> dispChannels;
 

	
 
    /// 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
0 comments (0 inline, 0 general)