CatalogueActionManager.cpp
174 lines
| 6.8 KiB
| text/x-c
|
CppLexer
r1163 | #include "Catalogue/CatalogueActionManager.h" | |||
#include <Actions/ActionsGuiController.h> | ||||
#include <Catalogue/CatalogueController.h> | ||||
r1232 | #include <DataSource/DataSourceItem.h> | |||
r1163 | #include <SqpApplication.h> | |||
#include <Variable/Variable.h> | ||||
#include <Visualization/VisualizationGraphWidget.h> | ||||
#include <Visualization/VisualizationSelectionZoneItem.h> | ||||
r1231 | #include <Catalogue/CatalogueEventsWidget.h> | |||
#include <Catalogue/CatalogueExplorer.h> | ||||
#include <Catalogue/CatalogueSideBarWidget.h> | ||||
r1163 | ||||
r1235 | #include <CatalogueDao.h> | |||
r1163 | #include <DBCatalogue.h> | |||
#include <DBEvent.h> | ||||
#include <DBEventProduct.h> | ||||
#include <QBoxLayout> | ||||
#include <QComboBox> | ||||
#include <QDialog> | ||||
#include <QDialogButtonBox> | ||||
#include <QLineEdit> | ||||
#include <memory> | ||||
r1326 | const auto CATALOGUE_MENU_NAME = QObject::tr("Catalogues"); | |||
r1327 | const auto CATALOGUE_CREATE_EVENT_MENU_NAME = QObject::tr("New Event..."); | |||
r1326 | ||||
r1327 | const auto DEFAULT_EVENT_NAME = QObject::tr("Event"); | |||
const auto DEFAULT_CATALOGUE_NAME = QObject::tr("Catalogue"); | ||||
r1326 | ||||
r1163 | struct CatalogueActionManager::CatalogueActionManagerPrivate { | |||
r1231 | ||||
CatalogueExplorer *m_CatalogueExplorer = nullptr; | ||||
r1327 | QVector<std::shared_ptr<SelectionZoneAction> > m_CreateInCatalogueActions; | |||
r1231 | ||||
CatalogueActionManagerPrivate(CatalogueExplorer *catalogueExplorer) | ||||
: m_CatalogueExplorer(catalogueExplorer) | ||||
{ | ||||
} | ||||
r1163 | void createEventFromZones(const QString &eventName, | |||
const QVector<VisualizationSelectionZoneItem *> &zones, | ||||
const std::shared_ptr<DBCatalogue> &catalogue = nullptr) | ||||
{ | ||||
auto event = std::make_shared<DBEvent>(); | ||||
event->setName(eventName); | ||||
std::list<DBEventProduct> productList; | ||||
for (auto zone : zones) { | ||||
auto graph = zone->parentGraphWidget(); | ||||
for (auto var : graph->variables()) { | ||||
auto eventProduct = std::make_shared<DBEventProduct>(); | ||||
eventProduct->setEvent(*event); | ||||
r1294 | auto productId | |||
= var->metadata().value(DataSourceItem::ID_DATA_KEY, "UnknownID").toString(); | ||||
r1163 | auto zoneRange = zone->range(); | |||
eventProduct->setTStart(zoneRange.m_TStart); | ||||
eventProduct->setTEnd(zoneRange.m_TEnd); | ||||
r1294 | eventProduct->setProductId(productId); | |||
r1163 | ||||
productList.push_back(*eventProduct); | ||||
} | ||||
} | ||||
event->setEventProducts(productList); | ||||
r1164 | sqpApp->catalogueController().addEvent(event); | |||
r1231 | ||||
r1163 | if (catalogue) { | |||
r1302 | catalogue->addEvent(event->getUniqId()); | |||
sqpApp->catalogueController().updateCatalogue(catalogue); | ||||
r1231 | m_CatalogueExplorer->sideBarWidget().setCatalogueChanges(catalogue, true); | |||
if (m_CatalogueExplorer->eventsWidget().displayedCatalogues().contains(catalogue)) { | ||||
m_CatalogueExplorer->eventsWidget().addEvent(event); | ||||
m_CatalogueExplorer->eventsWidget().setEventChanges(event, true); | ||||
} | ||||
} | ||||
else if (m_CatalogueExplorer->eventsWidget().isAllEventsDisplayed()) { | ||||
m_CatalogueExplorer->eventsWidget().addEvent(event); | ||||
m_CatalogueExplorer->eventsWidget().setEventChanges(event, true); | ||||
r1163 | } | |||
} | ||||
r1327 | ||||
SelectionZoneAction::EnableFunction createEventEnableFuntion() const | ||||
{ | ||||
return [](auto zones) { | ||||
// Checks that all variables in the zones doesn't refer to the same product | ||||
QSet<QString> usedDatasource; | ||||
for (auto zone : zones) { | ||||
auto graph = zone->parentGraphWidget(); | ||||
auto variables = graph->variables(); | ||||
for (auto var : variables) { | ||||
auto datasourceId | ||||
= var->metadata().value(DataSourceItem::ID_DATA_KEY).toString(); | ||||
if (!usedDatasource.contains(datasourceId)) { | ||||
usedDatasource.insert(datasourceId); | ||||
} | ||||
else { | ||||
return false; | ||||
} | ||||
} | ||||
} | ||||
return true; | ||||
}; | ||||
} | ||||
r1163 | }; | |||
r1231 | CatalogueActionManager::CatalogueActionManager(CatalogueExplorer *catalogueExplorer) | |||
: impl{spimpl::make_unique_impl<CatalogueActionManagerPrivate>(catalogueExplorer)} | ||||
r1163 | { | |||
} | ||||
void CatalogueActionManager::installSelectionZoneActions() | ||||
{ | ||||
auto &actionController = sqpApp->actionsGuiController(); | ||||
auto createEventAction = actionController.addSectionZoneAction( | ||||
r1326 | {CATALOGUE_MENU_NAME, CATALOGUE_CREATE_EVENT_MENU_NAME}, QObject::tr("Without Catalogue"), | |||
[this](auto zones) { impl->createEventFromZones(DEFAULT_EVENT_NAME, zones); }); | ||||
r1327 | createEventAction->setEnableFunction(impl->createEventEnableFuntion()); | |||
r1328 | createEventAction->setAllowedFiltering(false); | |||
r1163 | ||||
r1326 | auto createEventInNewCatalogueAction = actionController.addSectionZoneAction( | |||
{CATALOGUE_MENU_NAME, CATALOGUE_CREATE_EVENT_MENU_NAME}, QObject::tr("In New Catalogue"), | ||||
[this](auto zones) { | ||||
r1163 | ||||
r1326 | auto newCatalogue = std::make_shared<DBCatalogue>(); | |||
newCatalogue->setName(DEFAULT_CATALOGUE_NAME); | ||||
sqpApp->catalogueController().addCatalogue(newCatalogue); | ||||
impl->m_CatalogueExplorer->sideBarWidget().addCatalogue(newCatalogue, | ||||
REPOSITORY_DEFAULT); | ||||
impl->createEventFromZones(DEFAULT_EVENT_NAME, zones, newCatalogue); | ||||
r1163 | }); | |||
r1327 | createEventInNewCatalogueAction->setEnableFunction(impl->createEventEnableFuntion()); | |||
r1328 | createEventInNewCatalogueAction->setAllowedFiltering(false); | |||
r1327 | ||||
refreshCreateInCatalogueAction(); | ||||
r1328 | ||||
actionController.addFilterForMenu({CATALOGUE_MENU_NAME, CATALOGUE_CREATE_EVENT_MENU_NAME}); | ||||
r1327 | } | |||
void CatalogueActionManager::refreshCreateInCatalogueAction() | ||||
{ | ||||
auto &actionController = sqpApp->actionsGuiController(); | ||||
r1326 | ||||
r1327 | for (auto action : impl->m_CreateInCatalogueActions) { | |||
actionController.removeAction(action); | ||||
} | ||||
impl->m_CreateInCatalogueActions.clear(); | ||||
r1326 | ||||
auto allCatalogues | ||||
= impl->m_CatalogueExplorer->sideBarWidget().getCatalogues(REPOSITORY_DEFAULT); | ||||
r1327 | ||||
r1326 | for (auto catalogue : allCatalogues) { | |||
auto catalogueName = catalogue->getName(); | ||||
auto createEventInCatalogueAction = actionController.addSectionZoneAction( | ||||
{CATALOGUE_MENU_NAME, CATALOGUE_CREATE_EVENT_MENU_NAME}, | ||||
r1327 | QObject::tr("In \"").append(catalogueName).append("\""), [this, catalogue](auto zones) { | |||
r1326 | impl->createEventFromZones(DEFAULT_EVENT_NAME, zones, catalogue); | |||
}); | ||||
r1327 | createEventInCatalogueAction->setEnableFunction(impl->createEventEnableFuntion()); | |||
impl->m_CreateInCatalogueActions << createEventInCatalogueAction; | ||||
r1326 | } | |||
r1163 | } | |||