@@ -1,160 +1,159 | |||||
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 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper") |
|
8 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper") | |
9 |
|
9 | |||
10 | namespace { |
|
10 | namespace { | |
11 |
|
11 | |||
12 | class SqpDataContainer : public QCPGraphDataContainer { |
|
12 | class SqpDataContainer : public QCPGraphDataContainer { | |
13 | public: |
|
13 | public: | |
14 | void appendGraphDataUnsorted(const QCPGraphData &data) { mData.append(data); } |
|
14 | void appendGraphDataUnsorted(const QCPGraphData &data) { mData.append(data); } | |
15 | }; |
|
15 | }; | |
16 |
|
16 | |||
17 |
|
17 | |||
18 | /// Format for datetimes on a axis |
|
18 | /// Format for datetimes on a axis | |
19 | const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss"); |
|
19 | const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss"); | |
20 |
|
20 | |||
21 | /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or |
|
21 | /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or | |
22 | /// non-time data |
|
22 | /// non-time data | |
23 | QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis) |
|
23 | QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis) | |
24 | { |
|
24 | { | |
25 | if (isTimeAxis) { |
|
25 | if (isTimeAxis) { | |
26 | auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create(); |
|
26 | auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create(); | |
27 | dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT); |
|
27 | dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT); | |
28 |
|
28 | |||
29 | return dateTicker; |
|
29 | return dateTicker; | |
30 | } |
|
30 | } | |
31 | else { |
|
31 | else { | |
32 | // default ticker |
|
32 | // default ticker | |
33 | return QSharedPointer<QCPAxisTicker>::create(); |
|
33 | return QSharedPointer<QCPAxisTicker>::create(); | |
34 | } |
|
34 | } | |
35 | } |
|
35 | } | |
36 |
|
36 | |||
37 | void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSeries, |
|
37 | void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSeries, | |
38 | const SqpDateTime &dateTime) |
|
38 | const SqpDateTime &dateTime) | |
39 | { |
|
39 | { | |
40 | qCDebug(LOG_VisualizationGraphHelper()) << "TORM: updateScalarData" |
|
40 | qCDebug(LOG_VisualizationGraphHelper()) << "TORM: updateScalarData" | |
41 | << QThread::currentThread()->objectName(); |
|
41 | << QThread::currentThread()->objectName(); | |
42 | if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) { |
|
42 | if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) { | |
43 | // Clean the graph |
|
43 | // Clean the graph | |
44 | // NAIVE approch |
|
44 | // NAIVE approch | |
45 | scalarSeries.lockRead(); |
|
45 | scalarSeries.lockRead(); | |
46 | { |
|
46 | { | |
47 | const auto xData = scalarSeries.xAxisData()->data(); |
|
47 | const auto xData = scalarSeries.xAxisData()->data(); | |
48 | const auto valuesData = scalarSeries.valuesData()->data(); |
|
48 | const auto valuesData = scalarSeries.valuesData()->data(); | |
49 | const auto count = xData.count(); |
|
49 | const auto count = xData.count(); | |
50 | qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache" |
|
50 | qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache" | |
51 | << xData.count(); |
|
51 | << xData.count(); | |
52 |
|
52 | |||
53 | auto dataContainer = qcpGraph->data(); |
|
53 | auto dataContainer = qcpGraph->data(); | |
54 | dataContainer->clear(); |
|
54 | dataContainer->clear(); | |
55 | auto sqpDataContainer = QSharedPointer<SqpDataContainer>::create(); |
|
55 | auto sqpDataContainer = QSharedPointer<SqpDataContainer>::create(); | |
56 | qcpGraph->setData(sqpDataContainer); |
|
56 | qcpGraph->setData(sqpDataContainer); | |
57 |
|
57 | |||
58 | for (auto i = 0; i < count; ++i) { |
|
58 | for (auto i = 0; i < count; ++i) { | |
59 | const auto x = xData[i]; |
|
59 | const auto x = xData[i]; | |
60 | if (x >= dateTime.m_TStart && x <= dateTime.m_TEnd) { |
|
60 | if (x >= dateTime.m_TStart && x <= dateTime.m_TEnd) { | |
61 | sqpDataContainer->appendGraphDataUnsorted(QCPGraphData(x, valuesData[i])); |
|
61 | sqpDataContainer->appendGraphDataUnsorted(QCPGraphData(x, valuesData[i])); | |
62 | } |
|
62 | } | |
63 | } |
|
63 | } | |
64 | sqpDataContainer->sort(); |
|
64 | sqpDataContainer->sort(); | |
65 | qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points displayed" |
|
65 | qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points displayed" | |
66 | << sqpDataContainer->size(); |
|
66 | << sqpDataContainer->size(); | |
67 | } |
|
67 | } | |
68 | scalarSeries.unlock(); |
|
68 | scalarSeries.unlock(); | |
69 |
|
69 | |||
70 |
|
70 | |||
71 | // Display all data |
|
71 | // Display all data | |
72 | component->rescaleAxes(); |
|
|||
73 | component->parentPlot()->replot(); |
|
72 | component->parentPlot()->replot(); | |
74 | } |
|
73 | } | |
75 | else { |
|
74 | else { | |
76 | /// @todo DEBUG |
|
75 | /// @todo DEBUG | |
77 | } |
|
76 | } | |
78 | } |
|
77 | } | |
79 |
|
78 | |||
80 | QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot, |
|
79 | QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot, | |
81 | const SqpDateTime &dateTime) |
|
80 | const SqpDateTime &dateTime) | |
82 | { |
|
81 | { | |
83 | auto component = plot.addGraph(); |
|
82 | auto component = plot.addGraph(); | |
84 |
|
83 | |||
85 | if (component) { |
|
84 | if (component) { | |
86 | // // Graph data |
|
85 | // // Graph data | |
87 | component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(), |
|
86 | component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(), | |
88 | true); |
|
87 | true); | |
89 |
|
88 | |||
90 | updateScalarData(component, scalarSeries, dateTime); |
|
89 | updateScalarData(component, scalarSeries, dateTime); | |
91 |
|
90 | |||
92 | // Axes properties |
|
91 | // Axes properties | |
93 | /// @todo : for the moment, no control is performed on the axes: the units and the tickers |
|
92 | /// @todo : for the moment, no control is performed on the axes: the units and the tickers | |
94 | /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph |
|
93 | /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph | |
95 |
|
94 | |||
96 | auto setAxisProperties = [](auto axis, const auto &unit) { |
|
95 | auto setAxisProperties = [](auto axis, const auto &unit) { | |
97 | // label (unit name) |
|
96 | // label (unit name) | |
98 | axis->setLabel(unit.m_Name); |
|
97 | axis->setLabel(unit.m_Name); | |
99 |
|
98 | |||
100 | // ticker (depending on the type of unit) |
|
99 | // ticker (depending on the type of unit) | |
101 | axis->setTicker(axisTicker(unit.m_TimeUnit)); |
|
100 | axis->setTicker(axisTicker(unit.m_TimeUnit)); | |
102 | }; |
|
101 | }; | |
103 | setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit()); |
|
102 | setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit()); | |
104 | setAxisProperties(plot.yAxis, scalarSeries.valuesUnit()); |
|
103 | setAxisProperties(plot.yAxis, scalarSeries.valuesUnit()); | |
105 |
|
104 | |||
106 | // Display all data |
|
105 | // Display all data | |
107 | component->rescaleAxes(); |
|
106 | component->rescaleAxes(); | |
108 | plot.replot(); |
|
107 | plot.replot(); | |
109 | } |
|
108 | } | |
110 | else { |
|
109 | else { | |
111 | qCDebug(LOG_VisualizationGraphHelper()) |
|
110 | qCDebug(LOG_VisualizationGraphHelper()) | |
112 | << QObject::tr("Can't create graph for the scalar series"); |
|
111 | << QObject::tr("Can't create graph for the scalar series"); | |
113 | } |
|
112 | } | |
114 |
|
113 | |||
115 | return component; |
|
114 | return component; | |
116 | } |
|
115 | } | |
117 |
|
116 | |||
118 | } // namespace |
|
117 | } // namespace | |
119 |
|
118 | |||
120 | QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable, |
|
119 | QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable, | |
121 | QCustomPlot &plot) noexcept |
|
120 | QCustomPlot &plot) noexcept | |
122 | { |
|
121 | { | |
123 | auto result = QVector<QCPAbstractPlottable *>{}; |
|
122 | auto result = QVector<QCPAbstractPlottable *>{}; | |
124 |
|
123 | |||
125 | if (variable) { |
|
124 | if (variable) { | |
126 | // Gets the data series of the variable to call the creation of the right components |
|
125 | // Gets the data series of the variable to call the creation of the right components | |
127 | // according to its type |
|
126 | // according to its type | |
128 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) { |
|
127 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) { | |
129 | result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime())); |
|
128 | result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime())); | |
130 | } |
|
129 | } | |
131 | else { |
|
130 | else { | |
132 | qCDebug(LOG_VisualizationGraphHelper()) |
|
131 | qCDebug(LOG_VisualizationGraphHelper()) | |
133 | << QObject::tr("Can't create graph plottables : unmanaged data series type"); |
|
132 | << QObject::tr("Can't create graph plottables : unmanaged data series type"); | |
134 | } |
|
133 | } | |
135 | } |
|
134 | } | |
136 | else { |
|
135 | else { | |
137 | qCDebug(LOG_VisualizationGraphHelper()) |
|
136 | qCDebug(LOG_VisualizationGraphHelper()) | |
138 | << QObject::tr("Can't create graph plottables : the variable is null"); |
|
137 | << QObject::tr("Can't create graph plottables : the variable is null"); | |
139 | } |
|
138 | } | |
140 |
|
139 | |||
141 | return result; |
|
140 | return result; | |
142 | } |
|
141 | } | |
143 |
|
142 | |||
144 | void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect, |
|
143 | void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect, | |
145 | IDataSeries *dataSeries, const SqpDateTime &dateTime) |
|
144 | IDataSeries *dataSeries, const SqpDateTime &dateTime) | |
146 | { |
|
145 | { | |
147 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) { |
|
146 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) { | |
148 | if (plotableVect.size() == 1) { |
|
147 | if (plotableVect.size() == 1) { | |
149 | updateScalarData(plotableVect.at(0), *scalarSeries, dateTime); |
|
148 | updateScalarData(plotableVect.at(0), *scalarSeries, dateTime); | |
150 | } |
|
149 | } | |
151 | else { |
|
150 | else { | |
152 | qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr( |
|
151 | qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr( | |
153 | "Can't update Data of a scalarSeries because there is not only one component " |
|
152 | "Can't update Data of a scalarSeries because there is not only one component " | |
154 | "associated"); |
|
153 | "associated"); | |
155 | } |
|
154 | } | |
156 | } |
|
155 | } | |
157 | else { |
|
156 | else { | |
158 | /// @todo DEBUG |
|
157 | /// @todo DEBUG | |
159 | } |
|
158 | } | |
160 | } |
|
159 | } |
General Comments 0
You need to be logged in to leave comments.
Login now