Changeset - f641cc1fcdd8
[Not reviewed]
show-symbols
0 3 0
Hasan Yavuz Ă–ZDERYA - 9 years ago 2016-08-20 11:06:43
hy@ozderya.net
symbol size varies according to zoom level
3 files changed with 26 insertions and 12 deletions:
0 comments (0 inline, 0 general)
src/mainwindow.cpp
Show inline comments
 
@@ -110,12 +110,15 @@ MainWindow::MainWindow(QWidget *parent) 
 
    QObject::connect(&portControl, &PortControl::portToggled,
 
                     this, &MainWindow::onPortToggled);
 

	
 
    connect(&plotControlPanel, &PlotControlPanel::numOfSamplesChanged,
 
            this, &MainWindow::onNumOfSamplesChanged);
 

	
 
    connect(&plotControlPanel, &PlotControlPanel::numOfSamplesChanged,
 
            ui->plot, &Plot::onNumOfSamplesChanged);
 

	
 
    connect(&plotControlPanel, &PlotControlPanel::scaleChanged,
 
            ui->plot, &Plot::setAxis);
 

	
 
    QObject::connect(ui->actionClear, SIGNAL(triggered(bool)),
 
                     this, SLOT(clearPlot()));
 

	
src/plot.cpp
Show inline comments
 
@@ -20,31 +20,32 @@
 
#include <QRectF>
 
#include <QKeySequence>
 
#include <QColor>
 
#include <qwt_symbol.h>
 
#include <qwt_plot_curve.h>
 
#include <math.h>
 
#include <algorithm>
 

	
 
#include "plot.h"
 
#include "utils.h"
 

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

	
 
Plot::Plot(QWidget* parent) :
 
    QwtPlot(parent),
 
    zoomer(this->canvas(), false),
 
    sZoomer(this, &zoomer),
 
    showGridAction("Grid", this),
 
    showMinorGridAction("Minor Grid", this),
 
    unzoomAction("Unzoom", this),
 
    darkBackgroundAction("Dark Background", this),
 
    showLegendAction("Legend", this)
 
{
 
    isAutoScaled = true;
 
    isSymbolsOn = true;
 
    symbolSize = 0;
 

	
 
    QObject::connect(&zoomer, &Zoomer::unzoomed, this, &Plot::unzoomed);
 

	
 
    zoomer.setZoomBase();
 
    grid.attach(this);
 
    legend.attach(this);
 
@@ -91,13 +92,13 @@ Plot::Plot(QWidget* parent) :
 
                onXScaleChanged();
 
            });
 

	
 
    connect(this, &QwtPlot::itemAttached,
 
            [this](QwtPlotItem *plotItem, bool on)
 
            {
 
                if (isSymbolsOn) updateSymbols();
 
                if (symbolSize) updateSymbols();
 
            });
 

	
 
    snapshotOverlay = NULL;
 
}
 

	
 
Plot::~Plot()
 
@@ -250,21 +251,21 @@ void Plot::flashSnapshotOverlay()
 

	
 
void Plot::onXScaleChanged()
 
{
 
    auto sw = axisWidget(QwtPlot::xBottom);
 
    auto paintDist = sw->scaleDraw()->scaleMap().pDist();
 
    auto scaleDist = sw->scaleDraw()->scaleMap().sDist();
 
    double symDisPx = paintDist / scaleDist;
 
    int symDisPx = round(paintDist / scaleDist);
 

	
 
    if (symDisPx > SYMBOL_SHOW_AT_WIDTH)
 
    if (symDisPx < SYMBOL_SHOW_AT_WIDTH)
 
    {
 
        isSymbolsOn = true;
 
        symbolSize = 0;
 
    }
 
    else
 
    {
 
        isSymbolsOn = false;
 
        symbolSize = std::min(SYMBOL_SIZE_MAX, symDisPx-SYMBOL_SHOW_AT_WIDTH+1);
 
    }
 

	
 
    updateSymbols();
 
}
 

	
 
void Plot::updateSymbols()
 
@@ -274,23 +275,31 @@ void Plot::updateSymbols()
 
    if (curves.size() > 0)
 
    {
 
        for (unsigned i = 0; i < curves.size(); i++)
 
        {
 
            QwtSymbol* symbol = NULL;
 
            QwtPlotCurve* curve = static_cast<QwtPlotCurve*>(curves[i]);
 
            if (isSymbolsOn)
 
            if (symbolSize)
 
            {
 
                symbol = new QwtSymbol(QwtSymbol::Ellipse,
 
                                       QBrush(Qt::white),
 
                                       curve->pen(),
 
                                       QSize(SYMBOL_SIZE, SYMBOL_SIZE));
 
                                       QSize(symbolSize, symbolSize));
 
            }
 
            curve->setSymbol(symbol);
 
        }
 
    }
 
}
 

	
 
void Plot::resizeEvent(QResizeEvent * event)
 
{
 
    QwtPlot::resizeEvent(event);
 
    onXScaleChanged();
 
}
 

	
 
void Plot::onNumOfSamplesChanged(unsigned value)
 
{
 
    auto currentBase = zoomer.zoomBase();
 
    currentBase.setWidth(value);
 
    zoomer.setZoomBase(currentBase);
 
    onXScaleChanged();
 
}
src/plot.h
Show inline comments
 
@@ -44,26 +44,26 @@ public:
 

	
 
    static QColor makeColor(unsigned int channelIndex);
 

	
 
private:
 
    bool isAutoScaled;
 
    double yMin, yMax;
 
    bool isSymbolsOn;
 
    int symbolSize;
 
    Zoomer zoomer;
 
    ScaleZoomer sZoomer;
 
    QwtPlotGrid grid;
 
    PlotSnapshotOverlay* snapshotOverlay;
 
    QwtPlotLegendItem legend;
 

	
 
    QAction showGridAction;
 
    QAction showMinorGridAction;
 
    QAction unzoomAction;
 
    QAction darkBackgroundAction;
 
    QAction showLegendAction;
 

	
 
    /// update the display of symbols depending on `isSymbolsOn`
 
    /// update the display of symbols depending on `symbolSize`
 
    void updateSymbols();
 
    void resetAxes();
 
    void resizeEvent(QResizeEvent * event);
 

	
 
public slots:
 
    void showGrid(bool show = true);
 
@@ -71,12 +71,14 @@ public slots:
 
    void unzoom();
 
    void darkBackground(bool enabled = true);
 
    void setAxis(bool autoScaled, double yMin = 0, double yMax = 1);
 

	
 
    void flashSnapshotOverlay();
 

	
 
    void onNumOfSamplesChanged(unsigned value);
 

	
 
private slots:
 
    void unzoomed();
 
    void onXScaleChanged();
 
};
 

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