@@ -1,629 +1,599 | |||
|
1 | 1 | #include "Catalogue/CatalogueEventsWidget.h" |
|
2 | 2 | #include "ui_CatalogueEventsWidget.h" |
|
3 | 3 | |
|
4 | 4 | #include <Catalogue/CatalogueController.h> |
|
5 | 5 | #include <Catalogue/CatalogueEventsModel.h> |
|
6 | 6 | #include <Catalogue/CatalogueExplorerHelper.h> |
|
7 | 7 | #include <CatalogueDao.h> |
|
8 | 8 | #include <DBCatalogue.h> |
|
9 | 9 | #include <DBEventProduct.h> |
|
10 | 10 | #include <DataSource/DataSourceController.h> |
|
11 | 11 | #include <DataSource/DataSourceItem.h> |
|
12 | 12 | #include <SqpApplication.h> |
|
13 | 13 | #include <Variable/Variable.h> |
|
14 | 14 | #include <Variable/VariableController.h> |
|
15 | 15 | #include <Visualization/VisualizationGraphWidget.h> |
|
16 | 16 | #include <Visualization/VisualizationTabWidget.h> |
|
17 | 17 | #include <Visualization/VisualizationWidget.h> |
|
18 | 18 | #include <Visualization/VisualizationZoneWidget.h> |
|
19 | 19 | |
|
20 | #include <QActionGroup> | |
|
20 | 21 | #include <QDialog> |
|
21 | 22 | #include <QDialogButtonBox> |
|
22 | 23 | #include <QKeyEvent> |
|
23 | 24 | #include <QListWidget> |
|
25 | #include <QMenu> | |
|
24 | 26 | #include <QMessageBox> |
|
25 | 27 | |
|
26 | 28 | Q_LOGGING_CATEGORY(LOG_CatalogueEventsWidget, "CatalogueEventsWidget") |
|
27 | 29 | |
|
28 | 30 | /// Percentage added to the range of a event when it is displayed |
|
29 | 31 | const auto EVENT_RANGE_MARGE = 30; // in % |
|
30 | 32 | |
|
31 | 33 | struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate { |
|
32 | 34 | |
|
33 | 35 | CatalogueEventsModel *m_Model = nullptr; |
|
34 | 36 | QStringList m_ZonesForTimeMode; |
|
35 | 37 | QString m_ZoneForGraphMode; |
|
36 | 38 | QVector<std::shared_ptr<DBCatalogue> > m_DisplayedCatalogues; |
|
37 | 39 | bool m_AllEventDisplayed = false; |
|
38 | 40 | QVector<VisualizationGraphWidget *> m_CustomGraphs; |
|
39 | 41 | |
|
40 | 42 | VisualizationWidget *m_VisualizationWidget = nullptr; |
|
41 | 43 | |
|
42 | 44 | void setEvents(const QVector<std::shared_ptr<DBEvent> > &events, CatalogueEventsWidget *widget) |
|
43 | 45 | { |
|
44 | 46 | widget->ui->treeView->setSortingEnabled(false); |
|
45 | 47 | m_Model->setSourceCatalogues(m_DisplayedCatalogues); |
|
46 | 48 | m_Model->setEvents(events); |
|
47 | 49 | widget->ui->treeView->setSortingEnabled(true); |
|
48 | 50 | |
|
49 | 51 | for (auto event : events) { |
|
50 | 52 | if (sqpApp->catalogueController().eventHasChanges(event)) { |
|
51 | 53 | auto index = m_Model->indexOf(event); |
|
52 | 54 | widget->setEventChanges(event, true); |
|
53 | 55 | } |
|
54 | 56 | } |
|
55 | 57 | } |
|
56 | 58 | |
|
57 | 59 | void addEvent(const std::shared_ptr<DBEvent> &event, QTreeView *treeView) |
|
58 | 60 | { |
|
59 | 61 | treeView->setSortingEnabled(false); |
|
60 | 62 | m_Model->addEvent(event); |
|
61 | 63 | treeView->setSortingEnabled(true); |
|
62 | 64 | } |
|
63 | 65 | |
|
64 | 66 | void removeEvent(const std::shared_ptr<DBEvent> &event, QTreeView *treeView) |
|
65 | 67 | { |
|
66 | 68 | treeView->setSortingEnabled(false); |
|
67 | 69 | m_Model->removeEvent(event); |
|
68 | 70 | treeView->setSortingEnabled(true); |
|
69 | 71 | } |
|
70 | 72 | |
|
71 | 73 | QStringList getAvailableVisualizationZoneList() const |
|
72 | 74 | { |
|
73 | 75 | if (m_VisualizationWidget) { |
|
74 | 76 | if (auto tab = m_VisualizationWidget->currentTabWidget()) { |
|
75 | 77 | return tab->availableZoneWidgets(); |
|
76 | 78 | } |
|
77 | 79 | } |
|
78 | 80 | |
|
79 | 81 | return QStringList{}; |
|
80 | 82 | } |
|
81 | 83 | |
|
82 | 84 | QStringList selectZone(QWidget *parent, const QStringList &selectedZones, |
|
83 | 85 | bool allowMultiSelection, const QPoint &location) |
|
84 | 86 | { |
|
85 | 87 | auto availableZones = getAvailableVisualizationZoneList(); |
|
86 | 88 | if (availableZones.isEmpty()) { |
|
87 | 89 | return QStringList{}; |
|
88 | 90 | } |
|
89 | 91 | |
|
90 | QDialog d(parent, Qt::Tool); | |
|
91 | d.setWindowTitle("Choose a zone"); | |
|
92 | auto layout = new QVBoxLayout{&d}; | |
|
93 | layout->setContentsMargins(0, 0, 0, 0); | |
|
94 | auto listWidget = new QListWidget{&d}; | |
|
95 | layout->addWidget(listWidget); | |
|
92 | QActionGroup actionGroup{parent}; | |
|
93 | actionGroup.setExclusive(!allowMultiSelection); | |
|
96 | 94 | |
|
97 | QSet<QListWidgetItem *> checkedItems; | |
|
95 | QMenu selectionMenu{parent}; | |
|
96 | selectionMenu.addSeparator(); | |
|
97 | QVector<QAction *> zoneActions; | |
|
98 | 98 | for (auto zone : availableZones) { |
|
99 | auto item = new QListWidgetItem{zone}; | |
|
100 | item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable); | |
|
101 |
|
|
|
102 | item->setCheckState(Qt::Checked); | |
|
103 | checkedItems << item; | |
|
104 | } | |
|
105 | else { | |
|
106 | item->setCheckState(Qt::Unchecked); | |
|
107 | } | |
|
108 | ||
|
109 | listWidget->addItem(item); | |
|
99 | auto zoneAction = selectionMenu.addAction(zone); | |
|
100 | zoneAction->setCheckable(true); | |
|
101 | zoneAction->setChecked(selectedZones.contains(zone)); | |
|
102 | actionGroup.addAction(zoneAction); | |
|
103 | zoneActions << zoneAction; | |
|
110 | 104 | } |
|
111 | 105 | |
|
112 | auto buttonBox = new QDialogButtonBox{QDialogButtonBox::Ok, &d}; | |
|
113 | layout->addWidget(buttonBox); | |
|
114 | ||
|
115 | QObject::connect(buttonBox, &QDialogButtonBox::accepted, &d, &QDialog::accept); | |
|
116 | QObject::connect(buttonBox, &QDialogButtonBox::rejected, &d, &QDialog::reject); | |
|
117 | ||
|
118 | QObject::connect(listWidget, &QListWidget::itemChanged, | |
|
119 | [&checkedItems, allowMultiSelection, listWidget](auto item) { | |
|
120 | if (item->checkState() == Qt::Checked) { | |
|
121 | if (!allowMultiSelection) { | |
|
122 | for (auto checkedItem : checkedItems) { | |
|
123 | listWidget->blockSignals(true); | |
|
124 | checkedItem->setCheckState(Qt::Unchecked); | |
|
125 | listWidget->blockSignals(false); | |
|
126 | } | |
|
127 | ||
|
128 | checkedItems.clear(); | |
|
129 | } | |
|
130 | checkedItems << item; | |
|
131 | } | |
|
132 | else { | |
|
133 | checkedItems.remove(item); | |
|
134 | } | |
|
135 | }); | |
|
106 | auto action = selectionMenu.exec(QCursor::pos()); | |
|
136 | 107 | |
|
137 | 108 | QStringList result; |
|
138 | 109 | |
|
139 | d.setMinimumWidth(120); | |
|
140 | d.resize(d.minimumSizeHint()); | |
|
141 | d.move(location); | |
|
142 | if (d.exec() == QDialog::Accepted) { | |
|
143 | for (auto item : checkedItems) { | |
|
144 | result += item->text(); | |
|
145 | } | |
|
110 | if (action == nullptr) { | |
|
111 | result = selectedZones; | |
|
146 | 112 | } |
|
147 | 113 | else { |
|
148 | result = selectedZones; | |
|
114 | for (auto zoneAction : zoneActions) { | |
|
115 | if (zoneAction->isChecked()) { | |
|
116 | result << zoneAction->text(); | |
|
117 | } | |
|
118 | } | |
|
149 | 119 | } |
|
150 | 120 | |
|
151 | 121 | return result; |
|
152 | 122 | } |
|
153 | 123 | |
|
154 | 124 | void updateForTimeMode(QTreeView *treeView) |
|
155 | 125 | { |
|
156 | 126 | auto selectedRows = treeView->selectionModel()->selectedRows(); |
|
157 | 127 | |
|
158 | 128 | if (selectedRows.count() == 1) { |
|
159 | 129 | auto event = m_Model->getEvent(selectedRows.first()); |
|
160 | 130 | if (event) { |
|
161 | 131 | if (m_VisualizationWidget) { |
|
162 | 132 | if (auto tab = m_VisualizationWidget->currentTabWidget()) { |
|
163 | 133 | |
|
164 | 134 | for (auto zoneName : m_ZonesForTimeMode) { |
|
165 | 135 | if (auto zone = tab->getZoneWithName(zoneName)) { |
|
166 | 136 | SqpRange eventRange; |
|
167 | 137 | eventRange.m_TStart = event->getTStart(); |
|
168 | 138 | eventRange.m_TEnd = event->getTEnd(); |
|
169 | 139 | zone->setZoneRange(eventRange); |
|
170 | 140 | } |
|
171 | 141 | } |
|
172 | 142 | } |
|
173 | 143 | else { |
|
174 | 144 | qCWarning(LOG_CatalogueEventsWidget()) |
|
175 | 145 | << "updateTimeZone: no tab found in the visualization"; |
|
176 | 146 | } |
|
177 | 147 | } |
|
178 | 148 | else { |
|
179 | 149 | qCWarning(LOG_CatalogueEventsWidget()) |
|
180 | 150 | << "updateTimeZone: visualization widget not found"; |
|
181 | 151 | } |
|
182 | 152 | } |
|
183 | 153 | } |
|
184 | 154 | else { |
|
185 | 155 | qCWarning(LOG_CatalogueEventsWidget()) |
|
186 | 156 | << "updateTimeZone: not compatible with multiple events selected"; |
|
187 | 157 | } |
|
188 | 158 | } |
|
189 | 159 | |
|
190 | 160 | QVector<SqpRange> getGraphRanges(const std::shared_ptr<DBEvent> &event) |
|
191 | 161 | { |
|
192 | 162 | // Retrieves the range of each product and the maximum size |
|
193 | 163 | QVector<SqpRange> graphRanges; |
|
194 | 164 | double maxDt = 0; |
|
195 | 165 | for (auto eventProduct : event->getEventProducts()) { |
|
196 | 166 | SqpRange eventRange; |
|
197 | 167 | eventRange.m_TStart = eventProduct.getTStart(); |
|
198 | 168 | eventRange.m_TEnd = eventProduct.getTEnd(); |
|
199 | 169 | graphRanges << eventRange; |
|
200 | 170 | |
|
201 | 171 | auto dt = eventRange.m_TEnd - eventRange.m_TStart; |
|
202 | 172 | if (dt > maxDt) { |
|
203 | 173 | maxDt = dt; |
|
204 | 174 | } |
|
205 | 175 | } |
|
206 | 176 | |
|
207 | 177 | // Adds the marge |
|
208 | 178 | maxDt *= (100.0 + EVENT_RANGE_MARGE) / 100.0; |
|
209 | 179 | |
|
210 | 180 | // Corrects the graph ranges so that they all have the same size |
|
211 | 181 | QVector<SqpRange> correctedGraphRanges; |
|
212 | 182 | for (auto range : graphRanges) { |
|
213 | 183 | auto dt = range.m_TEnd - range.m_TStart; |
|
214 | 184 | auto diff = qAbs((maxDt - dt) / 2.0); |
|
215 | 185 | |
|
216 | 186 | SqpRange correctedRange; |
|
217 | 187 | correctedRange.m_TStart = range.m_TStart - diff; |
|
218 | 188 | correctedRange.m_TEnd = range.m_TEnd + diff; |
|
219 | 189 | |
|
220 | 190 | correctedGraphRanges << correctedRange; |
|
221 | 191 | } |
|
222 | 192 | |
|
223 | 193 | return correctedGraphRanges; |
|
224 | 194 | } |
|
225 | 195 | |
|
226 | 196 | void updateForGraphMode(CatalogueEventsWidget *catalogueEventWidget) |
|
227 | 197 | { |
|
228 | 198 | auto selectedRows = catalogueEventWidget->ui->treeView->selectionModel()->selectedRows(); |
|
229 | 199 | if (selectedRows.count() != 1) { |
|
230 | 200 | qCWarning(LOG_CatalogueEventsWidget()) |
|
231 | 201 | << "updateGraphMode: not compatible with multiple events selected"; |
|
232 | 202 | return; |
|
233 | 203 | } |
|
234 | 204 | |
|
235 | 205 | if (!m_VisualizationWidget) { |
|
236 | 206 | qCWarning(LOG_CatalogueEventsWidget()) |
|
237 | 207 | << "updateGraphMode: visualization widget not found"; |
|
238 | 208 | return; |
|
239 | 209 | } |
|
240 | 210 | |
|
241 | 211 | auto event = m_Model->getEvent(selectedRows.first()); |
|
242 | 212 | if (!event) { |
|
243 | 213 | // A event product is probably selected |
|
244 | 214 | qCInfo(LOG_CatalogueEventsWidget()) << "updateGraphMode: no events are selected"; |
|
245 | 215 | return; |
|
246 | 216 | } |
|
247 | 217 | |
|
248 | 218 | auto tab = m_VisualizationWidget->currentTabWidget(); |
|
249 | 219 | if (!tab) { |
|
250 | 220 | qCWarning(LOG_CatalogueEventsWidget()) |
|
251 | 221 | << "updateGraphMode: no tab found in the visualization"; |
|
252 | 222 | return; |
|
253 | 223 | } |
|
254 | 224 | |
|
255 | 225 | auto zone = tab->getZoneWithName(m_ZoneForGraphMode); |
|
256 | 226 | if (!zone) { |
|
257 | 227 | qCWarning(LOG_CatalogueEventsWidget()) << "updateGraphMode: zone not found"; |
|
258 | 228 | return; |
|
259 | 229 | } |
|
260 | 230 | |
|
261 | 231 | // Closes the previous graph and delete the asociated variables |
|
262 | 232 | for (auto graph : m_CustomGraphs) { |
|
263 | 233 | graph->close(); |
|
264 | 234 | auto variables = graph->variables().toVector(); |
|
265 | 235 | |
|
266 | 236 | QMetaObject::invokeMethod(&sqpApp->variableController(), "deleteVariables", |
|
267 | 237 | Qt::QueuedConnection, |
|
268 | 238 | Q_ARG(QVector<std::shared_ptr<Variable> >, variables)); |
|
269 | 239 | } |
|
270 | 240 | m_CustomGraphs.clear(); |
|
271 | 241 | |
|
272 | 242 | // Closes the remaining graphs inside the zone |
|
273 | 243 | zone->closeAllGraphs(); |
|
274 | 244 | |
|
275 | 245 | // Calculates the range of each graph which will be created |
|
276 | 246 | auto graphRange = getGraphRanges(event); |
|
277 | 247 | |
|
278 | 248 | // Loops through the event products and create the graph |
|
279 | 249 | auto itRange = graphRange.cbegin(); |
|
280 | 250 | for (auto eventProduct : event->getEventProducts()) { |
|
281 | 251 | auto productId = eventProduct.getProductId(); |
|
282 | 252 | |
|
283 | 253 | auto range = *itRange; |
|
284 | 254 | ++itRange; |
|
285 | 255 | |
|
286 | 256 | SqpRange productRange; |
|
287 | 257 | productRange.m_TStart = eventProduct.getTStart(); |
|
288 | 258 | productRange.m_TEnd = eventProduct.getTEnd(); |
|
289 | 259 | |
|
290 | 260 | auto context = new QObject{catalogueEventWidget}; |
|
291 | 261 | QObject::connect( |
|
292 | 262 | &sqpApp->variableController(), &VariableController::variableAdded, context, |
|
293 | 263 | [this, catalogueEventWidget, zone, context, event, range, productRange, |
|
294 | 264 | productId](auto variable) { |
|
295 | 265 | |
|
296 | 266 | if (variable->metadata().value(DataSourceItem::ID_DATA_KEY).toString() |
|
297 | 267 | == productId) { |
|
298 | 268 | auto graph = zone->createGraph(variable); |
|
299 | 269 | graph->setAutoRangeOnVariableInitialization(false); |
|
300 | 270 | |
|
301 | 271 | auto selectionZone |
|
302 | 272 | = graph->addSelectionZone(event->getName(), productRange); |
|
303 | 273 | emit catalogueEventWidget->selectionZoneAdded(event, productId, |
|
304 | 274 | selectionZone); |
|
305 | 275 | m_CustomGraphs << graph; |
|
306 | 276 | |
|
307 | 277 | graph->setGraphRange(range, true); |
|
308 | 278 | |
|
309 | 279 | // Removes the graph from the graph list if it is closed manually |
|
310 | 280 | QObject::connect(graph, &VisualizationGraphWidget::destroyed, |
|
311 | 281 | [this, graph]() { m_CustomGraphs.removeAll(graph); }); |
|
312 | 282 | |
|
313 | 283 | delete context; // removes the connection |
|
314 | 284 | } |
|
315 | 285 | }, |
|
316 | 286 | Qt::QueuedConnection); |
|
317 | 287 | |
|
318 | 288 | QMetaObject::invokeMethod(&sqpApp->dataSourceController(), |
|
319 | 289 | "requestVariableFromProductIdKey", Qt::QueuedConnection, |
|
320 | 290 | Q_ARG(QString, productId)); |
|
321 | 291 | } |
|
322 | 292 | } |
|
323 | 293 | |
|
324 | 294 | void getSelectedItems( |
|
325 | 295 | QTreeView *treeView, QVector<std::shared_ptr<DBEvent> > &events, |
|
326 | 296 | QVector<QPair<std::shared_ptr<DBEvent>, std::shared_ptr<DBEventProduct> > > &eventProducts) |
|
327 | 297 | { |
|
328 | 298 | for (auto rowIndex : treeView->selectionModel()->selectedRows()) { |
|
329 | 299 | auto itemType = m_Model->itemTypeOf(rowIndex); |
|
330 | 300 | if (itemType == CatalogueEventsModel::ItemType::Event) { |
|
331 | 301 | events << m_Model->getEvent(rowIndex); |
|
332 | 302 | } |
|
333 | 303 | else if (itemType == CatalogueEventsModel::ItemType::EventProduct) { |
|
334 | 304 | eventProducts << qMakePair(m_Model->getParentEvent(rowIndex), |
|
335 | 305 | m_Model->getEventProduct(rowIndex)); |
|
336 | 306 | } |
|
337 | 307 | } |
|
338 | 308 | } |
|
339 | 309 | }; |
|
340 | 310 | |
|
341 | 311 | CatalogueEventsWidget::CatalogueEventsWidget(QWidget *parent) |
|
342 | 312 | : QWidget(parent), |
|
343 | 313 | ui(new Ui::CatalogueEventsWidget), |
|
344 | 314 | impl{spimpl::make_unique_impl<CatalogueEventsWidgetPrivate>()} |
|
345 | 315 | { |
|
346 | 316 | ui->setupUi(this); |
|
347 | 317 | |
|
348 | 318 | impl->m_Model = new CatalogueEventsModel{this}; |
|
349 | 319 | ui->treeView->setModel(impl->m_Model); |
|
350 | 320 | |
|
351 | 321 | ui->treeView->setSortingEnabled(true); |
|
352 | 322 | ui->treeView->setDragDropMode(QAbstractItemView::DragDrop); |
|
353 | 323 | ui->treeView->setDragEnabled(true); |
|
354 | 324 | |
|
355 | 325 | |
|
356 | 326 | connect(ui->btnTime, &QToolButton::clicked, [this](auto checked) { |
|
357 | 327 | if (checked) { |
|
358 | 328 | ui->btnChart->setChecked(false); |
|
359 | 329 | impl->m_ZonesForTimeMode |
|
360 | 330 | = impl->selectZone(this, impl->m_ZonesForTimeMode, true, |
|
361 | 331 | this->mapToGlobal(ui->btnTime->frameGeometry().center())); |
|
362 | 332 | |
|
363 | 333 | impl->updateForTimeMode(ui->treeView); |
|
364 | 334 | } |
|
365 | 335 | }); |
|
366 | 336 | |
|
367 | 337 | connect(ui->btnChart, &QToolButton::clicked, [this](auto checked) { |
|
368 | 338 | if (checked) { |
|
369 | 339 | ui->btnTime->setChecked(false); |
|
370 | 340 | impl->m_ZoneForGraphMode |
|
371 | 341 | = impl->selectZone(this, {impl->m_ZoneForGraphMode}, false, |
|
372 | 342 | this->mapToGlobal(ui->btnChart->frameGeometry().center())) |
|
373 | 343 | .value(0); |
|
374 | 344 | |
|
375 | 345 | impl->updateForGraphMode(this); |
|
376 | 346 | } |
|
377 | 347 | }); |
|
378 | 348 | |
|
379 | 349 | connect(ui->btnRemove, &QToolButton::clicked, [this]() { |
|
380 | 350 | QVector<std::shared_ptr<DBEvent> > events; |
|
381 | 351 | QVector<QPair<std::shared_ptr<DBEvent>, std::shared_ptr<DBEventProduct> > > eventProducts; |
|
382 | 352 | impl->getSelectedItems(ui->treeView, events, eventProducts); |
|
383 | 353 | |
|
384 | 354 | if (!events.isEmpty() && eventProducts.isEmpty()) { |
|
385 | 355 | |
|
386 | 356 | auto canRemoveEvent |
|
387 | 357 | = !this->isAllEventsDisplayed() |
|
388 | 358 | || (QMessageBox::warning( |
|
389 | 359 | this, tr("Remove Event(s)"), |
|
390 | 360 | tr("The selected event(s) will be permanently removed " |
|
391 | 361 | "from the repository!\nAre you sure you want to continue?"), |
|
392 | 362 | QMessageBox::Yes | QMessageBox::No, QMessageBox::No) |
|
393 | 363 | == QMessageBox::Yes); |
|
394 | 364 | |
|
395 | 365 | if (canRemoveEvent) { |
|
396 | 366 | for (auto event : events) { |
|
397 | 367 | if (this->isAllEventsDisplayed()) { |
|
398 | 368 | sqpApp->catalogueController().removeEvent(event); |
|
399 | 369 | impl->removeEvent(event, ui->treeView); |
|
400 | 370 | } |
|
401 | 371 | else { |
|
402 | 372 | QVector<std::shared_ptr<DBCatalogue> > modifiedCatalogues; |
|
403 | 373 | for (auto catalogue : this->displayedCatalogues()) { |
|
404 | 374 | if (catalogue->removeEvent(event->getUniqId())) { |
|
405 | 375 | sqpApp->catalogueController().updateCatalogue(catalogue); |
|
406 | 376 | modifiedCatalogues << catalogue; |
|
407 | 377 | } |
|
408 | 378 | } |
|
409 | 379 | if (!modifiedCatalogues.empty()) { |
|
410 | 380 | emit eventCataloguesModified(modifiedCatalogues); |
|
411 | 381 | } |
|
412 | 382 | } |
|
413 | 383 | impl->m_Model->removeEvent(event); |
|
414 | 384 | } |
|
415 | 385 | |
|
416 | 386 | |
|
417 | 387 | emit this->eventsRemoved(events); |
|
418 | 388 | } |
|
419 | 389 | } |
|
420 | 390 | }); |
|
421 | 391 | |
|
422 | 392 | connect(ui->treeView, &QTreeView::clicked, this, &CatalogueEventsWidget::emitSelection); |
|
423 | 393 | connect(ui->treeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, |
|
424 | 394 | &CatalogueEventsWidget::emitSelection); |
|
425 | 395 | |
|
426 | 396 | ui->btnRemove->setEnabled(false); // Disabled by default when nothing is selected |
|
427 | 397 | connect(ui->treeView->selectionModel(), &QItemSelectionModel::selectionChanged, [this]() { |
|
428 | 398 | auto isNotMultiSelection = ui->treeView->selectionModel()->selectedRows().count() <= 1; |
|
429 | 399 | ui->btnChart->setEnabled(isNotMultiSelection); |
|
430 | 400 | ui->btnTime->setEnabled(isNotMultiSelection); |
|
431 | 401 | |
|
432 | 402 | if (isNotMultiSelection && ui->btnTime->isChecked()) { |
|
433 | 403 | impl->updateForTimeMode(ui->treeView); |
|
434 | 404 | } |
|
435 | 405 | else if (isNotMultiSelection && ui->btnChart->isChecked()) { |
|
436 | 406 | impl->updateForGraphMode(this); |
|
437 | 407 | } |
|
438 | 408 | |
|
439 | 409 | QVector<std::shared_ptr<DBEvent> > events; |
|
440 | 410 | QVector<QPair<std::shared_ptr<DBEvent>, std::shared_ptr<DBEventProduct> > > eventProducts; |
|
441 | 411 | impl->getSelectedItems(ui->treeView, events, eventProducts); |
|
442 | 412 | ui->btnRemove->setEnabled(!events.isEmpty() && eventProducts.isEmpty()); |
|
443 | 413 | }); |
|
444 | 414 | |
|
445 | 415 | ui->treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents); |
|
446 | 416 | ui->treeView->header()->setSectionResizeMode((int)CatalogueEventsModel::Column::Tags, |
|
447 | 417 | QHeaderView::Stretch); |
|
448 | 418 | ui->treeView->header()->setSectionResizeMode((int)CatalogueEventsModel::Column::Validation, |
|
449 | 419 | QHeaderView::ResizeToContents); |
|
450 | 420 | ui->treeView->header()->setSectionResizeMode((int)CatalogueEventsModel::Column::Name, |
|
451 | 421 | QHeaderView::Interactive); |
|
452 | 422 | ui->treeView->header()->setSectionResizeMode((int)CatalogueEventsModel::Column::TStart, |
|
453 | 423 | QHeaderView::ResizeToContents); |
|
454 | 424 | ui->treeView->header()->setSectionResizeMode((int)CatalogueEventsModel::Column::TEnd, |
|
455 | 425 | QHeaderView::ResizeToContents); |
|
456 | 426 | ui->treeView->header()->setSortIndicatorShown(true); |
|
457 | 427 | |
|
458 | 428 | connect(impl->m_Model, &CatalogueEventsModel::modelSorted, [this]() { |
|
459 | 429 | auto allEvents = impl->m_Model->events(); |
|
460 | 430 | for (auto event : allEvents) { |
|
461 | 431 | setEventChanges(event, sqpApp->catalogueController().eventHasChanges(event)); |
|
462 | 432 | } |
|
463 | 433 | }); |
|
464 | 434 | |
|
465 | 435 | populateWithAllEvents(); |
|
466 | 436 | } |
|
467 | 437 | |
|
468 | 438 | CatalogueEventsWidget::~CatalogueEventsWidget() |
|
469 | 439 | { |
|
470 | 440 | delete ui; |
|
471 | 441 | } |
|
472 | 442 | |
|
473 | 443 | void CatalogueEventsWidget::setVisualizationWidget(VisualizationWidget *visualization) |
|
474 | 444 | { |
|
475 | 445 | impl->m_VisualizationWidget = visualization; |
|
476 | 446 | } |
|
477 | 447 | |
|
478 | 448 | void CatalogueEventsWidget::addEvent(const std::shared_ptr<DBEvent> &event) |
|
479 | 449 | { |
|
480 | 450 | impl->addEvent(event, ui->treeView); |
|
481 | 451 | } |
|
482 | 452 | |
|
483 | 453 | void CatalogueEventsWidget::setEventChanges(const std::shared_ptr<DBEvent> &event, bool hasChanges) |
|
484 | 454 | { |
|
485 | 455 | impl->m_Model->refreshEvent(event); |
|
486 | 456 | |
|
487 | 457 | auto eventIndex = impl->m_Model->indexOf(event); |
|
488 | 458 | auto validationIndex |
|
489 | 459 | = eventIndex.sibling(eventIndex.row(), (int)CatalogueEventsModel::Column::Validation); |
|
490 | 460 | |
|
491 | 461 | if (validationIndex.isValid()) { |
|
492 | 462 | if (hasChanges) { |
|
493 | 463 | if (ui->treeView->indexWidget(validationIndex) == nullptr) { |
|
494 | 464 | auto widget = CatalogueExplorerHelper::buildValidationWidget( |
|
495 | 465 | ui->treeView, |
|
496 | 466 | [this, event]() { |
|
497 | 467 | sqpApp->catalogueController().saveEvent(event); |
|
498 | 468 | setEventChanges(event, false); |
|
499 | 469 | }, |
|
500 | 470 | [this, event]() { |
|
501 | 471 | bool removed = false; |
|
502 | 472 | sqpApp->catalogueController().discardEvent(event, removed); |
|
503 | 473 | if (removed) { |
|
504 | 474 | impl->m_Model->removeEvent(event); |
|
505 | 475 | } |
|
506 | 476 | else { |
|
507 | 477 | setEventChanges(event, false); |
|
508 | 478 | impl->m_Model->refreshEvent(event, true); |
|
509 | 479 | } |
|
510 | 480 | emitSelection(); |
|
511 | 481 | }); |
|
512 | 482 | ui->treeView->setIndexWidget(validationIndex, widget); |
|
513 | 483 | ui->treeView->header()->resizeSection((int)CatalogueEventsModel::Column::Validation, |
|
514 | 484 | QHeaderView::ResizeToContents); |
|
515 | 485 | } |
|
516 | 486 | } |
|
517 | 487 | else { |
|
518 | 488 | // Note: the widget is destroyed |
|
519 | 489 | ui->treeView->setIndexWidget(validationIndex, nullptr); |
|
520 | 490 | } |
|
521 | 491 | } |
|
522 | 492 | else { |
|
523 | 493 | qCWarning(LOG_CatalogueEventsWidget()) |
|
524 | 494 | << "setEventChanges: the event is not displayed in the model."; |
|
525 | 495 | } |
|
526 | 496 | } |
|
527 | 497 | |
|
528 | 498 | QVector<std::shared_ptr<DBCatalogue> > CatalogueEventsWidget::displayedCatalogues() const |
|
529 | 499 | { |
|
530 | 500 | return impl->m_DisplayedCatalogues; |
|
531 | 501 | } |
|
532 | 502 | |
|
533 | 503 | bool CatalogueEventsWidget::isAllEventsDisplayed() const |
|
534 | 504 | { |
|
535 | 505 | return impl->m_AllEventDisplayed; |
|
536 | 506 | } |
|
537 | 507 | |
|
538 | 508 | bool CatalogueEventsWidget::isEventDisplayed(const std::shared_ptr<DBEvent> &event) const |
|
539 | 509 | { |
|
540 | 510 | return impl->m_Model->indexOf(event).isValid(); |
|
541 | 511 | } |
|
542 | 512 | |
|
543 | 513 | void CatalogueEventsWidget::refreshEvent(const std::shared_ptr<DBEvent> &event) |
|
544 | 514 | { |
|
545 | 515 | impl->m_Model->refreshEvent(event, true); |
|
546 | 516 | } |
|
547 | 517 | |
|
548 | 518 | void CatalogueEventsWidget::populateWithCatalogues( |
|
549 | 519 | const QVector<std::shared_ptr<DBCatalogue> > &catalogues) |
|
550 | 520 | { |
|
551 | 521 | impl->m_DisplayedCatalogues = catalogues; |
|
552 | 522 | impl->m_AllEventDisplayed = false; |
|
553 | 523 | |
|
554 | 524 | QSet<QUuid> eventIds; |
|
555 | 525 | QVector<std::shared_ptr<DBEvent> > events; |
|
556 | 526 | |
|
557 | 527 | for (auto catalogue : catalogues) { |
|
558 | 528 | auto catalogueEvents = sqpApp->catalogueController().retrieveEventsFromCatalogue(catalogue); |
|
559 | 529 | for (auto event : catalogueEvents) { |
|
560 | 530 | if (!eventIds.contains(event->getUniqId())) { |
|
561 | 531 | events << event; |
|
562 | 532 | eventIds.insert(event->getUniqId()); |
|
563 | 533 | } |
|
564 | 534 | } |
|
565 | 535 | } |
|
566 | 536 | |
|
567 | 537 | impl->setEvents(events, this); |
|
568 | 538 | } |
|
569 | 539 | |
|
570 | 540 | void CatalogueEventsWidget::populateWithAllEvents() |
|
571 | 541 | { |
|
572 | 542 | impl->m_DisplayedCatalogues.clear(); |
|
573 | 543 | impl->m_AllEventDisplayed = true; |
|
574 | 544 | |
|
575 | 545 | auto allEvents = sqpApp->catalogueController().retrieveAllEvents(); |
|
576 | 546 | |
|
577 | 547 | QVector<std::shared_ptr<DBEvent> > events; |
|
578 | 548 | for (auto event : allEvents) { |
|
579 | 549 | events << event; |
|
580 | 550 | } |
|
581 | 551 | |
|
582 | 552 | impl->setEvents(events, this); |
|
583 | 553 | } |
|
584 | 554 | |
|
585 | 555 | void CatalogueEventsWidget::clear() |
|
586 | 556 | { |
|
587 | 557 | impl->m_DisplayedCatalogues.clear(); |
|
588 | 558 | impl->m_AllEventDisplayed = false; |
|
589 | 559 | impl->setEvents({}, this); |
|
590 | 560 | } |
|
591 | 561 | |
|
592 | 562 | void CatalogueEventsWidget::refresh() |
|
593 | 563 | { |
|
594 | 564 | if (isAllEventsDisplayed()) { |
|
595 | 565 | populateWithAllEvents(); |
|
596 | 566 | } |
|
597 | 567 | else if (!impl->m_DisplayedCatalogues.isEmpty()) { |
|
598 | 568 | populateWithCatalogues(impl->m_DisplayedCatalogues); |
|
599 | 569 | } |
|
600 | 570 | } |
|
601 | 571 | |
|
602 | 572 | void CatalogueEventsWidget::emitSelection() |
|
603 | 573 | { |
|
604 | 574 | QVector<std::shared_ptr<DBEvent> > events; |
|
605 | 575 | QVector<QPair<std::shared_ptr<DBEvent>, std::shared_ptr<DBEventProduct> > > eventProducts; |
|
606 | 576 | impl->getSelectedItems(ui->treeView, events, eventProducts); |
|
607 | 577 | |
|
608 | 578 | if (!events.isEmpty() && eventProducts.isEmpty()) { |
|
609 | 579 | emit eventsSelected(events); |
|
610 | 580 | } |
|
611 | 581 | else if (events.isEmpty() && !eventProducts.isEmpty()) { |
|
612 | 582 | emit eventProductsSelected(eventProducts); |
|
613 | 583 | } |
|
614 | 584 | else { |
|
615 | 585 | emit selectionCleared(); |
|
616 | 586 | } |
|
617 | 587 | } |
|
618 | 588 | |
|
619 | 589 | |
|
620 | 590 | void CatalogueEventsWidget::keyPressEvent(QKeyEvent *event) |
|
621 | 591 | { |
|
622 | 592 | switch (event->key()) { |
|
623 | 593 | case Qt::Key_Delete: { |
|
624 | 594 | ui->btnRemove->click(); |
|
625 | 595 | } |
|
626 | 596 | default: |
|
627 | 597 | break; |
|
628 | 598 | } |
|
629 | 599 | } |
General Comments 0
You need to be logged in to leave comments.
Login now