]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemmodel.cpp
Merge branch 'release/21.12'
[dolphin.git] / src / kitemviews / kstandarditemmodel.cpp
1 /*
2 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "kstandarditemmodel.h"
8
9 #include "kstandarditem.h"
10
11 KStandardItemModel::KStandardItemModel(QObject* parent) :
12 KItemModelBase(parent),
13 m_items(),
14 m_indexesForItems()
15 {
16 }
17
18 KStandardItemModel::~KStandardItemModel()
19 {
20 qDeleteAll(m_items);
21 m_items.clear();
22 m_indexesForItems.clear();
23 }
24
25 void KStandardItemModel::insertItem(int index, KStandardItem* item)
26 {
27 if (index < 0 || index > count() || !item) {
28 delete item;
29 return;
30 }
31
32 if (!m_indexesForItems.contains(item)) {
33 item->m_model = this;
34 m_items.insert(index, item);
35 m_indexesForItems.insert(item, index);
36
37 // Inserting an item requires to update the indexes
38 // afterwards from m_indexesForItems.
39 for (int i = index + 1; i < m_items.count(); ++i) {
40 m_indexesForItems.insert(m_items[i], i);
41 }
42
43 // TODO: no hierarchical items are handled yet
44
45 onItemInserted(index);
46 Q_EMIT itemsInserted(KItemRangeList() << KItemRange(index, 1));
47 }
48 }
49
50 void KStandardItemModel::changeItem(int index, KStandardItem* item)
51 {
52 if (index < 0 || index >= count() || !item) {
53 delete item;
54 return;
55 }
56
57 item->m_model = this;
58
59 QSet<QByteArray> changedRoles;
60
61 KStandardItem* oldItem = m_items[index];
62 const QHash<QByteArray, QVariant> oldData = oldItem->data();
63 const QHash<QByteArray, QVariant> newData = item->data();
64
65 // Determine which roles have been changed
66 QHashIterator<QByteArray, QVariant> it(oldData);
67 while (it.hasNext()) {
68 it.next();
69 const QByteArray role = it.key();
70 const QVariant oldValue = it.value();
71 if (newData.contains(role) && newData.value(role) != oldValue) {
72 changedRoles.insert(role);
73 }
74 }
75
76 m_indexesForItems.remove(oldItem);
77 delete oldItem;
78 oldItem = nullptr;
79
80 m_items[index] = item;
81 m_indexesForItems.insert(item, index);
82
83 onItemChanged(index, changedRoles);
84 Q_EMIT itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles);
85 }
86
87 void KStandardItemModel::removeItem(int index)
88 {
89 if (index >= 0 && index < count()) {
90 KStandardItem* item = m_items[index];
91 m_indexesForItems.remove(item);
92 m_items.removeAt(index);
93
94 // Removing an item requires to update the indexes
95 // afterwards from m_indexesForItems.
96 for (int i = index; i < m_items.count(); ++i) {
97 m_indexesForItems.insert(m_items[i], i);
98 }
99
100 onItemRemoved(index, item);
101
102 item->deleteLater();
103 item = nullptr;
104
105 Q_EMIT itemsRemoved(KItemRangeList() << KItemRange(index, 1));
106
107 // TODO: no hierarchical items are handled yet
108 }
109 }
110
111 void KStandardItemModel::clear()
112 {
113 int size = m_items.size();
114 m_items.clear();
115 m_indexesForItems.clear();
116
117 Q_EMIT itemsRemoved(KItemRangeList() << KItemRange(0, size));
118 }
119
120 KStandardItem* KStandardItemModel::item(int index) const
121 {
122 if (index < 0 || index >= m_items.count()) {
123 return nullptr;
124 }
125 return m_items[index];
126 }
127
128 int KStandardItemModel::index(const KStandardItem* item) const
129 {
130 return m_indexesForItems.value(item, -1);
131 }
132
133 void KStandardItemModel::appendItem(KStandardItem *item)
134 {
135 insertItem(m_items.count(), item);
136 }
137
138 int KStandardItemModel::count() const
139 {
140 return m_items.count();
141 }
142
143 QHash<QByteArray, QVariant> KStandardItemModel::data(int index) const
144 {
145 if (index >= 0 && index < count()) {
146 const KStandardItem* item = m_items[index];
147 if (item) {
148 return item->data();
149 }
150 }
151 return QHash<QByteArray, QVariant>();
152 }
153
154 bool KStandardItemModel::setData(int index, const QHash<QByteArray, QVariant>& values)
155 {
156 Q_UNUSED(values)
157 if (index < 0 || index >= count()) {
158 return false;
159 }
160
161 return true;
162 }
163
164 QMimeData* KStandardItemModel::createMimeData(const KItemSet& indexes) const
165 {
166 Q_UNUSED(indexes)
167 return nullptr;
168 }
169
170 int KStandardItemModel::indexForKeyboardSearch(const QString& text, int startFromIndex) const
171 {
172 Q_UNUSED(text)
173 Q_UNUSED(startFromIndex)
174 return -1;
175 }
176
177 bool KStandardItemModel::supportsDropping(int index) const
178 {
179 Q_UNUSED(index)
180 return false;
181 }
182
183 QString KStandardItemModel::roleDescription(const QByteArray& role) const
184 {
185 Q_UNUSED(role)
186 return QString();
187 }
188
189 QList<QPair<int, QVariant> > KStandardItemModel::groups() const
190 {
191 QList<QPair<int, QVariant> > groups;
192
193 const QByteArray role = sortRole().isEmpty() ? "group" : sortRole();
194 bool isFirstGroupValue = true;
195 QString groupValue;
196 const int maxIndex = count() - 1;
197 for (int i = 0; i <= maxIndex; ++i) {
198 const QString newGroupValue = m_items.at(i)->dataValue(role).toString();
199 if (newGroupValue != groupValue || isFirstGroupValue) {
200 groupValue = newGroupValue;
201 groups.append(QPair<int, QVariant>(i, newGroupValue));
202 isFirstGroupValue = false;
203 }
204 }
205
206 return groups;
207 }
208
209 void KStandardItemModel::onItemInserted(int index)
210 {
211 Q_UNUSED(index)
212 }
213
214 void KStandardItemModel::onItemChanged(int index, const QSet<QByteArray>& changedRoles)
215 {
216 Q_UNUSED(index)
217 Q_UNUSED(changedRoles)
218 }
219
220 void KStandardItemModel::onItemRemoved(int index, KStandardItem* removedItem)
221 {
222 Q_UNUSED(index)
223 Q_UNUSED(removedItem)
224 }
225