##// END OF EJS Templates
Correction delat T on start
perrinel -
r259:7cd04f2c2f1e
parent child
Show More
@@ -1,208 +1,208
1 #include "Visualization/VisualizationGraphWidget.h"
1 #include "Visualization/VisualizationGraphWidget.h"
2 #include "Visualization/IVisualizationWidgetVisitor.h"
2 #include "Visualization/IVisualizationWidgetVisitor.h"
3 #include "Visualization/VisualizationGraphHelper.h"
3 #include "Visualization/VisualizationGraphHelper.h"
4 #include "ui_VisualizationGraphWidget.h"
4 #include "ui_VisualizationGraphWidget.h"
5
5
6 #include <Data/ArrayData.h>
6 #include <Data/ArrayData.h>
7 #include <Data/IDataSeries.h>
7 #include <Data/IDataSeries.h>
8 #include <SqpApplication.h>
8 #include <SqpApplication.h>
9 #include <Variable/Variable.h>
9 #include <Variable/Variable.h>
10 #include <Variable/VariableController.h>
10 #include <Variable/VariableController.h>
11
11
12 #include <unordered_map>
12 #include <unordered_map>
13
13
14 Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget")
14 Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget")
15
15
16 namespace {
16 namespace {
17
17
18 /// Key pressed to enable zoom on horizontal axis
18 /// Key pressed to enable zoom on horizontal axis
19 const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier;
19 const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier;
20
20
21 /// Key pressed to enable zoom on vertical axis
21 /// Key pressed to enable zoom on vertical axis
22 const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier;
22 const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier;
23
23
24 } // namespace
24 } // namespace
25
25
26 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
26 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
27
27
28 // 1 variable -> n qcpplot
28 // 1 variable -> n qcpplot
29 std::unordered_multimap<std::shared_ptr<Variable>, QCPAbstractPlottable *>
29 std::unordered_multimap<std::shared_ptr<Variable>, QCPAbstractPlottable *>
30 m_VariableToPlotMultiMap;
30 m_VariableToPlotMultiMap;
31 };
31 };
32
32
33 VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent)
33 VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent)
34 : QWidget{parent},
34 : QWidget{parent},
35 ui{new Ui::VisualizationGraphWidget},
35 ui{new Ui::VisualizationGraphWidget},
36 impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()}
36 impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()}
37 {
37 {
38 ui->setupUi(this);
38 ui->setupUi(this);
39
39
40 // qcpplot title
40 // qcpplot title
41 ui->widget->plotLayout()->insertRow(0);
41 ui->widget->plotLayout()->insertRow(0);
42 ui->widget->plotLayout()->addElement(0, 0, new QCPTextElement{ui->widget, name});
42 ui->widget->plotLayout()->addElement(0, 0, new QCPTextElement{ui->widget, name});
43
43
44 // Set qcpplot properties :
44 // Set qcpplot properties :
45 // - Drag (on x-axis) and zoom are enabled
45 // - Drag (on x-axis) and zoom are enabled
46 // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation
46 // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation
47 ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
47 ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
48 ui->widget->axisRect()->setRangeDrag(Qt::Horizontal);
48 ui->widget->axisRect()->setRangeDrag(Qt::Horizontal);
49 connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel);
49 connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel);
50 connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>(
50 connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>(
51 &QCPAxis::rangeChanged),
51 &QCPAxis::rangeChanged),
52 this, &VisualizationGraphWidget::onRangeChanged);
52 this, &VisualizationGraphWidget::onRangeChanged);
53 }
53 }
54
54
55
55
56 VisualizationGraphWidget::~VisualizationGraphWidget()
56 VisualizationGraphWidget::~VisualizationGraphWidget()
57 {
57 {
58 delete ui;
58 delete ui;
59 }
59 }
60
60
61 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable)
61 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable)
62 {
62 {
63 // Uses delegate to create the qcpplot components according to the variable
63 // Uses delegate to create the qcpplot components according to the variable
64 auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget);
64 auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget);
65
65
66 for (auto createdPlottable : qAsConst(createdPlottables)) {
66 for (auto createdPlottable : qAsConst(createdPlottables)) {
67 impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable});
67 impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable});
68 }
68 }
69
69
70 connect(variable.get(), SIGNAL(dataCacheUpdated()), this, SLOT(onDataCacheVariableUpdated()));
70 connect(variable.get(), SIGNAL(dataCacheUpdated()), this, SLOT(onDataCacheVariableUpdated()));
71 }
71 }
72
72
73 void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor)
73 void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor)
74 {
74 {
75 if (visitor) {
75 if (visitor) {
76 visitor->visit(this);
76 visitor->visit(this);
77 }
77 }
78 else {
78 else {
79 qCCritical(LOG_VisualizationGraphWidget())
79 qCCritical(LOG_VisualizationGraphWidget())
80 << tr("Can't visit widget : the visitor is null");
80 << tr("Can't visit widget : the visitor is null");
81 }
81 }
82 }
82 }
83
83
84 bool VisualizationGraphWidget::canDrop(const Variable &variable) const
84 bool VisualizationGraphWidget::canDrop(const Variable &variable) const
85 {
85 {
86 /// @todo : for the moment, a graph can always accomodate a variable
86 /// @todo : for the moment, a graph can always accomodate a variable
87 Q_UNUSED(variable);
87 Q_UNUSED(variable);
88 return true;
88 return true;
89 }
89 }
90
90
91 void VisualizationGraphWidget::close()
91 void VisualizationGraphWidget::close()
92 {
92 {
93 // The main view cannot be directly closed.
93 // The main view cannot be directly closed.
94 return;
94 return;
95 }
95 }
96
96
97 QString VisualizationGraphWidget::name() const
97 QString VisualizationGraphWidget::name() const
98 {
98 {
99 if (auto title = dynamic_cast<QCPTextElement *>(ui->widget->plotLayout()->elementAt(0))) {
99 if (auto title = dynamic_cast<QCPTextElement *>(ui->widget->plotLayout()->elementAt(0))) {
100 return title->text();
100 return title->text();
101 }
101 }
102 else {
102 else {
103 return QString{};
103 return QString{};
104 }
104 }
105 }
105 }
106
106
107 void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2)
107 void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2)
108 {
108 {
109
109
110 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged");
110 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged");
111
111
112 for (auto it = impl->m_VariableToPlotMultiMap.cbegin();
112 for (auto it = impl->m_VariableToPlotMultiMap.cbegin();
113 it != impl->m_VariableToPlotMultiMap.cend(); ++it) {
113 it != impl->m_VariableToPlotMultiMap.cend(); ++it) {
114
114
115 auto variable = it->first;
115 auto variable = it->first;
116 qCInfo(LOG_VisualizationGraphWidget())
116 qCInfo(LOG_VisualizationGraphWidget())
117 << tr("TORM: VisualizationGraphWidget::onRangeChanged")
117 << tr("TORM: VisualizationGraphWidget::onRangeChanged")
118 << variable->dataSeries()->xAxisData()->size();
118 << variable->dataSeries()->xAxisData()->size();
119 auto dateTime = SqpDateTime{t2.lower, t2.upper};
119 auto dateTime = SqpDateTime{t2.lower, t2.upper};
120
120
121 if (!variable->contains(dateTime)) {
121 if (!variable->contains(dateTime)) {
122
122
123 auto variableDateTimeWithTolerance = dateTime;
123 auto variableDateTimeWithTolerance = dateTime;
124 if (variable->intersect(dateTime)) {
124 if (variable->intersect(dateTime)) {
125 auto variableDateTime = variable->dateTime();
125 auto variableDateTime = variable->dateTime();
126 if (variableDateTime.m_TStart < dateTime.m_TStart) {
126 if (variableDateTime.m_TStart < dateTime.m_TStart) {
127
127
128 auto diffEndToKeepDelta = dateTime.m_TEnd - variableDateTime.m_TEnd;
128 auto diffEndToKeepDelta = dateTime.m_TEnd - variableDateTime.m_TEnd;
129 dateTime.m_TStart = variableDateTime.m_TStart + diffEndToKeepDelta;
129 dateTime.m_TStart = variableDateTime.m_TStart + diffEndToKeepDelta;
130 // Tolerance have to be added to the right
130 // Tolerance have to be added to the right
131 // add 10% tolerance for right (end) side
131 // add 10% tolerance for right (end) side
132 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
132 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
133 variableDateTimeWithTolerance.m_TEnd += tolerance;
133 variableDateTimeWithTolerance.m_TEnd += tolerance;
134 }
134 }
135 if (variableDateTime.m_TEnd > dateTime.m_TEnd) {
135 if (variableDateTime.m_TEnd > dateTime.m_TEnd) {
136 auto diffStartToKeepDelta = dateTime.m_TStart - dateTime.m_TStart;
136 auto diffStartToKeepDelta = variableDateTime.m_TStart - dateTime.m_TStart;
137 dateTime.m_TEnd = variableDateTime.m_TEnd - diffStartToKeepDelta;
137 dateTime.m_TEnd = variableDateTime.m_TEnd - diffStartToKeepDelta;
138 // Tolerance have to be added to the left
138 // Tolerance have to be added to the left
139 // add 10% tolerance for left (start) side
139 // add 10% tolerance for left (start) side
140 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
140 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
141 variableDateTimeWithTolerance.m_TStart -= tolerance;
141 variableDateTimeWithTolerance.m_TStart -= tolerance;
142 }
142 }
143 }
143 }
144 else {
144 else {
145 // add 10% tolerance for each side
145 // add 10% tolerance for each side
146 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
146 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
147 variableDateTimeWithTolerance.m_TStart -= tolerance;
147 variableDateTimeWithTolerance.m_TStart -= tolerance;
148 variableDateTimeWithTolerance.m_TEnd += tolerance;
148 variableDateTimeWithTolerance.m_TEnd += tolerance;
149 }
149 }
150 variable->setDateTime(dateTime);
150 variable->setDateTime(dateTime);
151
151
152 // CHangement detected, we need to ask controller to request data loading
152 // CHangement detected, we need to ask controller to request data loading
153 sqpApp->variableController().requestDataLoading(variable,
153 sqpApp->variableController().requestDataLoading(variable,
154 variableDateTimeWithTolerance);
154 variableDateTimeWithTolerance);
155 }
155 }
156 }
156 }
157 }
157 }
158
158
159 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
159 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
160 {
160 {
161 auto zoomOrientations = QFlags<Qt::Orientation>{};
161 auto zoomOrientations = QFlags<Qt::Orientation>{};
162
162
163 // Lambda that enables a zoom orientation if the key modifier related to this orientation
163 // Lambda that enables a zoom orientation if the key modifier related to this orientation
164 // has
164 // has
165 // been pressed
165 // been pressed
166 auto enableOrientation
166 auto enableOrientation
167 = [&zoomOrientations, event](const auto &orientation, const auto &modifier) {
167 = [&zoomOrientations, event](const auto &orientation, const auto &modifier) {
168 auto orientationEnabled = event->modifiers().testFlag(modifier);
168 auto orientationEnabled = event->modifiers().testFlag(modifier);
169 zoomOrientations.setFlag(orientation, orientationEnabled);
169 zoomOrientations.setFlag(orientation, orientationEnabled);
170 };
170 };
171 enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER);
171 enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER);
172 enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER);
172 enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER);
173
173
174 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
174 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
175 }
175 }
176
176
177 void VisualizationGraphWidget::onDataCacheVariableUpdated()
177 void VisualizationGraphWidget::onDataCacheVariableUpdated()
178 {
178 {
179 // NOTE:
179 // NOTE:
180 // We don't want to call the method for each component of a variable unitarily, but for
180 // We don't want to call the method for each component of a variable unitarily, but for
181 // all
181 // all
182 // its components at once (eg its three components in the case of a vector).
182 // its components at once (eg its three components in the case of a vector).
183
183
184 // The unordered_multimap does not do this easily, so the question is whether to:
184 // The unordered_multimap does not do this easily, so the question is whether to:
185 // - use an ordered_multimap and the algos of std to group the values by key
185 // - use an ordered_multimap and the algos of std to group the values by key
186 // - use a map (unique keys) and store as values directly the list of components
186 // - use a map (unique keys) and store as values directly the list of components
187
187
188 for (auto it = impl->m_VariableToPlotMultiMap.cbegin();
188 for (auto it = impl->m_VariableToPlotMultiMap.cbegin();
189 it != impl->m_VariableToPlotMultiMap.cend(); ++it) {
189 it != impl->m_VariableToPlotMultiMap.cend(); ++it) {
190 auto variable = it->first;
190 auto variable = it->first;
191 VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second,
191 VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second,
192 variable->dataSeries(), variable->dateTime());
192 variable->dataSeries(), variable->dateTime());
193 }
193 }
194 }
194 }
195
195
196 void VisualizationGraphWidget::updateDisplay(std::shared_ptr<Variable> variable)
196 void VisualizationGraphWidget::updateDisplay(std::shared_ptr<Variable> variable)
197 {
197 {
198 auto abstractPlotableItPair = impl->m_VariableToPlotMultiMap.equal_range(variable);
198 auto abstractPlotableItPair = impl->m_VariableToPlotMultiMap.equal_range(variable);
199
199
200 auto abstractPlotableVect = QVector<QCPAbstractPlottable *>{};
200 auto abstractPlotableVect = QVector<QCPAbstractPlottable *>{};
201
201
202 for (auto it = abstractPlotableItPair.first; it != abstractPlotableItPair.second; ++it) {
202 for (auto it = abstractPlotableItPair.first; it != abstractPlotableItPair.second; ++it) {
203 abstractPlotableVect.push_back(it->second);
203 abstractPlotableVect.push_back(it->second);
204 }
204 }
205
205
206 VisualizationGraphHelper::updateData(abstractPlotableVect, variable->dataSeries(),
206 VisualizationGraphHelper::updateData(abstractPlotableVect, variable->dataSeries(),
207 variable->dateTime());
207 variable->dateTime());
208 }
208 }
General Comments 0
You need to be logged in to leave comments. Login now