diff --git a/core/include/DataSource/DataSourceItem.h b/core/include/DataSource/DataSourceItem.h index 4ff7629..bffa393 100644 --- a/core/include/DataSource/DataSourceItem.h +++ b/core/include/DataSource/DataSourceItem.h @@ -21,7 +21,11 @@ enum class DataSourceItemType { NODE, PRODUCT }; */ class DataSourceItem { public: - explicit DataSourceItem(DataSourceItemType type, QVector data = {}); + /// Key associated with the name of the item + static const QString NAME_DATA_KEY; + + explicit DataSourceItem(DataSourceItemType type, const QString &name); + explicit DataSourceItem(DataSourceItemType type, QHash data = {}); /// @return the actions of the item as a vector QVector actions() const noexcept; @@ -49,11 +53,11 @@ public: int childCount() const noexcept; /** - * Get the data associated to an index - * @param dataIndex the index to search - * @return the data found if index is valid, default QVariant otherwise + * Get the data associated to a key + * @param key the key to search + * @return the data found if key is valid, default QVariant otherwise */ - QVariant data(int dataIndex) const noexcept; + QVariant data(const QString &key) const noexcept; QString name() const noexcept; diff --git a/core/src/DataSource/DataSourceItem.cpp b/core/src/DataSource/DataSourceItem.cpp index 16e0c53..0b5c182 100644 --- a/core/src/DataSource/DataSourceItem.cpp +++ b/core/src/DataSource/DataSourceItem.cpp @@ -3,15 +3,10 @@ #include -namespace { - -/// Index of the 'name' value in the item -const auto NAME_INDEX = 0; - -} // namespace +const QString DataSourceItem::NAME_DATA_KEY = QStringLiteral("name"); struct DataSourceItem::DataSourceItemPrivate { - explicit DataSourceItemPrivate(DataSourceItemType type, QVector data) + explicit DataSourceItemPrivate(DataSourceItemType type, QHash data) : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)}, m_Actions{} { } @@ -19,11 +14,16 @@ struct DataSourceItem::DataSourceItemPrivate { DataSourceItem *m_Parent; std::vector > m_Children; DataSourceItemType m_Type; - QVector m_Data; + QHash m_Data; std::vector > m_Actions; }; -DataSourceItem::DataSourceItem(DataSourceItemType type, QVector data) +DataSourceItem::DataSourceItem(DataSourceItemType type, const QString &name) + : DataSourceItem{type, QHash{{NAME_DATA_KEY, name}}} +{ +} + +DataSourceItem::DataSourceItem(DataSourceItemType type, QHash data) : impl{spimpl::make_unique_impl(type, std::move(data))} { } @@ -65,14 +65,14 @@ int DataSourceItem::childCount() const noexcept return impl->m_Children.size(); } -QVariant DataSourceItem::data(int dataIndex) const noexcept +QVariant DataSourceItem::data(const QString &key) const noexcept { - return impl->m_Data.value(dataIndex); + return impl->m_Data.value(key); } QString DataSourceItem::name() const noexcept { - return data(NAME_INDEX).toString(); + return data(NAME_DATA_KEY).toString(); } DataSourceItem *DataSourceItem::parentItem() const noexcept