AmdaPlugin.cpp
77 lines
| 2.5 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r352 | #include "AmdaPlugin.h" | ||
Alexandre Leroux
|
r417 | #include "AmdaDefs.h" | ||
Alexandre Leroux
|
r354 | #include "AmdaParser.h" | ||
Alexandre Leroux
|
r381 | #include "AmdaProvider.h" | ||
Alexandre Leroux
|
r352 | |||
#include <DataSource/DataSourceController.h> | ||||
Alexandre Leroux
|
r354 | #include <DataSource/DataSourceItem.h> | ||
Alexandre Leroux
|
r381 | #include <DataSource/DataSourceItemAction.h> | ||
Alexandre Leroux
|
r352 | |||
#include <SqpApplication.h> | ||||
Q_LOGGING_CATEGORY(LOG_AmdaPlugin, "AmdaPlugin") | ||||
namespace { | ||||
/// Name of the data source | ||||
const auto DATA_SOURCE_NAME = QStringLiteral("AMDA"); | ||||
Alexandre Leroux
|
r353 | /// Path of the file used to generate the data source item for AMDA | ||
Alexandre Leroux
|
r942 | const auto JSON_FILE_PATH = QStringLiteral(":/samples/AmdaSampleV3.json"); | ||
Alexandre Leroux
|
r353 | |||
Alexandre Leroux
|
r381 | void associateActions(DataSourceItem &item, const QUuid &dataSourceUid) | ||
{ | ||||
Alexandre Leroux
|
r414 | auto addLoadAction = [&item, dataSourceUid](const QString &label) { | ||
item.addAction( | ||||
std::make_unique<DataSourceItemAction>(label, [dataSourceUid](DataSourceItem &item) { | ||||
Alexandre Leroux
|
r381 | if (auto app = sqpApp) { | ||
app->dataSourceController().loadProductItem(dataSourceUid, item); | ||||
} | ||||
})); | ||||
Alexandre Leroux
|
r414 | }; | ||
const auto itemType = item.type(); | ||||
Alexandre Leroux
|
r1036 | if (itemType == DataSourceItemType::PRODUCT || itemType == DataSourceItemType::COMPONENT) { | ||
// Adds plugin name to item metadata | ||||
item.setData(DataSourceItem::PLUGIN_DATA_KEY, DATA_SOURCE_NAME); | ||||
// Adds load action | ||||
auto actionLabel = QObject::tr( | ||||
itemType == DataSourceItemType::PRODUCT ? "Load %1 product" : "Load %1 component"); | ||||
addLoadAction(actionLabel.arg(item.name())); | ||||
Alexandre Leroux
|
r381 | } | ||
auto count = item.childCount(); | ||||
for (auto i = 0; i < count; ++i) { | ||||
if (auto child = item.child(i)) { | ||||
associateActions(*child, dataSourceUid); | ||||
} | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r352 | } // namespace | ||
void AmdaPlugin::initialize() | ||||
{ | ||||
if (auto app = sqpApp) { | ||||
// Registers to the data source controller | ||||
auto &dataSourceController = app->dataSourceController(); | ||||
auto dataSourceUid = dataSourceController.registerDataSource(DATA_SOURCE_NAME); | ||||
// Sets data source tree | ||||
Alexandre Leroux
|
r354 | if (auto dataSourceItem = AmdaParser::readJson(JSON_FILE_PATH)) { | ||
Alexandre Leroux
|
r381 | associateActions(*dataSourceItem, dataSourceUid); | ||
Alexandre Leroux
|
r354 | dataSourceController.setDataSourceItem(dataSourceUid, std::move(dataSourceItem)); | ||
} | ||||
else { | ||||
qCCritical(LOG_AmdaPlugin()) << tr("No data source item could be generated for AMDA"); | ||||
} | ||||
Alexandre Leroux
|
r377 | |||
// Sets data provider | ||||
dataSourceController.setDataProvider(dataSourceUid, std::make_unique<AmdaProvider>()); | ||||
Alexandre Leroux
|
r352 | } | ||
else { | ||||
qCWarning(LOG_AmdaPlugin()) << tr("Can't access to SciQlop application"); | ||||
} | ||||
} | ||||