diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -116,6 +116,9 @@ MainWindow::MainWindow(QWidget *parent) connect(&plotControlPanel, &PlotControlPanel::numOfSamplesChanged, this, &MainWindow::onNumOfSamplesChanged); + connect(&plotControlPanel, &PlotControlPanel::numOfSamplesChanged, + plotMan, &PlotManager::onNumOfSamplesChanged); + connect(&plotControlPanel, &PlotControlPanel::scaleChanged, plotMan, &PlotManager::setAxis); diff --git a/src/plot.cpp b/src/plot.cpp --- a/src/plot.cpp +++ b/src/plot.cpp @@ -20,18 +20,24 @@ #include #include #include - +#include +#include #include +#include #include "plot.h" #include "utils.h" +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) { isAutoScaled = true; + symbolSize = 0; QObject::connect(&zoomer, &Zoomer::unzoomed, this, &Plot::unzoomed); @@ -44,6 +50,18 @@ Plot::Plot(QWidget* parent) : snapshotOverlay = NULL; + connect(&zoomer, &QwtPlotZoomer::zoomed, + [this](const QRectF &rect) + { + onXScaleChanged(); + }); + + connect(this, &QwtPlot::itemAttached, + [this](QwtPlotItem *plotItem, bool on) + { + if (symbolSize) updateSymbols(); + }); + // init demo indicator QwtText demoText(" DEMO RUNNING "); // looks better with spaces demoText.setColor(QColor("white")); @@ -148,6 +166,7 @@ void Plot::darkBackground(bool enabled) sZoomer.setPickerPen(QPen(Qt::black)); legend.setTextPen(QPen(Qt::black)); } + updateSymbols(); replot(); } @@ -203,3 +222,58 @@ void Plot::flashSnapshotOverlay(bool lig snapshotOverlay = NULL; }); } + +void Plot::onXScaleChanged() +{ + auto sw = axisWidget(QwtPlot::xBottom); + auto paintDist = sw->scaleDraw()->scaleMap().pDist(); + auto scaleDist = sw->scaleDraw()->scaleMap().sDist(); + int symDisPx = round(paintDist / scaleDist); + + if (symDisPx < SYMBOL_SHOW_AT_WIDTH) + { + symbolSize = 0; + } + else + { + symbolSize = std::min(SYMBOL_SIZE_MAX, symDisPx-SYMBOL_SHOW_AT_WIDTH+1); + } + + updateSymbols(); +} + +void Plot::updateSymbols() +{ + const QwtPlotItemList curves = itemList( QwtPlotItem::Rtti_PlotCurve ); + + if (curves.size() > 0) + { + for (int i = 0; i < curves.size(); i++) + { + QwtSymbol* symbol = NULL; + QwtPlotCurve* curve = static_cast(curves[i]); + if (symbolSize) + { + symbol = new QwtSymbol(QwtSymbol::Ellipse, + canvasBackground(), + curve->pen(), + 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(); +} diff --git a/src/plot.h b/src/plot.h --- a/src/plot.h +++ b/src/plot.h @@ -46,6 +46,7 @@ public: private: bool isAutoScaled; double yMin, yMax; + int symbolSize; Zoomer zoomer; ScaleZoomer sZoomer; QwtPlotGrid grid; @@ -53,7 +54,10 @@ private: QwtPlotLegendItem legend; QwtPlotTextLabel demoIndicator; + /// update the display of symbols depending on `symbolSize` + void updateSymbols(); void resetAxes(); + void resizeEvent(QResizeEvent * event); public slots: void showGrid(bool show = true); @@ -71,8 +75,11 @@ public slots: */ void flashSnapshotOverlay(bool light); + void onNumOfSamplesChanged(unsigned value); + private slots: void unzoomed(); + void onXScaleChanged(); }; #endif // PLOT_H diff --git a/src/plotmanager.cpp b/src/plotmanager.cpp --- a/src/plotmanager.cpp +++ b/src/plotmanager.cpp @@ -364,3 +364,11 @@ void PlotManager::flashSnapshotOverlay() plot->flashSnapshotOverlay(darkBackgroundAction.isChecked()); } } + +void PlotManager::onNumOfSamplesChanged(unsigned value) +{ + for (auto plot : plotWidgets) + { + plot->onNumOfSamplesChanged(value); + } +} diff --git a/src/plotmanager.h b/src/plotmanager.h --- a/src/plotmanager.h +++ b/src/plotmanager.h @@ -62,6 +62,8 @@ public slots: void setAxis(bool autoScaled, double yMin = 0, double yMax = 1); /// Display an animation for snapshot void flashSnapshotOverlay(); + /// Should be called to update zoom base + void onNumOfSamplesChanged(unsigned value); private: bool isMulti;