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