diff --git a/core/include/DataSource/DataSourceController.h b/core/include/DataSource/DataSourceController.h index cbf2d67..9e9fcdb 100644 --- a/core/include/DataSource/DataSourceController.h +++ b/core/include/DataSource/DataSourceController.h @@ -75,6 +75,10 @@ public slots: void initialize(); void finalize(); + /// Request the creation of a variable from the ID_DATA_KEY of a product + void requestVariable(const QString &datasourceIdKey); + + /// Request the creation of a variable from metadata of a product void requestVariable(const QVariantHash &productData); signals: diff --git a/core/include/DataSource/DataSourceItem.h b/core/include/DataSource/DataSourceItem.h index b872ce6..a1edb5e 100644 --- a/core/include/DataSource/DataSourceItem.h +++ b/core/include/DataSource/DataSourceItem.h @@ -145,6 +145,14 @@ public: */ DataSourceItem *findItem(const QVariantHash &data, bool recursive); + /** + * @brief Searches the first child matching the specified \p ID_DATA_KEY in its metadata. + * @param id The id to search. + * @param recursive So the search recursively. + * @return the item matching the data or nullptr if it was not found. + */ + DataSourceItem *findItem(const QString &datasourceIdKey, bool recursive); + bool operator==(const DataSourceItem &other); bool operator!=(const DataSourceItem &other); diff --git a/core/src/DataSource/DataSourceController.cpp b/core/src/DataSource/DataSourceController.cpp index a7bc3d2..6532709 100644 --- a/core/src/DataSource/DataSourceController.cpp +++ b/core/src/DataSource/DataSourceController.cpp @@ -37,6 +37,20 @@ public: return sourceItem; } + + // Search for the first datasource item matching the specified ID_DATA_KEY + DataSourceItem *findDataSourceItem(const QString &datasourceIdKey) + { + DataSourceItem *sourceItem = nullptr; + for (const auto &item : m_DataSourceItems) { + sourceItem = item.second->findItem(datasourceIdKey, true); + if (sourceItem) { + break; + } + } + + return sourceItem; + } }; DataSourceController::DataSourceController(QObject *parent) @@ -149,6 +163,20 @@ void DataSourceController::finalize() impl->m_WorkingMutex.unlock(); } +void DataSourceController::requestVariable(const QString &datasourceIdKey) +{ + auto sourceItem = impl->findDataSourceItem(datasourceIdKey); + + if (sourceItem) { + auto sourceName = sourceItem->rootItem().name(); + auto sourceId = impl->m_DataSources.key(sourceName); + loadProductItem(sourceId, *sourceItem); + } + else { + qCWarning(LOG_DataSourceController()) << tr("requestVariable, product data not found"); + } +} + void DataSourceController::requestVariable(const QVariantHash &productData) { auto sourceItem = impl->findDataSourceItem(productData); diff --git a/core/src/DataSource/DataSourceItem.cpp b/core/src/DataSource/DataSourceItem.cpp index 86730d3..3d59332 100644 --- a/core/src/DataSource/DataSourceItem.cpp +++ b/core/src/DataSource/DataSourceItem.cpp @@ -165,6 +165,24 @@ DataSourceItem *DataSourceItem::findItem(const QVariantHash &data, bool recursiv return nullptr; } +DataSourceItem *DataSourceItem::findItem(const QString &datasourceIdKey, bool recursive) +{ + for (const auto &child : impl->m_Children) { + auto childId = child->impl->m_Data.value(ID_DATA_KEY); + if (childId == datasourceIdKey) { + return child.get(); + } + + if (recursive) { + if (auto foundItem = child->findItem(datasourceIdKey, true)) { + return foundItem; + } + } + } + + return nullptr; +} + bool DataSourceItem::operator==(const DataSourceItem &other) { // Compares items' attributes