##// END OF EJS Templates
onRangeChanged is now based on the good range (the new one)...
perrinel -
r315:076efd0d6728
parent child
Show More
@@ -1,64 +1,64
1 1 #ifndef SCIQLOP_VISUALIZATIONGRAPHWIDGET_H
2 2 #define SCIQLOP_VISUALIZATIONGRAPHWIDGET_H
3 3
4 4 #include "Visualization/IVisualizationWidget.h"
5 5
6 6 #include <QLoggingCategory>
7 7 #include <QWidget>
8 8
9 9 #include <memory>
10 10
11 11 #include <Common/spimpl.h>
12 12
13 13 Q_DECLARE_LOGGING_CATEGORY(LOG_VisualizationGraphWidget)
14 14
15 15 class QCPRange;
16 16 class SqpDateTime;
17 17 class Variable;
18 18
19 19 namespace Ui {
20 20 class VisualizationGraphWidget;
21 21 } // namespace Ui
22 22
23 23 class VisualizationGraphWidget : public QWidget, public IVisualizationWidget {
24 24 Q_OBJECT
25 25
26 26 public:
27 27 explicit VisualizationGraphWidget(const QString &name = {}, QWidget *parent = 0);
28 28 virtual ~VisualizationGraphWidget();
29 29
30 30 void addVariable(std::shared_ptr<Variable> variable);
31 31 void addVariableUsingGraph(std::shared_ptr<Variable> variable);
32 32 /// Removes a variable from the graph
33 33 void removeVariable(std::shared_ptr<Variable> variable) noexcept;
34 34
35 35 // IVisualizationWidget interface
36 36 void accept(IVisualizationWidgetVisitor *visitor) override;
37 37 bool canDrop(const Variable &variable) const override;
38 38 QString name() const override;
39 39
40 40 void updateDisplay(std::shared_ptr<Variable> variable);
41 41
42 42 signals:
43 43 void requestDataLoading(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime);
44 44
45 45
46 46 private:
47 47 Ui::VisualizationGraphWidget *ui;
48 48
49 49 class VisualizationGraphWidgetPrivate;
50 50 spimpl::unique_impl_ptr<VisualizationGraphWidgetPrivate> impl;
51 51
52 52 private slots:
53 53 /// Slot called when right clicking on the graph (displays a menu)
54 54 void onGraphMenuRequested(const QPoint &pos) noexcept;
55 55
56 void onRangeChanged(const QCPRange &t1, const QCPRange &t2);
56 void onRangeChanged(const QCPRange &t1);
57 57
58 58 /// Slot called when a mouse wheel was made, to perform some processing before the zoom is done
59 59 void onMouseWheel(QWheelEvent *event) noexcept;
60 60
61 61 void onDataCacheVariableUpdated();
62 62 };
63 63
64 64 #endif // SCIQLOP_VISUALIZATIONGRAPHWIDGET_H
@@ -1,153 +1,157
1 1 #include "Visualization/VisualizationGraphHelper.h"
2 2 #include "Visualization/qcustomplot.h"
3 3
4 4 #include <Data/ScalarSeries.h>
5 5
6 6 #include <Variable/Variable.h>
7 7
8 8 #include <QElapsedTimer>
9 9
10 10 Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper")
11 11
12 12 namespace {
13 13
14 14 /// Format for datetimes on a axis
15 15 const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss");
16 16
17 17 /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or
18 18 /// non-time data
19 19 QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis)
20 20 {
21 21 if (isTimeAxis) {
22 22 auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create();
23 23 dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT);
24 24
25 25 return dateTicker;
26 26 }
27 27 else {
28 28 // default ticker
29 29 return QSharedPointer<QCPAxisTicker>::create();
30 30 }
31 31 }
32 32
33 33 void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSeries,
34 34 const SqpDateTime &dateTime)
35 35 {
36 36 QElapsedTimer timer;
37 37 timer.start();
38 38 if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) {
39 39 // Clean the graph
40 40 // NAIVE approch
41 41 const auto &xData = scalarSeries.xAxisData()->data();
42 42 const auto &valuesData = scalarSeries.valuesData()->data();
43 43 const auto count = xData.count();
44 44 qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache" << xData.count();
45 45
46 46 auto xValue = QVector<double>(count);
47 47 auto vValue = QVector<double>(count);
48 48
49 49 int n = 0;
50 50 for (auto i = 0; i < count; ++i) {
51 51 const auto x = xData[i];
52 52 if (x >= dateTime.m_TStart && x <= dateTime.m_TEnd) {
53 53 xValue[n] = x;
54 54 vValue[n] = valuesData[i];
55 55 ++n;
56 56 }
57 57 }
58 58
59 59 xValue.resize(n);
60 60 vValue.resize(n);
61 61
62 qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points displayed"
63 << xValue.count();
62 qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points displayed" << xValue.count()
63 << dateTime;
64 64
65 65 qcpGraph->setData(xValue, vValue);
66
67 // Display all data
68 // component->parentPlot()->xAxis->setRange(dateTime.m_TStart, dateTime.m_TEnd);
69 component->rescaleAxes();
70 component->parentPlot()->replot();
66 71 }
67 72 else {
68 73 /// @todo DEBUG
69 74 }
70 75 }
71 76
72 77 QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot,
73 78 const SqpDateTime &dateTime)
74 79 {
75 80 auto component = plot.addGraph();
76 81
77 82 if (component) {
78 83 // // Graph data
79 84 component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(),
80 85 true);
81 86
82 87 updateScalarData(component, scalarSeries, dateTime);
83 88
84 89 // Axes properties
85 90 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
86 91 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
87 92
88 93 auto setAxisProperties = [](auto axis, const auto &unit) {
89 94 // label (unit name)
90 95 axis->setLabel(unit.m_Name);
91 96
92 97 // ticker (depending on the type of unit)
93 98 axis->setTicker(axisTicker(unit.m_TimeUnit));
94 99 };
95 100 setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit());
96 101 setAxisProperties(plot.yAxis, scalarSeries.valuesUnit());
97 102
98 103 // Display all data
99 104 component->rescaleAxes();
100
101 105 plot.replot();
102 106 }
103 107 else {
104 108 qCDebug(LOG_VisualizationGraphHelper())
105 109 << QObject::tr("Can't create graph for the scalar series");
106 110 }
107 111
108 112 return component;
109 113 }
110 114
111 115 } // namespace
112 116
113 117 QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
114 118 QCustomPlot &plot) noexcept
115 119 {
116 120 auto result = QVector<QCPAbstractPlottable *>{};
117 121
118 122 if (variable) {
119 123 // Gets the data series of the variable to call the creation of the right components
120 124 // according to its type
121 125 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) {
122 126 result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime()));
123 127 }
124 128 else {
125 129 qCDebug(LOG_VisualizationGraphHelper())
126 130 << QObject::tr("Can't create graph plottables : unmanaged data series type");
127 131 }
128 132 }
129 133 else {
130 134 qCDebug(LOG_VisualizationGraphHelper())
131 135 << QObject::tr("Can't create graph plottables : the variable is null");
132 136 }
133 137
134 138 return result;
135 139 }
136 140
137 141 void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect,
138 142 IDataSeries *dataSeries, const SqpDateTime &dateTime)
139 143 {
140 144 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) {
141 145 if (plotableVect.size() == 1) {
142 146 updateScalarData(plotableVect.at(0), *scalarSeries, dateTime);
143 147 }
144 148 else {
145 149 qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr(
146 150 "Can't update Data of a scalarSeries because there is not only one component "
147 151 "associated");
148 152 }
149 153 }
150 154 else {
151 155 /// @todo DEBUG
152 156 }
153 157 }
@@ -1,268 +1,269
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 connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>(
53 &QCPAxis::rangeChanged),
54 this, &VisualizationGraphWidget::onRangeChanged);
52 connect(ui->widget->xAxis,
53 static_cast<void (QCPAxis::*)(const QCPRange &)>(&QCPAxis::rangeChanged), this,
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
83 83 void VisualizationGraphWidget::addVariableUsingGraph(std::shared_ptr<Variable> variable)
84 84 {
85 85
86 86 // when adding a variable, we need to set its time range to the current graph range
87 87 auto grapheRange = ui->widget->xAxis->range();
88 88 auto dateTime = SqpDateTime{grapheRange.lower, grapheRange.upper};
89 89 variable->setDateTime(dateTime);
90 90 qCInfo(LOG_VisualizationGraphWidget()) << "ADD Variable with range : " << dateTime;
91 91
92 92 auto variableDateTimeWithTolerance = dateTime;
93 93
94 94 // add 10% tolerance for each side
95 95 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
96 96 variableDateTimeWithTolerance.m_TStart -= tolerance;
97 97 variableDateTimeWithTolerance.m_TEnd += tolerance;
98 98
99 99 qCInfo(LOG_VisualizationGraphWidget()) << "ADD Variable with range TOL: "
100 100 << variableDateTimeWithTolerance;
101 101
102 102 // Uses delegate to create the qcpplot components according to the variable
103 103 auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget);
104 104
105 105 for (auto createdPlottable : qAsConst(createdPlottables)) {
106 106 impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable});
107 107 }
108 108
109 109 connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated()));
110 110
111 111 // CHangement detected, we need to ask controller to request data loading
112 112 emit requestDataLoading(variable, variableDateTimeWithTolerance);
113 113 }
114 114
115 115 void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept
116 116 {
117 117 // Each component associated to the variable :
118 118 // - is removed from qcpplot (which deletes it)
119 119 // - is no longer referenced in the map
120 120 auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable);
121 121 for (auto it = componentsIt.first; it != componentsIt.second;) {
122 122 ui->widget->removePlottable(it->second);
123 123 it = impl->m_VariableToPlotMultiMap.erase(it);
124 124 }
125 125
126 126 // Updates graph
127 127 ui->widget->replot();
128 128 }
129 129
130 130 void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor)
131 131 {
132 132 if (visitor) {
133 133 visitor->visit(this);
134 134 }
135 135 else {
136 136 qCCritical(LOG_VisualizationGraphWidget())
137 137 << tr("Can't visit widget : the visitor is null");
138 138 }
139 139 }
140 140
141 141 bool VisualizationGraphWidget::canDrop(const Variable &variable) const
142 142 {
143 143 /// @todo : for the moment, a graph can always accomodate a variable
144 144 Q_UNUSED(variable);
145 145 return true;
146 146 }
147 147
148 148 QString VisualizationGraphWidget::name() const
149 149 {
150 150 return ui->graphNameLabel->text();
151 151 }
152 152
153 153 void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept
154 154 {
155 155 QMenu graphMenu{};
156 156
157 157 // Iterates on variables (unique keys)
158 158 for (auto it = impl->m_VariableToPlotMultiMap.cbegin(),
159 159 end = impl->m_VariableToPlotMultiMap.cend();
160 160 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
161 161 // 'Remove variable' action
162 162 graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()),
163 163 [ this, var = it->first ]() { removeVariable(var); });
164 164 }
165 165
166 166 if (!graphMenu.isEmpty()) {
167 167 graphMenu.exec(mapToGlobal(pos));
168 168 }
169 169 }
170 170
171 void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2)
171 void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1)
172 172 {
173 173
174 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged");
174 qCInfo(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged");
175 175
176 176 for (auto it = impl->m_VariableToPlotMultiMap.cbegin();
177 177 it != impl->m_VariableToPlotMultiMap.cend(); ++it) {
178 178
179 179 auto variable = it->first;
180 auto dateTime = SqpDateTime{t2.lower, t2.upper};
180 auto dateTime = SqpDateTime{t1.lower, t1.upper};
181 181
182 182 if (!variable->contains(dateTime)) {
183 qCInfo(LOG_VisualizationGraphWidget()) << dateTime << variable->dateTime();
183 184
184 185 auto variableDateTimeWithTolerance = dateTime;
185 186 if (variable->intersect(dateTime)) {
186 187 auto variableDateTime = variable->dateTime();
187 188 if (variableDateTime.m_TStart < dateTime.m_TStart) {
188 189
189 190 auto diffEndToKeepDelta = dateTime.m_TEnd - variableDateTime.m_TEnd;
190 191 dateTime.m_TStart = variableDateTime.m_TStart + diffEndToKeepDelta;
191 192 // Tolerance have to be added to the right
192 193 // add 10% tolerance for right (end) side
193 194 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
194 195 variableDateTimeWithTolerance.m_TEnd += tolerance;
195 196 }
196 197 if (variableDateTime.m_TEnd > dateTime.m_TEnd) {
197 198 auto diffStartToKeepDelta = variableDateTime.m_TStart - dateTime.m_TStart;
198 199 dateTime.m_TEnd = variableDateTime.m_TEnd - diffStartToKeepDelta;
199 200 // Tolerance have to be added to the left
200 201 // add 10% tolerance for left (start) side
201 202 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
202 203 variableDateTimeWithTolerance.m_TStart -= tolerance;
203 204 }
204 205 }
205 206 else {
206 207 // add 10% tolerance for each side
207 208 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
208 209 variableDateTimeWithTolerance.m_TStart -= tolerance;
209 210 variableDateTimeWithTolerance.m_TEnd += tolerance;
210 211 }
211 212 variable->setDateTime(dateTime);
212 213
213 214 // CHangement detected, we need to ask controller to request data loading
214 215 emit requestDataLoading(variable, variableDateTimeWithTolerance);
215 216 }
216 217 }
217 218 }
218 219
219 220 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
220 221 {
221 222 auto zoomOrientations = QFlags<Qt::Orientation>{};
222 223
223 224 // Lambda that enables a zoom orientation if the key modifier related to this orientation
224 225 // has
225 226 // been pressed
226 227 auto enableOrientation
227 228 = [&zoomOrientations, event](const auto &orientation, const auto &modifier) {
228 229 auto orientationEnabled = event->modifiers().testFlag(modifier);
229 230 zoomOrientations.setFlag(orientation, orientationEnabled);
230 231 };
231 232 enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER);
232 233 enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER);
233 234
234 235 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
235 236 }
236 237
237 238 void VisualizationGraphWidget::onDataCacheVariableUpdated()
238 239 {
239 240 // NOTE:
240 241 // We don't want to call the method for each component of a variable unitarily, but for
241 242 // all
242 243 // its components at once (eg its three components in the case of a vector).
243 244
244 245 // The unordered_multimap does not do this easily, so the question is whether to:
245 246 // - use an ordered_multimap and the algos of std to group the values by key
246 247 // - use a map (unique keys) and store as values directly the list of components
247 248
248 249 for (auto it = impl->m_VariableToPlotMultiMap.cbegin();
249 250 it != impl->m_VariableToPlotMultiMap.cend(); ++it) {
250 251 auto variable = it->first;
251 252 VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second,
252 253 variable->dataSeries(), variable->dateTime());
253 254 }
254 255 }
255 256
256 257 void VisualizationGraphWidget::updateDisplay(std::shared_ptr<Variable> variable)
257 258 {
258 259 auto abstractPlotableItPair = impl->m_VariableToPlotMultiMap.equal_range(variable);
259 260
260 261 auto abstractPlotableVect = QVector<QCPAbstractPlottable *>{};
261 262
262 263 for (auto it = abstractPlotableItPair.first; it != abstractPlotableItPair.second; ++it) {
263 264 abstractPlotableVect.push_back(it->second);
264 265 }
265 266
266 267 VisualizationGraphHelper::updateData(abstractPlotableVect, variable->dataSeries(),
267 268 variable->dateTime());
268 269 }
General Comments 0
You need to be logged in to leave comments. Login now