@@ -0,0 +1,52 | |||
|
1 | #ifndef SCIQLOP_DATASOURCEITEM_H | |
|
2 | #define SCIQLOP_DATASOURCEITEM_H | |
|
3 | ||
|
4 | #include <Common/spimpl.h> | |
|
5 | ||
|
6 | #include <QVariant> | |
|
7 | #include <QVector> | |
|
8 | ||
|
9 | /** | |
|
10 | * @brief The DataSourceItem class aims to represent a structure element of a data source. | |
|
11 | * A data source has a tree structure that is made up of a main DataSourceItem object (root) | |
|
12 | * containing other DataSourceItem objects (children). | |
|
13 | * For each DataSourceItem can be associated a set of data representing it. | |
|
14 | */ | |
|
15 | class DataSourceItem { | |
|
16 | public: | |
|
17 | explicit DataSourceItem(QVector<QVariant> data = {}); | |
|
18 | ||
|
19 | /** | |
|
20 | * Adds a child to the item. The item takes ownership of the child. | |
|
21 | * @param child the child to add | |
|
22 | */ | |
|
23 | void appendChild(std::unique_ptr<DataSourceItem> child) noexcept; | |
|
24 | ||
|
25 | /** | |
|
26 | * Returns the item's child associated to an index | |
|
27 | * @param childIndex the index to search | |
|
28 | * @return a pointer to the child if index is valid, nullptr otherwise | |
|
29 | */ | |
|
30 | DataSourceItem *child(int childIndex) const noexcept; | |
|
31 | ||
|
32 | int childCount() const noexcept; | |
|
33 | ||
|
34 | /** | |
|
35 | * Get the data associated to an index | |
|
36 | * @param dataIndex the index to search | |
|
37 | * @return the data found if index is valid, default QVariant otherwise | |
|
38 | */ | |
|
39 | QVariant data(int dataIndex) const noexcept; | |
|
40 | ||
|
41 | /** | |
|
42 | * Get the item's parent | |
|
43 | * @return a pointer to the parent if it exists, nullptr if the item is a root | |
|
44 | */ | |
|
45 | DataSourceItem *parentItem() const noexcept; | |
|
46 | ||
|
47 | private: | |
|
48 | class DataSourceItemPrivate; | |
|
49 | spimpl::unique_impl_ptr<DataSourceItemPrivate> impl; | |
|
50 | }; | |
|
51 | ||
|
52 | #endif // SCIQLOP_DATASOURCEITEMMODEL_H |
@@ -0,0 +1,50 | |||
|
1 | #include <DataSource/DataSourceItem.h> | |
|
2 | ||
|
3 | #include <QVector> | |
|
4 | ||
|
5 | struct DataSourceItem::DataSourceItemPrivate { | |
|
6 | explicit DataSourceItemPrivate(QVector<QVariant> data) | |
|
7 | : m_Parent{nullptr}, m_Children{}, m_Data{std::move(data)} | |
|
8 | { | |
|
9 | } | |
|
10 | ||
|
11 | DataSourceItem *m_Parent; | |
|
12 | std::vector<std::unique_ptr<DataSourceItem> > m_Children; | |
|
13 | QVector<QVariant> m_Data; | |
|
14 | }; | |
|
15 | ||
|
16 | DataSourceItem::DataSourceItem(QVector<QVariant> data) | |
|
17 | : impl{spimpl::make_unique_impl<DataSourceItemPrivate>(data)} | |
|
18 | { | |
|
19 | } | |
|
20 | ||
|
21 | void DataSourceItem::appendChild(std::unique_ptr<DataSourceItem> child) noexcept | |
|
22 | { | |
|
23 | child->impl->m_Parent = this; | |
|
24 | impl->m_Children.push_back(std::move(child)); | |
|
25 | } | |
|
26 | ||
|
27 | DataSourceItem *DataSourceItem::child(int childIndex) const noexcept | |
|
28 | { | |
|
29 | if (childIndex < 0 || childIndex >= childCount()) { | |
|
30 | return nullptr; | |
|
31 | } | |
|
32 | else { | |
|
33 | return impl->m_Children.at(childIndex).get(); | |
|
34 | } | |
|
35 | } | |
|
36 | ||
|
37 | int DataSourceItem::childCount() const noexcept | |
|
38 | { | |
|
39 | return impl->m_Children.size(); | |
|
40 | } | |
|
41 | ||
|
42 | QVariant DataSourceItem::data(int dataIndex) const noexcept | |
|
43 | { | |
|
44 | return impl->m_Data.value(dataIndex); | |
|
45 | } | |
|
46 | ||
|
47 | DataSourceItem *DataSourceItem::parentItem() const noexcept | |
|
48 | { | |
|
49 | return impl->m_Parent; | |
|
50 | } |
General Comments 0
You need to be logged in to leave comments.
Login now