diff --git a/core/include/DataSource/DataSourceItem.h b/core/include/DataSource/DataSourceItem.h index 58bd71e..be60242 100644 --- a/core/include/DataSource/DataSourceItem.h +++ b/core/include/DataSource/DataSourceItem.h @@ -72,6 +72,15 @@ public: */ DataSourceItem *parentItem() const noexcept; + /** + * Sets or appends a value to a key + * @param key the key + * @param value the value + * @param append if true, the value is added to the values already existing for the key, + * otherwise it replaces the existing values + */ + void setData(const QString &key, const QVariant &value, bool append = false) noexcept; + DataSourceItemType type() const noexcept; private: diff --git a/core/src/DataSource/DataSourceItem.cpp b/core/src/DataSource/DataSourceItem.cpp index 7f52669..19b1ae2 100644 --- a/core/src/DataSource/DataSourceItem.cpp +++ b/core/src/DataSource/DataSourceItem.cpp @@ -90,6 +90,29 @@ DataSourceItem *DataSourceItem::parentItem() const noexcept return impl->m_Parent; } +void DataSourceItem::setData(const QString &key, const QVariant &value, bool append) noexcept +{ + auto it = impl->m_Data.constFind(key); + if (append && it != impl->m_Data.constEnd()) { + // Case of an existing value to which we want to add to the new value + if (it->canConvert()) { + auto variantList = it->value(); + variantList.append(value); + + impl->m_Data.insert(key, variantList); + } + else { + impl->m_Data.insert(key, QVariantList{*it, value}); + } + } + else { + // Other cases : + // - new value in map OR + // - replacement of an existing value (not appending) + impl->m_Data.insert(key, value); + } +} + DataSourceItemType DataSourceItem::type() const noexcept { return impl->m_Type;