@@ -1,311 +1,307 | |||
|
1 | 1 | #include "Visualization/VisualizationGraphWidget.h" |
|
2 | 2 | #include "Visualization/IVisualizationWidgetVisitor.h" |
|
3 | 3 | #include "Visualization/VisualizationGraphHelper.h" |
|
4 | 4 | #include "ui_VisualizationGraphWidget.h" |
|
5 | 5 | |
|
6 | 6 | #include <Data/ArrayData.h> |
|
7 | 7 | #include <Data/IDataSeries.h> |
|
8 | 8 | #include <SqpApplication.h> |
|
9 | 9 | #include <Variable/Variable.h> |
|
10 | 10 | #include <Variable/VariableController.h> |
|
11 | 11 | |
|
12 | 12 | #include <unordered_map> |
|
13 | 13 | |
|
14 | 14 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget") |
|
15 | 15 | |
|
16 | 16 | namespace { |
|
17 | 17 | |
|
18 | 18 | /// Key pressed to enable zoom on horizontal axis |
|
19 | 19 | const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier; |
|
20 | 20 | |
|
21 | 21 | /// Key pressed to enable zoom on vertical axis |
|
22 | 22 | const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier; |
|
23 | 23 | |
|
24 | 24 | } // namespace |
|
25 | 25 | |
|
26 | 26 | struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate { |
|
27 | 27 | |
|
28 | 28 | // 1 variable -> n qcpplot |
|
29 | 29 | std::multimap<std::shared_ptr<Variable>, QCPAbstractPlottable *> m_VariableToPlotMultiMap; |
|
30 | 30 | }; |
|
31 | 31 | |
|
32 | 32 | VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent) |
|
33 | 33 | : QWidget{parent}, |
|
34 | 34 | ui{new Ui::VisualizationGraphWidget}, |
|
35 | 35 | impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()} |
|
36 | 36 | { |
|
37 | 37 | ui->setupUi(this); |
|
38 | 38 | |
|
39 | 39 | ui->graphNameLabel->setText(name); |
|
40 | 40 | |
|
41 | 41 | // 'Close' options : widget is deleted when closed |
|
42 | 42 | setAttribute(Qt::WA_DeleteOnClose); |
|
43 | 43 | connect(ui->closeButton, &QToolButton::clicked, this, &VisualizationGraphWidget::close); |
|
44 | 44 | ui->closeButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton)); |
|
45 | 45 | |
|
46 | 46 | // Set qcpplot properties : |
|
47 | 47 | // - Drag (on x-axis) and zoom are enabled |
|
48 | 48 | // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation |
|
49 | 49 | ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); |
|
50 | 50 | ui->widget->axisRect()->setRangeDrag(Qt::Horizontal); |
|
51 | 51 | connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel); |
|
52 | 52 | connect(ui->widget->xAxis, |
|
53 | 53 | static_cast<void (QCPAxis::*)(const QCPRange &)>(&QCPAxis::rangeChanged), this, |
|
54 | 54 | &VisualizationGraphWidget::onRangeChanged); |
|
55 | 55 | |
|
56 | 56 | // Activates menu when right clicking on the graph |
|
57 | 57 | ui->widget->setContextMenuPolicy(Qt::CustomContextMenu); |
|
58 | 58 | connect(ui->widget, &QCustomPlot::customContextMenuRequested, this, |
|
59 | 59 | &VisualizationGraphWidget::onGraphMenuRequested); |
|
60 | 60 | |
|
61 | 61 | connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(), |
|
62 | 62 | &VariableController::onRequestDataLoading); |
|
63 | 63 | } |
|
64 | 64 | |
|
65 | 65 | |
|
66 | 66 | VisualizationGraphWidget::~VisualizationGraphWidget() |
|
67 | 67 | { |
|
68 | 68 | delete ui; |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable) |
|
72 | 72 | { |
|
73 | 73 | // Uses delegate to create the qcpplot components according to the variable |
|
74 | 74 | auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); |
|
75 | 75 | |
|
76 | 76 | for (auto createdPlottable : qAsConst(createdPlottables)) { |
|
77 | 77 | impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable}); |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); |
|
81 | 81 | } |
|
82 | 82 | void VisualizationGraphWidget::addVariableUsingGraph(std::shared_ptr<Variable> variable) |
|
83 | 83 | { |
|
84 | 84 | |
|
85 | 85 | // when adding a variable, we need to set its time range to the current graph range |
|
86 | 86 | auto grapheRange = ui->widget->xAxis->range(); |
|
87 | 87 | auto dateTime = SqpDateTime{grapheRange.lower, grapheRange.upper}; |
|
88 | 88 | variable->setDateTime(dateTime); |
|
89 | 89 | |
|
90 | 90 | auto variableDateTimeWithTolerance = dateTime; |
|
91 | 91 | |
|
92 | 92 | // add 10% tolerance for each side |
|
93 | 93 | auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); |
|
94 | 94 | variableDateTimeWithTolerance.m_TStart -= tolerance; |
|
95 | 95 | variableDateTimeWithTolerance.m_TEnd += tolerance; |
|
96 | 96 | |
|
97 | 97 | // Uses delegate to create the qcpplot components according to the variable |
|
98 | 98 | auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); |
|
99 | 99 | |
|
100 | 100 | for (auto createdPlottable : qAsConst(createdPlottables)) { |
|
101 | 101 | impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable}); |
|
102 | 102 | } |
|
103 | 103 | |
|
104 | 104 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); |
|
105 | 105 | |
|
106 | 106 | // CHangement detected, we need to ask controller to request data loading |
|
107 | 107 | emit requestDataLoading(variable, variableDateTimeWithTolerance); |
|
108 | 108 | } |
|
109 | 109 | |
|
110 | 110 | void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept |
|
111 | 111 | { |
|
112 | 112 | // Each component associated to the variable : |
|
113 | 113 | // - is removed from qcpplot (which deletes it) |
|
114 | 114 | // - is no longer referenced in the map |
|
115 | 115 | auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable); |
|
116 | 116 | for (auto it = componentsIt.first; it != componentsIt.second;) { |
|
117 | 117 | ui->widget->removePlottable(it->second); |
|
118 | 118 | it = impl->m_VariableToPlotMultiMap.erase(it); |
|
119 | 119 | } |
|
120 | 120 | |
|
121 | 121 | // Updates graph |
|
122 | 122 | ui->widget->replot(); |
|
123 | 123 | } |
|
124 | 124 | |
|
125 | void VisualizationGraphWidget::setRange(std::shared_ptr<Variable> variable, | |
|
126 | const SqpDateTime &range) | |
|
127 | { | |
|
128 | // auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable); | |
|
129 | // for (auto it = componentsIt.first; it != componentsIt.second;) { | |
|
130 | // } | |
|
131 | ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd); | |
|
132 | } | |
|
133 | ||
|
125 | 134 | void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor) |
|
126 | 135 | { |
|
127 | 136 | if (visitor) { |
|
128 | 137 | visitor->visit(this); |
|
129 | 138 | } |
|
130 | 139 | else { |
|
131 | 140 | qCCritical(LOG_VisualizationGraphWidget()) |
|
132 | 141 | << tr("Can't visit widget : the visitor is null"); |
|
133 | 142 | } |
|
134 | 143 | } |
|
135 | 144 | |
|
136 | 145 | bool VisualizationGraphWidget::canDrop(const Variable &variable) const |
|
137 | 146 | { |
|
138 | 147 | /// @todo : for the moment, a graph can always accomodate a variable |
|
139 | 148 | Q_UNUSED(variable); |
|
140 | 149 | return true; |
|
141 | 150 | } |
|
142 | 151 | |
|
143 | 152 | bool VisualizationGraphWidget::contains(const Variable &variable) const |
|
144 | 153 | { |
|
145 | 154 | // Finds the variable among the keys of the map |
|
146 | 155 | auto variablePtr = &variable; |
|
147 | 156 | auto findVariable |
|
148 | 157 | = [variablePtr](const auto &entry) { return variablePtr == entry.first.get(); }; |
|
149 | 158 | |
|
150 | 159 | auto end = impl->m_VariableToPlotMultiMap.cend(); |
|
151 | 160 | auto it = std::find_if(impl->m_VariableToPlotMultiMap.cbegin(), end, findVariable); |
|
152 | 161 | return it != end; |
|
153 | 162 | } |
|
154 | 163 | |
|
155 | 164 | QString VisualizationGraphWidget::name() const |
|
156 | 165 | { |
|
157 | 166 | return ui->graphNameLabel->text(); |
|
158 | 167 | } |
|
159 | 168 | |
|
160 | 169 | void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept |
|
161 | 170 | { |
|
162 | 171 | QMenu graphMenu{}; |
|
163 | 172 | |
|
164 | 173 | // Iterates on variables (unique keys) |
|
165 | 174 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(), |
|
166 | 175 | end = impl->m_VariableToPlotMultiMap.cend(); |
|
167 | 176 | it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) { |
|
168 | 177 | // 'Remove variable' action |
|
169 | 178 | graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()), |
|
170 | 179 | [ this, var = it->first ]() { removeVariable(var); }); |
|
171 | 180 | } |
|
172 | 181 | |
|
173 | 182 | if (!graphMenu.isEmpty()) { |
|
174 | 183 | graphMenu.exec(mapToGlobal(pos)); |
|
175 | 184 | } |
|
176 | 185 | } |
|
177 | 186 | |
|
178 | 187 | void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1) |
|
179 | 188 | { |
|
180 | 189 | qCInfo(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged") |
|
181 | 190 | << QThread::currentThread()->objectName(); |
|
182 | 191 | |
|
183 | 192 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); |
|
184 | 193 | it != impl->m_VariableToPlotMultiMap.cend(); ++it) { |
|
185 | 194 | |
|
186 | 195 | auto variable = it->first; |
|
187 | 196 | auto dateTime = SqpDateTime{t1.lower, t1.upper}; |
|
188 | 197 | auto dateTimeRange = dateTime; |
|
189 | 198 | |
|
190 | auto tolerance = 0.2 * (dateTime.m_TEnd - dateTime.m_TStart); | |
|
199 | auto toleranceFactor = 0.2; | |
|
200 | auto tolerance = toleranceFactor * (dateTime.m_TEnd - dateTime.m_TStart); | |
|
191 | 201 | auto variableDateTimeWithTolerance = dateTime; |
|
192 | 202 | variableDateTimeWithTolerance.m_TStart -= tolerance; |
|
193 | 203 | variableDateTimeWithTolerance.m_TEnd += tolerance; |
|
194 | 204 | |
|
195 | 205 | qCInfo(LOG_VisualizationGraphWidget()) << "v" << dateTime; |
|
196 | 206 | qCInfo(LOG_VisualizationGraphWidget()) << "vtol" << variableDateTimeWithTolerance; |
|
197 | 207 | // If new range with tol is upper than variable datetime parameters. we need to request new |
|
198 | 208 | // data |
|
199 | 209 | if (!variable->contains(variableDateTimeWithTolerance)) { |
|
200 | 210 | |
|
201 | 211 | auto variableDateTimeWithTolerance = dateTime; |
|
202 | 212 | if (!variable->isInside(dateTime)) { |
|
203 | 213 | auto variableDateTime = variable->dateTime(); |
|
204 | 214 | if (variableDateTime.m_TStart < dateTime.m_TStart) { |
|
205 | 215 | qCInfo(LOG_VisualizationGraphWidget()) << tr("TORM: Detection pan to right:"); |
|
206 | 216 | |
|
207 | 217 | auto diffEndToKeepDelta = dateTime.m_TEnd - variableDateTime.m_TEnd; |
|
208 | 218 | dateTime.m_TStart = variableDateTime.m_TStart + diffEndToKeepDelta; |
|
209 | 219 | // Tolerance have to be added to the right |
|
210 |
// add |
|
|
211 |
|
|
|
212 | // dateTime.m_TStart); | |
|
220 | // add tolerance for right (end) side | |
|
221 | tolerance = toleranceFactor * (dateTime.m_TEnd - dateTime.m_TStart); | |
|
213 | 222 | variableDateTimeWithTolerance.m_TEnd += tolerance; |
|
214 | 223 | } |
|
215 | 224 | else if (variableDateTime.m_TEnd > dateTime.m_TEnd) { |
|
216 | 225 | qCInfo(LOG_VisualizationGraphWidget()) << tr("TORM: Detection pan to left: "); |
|
217 | 226 | auto diffStartToKeepDelta = variableDateTime.m_TStart - dateTime.m_TStart; |
|
218 | 227 | dateTime.m_TEnd = variableDateTime.m_TEnd - diffStartToKeepDelta; |
|
219 | 228 | // Tolerance have to be added to the left |
|
220 |
// add |
|
|
221 |
tolerance = |
|
|
229 | // add tolerance for left (start) side | |
|
230 | tolerance = toleranceFactor * (dateTime.m_TEnd - dateTime.m_TStart); | |
|
222 | 231 | variableDateTimeWithTolerance.m_TStart -= tolerance; |
|
223 | 232 | } |
|
224 | 233 | else { |
|
225 | 234 | qCWarning(LOG_VisualizationGraphWidget()) |
|
226 | 235 | << tr("Detection anormal zoom detection: "); |
|
227 | 236 | } |
|
228 | 237 | } |
|
229 | 238 | else { |
|
230 | 239 | qCInfo(LOG_VisualizationGraphWidget()) << tr("Detection zoom out: "); |
|
231 | 240 | // add 10% tolerance for each side |
|
232 | 241 | tolerance = 0.2 * (dateTime.m_TEnd - dateTime.m_TStart); |
|
233 | 242 | variableDateTimeWithTolerance.m_TStart -= tolerance; |
|
234 | 243 | variableDateTimeWithTolerance.m_TEnd += tolerance; |
|
235 | 244 | } |
|
236 | 245 | if (!variable->contains(dateTimeRange)) { |
|
237 |
qCInfo(LOG_VisualizationGraphWidget()) |
|
|
246 | qCInfo(LOG_VisualizationGraphWidget()) | |
|
247 | << "TORM: Modif on variable datetime detected" << dateTime; | |
|
238 | 248 | variable->setDateTime(dateTime); |
|
239 | 249 | } |
|
240 | 250 | |
|
241 | 251 | qCInfo(LOG_VisualizationGraphWidget()) << tr("Request data detection: "); |
|
242 | 252 | // CHangement detected, we need to ask controller to request data loading |
|
243 | 253 | emit requestDataLoading(variable, variableDateTimeWithTolerance); |
|
244 | 254 | } |
|
245 | 255 | else { |
|
246 | 256 | qCInfo(LOG_VisualizationGraphWidget()) << tr("Detection zoom in: "); |
|
247 | 257 | } |
|
248 | 258 | } |
|
249 | 259 | } |
|
250 | 260 | |
|
251 | 261 | void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept |
|
252 | 262 | { |
|
253 | 263 | auto zoomOrientations = QFlags<Qt::Orientation>{}; |
|
254 | 264 | |
|
255 | 265 | // Lambda that enables a zoom orientation if the key modifier related to this orientation |
|
256 | 266 | // has |
|
257 | 267 | // been pressed |
|
258 | 268 | auto enableOrientation |
|
259 | 269 | = [&zoomOrientations, event](const auto &orientation, const auto &modifier) { |
|
260 | 270 | auto orientationEnabled = event->modifiers().testFlag(modifier); |
|
261 | 271 | zoomOrientations.setFlag(orientation, orientationEnabled); |
|
262 | 272 | }; |
|
263 | 273 | enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER); |
|
264 | 274 | enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER); |
|
265 | 275 | |
|
266 | 276 | ui->widget->axisRect()->setRangeZoom(zoomOrientations); |
|
267 | 277 | } |
|
268 | 278 | |
|
269 | 279 | void VisualizationGraphWidget::onDataCacheVariableUpdated() |
|
270 | 280 | { |
|
271 | 281 | // NOTE: |
|
272 | 282 | // We don't want to call the method for each component of a variable unitarily, but for |
|
273 | 283 | // all |
|
274 | 284 | // its components at once (eg its three components in the case of a vector). |
|
275 | 285 | |
|
276 | 286 | // The unordered_multimap does not do this easily, so the question is whether to: |
|
277 | 287 | // - use an ordered_multimap and the algos of std to group the values by key |
|
278 | 288 | // - use a map (unique keys) and store as values directly the list of components |
|
279 | 289 | |
|
280 | 290 | auto grapheRange = ui->widget->xAxis->range(); |
|
281 | 291 | auto dateTime = SqpDateTime{grapheRange.lower, grapheRange.upper}; |
|
282 | 292 | |
|
283 | 293 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); |
|
284 | 294 | it != impl->m_VariableToPlotMultiMap.cend(); ++it) { |
|
285 | 295 | auto variable = it->first; |
|
286 | 296 | qCInfo(LOG_VisualizationGraphWidget()) |
|
287 | 297 | << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" |
|
288 | 298 | << variable->dateTime(); |
|
289 | 299 | qCInfo(LOG_VisualizationGraphWidget()) |
|
290 | 300 | << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime; |
|
291 | 301 | if (dateTime.contains(variable->dateTime()) || dateTime.intersect(variable->dateTime())) { |
|
292 | 302 | |
|
293 | 303 | VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second, |
|
294 | 304 | variable->dataSeries(), variable->dateTime()); |
|
295 | 305 | } |
|
296 | 306 | } |
|
297 | 307 | } |
|
298 | ||
|
299 | void VisualizationGraphWidget::updateDisplay(std::shared_ptr<Variable> variable) | |
|
300 | { | |
|
301 | auto abstractPlotableItPair = impl->m_VariableToPlotMultiMap.equal_range(variable); | |
|
302 | ||
|
303 | auto abstractPlotableVect = QVector<QCPAbstractPlottable *>{}; | |
|
304 | ||
|
305 | for (auto it = abstractPlotableItPair.first; it != abstractPlotableItPair.second; ++it) { | |
|
306 | abstractPlotableVect.push_back(it->second); | |
|
307 | } | |
|
308 | ||
|
309 | VisualizationGraphHelper::updateData(abstractPlotableVect, variable->dataSeries(), | |
|
310 | variable->dateTime()); | |
|
311 | } |
General Comments 0
You need to be logged in to leave comments.
Login now