diff --git a/src/plot.cpp b/src/plot.cpp --- a/src/plot.cpp +++ b/src/plot.cpp @@ -20,12 +20,16 @@ #include #include #include - +#include +#include #include #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::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(curves[i]); + qDebug() << i; + if (isSymbolsOn) + { + symbol = new QwtSymbol(QwtSymbol::Ellipse, + QBrush(Qt::white), + curve->pen(), + QSize(SYMBOL_SIZE, SYMBOL_SIZE)); + } + curve->setSymbol(symbol); + } + } +} diff --git a/src/plot.h b/src/plot.h --- a/src/plot.h +++ b/src/plot.h @@ -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