From 7497b3f1d25442bc5341cade59c68a2e03e36739 2017-12-20 09:36:15 From: Thibaud Rabillard Date: 2017-12-20 09:36:15 Subject: [PATCH] connect remove event button --- diff --git a/core/src/Catalogue/CatalogueController.cpp b/core/src/Catalogue/CatalogueController.cpp index 653d975..fccf228 100644 --- a/core/src/Catalogue/CatalogueController.cpp +++ b/core/src/Catalogue/CatalogueController.cpp @@ -159,6 +159,7 @@ void CatalogueController::removeEvent(std::shared_ptr event) impl->m_CatalogueDao.removeEvent(*event); event->setRepository(impl->toSyncRepository(event->getRepository())); impl->m_CatalogueDao.removeEvent(*event); + impl->savAllDB(); } void CatalogueController::addEvent(std::shared_ptr event) diff --git a/gui/src/Catalogue/CatalogueEventsWidget.cpp b/gui/src/Catalogue/CatalogueEventsWidget.cpp index e8eb332..35a9f50 100644 --- a/gui/src/Catalogue/CatalogueEventsWidget.cpp +++ b/gui/src/Catalogue/CatalogueEventsWidget.cpp @@ -14,6 +14,7 @@ #include #include #include +#include Q_LOGGING_CATEGORY(LOG_CatalogueEventsWidget, "CatalogueEventsWidget") @@ -203,6 +204,22 @@ struct CatalogueEventsWidget::CatalogueEventsWidgetPrivate { << "updateGraphMode: not compatible with multiple events selected"; } } + + void getSelectedItems( + QTreeView *treeView, QVector > &events, + QVector, std::shared_ptr > > &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)); + } + } + } }; CatalogueEventsWidget::CatalogueEventsWidget(QWidget *parent) @@ -242,21 +259,31 @@ CatalogueEventsWidget::CatalogueEventsWidget(QWidget *parent) } }); - auto emitSelection = [this]() { + connect(ui->btnRemove, &QToolButton::clicked, [this]() { QVector > events; QVector, std::shared_ptr > > eventProducts; + impl->getSelectedItems(ui->treeView, events, eventProducts); - for (auto rowIndex : ui->treeView->selectionModel()->selectedRows()) { + if (!events.isEmpty() && eventProducts.isEmpty()) { - auto itemType = impl->m_Model->itemTypeOf(rowIndex); - if (itemType == CatalogueEventsModel::ItemType::Event) { - events << impl->m_Model->getEvent(rowIndex); - } - else if (itemType == CatalogueEventsModel::ItemType::EventProduct) { - eventProducts << qMakePair(impl->m_Model->getParentEvent(rowIndex), - impl->m_Model->getEventProduct(rowIndex)); + if (QMessageBox::warning(this, tr("Remove Event(s)"), + tr("The selected event(s) will be completly removed " + "from the repository!\nAre you sure you want to continue?"), + QMessageBox::Yes | QMessageBox::No, QMessageBox::No) + == QMessageBox::Yes) { + + for (auto event : events) { + sqpApp->catalogueController().removeEvent(event); + impl->removeEvent(event, ui->treeView); + } } } + }); + + auto emitSelection = [this]() { + QVector > events; + QVector, std::shared_ptr > > eventProducts; + impl->getSelectedItems(ui->treeView, events, eventProducts); if (!events.isEmpty() && eventProducts.isEmpty()) { emit this->eventsSelected(events); @@ -272,6 +299,7 @@ CatalogueEventsWidget::CatalogueEventsWidget(QWidget *parent) connect(ui->treeView, &QTreeView::clicked, emitSelection); connect(ui->treeView->selectionModel(), &QItemSelectionModel::selectionChanged, emitSelection); + ui->btnRemove->setEnabled(false); // Disabled by default when nothing is selected connect(ui->treeView->selectionModel(), &QItemSelectionModel::selectionChanged, [this]() { auto isNotMultiSelection = ui->treeView->selectionModel()->selectedRows().count() <= 1; ui->btnChart->setEnabled(isNotMultiSelection); @@ -283,6 +311,11 @@ CatalogueEventsWidget::CatalogueEventsWidget(QWidget *parent) else if (isNotMultiSelection && ui->btnChart->isChecked()) { impl->updateForGraphMode(ui->treeView); } + + QVector > events; + QVector, std::shared_ptr > > eventProducts; + impl->getSelectedItems(ui->treeView, events, eventProducts); + ui->btnRemove->setEnabled(!events.isEmpty() && eventProducts.isEmpty()); }); ui->treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);