@@ -1,94 +1,94 | |||||
1 | #ifndef SCIQLOP_DATASOURCEITEM_H |
|
1 | #ifndef SCIQLOP_DATASOURCEITEM_H | |
2 | #define SCIQLOP_DATASOURCEITEM_H |
|
2 | #define SCIQLOP_DATASOURCEITEM_H | |
3 |
|
3 | |||
4 | #include <Common/spimpl.h> |
|
4 | #include <Common/spimpl.h> | |
5 |
|
5 | |||
6 | #include <QVariant> |
|
6 | #include <QVariant> | |
7 | #include <QVector> |
|
7 | #include <QVector> | |
8 |
|
8 | |||
9 | class DataSourceItemAction; |
|
9 | class DataSourceItemAction; | |
10 |
|
10 | |||
11 | /** |
|
11 | /** | |
12 | * Possible types of an item |
|
12 | * Possible types of an item | |
13 | */ |
|
13 | */ | |
14 | enum class DataSourceItemType { NODE, PRODUCT, COMPONENT }; |
|
14 | enum class DataSourceItemType { NODE, PRODUCT, COMPONENT }; | |
15 |
|
15 | |||
16 | /** |
|
16 | /** | |
17 | * @brief The DataSourceItem class aims to represent a structure element of a data source. |
|
17 | * @brief The DataSourceItem class aims to represent a structure element of a data source. | |
18 | * A data source has a tree structure that is made up of a main DataSourceItem object (root) |
|
18 | * A data source has a tree structure that is made up of a main DataSourceItem object (root) | |
19 | * containing other DataSourceItem objects (children). |
|
19 | * containing other DataSourceItem objects (children). | |
20 | * For each DataSourceItem can be associated a set of data representing it. |
|
20 | * For each DataSourceItem can be associated a set of data representing it. | |
21 | */ |
|
21 | */ | |
22 | class DataSourceItem { |
|
22 | class DataSourceItem { | |
23 | public: |
|
23 | public: | |
24 | /// Key associated with the name of the item |
|
24 | /// Key associated with the name of the item | |
25 | static const QString NAME_DATA_KEY; |
|
25 | static const QString NAME_DATA_KEY; | |
26 |
|
26 | |||
27 | explicit DataSourceItem(DataSourceItemType type, const QString &name); |
|
27 | explicit DataSourceItem(DataSourceItemType type, const QString &name); | |
28 |
explicit DataSourceItem(DataSourceItemType type, |
|
28 | explicit DataSourceItem(DataSourceItemType type, QVariantHash data = {}); | |
29 |
|
29 | |||
30 | /// @return the actions of the item as a vector |
|
30 | /// @return the actions of the item as a vector | |
31 | QVector<DataSourceItemAction *> actions() const noexcept; |
|
31 | QVector<DataSourceItemAction *> actions() const noexcept; | |
32 |
|
32 | |||
33 | /** |
|
33 | /** | |
34 | * Adds an action to the item. The item takes ownership of the action, and the action is |
|
34 | * Adds an action to the item. The item takes ownership of the action, and the action is | |
35 | * automatically associated to the item |
|
35 | * automatically associated to the item | |
36 | * @param action the action to add |
|
36 | * @param action the action to add | |
37 | */ |
|
37 | */ | |
38 | void addAction(std::unique_ptr<DataSourceItemAction> action) noexcept; |
|
38 | void addAction(std::unique_ptr<DataSourceItemAction> action) noexcept; | |
39 |
|
39 | |||
40 | /** |
|
40 | /** | |
41 | * Adds a child to the item. The item takes ownership of the child. |
|
41 | * Adds a child to the item. The item takes ownership of the child. | |
42 | * @param child the child to add |
|
42 | * @param child the child to add | |
43 | */ |
|
43 | */ | |
44 | void appendChild(std::unique_ptr<DataSourceItem> child) noexcept; |
|
44 | void appendChild(std::unique_ptr<DataSourceItem> child) noexcept; | |
45 |
|
45 | |||
46 | /** |
|
46 | /** | |
47 | * Returns the item's child associated to an index |
|
47 | * Returns the item's child associated to an index | |
48 | * @param childIndex the index to search |
|
48 | * @param childIndex the index to search | |
49 | * @return a pointer to the child if index is valid, nullptr otherwise |
|
49 | * @return a pointer to the child if index is valid, nullptr otherwise | |
50 | */ |
|
50 | */ | |
51 | DataSourceItem *child(int childIndex) const noexcept; |
|
51 | DataSourceItem *child(int childIndex) const noexcept; | |
52 |
|
52 | |||
53 | int childCount() const noexcept; |
|
53 | int childCount() const noexcept; | |
54 |
|
54 | |||
55 | /** |
|
55 | /** | |
56 | * Get the data associated to a key |
|
56 | * Get the data associated to a key | |
57 | * @param key the key to search |
|
57 | * @param key the key to search | |
58 | * @return the data found if key is valid, default QVariant otherwise |
|
58 | * @return the data found if key is valid, default QVariant otherwise | |
59 | */ |
|
59 | */ | |
60 | QVariant data(const QString &key) const noexcept; |
|
60 | QVariant data(const QString &key) const noexcept; | |
61 |
|
61 | |||
62 | /// Gets all data |
|
62 | /// Gets all data | |
63 |
|
|
63 | QVariantHash data() const noexcept; | |
64 |
|
64 | |||
65 | bool isRoot() const noexcept; |
|
65 | bool isRoot() const noexcept; | |
66 |
|
66 | |||
67 | QString name() const noexcept; |
|
67 | QString name() const noexcept; | |
68 |
|
68 | |||
69 | /** |
|
69 | /** | |
70 | * Get the item's parent |
|
70 | * Get the item's parent | |
71 | * @return a pointer to the parent if it exists, nullptr if the item is a root |
|
71 | * @return a pointer to the parent if it exists, nullptr if the item is a root | |
72 | */ |
|
72 | */ | |
73 | DataSourceItem *parentItem() const noexcept; |
|
73 | DataSourceItem *parentItem() const noexcept; | |
74 |
|
74 | |||
75 | /** |
|
75 | /** | |
76 | * Sets or appends a value to a key |
|
76 | * Sets or appends a value to a key | |
77 | * @param key the key |
|
77 | * @param key the key | |
78 | * @param value the value |
|
78 | * @param value the value | |
79 | * @param append if true, the value is added to the values already existing for the key, |
|
79 | * @param append if true, the value is added to the values already existing for the key, | |
80 | * otherwise it replaces the existing values |
|
80 | * otherwise it replaces the existing values | |
81 | */ |
|
81 | */ | |
82 | void setData(const QString &key, const QVariant &value, bool append = false) noexcept; |
|
82 | void setData(const QString &key, const QVariant &value, bool append = false) noexcept; | |
83 |
|
83 | |||
84 | DataSourceItemType type() const noexcept; |
|
84 | DataSourceItemType type() const noexcept; | |
85 |
|
85 | |||
86 | bool operator==(const DataSourceItem &other); |
|
86 | bool operator==(const DataSourceItem &other); | |
87 | bool operator!=(const DataSourceItem &other); |
|
87 | bool operator!=(const DataSourceItem &other); | |
88 |
|
88 | |||
89 | private: |
|
89 | private: | |
90 | class DataSourceItemPrivate; |
|
90 | class DataSourceItemPrivate; | |
91 | spimpl::unique_impl_ptr<DataSourceItemPrivate> impl; |
|
91 | spimpl::unique_impl_ptr<DataSourceItemPrivate> impl; | |
92 | }; |
|
92 | }; | |
93 |
|
93 | |||
94 | #endif // SCIQLOP_DATASOURCEITEMMODEL_H |
|
94 | #endif // SCIQLOP_DATASOURCEITEMMODEL_H |
@@ -1,140 +1,140 | |||||
1 | #include <DataSource/DataSourceItem.h> |
|
1 | #include <DataSource/DataSourceItem.h> | |
2 | #include <DataSource/DataSourceItemAction.h> |
|
2 | #include <DataSource/DataSourceItemAction.h> | |
3 |
|
3 | |||
4 | #include <QVector> |
|
4 | #include <QVector> | |
5 |
|
5 | |||
6 | const QString DataSourceItem::NAME_DATA_KEY = QStringLiteral("name"); |
|
6 | const QString DataSourceItem::NAME_DATA_KEY = QStringLiteral("name"); | |
7 |
|
7 | |||
8 | struct DataSourceItem::DataSourceItemPrivate { |
|
8 | struct DataSourceItem::DataSourceItemPrivate { | |
9 |
explicit DataSourceItemPrivate(DataSourceItemType type, |
|
9 | explicit DataSourceItemPrivate(DataSourceItemType type, QVariantHash data) | |
10 | : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)}, m_Actions{} |
|
10 | : m_Parent{nullptr}, m_Children{}, m_Type{type}, m_Data{std::move(data)}, m_Actions{} | |
11 | { |
|
11 | { | |
12 | } |
|
12 | } | |
13 |
|
13 | |||
14 | DataSourceItem *m_Parent; |
|
14 | DataSourceItem *m_Parent; | |
15 | std::vector<std::unique_ptr<DataSourceItem> > m_Children; |
|
15 | std::vector<std::unique_ptr<DataSourceItem> > m_Children; | |
16 | DataSourceItemType m_Type; |
|
16 | DataSourceItemType m_Type; | |
17 |
|
|
17 | QVariantHash m_Data; | |
18 | std::vector<std::unique_ptr<DataSourceItemAction> > m_Actions; |
|
18 | std::vector<std::unique_ptr<DataSourceItemAction> > m_Actions; | |
19 | }; |
|
19 | }; | |
20 |
|
20 | |||
21 | DataSourceItem::DataSourceItem(DataSourceItemType type, const QString &name) |
|
21 | DataSourceItem::DataSourceItem(DataSourceItemType type, const QString &name) | |
22 |
: DataSourceItem{type, |
|
22 | : DataSourceItem{type, QVariantHash{{NAME_DATA_KEY, name}}} | |
23 | { |
|
23 | { | |
24 | } |
|
24 | } | |
25 |
|
25 | |||
26 |
DataSourceItem::DataSourceItem(DataSourceItemType type, |
|
26 | DataSourceItem::DataSourceItem(DataSourceItemType type, QVariantHash data) | |
27 | : impl{spimpl::make_unique_impl<DataSourceItemPrivate>(type, std::move(data))} |
|
27 | : impl{spimpl::make_unique_impl<DataSourceItemPrivate>(type, std::move(data))} | |
28 | { |
|
28 | { | |
29 | } |
|
29 | } | |
30 |
|
30 | |||
31 | QVector<DataSourceItemAction *> DataSourceItem::actions() const noexcept |
|
31 | QVector<DataSourceItemAction *> DataSourceItem::actions() const noexcept | |
32 | { |
|
32 | { | |
33 | auto result = QVector<DataSourceItemAction *>{}; |
|
33 | auto result = QVector<DataSourceItemAction *>{}; | |
34 |
|
34 | |||
35 | std::transform(std::cbegin(impl->m_Actions), std::cend(impl->m_Actions), |
|
35 | std::transform(std::cbegin(impl->m_Actions), std::cend(impl->m_Actions), | |
36 | std::back_inserter(result), [](const auto &action) { return action.get(); }); |
|
36 | std::back_inserter(result), [](const auto &action) { return action.get(); }); | |
37 |
|
37 | |||
38 | return result; |
|
38 | return result; | |
39 | } |
|
39 | } | |
40 |
|
40 | |||
41 | void DataSourceItem::addAction(std::unique_ptr<DataSourceItemAction> action) noexcept |
|
41 | void DataSourceItem::addAction(std::unique_ptr<DataSourceItemAction> action) noexcept | |
42 | { |
|
42 | { | |
43 | action->setDataSourceItem(this); |
|
43 | action->setDataSourceItem(this); | |
44 | impl->m_Actions.push_back(std::move(action)); |
|
44 | impl->m_Actions.push_back(std::move(action)); | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | void DataSourceItem::appendChild(std::unique_ptr<DataSourceItem> child) noexcept |
|
47 | void DataSourceItem::appendChild(std::unique_ptr<DataSourceItem> child) noexcept | |
48 | { |
|
48 | { | |
49 | child->impl->m_Parent = this; |
|
49 | child->impl->m_Parent = this; | |
50 | impl->m_Children.push_back(std::move(child)); |
|
50 | impl->m_Children.push_back(std::move(child)); | |
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 | DataSourceItem *DataSourceItem::child(int childIndex) const noexcept |
|
53 | DataSourceItem *DataSourceItem::child(int childIndex) const noexcept | |
54 | { |
|
54 | { | |
55 | if (childIndex < 0 || childIndex >= childCount()) { |
|
55 | if (childIndex < 0 || childIndex >= childCount()) { | |
56 | return nullptr; |
|
56 | return nullptr; | |
57 | } |
|
57 | } | |
58 | else { |
|
58 | else { | |
59 | return impl->m_Children.at(childIndex).get(); |
|
59 | return impl->m_Children.at(childIndex).get(); | |
60 | } |
|
60 | } | |
61 | } |
|
61 | } | |
62 |
|
62 | |||
63 | int DataSourceItem::childCount() const noexcept |
|
63 | int DataSourceItem::childCount() const noexcept | |
64 | { |
|
64 | { | |
65 | return impl->m_Children.size(); |
|
65 | return impl->m_Children.size(); | |
66 | } |
|
66 | } | |
67 |
|
67 | |||
68 | QVariant DataSourceItem::data(const QString &key) const noexcept |
|
68 | QVariant DataSourceItem::data(const QString &key) const noexcept | |
69 | { |
|
69 | { | |
70 | return impl->m_Data.value(key); |
|
70 | return impl->m_Data.value(key); | |
71 | } |
|
71 | } | |
72 |
|
72 | |||
73 |
|
|
73 | QVariantHash DataSourceItem::data() const noexcept | |
74 | { |
|
74 | { | |
75 | return impl->m_Data; |
|
75 | return impl->m_Data; | |
76 | } |
|
76 | } | |
77 |
|
77 | |||
78 | bool DataSourceItem::isRoot() const noexcept |
|
78 | bool DataSourceItem::isRoot() const noexcept | |
79 | { |
|
79 | { | |
80 | return impl->m_Parent == nullptr; |
|
80 | return impl->m_Parent == nullptr; | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
83 | QString DataSourceItem::name() const noexcept |
|
83 | QString DataSourceItem::name() const noexcept | |
84 | { |
|
84 | { | |
85 | return data(NAME_DATA_KEY).toString(); |
|
85 | return data(NAME_DATA_KEY).toString(); | |
86 | } |
|
86 | } | |
87 |
|
87 | |||
88 | DataSourceItem *DataSourceItem::parentItem() const noexcept |
|
88 | DataSourceItem *DataSourceItem::parentItem() const noexcept | |
89 | { |
|
89 | { | |
90 | return impl->m_Parent; |
|
90 | return impl->m_Parent; | |
91 | } |
|
91 | } | |
92 |
|
92 | |||
93 | void DataSourceItem::setData(const QString &key, const QVariant &value, bool append) noexcept |
|
93 | void DataSourceItem::setData(const QString &key, const QVariant &value, bool append) noexcept | |
94 | { |
|
94 | { | |
95 | auto it = impl->m_Data.constFind(key); |
|
95 | auto it = impl->m_Data.constFind(key); | |
96 | if (append && it != impl->m_Data.constEnd()) { |
|
96 | if (append && it != impl->m_Data.constEnd()) { | |
97 | // Case of an existing value to which we want to add to the new value |
|
97 | // Case of an existing value to which we want to add to the new value | |
98 | if (it->canConvert<QVariantList>()) { |
|
98 | if (it->canConvert<QVariantList>()) { | |
99 | auto variantList = it->value<QVariantList>(); |
|
99 | auto variantList = it->value<QVariantList>(); | |
100 | variantList.append(value); |
|
100 | variantList.append(value); | |
101 |
|
101 | |||
102 | impl->m_Data.insert(key, variantList); |
|
102 | impl->m_Data.insert(key, variantList); | |
103 | } |
|
103 | } | |
104 | else { |
|
104 | else { | |
105 | impl->m_Data.insert(key, QVariantList{*it, value}); |
|
105 | impl->m_Data.insert(key, QVariantList{*it, value}); | |
106 | } |
|
106 | } | |
107 | } |
|
107 | } | |
108 | else { |
|
108 | else { | |
109 | // Other cases : |
|
109 | // Other cases : | |
110 | // - new value in map OR |
|
110 | // - new value in map OR | |
111 | // - replacement of an existing value (not appending) |
|
111 | // - replacement of an existing value (not appending) | |
112 | impl->m_Data.insert(key, value); |
|
112 | impl->m_Data.insert(key, value); | |
113 | } |
|
113 | } | |
114 | } |
|
114 | } | |
115 |
|
115 | |||
116 | DataSourceItemType DataSourceItem::type() const noexcept |
|
116 | DataSourceItemType DataSourceItem::type() const noexcept | |
117 | { |
|
117 | { | |
118 | return impl->m_Type; |
|
118 | return impl->m_Type; | |
119 | } |
|
119 | } | |
120 |
|
120 | |||
121 | bool DataSourceItem::operator==(const DataSourceItem &other) |
|
121 | bool DataSourceItem::operator==(const DataSourceItem &other) | |
122 | { |
|
122 | { | |
123 | // Compares items' attributes |
|
123 | // Compares items' attributes | |
124 | if (std::tie(impl->m_Type, impl->m_Data) == std::tie(other.impl->m_Type, other.impl->m_Data)) { |
|
124 | if (std::tie(impl->m_Type, impl->m_Data) == std::tie(other.impl->m_Type, other.impl->m_Data)) { | |
125 | // Compares contents of items' children |
|
125 | // Compares contents of items' children | |
126 | return std::equal(std::cbegin(impl->m_Children), std::cend(impl->m_Children), |
|
126 | return std::equal(std::cbegin(impl->m_Children), std::cend(impl->m_Children), | |
127 | std::cbegin(other.impl->m_Children), |
|
127 | std::cbegin(other.impl->m_Children), | |
128 | [](const auto &itemChild, const auto &otherChild) { |
|
128 | [](const auto &itemChild, const auto &otherChild) { | |
129 | return *itemChild == *otherChild; |
|
129 | return *itemChild == *otherChild; | |
130 | }); |
|
130 | }); | |
131 | } |
|
131 | } | |
132 | else { |
|
132 | else { | |
133 | return false; |
|
133 | return false; | |
134 | } |
|
134 | } | |
135 | } |
|
135 | } | |
136 |
|
136 | |||
137 | bool DataSourceItem::operator!=(const DataSourceItem &other) |
|
137 | bool DataSourceItem::operator!=(const DataSourceItem &other) | |
138 | { |
|
138 | { | |
139 | return !(*this == other); |
|
139 | return !(*this == other); | |
140 | } |
|
140 | } |
General Comments 0
You need to be logged in to leave comments.
Login now