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