]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemmodel.cpp
Merge branch 'davidedmundson/highdpi'
[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 "kstandarditem.h"
23 #include <QSet>
24 #include <QByteArray>
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 void KStandardItemModel::clear()
126 {
127 int size = m_items.size();
128 m_items.clear();
129 m_indexesForItems.clear();
130
131 emit itemsRemoved(KItemRangeList() << KItemRange(0, size));
132 }
133
134 KStandardItem* KStandardItemModel::item(int index) const
135 {
136 if (index < 0 || index >= m_items.count()) {
137 return 0;
138 }
139 return m_items[index];
140 }
141
142 int KStandardItemModel::index(const KStandardItem* item) const
143 {
144 return m_indexesForItems.value(item, -1);
145 }
146
147 void KStandardItemModel::appendItem(KStandardItem *item)
148 {
149 insertItem(m_items.count(), item);
150 }
151
152 int KStandardItemModel::count() const
153 {
154 return m_items.count();
155 }
156
157 QHash<QByteArray, QVariant> KStandardItemModel::data(int index) const
158 {
159 if (index >= 0 && index < count()) {
160 const KStandardItem* item = m_items[index];
161 if (item) {
162 return item->data();
163 }
164 }
165 return QHash<QByteArray, QVariant>();
166 }
167
168 bool KStandardItemModel::setData(int index, const QHash<QByteArray, QVariant>& values)
169 {
170 Q_UNUSED(values);
171 if (index < 0 || index >= count()) {
172 return false;
173 }
174
175 return true;
176 }
177
178 QMimeData* KStandardItemModel::createMimeData(const KItemSet& indexes) const
179 {
180 Q_UNUSED(indexes);
181 return 0;
182 }
183
184 int KStandardItemModel::indexForKeyboardSearch(const QString& text, int startFromIndex) const
185 {
186 Q_UNUSED(text);
187 Q_UNUSED(startFromIndex);
188 return -1;
189 }
190
191 bool KStandardItemModel::supportsDropping(int index) const
192 {
193 Q_UNUSED(index);
194 return false;
195 }
196
197 QString KStandardItemModel::roleDescription(const QByteArray& role) const
198 {
199 Q_UNUSED(role);
200 return QString();
201 }
202
203 QList<QPair<int, QVariant> > KStandardItemModel::groups() const
204 {
205 QList<QPair<int, QVariant> > groups;
206
207 const QByteArray role = sortRole().isEmpty() ? "group" : sortRole();
208 bool isFirstGroupValue = true;
209 QString groupValue;
210 const int maxIndex = count() - 1;
211 for (int i = 0; i <= maxIndex; ++i) {
212 const QString newGroupValue = m_items.at(i)->dataValue(role).toString();
213 if (newGroupValue != groupValue || isFirstGroupValue) {
214 groupValue = newGroupValue;
215 groups.append(QPair<int, QVariant>(i, newGroupValue));
216 isFirstGroupValue = false;
217 }
218 }
219
220 return groups;
221 }
222
223 void KStandardItemModel::onItemInserted(int index)
224 {
225 Q_UNUSED(index);
226 }
227
228 void KStandardItemModel::onItemChanged(int index, const QSet<QByteArray>& changedRoles)
229 {
230 Q_UNUSED(index);
231 Q_UNUSED(changedRoles);
232 }
233
234 void KStandardItemModel::onItemRemoved(int index, KStandardItem* removedItem)
235 {
236 Q_UNUSED(index);
237 Q_UNUSED(removedItem);
238 }
239