@@ -218,6 +218,8 public: | |||||
218 | } |
|
218 | } | |
219 | } |
|
219 | } | |
220 |
|
220 | |||
|
221 | int componentCount() const noexcept { return m_Data.size(); } | |||
|
222 | ||||
221 | /** |
|
223 | /** | |
222 | * @return the data of a component |
|
224 | * @return the data of a component | |
223 | * @param componentIndex the index of the component to retrieve the data |
|
225 | * @param componentIndex the index of the component to retrieve the data |
@@ -2,6 +2,7 | |||||
2 | #include "Visualization/qcustomplot.h" |
|
2 | #include "Visualization/qcustomplot.h" | |
3 |
|
3 | |||
4 | #include <Data/ScalarSeries.h> |
|
4 | #include <Data/ScalarSeries.h> | |
|
5 | #include <Data/VectorSeries.h> | |||
5 |
|
6 | |||
6 | #include <Variable/Variable.h> |
|
7 | #include <Variable/Variable.h> | |
7 |
|
8 | |||
@@ -35,45 +36,12 QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis) | |||||
35 | } |
|
36 | } | |
36 | } |
|
37 | } | |
37 |
|
38 | |||
38 | void updateScalarData(QCPAbstractPlottable *component, std::shared_ptr<ScalarSeries> scalarSeries, |
|
39 | /// Sets axes properties according to the properties of a data series | |
39 | const SqpRange &range) |
|
40 | template <int Dim> | |
|
41 | void setAxesProperties(const DataSeries<Dim> &dataSeries, QCustomPlot &plot) noexcept | |||
40 | { |
|
42 | { | |
41 | qCDebug(LOG_VisualizationGraphHelper()) << "TORM: updateScalarData" |
|
|||
42 | << QThread::currentThread()->objectName(); |
|
|||
43 | if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) { |
|
|||
44 | scalarSeries->lockRead(); |
|
|||
45 | { |
|
|||
46 | auto sqpDataContainer = QSharedPointer<SqpDataContainer>::create(); |
|
|||
47 | qcpGraph->setData(sqpDataContainer); |
|
|||
48 | auto bounds = scalarSeries->subData(range.m_TStart, range.m_TEnd); |
|
|||
49 | for (auto it = bounds.first; it != bounds.second; ++it) { |
|
|||
50 | sqpDataContainer->appendGraphData(QCPGraphData(it->x(), it->value())); |
|
|||
51 | } |
|
|||
52 |
|
||||
53 | qCInfo(LOG_VisualizationGraphHelper()) << "TODEBUG: Current points displayed" |
|
|||
54 | << sqpDataContainer->size(); |
|
|||
55 | } |
|
|||
56 | scalarSeries->unlock(); |
|
|||
57 |
|
||||
58 |
|
||||
59 | // Display all data |
|
|||
60 | component->parentPlot()->replot(); |
|
|||
61 | } |
|
|||
62 | else { |
|
|||
63 | /// @todo DEBUG |
|
|||
64 | } |
|
|||
65 | } |
|
|||
66 |
|
||||
67 | QCPAbstractPlottable *createScalarSeriesComponentV2(std::shared_ptr<ScalarSeries> scalarSeries, |
|
|||
68 | QCustomPlot &plot) |
|
|||
69 | { |
|
|||
70 | auto component = plot.addGraph(); |
|
|||
71 |
|
||||
72 | if (component) { |
|
|||
73 | // Axes properties |
|
|||
74 |
|
|
43 | /// @todo : for the moment, no control is performed on the axes: the units and the tickers | |
75 |
|
|
44 | /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph | |
76 |
|
||||
77 |
|
|
45 | auto setAxisProperties = [](auto axis, const auto &unit) { | |
78 |
|
|
46 | // label (unit name) | |
79 |
|
|
47 | axis->setLabel(unit.m_Name); | |
@@ -81,115 +49,186 QCPAbstractPlottable *createScalarSeriesComponentV2(std::shared_ptr<ScalarSeries | |||||
81 |
|
|
49 | // ticker (depending on the type of unit) | |
82 |
|
|
50 | axis->setTicker(axisTicker(unit.m_TimeUnit)); | |
83 |
|
|
51 | }; | |
84 |
|
|
52 | setAxisProperties(plot.xAxis, dataSeries.xAxisUnit()); | |
85 |
|
|
53 | setAxisProperties(plot.yAxis, dataSeries.valuesUnit()); | |
86 | } |
|
54 | } | |
87 | return component; |
|
55 | ||
|
56 | /** | |||
|
57 | * Struct used to create plottables, depending on the type of the data series from which to create them | |||
|
58 | * @tparam T the data series' type | |||
|
59 | * @remarks Default implementation can't create plottables | |||
|
60 | */ | |||
|
61 | template <typename T, typename Enabled = void> | |||
|
62 | struct PlottablesCreator { | |||
|
63 | static PlottablesMap createPlottables(T &, QCustomPlot &) | |||
|
64 | { | |||
|
65 | qCCritical(LOG_DataSeries()) | |||
|
66 | << QObject::tr("Can't create plottables: unmanaged data series type"); | |||
|
67 | return {}; | |||
88 | } |
|
68 | } | |
|
69 | }; | |||
89 |
|
70 | |||
90 | QCPAbstractPlottable *createScalarSeriesComponent(std::shared_ptr<ScalarSeries> scalarSeries, |
|
71 | /** | |
91 | QCustomPlot &plot, const SqpRange &dateTime) |
|
72 | * Specialization of PlottablesCreator for scalars and vectors | |
|
73 | * @sa ScalarSeries | |||
|
74 | * @sa VectorSeries | |||
|
75 | */ | |||
|
76 | template <typename T> | |||
|
77 | struct PlottablesCreator<T, | |||
|
78 | typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value | |||
|
79 | or std::is_base_of<VectorSeries, T>::value> > { | |||
|
80 | static PlottablesMap createPlottables(T &dataSeries, QCustomPlot &plot) | |||
92 | { |
|
81 | { | |
93 | auto component = plot.addGraph(); |
|
82 | PlottablesMap result{}; | |
94 |
|
83 | |||
95 | if (component) { |
|
84 | // Gets the number of components of the data series | |
96 | // // Graph data |
|
85 | auto componentCount = dataSeries.valuesData()->componentCount(); | |
97 | component->setData(scalarSeries->xAxisData()->data(), scalarSeries->valuesData()->data(), |
|
|||
98 | true); |
|
|||
99 |
|
86 | |||
100 | updateScalarData(component, scalarSeries, dateTime); |
|
87 | // For each component of the data series, creates a QCPGraph to add to the plot | |
|
88 | for (auto i = 0; i < componentCount; ++i) { | |||
|
89 | auto graph = plot.addGraph(); | |||
|
90 | ||||
|
91 | result.insert({i, graph}); | |||
|
92 | } | |||
101 |
|
93 | |||
102 | // Axes properties |
|
94 | // Axes properties | |
103 | /// @todo : for the moment, no control is performed on the axes: the units and the tickers |
|
95 | setAxesProperties(dataSeries, plot); | |
104 | /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph |
|
|||
105 |
|
96 | |||
106 | auto setAxisProperties = [](auto axis, const auto &unit) { |
|
97 | plot.replot(); | |
107 | // label (unit name) |
|
|||
108 | axis->setLabel(unit.m_Name); |
|
|||
109 |
|
98 | |||
110 | // ticker (depending on the type of unit) |
|
99 | return result; | |
111 | axis->setTicker(axisTicker(unit.m_TimeUnit)); |
|
100 | } | |
112 | }; |
|
101 | }; | |
113 | setAxisProperties(plot.xAxis, scalarSeries->xAxisUnit()); |
|
|||
114 | setAxisProperties(plot.yAxis, scalarSeries->valuesUnit()); |
|
|||
115 |
|
102 | |||
116 | // Display all data |
|
103 | /** | |
117 | component->rescaleAxes(); |
|
104 | * Struct used to update plottables, depending on the type of the data series from which to update them | |
118 | plot.replot(); |
|
105 | * @tparam T the data series' type | |
|
106 | * @remarks Default implementation can't update plottables | |||
|
107 | */ | |||
|
108 | template <typename T, typename Enabled = void> | |||
|
109 | struct PlottablesUpdater { | |||
|
110 | static void updatePlottables(T &, PlottablesMap &, const SqpRange &, bool) | |||
|
111 | { | |||
|
112 | qCCritical(LOG_DataSeries()) | |||
|
113 | << QObject::tr("Can't update plottables: unmanaged data series type"); | |||
|
114 | } | |||
|
115 | }; | |||
|
116 | ||||
|
117 | /** | |||
|
118 | * Specialization of PlottablesUpdater for scalars and vectors | |||
|
119 | * @sa ScalarSeries | |||
|
120 | * @sa VectorSeries | |||
|
121 | */ | |||
|
122 | template <typename T> | |||
|
123 | struct PlottablesUpdater<T, | |||
|
124 | typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value | |||
|
125 | or std::is_base_of<VectorSeries, T>::value> > { | |||
|
126 | static void updatePlottables(T &dataSeries, PlottablesMap &plottables, const SqpRange &range, | |||
|
127 | bool rescaleAxes) | |||
|
128 | { | |||
|
129 | dataSeries.lockRead(); | |||
|
130 | ||||
|
131 | // For each plottable to update, resets its data | |||
|
132 | std::map<int, QSharedPointer<SqpDataContainer> > dataContainers{}; | |||
|
133 | for (const auto &plottable : plottables) { | |||
|
134 | if (auto graph = dynamic_cast<QCPGraph *>(plottable.second)) { | |||
|
135 | auto dataContainer = QSharedPointer<SqpDataContainer>::create(); | |||
|
136 | graph->setData(dataContainer); | |||
|
137 | ||||
|
138 | dataContainers.insert({plottable.first, dataContainer}); | |||
119 | } |
|
139 | } | |
120 | else { |
|
|||
121 | qCDebug(LOG_VisualizationGraphHelper()) |
|
|||
122 | << QObject::tr("Can't create graph for the scalar series"); |
|
|||
123 | } |
|
140 | } | |
124 |
|
141 | |||
125 | return component; |
|
142 | // - Gets the data of the series included in the current range | |
|
143 | // - Updates each plottable by adding, for each data item, a point that takes x-axis data and value data. The correct value is retrieved according to the index of the component | |||
|
144 | auto subDataIts = dataSeries.subData(range.m_TStart, range.m_TEnd); | |||
|
145 | for (auto it = subDataIts.first; it != subDataIts.second; ++it) { | |||
|
146 | for (const auto &dataContainer : dataContainers) { | |||
|
147 | auto componentIndex = dataContainer.first; | |||
|
148 | dataContainer.second->appendGraphData( | |||
|
149 | QCPGraphData(it->x(), it->value(componentIndex))); | |||
|
150 | } | |||
126 | } |
|
151 | } | |
127 |
|
152 | |||
128 | } // namespace |
|
153 | dataSeries.unlock(); | |
129 |
|
154 | |||
130 | QVector<QCPAbstractPlottable *> |
|
155 | if (!plottables.empty()) { | |
131 | VisualizationGraphHelper::createV2(std::shared_ptr<Variable> variable, QCustomPlot &plot) noexcept |
|
156 | auto plot = plottables.begin()->second->parentPlot(); | |
132 | { |
|
|||
133 | auto result = QVector<QCPAbstractPlottable *>{}; |
|
|||
134 |
|
157 | |||
135 | if (variable) { |
|
158 | if (rescaleAxes) { | |
136 | // Gets the data series of the variable to call the creation of the right components |
|
159 | plot->rescaleAxes(); | |
137 | // according to its type |
|
|||
138 | if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(variable->dataSeries())) { |
|
|||
139 | result.append(createScalarSeriesComponentV2(scalarSeries, plot)); |
|
|||
140 | } |
|
|||
141 | else { |
|
|||
142 | qCDebug(LOG_VisualizationGraphHelper()) |
|
|||
143 | << QObject::tr("Can't create graph plottables : unmanaged data series type"); |
|
|||
144 | } |
|
160 | } | |
|
161 | ||||
|
162 | plot->replot(); | |||
145 | } |
|
163 | } | |
146 | else { |
|
|||
147 | qCDebug(LOG_VisualizationGraphHelper()) |
|
|||
148 | << QObject::tr("Can't create graph plottables : the variable is null"); |
|
|||
149 | } |
|
164 | } | |
|
165 | }; | |||
150 |
|
166 | |||
151 | return result; |
|
167 | /** | |
|
168 | * Helper used to create/update plottables | |||
|
169 | */ | |||
|
170 | struct IPlottablesHelper { | |||
|
171 | virtual ~IPlottablesHelper() noexcept = default; | |||
|
172 | virtual PlottablesMap create(QCustomPlot &plot) const = 0; | |||
|
173 | virtual void update(PlottablesMap &plottables, const SqpRange &range, | |||
|
174 | bool rescaleAxes = false) const = 0; | |||
|
175 | }; | |||
|
176 | ||||
|
177 | /** | |||
|
178 | * Default implementation of IPlottablesHelper, which takes data series to create/update plottables | |||
|
179 | * @tparam T the data series' type | |||
|
180 | */ | |||
|
181 | template <typename T> | |||
|
182 | struct PlottablesHelper : public IPlottablesHelper { | |||
|
183 | explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {} | |||
|
184 | ||||
|
185 | PlottablesMap create(QCustomPlot &plot) const override | |||
|
186 | { | |||
|
187 | return PlottablesCreator<T>::createPlottables(m_DataSeries, plot); | |||
152 | } |
|
188 | } | |
153 |
|
189 | |||
154 | QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable, |
|
190 | void update(PlottablesMap &plottables, const SqpRange &range, bool rescaleAxes) const override | |
155 | QCustomPlot &plot) noexcept |
|
|||
156 | { |
|
191 | { | |
157 | auto result = QVector<QCPAbstractPlottable *>{}; |
|
192 | PlottablesUpdater<T>::updatePlottables(m_DataSeries, plottables, range, rescaleAxes); | |
|
193 | } | |||
158 |
|
194 | |||
159 | if (variable) { |
|
195 | T &m_DataSeries; | |
160 | // Gets the data series of the variable to call the creation of the right components |
|
196 | }; | |
161 | // according to its type |
|
197 | ||
162 | if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(variable->dataSeries())) { |
|
198 | /// Creates IPlottablesHelper according to a data series | |
163 | result.append(createScalarSeriesComponent(scalarSeries, plot, variable->range())); |
|
199 | std::unique_ptr<IPlottablesHelper> createHelper(IDataSeries *dataSeries) noexcept | |
|
200 | { | |||
|
201 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) { | |||
|
202 | return std::make_unique<PlottablesHelper<ScalarSeries> >(*scalarSeries); | |||
|
203 | } | |||
|
204 | else if (auto vectorSeries = dynamic_cast<VectorSeries *>(dataSeries)) { | |||
|
205 | return std::make_unique<PlottablesHelper<VectorSeries> >(*vectorSeries); | |||
164 |
|
|
206 | } | |
165 |
|
|
207 | else { | |
166 | qCDebug(LOG_VisualizationGraphHelper()) |
|
208 | return std::make_unique<PlottablesHelper<IDataSeries> >(*dataSeries); | |
167 | << QObject::tr("Can't create graph plottables : unmanaged data series type"); |
|
|||
168 |
|
|
209 | } | |
169 | } |
|
210 | } | |
|
211 | ||||
|
212 | } // namespace | |||
|
213 | ||||
|
214 | PlottablesMap VisualizationGraphHelper::create(std::shared_ptr<Variable> variable, | |||
|
215 | QCustomPlot &plot) noexcept | |||
|
216 | { | |||
|
217 | if (variable) { | |||
|
218 | auto helper = createHelper(variable->dataSeries().get()); | |||
|
219 | auto plottables = helper->create(plot); | |||
|
220 | return plottables; | |||
|
221 | } | |||
170 | else { |
|
222 | else { | |
171 | qCDebug(LOG_VisualizationGraphHelper()) |
|
223 | qCDebug(LOG_VisualizationGraphHelper()) | |
172 | << QObject::tr("Can't create graph plottables : the variable is null"); |
|
224 | << QObject::tr("Can't create graph plottables : the variable is null"); | |
|
225 | return PlottablesMap{}; | |||
173 | } |
|
226 | } | |
174 |
|
||||
175 | return result; |
|
|||
176 | } |
|
227 | } | |
177 |
|
228 | |||
178 |
void VisualizationGraphHelper::updateData( |
|
229 | void VisualizationGraphHelper::updateData(PlottablesMap &plottables, IDataSeries *dataSeries, | |
179 | std::shared_ptr<IDataSeries> dataSeries, |
|
|||
180 | const SqpRange &dateTime) |
|
230 | const SqpRange &dateTime) | |
181 | { |
|
231 | { | |
182 | if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) { |
|
232 | auto helper = createHelper(dataSeries); | |
183 | if (plotableVect.size() == 1) { |
|
233 | helper->update(plottables, dateTime); | |
184 | updateScalarData(plotableVect.at(0), scalarSeries, dateTime); |
|
|||
185 | } |
|
|||
186 | else { |
|
|||
187 | qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr( |
|
|||
188 | "Can't update Data of a scalarSeries because there is not only one component " |
|
|||
189 | "associated"); |
|
|||
190 | } |
|
|||
191 | } |
|
|||
192 | else { |
|
|||
193 | /// @todo DEBUG |
|
|||
194 | } |
|
|||
195 | } |
|
234 | } |
General Comments 0
You need to be logged in to leave comments.
Login now