]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemmodel.cpp
Places Panel: Show drop indicator
[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 (index < 0 || index > count() || !item) {
42 delete item;
43 return;
44 }
45
46 if (!m_indexesForItems.contains(item)) {
47 item->m_model = this;
48 m_items.insert(index, item);
49 m_indexesForItems.insert(item, index);
50
51 // Inserting an item requires to update the indexes
52 // afterwards from m_indexesForItems.
53 for (int i = index + 1; i < m_items.count(); ++i) {
54 m_indexesForItems.insert(m_items[i], i);
55 }
56
57 // TODO: no hierarchical items are handled yet
58
59 onItemInserted(index);
60 emit itemsInserted(KItemRangeList() << KItemRange(index, 1));
61 }
62 }
63
64 void KStandardItemModel::changeItem(int index, KStandardItem* item)
65 {
66 if (index < 0 || index >= count() || !item) {
67 delete item;
68 return;
69 }
70
71 item->m_model = this;
72
73 QSet<QByteArray> changedRoles;
74
75 KStandardItem* oldItem = m_items[index];
76 const QHash<QByteArray, QVariant> oldData = oldItem->data();
77 const QHash<QByteArray, QVariant> newData = item->data();
78
79 // Determine which roles have been changed
80 QHashIterator<QByteArray, QVariant> it(oldData);
81 while (it.hasNext()) {
82 it.next();
83 const QByteArray role = it.key();
84 const QVariant oldValue = it.value();
85 if (newData.contains(role) && newData.value(role) != oldValue) {
86 changedRoles.insert(role);
87 }
88 }
89
90 m_indexesForItems.remove(oldItem);
91 delete oldItem;
92 oldItem = 0;
93
94 m_items[index] = item;
95 m_indexesForItems.insert(item, index);
96
97 onItemChanged(index, changedRoles);
98 emit itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles);
99 }
100
101 void KStandardItemModel::removeItem(int index)
102 {
103 if (index >= 0 && index < count()) {
104 KStandardItem* item = m_items[index];
105 m_indexesForItems.remove(item);
106 m_items.removeAt(index);
107
108 // Removing an item requires to update the indexes
109 // afterwards from m_indexesForItems.
110 for (int i = index; i < m_items.count(); ++i) {
111 m_indexesForItems.insert(m_items[i], i);
112 }
113
114 onItemRemoved(index, item);
115
116 delete item;
117 item = 0;
118
119 emit itemsRemoved(KItemRangeList() << KItemRange(index, 1));
120
121 // TODO: no hierarchical items are handled yet
122 }
123 }
124
125 KStandardItem* KStandardItemModel::item(int index) const
126 {
127 if (index < 0 || index >= m_items.count()) {
128 return 0;
129 }
130 return m_items[index];
131 }
132
133 int KStandardItemModel::index(const KStandardItem* item) const
134 {
135 return m_indexesForItems.value(item, -1);
136 }
137
138 void KStandardItemModel::appendItem(KStandardItem *item)
139 {
140 insertItem(m_items.count(), item);
141 }
142
143 int KStandardItemModel::count() const
144 {
145 return m_items.count();
146 }
147
148 QHash<QByteArray, QVariant> KStandardItemModel::data(int index) const
149 {
150 if (index >= 0 && index < count()) {
151 const KStandardItem* item = m_items[index];
152 if (item) {
153 return item->data();
154 }
155 }
156 return QHash<QByteArray, QVariant>();
157 }
158
159 bool KStandardItemModel::setData(int index, const QHash<QByteArray, QVariant>& values)
160 {
161 Q_UNUSED(values);
162 if (index < 0 || index >= count()) {
163 return false;
164 }
165
166 return true;
167 }
168
169 QMimeData* KStandardItemModel::createMimeData(const QSet<int>& indexes) const
170 {
171 Q_UNUSED(indexes);
172 return 0;
173 }
174
175 int KStandardItemModel::indexForKeyboardSearch(const QString& text, int startFromIndex) const
176 {
177 Q_UNUSED(text);
178 Q_UNUSED(startFromIndex);
179 return -1;
180 }
181
182 bool KStandardItemModel::supportsDropping(int index) const
183 {
184 Q_UNUSED(index);
185 return false;
186 }
187
188 QString KStandardItemModel::roleDescription(const QByteArray& role) const
189 {
190 Q_UNUSED(role);
191 return QString();
192 }
193
194 QList<QPair<int, QVariant> > KStandardItemModel::groups() const
195 {
196 QList<QPair<int, QVariant> > groups;
197
198 const QByteArray role = sortRole().isEmpty() ? "group" : sortRole();
199 bool isFirstGroupValue = true;
200 QString groupValue;
201 const int maxIndex = count() - 1;
202 for (int i = 0; i <= maxIndex; ++i) {
203 const QString newGroupValue = m_items.at(i)->dataValue(role).toString();
204 if (newGroupValue != groupValue || isFirstGroupValue) {
205 groupValue = newGroupValue;
206 groups.append(QPair<int, QVariant>(i, newGroupValue));
207 isFirstGroupValue = false;
208 }
209 }
210
211 return groups;
212 }
213
214 void KStandardItemModel::onItemInserted(int index)
215 {
216 Q_UNUSED(index);
217 }
218
219 void KStandardItemModel::onItemChanged(int index, const QSet<QByteArray>& changedRoles)
220 {
221 Q_UNUSED(index);
222 Q_UNUSED(changedRoles);
223 }
224
225 void KStandardItemModel::onItemRemoved(int index, KStandardItem* removedItem)
226 {
227 Q_UNUSED(index);
228 Q_UNUSED(removedItem);
229 }
230
231
232 #include "kstandarditemmodel.moc"