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