@@ -1,73 +1,77 | |||
|
1 | 1 | #ifndef SCIQLOP_DATASOURCEITEM_H |
|
2 | 2 | #define SCIQLOP_DATASOURCEITEM_H |
|
3 | 3 | |
|
4 | 4 | #include <Common/spimpl.h> |
|
5 | 5 | |
|
6 | 6 | #include <QVariant> |
|
7 | 7 | #include <QVector> |
|
8 | 8 | |
|
9 | 9 | class DataSourceItemAction; |
|
10 | 10 | |
|
11 | 11 | /** |
|
12 | 12 | * Possible types of an item |
|
13 | 13 | */ |
|
14 | 14 | enum class DataSourceItemType { NODE, PRODUCT }; |
|
15 | 15 | |
|
16 | 16 | /** |
|
17 | 17 | * @brief The DataSourceItem class aims to represent a structure element of a data source. |
|
18 | 18 | * A data source has a tree structure that is made up of a main DataSourceItem object (root) |
|
19 | 19 | * containing other DataSourceItem objects (children). |
|
20 | 20 | * For each DataSourceItem can be associated a set of data representing it. |
|
21 | 21 | */ |
|
22 | 22 | class DataSourceItem { |
|
23 | 23 | public: |
|
24 | explicit DataSourceItem(DataSourceItemType type, QVector<QVariant> data = {}); | |
|
24 | /// Key associated with the name of the item | |
|
25 | static const QString NAME_DATA_KEY; | |
|
26 | ||
|
27 | explicit DataSourceItem(DataSourceItemType type, const QString &name); | |
|
28 | explicit DataSourceItem(DataSourceItemType type, QHash<QString, QVariant> data = {}); | |
|
25 | 29 | |
|
26 | 30 | /// @return the actions of the item as a vector |
|
27 | 31 | QVector<DataSourceItemAction *> actions() const noexcept; |
|
28 | 32 | |
|
29 | 33 | /** |
|
30 | 34 | * Adds an action to the item. The item takes ownership of the action, and the action is |
|
31 | 35 | * automatically associated to the item |
|
32 | 36 | * @param action the action to add |
|
33 | 37 | */ |
|
34 | 38 | void addAction(std::unique_ptr<DataSourceItemAction> action) noexcept; |
|
35 | 39 | |
|
36 | 40 | /** |
|
37 | 41 | * Adds a child to the item. The item takes ownership of the child. |
|
38 | 42 | * @param child the child to add |
|
39 | 43 | */ |
|
40 | 44 | void appendChild(std::unique_ptr<DataSourceItem> child) noexcept; |
|
41 | 45 | |
|
42 | 46 | /** |
|
43 | 47 | * Returns the item's child associated to an index |
|
44 | 48 | * @param childIndex the index to search |
|
45 | 49 | * @return a pointer to the child if index is valid, nullptr otherwise |
|
46 | 50 | */ |
|
47 | 51 | DataSourceItem *child(int childIndex) const noexcept; |
|
48 | 52 | |
|
49 | 53 | int childCount() const noexcept; |
|
50 | 54 | |
|
51 | 55 | /** |
|
52 |
* Get the data associated to a |
|
|
53 |
* @param |
|
|
54 |
* @return the data found if |
|
|
56 | * Get the data associated to a key | |
|
57 | * @param key the key to search | |
|
58 | * @return the data found if key is valid, default QVariant otherwise | |
|
55 | 59 | */ |
|
56 |
QVariant data( |
|
|
60 | QVariant data(const QString &key) const noexcept; | |
|
57 | 61 | |
|
58 | 62 | QString name() const noexcept; |
|
59 | 63 | |
|
60 | 64 | /** |
|
61 | 65 | * Get the item's parent |
|
62 | 66 | * @return a pointer to the parent if it exists, nullptr if the item is a root |
|
63 | 67 | */ |
|
64 | 68 | DataSourceItem *parentItem() const noexcept; |
|
65 | 69 | |
|
66 | 70 | DataSourceItemType type() const noexcept; |
|
67 | 71 | |
|
68 | 72 | private: |
|
69 | 73 | class DataSourceItemPrivate; |
|
70 | 74 | spimpl::unique_impl_ptr<DataSourceItemPrivate> impl; |
|
71 | 75 | }; |
|
72 | 76 | |
|
73 | 77 | #endif // SCIQLOP_DATASOURCEITEMMODEL_H |
@@ -1,86 +1,86 | |||
|
1 | 1 | #include <DataSource/DataSourceItem.h> |
|
2 | 2 | #include <DataSource/DataSourceItemAction.h> |
|
3 | 3 | |
|
4 | 4 | #include <QVector> |
|
5 | 5 | |
|
6 | namespace { | |
|
7 | ||
|
8 | /// Index of the 'name' value in the item | |
|
9 | const auto NAME_INDEX = 0; | |
|
10 | ||
|
11 | } // namespace | |
|
6 | const QString DataSourceItem::NAME_DATA_KEY = QStringLiteral("name"); | |
|
12 | 7 | |
|
13 | 8 | struct DataSourceItem::DataSourceItemPrivate { |
|
14 |
explicit DataSourceItemPrivate(DataSourceItemType type, Q |
|
|
9 | explicit DataSourceItemPrivate(DataSourceItemType type, QHash<QString, QVariant> data) | |
|
15 | 10 | : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)}, m_Actions{} |
|
16 | 11 | { |
|
17 | 12 | } |
|
18 | 13 | |
|
19 | 14 | DataSourceItem *m_Parent; |
|
20 | 15 | std::vector<std::unique_ptr<DataSourceItem> > m_Children; |
|
21 | 16 | DataSourceItemType m_Type; |
|
22 |
Q |
|
|
17 | QHash<QString, QVariant> m_Data; | |
|
23 | 18 | std::vector<std::unique_ptr<DataSourceItemAction> > m_Actions; |
|
24 | 19 | }; |
|
25 | 20 | |
|
26 |
DataSourceItem::DataSourceItem(DataSourceItemType type, |
|
|
21 | DataSourceItem::DataSourceItem(DataSourceItemType type, const QString &name) | |
|
22 | : DataSourceItem{type, QHash<QString, QVariant>{{NAME_DATA_KEY, name}}} | |
|
23 | { | |
|
24 | } | |
|
25 | ||
|
26 | DataSourceItem::DataSourceItem(DataSourceItemType type, QHash<QString, QVariant> data) | |
|
27 | 27 | : impl{spimpl::make_unique_impl<DataSourceItemPrivate>(type, std::move(data))} |
|
28 | 28 | { |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | QVector<DataSourceItemAction *> DataSourceItem::actions() const noexcept |
|
32 | 32 | { |
|
33 | 33 | auto result = QVector<DataSourceItemAction *>{}; |
|
34 | 34 | |
|
35 | 35 | std::transform(std::cbegin(impl->m_Actions), std::cend(impl->m_Actions), |
|
36 | 36 | std::back_inserter(result), [](const auto &action) { return action.get(); }); |
|
37 | 37 | |
|
38 | 38 | return result; |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | void DataSourceItem::addAction(std::unique_ptr<DataSourceItemAction> action) noexcept |
|
42 | 42 | { |
|
43 | 43 | action->setDataSourceItem(this); |
|
44 | 44 | impl->m_Actions.push_back(std::move(action)); |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | void DataSourceItem::appendChild(std::unique_ptr<DataSourceItem> child) noexcept |
|
48 | 48 | { |
|
49 | 49 | child->impl->m_Parent = this; |
|
50 | 50 | impl->m_Children.push_back(std::move(child)); |
|
51 | 51 | } |
|
52 | 52 | |
|
53 | 53 | DataSourceItem *DataSourceItem::child(int childIndex) const noexcept |
|
54 | 54 | { |
|
55 | 55 | if (childIndex < 0 || childIndex >= childCount()) { |
|
56 | 56 | return nullptr; |
|
57 | 57 | } |
|
58 | 58 | else { |
|
59 | 59 | return impl->m_Children.at(childIndex).get(); |
|
60 | 60 | } |
|
61 | 61 | } |
|
62 | 62 | |
|
63 | 63 | int DataSourceItem::childCount() const noexcept |
|
64 | 64 | { |
|
65 | 65 | return impl->m_Children.size(); |
|
66 | 66 | } |
|
67 | 67 | |
|
68 |
QVariant DataSourceItem::data( |
|
|
68 | QVariant DataSourceItem::data(const QString &key) const noexcept | |
|
69 | 69 | { |
|
70 |
return impl->m_Data.value( |
|
|
70 | return impl->m_Data.value(key); | |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | QString DataSourceItem::name() const noexcept |
|
74 | 74 | { |
|
75 |
return data(NAME_ |
|
|
75 | return data(NAME_DATA_KEY).toString(); | |
|
76 | 76 | } |
|
77 | 77 | |
|
78 | 78 | DataSourceItem *DataSourceItem::parentItem() const noexcept |
|
79 | 79 | { |
|
80 | 80 | return impl->m_Parent; |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | DataSourceItemType DataSourceItem::type() const noexcept |
|
84 | 84 | { |
|
85 | 85 | return impl->m_Type; |
|
86 | 86 | } |
General Comments 0
You need to be logged in to leave comments.
Login now