]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemmodel.cpp
PlacesItemModel: Remove setItemHidden and isItemHidden
[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 delete item;
97 item = 0;
98
99 onItemRemoved(index);
100 emit itemsRemoved(KItemRangeList() << KItemRange(index, 1));
101 // TODO: no hierarchical items are handled yet
102 }
103 }
104
105 KStandardItem* KStandardItemModel::item(int index) const
106 {
107 if (index < 0 || index >= m_items.count()) {
108 return 0;
109 }
110 return m_items[index];
111 }
112
113 int KStandardItemModel::index(const KStandardItem* item) const
114 {
115 return m_indexesForItems.value(item, -1);
116 }
117
118 void KStandardItemModel::appendItem(KStandardItem *item)
119 {
120 insertItem(m_items.count(), item);
121 }
122
123 int KStandardItemModel::count() const
124 {
125 return m_items.count();
126 }
127
128 QHash<QByteArray, QVariant> KStandardItemModel::data(int index) const
129 {
130 if (index >= 0 && index < count()) {
131 const KStandardItem* item = m_items[index];
132 if (item) {
133 return item->data();
134 }
135 }
136 return QHash<QByteArray, QVariant>();
137 }
138
139 bool KStandardItemModel::setData(int index, const QHash<QByteArray, QVariant>& values)
140 {
141 Q_UNUSED(values);
142 if (index < 0 || index >= count()) {
143 return false;
144 }
145
146 return true;
147 }
148
149 QMimeData* KStandardItemModel::createMimeData(const QSet<int>& indexes) const
150 {
151 Q_UNUSED(indexes);
152 return 0;
153 }
154
155 int KStandardItemModel::indexForKeyboardSearch(const QString& text, int startFromIndex) const
156 {
157 Q_UNUSED(text);
158 Q_UNUSED(startFromIndex);
159 return -1;
160 }
161
162 bool KStandardItemModel::supportsDropping(int index) const
163 {
164 Q_UNUSED(index);
165 return false;
166 }
167
168 QString KStandardItemModel::roleDescription(const QByteArray& role) const
169 {
170 Q_UNUSED(role);
171 return QString();
172 }
173
174 QList<QPair<int, QVariant> > KStandardItemModel::groups() const
175 {
176 QList<QPair<int, QVariant> > groups;
177
178 const QByteArray role = sortRole();
179 bool isFirstGroupValue = true;
180 QString groupValue;
181 const int maxIndex = count() - 1;
182 for (int i = 0; i <= maxIndex; ++i) {
183 const QString newGroupValue = m_items.at(i)->dataValue(role).toString();
184 if (newGroupValue != groupValue || isFirstGroupValue) {
185 groupValue = newGroupValue;
186 groups.append(QPair<int, QVariant>(i, newGroupValue));
187 isFirstGroupValue = false;
188 }
189 }
190
191 return groups;
192 }
193
194 void KStandardItemModel::onItemInserted(int index)
195 {
196 Q_UNUSED(index);
197 }
198
199 void KStandardItemModel::onItemChanged(int index, const QSet<QByteArray>& changedRoles)
200 {
201 Q_UNUSED(index);
202 }
203
204 void KStandardItemModel::onItemRemoved(int index)
205 {
206 Q_UNUSED(index);
207 }
208
209
210 #include "kstandarditemmodel.moc"