@@ -72,6 +72,15 public: | |||||
72 | */ |
|
72 | */ | |
73 | DataSourceItem *parentItem() const noexcept; |
|
73 | DataSourceItem *parentItem() const noexcept; | |
74 |
|
74 | |||
|
75 | /** | |||
|
76 | * Sets or appends a value to a key | |||
|
77 | * @param key the key | |||
|
78 | * @param value the value | |||
|
79 | * @param append if true, the value is added to the values already existing for the key, | |||
|
80 | * otherwise it replaces the existing values | |||
|
81 | */ | |||
|
82 | void setData(const QString &key, const QVariant &value, bool append = false) noexcept; | |||
|
83 | ||||
75 | DataSourceItemType type() const noexcept; |
|
84 | DataSourceItemType type() const noexcept; | |
76 |
|
85 | |||
77 | private: |
|
86 | private: |
@@ -90,6 +90,29 DataSourceItem *DataSourceItem::parentItem() const noexcept | |||||
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 | |||
|
94 | { | |||
|
95 | auto it = impl->m_Data.constFind(key); | |||
|
96 | if (append && it != impl->m_Data.constEnd()) { | |||
|
97 | // Case of an existing value to which we want to add to the new value | |||
|
98 | if (it->canConvert<QVariantList>()) { | |||
|
99 | auto variantList = it->value<QVariantList>(); | |||
|
100 | variantList.append(value); | |||
|
101 | ||||
|
102 | impl->m_Data.insert(key, variantList); | |||
|
103 | } | |||
|
104 | else { | |||
|
105 | impl->m_Data.insert(key, QVariantList{*it, value}); | |||
|
106 | } | |||
|
107 | } | |||
|
108 | else { | |||
|
109 | // Other cases : | |||
|
110 | // - new value in map OR | |||
|
111 | // - replacement of an existing value (not appending) | |||
|
112 | impl->m_Data.insert(key, value); | |||
|
113 | } | |||
|
114 | } | |||
|
115 | ||||
93 | DataSourceItemType DataSourceItem::type() const noexcept |
|
116 | DataSourceItemType DataSourceItem::type() const noexcept | |
94 | { |
|
117 | { | |
95 | return impl->m_Type; |
|
118 | return impl->m_Type; |
General Comments 0
You need to be logged in to leave comments.
Login now