]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemmodel.cpp
Provide additional default groups for the Places Panel
[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 #include "kstandarditem.h"
22
23 KStandardItemModel::KStandardItemModel(QObject* parent) :
24 KItemModelBase(parent),
25 m_items(),
26 m_indexesForItems()
27 {
28 }
29
30 KStandardItemModel::~KStandardItemModel()
31 {
32 }
33
34 void KStandardItemModel::insertItem(int index, KStandardItem* item)
35 {
36 if (!m_indexesForItems.contains(item) && !item->m_model) {
37 m_items.insert(index, item);
38 m_indexesForItems.insert(item, index);
39 item->m_model = this;
40 // TODO: no hierarchical items are handled yet
41
42 emit itemsInserted(KItemRangeList() << KItemRange(index, 1));
43 }
44 }
45
46 void KStandardItemModel::appendItem(KStandardItem *item)
47 {
48 insertItem(m_items.count(), item);
49 }
50
51 void KStandardItemModel::removeItem(KStandardItem* item)
52 {
53 const int index = m_indexesForItems.value(item, -1);
54 if (index >= 0) {
55 m_items.removeAt(index);
56 m_indexesForItems.remove(item);
57 delete item;
58 // TODO: no hierarchical items are handled yet
59 }
60 }
61
62 KStandardItem* KStandardItemModel::item(int index) const
63 {
64 if (index < 0 || index >= m_items.count()) {
65 return 0;
66 }
67 return m_items[index];
68 }
69
70 int KStandardItemModel::index(const KStandardItem* item) const
71 {
72 return m_indexesForItems.value(item, -1);
73 }
74
75 int KStandardItemModel::count() const
76 {
77 return m_items.count();
78 }
79
80 QHash<QByteArray, QVariant> KStandardItemModel::data(int index) const
81 {
82 const KStandardItem* item = m_items[index];
83 if (item) {
84 return item->data();
85 }
86 return QHash<QByteArray, QVariant>();
87 }
88
89 bool KStandardItemModel::setData(int index, const QHash<QByteArray, QVariant>& values)
90 {
91 Q_UNUSED(values);
92 if (index < 0 || index >= count()) {
93 return false;
94 }
95
96 return true;
97 }
98
99 QMimeData* KStandardItemModel::createMimeData(const QSet<int>& indexes) const
100 {
101 Q_UNUSED(indexes);
102 return 0;
103 }
104
105 int KStandardItemModel::indexForKeyboardSearch(const QString& text, int startFromIndex) const
106 {
107 Q_UNUSED(text);
108 Q_UNUSED(startFromIndex);
109 return -1;
110 }
111
112 bool KStandardItemModel::supportsDropping(int index) const
113 {
114 Q_UNUSED(index);
115 return false;
116 }
117
118 QString KStandardItemModel::roleDescription(const QByteArray& role) const
119 {
120 Q_UNUSED(role);
121 return QString();
122 }
123
124 QList<QPair<int, QVariant> > KStandardItemModel::groups() const
125 {
126 QList<QPair<int, QVariant> > groups;
127
128 const QByteArray role = sortRole();
129 bool isFirstGroupValue = true;
130 QString groupValue;
131 const int maxIndex = count() - 1;
132 for (int i = 0; i <= maxIndex; ++i) {
133 const QString newGroupValue = m_items.at(i)->dataValue(role).toString();
134 if (newGroupValue != groupValue || isFirstGroupValue) {
135 groupValue = newGroupValue;
136 groups.append(QPair<int, QVariant>(i, newGroupValue));
137 isFirstGroupValue = false;
138 }
139 }
140
141 return groups;
142 }
143
144 #include "kstandarditemmodel.moc"