diff --git a/core/include/DataSource/DataSourceItem.h b/core/include/DataSource/DataSourceItem.h new file mode 100644 index 0000000..2f71ec6 --- /dev/null +++ b/core/include/DataSource/DataSourceItem.h @@ -0,0 +1,52 @@ +#ifndef SCIQLOP_DATASOURCEITEM_H +#define SCIQLOP_DATASOURCEITEM_H + +#include + +#include +#include + +/** + * @brief The DataSourceItem class aims to represent a structure element of a data source. + * A data source has a tree structure that is made up of a main DataSourceItem object (root) + * containing other DataSourceItem objects (children). + * For each DataSourceItem can be associated a set of data representing it. + */ +class DataSourceItem { +public: + explicit DataSourceItem(QVector data = {}); + + /** + * Adds a child to the item. The item takes ownership of the child. + * @param child the child to add + */ + void appendChild(std::unique_ptr child) noexcept; + + /** + * Returns the item's child associated to an index + * @param childIndex the index to search + * @return a pointer to the child if index is valid, nullptr otherwise + */ + DataSourceItem *child(int childIndex) const noexcept; + + 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 + */ + QVariant data(int dataIndex) const noexcept; + + /** + * Get the item's parent + * @return a pointer to the parent if it exists, nullptr if the item is a root + */ + DataSourceItem *parentItem() const noexcept; + +private: + class DataSourceItemPrivate; + spimpl::unique_impl_ptr impl; +}; + +#endif // SCIQLOP_DATASOURCEITEMMODEL_H diff --git a/core/src/DataSource/DataSourceItem.cpp b/core/src/DataSource/DataSourceItem.cpp new file mode 100644 index 0000000..2d7e242 --- /dev/null +++ b/core/src/DataSource/DataSourceItem.cpp @@ -0,0 +1,50 @@ +#include + +#include + +struct DataSourceItem::DataSourceItemPrivate { + explicit DataSourceItemPrivate(QVector data) + : m_Parent{nullptr}, m_Children{}, m_Data{std::move(data)} + { + } + + DataSourceItem *m_Parent; + std::vector > m_Children; + QVector m_Data; +}; + +DataSourceItem::DataSourceItem(QVector data) + : impl{spimpl::make_unique_impl(data)} +{ +} + +void DataSourceItem::appendChild(std::unique_ptr child) noexcept +{ + child->impl->m_Parent = this; + impl->m_Children.push_back(std::move(child)); +} + +DataSourceItem *DataSourceItem::child(int childIndex) const noexcept +{ + if (childIndex < 0 || childIndex >= childCount()) { + return nullptr; + } + else { + return impl->m_Children.at(childIndex).get(); + } +} + +int DataSourceItem::childCount() const noexcept +{ + return impl->m_Children.size(); +} + +QVariant DataSourceItem::data(int dataIndex) const noexcept +{ + return impl->m_Data.value(dataIndex); +} + +DataSourceItem *DataSourceItem::parentItem() const noexcept +{ + return impl->m_Parent; +}