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