##// END OF EJS Templates
Removes title and close button from graph widget...
Alexandre Leroux -
r724:6a98c66c04c4
parent child
Show More
@@ -1,313 +1,313
1 1 #include "Visualization/VisualizationGraphWidget.h"
2 2 #include "Visualization/IVisualizationWidgetVisitor.h"
3 3 #include "Visualization/VisualizationDefs.h"
4 4 #include "Visualization/VisualizationGraphHelper.h"
5 5 #include "Visualization/VisualizationGraphRenderingDelegate.h"
6 6 #include "ui_VisualizationGraphWidget.h"
7 7
8 8 #include <Data/ArrayData.h>
9 9 #include <Data/IDataSeries.h>
10 10 #include <Settings/SqpSettingsDefs.h>
11 11 #include <SqpApplication.h>
12 12 #include <Variable/Variable.h>
13 13 #include <Variable/VariableController.h>
14 14
15 15 #include <unordered_map>
16 16
17 17 Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget")
18 18
19 19 namespace {
20 20
21 21 /// Key pressed to enable zoom on horizontal axis
22 22 const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier;
23 23
24 24 /// Key pressed to enable zoom on vertical axis
25 25 const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier;
26 26
27 27 } // namespace
28 28
29 29 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
30 30
31 explicit VisualizationGraphWidgetPrivate()
32 : m_DoAcquisition{true}, m_IsCalibration{false}, m_RenderingDelegate{nullptr}
31 explicit VisualizationGraphWidgetPrivate(const QString &name)
32 : m_Name{name},
33 m_DoAcquisition{true},
34 m_IsCalibration{false},
35 m_RenderingDelegate{nullptr}
33 36 {
34 37 }
35 38
39 QString m_Name;
36 40 // 1 variable -> n qcpplot
37 41 std::map<std::shared_ptr<Variable>, PlottablesMap> m_VariableToPlotMultiMap;
38 42 bool m_DoAcquisition;
39 43 bool m_IsCalibration;
40 44 QCPItemTracer *m_TextTracer;
41 45 /// Delegate used to attach rendering features to the plot
42 46 std::unique_ptr<VisualizationGraphRenderingDelegate> m_RenderingDelegate;
43 47 };
44 48
45 49 VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent)
46 50 : QWidget{parent},
47 51 ui{new Ui::VisualizationGraphWidget},
48 impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()}
52 impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>(name)}
49 53 {
50 54 ui->setupUi(this);
51 55
52 // The delegate must be initialized after the ui as it uses the plot
53 impl->m_RenderingDelegate = std::make_unique<VisualizationGraphRenderingDelegate>(*ui->widget);
54
55 ui->graphNameLabel->setText(name);
56
57 56 // 'Close' options : widget is deleted when closed
58 57 setAttribute(Qt::WA_DeleteOnClose);
59 connect(ui->closeButton, &QToolButton::clicked, this, &VisualizationGraphWidget::close);
60 ui->closeButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
61 58
62 59 // Set qcpplot properties :
63 60 // - Drag (on x-axis) and zoom are enabled
64 61 // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation
65 62 ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
66 63 ui->widget->axisRect()->setRangeDrag(Qt::Horizontal);
67 64
65 // The delegate must be initialized after the ui as it uses the plot
66 impl->m_RenderingDelegate = std::make_unique<VisualizationGraphRenderingDelegate>(*this);
67
68 68 connect(ui->widget, &QCustomPlot::mousePress, this, &VisualizationGraphWidget::onMousePress);
69 69 connect(ui->widget, &QCustomPlot::mouseRelease, this,
70 70 &VisualizationGraphWidget::onMouseRelease);
71 71 connect(ui->widget, &QCustomPlot::mouseMove, this, &VisualizationGraphWidget::onMouseMove);
72 72 connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel);
73 73 connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>(
74 74 &QCPAxis::rangeChanged),
75 75 this, &VisualizationGraphWidget::onRangeChanged, Qt::DirectConnection);
76 76
77 77 // Activates menu when right clicking on the graph
78 78 ui->widget->setContextMenuPolicy(Qt::CustomContextMenu);
79 79 connect(ui->widget, &QCustomPlot::customContextMenuRequested, this,
80 80 &VisualizationGraphWidget::onGraphMenuRequested);
81 81
82 82 connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(),
83 83 &VariableController::onRequestDataLoading);
84 84
85 85 connect(&sqpApp->variableController(), &VariableController::updateVarDisplaying, this,
86 86 &VisualizationGraphWidget::onUpdateVarDisplaying);
87 87 }
88 88
89 89
90 90 VisualizationGraphWidget::~VisualizationGraphWidget()
91 91 {
92 92 delete ui;
93 93 }
94 94
95 95 void VisualizationGraphWidget::enableAcquisition(bool enable)
96 96 {
97 97 impl->m_DoAcquisition = enable;
98 98 }
99 99
100 100 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable, SqpRange range)
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 impl->m_VariableToPlotMultiMap.insert({variable, std::move(createdPlottables)});
105 105
106 106 connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated()));
107 107
108 108 auto varRange = variable->range();
109 109
110 110 this->enableAcquisition(false);
111 111 this->setGraphRange(range);
112 112 this->enableAcquisition(true);
113 113
114 114 emit requestDataLoading(QVector<std::shared_ptr<Variable> >() << variable, range, varRange,
115 115 false);
116 116
117 117 emit variableAdded(variable);
118 118 }
119 119
120 120 void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept
121 121 {
122 122 // Each component associated to the variable :
123 123 // - is removed from qcpplot (which deletes it)
124 124 // - is no longer referenced in the map
125 125 auto variableIt = impl->m_VariableToPlotMultiMap.find(variable);
126 126 if (variableIt != impl->m_VariableToPlotMultiMap.cend()) {
127 127 auto &plottablesMap = variableIt->second;
128 128
129 129 for (auto plottableIt = plottablesMap.cbegin(), plottableEnd = plottablesMap.cend();
130 130 plottableIt != plottableEnd;) {
131 131 ui->widget->removePlottable(plottableIt->second);
132 132 plottableIt = plottablesMap.erase(plottableIt);
133 133 }
134 134
135 135 impl->m_VariableToPlotMultiMap.erase(variableIt);
136 136 }
137 137
138 138 // Updates graph
139 139 ui->widget->replot();
140 140 }
141 141
142 142 void VisualizationGraphWidget::setRange(std::shared_ptr<Variable> variable, const SqpRange &range)
143 143 {
144 144 // Note: in case of different axes that depends on variable, we could start with a code like
145 145 // that:
146 146 // auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable);
147 147 // for (auto it = componentsIt.first; it != componentsIt.second;) {
148 148 // }
149 149 ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd);
150 150 ui->widget->replot();
151 151 }
152 152
153 153 void VisualizationGraphWidget::setYRange(const SqpRange &range)
154 154 {
155 155 ui->widget->yAxis->setRange(range.m_TStart, range.m_TEnd);
156 156 }
157 157
158 158 SqpRange VisualizationGraphWidget::graphRange() const noexcept
159 159 {
160 160 auto graphRange = ui->widget->xAxis->range();
161 161 return SqpRange{graphRange.lower, graphRange.upper};
162 162 }
163 163
164 164 void VisualizationGraphWidget::setGraphRange(const SqpRange &range)
165 165 {
166 166 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange START");
167 167 ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd);
168 168 ui->widget->replot();
169 169 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange END");
170 170 }
171 171
172 172 void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor)
173 173 {
174 174 if (visitor) {
175 175 visitor->visit(this);
176 176 }
177 177 else {
178 178 qCCritical(LOG_VisualizationGraphWidget())
179 179 << tr("Can't visit widget : the visitor is null");
180 180 }
181 181 }
182 182
183 183 bool VisualizationGraphWidget::canDrop(const Variable &variable) const
184 184 {
185 185 /// @todo : for the moment, a graph can always accomodate a variable
186 186 Q_UNUSED(variable);
187 187 return true;
188 188 }
189 189
190 190 bool VisualizationGraphWidget::contains(const Variable &variable) const
191 191 {
192 192 // Finds the variable among the keys of the map
193 193 auto variablePtr = &variable;
194 194 auto findVariable
195 195 = [variablePtr](const auto &entry) { return variablePtr == entry.first.get(); };
196 196
197 197 auto end = impl->m_VariableToPlotMultiMap.cend();
198 198 auto it = std::find_if(impl->m_VariableToPlotMultiMap.cbegin(), end, findVariable);
199 199 return it != end;
200 200 }
201 201
202 202 QString VisualizationGraphWidget::name() const
203 203 {
204 return ui->graphNameLabel->text();
204 return impl->m_Name;
205 205 }
206 206
207 207 void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept
208 208 {
209 209 QMenu graphMenu{};
210 210
211 211 // Iterates on variables (unique keys)
212 212 for (auto it = impl->m_VariableToPlotMultiMap.cbegin(),
213 213 end = impl->m_VariableToPlotMultiMap.cend();
214 214 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
215 215 // 'Remove variable' action
216 216 graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()),
217 217 [ this, var = it->first ]() { removeVariable(var); });
218 218 }
219 219
220 220 if (!graphMenu.isEmpty()) {
221 221 graphMenu.exec(QCursor::pos());
222 222 }
223 223 }
224 224
225 225 void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2)
226 226 {
227 227 qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: VisualizationGraphWidget::onRangeChanged")
228 228 << QThread::currentThread()->objectName() << "DoAcqui"
229 229 << impl->m_DoAcquisition;
230 230
231 231 auto graphRange = SqpRange{t1.lower, t1.upper};
232 232 auto oldGraphRange = SqpRange{t2.lower, t2.upper};
233 233
234 234 if (impl->m_DoAcquisition) {
235 235 QVector<std::shared_ptr<Variable> > variableUnderGraphVector;
236 236
237 237 for (auto it = impl->m_VariableToPlotMultiMap.begin(),
238 238 end = impl->m_VariableToPlotMultiMap.end();
239 239 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
240 240 variableUnderGraphVector.push_back(it->first);
241 241 }
242 242 emit requestDataLoading(std::move(variableUnderGraphVector), graphRange, oldGraphRange,
243 243 !impl->m_IsCalibration);
244 244
245 245 if (!impl->m_IsCalibration) {
246 246 qCDebug(LOG_VisualizationGraphWidget())
247 247 << tr("TORM: VisualizationGraphWidget::Synchronize notify !!")
248 248 << QThread::currentThread()->objectName() << graphRange << oldGraphRange;
249 249 emit synchronize(graphRange, oldGraphRange);
250 250 }
251 251 }
252 252 }
253 253
254 254 void VisualizationGraphWidget::onMouseMove(QMouseEvent *event) noexcept
255 255 {
256 256 // Handles plot rendering when mouse is moving
257 257 impl->m_RenderingDelegate->onMouseMove(event);
258 258 }
259 259
260 260 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
261 261 {
262 262 auto zoomOrientations = QFlags<Qt::Orientation>{};
263 263
264 264 // Lambda that enables a zoom orientation if the key modifier related to this orientation
265 265 // has
266 266 // been pressed
267 267 auto enableOrientation
268 268 = [&zoomOrientations, event](const auto &orientation, const auto &modifier) {
269 269 auto orientationEnabled = event->modifiers().testFlag(modifier);
270 270 zoomOrientations.setFlag(orientation, orientationEnabled);
271 271 };
272 272 enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER);
273 273 enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER);
274 274
275 275 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
276 276 }
277 277
278 278 void VisualizationGraphWidget::onMousePress(QMouseEvent *event) noexcept
279 279 {
280 280 impl->m_IsCalibration = event->modifiers().testFlag(Qt::ControlModifier);
281 281 }
282 282
283 283 void VisualizationGraphWidget::onMouseRelease(QMouseEvent *event) noexcept
284 284 {
285 285 impl->m_IsCalibration = false;
286 286 }
287 287
288 288 void VisualizationGraphWidget::onDataCacheVariableUpdated()
289 289 {
290 290 auto graphRange = ui->widget->xAxis->range();
291 291 auto dateTime = SqpRange{graphRange.lower, graphRange.upper};
292 292
293 293 for (auto &variableEntry : impl->m_VariableToPlotMultiMap) {
294 294 auto variable = variableEntry.first;
295 295 qCDebug(LOG_VisualizationGraphWidget())
296 296 << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" << variable->range();
297 297 qCDebug(LOG_VisualizationGraphWidget())
298 298 << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime;
299 299 if (dateTime.contains(variable->range()) || dateTime.intersect(variable->range())) {
300 300 VisualizationGraphHelper::updateData(variableEntry.second, variable->dataSeries(),
301 301 variable->range());
302 302 }
303 303 }
304 304 }
305 305
306 306 void VisualizationGraphWidget::onUpdateVarDisplaying(std::shared_ptr<Variable> variable,
307 307 const SqpRange &range)
308 308 {
309 309 auto it = impl->m_VariableToPlotMultiMap.find(variable);
310 310 if (it != impl->m_VariableToPlotMultiMap.end()) {
311 311 VisualizationGraphHelper::updateData(it->second, variable->dataSeries(), range);
312 312 }
313 313 }
@@ -1,95 +1,51
1 1 <?xml version="1.0" encoding="UTF-8"?>
2 2 <ui version="4.0">
3 3 <class>VisualizationGraphWidget</class>
4 4 <widget class="QWidget" name="VisualizationGraphWidget">
5 5 <property name="geometry">
6 6 <rect>
7 7 <x>0</x>
8 8 <y>0</y>
9 9 <width>400</width>
10 10 <height>300</height>
11 11 </rect>
12 12 </property>
13 13 <property name="windowTitle">
14 14 <string>Form</string>
15 15 </property>
16 16 <layout class="QVBoxLayout" name="verticalLayout">
17 17 <property name="leftMargin">
18 18 <number>0</number>
19 19 </property>
20 20 <property name="topMargin">
21 21 <number>0</number>
22 22 </property>
23 23 <property name="rightMargin">
24 24 <number>0</number>
25 25 </property>
26 26 <property name="bottomMargin">
27 27 <number>0</number>
28 28 </property>
29 29 <item>
30 <widget class="QWidget" name="infobar" native="true">
31 <layout class="QHBoxLayout" name="horizontalLayout_2">
32 <property name="leftMargin">
33 <number>0</number>
34 </property>
35 <property name="topMargin">
36 <number>0</number>
37 </property>
38 <property name="rightMargin">
39 <number>0</number>
40 </property>
41 <property name="bottomMargin">
42 <number>0</number>
43 </property>
44 <item>
45 <widget class="QLabel" name="graphNameLabel">
46 <property name="styleSheet">
47 <string notr="true">font: 75 9pt &quot;MS Shell Dlg 2&quot;;</string>
48 </property>
49 <property name="text">
50 <string>TextLabel</string>
51 </property>
52 <property name="textFormat">
53 <enum>Qt::AutoText</enum>
54 </property>
55 <property name="alignment">
56 <set>Qt::AlignCenter</set>
57 </property>
58 </widget>
59 </item>
60 <item>
61 <widget class="QToolButton" name="closeButton">
62 <property name="styleSheet">
63 <string notr="true">background-color: transparent;</string>
64 </property>
65 <property name="text">
66 <string>Close</string>
67 </property>
68 </widget>
69 </item>
70 </layout>
71 </widget>
72 </item>
73 <item>
74 30 <widget class="QCustomPlot" name="widget" native="true">
75 31 <property name="sizePolicy">
76 32 <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
77 33 <horstretch>0</horstretch>
78 34 <verstretch>0</verstretch>
79 35 </sizepolicy>
80 36 </property>
81 37 </widget>
82 38 </item>
83 39 </layout>
84 40 </widget>
85 41 <customwidgets>
86 42 <customwidget>
87 43 <class>QCustomPlot</class>
88 44 <extends>QWidget</extends>
89 45 <header>Visualization/qcustomplot.h</header>
90 46 <container>1</container>
91 47 </customwidget>
92 48 </customwidgets>
93 49 <resources/>
94 50 <connections/>
95 51 </ui>
General Comments 0
You need to be logged in to leave comments. Login now