]>
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 (!m_indexesForItems
.contains(item
) && !item
->m_model
) {
42 m_items
.insert(index
, item
);
43 m_indexesForItems
.insert(item
, index
);
45 // TODO: no hierarchical items are handled yet
47 emit
itemsInserted(KItemRangeList() << KItemRange(index
, 1));
51 void KStandardItemModel::replaceItem(int index
, KStandardItem
* item
)
53 if (index
>= 0 && index
< count()) {
54 QSet
<QByteArray
> changedRoles
;
56 KStandardItem
* oldItem
= m_items
[index
];
57 const QHash
<QByteArray
, QVariant
> oldData
= oldItem
->data();
58 const QHash
<QByteArray
, QVariant
> newData
= item
->data();
60 // Determine which roles have been changed
61 QHashIterator
<QByteArray
, QVariant
> it(oldData
);
62 while (it
.hasNext()) {
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
);
71 m_indexesForItems
.remove(oldItem
);
75 m_items
[index
] = item
;
76 m_indexesForItems
.insert(item
, index
);
78 emit
itemsChanged(KItemRangeList() << KItemRange(index
, 1), changedRoles
);
80 kWarning() << "No item available to replace on the given index" << index
;
86 void KStandardItemModel::appendItem(KStandardItem
*item
)
88 insertItem(m_items
.count(), item
);
91 void KStandardItemModel::removeItem(int index
)
93 if (index
>= 0 && index
< count()) {
94 KStandardItem
* item
= m_items
[index
];
95 m_indexesForItems
.remove(item
);
96 m_items
.removeAt(index
);
100 emit
itemsRemoved(KItemRangeList() << KItemRange(index
, 1));
101 // TODO: no hierarchical items are handled yet
105 KStandardItem
* KStandardItemModel::item(int index
) const
107 if (index
< 0 || index
>= m_items
.count()) {
110 return m_items
[index
];
113 int KStandardItemModel::index(const KStandardItem
* item
) const
115 return m_indexesForItems
.value(item
, -1);
118 int KStandardItemModel::count() const
120 return m_items
.count();
123 QHash
<QByteArray
, QVariant
> KStandardItemModel::data(int index
) const
125 if (index
>= 0 && index
< count()) {
126 const KStandardItem
* item
= m_items
[index
];
131 return QHash
<QByteArray
, QVariant
>();
134 bool KStandardItemModel::setData(int index
, const QHash
<QByteArray
, QVariant
>& values
)
137 if (index
< 0 || index
>= count()) {
144 QMimeData
* KStandardItemModel::createMimeData(const QSet
<int>& indexes
) const
150 int KStandardItemModel::indexForKeyboardSearch(const QString
& text
, int startFromIndex
) const
153 Q_UNUSED(startFromIndex
);
157 bool KStandardItemModel::supportsDropping(int index
) const
163 QString
KStandardItemModel::roleDescription(const QByteArray
& role
) const
169 QList
<QPair
<int, QVariant
> > KStandardItemModel::groups() const
171 QList
<QPair
<int, QVariant
> > groups
;
173 const QByteArray role
= sortRole();
174 bool isFirstGroupValue
= true;
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;
189 #include "kstandarditemmodel.moc"