@@ -1,64 +1,64 | |||||
1 | #ifndef SCIQLOP_VISUALIZATIONGRAPHWIDGET_H |
|
1 | #ifndef SCIQLOP_VISUALIZATIONGRAPHWIDGET_H | |
2 | #define SCIQLOP_VISUALIZATIONGRAPHWIDGET_H |
|
2 | #define SCIQLOP_VISUALIZATIONGRAPHWIDGET_H | |
3 |
|
3 | |||
4 | #include "Visualization/IVisualizationWidget.h" |
|
4 | #include "Visualization/IVisualizationWidget.h" | |
5 |
|
5 | |||
6 | #include <QLoggingCategory> |
|
6 | #include <QLoggingCategory> | |
7 | #include <QWidget> |
|
7 | #include <QWidget> | |
8 |
|
8 | |||
9 | #include <memory> |
|
9 | #include <memory> | |
10 |
|
10 | |||
11 | #include <Common/spimpl.h> |
|
11 | #include <Common/spimpl.h> | |
12 |
|
12 | |||
13 | Q_DECLARE_LOGGING_CATEGORY(LOG_VisualizationGraphWidget) |
|
13 | Q_DECLARE_LOGGING_CATEGORY(LOG_VisualizationGraphWidget) | |
14 |
|
14 | |||
15 | class QCPRange; |
|
15 | class QCPRange; | |
16 | class SqpDateTime; |
|
16 | class SqpDateTime; | |
17 | class Variable; |
|
17 | class Variable; | |
18 |
|
18 | |||
19 | namespace Ui { |
|
19 | namespace Ui { | |
20 | class VisualizationGraphWidget; |
|
20 | class VisualizationGraphWidget; | |
21 | } // namespace Ui |
|
21 | } // namespace Ui | |
22 |
|
22 | |||
23 | class VisualizationGraphWidget : public QWidget, public IVisualizationWidget { |
|
23 | class VisualizationGraphWidget : public QWidget, public IVisualizationWidget { | |
24 | Q_OBJECT |
|
24 | Q_OBJECT | |
25 |
|
25 | |||
26 | public: |
|
26 | public: | |
27 | explicit VisualizationGraphWidget(const QString &name = {}, QWidget *parent = 0); |
|
27 | explicit VisualizationGraphWidget(const QString &name = {}, QWidget *parent = 0); | |
28 | virtual ~VisualizationGraphWidget(); |
|
28 | virtual ~VisualizationGraphWidget(); | |
29 |
|
29 | |||
30 | void addVariable(std::shared_ptr<Variable> variable); |
|
30 | void addVariable(std::shared_ptr<Variable> variable); | |
31 | void addVariableUsingGraph(std::shared_ptr<Variable> variable); |
|
31 | void addVariableUsingGraph(std::shared_ptr<Variable> variable); | |
32 | /// Removes a variable from the graph |
|
32 | /// Removes a variable from the graph | |
33 | void removeVariable(std::shared_ptr<Variable> variable) noexcept; |
|
33 | void removeVariable(std::shared_ptr<Variable> variable) noexcept; | |
34 |
|
34 | |||
35 | // IVisualizationWidget interface |
|
35 | // IVisualizationWidget interface | |
36 | void accept(IVisualizationWidgetVisitor *visitor) override; |
|
36 | void accept(IVisualizationWidgetVisitor *visitor) override; | |
37 | bool canDrop(const Variable &variable) const override; |
|
37 | bool canDrop(const Variable &variable) const override; | |
38 | QString name() const override; |
|
38 | QString name() const override; | |
39 |
|
39 | |||
40 | void updateDisplay(std::shared_ptr<Variable> variable); |
|
40 | void updateDisplay(std::shared_ptr<Variable> variable); | |
41 |
|
41 | |||
42 | signals: |
|
42 | signals: | |
43 | void requestDataLoading(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime); |
|
43 | void requestDataLoading(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime); | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | private: |
|
46 | private: | |
47 | Ui::VisualizationGraphWidget *ui; |
|
47 | Ui::VisualizationGraphWidget *ui; | |
48 |
|
48 | |||
49 | class VisualizationGraphWidgetPrivate; |
|
49 | class VisualizationGraphWidgetPrivate; | |
50 | spimpl::unique_impl_ptr<VisualizationGraphWidgetPrivate> impl; |
|
50 | spimpl::unique_impl_ptr<VisualizationGraphWidgetPrivate> impl; | |
51 |
|
51 | |||
52 | private slots: |
|
52 | private slots: | |
53 | /// Slot called when right clicking on the graph (displays a menu) |
|
53 | /// Slot called when right clicking on the graph (displays a menu) | |
54 | void onGraphMenuRequested(const QPoint &pos) noexcept; |
|
54 | void onGraphMenuRequested(const QPoint &pos) noexcept; | |
55 |
|
55 | |||
56 |
void onRangeChanged(const QCPRange &t1 |
|
56 | void onRangeChanged(const QCPRange &t1); | |
57 |
|
57 | |||
58 | /// Slot called when a mouse wheel was made, to perform some processing before the zoom is done |
|
58 | /// Slot called when a mouse wheel was made, to perform some processing before the zoom is done | |
59 | void onMouseWheel(QWheelEvent *event) noexcept; |
|
59 | void onMouseWheel(QWheelEvent *event) noexcept; | |
60 |
|
60 | |||
61 | void onDataCacheVariableUpdated(); |
|
61 | void onDataCacheVariableUpdated(); | |
62 | }; |
|
62 | }; | |
63 |
|
63 | |||
64 | #endif // SCIQLOP_VISUALIZATIONGRAPHWIDGET_H |
|
64 | #endif // SCIQLOP_VISUALIZATIONGRAPHWIDGET_H |
@@ -1,153 +1,157 | |||||
1 | #include "Visualization/VisualizationGraphHelper.h" |
|
1 | #include "Visualization/VisualizationGraphHelper.h" | |
2 | #include "Visualization/qcustomplot.h" |
|
2 | #include "Visualization/qcustomplot.h" | |
3 |
|
3 | |||
4 | #include <Data/ScalarSeries.h> |
|
4 | #include <Data/ScalarSeries.h> | |
5 |
|
5 | |||
6 | #include <Variable/Variable.h> |
|
6 | #include <Variable/Variable.h> | |
7 |
|
7 | |||
8 | #include <QElapsedTimer> |
|
8 | #include <QElapsedTimer> | |
9 |
|
9 | |||
10 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper") |
|
10 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper") | |
11 |
|
11 | |||
12 | namespace { |
|
12 | namespace { | |
13 |
|
13 | |||
14 | /// Format for datetimes on a axis |
|
14 | /// Format for datetimes on a axis | |
15 | const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss"); |
|
15 | const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss"); | |
16 |
|
16 | |||
17 | /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or |
|
17 | /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or | |
18 | /// non-time data |
|
18 | /// non-time data | |
19 | QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis) |
|
19 | QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis) | |
20 | { |
|
20 | { | |
21 | if (isTimeAxis) { |
|
21 | if (isTimeAxis) { | |
22 | auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create(); |
|
22 | auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create(); | |
23 | dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT); |
|
23 | dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT); | |
24 |
|
24 | |||
25 | return dateTicker; |
|
25 | return dateTicker; | |
26 | } |
|
26 | } | |
27 | else { |
|
27 | else { | |
28 | // default ticker |
|
28 | // default ticker | |
29 | return QSharedPointer<QCPAxisTicker>::create(); |
|
29 | return QSharedPointer<QCPAxisTicker>::create(); | |
30 | } |
|
30 | } | |
31 | } |
|
31 | } | |
32 |
|
32 | |||
33 | void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSeries, |
|
33 | void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSeries, | |
34 | const SqpDateTime &dateTime) |
|
34 | const SqpDateTime &dateTime) | |
35 | { |
|
35 | { | |
36 | QElapsedTimer timer; |
|
36 | QElapsedTimer timer; | |
37 | timer.start(); |
|
37 | timer.start(); | |
38 | if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) { |
|
38 | if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) { | |
39 | // Clean the graph |
|
39 | // Clean the graph | |
40 | // NAIVE approch |
|
40 | // NAIVE approch | |
41 | const auto &xData = scalarSeries.xAxisData()->data(); |
|
41 | const auto &xData = scalarSeries.xAxisData()->data(); | |
42 | const auto &valuesData = scalarSeries.valuesData()->data(); |
|
42 | const auto &valuesData = scalarSeries.valuesData()->data(); | |
43 | const auto count = xData.count(); |
|
43 | const auto count = xData.count(); | |
44 | qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache" << xData.count(); |
|
44 | qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache" << xData.count(); | |
45 |
|
45 | |||
46 | auto xValue = QVector<double>(count); |
|
46 | auto xValue = QVector<double>(count); | |
47 | auto vValue = QVector<double>(count); |
|
47 | auto vValue = QVector<double>(count); | |
48 |
|
48 | |||
49 | int n = 0; |
|
49 | int n = 0; | |
50 | for (auto i = 0; i < count; ++i) { |
|
50 | for (auto i = 0; i < count; ++i) { | |
51 | const auto x = xData[i]; |
|
51 | const auto x = xData[i]; | |
52 | if (x >= dateTime.m_TStart && x <= dateTime.m_TEnd) { |
|
52 | if (x >= dateTime.m_TStart && x <= dateTime.m_TEnd) { | |
53 | xValue[n] = x; |
|
53 | xValue[n] = x; | |
54 | vValue[n] = valuesData[i]; |
|
54 | vValue[n] = valuesData[i]; | |
55 | ++n; |
|
55 | ++n; | |
56 | } |
|
56 | } | |
57 | } |
|
57 | } | |
58 |
|
58 | |||
59 | xValue.resize(n); |
|
59 | xValue.resize(n); | |
60 | vValue.resize(n); |
|
60 | vValue.resize(n); | |
61 |
|
61 | |||
62 | qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points displayed" |
|
62 | qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points displayed" << xValue.count() | |
63 |
<< |
|
63 | << dateTime; | |
64 |
|
64 | |||
65 | qcpGraph->setData(xValue, vValue); |
|
65 | qcpGraph->setData(xValue, vValue); | |
|
66 | ||||
|
67 | // Display all data | |||
|
68 | // component->parentPlot()->xAxis->setRange(dateTime.m_TStart, dateTime.m_TEnd); | |||
|
69 | component->rescaleAxes(); | |||
|
70 | component->parentPlot()->replot(); | |||
66 | } |
|
71 | } | |
67 | else { |
|
72 | else { | |
68 | /// @todo DEBUG |
|
73 | /// @todo DEBUG | |
69 | } |
|
74 | } | |
70 | } |
|
75 | } | |
71 |
|
76 | |||
72 | QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot, |
|
77 | QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot, | |
73 | const SqpDateTime &dateTime) |
|
78 | const SqpDateTime &dateTime) | |
74 | { |
|
79 | { | |
75 | auto component = plot.addGraph(); |
|
80 | auto component = plot.addGraph(); | |
76 |
|
81 | |||
77 | if (component) { |
|
82 | if (component) { | |
78 | // // Graph data |
|
83 | // // Graph data | |
79 | component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(), |
|
84 | component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(), | |
80 | true); |
|
85 | true); | |
81 |
|
86 | |||
82 | updateScalarData(component, scalarSeries, dateTime); |
|
87 | updateScalarData(component, scalarSeries, dateTime); | |
83 |
|
88 | |||
84 | // Axes properties |
|
89 | // Axes properties | |
85 | /// @todo : for the moment, no control is performed on the axes: the units and the tickers |
|
90 | /// @todo : for the moment, no control is performed on the axes: the units and the tickers | |
86 | /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph |
|
91 | /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph | |
87 |
|
92 | |||
88 | auto setAxisProperties = [](auto axis, const auto &unit) { |
|
93 | auto setAxisProperties = [](auto axis, const auto &unit) { | |
89 | // label (unit name) |
|
94 | // label (unit name) | |
90 | axis->setLabel(unit.m_Name); |
|
95 | axis->setLabel(unit.m_Name); | |
91 |
|
96 | |||
92 | // ticker (depending on the type of unit) |
|
97 | // ticker (depending on the type of unit) | |
93 | axis->setTicker(axisTicker(unit.m_TimeUnit)); |
|
98 | axis->setTicker(axisTicker(unit.m_TimeUnit)); | |
94 | }; |
|
99 | }; | |
95 | setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit()); |
|
100 | setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit()); | |
96 | setAxisProperties(plot.yAxis, scalarSeries.valuesUnit()); |
|
101 | setAxisProperties(plot.yAxis, scalarSeries.valuesUnit()); | |
97 |
|
102 | |||
98 | // Display all data |
|
103 | // Display all data | |
99 | component->rescaleAxes(); |
|
104 | component->rescaleAxes(); | |
100 |
|
||||
101 | plot.replot(); |
|
105 | plot.replot(); | |
102 | } |
|
106 | } | |
103 | else { |
|
107 | else { | |
104 | qCDebug(LOG_VisualizationGraphHelper()) |
|
108 | qCDebug(LOG_VisualizationGraphHelper()) | |
105 | << QObject::tr("Can't create graph for the scalar series"); |
|
109 | << QObject::tr("Can't create graph for the scalar series"); | |
106 | } |
|
110 | } | |
107 |
|
111 | |||
108 | return component; |
|
112 | return component; | |
109 | } |
|
113 | } | |
110 |
|
114 | |||
111 | } // namespace |
|
115 | } // namespace | |
112 |
|
116 | |||
113 | QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable, |
|
117 | QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable, | |
114 | QCustomPlot &plot) noexcept |
|
118 | QCustomPlot &plot) noexcept | |
115 | { |
|
119 | { | |
116 | auto result = QVector<QCPAbstractPlottable *>{}; |
|
120 | auto result = QVector<QCPAbstractPlottable *>{}; | |
117 |
|
121 | |||
118 | if (variable) { |
|
122 | if (variable) { | |
119 | // Gets the data series of the variable to call the creation of the right components |
|
123 | // Gets the data series of the variable to call the creation of the right components | |
120 | // according to its type |
|
124 | // according to its type | |
121 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) { |
|
125 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) { | |
122 | result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime())); |
|
126 | result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime())); | |
123 | } |
|
127 | } | |
124 | else { |
|
128 | else { | |
125 | qCDebug(LOG_VisualizationGraphHelper()) |
|
129 | qCDebug(LOG_VisualizationGraphHelper()) | |
126 | << QObject::tr("Can't create graph plottables : unmanaged data series type"); |
|
130 | << QObject::tr("Can't create graph plottables : unmanaged data series type"); | |
127 | } |
|
131 | } | |
128 | } |
|
132 | } | |
129 | else { |
|
133 | else { | |
130 | qCDebug(LOG_VisualizationGraphHelper()) |
|
134 | qCDebug(LOG_VisualizationGraphHelper()) | |
131 | << QObject::tr("Can't create graph plottables : the variable is null"); |
|
135 | << QObject::tr("Can't create graph plottables : the variable is null"); | |
132 | } |
|
136 | } | |
133 |
|
137 | |||
134 | return result; |
|
138 | return result; | |
135 | } |
|
139 | } | |
136 |
|
140 | |||
137 | void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect, |
|
141 | void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect, | |
138 | IDataSeries *dataSeries, const SqpDateTime &dateTime) |
|
142 | IDataSeries *dataSeries, const SqpDateTime &dateTime) | |
139 | { |
|
143 | { | |
140 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) { |
|
144 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) { | |
141 | if (plotableVect.size() == 1) { |
|
145 | if (plotableVect.size() == 1) { | |
142 | updateScalarData(plotableVect.at(0), *scalarSeries, dateTime); |
|
146 | updateScalarData(plotableVect.at(0), *scalarSeries, dateTime); | |
143 | } |
|
147 | } | |
144 | else { |
|
148 | else { | |
145 | qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr( |
|
149 | qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr( | |
146 | "Can't update Data of a scalarSeries because there is not only one component " |
|
150 | "Can't update Data of a scalarSeries because there is not only one component " | |
147 | "associated"); |
|
151 | "associated"); | |
148 | } |
|
152 | } | |
149 | } |
|
153 | } | |
150 | else { |
|
154 | else { | |
151 | /// @todo DEBUG |
|
155 | /// @todo DEBUG | |
152 | } |
|
156 | } | |
153 | } |
|
157 | } |
@@ -1,268 +1,269 | |||||
1 | #include "Visualization/VisualizationGraphWidget.h" |
|
1 | #include "Visualization/VisualizationGraphWidget.h" | |
2 | #include "Visualization/IVisualizationWidgetVisitor.h" |
|
2 | #include "Visualization/IVisualizationWidgetVisitor.h" | |
3 | #include "Visualization/VisualizationGraphHelper.h" |
|
3 | #include "Visualization/VisualizationGraphHelper.h" | |
4 | #include "ui_VisualizationGraphWidget.h" |
|
4 | #include "ui_VisualizationGraphWidget.h" | |
5 |
|
5 | |||
6 | #include <Data/ArrayData.h> |
|
6 | #include <Data/ArrayData.h> | |
7 | #include <Data/IDataSeries.h> |
|
7 | #include <Data/IDataSeries.h> | |
8 | #include <SqpApplication.h> |
|
8 | #include <SqpApplication.h> | |
9 | #include <Variable/Variable.h> |
|
9 | #include <Variable/Variable.h> | |
10 | #include <Variable/VariableController.h> |
|
10 | #include <Variable/VariableController.h> | |
11 |
|
11 | |||
12 | #include <unordered_map> |
|
12 | #include <unordered_map> | |
13 |
|
13 | |||
14 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget") |
|
14 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget") | |
15 |
|
15 | |||
16 | namespace { |
|
16 | namespace { | |
17 |
|
17 | |||
18 | /// Key pressed to enable zoom on horizontal axis |
|
18 | /// Key pressed to enable zoom on horizontal axis | |
19 | const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier; |
|
19 | const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier; | |
20 |
|
20 | |||
21 | /// Key pressed to enable zoom on vertical axis |
|
21 | /// Key pressed to enable zoom on vertical axis | |
22 | const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier; |
|
22 | const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier; | |
23 |
|
23 | |||
24 | } // namespace |
|
24 | } // namespace | |
25 |
|
25 | |||
26 | struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate { |
|
26 | struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate { | |
27 |
|
27 | |||
28 | // 1 variable -> n qcpplot |
|
28 | // 1 variable -> n qcpplot | |
29 | std::multimap<std::shared_ptr<Variable>, QCPAbstractPlottable *> m_VariableToPlotMultiMap; |
|
29 | std::multimap<std::shared_ptr<Variable>, QCPAbstractPlottable *> m_VariableToPlotMultiMap; | |
30 | }; |
|
30 | }; | |
31 |
|
31 | |||
32 | VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent) |
|
32 | VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent) | |
33 | : QWidget{parent}, |
|
33 | : QWidget{parent}, | |
34 | ui{new Ui::VisualizationGraphWidget}, |
|
34 | ui{new Ui::VisualizationGraphWidget}, | |
35 | impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()} |
|
35 | impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()} | |
36 | { |
|
36 | { | |
37 | ui->setupUi(this); |
|
37 | ui->setupUi(this); | |
38 |
|
38 | |||
39 | ui->graphNameLabel->setText(name); |
|
39 | ui->graphNameLabel->setText(name); | |
40 |
|
40 | |||
41 | // 'Close' options : widget is deleted when closed |
|
41 | // 'Close' options : widget is deleted when closed | |
42 | setAttribute(Qt::WA_DeleteOnClose); |
|
42 | setAttribute(Qt::WA_DeleteOnClose); | |
43 | connect(ui->closeButton, &QToolButton::clicked, this, &VisualizationGraphWidget::close); |
|
43 | connect(ui->closeButton, &QToolButton::clicked, this, &VisualizationGraphWidget::close); | |
44 | ui->closeButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton)); |
|
44 | ui->closeButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton)); | |
45 |
|
45 | |||
46 | // Set qcpplot properties : |
|
46 | // Set qcpplot properties : | |
47 | // - Drag (on x-axis) and zoom are enabled |
|
47 | // - Drag (on x-axis) and zoom are enabled | |
48 | // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation |
|
48 | // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation | |
49 | ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); |
|
49 | ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); | |
50 | ui->widget->axisRect()->setRangeDrag(Qt::Horizontal); |
|
50 | ui->widget->axisRect()->setRangeDrag(Qt::Horizontal); | |
51 | connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel); |
|
51 | connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel); | |
52 | connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>( |
|
52 | connect(ui->widget->xAxis, | |
53 | &QCPAxis::rangeChanged), |
|
53 | static_cast<void (QCPAxis::*)(const QCPRange &)>(&QCPAxis::rangeChanged), this, | |
54 |
|
|
54 | &VisualizationGraphWidget::onRangeChanged); | |
55 |
|
55 | |||
56 | // Activates menu when right clicking on the graph |
|
56 | // Activates menu when right clicking on the graph | |
57 | ui->widget->setContextMenuPolicy(Qt::CustomContextMenu); |
|
57 | ui->widget->setContextMenuPolicy(Qt::CustomContextMenu); | |
58 | connect(ui->widget, &QCustomPlot::customContextMenuRequested, this, |
|
58 | connect(ui->widget, &QCustomPlot::customContextMenuRequested, this, | |
59 | &VisualizationGraphWidget::onGraphMenuRequested); |
|
59 | &VisualizationGraphWidget::onGraphMenuRequested); | |
60 |
|
60 | |||
61 | connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(), |
|
61 | connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(), | |
62 | &VariableController::onRequestDataLoading); |
|
62 | &VariableController::onRequestDataLoading); | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 |
|
65 | |||
66 | VisualizationGraphWidget::~VisualizationGraphWidget() |
|
66 | VisualizationGraphWidget::~VisualizationGraphWidget() | |
67 | { |
|
67 | { | |
68 | delete ui; |
|
68 | delete ui; | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable) |
|
71 | void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable) | |
72 | { |
|
72 | { | |
73 | // Uses delegate to create the qcpplot components according to the variable |
|
73 | // Uses delegate to create the qcpplot components according to the variable | |
74 | auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); |
|
74 | auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); | |
75 |
|
75 | |||
76 | for (auto createdPlottable : qAsConst(createdPlottables)) { |
|
76 | for (auto createdPlottable : qAsConst(createdPlottables)) { | |
77 | impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable}); |
|
77 | impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable}); | |
78 | } |
|
78 | } | |
79 |
|
79 | |||
80 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); |
|
80 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
83 | void VisualizationGraphWidget::addVariableUsingGraph(std::shared_ptr<Variable> variable) |
|
83 | void VisualizationGraphWidget::addVariableUsingGraph(std::shared_ptr<Variable> variable) | |
84 | { |
|
84 | { | |
85 |
|
85 | |||
86 | // when adding a variable, we need to set its time range to the current graph range |
|
86 | // when adding a variable, we need to set its time range to the current graph range | |
87 | auto grapheRange = ui->widget->xAxis->range(); |
|
87 | auto grapheRange = ui->widget->xAxis->range(); | |
88 | auto dateTime = SqpDateTime{grapheRange.lower, grapheRange.upper}; |
|
88 | auto dateTime = SqpDateTime{grapheRange.lower, grapheRange.upper}; | |
89 | variable->setDateTime(dateTime); |
|
89 | variable->setDateTime(dateTime); | |
90 | qCInfo(LOG_VisualizationGraphWidget()) << "ADD Variable with range : " << dateTime; |
|
90 | qCInfo(LOG_VisualizationGraphWidget()) << "ADD Variable with range : " << dateTime; | |
91 |
|
91 | |||
92 | auto variableDateTimeWithTolerance = dateTime; |
|
92 | auto variableDateTimeWithTolerance = dateTime; | |
93 |
|
93 | |||
94 | // add 10% tolerance for each side |
|
94 | // add 10% tolerance for each side | |
95 | auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); |
|
95 | auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); | |
96 | variableDateTimeWithTolerance.m_TStart -= tolerance; |
|
96 | variableDateTimeWithTolerance.m_TStart -= tolerance; | |
97 | variableDateTimeWithTolerance.m_TEnd += tolerance; |
|
97 | variableDateTimeWithTolerance.m_TEnd += tolerance; | |
98 |
|
98 | |||
99 | qCInfo(LOG_VisualizationGraphWidget()) << "ADD Variable with range TOL: " |
|
99 | qCInfo(LOG_VisualizationGraphWidget()) << "ADD Variable with range TOL: " | |
100 | << variableDateTimeWithTolerance; |
|
100 | << variableDateTimeWithTolerance; | |
101 |
|
101 | |||
102 | // Uses delegate to create the qcpplot components according to the variable |
|
102 | // Uses delegate to create the qcpplot components according to the variable | |
103 | auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); |
|
103 | auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); | |
104 |
|
104 | |||
105 | for (auto createdPlottable : qAsConst(createdPlottables)) { |
|
105 | for (auto createdPlottable : qAsConst(createdPlottables)) { | |
106 | impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable}); |
|
106 | impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable}); | |
107 | } |
|
107 | } | |
108 |
|
108 | |||
109 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); |
|
109 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); | |
110 |
|
110 | |||
111 | // CHangement detected, we need to ask controller to request data loading |
|
111 | // CHangement detected, we need to ask controller to request data loading | |
112 | emit requestDataLoading(variable, variableDateTimeWithTolerance); |
|
112 | emit requestDataLoading(variable, variableDateTimeWithTolerance); | |
113 | } |
|
113 | } | |
114 |
|
114 | |||
115 | void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept |
|
115 | void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept | |
116 | { |
|
116 | { | |
117 | // Each component associated to the variable : |
|
117 | // Each component associated to the variable : | |
118 | // - is removed from qcpplot (which deletes it) |
|
118 | // - is removed from qcpplot (which deletes it) | |
119 | // - is no longer referenced in the map |
|
119 | // - is no longer referenced in the map | |
120 | auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable); |
|
120 | auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable); | |
121 | for (auto it = componentsIt.first; it != componentsIt.second;) { |
|
121 | for (auto it = componentsIt.first; it != componentsIt.second;) { | |
122 | ui->widget->removePlottable(it->second); |
|
122 | ui->widget->removePlottable(it->second); | |
123 | it = impl->m_VariableToPlotMultiMap.erase(it); |
|
123 | it = impl->m_VariableToPlotMultiMap.erase(it); | |
124 | } |
|
124 | } | |
125 |
|
125 | |||
126 | // Updates graph |
|
126 | // Updates graph | |
127 | ui->widget->replot(); |
|
127 | ui->widget->replot(); | |
128 | } |
|
128 | } | |
129 |
|
129 | |||
130 | void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor) |
|
130 | void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor) | |
131 | { |
|
131 | { | |
132 | if (visitor) { |
|
132 | if (visitor) { | |
133 | visitor->visit(this); |
|
133 | visitor->visit(this); | |
134 | } |
|
134 | } | |
135 | else { |
|
135 | else { | |
136 | qCCritical(LOG_VisualizationGraphWidget()) |
|
136 | qCCritical(LOG_VisualizationGraphWidget()) | |
137 | << tr("Can't visit widget : the visitor is null"); |
|
137 | << tr("Can't visit widget : the visitor is null"); | |
138 | } |
|
138 | } | |
139 | } |
|
139 | } | |
140 |
|
140 | |||
141 | bool VisualizationGraphWidget::canDrop(const Variable &variable) const |
|
141 | bool VisualizationGraphWidget::canDrop(const Variable &variable) const | |
142 | { |
|
142 | { | |
143 | /// @todo : for the moment, a graph can always accomodate a variable |
|
143 | /// @todo : for the moment, a graph can always accomodate a variable | |
144 | Q_UNUSED(variable); |
|
144 | Q_UNUSED(variable); | |
145 | return true; |
|
145 | return true; | |
146 | } |
|
146 | } | |
147 |
|
147 | |||
148 | QString VisualizationGraphWidget::name() const |
|
148 | QString VisualizationGraphWidget::name() const | |
149 | { |
|
149 | { | |
150 | return ui->graphNameLabel->text(); |
|
150 | return ui->graphNameLabel->text(); | |
151 | } |
|
151 | } | |
152 |
|
152 | |||
153 | void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept |
|
153 | void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept | |
154 | { |
|
154 | { | |
155 | QMenu graphMenu{}; |
|
155 | QMenu graphMenu{}; | |
156 |
|
156 | |||
157 | // Iterates on variables (unique keys) |
|
157 | // Iterates on variables (unique keys) | |
158 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(), |
|
158 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(), | |
159 | end = impl->m_VariableToPlotMultiMap.cend(); |
|
159 | end = impl->m_VariableToPlotMultiMap.cend(); | |
160 | it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) { |
|
160 | it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) { | |
161 | // 'Remove variable' action |
|
161 | // 'Remove variable' action | |
162 | graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()), |
|
162 | graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()), | |
163 | [ this, var = it->first ]() { removeVariable(var); }); |
|
163 | [ this, var = it->first ]() { removeVariable(var); }); | |
164 | } |
|
164 | } | |
165 |
|
165 | |||
166 | if (!graphMenu.isEmpty()) { |
|
166 | if (!graphMenu.isEmpty()) { | |
167 | graphMenu.exec(mapToGlobal(pos)); |
|
167 | graphMenu.exec(mapToGlobal(pos)); | |
168 | } |
|
168 | } | |
169 | } |
|
169 | } | |
170 |
|
170 | |||
171 |
void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1 |
|
171 | void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1) | |
172 | { |
|
172 | { | |
173 |
|
173 | |||
174 |
qC |
|
174 | qCInfo(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged"); | |
175 |
|
175 | |||
176 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); |
|
176 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); | |
177 | it != impl->m_VariableToPlotMultiMap.cend(); ++it) { |
|
177 | it != impl->m_VariableToPlotMultiMap.cend(); ++it) { | |
178 |
|
178 | |||
179 | auto variable = it->first; |
|
179 | auto variable = it->first; | |
180 |
auto dateTime = SqpDateTime{t |
|
180 | auto dateTime = SqpDateTime{t1.lower, t1.upper}; | |
181 |
|
181 | |||
182 | if (!variable->contains(dateTime)) { |
|
182 | if (!variable->contains(dateTime)) { | |
|
183 | qCInfo(LOG_VisualizationGraphWidget()) << dateTime << variable->dateTime(); | |||
183 |
|
184 | |||
184 | auto variableDateTimeWithTolerance = dateTime; |
|
185 | auto variableDateTimeWithTolerance = dateTime; | |
185 | if (variable->intersect(dateTime)) { |
|
186 | if (variable->intersect(dateTime)) { | |
186 | auto variableDateTime = variable->dateTime(); |
|
187 | auto variableDateTime = variable->dateTime(); | |
187 | if (variableDateTime.m_TStart < dateTime.m_TStart) { |
|
188 | if (variableDateTime.m_TStart < dateTime.m_TStart) { | |
188 |
|
189 | |||
189 | auto diffEndToKeepDelta = dateTime.m_TEnd - variableDateTime.m_TEnd; |
|
190 | auto diffEndToKeepDelta = dateTime.m_TEnd - variableDateTime.m_TEnd; | |
190 | dateTime.m_TStart = variableDateTime.m_TStart + diffEndToKeepDelta; |
|
191 | dateTime.m_TStart = variableDateTime.m_TStart + diffEndToKeepDelta; | |
191 | // Tolerance have to be added to the right |
|
192 | // Tolerance have to be added to the right | |
192 | // add 10% tolerance for right (end) side |
|
193 | // add 10% tolerance for right (end) side | |
193 | auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); |
|
194 | auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); | |
194 | variableDateTimeWithTolerance.m_TEnd += tolerance; |
|
195 | variableDateTimeWithTolerance.m_TEnd += tolerance; | |
195 | } |
|
196 | } | |
196 | if (variableDateTime.m_TEnd > dateTime.m_TEnd) { |
|
197 | if (variableDateTime.m_TEnd > dateTime.m_TEnd) { | |
197 | auto diffStartToKeepDelta = variableDateTime.m_TStart - dateTime.m_TStart; |
|
198 | auto diffStartToKeepDelta = variableDateTime.m_TStart - dateTime.m_TStart; | |
198 | dateTime.m_TEnd = variableDateTime.m_TEnd - diffStartToKeepDelta; |
|
199 | dateTime.m_TEnd = variableDateTime.m_TEnd - diffStartToKeepDelta; | |
199 | // Tolerance have to be added to the left |
|
200 | // Tolerance have to be added to the left | |
200 | // add 10% tolerance for left (start) side |
|
201 | // add 10% tolerance for left (start) side | |
201 | auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); |
|
202 | auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); | |
202 | variableDateTimeWithTolerance.m_TStart -= tolerance; |
|
203 | variableDateTimeWithTolerance.m_TStart -= tolerance; | |
203 | } |
|
204 | } | |
204 | } |
|
205 | } | |
205 | else { |
|
206 | else { | |
206 | // add 10% tolerance for each side |
|
207 | // add 10% tolerance for each side | |
207 | auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); |
|
208 | auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); | |
208 | variableDateTimeWithTolerance.m_TStart -= tolerance; |
|
209 | variableDateTimeWithTolerance.m_TStart -= tolerance; | |
209 | variableDateTimeWithTolerance.m_TEnd += tolerance; |
|
210 | variableDateTimeWithTolerance.m_TEnd += tolerance; | |
210 | } |
|
211 | } | |
211 | variable->setDateTime(dateTime); |
|
212 | variable->setDateTime(dateTime); | |
212 |
|
213 | |||
213 | // CHangement detected, we need to ask controller to request data loading |
|
214 | // CHangement detected, we need to ask controller to request data loading | |
214 | emit requestDataLoading(variable, variableDateTimeWithTolerance); |
|
215 | emit requestDataLoading(variable, variableDateTimeWithTolerance); | |
215 | } |
|
216 | } | |
216 | } |
|
217 | } | |
217 | } |
|
218 | } | |
218 |
|
219 | |||
219 | void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept |
|
220 | void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept | |
220 | { |
|
221 | { | |
221 | auto zoomOrientations = QFlags<Qt::Orientation>{}; |
|
222 | auto zoomOrientations = QFlags<Qt::Orientation>{}; | |
222 |
|
223 | |||
223 | // Lambda that enables a zoom orientation if the key modifier related to this orientation |
|
224 | // Lambda that enables a zoom orientation if the key modifier related to this orientation | |
224 | // has |
|
225 | // has | |
225 | // been pressed |
|
226 | // been pressed | |
226 | auto enableOrientation |
|
227 | auto enableOrientation | |
227 | = [&zoomOrientations, event](const auto &orientation, const auto &modifier) { |
|
228 | = [&zoomOrientations, event](const auto &orientation, const auto &modifier) { | |
228 | auto orientationEnabled = event->modifiers().testFlag(modifier); |
|
229 | auto orientationEnabled = event->modifiers().testFlag(modifier); | |
229 | zoomOrientations.setFlag(orientation, orientationEnabled); |
|
230 | zoomOrientations.setFlag(orientation, orientationEnabled); | |
230 | }; |
|
231 | }; | |
231 | enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER); |
|
232 | enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER); | |
232 | enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER); |
|
233 | enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER); | |
233 |
|
234 | |||
234 | ui->widget->axisRect()->setRangeZoom(zoomOrientations); |
|
235 | ui->widget->axisRect()->setRangeZoom(zoomOrientations); | |
235 | } |
|
236 | } | |
236 |
|
237 | |||
237 | void VisualizationGraphWidget::onDataCacheVariableUpdated() |
|
238 | void VisualizationGraphWidget::onDataCacheVariableUpdated() | |
238 | { |
|
239 | { | |
239 | // NOTE: |
|
240 | // NOTE: | |
240 | // We don't want to call the method for each component of a variable unitarily, but for |
|
241 | // We don't want to call the method for each component of a variable unitarily, but for | |
241 | // all |
|
242 | // all | |
242 | // its components at once (eg its three components in the case of a vector). |
|
243 | // its components at once (eg its three components in the case of a vector). | |
243 |
|
244 | |||
244 | // The unordered_multimap does not do this easily, so the question is whether to: |
|
245 | // The unordered_multimap does not do this easily, so the question is whether to: | |
245 | // - use an ordered_multimap and the algos of std to group the values by key |
|
246 | // - use an ordered_multimap and the algos of std to group the values by key | |
246 | // - use a map (unique keys) and store as values directly the list of components |
|
247 | // - use a map (unique keys) and store as values directly the list of components | |
247 |
|
248 | |||
248 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); |
|
249 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); | |
249 | it != impl->m_VariableToPlotMultiMap.cend(); ++it) { |
|
250 | it != impl->m_VariableToPlotMultiMap.cend(); ++it) { | |
250 | auto variable = it->first; |
|
251 | auto variable = it->first; | |
251 | VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second, |
|
252 | VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second, | |
252 | variable->dataSeries(), variable->dateTime()); |
|
253 | variable->dataSeries(), variable->dateTime()); | |
253 | } |
|
254 | } | |
254 | } |
|
255 | } | |
255 |
|
256 | |||
256 | void VisualizationGraphWidget::updateDisplay(std::shared_ptr<Variable> variable) |
|
257 | void VisualizationGraphWidget::updateDisplay(std::shared_ptr<Variable> variable) | |
257 | { |
|
258 | { | |
258 | auto abstractPlotableItPair = impl->m_VariableToPlotMultiMap.equal_range(variable); |
|
259 | auto abstractPlotableItPair = impl->m_VariableToPlotMultiMap.equal_range(variable); | |
259 |
|
260 | |||
260 | auto abstractPlotableVect = QVector<QCPAbstractPlottable *>{}; |
|
261 | auto abstractPlotableVect = QVector<QCPAbstractPlottable *>{}; | |
261 |
|
262 | |||
262 | for (auto it = abstractPlotableItPair.first; it != abstractPlotableItPair.second; ++it) { |
|
263 | for (auto it = abstractPlotableItPair.first; it != abstractPlotableItPair.second; ++it) { | |
263 | abstractPlotableVect.push_back(it->second); |
|
264 | abstractPlotableVect.push_back(it->second); | |
264 | } |
|
265 | } | |
265 |
|
266 | |||
266 | VisualizationGraphHelper::updateData(abstractPlotableVect, variable->dataSeries(), |
|
267 | VisualizationGraphHelper::updateData(abstractPlotableVect, variable->dataSeries(), | |
267 | variable->dateTime()); |
|
268 | variable->dateTime()); | |
268 | } |
|
269 | } |
General Comments 0
You need to be logged in to leave comments.
Login now