##// END OF EJS Templates
Merge branch 'feature/VectorSeries2' into develop
Alexandre Leroux -
r585:fb70ebcca9b1 merge
parent child
Show More
@@ -0,0 +1,19
1 #ifndef SCIQLOP_COLORUTILS_H
2 #define SCIQLOP_COLORUTILS_H
3
4 #include <vector>
5
6 class QColor;
7
8 /**
9 * Utility class with methods for colors
10 */
11 struct ColorUtils {
12 /// Generates a color scale from min / max values and a number of colors.
13 /// The algorithm uses the HSV color model to generate color variations (see
14 /// http://doc.qt.io/qt-4.8/qcolor.html#the-hsv-color-model)
15 static std::vector<QColor> colors(const QColor &minColor, const QColor &maxColor,
16 int nbColors) noexcept;
17 };
18
19 #endif // SCIQLOP_COLORUTILS_H
@@ -0,0 +1,10
1 #ifndef SCIQLOP_VISUALIZATIONDEFS_H
2 #define SCIQLOP_VISUALIZATIONDEFS_H
3
4 #include <map>
5
6 class QCPAbstractPlottable;
7
8 using PlottablesMap = std::map<int, QCPAbstractPlottable *>;
9
10 #endif // SCIQLOP_VISUALIZATIONDEFS_H
@@ -0,0 +1,29
1 #include "Common/ColorUtils.h"
2
3 #include <QtGui/QColor>
4
5 std::vector<QColor> ColorUtils::colors(const QColor &minColor, const QColor &maxColor,
6 int nbColors) noexcept
7 {
8 auto result = std::vector<QColor>{};
9
10 if (nbColors == 1) {
11 result.push_back(minColor);
12 }
13 else if (nbColors > 0) {
14 const auto nbSteps = static_cast<double>(nbColors - 1);
15
16 const auto colorHStep = (maxColor.hue() - minColor.hue()) / nbSteps;
17 const auto colorSStep = (maxColor.saturation() - minColor.saturation()) / nbSteps;
18 const auto colorVStep = (maxColor.value() - minColor.value()) / nbSteps;
19 const auto colorAStep = (maxColor.alpha() - minColor.alpha()) / nbSteps;
20
21 for (auto i = 0; i < nbColors; ++i) {
22 result.push_back(QColor::fromHsv(
23 minColor.hue() + i * colorHStep, minColor.saturation() + i * colorSStep,
24 minColor.value() + i * colorVStep, minColor.alpha() + i * colorAStep));
25 }
26 }
27
28 return result;
29 }
@@ -218,6 +218,8 public:
218 218 }
219 219 }
220 220
221 int componentCount() const noexcept { return m_Data.size(); }
222
221 223 /**
222 224 * @return the data of a component
223 225 * @param componentIndex the index of the component to retrieve the data
@@ -1,6 +1,8
1 1 #ifndef SCIQLOP_VISUALIZATIONGRAPHHELPER_H
2 2 #define SCIQLOP_VISUALIZATIONGRAPHHELPER_H
3 3
4 #include "Visualization/VisualizationDefs.h"
5
4 6 #include <Data/SqpRange.h>
5 7
6 8 #include <QLoggingCategory>
@@ -28,13 +30,10 struct VisualizationGraphHelper {
28 30 * components.
29 31 * @return the list of the components created
30 32 */
31 static QVector<QCPAbstractPlottable *> create(std::shared_ptr<Variable> variable,
32 QCustomPlot &plot) noexcept;
33 static QVector<QCPAbstractPlottable *> createV2(std::shared_ptr<Variable> variable,
34 QCustomPlot &plot) noexcept;
33 static PlottablesMap create(std::shared_ptr<Variable> variable, QCustomPlot &plot) noexcept;
35 34
36 static void updateData(QVector<QCPAbstractPlottable *> plotableVect,
37 std::shared_ptr<IDataSeries> dataSeries, const SqpRange &dateTime);
35 static void updateData(PlottablesMap &plottables, IDataSeries *dataSeries,
36 const SqpRange &dateTime);
38 37 };
39 38
40 39 #endif // SCIQLOP_VISUALIZATIONGRAPHHELPER_H
@@ -1,7 +1,10
1 1 #include "Visualization/VisualizationGraphHelper.h"
2 2 #include "Visualization/qcustomplot.h"
3 3
4 #include <Common/ColorUtils.h>
5
4 6 #include <Data/ScalarSeries.h>
7 #include <Data/VectorSeries.h>
5 8
6 9 #include <Variable/Variable.h>
7 10
@@ -35,161 +38,202 QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis)
35 38 }
36 39 }
37 40
38 void updateScalarData(QCPAbstractPlottable *component, std::shared_ptr<ScalarSeries> scalarSeries,
39 const SqpRange &range)
41 /// Sets axes properties according to the properties of a data series
42 template <int Dim>
43 void setAxesProperties(const DataSeries<Dim> &dataSeries, QCustomPlot &plot) noexcept
40 44 {
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
45 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
46 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
47 auto setAxisProperties = [](auto axis, const auto &unit) {
48 // label (unit name)
49 axis->setLabel(unit.m_Name);
50
51 // ticker (depending on the type of unit)
52 axis->setTicker(axisTicker(unit.m_TimeUnit));
53 };
54 setAxisProperties(plot.xAxis, dataSeries.xAxisUnit());
55 setAxisProperties(plot.yAxis, dataSeries.valuesUnit());
56 }
58 57
59 // Display all data
60 component->parentPlot()->replot();
61 }
62 else {
63 /// @todo DEBUG
58 /**
59 * Struct used to create plottables, depending on the type of the data series from which to create them
60 * @tparam T the data series' type
61 * @remarks Default implementation can't create plottables
62 */
63 template <typename T, typename Enabled = void>
64 struct PlottablesCreator {
65 static PlottablesMap createPlottables(T &, QCustomPlot &)
66 {
67 qCCritical(LOG_DataSeries())
68 << QObject::tr("Can't create plottables: unmanaged data series type");
69 return {};
64 70 }
65 }
71 };
66 72
67 QCPAbstractPlottable *createScalarSeriesComponentV2(std::shared_ptr<ScalarSeries> scalarSeries,
68 QCustomPlot &plot)
69 {
70 auto component = plot.addGraph();
73 /**
74 * Specialization of PlottablesCreator for scalars and vectors
75 * @sa ScalarSeries
76 * @sa VectorSeries
77 */
78 template <typename T>
79 struct PlottablesCreator<T,
80 typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
81 or std::is_base_of<VectorSeries, T>::value> > {
82 static PlottablesMap createPlottables(T &dataSeries, QCustomPlot &plot)
83 {
84 PlottablesMap result{};
85
86 // Gets the number of components of the data series
87 auto componentCount = dataSeries.valuesData()->componentCount();
88
89 auto colors = ColorUtils::colors(Qt::blue, Qt::red, componentCount);
90
91 // For each component of the data series, creates a QCPGraph to add to the plot
92 for (auto i = 0; i < componentCount; ++i) {
93 auto graph = plot.addGraph();
94 graph->setPen(QPen{colors.at(i)});
95
96 result.insert({i, graph});
97 }
71 98
72 if (component) {
73 99 // Axes properties
74 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
75 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
76
77 auto setAxisProperties = [](auto axis, const auto &unit) {
78 // label (unit name)
79 axis->setLabel(unit.m_Name);
80
81 // ticker (depending on the type of unit)
82 axis->setTicker(axisTicker(unit.m_TimeUnit));
83 };
84 setAxisProperties(plot.xAxis, scalarSeries->xAxisUnit());
85 setAxisProperties(plot.yAxis, scalarSeries->valuesUnit());
100 setAxesProperties(dataSeries, plot);
101
102 plot.replot();
103
104 return result;
86 105 }
87 return component;
88 }
106 };
89 107
90 QCPAbstractPlottable *createScalarSeriesComponent(std::shared_ptr<ScalarSeries> scalarSeries,
91 QCustomPlot &plot, const SqpRange &dateTime)
92 {
93 auto component = plot.addGraph();
108 /**
109 * Struct used to update plottables, depending on the type of the data series from which to update them
110 * @tparam T the data series' type
111 * @remarks Default implementation can't update plottables
112 */
113 template <typename T, typename Enabled = void>
114 struct PlottablesUpdater {
115 static void updatePlottables(T &, PlottablesMap &, const SqpRange &, bool)
116 {
117 qCCritical(LOG_DataSeries())
118 << QObject::tr("Can't update plottables: unmanaged data series type");
119 }
120 };
94 121
95 if (component) {
96 // // Graph data
97 component->setData(scalarSeries->xAxisData()->data(), scalarSeries->valuesData()->data(),
98 true);
122 /**
123 * Specialization of PlottablesUpdater for scalars and vectors
124 * @sa ScalarSeries
125 * @sa VectorSeries
126 */
127 template <typename T>
128 struct PlottablesUpdater<T,
129 typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
130 or std::is_base_of<VectorSeries, T>::value> > {
131 static void updatePlottables(T &dataSeries, PlottablesMap &plottables, const SqpRange &range,
132 bool rescaleAxes)
133 {
134 dataSeries.lockRead();
135
136 // For each plottable to update, resets its data
137 std::map<int, QSharedPointer<SqpDataContainer> > dataContainers{};
138 for (const auto &plottable : plottables) {
139 if (auto graph = dynamic_cast<QCPGraph *>(plottable.second)) {
140 auto dataContainer = QSharedPointer<SqpDataContainer>::create();
141 graph->setData(dataContainer);
142
143 dataContainers.insert({plottable.first, dataContainer});
144 }
145 }
99 146
100 updateScalarData(component, scalarSeries, dateTime);
147 // - Gets the data of the series included in the current range
148 // - 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
149 auto subDataIts = dataSeries.subData(range.m_TStart, range.m_TEnd);
150 for (auto it = subDataIts.first; it != subDataIts.second; ++it) {
151 for (const auto &dataContainer : dataContainers) {
152 auto componentIndex = dataContainer.first;
153 dataContainer.second->appendGraphData(
154 QCPGraphData(it->x(), it->value(componentIndex)));
155 }
156 }
101 157
102 // Axes properties
103 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
104 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
158 dataSeries.unlock();
105 159
106 auto setAxisProperties = [](auto axis, const auto &unit) {
107 // label (unit name)
108 axis->setLabel(unit.m_Name);
160 if (!plottables.empty()) {
161 auto plot = plottables.begin()->second->parentPlot();
109 162
110 // ticker (depending on the type of unit)
111 axis->setTicker(axisTicker(unit.m_TimeUnit));
112 };
113 setAxisProperties(plot.xAxis, scalarSeries->xAxisUnit());
114 setAxisProperties(plot.yAxis, scalarSeries->valuesUnit());
163 if (rescaleAxes) {
164 plot->rescaleAxes();
165 }
115 166
116 // Display all data
117 component->rescaleAxes();
118 plot.replot();
167 plot->replot();
168 }
119 169 }
120 else {
121 qCDebug(LOG_VisualizationGraphHelper())
122 << QObject::tr("Can't create graph for the scalar series");
170 };
171
172 /**
173 * Helper used to create/update plottables
174 */
175 struct IPlottablesHelper {
176 virtual ~IPlottablesHelper() noexcept = default;
177 virtual PlottablesMap create(QCustomPlot &plot) const = 0;
178 virtual void update(PlottablesMap &plottables, const SqpRange &range,
179 bool rescaleAxes = false) const = 0;
180 };
181
182 /**
183 * Default implementation of IPlottablesHelper, which takes data series to create/update plottables
184 * @tparam T the data series' type
185 */
186 template <typename T>
187 struct PlottablesHelper : public IPlottablesHelper {
188 explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {}
189
190 PlottablesMap create(QCustomPlot &plot) const override
191 {
192 return PlottablesCreator<T>::createPlottables(m_DataSeries, plot);
123 193 }
124 194
125 return component;
126 }
195 void update(PlottablesMap &plottables, const SqpRange &range, bool rescaleAxes) const override
196 {
197 PlottablesUpdater<T>::updatePlottables(m_DataSeries, plottables, range, rescaleAxes);
198 }
127 199
128 } // namespace
200 T &m_DataSeries;
201 };
129 202
130 QVector<QCPAbstractPlottable *>
131 VisualizationGraphHelper::createV2(std::shared_ptr<Variable> variable, QCustomPlot &plot) noexcept
203 /// Creates IPlottablesHelper according to a data series
204 std::unique_ptr<IPlottablesHelper> createHelper(IDataSeries *dataSeries) noexcept
132 205 {
133 auto result = QVector<QCPAbstractPlottable *>{};
134
135 if (variable) {
136 // Gets the data series of the variable to call the creation of the right components
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 }
206 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) {
207 return std::make_unique<PlottablesHelper<ScalarSeries> >(*scalarSeries);
208 }
209 else if (auto vectorSeries = dynamic_cast<VectorSeries *>(dataSeries)) {
210 return std::make_unique<PlottablesHelper<VectorSeries> >(*vectorSeries);
145 211 }
146 212 else {
147 qCDebug(LOG_VisualizationGraphHelper())
148 << QObject::tr("Can't create graph plottables : the variable is null");
213 return std::make_unique<PlottablesHelper<IDataSeries> >(*dataSeries);
149 214 }
150
151 return result;
152 215 }
153 216
154 QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
155 QCustomPlot &plot) noexcept
156 {
157 auto result = QVector<QCPAbstractPlottable *>{};
217 } // namespace
158 218
219 PlottablesMap VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
220 QCustomPlot &plot) noexcept
221 {
159 222 if (variable) {
160 // Gets the data series of the variable to call the creation of the right components
161 // according to its type
162 if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(variable->dataSeries())) {
163 result.append(createScalarSeriesComponent(scalarSeries, plot, variable->range()));
164 }
165 else {
166 qCDebug(LOG_VisualizationGraphHelper())
167 << QObject::tr("Can't create graph plottables : unmanaged data series type");
168 }
223 auto helper = createHelper(variable->dataSeries().get());
224 auto plottables = helper->create(plot);
225 return plottables;
169 226 }
170 227 else {
171 228 qCDebug(LOG_VisualizationGraphHelper())
172 229 << QObject::tr("Can't create graph plottables : the variable is null");
230 return PlottablesMap{};
173 231 }
174
175 return result;
176 232 }
177 233
178 void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect,
179 std::shared_ptr<IDataSeries> dataSeries,
234 void VisualizationGraphHelper::updateData(PlottablesMap &plottables, IDataSeries *dataSeries,
180 235 const SqpRange &dateTime)
181 236 {
182 if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) {
183 if (plotableVect.size() == 1) {
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 }
237 auto helper = createHelper(dataSeries);
238 helper->update(plottables, dateTime);
195 239 }
@@ -1,5 +1,6
1 1 #include "Visualization/VisualizationGraphWidget.h"
2 2 #include "Visualization/IVisualizationWidgetVisitor.h"
3 #include "Visualization/VisualizationDefs.h"
3 4 #include "Visualization/VisualizationGraphHelper.h"
4 5 #include "Visualization/VisualizationGraphRenderingDelegate.h"
5 6 #include "ui_VisualizationGraphWidget.h"
@@ -33,7 +34,7 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
33 34 }
34 35
35 36 // 1 variable -> n qcpplot
36 std::multimap<std::shared_ptr<Variable>, QCPAbstractPlottable *> m_VariableToPlotMultiMap;
37 std::map<std::shared_ptr<Variable>, PlottablesMap> m_VariableToPlotMultiMap;
37 38 bool m_DoAcquisition;
38 39 bool m_IsCalibration;
39 40 QCPItemTracer *m_TextTracer;
@@ -99,11 +100,8 void VisualizationGraphWidget::enableAcquisition(bool enable)
99 100 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable, SqpRange range)
100 101 {
101 102 // Uses delegate to create the qcpplot components according to the variable
102 auto createdPlottables = VisualizationGraphHelper::createV2(variable, *ui->widget);
103
104 for (auto createdPlottable : qAsConst(createdPlottables)) {
105 impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable});
106 }
103 auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget);
104 impl->m_VariableToPlotMultiMap.insert({variable, std::move(createdPlottables)});
107 105
108 106 connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated()));
109 107
@@ -124,10 +122,17 void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable
124 122 // Each component associated to the variable :
125 123 // - is removed from qcpplot (which deletes it)
126 124 // - is no longer referenced in the map
127 auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable);
128 for (auto it = componentsIt.first; it != componentsIt.second;) {
129 ui->widget->removePlottable(it->second);
130 it = impl->m_VariableToPlotMultiMap.erase(it);
125 auto variableIt = impl->m_VariableToPlotMultiMap.find(variable);
126 if (variableIt != impl->m_VariableToPlotMultiMap.cend()) {
127 auto &plottablesMap = variableIt->second;
128
129 for (auto plottableIt = plottablesMap.cbegin(), plottableEnd = plottablesMap.cend();
130 plottableIt != plottableEnd;) {
131 ui->widget->removePlottable(plottableIt->second);
132 plottableIt = plottablesMap.erase(plottableIt);
133 }
134
135 impl->m_VariableToPlotMultiMap.erase(variableIt);
131 136 }
132 137
133 138 // Updates graph
@@ -282,29 +287,18 void VisualizationGraphWidget::onMouseRelease(QMouseEvent *event) noexcept
282 287
283 288 void VisualizationGraphWidget::onDataCacheVariableUpdated()
284 289 {
285 // NOTE:
286 // We don't want to call the method for each component of a variable unitarily, but for
287 // all
288 // its components at once (eg its three components in the case of a vector).
289
290 // The unordered_multimap does not do this easily, so the question is whether to:
291 // - use an ordered_multimap and the algos of std to group the values by key
292 // - use a map (unique keys) and store as values directly the list of components
293
294 290 auto graphRange = ui->widget->xAxis->range();
295 291 auto dateTime = SqpRange{graphRange.lower, graphRange.upper};
296 292
297 for (auto it = impl->m_VariableToPlotMultiMap.cbegin();
298 it != impl->m_VariableToPlotMultiMap.cend(); ++it) {
299 auto variable = it->first;
293 for (auto &variableEntry : impl->m_VariableToPlotMultiMap) {
294 auto variable = variableEntry.first;
300 295 qCDebug(LOG_VisualizationGraphWidget())
301 296 << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" << variable->range();
302 297 qCDebug(LOG_VisualizationGraphWidget())
303 298 << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime;
304 299 if (dateTime.contains(variable->range()) || dateTime.intersect(variable->range())) {
305
306 VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second,
307 variable->dataSeries(), variable->range());
300 VisualizationGraphHelper::updateData(variableEntry.second, variable->dataSeries().get(),
301 variable->range());
308 302 }
309 303 }
310 304 }
@@ -312,9 +306,8 void VisualizationGraphWidget::onDataCacheVariableUpdated()
312 306 void VisualizationGraphWidget::onUpdateVarDisplaying(std::shared_ptr<Variable> variable,
313 307 const SqpRange &range)
314 308 {
315 auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable);
316 for (auto it = componentsIt.first; it != componentsIt.second;) {
317 VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second,
318 variable->dataSeries(), range);
309 auto it = impl->m_VariableToPlotMultiMap.find(variable);
310 if (it != impl->m_VariableToPlotMultiMap.end()) {
311 VisualizationGraphHelper::updateData(it->second, variable->dataSeries().get(), range);
319 312 }
320 313 }
General Comments 0
You need to be logged in to leave comments. Login now