AmdaPlugin.cpp
83 lines
| 2.9 KiB
| text/x-c
|
CppLexer
|
r325 | #include "AmdaPlugin.h" | ||
|
r384 | #include "AmdaDefs.h" | ||
|
r327 | #include "AmdaParser.h" | ||
|
r352 | #include "AmdaProvider.h" | ||
|
r325 | |||
#include <DataSource/DataSourceController.h> | ||||
|
r327 | #include <DataSource/DataSourceItem.h> | ||
|
r352 | #include <DataSource/DataSourceItemAction.h> | ||
|
r325 | |||
#include <SqpApplication.h> | ||||
Q_LOGGING_CATEGORY(LOG_AmdaPlugin, "AmdaPlugin") | ||||
namespace { | ||||
/// Name of the data source | ||||
const auto DATA_SOURCE_NAME = QStringLiteral("AMDA"); | ||||
|
r326 | /// Path of the file used to generate the data source item for AMDA | ||
|
r481 | const auto JSON_FILE_PATH = QStringLiteral(":/samples/AmdaSampleV2.json"); | ||
|
r326 | |||
|
r352 | void associateActions(DataSourceItem &item, const QUuid &dataSourceUid) | ||
{ | ||||
|
r381 | auto addLoadAction = [&item, dataSourceUid](const QString &label) { | ||
item.addAction( | ||||
std::make_unique<DataSourceItemAction>(label, [dataSourceUid](DataSourceItem &item) { | ||||
|
r352 | if (auto app = sqpApp) { | ||
app->dataSourceController().loadProductItem(dataSourceUid, item); | ||||
} | ||||
})); | ||||
|
r381 | }; | ||
const auto itemType = item.type(); | ||||
if (itemType == DataSourceItemType::PRODUCT) { | ||||
|
r384 | /// @todo : As for the moment we do not manage the loading of vectors, in the case of a | ||
/// parameter, we update the identifier of download of the data: | ||||
/// - if the parameter has no component, the identifier remains the same | ||||
/// - if the parameter has at least one component, the identifier is that of the first | ||||
/// component (for example, "imf" becomes "imf (0)") | ||||
if (item.childCount() != 0) { | ||||
item.setData(AMDA_XML_ID_KEY, item.child(0)->data(AMDA_XML_ID_KEY)); | ||||
} | ||||
|
r381 | addLoadAction(QObject::tr("Load %1 product").arg(item.name())); | ||
} | ||||
else if (itemType == DataSourceItemType::COMPONENT) { | ||||
addLoadAction(QObject::tr("Load %1 component").arg(item.name())); | ||||
|
r352 | } | ||
auto count = item.childCount(); | ||||
for (auto i = 0; i < count; ++i) { | ||||
if (auto child = item.child(i)) { | ||||
associateActions(*child, dataSourceUid); | ||||
} | ||||
} | ||||
} | ||||
|
r325 | } // 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 | ||||
|
r327 | if (auto dataSourceItem = AmdaParser::readJson(JSON_FILE_PATH)) { | ||
|
r352 | associateActions(*dataSourceItem, dataSourceUid); | ||
|
r327 | dataSourceController.setDataSourceItem(dataSourceUid, std::move(dataSourceItem)); | ||
} | ||||
else { | ||||
qCCritical(LOG_AmdaPlugin()) << tr("No data source item could be generated for AMDA"); | ||||
} | ||||
|
r348 | |||
// Sets data provider | ||||
dataSourceController.setDataProvider(dataSourceUid, std::make_unique<AmdaProvider>()); | ||||
|
r325 | } | ||
else { | ||||
qCWarning(LOG_AmdaPlugin()) << tr("Can't access to SciQlop application"); | ||||
} | ||||
} | ||||