##// END OF EJS Templates
Uses std::shared_ptr
Alexandre Leroux -
r587:221951fe7cca
parent child
Show More
@@ -1,39 +1,39
1 #ifndef SCIQLOP_VISUALIZATIONGRAPHHELPER_H
1 #ifndef SCIQLOP_VISUALIZATIONGRAPHHELPER_H
2 #define SCIQLOP_VISUALIZATIONGRAPHHELPER_H
2 #define SCIQLOP_VISUALIZATIONGRAPHHELPER_H
3
3
4 #include "Visualization/VisualizationDefs.h"
4 #include "Visualization/VisualizationDefs.h"
5
5
6 #include <Data/SqpRange.h>
6 #include <Data/SqpRange.h>
7
7
8 #include <QLoggingCategory>
8 #include <QLoggingCategory>
9 #include <QVector>
9 #include <QVector>
10
10
11 #include <memory>
11 #include <memory>
12
12
13 Q_DECLARE_LOGGING_CATEGORY(LOG_VisualizationGraphHelper)
13 Q_DECLARE_LOGGING_CATEGORY(LOG_VisualizationGraphHelper)
14
14
15 class IDataSeries;
15 class IDataSeries;
16 class QCPAbstractPlottable;
16 class QCPAbstractPlottable;
17 class QCustomPlot;
17 class QCustomPlot;
18 class Variable;
18 class Variable;
19
19
20 /**
20 /**
21 * @brief The VisualizationGraphHelper class aims to create the QCustomPlot components relative to a
21 * @brief The VisualizationGraphHelper class aims to create the QCustomPlot components relative to a
22 * variable, depending on the data series of this variable
22 * variable, depending on the data series of this variable
23 */
23 */
24 struct VisualizationGraphHelper {
24 struct VisualizationGraphHelper {
25 /**
25 /**
26 * Creates (if possible) the QCustomPlot components relative to the variable passed in
26 * Creates (if possible) the QCustomPlot components relative to the variable passed in
27 * parameter, and adds these to the plot passed in parameter.
27 * parameter, and adds these to the plot passed in parameter.
28 * @param variable the variable for which to create the components
28 * @param variable the variable for which to create the components
29 * @param plot the plot in which to add the created components. It takes ownership of these
29 * @param plot the plot in which to add the created components. It takes ownership of these
30 * components.
30 * components.
31 * @return the list of the components created
31 * @return the list of the components created
32 */
32 */
33 static PlottablesMap create(std::shared_ptr<Variable> variable, QCustomPlot &plot) noexcept;
33 static PlottablesMap create(std::shared_ptr<Variable> variable, QCustomPlot &plot) noexcept;
34
34
35 static void updateData(PlottablesMap &plottables, IDataSeries *dataSeries,
35 static void updateData(PlottablesMap &plottables, std::shared_ptr<IDataSeries> dataSeries,
36 const SqpRange &dateTime);
36 const SqpRange &dateTime);
37 };
37 };
38
38
39 #endif // SCIQLOP_VISUALIZATIONGRAPHHELPER_H
39 #endif // SCIQLOP_VISUALIZATIONGRAPHHELPER_H
@@ -1,239 +1,240
1 #include "Visualization/VisualizationGraphHelper.h"
1 #include "Visualization/VisualizationGraphHelper.h"
2 #include "Visualization/qcustomplot.h"
2 #include "Visualization/qcustomplot.h"
3
3
4 #include <Common/ColorUtils.h>
4 #include <Common/ColorUtils.h>
5
5
6 #include <Data/ScalarSeries.h>
6 #include <Data/ScalarSeries.h>
7 #include <Data/VectorSeries.h>
7 #include <Data/VectorSeries.h>
8
8
9 #include <Variable/Variable.h>
9 #include <Variable/Variable.h>
10
10
11 Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper")
11 Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper")
12
12
13 namespace {
13 namespace {
14
14
15 class SqpDataContainer : public QCPGraphDataContainer {
15 class SqpDataContainer : public QCPGraphDataContainer {
16 public:
16 public:
17 void appendGraphData(const QCPGraphData &data) { mData.append(data); }
17 void appendGraphData(const QCPGraphData &data) { mData.append(data); }
18 };
18 };
19
19
20
20
21 /// Format for datetimes on a axis
21 /// Format for datetimes on a axis
22 const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss");
22 const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss");
23
23
24 /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or
24 /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or
25 /// non-time data
25 /// non-time data
26 QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis)
26 QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis)
27 {
27 {
28 if (isTimeAxis) {
28 if (isTimeAxis) {
29 auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create();
29 auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create();
30 dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT);
30 dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT);
31 dateTicker->setDateTimeSpec(Qt::UTC);
31 dateTicker->setDateTimeSpec(Qt::UTC);
32
32
33 return dateTicker;
33 return dateTicker;
34 }
34 }
35 else {
35 else {
36 // default ticker
36 // default ticker
37 return QSharedPointer<QCPAxisTicker>::create();
37 return QSharedPointer<QCPAxisTicker>::create();
38 }
38 }
39 }
39 }
40
40
41 /// Sets axes properties according to the properties of a data series
41 /// Sets axes properties according to the properties of a data series
42 template <int Dim>
42 template <int Dim>
43 void setAxesProperties(const DataSeries<Dim> &dataSeries, QCustomPlot &plot) noexcept
43 void setAxesProperties(const DataSeries<Dim> &dataSeries, QCustomPlot &plot) noexcept
44 {
44 {
45 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
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
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) {
47 auto setAxisProperties = [](auto axis, const auto &unit) {
48 // label (unit name)
48 // label (unit name)
49 axis->setLabel(unit.m_Name);
49 axis->setLabel(unit.m_Name);
50
50
51 // ticker (depending on the type of unit)
51 // ticker (depending on the type of unit)
52 axis->setTicker(axisTicker(unit.m_TimeUnit));
52 axis->setTicker(axisTicker(unit.m_TimeUnit));
53 };
53 };
54 setAxisProperties(plot.xAxis, dataSeries.xAxisUnit());
54 setAxisProperties(plot.xAxis, dataSeries.xAxisUnit());
55 setAxisProperties(plot.yAxis, dataSeries.valuesUnit());
55 setAxisProperties(plot.yAxis, dataSeries.valuesUnit());
56 }
56 }
57
57
58 /**
58 /**
59 * Struct used to create plottables, depending on the type of the data series from which to create them
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
60 * @tparam T the data series' type
61 * @remarks Default implementation can't create plottables
61 * @remarks Default implementation can't create plottables
62 */
62 */
63 template <typename T, typename Enabled = void>
63 template <typename T, typename Enabled = void>
64 struct PlottablesCreator {
64 struct PlottablesCreator {
65 static PlottablesMap createPlottables(T &, QCustomPlot &)
65 static PlottablesMap createPlottables(T &, QCustomPlot &)
66 {
66 {
67 qCCritical(LOG_DataSeries())
67 qCCritical(LOG_DataSeries())
68 << QObject::tr("Can't create plottables: unmanaged data series type");
68 << QObject::tr("Can't create plottables: unmanaged data series type");
69 return {};
69 return {};
70 }
70 }
71 };
71 };
72
72
73 /**
73 /**
74 * Specialization of PlottablesCreator for scalars and vectors
74 * Specialization of PlottablesCreator for scalars and vectors
75 * @sa ScalarSeries
75 * @sa ScalarSeries
76 * @sa VectorSeries
76 * @sa VectorSeries
77 */
77 */
78 template <typename T>
78 template <typename T>
79 struct PlottablesCreator<T,
79 struct PlottablesCreator<T,
80 typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
80 typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
81 or std::is_base_of<VectorSeries, T>::value> > {
81 or std::is_base_of<VectorSeries, T>::value> > {
82 static PlottablesMap createPlottables(T &dataSeries, QCustomPlot &plot)
82 static PlottablesMap createPlottables(T &dataSeries, QCustomPlot &plot)
83 {
83 {
84 PlottablesMap result{};
84 PlottablesMap result{};
85
85
86 // Gets the number of components of the data series
86 // Gets the number of components of the data series
87 auto componentCount = dataSeries.valuesData()->componentCount();
87 auto componentCount = dataSeries.valuesData()->componentCount();
88
88
89 auto colors = ColorUtils::colors(Qt::blue, Qt::red, componentCount);
89 auto colors = ColorUtils::colors(Qt::blue, Qt::red, componentCount);
90
90
91 // For each component of the data series, creates a QCPGraph to add to the plot
91 // For each component of the data series, creates a QCPGraph to add to the plot
92 for (auto i = 0; i < componentCount; ++i) {
92 for (auto i = 0; i < componentCount; ++i) {
93 auto graph = plot.addGraph();
93 auto graph = plot.addGraph();
94 graph->setPen(QPen{colors.at(i)});
94 graph->setPen(QPen{colors.at(i)});
95
95
96 result.insert({i, graph});
96 result.insert({i, graph});
97 }
97 }
98
98
99 // Axes properties
99 // Axes properties
100 setAxesProperties(dataSeries, plot);
100 setAxesProperties(dataSeries, plot);
101
101
102 plot.replot();
102 plot.replot();
103
103
104 return result;
104 return result;
105 }
105 }
106 };
106 };
107
107
108 /**
108 /**
109 * Struct used to update plottables, depending on the type of the data series from which to update them
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
110 * @tparam T the data series' type
111 * @remarks Default implementation can't update plottables
111 * @remarks Default implementation can't update plottables
112 */
112 */
113 template <typename T, typename Enabled = void>
113 template <typename T, typename Enabled = void>
114 struct PlottablesUpdater {
114 struct PlottablesUpdater {
115 static void updatePlottables(T &, PlottablesMap &, const SqpRange &, bool)
115 static void updatePlottables(T &, PlottablesMap &, const SqpRange &, bool)
116 {
116 {
117 qCCritical(LOG_DataSeries())
117 qCCritical(LOG_DataSeries())
118 << QObject::tr("Can't update plottables: unmanaged data series type");
118 << QObject::tr("Can't update plottables: unmanaged data series type");
119 }
119 }
120 };
120 };
121
121
122 /**
122 /**
123 * Specialization of PlottablesUpdater for scalars and vectors
123 * Specialization of PlottablesUpdater for scalars and vectors
124 * @sa ScalarSeries
124 * @sa ScalarSeries
125 * @sa VectorSeries
125 * @sa VectorSeries
126 */
126 */
127 template <typename T>
127 template <typename T>
128 struct PlottablesUpdater<T,
128 struct PlottablesUpdater<T,
129 typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
129 typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value
130 or std::is_base_of<VectorSeries, T>::value> > {
130 or std::is_base_of<VectorSeries, T>::value> > {
131 static void updatePlottables(T &dataSeries, PlottablesMap &plottables, const SqpRange &range,
131 static void updatePlottables(T &dataSeries, PlottablesMap &plottables, const SqpRange &range,
132 bool rescaleAxes)
132 bool rescaleAxes)
133 {
133 {
134 dataSeries.lockRead();
134 dataSeries.lockRead();
135
135
136 // For each plottable to update, resets its data
136 // For each plottable to update, resets its data
137 std::map<int, QSharedPointer<SqpDataContainer> > dataContainers{};
137 std::map<int, QSharedPointer<SqpDataContainer> > dataContainers{};
138 for (const auto &plottable : plottables) {
138 for (const auto &plottable : plottables) {
139 if (auto graph = dynamic_cast<QCPGraph *>(plottable.second)) {
139 if (auto graph = dynamic_cast<QCPGraph *>(plottable.second)) {
140 auto dataContainer = QSharedPointer<SqpDataContainer>::create();
140 auto dataContainer = QSharedPointer<SqpDataContainer>::create();
141 graph->setData(dataContainer);
141 graph->setData(dataContainer);
142
142
143 dataContainers.insert({plottable.first, dataContainer});
143 dataContainers.insert({plottable.first, dataContainer});
144 }
144 }
145 }
145 }
146
146
147 // - Gets the data of the series included in the current range
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
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);
149 auto subDataIts = dataSeries.subData(range.m_TStart, range.m_TEnd);
150 for (auto it = subDataIts.first; it != subDataIts.second; ++it) {
150 for (auto it = subDataIts.first; it != subDataIts.second; ++it) {
151 for (const auto &dataContainer : dataContainers) {
151 for (const auto &dataContainer : dataContainers) {
152 auto componentIndex = dataContainer.first;
152 auto componentIndex = dataContainer.first;
153 dataContainer.second->appendGraphData(
153 dataContainer.second->appendGraphData(
154 QCPGraphData(it->x(), it->value(componentIndex)));
154 QCPGraphData(it->x(), it->value(componentIndex)));
155 }
155 }
156 }
156 }
157
157
158 dataSeries.unlock();
158 dataSeries.unlock();
159
159
160 if (!plottables.empty()) {
160 if (!plottables.empty()) {
161 auto plot = plottables.begin()->second->parentPlot();
161 auto plot = plottables.begin()->second->parentPlot();
162
162
163 if (rescaleAxes) {
163 if (rescaleAxes) {
164 plot->rescaleAxes();
164 plot->rescaleAxes();
165 }
165 }
166
166
167 plot->replot();
167 plot->replot();
168 }
168 }
169 }
169 }
170 };
170 };
171
171
172 /**
172 /**
173 * Helper used to create/update plottables
173 * Helper used to create/update plottables
174 */
174 */
175 struct IPlottablesHelper {
175 struct IPlottablesHelper {
176 virtual ~IPlottablesHelper() noexcept = default;
176 virtual ~IPlottablesHelper() noexcept = default;
177 virtual PlottablesMap create(QCustomPlot &plot) const = 0;
177 virtual PlottablesMap create(QCustomPlot &plot) const = 0;
178 virtual void update(PlottablesMap &plottables, const SqpRange &range,
178 virtual void update(PlottablesMap &plottables, const SqpRange &range,
179 bool rescaleAxes = false) const = 0;
179 bool rescaleAxes = false) const = 0;
180 };
180 };
181
181
182 /**
182 /**
183 * Default implementation of IPlottablesHelper, which takes data series to create/update plottables
183 * Default implementation of IPlottablesHelper, which takes data series to create/update plottables
184 * @tparam T the data series' type
184 * @tparam T the data series' type
185 */
185 */
186 template <typename T>
186 template <typename T>
187 struct PlottablesHelper : public IPlottablesHelper {
187 struct PlottablesHelper : public IPlottablesHelper {
188 explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {}
188 explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {}
189
189
190 PlottablesMap create(QCustomPlot &plot) const override
190 PlottablesMap create(QCustomPlot &plot) const override
191 {
191 {
192 return PlottablesCreator<T>::createPlottables(m_DataSeries, plot);
192 return PlottablesCreator<T>::createPlottables(m_DataSeries, plot);
193 }
193 }
194
194
195 void update(PlottablesMap &plottables, const SqpRange &range, bool rescaleAxes) const override
195 void update(PlottablesMap &plottables, const SqpRange &range, bool rescaleAxes) const override
196 {
196 {
197 PlottablesUpdater<T>::updatePlottables(m_DataSeries, plottables, range, rescaleAxes);
197 PlottablesUpdater<T>::updatePlottables(m_DataSeries, plottables, range, rescaleAxes);
198 }
198 }
199
199
200 T &m_DataSeries;
200 T &m_DataSeries;
201 };
201 };
202
202
203 /// Creates IPlottablesHelper according to a data series
203 /// Creates IPlottablesHelper according to a data series
204 std::unique_ptr<IPlottablesHelper> createHelper(IDataSeries *dataSeries) noexcept
204 std::unique_ptr<IPlottablesHelper> createHelper(std::shared_ptr<IDataSeries> dataSeries) noexcept
205 {
205 {
206 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) {
206 if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) {
207 return std::make_unique<PlottablesHelper<ScalarSeries> >(*scalarSeries);
207 return std::make_unique<PlottablesHelper<ScalarSeries> >(*scalarSeries);
208 }
208 }
209 else if (auto vectorSeries = dynamic_cast<VectorSeries *>(dataSeries)) {
209 else if (auto vectorSeries = std::dynamic_pointer_cast<VectorSeries>(dataSeries)) {
210 return std::make_unique<PlottablesHelper<VectorSeries> >(*vectorSeries);
210 return std::make_unique<PlottablesHelper<VectorSeries> >(*vectorSeries);
211 }
211 }
212 else {
212 else {
213 return std::make_unique<PlottablesHelper<IDataSeries> >(*dataSeries);
213 return std::make_unique<PlottablesHelper<IDataSeries> >(*dataSeries);
214 }
214 }
215 }
215 }
216
216
217 } // namespace
217 } // namespace
218
218
219 PlottablesMap VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
219 PlottablesMap VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
220 QCustomPlot &plot) noexcept
220 QCustomPlot &plot) noexcept
221 {
221 {
222 if (variable) {
222 if (variable) {
223 auto helper = createHelper(variable->dataSeries().get());
223 auto helper = createHelper(variable->dataSeries());
224 auto plottables = helper->create(plot);
224 auto plottables = helper->create(plot);
225 return plottables;
225 return plottables;
226 }
226 }
227 else {
227 else {
228 qCDebug(LOG_VisualizationGraphHelper())
228 qCDebug(LOG_VisualizationGraphHelper())
229 << QObject::tr("Can't create graph plottables : the variable is null");
229 << QObject::tr("Can't create graph plottables : the variable is null");
230 return PlottablesMap{};
230 return PlottablesMap{};
231 }
231 }
232 }
232 }
233
233
234 void VisualizationGraphHelper::updateData(PlottablesMap &plottables, IDataSeries *dataSeries,
234 void VisualizationGraphHelper::updateData(PlottablesMap &plottables,
235 std::shared_ptr<IDataSeries> dataSeries,
235 const SqpRange &dateTime)
236 const SqpRange &dateTime)
236 {
237 {
237 auto helper = createHelper(dataSeries);
238 auto helper = createHelper(dataSeries);
238 helper->update(plottables, dateTime);
239 helper->update(plottables, dateTime);
239 }
240 }
@@ -1,313 +1,313
1 #include "Visualization/VisualizationGraphWidget.h"
1 #include "Visualization/VisualizationGraphWidget.h"
2 #include "Visualization/IVisualizationWidgetVisitor.h"
2 #include "Visualization/IVisualizationWidgetVisitor.h"
3 #include "Visualization/VisualizationDefs.h"
3 #include "Visualization/VisualizationDefs.h"
4 #include "Visualization/VisualizationGraphHelper.h"
4 #include "Visualization/VisualizationGraphHelper.h"
5 #include "Visualization/VisualizationGraphRenderingDelegate.h"
5 #include "Visualization/VisualizationGraphRenderingDelegate.h"
6 #include "ui_VisualizationGraphWidget.h"
6 #include "ui_VisualizationGraphWidget.h"
7
7
8 #include <Data/ArrayData.h>
8 #include <Data/ArrayData.h>
9 #include <Data/IDataSeries.h>
9 #include <Data/IDataSeries.h>
10 #include <Settings/SqpSettingsDefs.h>
10 #include <Settings/SqpSettingsDefs.h>
11 #include <SqpApplication.h>
11 #include <SqpApplication.h>
12 #include <Variable/Variable.h>
12 #include <Variable/Variable.h>
13 #include <Variable/VariableController.h>
13 #include <Variable/VariableController.h>
14
14
15 #include <unordered_map>
15 #include <unordered_map>
16
16
17 Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget")
17 Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget")
18
18
19 namespace {
19 namespace {
20
20
21 /// Key pressed to enable zoom on horizontal axis
21 /// Key pressed to enable zoom on horizontal axis
22 const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier;
22 const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier;
23
23
24 /// Key pressed to enable zoom on vertical axis
24 /// Key pressed to enable zoom on vertical axis
25 const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier;
25 const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier;
26
26
27 } // namespace
27 } // namespace
28
28
29 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
29 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
30
30
31 explicit VisualizationGraphWidgetPrivate()
31 explicit VisualizationGraphWidgetPrivate()
32 : m_DoAcquisition{true}, m_IsCalibration{false}, m_RenderingDelegate{nullptr}
32 : m_DoAcquisition{true}, m_IsCalibration{false}, m_RenderingDelegate{nullptr}
33 {
33 {
34 }
34 }
35
35
36 // 1 variable -> n qcpplot
36 // 1 variable -> n qcpplot
37 std::map<std::shared_ptr<Variable>, PlottablesMap> m_VariableToPlotMultiMap;
37 std::map<std::shared_ptr<Variable>, PlottablesMap> m_VariableToPlotMultiMap;
38 bool m_DoAcquisition;
38 bool m_DoAcquisition;
39 bool m_IsCalibration;
39 bool m_IsCalibration;
40 QCPItemTracer *m_TextTracer;
40 QCPItemTracer *m_TextTracer;
41 /// Delegate used to attach rendering features to the plot
41 /// Delegate used to attach rendering features to the plot
42 std::unique_ptr<VisualizationGraphRenderingDelegate> m_RenderingDelegate;
42 std::unique_ptr<VisualizationGraphRenderingDelegate> m_RenderingDelegate;
43 };
43 };
44
44
45 VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent)
45 VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent)
46 : QWidget{parent},
46 : QWidget{parent},
47 ui{new Ui::VisualizationGraphWidget},
47 ui{new Ui::VisualizationGraphWidget},
48 impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()}
48 impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()}
49 {
49 {
50 ui->setupUi(this);
50 ui->setupUi(this);
51
51
52 // The delegate must be initialized after the ui as it uses the plot
52 // The delegate must be initialized after the ui as it uses the plot
53 impl->m_RenderingDelegate = std::make_unique<VisualizationGraphRenderingDelegate>(*ui->widget);
53 impl->m_RenderingDelegate = std::make_unique<VisualizationGraphRenderingDelegate>(*ui->widget);
54
54
55 ui->graphNameLabel->setText(name);
55 ui->graphNameLabel->setText(name);
56
56
57 // 'Close' options : widget is deleted when closed
57 // 'Close' options : widget is deleted when closed
58 setAttribute(Qt::WA_DeleteOnClose);
58 setAttribute(Qt::WA_DeleteOnClose);
59 connect(ui->closeButton, &QToolButton::clicked, this, &VisualizationGraphWidget::close);
59 connect(ui->closeButton, &QToolButton::clicked, this, &VisualizationGraphWidget::close);
60 ui->closeButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
60 ui->closeButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
61
61
62 // Set qcpplot properties :
62 // Set qcpplot properties :
63 // - Drag (on x-axis) and zoom are enabled
63 // - Drag (on x-axis) and zoom are enabled
64 // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation
64 // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation
65 ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
65 ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
66 ui->widget->axisRect()->setRangeDrag(Qt::Horizontal);
66 ui->widget->axisRect()->setRangeDrag(Qt::Horizontal);
67
67
68 connect(ui->widget, &QCustomPlot::mousePress, this, &VisualizationGraphWidget::onMousePress);
68 connect(ui->widget, &QCustomPlot::mousePress, this, &VisualizationGraphWidget::onMousePress);
69 connect(ui->widget, &QCustomPlot::mouseRelease, this,
69 connect(ui->widget, &QCustomPlot::mouseRelease, this,
70 &VisualizationGraphWidget::onMouseRelease);
70 &VisualizationGraphWidget::onMouseRelease);
71 connect(ui->widget, &QCustomPlot::mouseMove, this, &VisualizationGraphWidget::onMouseMove);
71 connect(ui->widget, &QCustomPlot::mouseMove, this, &VisualizationGraphWidget::onMouseMove);
72 connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel);
72 connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel);
73 connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>(
73 connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>(
74 &QCPAxis::rangeChanged),
74 &QCPAxis::rangeChanged),
75 this, &VisualizationGraphWidget::onRangeChanged, Qt::DirectConnection);
75 this, &VisualizationGraphWidget::onRangeChanged, Qt::DirectConnection);
76
76
77 // Activates menu when right clicking on the graph
77 // Activates menu when right clicking on the graph
78 ui->widget->setContextMenuPolicy(Qt::CustomContextMenu);
78 ui->widget->setContextMenuPolicy(Qt::CustomContextMenu);
79 connect(ui->widget, &QCustomPlot::customContextMenuRequested, this,
79 connect(ui->widget, &QCustomPlot::customContextMenuRequested, this,
80 &VisualizationGraphWidget::onGraphMenuRequested);
80 &VisualizationGraphWidget::onGraphMenuRequested);
81
81
82 connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(),
82 connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(),
83 &VariableController::onRequestDataLoading);
83 &VariableController::onRequestDataLoading);
84
84
85 connect(&sqpApp->variableController(), &VariableController::updateVarDisplaying, this,
85 connect(&sqpApp->variableController(), &VariableController::updateVarDisplaying, this,
86 &VisualizationGraphWidget::onUpdateVarDisplaying);
86 &VisualizationGraphWidget::onUpdateVarDisplaying);
87 }
87 }
88
88
89
89
90 VisualizationGraphWidget::~VisualizationGraphWidget()
90 VisualizationGraphWidget::~VisualizationGraphWidget()
91 {
91 {
92 delete ui;
92 delete ui;
93 }
93 }
94
94
95 void VisualizationGraphWidget::enableAcquisition(bool enable)
95 void VisualizationGraphWidget::enableAcquisition(bool enable)
96 {
96 {
97 impl->m_DoAcquisition = enable;
97 impl->m_DoAcquisition = enable;
98 }
98 }
99
99
100 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable, SqpRange range)
100 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable, SqpRange range)
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 impl->m_VariableToPlotMultiMap.insert({variable, std::move(createdPlottables)});
104 impl->m_VariableToPlotMultiMap.insert({variable, std::move(createdPlottables)});
105
105
106 connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated()));
106 connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated()));
107
107
108 auto varRange = variable->range();
108 auto varRange = variable->range();
109
109
110 this->enableAcquisition(false);
110 this->enableAcquisition(false);
111 this->setGraphRange(range);
111 this->setGraphRange(range);
112 this->enableAcquisition(true);
112 this->enableAcquisition(true);
113
113
114 emit requestDataLoading(QVector<std::shared_ptr<Variable> >() << variable, range, varRange,
114 emit requestDataLoading(QVector<std::shared_ptr<Variable> >() << variable, range, varRange,
115 false);
115 false);
116
116
117 emit variableAdded(variable);
117 emit variableAdded(variable);
118 }
118 }
119
119
120 void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept
120 void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept
121 {
121 {
122 // Each component associated to the variable :
122 // Each component associated to the variable :
123 // - is removed from qcpplot (which deletes it)
123 // - is removed from qcpplot (which deletes it)
124 // - is no longer referenced in the map
124 // - is no longer referenced in the map
125 auto variableIt = impl->m_VariableToPlotMultiMap.find(variable);
125 auto variableIt = impl->m_VariableToPlotMultiMap.find(variable);
126 if (variableIt != impl->m_VariableToPlotMultiMap.cend()) {
126 if (variableIt != impl->m_VariableToPlotMultiMap.cend()) {
127 auto &plottablesMap = variableIt->second;
127 auto &plottablesMap = variableIt->second;
128
128
129 for (auto plottableIt = plottablesMap.cbegin(), plottableEnd = plottablesMap.cend();
129 for (auto plottableIt = plottablesMap.cbegin(), plottableEnd = plottablesMap.cend();
130 plottableIt != plottableEnd;) {
130 plottableIt != plottableEnd;) {
131 ui->widget->removePlottable(plottableIt->second);
131 ui->widget->removePlottable(plottableIt->second);
132 plottableIt = plottablesMap.erase(plottableIt);
132 plottableIt = plottablesMap.erase(plottableIt);
133 }
133 }
134
134
135 impl->m_VariableToPlotMultiMap.erase(variableIt);
135 impl->m_VariableToPlotMultiMap.erase(variableIt);
136 }
136 }
137
137
138 // Updates graph
138 // Updates graph
139 ui->widget->replot();
139 ui->widget->replot();
140 }
140 }
141
141
142 void VisualizationGraphWidget::setRange(std::shared_ptr<Variable> variable, const SqpRange &range)
142 void VisualizationGraphWidget::setRange(std::shared_ptr<Variable> variable, const SqpRange &range)
143 {
143 {
144 // Note: in case of different axes that depends on variable, we could start with a code like
144 // Note: in case of different axes that depends on variable, we could start with a code like
145 // that:
145 // that:
146 // auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable);
146 // auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable);
147 // for (auto it = componentsIt.first; it != componentsIt.second;) {
147 // for (auto it = componentsIt.first; it != componentsIt.second;) {
148 // }
148 // }
149 ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd);
149 ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd);
150 ui->widget->replot();
150 ui->widget->replot();
151 }
151 }
152
152
153 void VisualizationGraphWidget::setYRange(const SqpRange &range)
153 void VisualizationGraphWidget::setYRange(const SqpRange &range)
154 {
154 {
155 ui->widget->yAxis->setRange(range.m_TStart, range.m_TEnd);
155 ui->widget->yAxis->setRange(range.m_TStart, range.m_TEnd);
156 }
156 }
157
157
158 SqpRange VisualizationGraphWidget::graphRange() const noexcept
158 SqpRange VisualizationGraphWidget::graphRange() const noexcept
159 {
159 {
160 auto graphRange = ui->widget->xAxis->range();
160 auto graphRange = ui->widget->xAxis->range();
161 return SqpRange{graphRange.lower, graphRange.upper};
161 return SqpRange{graphRange.lower, graphRange.upper};
162 }
162 }
163
163
164 void VisualizationGraphWidget::setGraphRange(const SqpRange &range)
164 void VisualizationGraphWidget::setGraphRange(const SqpRange &range)
165 {
165 {
166 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange START");
166 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange START");
167 ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd);
167 ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd);
168 ui->widget->replot();
168 ui->widget->replot();
169 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange END");
169 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange END");
170 }
170 }
171
171
172 void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor)
172 void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor)
173 {
173 {
174 if (visitor) {
174 if (visitor) {
175 visitor->visit(this);
175 visitor->visit(this);
176 }
176 }
177 else {
177 else {
178 qCCritical(LOG_VisualizationGraphWidget())
178 qCCritical(LOG_VisualizationGraphWidget())
179 << tr("Can't visit widget : the visitor is null");
179 << tr("Can't visit widget : the visitor is null");
180 }
180 }
181 }
181 }
182
182
183 bool VisualizationGraphWidget::canDrop(const Variable &variable) const
183 bool VisualizationGraphWidget::canDrop(const Variable &variable) const
184 {
184 {
185 /// @todo : for the moment, a graph can always accomodate a variable
185 /// @todo : for the moment, a graph can always accomodate a variable
186 Q_UNUSED(variable);
186 Q_UNUSED(variable);
187 return true;
187 return true;
188 }
188 }
189
189
190 bool VisualizationGraphWidget::contains(const Variable &variable) const
190 bool VisualizationGraphWidget::contains(const Variable &variable) const
191 {
191 {
192 // Finds the variable among the keys of the map
192 // Finds the variable among the keys of the map
193 auto variablePtr = &variable;
193 auto variablePtr = &variable;
194 auto findVariable
194 auto findVariable
195 = [variablePtr](const auto &entry) { return variablePtr == entry.first.get(); };
195 = [variablePtr](const auto &entry) { return variablePtr == entry.first.get(); };
196
196
197 auto end = impl->m_VariableToPlotMultiMap.cend();
197 auto end = impl->m_VariableToPlotMultiMap.cend();
198 auto it = std::find_if(impl->m_VariableToPlotMultiMap.cbegin(), end, findVariable);
198 auto it = std::find_if(impl->m_VariableToPlotMultiMap.cbegin(), end, findVariable);
199 return it != end;
199 return it != end;
200 }
200 }
201
201
202 QString VisualizationGraphWidget::name() const
202 QString VisualizationGraphWidget::name() const
203 {
203 {
204 return ui->graphNameLabel->text();
204 return ui->graphNameLabel->text();
205 }
205 }
206
206
207 void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept
207 void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept
208 {
208 {
209 QMenu graphMenu{};
209 QMenu graphMenu{};
210
210
211 // Iterates on variables (unique keys)
211 // Iterates on variables (unique keys)
212 for (auto it = impl->m_VariableToPlotMultiMap.cbegin(),
212 for (auto it = impl->m_VariableToPlotMultiMap.cbegin(),
213 end = impl->m_VariableToPlotMultiMap.cend();
213 end = impl->m_VariableToPlotMultiMap.cend();
214 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
214 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
215 // 'Remove variable' action
215 // 'Remove variable' action
216 graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()),
216 graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()),
217 [ this, var = it->first ]() { removeVariable(var); });
217 [ this, var = it->first ]() { removeVariable(var); });
218 }
218 }
219
219
220 if (!graphMenu.isEmpty()) {
220 if (!graphMenu.isEmpty()) {
221 graphMenu.exec(mapToGlobal(pos));
221 graphMenu.exec(mapToGlobal(pos));
222 }
222 }
223 }
223 }
224
224
225 void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2)
225 void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2)
226 {
226 {
227 qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: VisualizationGraphWidget::onRangeChanged")
227 qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: VisualizationGraphWidget::onRangeChanged")
228 << QThread::currentThread()->objectName() << "DoAcqui"
228 << QThread::currentThread()->objectName() << "DoAcqui"
229 << impl->m_DoAcquisition;
229 << impl->m_DoAcquisition;
230
230
231 auto graphRange = SqpRange{t1.lower, t1.upper};
231 auto graphRange = SqpRange{t1.lower, t1.upper};
232 auto oldGraphRange = SqpRange{t2.lower, t2.upper};
232 auto oldGraphRange = SqpRange{t2.lower, t2.upper};
233
233
234 if (impl->m_DoAcquisition) {
234 if (impl->m_DoAcquisition) {
235 QVector<std::shared_ptr<Variable> > variableUnderGraphVector;
235 QVector<std::shared_ptr<Variable> > variableUnderGraphVector;
236
236
237 for (auto it = impl->m_VariableToPlotMultiMap.begin(),
237 for (auto it = impl->m_VariableToPlotMultiMap.begin(),
238 end = impl->m_VariableToPlotMultiMap.end();
238 end = impl->m_VariableToPlotMultiMap.end();
239 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
239 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
240 variableUnderGraphVector.push_back(it->first);
240 variableUnderGraphVector.push_back(it->first);
241 }
241 }
242 emit requestDataLoading(std::move(variableUnderGraphVector), graphRange, oldGraphRange,
242 emit requestDataLoading(std::move(variableUnderGraphVector), graphRange, oldGraphRange,
243 !impl->m_IsCalibration);
243 !impl->m_IsCalibration);
244
244
245 if (!impl->m_IsCalibration) {
245 if (!impl->m_IsCalibration) {
246 qCDebug(LOG_VisualizationGraphWidget())
246 qCDebug(LOG_VisualizationGraphWidget())
247 << tr("TORM: VisualizationGraphWidget::Synchronize notify !!")
247 << tr("TORM: VisualizationGraphWidget::Synchronize notify !!")
248 << QThread::currentThread()->objectName() << graphRange << oldGraphRange;
248 << QThread::currentThread()->objectName() << graphRange << oldGraphRange;
249 emit synchronize(graphRange, oldGraphRange);
249 emit synchronize(graphRange, oldGraphRange);
250 }
250 }
251 }
251 }
252 }
252 }
253
253
254 void VisualizationGraphWidget::onMouseMove(QMouseEvent *event) noexcept
254 void VisualizationGraphWidget::onMouseMove(QMouseEvent *event) noexcept
255 {
255 {
256 // Handles plot rendering when mouse is moving
256 // Handles plot rendering when mouse is moving
257 impl->m_RenderingDelegate->onMouseMove(event);
257 impl->m_RenderingDelegate->onMouseMove(event);
258 }
258 }
259
259
260 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
260 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
261 {
261 {
262 auto zoomOrientations = QFlags<Qt::Orientation>{};
262 auto zoomOrientations = QFlags<Qt::Orientation>{};
263
263
264 // Lambda that enables a zoom orientation if the key modifier related to this orientation
264 // Lambda that enables a zoom orientation if the key modifier related to this orientation
265 // has
265 // has
266 // been pressed
266 // been pressed
267 auto enableOrientation
267 auto enableOrientation
268 = [&zoomOrientations, event](const auto &orientation, const auto &modifier) {
268 = [&zoomOrientations, event](const auto &orientation, const auto &modifier) {
269 auto orientationEnabled = event->modifiers().testFlag(modifier);
269 auto orientationEnabled = event->modifiers().testFlag(modifier);
270 zoomOrientations.setFlag(orientation, orientationEnabled);
270 zoomOrientations.setFlag(orientation, orientationEnabled);
271 };
271 };
272 enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER);
272 enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER);
273 enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER);
273 enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER);
274
274
275 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
275 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
276 }
276 }
277
277
278 void VisualizationGraphWidget::onMousePress(QMouseEvent *event) noexcept
278 void VisualizationGraphWidget::onMousePress(QMouseEvent *event) noexcept
279 {
279 {
280 impl->m_IsCalibration = event->modifiers().testFlag(Qt::ControlModifier);
280 impl->m_IsCalibration = event->modifiers().testFlag(Qt::ControlModifier);
281 }
281 }
282
282
283 void VisualizationGraphWidget::onMouseRelease(QMouseEvent *event) noexcept
283 void VisualizationGraphWidget::onMouseRelease(QMouseEvent *event) noexcept
284 {
284 {
285 impl->m_IsCalibration = false;
285 impl->m_IsCalibration = false;
286 }
286 }
287
287
288 void VisualizationGraphWidget::onDataCacheVariableUpdated()
288 void VisualizationGraphWidget::onDataCacheVariableUpdated()
289 {
289 {
290 auto graphRange = ui->widget->xAxis->range();
290 auto graphRange = ui->widget->xAxis->range();
291 auto dateTime = SqpRange{graphRange.lower, graphRange.upper};
291 auto dateTime = SqpRange{graphRange.lower, graphRange.upper};
292
292
293 for (auto &variableEntry : impl->m_VariableToPlotMultiMap) {
293 for (auto &variableEntry : impl->m_VariableToPlotMultiMap) {
294 auto variable = variableEntry.first;
294 auto variable = variableEntry.first;
295 qCDebug(LOG_VisualizationGraphWidget())
295 qCDebug(LOG_VisualizationGraphWidget())
296 << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" << variable->range();
296 << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" << variable->range();
297 qCDebug(LOG_VisualizationGraphWidget())
297 qCDebug(LOG_VisualizationGraphWidget())
298 << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime;
298 << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime;
299 if (dateTime.contains(variable->range()) || dateTime.intersect(variable->range())) {
299 if (dateTime.contains(variable->range()) || dateTime.intersect(variable->range())) {
300 VisualizationGraphHelper::updateData(variableEntry.second, variable->dataSeries().get(),
300 VisualizationGraphHelper::updateData(variableEntry.second, variable->dataSeries(),
301 variable->range());
301 variable->range());
302 }
302 }
303 }
303 }
304 }
304 }
305
305
306 void VisualizationGraphWidget::onUpdateVarDisplaying(std::shared_ptr<Variable> variable,
306 void VisualizationGraphWidget::onUpdateVarDisplaying(std::shared_ptr<Variable> variable,
307 const SqpRange &range)
307 const SqpRange &range)
308 {
308 {
309 auto it = impl->m_VariableToPlotMultiMap.find(variable);
309 auto it = impl->m_VariableToPlotMultiMap.find(variable);
310 if (it != impl->m_VariableToPlotMultiMap.end()) {
310 if (it != impl->m_VariableToPlotMultiMap.end()) {
311 VisualizationGraphHelper::updateData(it->second, variable->dataSeries().get(), range);
311 VisualizationGraphHelper::updateData(it->second, variable->dataSeries(), range);
312 }
312 }
313 }
313 }
General Comments 0
You need to be logged in to leave comments. Login now