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
 
@@ -17,29 +17,34 @@
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#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),
 
    sZoomer(this, &zoomer),
 
    showGridAction("Grid", this),
 
    showMinorGridAction("Minor Grid", this),
 
    unzoomAction("Unzoom", this),
 
    darkBackgroundAction("Dark Background", this),
 
    showLegendAction("Legend", this)
 
{
 
    isAutoScaled = true;
 
    isSymbolsOn = true;
 

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

	
 
    zoomer.setZoomBase();
 
    grid.attach(this);
 
    legend.attach(this);
 
@@ -77,12 +82,24 @@ Plot::Plot(QWidget* parent) :
 
    connect(&unzoomAction, &QAction::triggered, this, &Plot::unzoom);
 
    connect(&darkBackgroundAction, SELECT<bool>::OVERLOAD_OF(&QAction::triggered),
 
            this, &Plot::darkBackground);
 
    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;
 
}
 

	
 
Plot::~Plot()
 
{
 
    if (snapshotOverlay != NULL) delete snapshotOverlay;
 
@@ -227,6 +244,48 @@ void Plot::flashSnapshotOverlay()
 
            [this]()
 
            {
 
                delete snapshotOverlay;
 
                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
 
@@ -44,24 +44,27 @@ public:
 

	
 
    static QColor makeColor(unsigned int channelIndex);
 

	
 
private:
 
    bool isAutoScaled;
 
    double yMin, yMax;
 
    bool isSymbolsOn;
 
    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`
 
    void updateSymbols();
 
    void resetAxes();
 

	
 
public slots:
 
    void showGrid(bool show = true);
 
    void showMinorGrid(bool show = true);
 
    void unzoom();
 
@@ -69,9 +72,10 @@ public slots:
 
    void setAxis(bool autoScaled, double yMin = 0, double yMax = 1);
 

	
 
    void flashSnapshotOverlay();
 

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

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