@@ -1,159 +1,161 | |||||
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 appendGraphData |
|
14 | void appendGraphData(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 |
|
|||
44 | // NAIVE approch |
|
|||
45 | scalarSeries.lockRead(); |
|
43 | scalarSeries.lockRead(); | |
46 | { |
|
44 | { | |
47 | const auto xData = scalarSeries.xAxisData()->data(); |
|
45 | const auto &xData = scalarSeries.xAxisData()->cdata(); | |
48 | const auto valuesData = scalarSeries.valuesData()->data(); |
|
46 | const auto &valuesData = scalarSeries.valuesData()->cdata(); | |
49 | const auto count = xData.count(); |
|
47 | ||
50 | qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache" |
|
48 | auto xDataBegin = xData.cbegin(); | |
51 | << xData.count(); |
|
49 | auto xDataEnd = xData.cend(); | |
52 |
|
50 | |||
53 | auto dataContainer = qcpGraph->data(); |
|
51 | qCInfo(LOG_VisualizationGraphHelper()) | |
54 | dataContainer->clear(); |
|
52 | << "TORM: Current points in cache" << xData.count(); | |
|
53 | ||||
55 | auto sqpDataContainer = QSharedPointer<SqpDataContainer>::create(); |
|
54 | auto sqpDataContainer = QSharedPointer<SqpDataContainer>::create(); | |
56 | qcpGraph->setData(sqpDataContainer); |
|
55 | qcpGraph->setData(sqpDataContainer); | |
57 |
|
56 | |||
58 | for (auto i = 0; i < count; ++i) { |
|
57 | auto lowerIt = std::lower_bound(xDataBegin, xDataEnd, dateTime.m_TStart); | |
59 | const auto x = xData[i]; |
|
58 | auto upperIt = std::upper_bound(xDataBegin, xDataEnd, dateTime.m_TEnd); | |
60 | if (x >= dateTime.m_TStart && x <= dateTime.m_TEnd) { |
|
59 | auto distance = std::distance(xDataBegin, lowerIt); | |
61 | sqpDataContainer->appendGraphDataUnsorted(QCPGraphData(x, valuesData[i])); |
|
60 | ||
62 | } |
|
61 | auto valuesDataIt = valuesData.cbegin() + distance; | |
|
62 | for (auto xAxisDataIt = lowerIt; xAxisDataIt != upperIt; | |||
|
63 | ++xAxisDataIt, ++valuesDataIt) { | |||
|
64 | sqpDataContainer->appendGraphData(QCPGraphData(*xAxisDataIt, *valuesDataIt)); | |||
63 | } |
|
65 | } | |
64 | sqpDataContainer->sort(); |
|
66 | ||
65 |
qCInfo(LOG_VisualizationGraphHelper()) |
|
67 | qCInfo(LOG_VisualizationGraphHelper()) | |
66 |
|
|
68 | << "TORM: Current points displayed" << sqpDataContainer->size(); | |
67 | } |
|
69 | } | |
68 | scalarSeries.unlock(); |
|
70 | scalarSeries.unlock(); | |
69 |
|
71 | |||
70 |
|
72 | |||
71 | // Display all data |
|
73 | // Display all data | |
72 | component->parentPlot()->replot(); |
|
74 | component->parentPlot()->replot(); | |
73 | } |
|
75 | } | |
74 | else { |
|
76 | else { | |
75 | /// @todo DEBUG |
|
77 | /// @todo DEBUG | |
76 | } |
|
78 | } | |
77 | } |
|
79 | } | |
78 |
|
80 | |||
79 | QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot, |
|
81 | QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot, | |
80 | const SqpDateTime &dateTime) |
|
82 | const SqpDateTime &dateTime) | |
81 | { |
|
83 | { | |
82 | auto component = plot.addGraph(); |
|
84 | auto component = plot.addGraph(); | |
83 |
|
85 | |||
84 | if (component) { |
|
86 | if (component) { | |
85 | // // Graph data |
|
87 | // // Graph data | |
86 | component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(), |
|
88 | component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(), | |
87 | true); |
|
89 | true); | |
88 |
|
90 | |||
89 | updateScalarData(component, scalarSeries, dateTime); |
|
91 | updateScalarData(component, scalarSeries, dateTime); | |
90 |
|
92 | |||
91 | // Axes properties |
|
93 | // Axes properties | |
92 | /// @todo : for the moment, no control is performed on the axes: the units and the tickers |
|
94 | /// @todo : for the moment, no control is performed on the axes: the units and the tickers | |
93 | /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph |
|
95 | /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph | |
94 |
|
96 | |||
95 | auto setAxisProperties = [](auto axis, const auto &unit) { |
|
97 | auto setAxisProperties = [](auto axis, const auto &unit) { | |
96 | // label (unit name) |
|
98 | // label (unit name) | |
97 | axis->setLabel(unit.m_Name); |
|
99 | axis->setLabel(unit.m_Name); | |
98 |
|
100 | |||
99 | // ticker (depending on the type of unit) |
|
101 | // ticker (depending on the type of unit) | |
100 | axis->setTicker(axisTicker(unit.m_TimeUnit)); |
|
102 | axis->setTicker(axisTicker(unit.m_TimeUnit)); | |
101 | }; |
|
103 | }; | |
102 | setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit()); |
|
104 | setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit()); | |
103 | setAxisProperties(plot.yAxis, scalarSeries.valuesUnit()); |
|
105 | setAxisProperties(plot.yAxis, scalarSeries.valuesUnit()); | |
104 |
|
106 | |||
105 | // Display all data |
|
107 | // Display all data | |
106 | component->rescaleAxes(); |
|
108 | component->rescaleAxes(); | |
107 | plot.replot(); |
|
109 | plot.replot(); | |
108 | } |
|
110 | } | |
109 | else { |
|
111 | else { | |
110 | qCDebug(LOG_VisualizationGraphHelper()) |
|
112 | qCDebug(LOG_VisualizationGraphHelper()) | |
111 | << QObject::tr("Can't create graph for the scalar series"); |
|
113 | << QObject::tr("Can't create graph for the scalar series"); | |
112 | } |
|
114 | } | |
113 |
|
115 | |||
114 | return component; |
|
116 | return component; | |
115 | } |
|
117 | } | |
116 |
|
118 | |||
117 | } // namespace |
|
119 | } // namespace | |
118 |
|
120 | |||
119 | QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable, |
|
121 | QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable, | |
120 | QCustomPlot &plot) noexcept |
|
122 | QCustomPlot &plot) noexcept | |
121 | { |
|
123 | { | |
122 | auto result = QVector<QCPAbstractPlottable *>{}; |
|
124 | auto result = QVector<QCPAbstractPlottable *>{}; | |
123 |
|
125 | |||
124 | if (variable) { |
|
126 | if (variable) { | |
125 | // Gets the data series of the variable to call the creation of the right components |
|
127 | // Gets the data series of the variable to call the creation of the right components | |
126 | // according to its type |
|
128 | // according to its type | |
127 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) { |
|
129 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) { | |
128 | result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime())); |
|
130 | result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime())); | |
129 | } |
|
131 | } | |
130 | else { |
|
132 | else { | |
131 | qCDebug(LOG_VisualizationGraphHelper()) |
|
133 | qCDebug(LOG_VisualizationGraphHelper()) | |
132 | << QObject::tr("Can't create graph plottables : unmanaged data series type"); |
|
134 | << QObject::tr("Can't create graph plottables : unmanaged data series type"); | |
133 | } |
|
135 | } | |
134 | } |
|
136 | } | |
135 | else { |
|
137 | else { | |
136 | qCDebug(LOG_VisualizationGraphHelper()) |
|
138 | qCDebug(LOG_VisualizationGraphHelper()) | |
137 | << QObject::tr("Can't create graph plottables : the variable is null"); |
|
139 | << QObject::tr("Can't create graph plottables : the variable is null"); | |
138 | } |
|
140 | } | |
139 |
|
141 | |||
140 | return result; |
|
142 | return result; | |
141 | } |
|
143 | } | |
142 |
|
144 | |||
143 | void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect, |
|
145 | void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect, | |
144 | IDataSeries *dataSeries, const SqpDateTime &dateTime) |
|
146 | IDataSeries *dataSeries, const SqpDateTime &dateTime) | |
145 | { |
|
147 | { | |
146 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) { |
|
148 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) { | |
147 | if (plotableVect.size() == 1) { |
|
149 | if (plotableVect.size() == 1) { | |
148 | updateScalarData(plotableVect.at(0), *scalarSeries, dateTime); |
|
150 | updateScalarData(plotableVect.at(0), *scalarSeries, dateTime); | |
149 | } |
|
151 | } | |
150 | else { |
|
152 | else { | |
151 | qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr( |
|
153 | qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr( | |
152 | "Can't update Data of a scalarSeries because there is not only one component " |
|
154 | "Can't update Data of a scalarSeries because there is not only one component " | |
153 | "associated"); |
|
155 | "associated"); | |
154 | } |
|
156 | } | |
155 | } |
|
157 | } | |
156 | else { |
|
158 | else { | |
157 | /// @todo DEBUG |
|
159 | /// @todo DEBUG | |
158 | } |
|
160 | } | |
159 | } |
|
161 | } |
General Comments 0
You need to be logged in to leave comments.
Login now