CatalogueEventsWidget.cpp
629 lines
| 23.5 KiB
| text/x-c
|
CppLexer
r1130 | #include "Catalogue/CatalogueEventsWidget.h" | |||
#include "ui_CatalogueEventsWidget.h" | ||||
r1162 | #include <Catalogue/CatalogueController.h> | |||
r1182 | #include <Catalogue/CatalogueEventsModel.h> | |||
r1194 | #include <Catalogue/CatalogueExplorerHelper.h> | |||
r1162 | #include <CatalogueDao.h> | |||
#include <DBCatalogue.h> | ||||
r1344 | #include <DBEventProduct.h> | |||
#include <DataSource/DataSourceController.h> | ||||
r1345 | #include <DataSource/DataSourceItem.h> | |||
r1162 | #include <SqpApplication.h> | |||
r1344 | #include <Variable/Variable.h> | |||
#include <Variable/VariableController.h> | ||||
r1345 | #include <Visualization/VisualizationGraphWidget.h> | |||
r1169 | #include <Visualization/VisualizationTabWidget.h> | |||
#include <Visualization/VisualizationWidget.h> | ||||
r1171 | #include <Visualization/VisualizationZoneWidget.h> | |||
r1162 | ||||
r1168 | #include <QDialog> | |||
#include <QDialogButtonBox> | ||||
r1368 | #include <QKeyEvent> | |||
r1168 | #include <QListWidget> | |||
r1296 | #include <QMessageBox> | |||
r1168 | ||||
r1171 | Q_LOGGING_CATEGORY(LOG_CatalogueEventsWidget, "CatalogueEventsWidget") | |||
r1162 | ||||
r1346 | /// Percentage added to the range of a event when it is displayed | |||
const auto EVENT_RANGE_MARGE = 30; // in % | ||||
r1136 | struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate { | |||
r1182 | CatalogueEventsModel *m_Model = nullptr; | |||
r1171 | QStringList m_ZonesForTimeMode; | |||
r1168 | QString m_ZoneForGraphMode; | |||
r1286 | QVector<std::shared_ptr<DBCatalogue> > m_DisplayedCatalogues; | |||
r1311 | bool m_AllEventDisplayed = false; | |||
r1344 | QVector<VisualizationGraphWidget *> m_CustomGraphs; | |||
r1167 | ||||
r1169 | VisualizationWidget *m_VisualizationWidget = nullptr; | |||
r1292 | void setEvents(const QVector<std::shared_ptr<DBEvent> > &events, CatalogueEventsWidget *widget) | |||
r1167 | { | |||
r1292 | widget->ui->treeView->setSortingEnabled(false); | |||
r1362 | m_Model->setSourceCatalogues(m_DisplayedCatalogues); | |||
r1167 | m_Model->setEvents(events); | |||
r1292 | widget->ui->treeView->setSortingEnabled(true); | |||
for (auto event : events) { | ||||
if (sqpApp->catalogueController().eventHasChanges(event)) { | ||||
auto index = m_Model->indexOf(event); | ||||
widget->setEventChanges(event, true); | ||||
} | ||||
} | ||||
r1167 | } | |||
r1182 | void addEvent(const std::shared_ptr<DBEvent> &event, QTreeView *treeView) | |||
r1167 | { | |||
r1182 | treeView->setSortingEnabled(false); | |||
r1167 | m_Model->addEvent(event); | |||
r1182 | treeView->setSortingEnabled(true); | |||
r1167 | } | |||
r1182 | void removeEvent(const std::shared_ptr<DBEvent> &event, QTreeView *treeView) | |||
r1167 | { | |||
r1182 | treeView->setSortingEnabled(false); | |||
r1167 | m_Model->removeEvent(event); | |||
r1182 | treeView->setSortingEnabled(true); | |||
r1167 | } | |||
r1136 | ||||
r1169 | QStringList getAvailableVisualizationZoneList() const | |||
{ | ||||
if (m_VisualizationWidget) { | ||||
if (auto tab = m_VisualizationWidget->currentTabWidget()) { | ||||
return tab->availableZoneWidgets(); | ||||
} | ||||
} | ||||
return QStringList{}; | ||||
} | ||||
QStringList selectZone(QWidget *parent, const QStringList &selectedZones, | ||||
bool allowMultiSelection, const QPoint &location) | ||||
r1168 | { | |||
r1169 | auto availableZones = getAvailableVisualizationZoneList(); | |||
if (availableZones.isEmpty()) { | ||||
return QStringList{}; | ||||
} | ||||
r1168 | QDialog d(parent, Qt::Tool); | |||
d.setWindowTitle("Choose a zone"); | ||||
auto layout = new QVBoxLayout{&d}; | ||||
layout->setContentsMargins(0, 0, 0, 0); | ||||
auto listWidget = new QListWidget{&d}; | ||||
layout->addWidget(listWidget); | ||||
QSet<QListWidgetItem *> checkedItems; | ||||
for (auto zone : availableZones) { | ||||
auto item = new QListWidgetItem{zone}; | ||||
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable); | ||||
if (selectedZones.contains(zone)) { | ||||
item->setCheckState(Qt::Checked); | ||||
checkedItems << item; | ||||
} | ||||
else { | ||||
item->setCheckState(Qt::Unchecked); | ||||
} | ||||
listWidget->addItem(item); | ||||
} | ||||
auto buttonBox = new QDialogButtonBox{QDialogButtonBox::Ok, &d}; | ||||
layout->addWidget(buttonBox); | ||||
QObject::connect(buttonBox, &QDialogButtonBox::accepted, &d, &QDialog::accept); | ||||
QObject::connect(buttonBox, &QDialogButtonBox::rejected, &d, &QDialog::reject); | ||||
QObject::connect(listWidget, &QListWidget::itemChanged, | ||||
[&checkedItems, allowMultiSelection, listWidget](auto item) { | ||||
if (item->checkState() == Qt::Checked) { | ||||
if (!allowMultiSelection) { | ||||
for (auto checkedItem : checkedItems) { | ||||
listWidget->blockSignals(true); | ||||
checkedItem->setCheckState(Qt::Unchecked); | ||||
listWidget->blockSignals(false); | ||||
} | ||||
checkedItems.clear(); | ||||
} | ||||
checkedItems << item; | ||||
} | ||||
else { | ||||
checkedItems.remove(item); | ||||
} | ||||
}); | ||||
QStringList result; | ||||
d.setMinimumWidth(120); | ||||
d.resize(d.minimumSizeHint()); | ||||
d.move(location); | ||||
if (d.exec() == QDialog::Accepted) { | ||||
for (auto item : checkedItems) { | ||||
result += item->text(); | ||||
} | ||||
} | ||||
r1169 | else { | |||
result = selectedZones; | ||||
} | ||||
r1168 | ||||
return result; | ||||
} | ||||
r1171 | ||||
r1182 | void updateForTimeMode(QTreeView *treeView) | |||
r1171 | { | |||
r1182 | auto selectedRows = treeView->selectionModel()->selectedRows(); | |||
r1171 | ||||
if (selectedRows.count() == 1) { | ||||
r1183 | auto event = m_Model->getEvent(selectedRows.first()); | |||
r1185 | if (event) { | |||
if (m_VisualizationWidget) { | ||||
if (auto tab = m_VisualizationWidget->currentTabWidget()) { | ||||
for (auto zoneName : m_ZonesForTimeMode) { | ||||
if (auto zone = tab->getZoneWithName(zoneName)) { | ||||
SqpRange eventRange; | ||||
eventRange.m_TStart = event->getTStart(); | ||||
eventRange.m_TEnd = event->getTEnd(); | ||||
zone->setZoneRange(eventRange); | ||||
} | ||||
r1171 | } | |||
} | ||||
r1185 | else { | |||
qCWarning(LOG_CatalogueEventsWidget()) | ||||
<< "updateTimeZone: no tab found in the visualization"; | ||||
} | ||||
r1171 | } | |||
else { | ||||
qCWarning(LOG_CatalogueEventsWidget()) | ||||
r1185 | << "updateTimeZone: visualization widget not found"; | |||
r1171 | } | |||
} | ||||
} | ||||
else { | ||||
qCWarning(LOG_CatalogueEventsWidget()) | ||||
<< "updateTimeZone: not compatible with multiple events selected"; | ||||
} | ||||
} | ||||
r1346 | QVector<SqpRange> getGraphRanges(const std::shared_ptr<DBEvent> &event) | |||
{ | ||||
// Retrieves the range of each product and the maximum size | ||||
QVector<SqpRange> graphRanges; | ||||
double maxDt = 0; | ||||
for (auto eventProduct : event->getEventProducts()) { | ||||
SqpRange eventRange; | ||||
eventRange.m_TStart = eventProduct.getTStart(); | ||||
eventRange.m_TEnd = eventProduct.getTEnd(); | ||||
graphRanges << eventRange; | ||||
auto dt = eventRange.m_TEnd - eventRange.m_TStart; | ||||
if (dt > maxDt) { | ||||
maxDt = dt; | ||||
} | ||||
} | ||||
// Adds the marge | ||||
maxDt *= (100.0 + EVENT_RANGE_MARGE) / 100.0; | ||||
// Corrects the graph ranges so that they all have the same size | ||||
QVector<SqpRange> correctedGraphRanges; | ||||
for (auto range : graphRanges) { | ||||
auto dt = range.m_TEnd - range.m_TStart; | ||||
auto diff = qAbs((maxDt - dt) / 2.0); | ||||
SqpRange correctedRange; | ||||
correctedRange.m_TStart = range.m_TStart - diff; | ||||
correctedRange.m_TEnd = range.m_TEnd + diff; | ||||
correctedGraphRanges << correctedRange; | ||||
} | ||||
return correctedGraphRanges; | ||||
} | ||||
r1347 | void updateForGraphMode(CatalogueEventsWidget *catalogueEventWidget) | |||
r1171 | { | |||
r1347 | auto selectedRows = catalogueEventWidget->ui->treeView->selectionModel()->selectedRows(); | |||
r1346 | if (selectedRows.count() != 1) { | |||
qCWarning(LOG_CatalogueEventsWidget()) | ||||
<< "updateGraphMode: not compatible with multiple events selected"; | ||||
return; | ||||
} | ||||
r1171 | ||||
r1346 | if (!m_VisualizationWidget) { | |||
qCWarning(LOG_CatalogueEventsWidget()) | ||||
<< "updateGraphMode: visualization widget not found"; | ||||
return; | ||||
} | ||||
auto event = m_Model->getEvent(selectedRows.first()); | ||||
if (!event) { | ||||
// A event product is probably selected | ||||
qCInfo(LOG_CatalogueEventsWidget()) << "updateGraphMode: no events are selected"; | ||||
return; | ||||
} | ||||
auto tab = m_VisualizationWidget->currentTabWidget(); | ||||
if (!tab) { | ||||
qCWarning(LOG_CatalogueEventsWidget()) | ||||
<< "updateGraphMode: no tab found in the visualization"; | ||||
return; | ||||
} | ||||
r1345 | ||||
r1346 | auto zone = tab->getZoneWithName(m_ZoneForGraphMode); | |||
if (!zone) { | ||||
qCWarning(LOG_CatalogueEventsWidget()) << "updateGraphMode: zone not found"; | ||||
return; | ||||
} | ||||
r1345 | ||||
r1361 | // Closes the previous graph and delete the asociated variables | |||
r1346 | for (auto graph : m_CustomGraphs) { | |||
graph->close(); | ||||
auto variables = graph->variables().toVector(); | ||||
r1345 | ||||
r1346 | QMetaObject::invokeMethod(&sqpApp->variableController(), "deleteVariables", | |||
Qt::QueuedConnection, | ||||
Q_ARG(QVector<std::shared_ptr<Variable> >, variables)); | ||||
} | ||||
m_CustomGraphs.clear(); | ||||
r1345 | ||||
r1361 | // Closes the remaining graphs inside the zone | |||
zone->closeAllGraphs(); | ||||
r1346 | // Calculates the range of each graph which will be created | |||
auto graphRange = getGraphRanges(event); | ||||
r1344 | ||||
r1346 | // Loops through the event products and create the graph | |||
auto itRange = graphRange.cbegin(); | ||||
for (auto eventProduct : event->getEventProducts()) { | ||||
auto productId = eventProduct.getProductId(); | ||||
r1345 | ||||
r1346 | auto range = *itRange; | |||
++itRange; | ||||
r1345 | ||||
r1346 | SqpRange productRange; | |||
productRange.m_TStart = eventProduct.getTStart(); | ||||
productRange.m_TEnd = eventProduct.getTEnd(); | ||||
r1345 | ||||
r1347 | auto context = new QObject{catalogueEventWidget}; | |||
r1346 | QObject::connect( | |||
&sqpApp->variableController(), &VariableController::variableAdded, context, | ||||
r1347 | [this, catalogueEventWidget, zone, context, event, range, productRange, | |||
productId](auto variable) { | ||||
r1345 | ||||
r1346 | if (variable->metadata().value(DataSourceItem::ID_DATA_KEY).toString() | |||
== productId) { | ||||
auto graph = zone->createGraph(variable); | ||||
graph->setAutoRangeOnVariableInitialization(false); | ||||
r1344 | ||||
r1347 | auto selectionZone | |||
= graph->addSelectionZone(event->getName(), productRange); | ||||
emit catalogueEventWidget->selectionZoneAdded(event, productId, | ||||
selectionZone); | ||||
r1346 | m_CustomGraphs << graph; | |||
graph->setGraphRange(range, true); | ||||
// Removes the graph from the graph list if it is closed manually | ||||
QObject::connect(graph, &VisualizationGraphWidget::destroyed, | ||||
[this, graph]() { m_CustomGraphs.removeAll(graph); }); | ||||
delete context; // removes the connection | ||||
r1171 | } | |||
r1346 | }, | |||
Qt::QueuedConnection); | ||||
QMetaObject::invokeMethod(&sqpApp->dataSourceController(), | ||||
"requestVariableFromProductIdKey", Qt::QueuedConnection, | ||||
Q_ARG(QString, productId)); | ||||
r1171 | } | |||
} | ||||
r1296 | ||||
void getSelectedItems( | ||||
QTreeView *treeView, QVector<std::shared_ptr<DBEvent> > &events, | ||||
QVector<QPair<std::shared_ptr<DBEvent>, std::shared_ptr<DBEventProduct> > > &eventProducts) | ||||
{ | ||||
for (auto rowIndex : treeView->selectionModel()->selectedRows()) { | ||||
auto itemType = m_Model->itemTypeOf(rowIndex); | ||||
if (itemType == CatalogueEventsModel::ItemType::Event) { | ||||
events << m_Model->getEvent(rowIndex); | ||||
} | ||||
else if (itemType == CatalogueEventsModel::ItemType::EventProduct) { | ||||
eventProducts << qMakePair(m_Model->getParentEvent(rowIndex), | ||||
m_Model->getEventProduct(rowIndex)); | ||||
} | ||||
} | ||||
} | ||||
r1168 | }; | |||
r1136 | ||||
r1130 | CatalogueEventsWidget::CatalogueEventsWidget(QWidget *parent) | |||
r1136 | : QWidget(parent), | |||
ui(new Ui::CatalogueEventsWidget), | ||||
impl{spimpl::make_unique_impl<CatalogueEventsWidgetPrivate>()} | ||||
r1130 | { | |||
ui->setupUi(this); | ||||
r1136 | ||||
r1182 | impl->m_Model = new CatalogueEventsModel{this}; | |||
ui->treeView->setModel(impl->m_Model); | ||||
r1163 | ||||
r1182 | ui->treeView->setSortingEnabled(true); | |||
ui->treeView->setDragDropMode(QAbstractItemView::DragDrop); | ||||
ui->treeView->setDragEnabled(true); | ||||
r1163 | ||||
r1369 | ||||
r1136 | connect(ui->btnTime, &QToolButton::clicked, [this](auto checked) { | |||
if (checked) { | ||||
ui->btnChart->setChecked(false); | ||||
r1171 | impl->m_ZonesForTimeMode | |||
= impl->selectZone(this, impl->m_ZonesForTimeMode, true, | ||||
this->mapToGlobal(ui->btnTime->frameGeometry().center())); | ||||
r1182 | impl->updateForTimeMode(ui->treeView); | |||
r1136 | } | |||
}); | ||||
connect(ui->btnChart, &QToolButton::clicked, [this](auto checked) { | ||||
if (checked) { | ||||
ui->btnTime->setChecked(false); | ||||
r1168 | impl->m_ZoneForGraphMode | |||
r1169 | = impl->selectZone(this, {impl->m_ZoneForGraphMode}, false, | |||
r1168 | this->mapToGlobal(ui->btnChart->frameGeometry().center())) | |||
.value(0); | ||||
r1171 | ||||
r1347 | impl->updateForGraphMode(this); | |||
r1136 | } | |||
}); | ||||
r1296 | connect(ui->btnRemove, &QToolButton::clicked, [this]() { | |||
r1176 | QVector<std::shared_ptr<DBEvent> > events; | |||
r1183 | QVector<QPair<std::shared_ptr<DBEvent>, std::shared_ptr<DBEventProduct> > > eventProducts; | |||
r1296 | impl->getSelectedItems(ui->treeView, events, eventProducts); | |||
r1183 | ||||
r1296 | if (!events.isEmpty() && eventProducts.isEmpty()) { | |||
r1183 | ||||
r1366 | auto canRemoveEvent | |||
= !this->isAllEventsDisplayed() | ||||
|| (QMessageBox::warning( | ||||
this, tr("Remove Event(s)"), | ||||
tr("The selected event(s) will be permanently removed " | ||||
"from the repository!\nAre you sure you want to continue?"), | ||||
QMessageBox::Yes | QMessageBox::No, QMessageBox::No) | ||||
== QMessageBox::Yes); | ||||
if (canRemoveEvent) { | ||||
r1296 | for (auto event : events) { | |||
r1366 | if (this->isAllEventsDisplayed()) { | |||
r1367 | sqpApp->catalogueController().removeEvent(event); | |||
r1366 | impl->removeEvent(event, ui->treeView); | |||
} | ||||
else { | ||||
r1367 | QVector<std::shared_ptr<DBCatalogue> > modifiedCatalogues; | |||
r1366 | for (auto catalogue : this->displayedCatalogues()) { | |||
r1367 | if (catalogue->removeEvent(event->getUniqId())) { | |||
sqpApp->catalogueController().updateCatalogue(catalogue); | ||||
modifiedCatalogues << catalogue; | ||||
} | ||||
} | ||||
if (!modifiedCatalogues.empty()) { | ||||
emit eventCataloguesModified(modifiedCatalogues); | ||||
r1366 | } | |||
} | ||||
impl->m_Model->removeEvent(event); | ||||
r1296 | } | |||
r1347 | ||||
r1366 | ||||
r1347 | emit this->eventsRemoved(events); | |||
r1183 | } | |||
r1164 | } | |||
r1296 | }); | |||
r1300 | connect(ui->treeView, &QTreeView::clicked, this, &CatalogueEventsWidget::emitSelection); | |||
connect(ui->treeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, | ||||
&CatalogueEventsWidget::emitSelection); | ||||
r1140 | ||||
r1296 | ui->btnRemove->setEnabled(false); // Disabled by default when nothing is selected | |||
r1182 | connect(ui->treeView->selectionModel(), &QItemSelectionModel::selectionChanged, [this]() { | |||
auto isNotMultiSelection = ui->treeView->selectionModel()->selectedRows().count() <= 1; | ||||
r1141 | ui->btnChart->setEnabled(isNotMultiSelection); | |||
ui->btnTime->setEnabled(isNotMultiSelection); | ||||
r1171 | ||||
if (isNotMultiSelection && ui->btnTime->isChecked()) { | ||||
r1182 | impl->updateForTimeMode(ui->treeView); | |||
r1171 | } | |||
else if (isNotMultiSelection && ui->btnChart->isChecked()) { | ||||
r1347 | impl->updateForGraphMode(this); | |||
r1171 | } | |||
r1296 | ||||
QVector<std::shared_ptr<DBEvent> > events; | ||||
QVector<QPair<std::shared_ptr<DBEvent>, std::shared_ptr<DBEventProduct> > > eventProducts; | ||||
impl->getSelectedItems(ui->treeView, events, eventProducts); | ||||
ui->btnRemove->setEnabled(!events.isEmpty() && eventProducts.isEmpty()); | ||||
r1140 | }); | |||
r1138 | ||||
r1182 | ui->treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents); | |||
r1293 | ui->treeView->header()->setSectionResizeMode((int)CatalogueEventsModel::Column::Tags, | |||
r1194 | QHeaderView::Stretch); | |||
ui->treeView->header()->setSectionResizeMode((int)CatalogueEventsModel::Column::Validation, | ||||
r1372 | QHeaderView::ResizeToContents); | |||
r1293 | ui->treeView->header()->setSectionResizeMode((int)CatalogueEventsModel::Column::Name, | |||
QHeaderView::Interactive); | ||||
r1369 | ui->treeView->header()->setSectionResizeMode((int)CatalogueEventsModel::Column::TStart, | |||
QHeaderView::ResizeToContents); | ||||
ui->treeView->header()->setSectionResizeMode((int)CatalogueEventsModel::Column::TEnd, | ||||
QHeaderView::ResizeToContents); | ||||
r1182 | ui->treeView->header()->setSortIndicatorShown(true); | |||
r1194 | ||||
connect(impl->m_Model, &CatalogueEventsModel::modelSorted, [this]() { | ||||
auto allEvents = impl->m_Model->events(); | ||||
for (auto event : allEvents) { | ||||
r1292 | setEventChanges(event, sqpApp->catalogueController().eventHasChanges(event)); | |||
r1194 | } | |||
}); | ||||
r1289 | ||||
populateWithAllEvents(); | ||||
r1130 | } | |||
CatalogueEventsWidget::~CatalogueEventsWidget() | ||||
{ | ||||
delete ui; | ||||
} | ||||
r1136 | ||||
r1169 | void CatalogueEventsWidget::setVisualizationWidget(VisualizationWidget *visualization) | |||
{ | ||||
impl->m_VisualizationWidget = visualization; | ||||
} | ||||
r1286 | void CatalogueEventsWidget::addEvent(const std::shared_ptr<DBEvent> &event) | |||
{ | ||||
impl->addEvent(event, ui->treeView); | ||||
} | ||||
r1181 | void CatalogueEventsWidget::setEventChanges(const std::shared_ptr<DBEvent> &event, bool hasChanges) | |||
{ | ||||
impl->m_Model->refreshEvent(event); | ||||
r1194 | ||||
auto eventIndex = impl->m_Model->indexOf(event); | ||||
auto validationIndex | ||||
= eventIndex.sibling(eventIndex.row(), (int)CatalogueEventsModel::Column::Validation); | ||||
r1290 | if (validationIndex.isValid()) { | |||
if (hasChanges) { | ||||
if (ui->treeView->indexWidget(validationIndex) == nullptr) { | ||||
auto widget = CatalogueExplorerHelper::buildValidationWidget( | ||||
ui->treeView, | ||||
[this, event]() { | ||||
sqpApp->catalogueController().saveEvent(event); | ||||
setEventChanges(event, false); | ||||
}, | ||||
r1298 | [this, event]() { | |||
r1314 | bool removed = false; | |||
sqpApp->catalogueController().discardEvent(event, removed); | ||||
if (removed) { | ||||
impl->m_Model->removeEvent(event); | ||||
} | ||||
else { | ||||
setEventChanges(event, false); | ||||
impl->m_Model->refreshEvent(event, true); | ||||
} | ||||
r1300 | emitSelection(); | |||
r1298 | }); | |||
r1290 | ui->treeView->setIndexWidget(validationIndex, widget); | |||
r1372 | ui->treeView->header()->resizeSection((int)CatalogueEventsModel::Column::Validation, | |||
QHeaderView::ResizeToContents); | ||||
r1290 | } | |||
r1292 | } | |||
else { | ||||
// Note: the widget is destroyed | ||||
ui->treeView->setIndexWidget(validationIndex, nullptr); | ||||
r1290 | } | |||
r1194 | } | |||
else { | ||||
r1286 | qCWarning(LOG_CatalogueEventsWidget()) | |||
<< "setEventChanges: the event is not displayed in the model."; | ||||
r1194 | } | |||
r1286 | } | |||
QVector<std::shared_ptr<DBCatalogue> > CatalogueEventsWidget::displayedCatalogues() const | ||||
{ | ||||
return impl->m_DisplayedCatalogues; | ||||
} | ||||
bool CatalogueEventsWidget::isAllEventsDisplayed() const | ||||
{ | ||||
r1311 | return impl->m_AllEventDisplayed; | |||
r1286 | } | |||
r1194 | ||||
r1286 | bool CatalogueEventsWidget::isEventDisplayed(const std::shared_ptr<DBEvent> &event) const | |||
{ | ||||
return impl->m_Model->indexOf(event).isValid(); | ||||
r1181 | } | |||
r1347 | void CatalogueEventsWidget::refreshEvent(const std::shared_ptr<DBEvent> &event) | |||
{ | ||||
impl->m_Model->refreshEvent(event, true); | ||||
} | ||||
r1176 | void CatalogueEventsWidget::populateWithCatalogues( | |||
const QVector<std::shared_ptr<DBCatalogue> > &catalogues) | ||||
r1138 | { | |||
r1286 | impl->m_DisplayedCatalogues = catalogues; | |||
r1311 | impl->m_AllEventDisplayed = false; | |||
r1286 | ||||
r1165 | QSet<QUuid> eventIds; | |||
r1176 | QVector<std::shared_ptr<DBEvent> > events; | |||
r1165 | ||||
for (auto catalogue : catalogues) { | ||||
r1176 | auto catalogueEvents = sqpApp->catalogueController().retrieveEventsFromCatalogue(catalogue); | |||
r1165 | for (auto event : catalogueEvents) { | |||
r1176 | if (!eventIds.contains(event->getUniqId())) { | |||
r1165 | events << event; | |||
r1176 | eventIds.insert(event->getUniqId()); | |||
r1165 | } | |||
} | ||||
r1162 | } | |||
r1138 | ||||
r1292 | impl->setEvents(events, this); | |||
r1136 | } | |||
r1192 | ||||
void CatalogueEventsWidget::populateWithAllEvents() | ||||
{ | ||||
r1286 | impl->m_DisplayedCatalogues.clear(); | |||
r1311 | impl->m_AllEventDisplayed = true; | |||
r1286 | ||||
r1192 | auto allEvents = sqpApp->catalogueController().retrieveAllEvents(); | |||
QVector<std::shared_ptr<DBEvent> > events; | ||||
for (auto event : allEvents) { | ||||
events << event; | ||||
} | ||||
r1292 | impl->setEvents(events, this); | |||
r1192 | } | |||
r1286 | ||||
r1289 | void CatalogueEventsWidget::clear() | |||
{ | ||||
impl->m_DisplayedCatalogues.clear(); | ||||
r1311 | impl->m_AllEventDisplayed = false; | |||
r1292 | impl->setEvents({}, this); | |||
r1289 | } | |||
r1286 | void CatalogueEventsWidget::refresh() | |||
{ | ||||
r1311 | if (isAllEventsDisplayed()) { | |||
r1286 | populateWithAllEvents(); | |||
} | ||||
r1311 | else if (!impl->m_DisplayedCatalogues.isEmpty()) { | |||
r1286 | populateWithCatalogues(impl->m_DisplayedCatalogues); | |||
} | ||||
} | ||||
r1300 | ||||
void CatalogueEventsWidget::emitSelection() | ||||
{ | ||||
QVector<std::shared_ptr<DBEvent> > events; | ||||
QVector<QPair<std::shared_ptr<DBEvent>, std::shared_ptr<DBEventProduct> > > eventProducts; | ||||
impl->getSelectedItems(ui->treeView, events, eventProducts); | ||||
if (!events.isEmpty() && eventProducts.isEmpty()) { | ||||
emit eventsSelected(events); | ||||
} | ||||
else if (events.isEmpty() && !eventProducts.isEmpty()) { | ||||
emit eventProductsSelected(eventProducts); | ||||
} | ||||
else { | ||||
emit selectionCleared(); | ||||
} | ||||
} | ||||
r1368 | ||||
void CatalogueEventsWidget::keyPressEvent(QKeyEvent *event) | ||||
{ | ||||
switch (event->key()) { | ||||
case Qt::Key_Delete: { | ||||
ui->btnRemove->click(); | ||||
} | ||||
default: | ||||
break; | ||||
} | ||||
} | ||||