]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemmodel.cpp
KStandardItemModel: Fix inconsistent internal state
[dolphin.git] / src / kitemviews / kstandarditemmodel.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "kstandarditemmodel.h"
21
22 #include <KDebug>
23 #include "kstandarditem.h"
24
25 KStandardItemModel::KStandardItemModel(QObject* parent) :
26 KItemModelBase(parent),
27 m_items(),
28 m_indexesForItems()
29 {
30 }
31
32 KStandardItemModel::~KStandardItemModel()
33 {
34 qDeleteAll(m_items);
35 m_items.clear();
36 m_indexesForItems.clear();
37 }
38
39 void KStandardItemModel::insertItem(int index, KStandardItem* item)
40 {
41 if (item && !m_indexesForItems.contains(item)) {
42 item->m_model = this;
43 m_items.insert(index, item);
44 m_indexesForItems.insert(item, index);
45
46 // Inserting an item requires to update the indexes
47 // afterwards from m_indexesForItems.
48 for (int i = index + 1; i < m_items.count(); ++i) {
49 m_indexesForItems.insert(m_items[i], i);
50 }
51
52 // TODO: no hierarchical items are handled yet
53
54 onItemInserted(index);
55 emit itemsInserted(KItemRangeList() << KItemRange(index, 1));
56 }
57 }
58
59 void KStandardItemModel::changeItem(int index, KStandardItem* item)
60 {
61 if (item && index >= 0 && index < count()) {
62 item->m_model = this;
63
64 QSet<QByteArray> changedRoles;
65
66 KStandardItem* oldItem = m_items[index];
67 const QHash<QByteArray, QVariant> oldData = oldItem->data();
68 const QHash<QByteArray, QVariant> newData = item->data();
69
70 // Determine which roles have been changed
71 QHashIterator<QByteArray, QVariant> it(oldData);
72 while (it.hasNext()) {
73 it.next();
74 const QByteArray role = it.key();
75 const QVariant oldValue = it.value();
76 if (newData.contains(role) && newData.value(role) != oldValue) {
77 changedRoles.insert(role);
78 }
79 }
80
81 m_indexesForItems.remove(oldItem);
82 delete oldItem;
83 oldItem = 0;
84
85 m_items[index] = item;
86 m_indexesForItems.insert(item, index);
87
88 onItemChanged(index, changedRoles);
89 emit itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles);
90 } else {
91 kWarning() << "No item available to replace on the given index" << index;
92 delete item;
93 item = 0;
94 }
95 }
96
97 void KStandardItemModel::removeItem(int index)
98 {
99 if (index >= 0 && index < count()) {
100 KStandardItem* item = m_items[index];
101 m_indexesForItems.remove(item);
102 m_items.removeAt(index);
103
104 // Removing an item requires to update the indexes
105 // afterwards from m_indexesForItems.
106 for (int i = index; i < m_items.count(); ++i) {
107 m_indexesForItems.insert(m_items[i], i);
108 }
109
110 onItemRemoved(index, item);
111
112 delete item;
113 item = 0;
114
115 emit itemsRemoved(KItemRangeList() << KItemRange(index, 1));
116
117 // TODO: no hierarchical items are handled yet
118 }
119 }
120
121 KStandardItem* KStandardItemModel::item(int index) const
122 {
123 if (index < 0 || index >= m_items.count()) {
124 return 0;
125 }
126 return m_items[index];
127 }
128
129 int KStandardItemModel::index(const KStandardItem* item) const
130 {
131 return m_indexesForItems.value(item, -1);
132 }
133
134 void KStandardItemModel::appendItem(KStandardItem *item)
135 {
136 insertItem(m_items.count(), item);
137 }
138
139 int KStandardItemModel::count() const
140 {
141 return m_items.count();
142 }
143
144 QHash<QByteArray, QVariant> KStandardItemModel::data(int index) const
145 {
146 if (index >= 0 && index < count()) {
147 const KStandardItem* item = m_items[index];
148 if (item) {
149 return item->data();
150 }
151 }
152 return QHash<QByteArray, QVariant>();
153 }
154
155 bool KStandardItemModel::setData(int index, const QHash<QByteArray, QVariant>& values)
156 {
157 Q_UNUSED(values);
158 if (index < 0 || index >= count()) {
159 return false;
160 }
161
162 return true;
163 }
164
165 QMimeData* KStandardItemModel::createMimeData(const QSet<int>& indexes) const
166 {
167 Q_UNUSED(indexes);
168 return 0;
169 }
170
171 int KStandardItemModel::indexForKeyboardSearch(const QString& text, int startFromIndex) const
172 {
173 Q_UNUSED(text);
174 Q_UNUSED(startFromIndex);
175 return -1;
176 }
177
178 bool KStandardItemModel::supportsDropping(int index) const
179 {
180 Q_UNUSED(index);
181 return false;
182 }
183
184 QString KStandardItemModel::roleDescription(const QByteArray& role) const
185 {
186 Q_UNUSED(role);
187 return QString();
188 }
189
190 QList<QPair<int, QVariant> > KStandardItemModel::groups() const
191 {
192 QList<QPair<int, QVariant> > groups;
193
194 const QByteArray role = sortRole();
195 bool isFirstGroupValue = true;
196 QString groupValue;
197 const int maxIndex = count() - 1;
198 for (int i = 0; i <= maxIndex; ++i) {
199 const QString newGroupValue = m_items.at(i)->dataValue(role).toString();
200 if (newGroupValue != groupValue || isFirstGroupValue) {
201 groupValue = newGroupValue;
202 groups.append(QPair<int, QVariant>(i, newGroupValue));
203 isFirstGroupValue = false;
204 }
205 }
206
207 return groups;
208 }
209
210 void KStandardItemModel::onItemInserted(int index)
211 {
212 Q_UNUSED(index);
213 }
214
215 void KStandardItemModel::onItemChanged(int index, const QSet<QByteArray>& changedRoles)
216 {
217 Q_UNUSED(index);
218 Q_UNUSED(changedRoles);
219 }
220
221 void KStandardItemModel::onItemRemoved(int index, KStandardItem* removedItem)
222 {
223 Q_UNUSED(index);
224 Q_UNUSED(removedItem);
225 }
226
227
228 #include "kstandarditemmodel.moc"