Changeset - 1258272f91d8
[Not reviewed]
show-symbols
0 2 0
Hasan Yavuz Ă–ZDERYA - 9 years ago 2016-08-20 10:21:15
hy@ozderya.net
automatically show symbols when zoomed
2 files changed with 64 insertions and 1 deletions:
0 comments (0 inline, 0 general)
src/plot.cpp
Show inline comments
 
@@ -20,12 +20,16 @@
 
#include <QRectF>
 
#include <QKeySequence>
 
#include <QColor>
 

	
 
#include <qwt_symbol.h>
 
#include <qwt_plot_curve.h>
 
#include <math.h>
 

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

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

	
 
Plot::Plot(QWidget* parent) :
 
    QwtPlot(parent),
 
    zoomer(this->canvas(), false),
 
@@ -37,6 +41,7 @@ Plot::Plot(QWidget* parent) :
 
    showLegendAction("Legend", this)
 
{
 
    isAutoScaled = true;
 
    isSymbolsOn = true;
 

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

	
 
@@ -80,6 +85,18 @@ Plot::Plot(QWidget* parent) :
 
    connect(&showLegendAction, SELECT<bool>::OVERLOAD_OF(&QAction::triggered),
 
            [this](bool enabled){legend.setVisible(enabled); replot();});
 

	
 
    connect(&zoomer, &QwtPlotZoomer::zoomed,
 
            [this](const QRectF &rect)
 
            {
 
                onXScaleChanged();
 
            });
 

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

	
 
    snapshotOverlay = NULL;
 
}
 

	
 
@@ -230,3 +247,45 @@ void Plot::flashSnapshotOverlay()
 
                snapshotOverlay = NULL;
 
            });
 
}
 

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

	
 
    if (symDisPx > SYMBOL_SHOW_AT_WIDTH)
 
    {
 
        isSymbolsOn = true;
 
    }
 
    else
 
    {
 
        isSymbolsOn = false;
 
    }
 

	
 
    updateSymbols();
 
}
 

	
 
void Plot::updateSymbols()
 
{
 
    const QwtPlotItemList curves = itemList( QwtPlotItem::Rtti_PlotCurve );
 

	
 
    if (curves.size() > 0)
 
    {
 
        for (unsigned i = 0; i < curves.size(); i++)
 
        {
 
            QwtSymbol* symbol = NULL;
 
            QwtPlotCurve* curve = static_cast<QwtPlotCurve*>(curves[i]);
 
            qDebug() << i;
 
            if (isSymbolsOn)
 
            {
 
                symbol = new QwtSymbol(QwtSymbol::Ellipse,
 
                                       QBrush(Qt::white),
 
                                       curve->pen(),
 
                                       QSize(SYMBOL_SIZE, SYMBOL_SIZE));
 
            }
 
            curve->setSymbol(symbol);
 
        }
 
    }
 
}
src/plot.h
Show inline comments
 
@@ -47,6 +47,7 @@ public:
 
private:
 
    bool isAutoScaled;
 
    double yMin, yMax;
 
    bool isSymbolsOn;
 
    Zoomer zoomer;
 
    ScaleZoomer sZoomer;
 
    QwtPlotGrid grid;
 
@@ -59,6 +60,8 @@ private:
 
    QAction darkBackgroundAction;
 
    QAction showLegendAction;
 

	
 
    /// update the display of symbols depending on `isSymbolsOn`
 
    void updateSymbols();
 
    void resetAxes();
 

	
 
public slots:
 
@@ -72,6 +75,7 @@ public slots:
 

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

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