]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemmodel.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "kstandarditemmodel.h"
23 #include "kstandarditem.h"
25 KStandardItemModel::KStandardItemModel(QObject
* parent
) :
26 KItemModelBase(parent
),
32 KStandardItemModel::~KStandardItemModel()
36 m_indexesForItems
.clear();
39 void KStandardItemModel::insertItem(int index
, KStandardItem
* item
)
41 if (index
< 0 || index
> count() || !item
) {
46 if (!m_indexesForItems
.contains(item
)) {
48 m_items
.insert(index
, item
);
49 m_indexesForItems
.insert(item
, index
);
51 // Inserting an item requires to update the indexes
52 // afterwards from m_indexesForItems.
53 for (int i
= index
+ 1; i
< m_items
.count(); ++i
) {
54 m_indexesForItems
.insert(m_items
[i
], i
);
57 // TODO: no hierarchical items are handled yet
59 onItemInserted(index
);
60 emit
itemsInserted(KItemRangeList() << KItemRange(index
, 1));
64 void KStandardItemModel::changeItem(int index
, KStandardItem
* item
)
66 if (index
< 0 || index
>= count() || !item
) {
73 QSet
<QByteArray
> changedRoles
;
75 KStandardItem
* oldItem
= m_items
[index
];
76 const QHash
<QByteArray
, QVariant
> oldData
= oldItem
->data();
77 const QHash
<QByteArray
, QVariant
> newData
= item
->data();
79 // Determine which roles have been changed
80 QHashIterator
<QByteArray
, QVariant
> it(oldData
);
81 while (it
.hasNext()) {
83 const QByteArray role
= it
.key();
84 const QVariant oldValue
= it
.value();
85 if (newData
.contains(role
) && newData
.value(role
) != oldValue
) {
86 changedRoles
.insert(role
);
90 m_indexesForItems
.remove(oldItem
);
94 m_items
[index
] = item
;
95 m_indexesForItems
.insert(item
, index
);
97 onItemChanged(index
, changedRoles
);
98 emit
itemsChanged(KItemRangeList() << KItemRange(index
, 1), changedRoles
);
101 void KStandardItemModel::removeItem(int index
)
103 if (index
>= 0 && index
< count()) {
104 KStandardItem
* item
= m_items
[index
];
105 m_indexesForItems
.remove(item
);
106 m_items
.removeAt(index
);
108 // Removing an item requires to update the indexes
109 // afterwards from m_indexesForItems.
110 for (int i
= index
; i
< m_items
.count(); ++i
) {
111 m_indexesForItems
.insert(m_items
[i
], i
);
114 onItemRemoved(index
, item
);
119 emit
itemsRemoved(KItemRangeList() << KItemRange(index
, 1));
121 // TODO: no hierarchical items are handled yet
125 KStandardItem
* KStandardItemModel::item(int index
) const
127 if (index
< 0 || index
>= m_items
.count()) {
130 return m_items
[index
];
133 int KStandardItemModel::index(const KStandardItem
* item
) const
135 return m_indexesForItems
.value(item
, -1);
138 void KStandardItemModel::appendItem(KStandardItem
*item
)
140 insertItem(m_items
.count(), item
);
143 int KStandardItemModel::count() const
145 return m_items
.count();
148 QHash
<QByteArray
, QVariant
> KStandardItemModel::data(int index
) const
150 if (index
>= 0 && index
< count()) {
151 const KStandardItem
* item
= m_items
[index
];
156 return QHash
<QByteArray
, QVariant
>();
159 bool KStandardItemModel::setData(int index
, const QHash
<QByteArray
, QVariant
>& values
)
162 if (index
< 0 || index
>= count()) {
169 QMimeData
* KStandardItemModel::createMimeData(const QSet
<int>& indexes
) const
175 int KStandardItemModel::indexForKeyboardSearch(const QString
& text
, int startFromIndex
) const
178 Q_UNUSED(startFromIndex
);
182 bool KStandardItemModel::supportsDropping(int index
) const
188 QString
KStandardItemModel::roleDescription(const QByteArray
& role
) const
194 QList
<QPair
<int, QVariant
> > KStandardItemModel::groups() const
196 QList
<QPair
<int, QVariant
> > groups
;
198 const QByteArray role
= sortRole();
199 bool isFirstGroupValue
= true;
201 const int maxIndex
= count() - 1;
202 for (int i
= 0; i
<= maxIndex
; ++i
) {
203 const QString newGroupValue
= m_items
.at(i
)->dataValue(role
).toString();
204 if (newGroupValue
!= groupValue
|| isFirstGroupValue
) {
205 groupValue
= newGroupValue
;
206 groups
.append(QPair
<int, QVariant
>(i
, newGroupValue
));
207 isFirstGroupValue
= false;
214 void KStandardItemModel::onItemInserted(int index
)
219 void KStandardItemModel::onItemChanged(int index
, const QSet
<QByteArray
>& changedRoles
)
222 Q_UNUSED(changedRoles
);
225 void KStandardItemModel::onItemRemoved(int index
, KStandardItem
* removedItem
)
228 Q_UNUSED(removedItem
);
232 #include "kstandarditemmodel.moc"