]>
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"
22 #include "kstandarditem.h"
24 KStandardItemModel::KStandardItemModel(QObject
* parent
) :
25 KItemModelBase(parent
),
31 KStandardItemModel::~KStandardItemModel()
35 m_indexesForItems
.clear();
38 void KStandardItemModel::insertItem(int index
, KStandardItem
* item
)
40 if (index
< 0 || index
> count() || !item
) {
45 if (!m_indexesForItems
.contains(item
)) {
47 m_items
.insert(index
, item
);
48 m_indexesForItems
.insert(item
, index
);
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
);
56 // TODO: no hierarchical items are handled yet
58 onItemInserted(index
);
59 emit
itemsInserted(KItemRangeList() << KItemRange(index
, 1));
63 void KStandardItemModel::changeItem(int index
, KStandardItem
* item
)
65 if (index
< 0 || index
>= count() || !item
) {
72 QSet
<QByteArray
> changedRoles
;
74 KStandardItem
* oldItem
= m_items
[index
];
75 const QHash
<QByteArray
, QVariant
> oldData
= oldItem
->data();
76 const QHash
<QByteArray
, QVariant
> newData
= item
->data();
78 // Determine which roles have been changed
79 QHashIterator
<QByteArray
, QVariant
> it(oldData
);
80 while (it
.hasNext()) {
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
);
89 m_indexesForItems
.remove(oldItem
);
93 m_items
[index
] = item
;
94 m_indexesForItems
.insert(item
, index
);
96 onItemChanged(index
, changedRoles
);
97 emit
itemsChanged(KItemRangeList() << KItemRange(index
, 1), changedRoles
);
100 void KStandardItemModel::removeItem(int index
)
102 if (index
>= 0 && index
< count()) {
103 KStandardItem
* item
= m_items
[index
];
104 m_indexesForItems
.remove(item
);
105 m_items
.removeAt(index
);
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
);
113 onItemRemoved(index
, item
);
118 emit
itemsRemoved(KItemRangeList() << KItemRange(index
, 1));
120 // TODO: no hierarchical items are handled yet
124 void KStandardItemModel::clear()
126 int size
= m_items
.size();
128 m_indexesForItems
.clear();
130 emit
itemsRemoved(KItemRangeList() << KItemRange(0, size
));
133 KStandardItem
* KStandardItemModel::item(int index
) const
135 if (index
< 0 || index
>= m_items
.count()) {
138 return m_items
[index
];
141 int KStandardItemModel::index(const KStandardItem
* item
) const
143 return m_indexesForItems
.value(item
, -1);
146 void KStandardItemModel::appendItem(KStandardItem
*item
)
148 insertItem(m_items
.count(), item
);
151 int KStandardItemModel::count() const
153 return m_items
.count();
156 QHash
<QByteArray
, QVariant
> KStandardItemModel::data(int index
) const
158 if (index
>= 0 && index
< count()) {
159 const KStandardItem
* item
= m_items
[index
];
164 return QHash
<QByteArray
, QVariant
>();
167 bool KStandardItemModel::setData(int index
, const QHash
<QByteArray
, QVariant
>& values
)
170 if (index
< 0 || index
>= count()) {
177 QMimeData
* KStandardItemModel::createMimeData(const KItemSet
& indexes
) const
183 int KStandardItemModel::indexForKeyboardSearch(const QString
& text
, int startFromIndex
) const
186 Q_UNUSED(startFromIndex
)
190 bool KStandardItemModel::supportsDropping(int index
) const
196 QString
KStandardItemModel::roleDescription(const QByteArray
& role
) const
202 QList
<QPair
<int, QVariant
> > KStandardItemModel::groups() const
204 QList
<QPair
<int, QVariant
> > groups
;
206 const QByteArray role
= sortRole().isEmpty() ? "group" : sortRole();
207 bool isFirstGroupValue
= true;
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;
222 void KStandardItemModel::onItemInserted(int index
)
227 void KStandardItemModel::onItemChanged(int index
, const QSet
<QByteArray
>& changedRoles
)
230 Q_UNUSED(changedRoles
)
233 void KStandardItemModel::onItemRemoved(int index
, KStandardItem
* removedItem
)
236 Q_UNUSED(removedItem
)