|
| 1 | +#include "ArrowPlot.hpp" |
| 2 | +#include "pre/widgets/PlotWidget.hpp" |
| 3 | +#include "pre/models/units/UnitSystem.hpp" |
| 4 | + |
| 5 | +ArrowPlot::ArrowPlot(const Common& common, const States& states) |
| 6 | + : common(common), |
| 7 | + states(states) |
| 8 | +{ |
| 9 | + posPlot = new PlotWidget(); |
| 10 | + velPlot = new PlotWidget(); |
| 11 | + accPlot = new PlotWidget(); |
| 12 | + |
| 13 | + posGraph = posPlot->addGraph(); |
| 14 | + posGraph->setName("Position"); |
| 15 | + posGraph->setPen({Qt::blue, 2.0}); |
| 16 | + |
| 17 | + velGraph = velPlot->addGraph(); |
| 18 | + velGraph->setName("Velocity"); |
| 19 | + velGraph->setPen({QColor(128, 0, 128), 2.0}); |
| 20 | + |
| 21 | + accGraph = accPlot->addGraph(); |
| 22 | + accGraph->setName("Acceleration"); |
| 23 | + accGraph->setPen({Qt::red, 2.0}); |
| 24 | + |
| 25 | + auto vbox = new QVBoxLayout(); |
| 26 | + vbox->setContentsMargins({}); |
| 27 | + vbox->setSpacing(0); |
| 28 | + vbox->addWidget(posPlot); |
| 29 | + vbox->addWidget(velPlot); |
| 30 | + vbox->addWidget(accPlot); |
| 31 | + setLayout(vbox); |
| 32 | + |
| 33 | + // Connect x-axis ranges of the plots to each other |
| 34 | + QObject::connect(posPlot->xAxis, QOverload<const QCPRange&>::of(&QCPAxis::rangeChanged), this, &ArrowPlot::updateRanges); |
| 35 | + QObject::connect(velPlot->xAxis, QOverload<const QCPRange&>::of(&QCPAxis::rangeChanged), this, &ArrowPlot::updateRanges); |
| 36 | + QObject::connect(accPlot->xAxis, QOverload<const QCPRange&>::of(&QCPAxis::rangeChanged), this, &ArrowPlot::updateRanges); |
| 37 | + |
| 38 | + |
| 39 | + // Update plots on unit changes |
| 40 | + QObject::connect(&Quantities::time, &Quantity::unitChanged, this, &ArrowPlot::updatePlots); |
| 41 | + QObject::connect(&Quantities::position, &Quantity::unitChanged, this, &ArrowPlot::updatePlots); |
| 42 | + QObject::connect(&Quantities::velocity, &Quantity::unitChanged, this, &ArrowPlot::updatePlots); |
| 43 | + QObject::connect(&Quantities::acceleration, &Quantity::unitChanged, this, &ArrowPlot::updatePlots); |
| 44 | + |
| 45 | + // Initialize plots once |
| 46 | + updatePlots(); |
| 47 | +} |
| 48 | + |
| 49 | +void ArrowPlot::updateRanges(const QCPRange& range) { |
| 50 | + posPlot->xAxis->setRange(range); |
| 51 | + velPlot->xAxis->setRange(range); |
| 52 | + accPlot->xAxis->setRange(range); |
| 53 | + |
| 54 | + posPlot->replot(); |
| 55 | + velPlot->replot(); |
| 56 | + accPlot->replot(); |
| 57 | +} |
| 58 | + |
| 59 | +void ArrowPlot::updatePlots() { |
| 60 | + posPlot->xAxis->setLabel("Time " + Quantities::time.getUnit().getLabel()); |
| 61 | + posPlot->yAxis->setLabel("Position " + Quantities::position.getUnit().getLabel()); |
| 62 | + |
| 63 | + velPlot->xAxis->setLabel("Time " + Quantities::time.getUnit().getLabel()); |
| 64 | + velPlot->yAxis->setLabel("Velocity " + Quantities::velocity.getUnit().getLabel()); |
| 65 | + |
| 66 | + accPlot->xAxis->setLabel("Time " + Quantities::time.getUnit().getLabel()); |
| 67 | + accPlot->yAxis->setLabel("Acceleration " + Quantities::acceleration.getUnit().getLabel()); |
| 68 | + |
| 69 | + posGraph->setData( |
| 70 | + Quantities::time.getUnit().fromBase(states.time), |
| 71 | + Quantities::position.getUnit().fromBase(states.arrow_pos) |
| 72 | + ); |
| 73 | + |
| 74 | + velGraph->setData( |
| 75 | + Quantities::time.getUnit().fromBase(states.time), |
| 76 | + Quantities::velocity.getUnit().fromBase(states.arrow_vel) |
| 77 | + ); |
| 78 | + |
| 79 | + accGraph->setData( |
| 80 | + Quantities::time.getUnit().fromBase(states.time), |
| 81 | + Quantities::acceleration.getUnit().fromBase(states.arrow_acc) |
| 82 | + ); |
| 83 | + |
| 84 | + // Rescale axes without triggering ranges update |
| 85 | + { |
| 86 | + QSignalBlocker blocker1(posPlot->xAxis); |
| 87 | + posPlot->rescaleAxes(); |
| 88 | + |
| 89 | + QSignalBlocker blocker2(velPlot->xAxis); |
| 90 | + velPlot->rescaleAxes(); |
| 91 | + |
| 92 | + QSignalBlocker blocker3(accPlot->xAxis); |
| 93 | + accPlot->rescaleAxes(); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + posPlot->replot(); |
| 98 | + velPlot->replot(); |
| 99 | + accPlot->replot(); |
| 100 | +} |
0 commit comments