]>
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 (item
&& !m_indexesForItems
.contains(item
)) {
43 m_items
.insert(index
, item
);
44 m_indexesForItems
.insert(item
, index
);
45 // TODO: no hierarchical items are handled yet
47 onItemInserted(index
);
48 emit
itemsInserted(KItemRangeList() << KItemRange(index
, 1));
52 void KStandardItemModel::changeItem(int index
, KStandardItem
* item
)
54 if (item
&& index
>= 0 && index
< count()) {
57 QSet
<QByteArray
> changedRoles
;
59 KStandardItem
* oldItem
= m_items
[index
];
60 const QHash
<QByteArray
, QVariant
> oldData
= oldItem
->data();
61 const QHash
<QByteArray
, QVariant
> newData
= item
->data();
63 // Determine which roles have been changed
64 QHashIterator
<QByteArray
, QVariant
> it(oldData
);
65 while (it
.hasNext()) {
67 const QByteArray role
= it
.key();
68 const QVariant oldValue
= it
.value();
69 if (newData
.contains(role
) && newData
.value(role
) != oldValue
) {
70 changedRoles
.insert(role
);
74 m_indexesForItems
.remove(oldItem
);
78 m_items
[index
] = item
;
79 m_indexesForItems
.insert(item
, index
);
81 onItemChanged(index
, changedRoles
);
82 emit
itemsChanged(KItemRangeList() << KItemRange(index
, 1), changedRoles
);
84 kWarning() << "No item available to replace on the given index" << index
;
90 void KStandardItemModel::removeItem(int index
)
92 if (index
>= 0 && index
< count()) {
93 KStandardItem
* item
= m_items
[index
];
94 m_indexesForItems
.remove(item
);
95 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 void KStandardItemModel::appendItem(KStandardItem
*item
)
120 insertItem(m_items
.count(), item
);
123 int KStandardItemModel::count() const
125 return m_items
.count();
128 QHash
<QByteArray
, QVariant
> KStandardItemModel::data(int index
) const
130 if (index
>= 0 && index
< count()) {
131 const KStandardItem
* item
= m_items
[index
];
136 return QHash
<QByteArray
, QVariant
>();
139 bool KStandardItemModel::setData(int index
, const QHash
<QByteArray
, QVariant
>& values
)
142 if (index
< 0 || index
>= count()) {
149 QMimeData
* KStandardItemModel::createMimeData(const QSet
<int>& indexes
) const
155 int KStandardItemModel::indexForKeyboardSearch(const QString
& text
, int startFromIndex
) const
158 Q_UNUSED(startFromIndex
);
162 bool KStandardItemModel::supportsDropping(int index
) const
168 QString
KStandardItemModel::roleDescription(const QByteArray
& role
) const
174 QList
<QPair
<int, QVariant
> > KStandardItemModel::groups() const
176 QList
<QPair
<int, QVariant
> > groups
;
178 const QByteArray role
= sortRole();
179 bool isFirstGroupValue
= true;
181 const int maxIndex
= count() - 1;
182 for (int i
= 0; i
<= maxIndex
; ++i
) {
183 const QString newGroupValue
= m_items
.at(i
)->dataValue(role
).toString();
184 if (newGroupValue
!= groupValue
|| isFirstGroupValue
) {
185 groupValue
= newGroupValue
;
186 groups
.append(QPair
<int, QVariant
>(i
, newGroupValue
));
187 isFirstGroupValue
= false;
194 void KStandardItemModel::onItemInserted(int index
)
199 void KStandardItemModel::onItemChanged(int index
, const QSet
<QByteArray
>& changedRoles
)
204 void KStandardItemModel::onItemRemoved(int index
)
210 #include "kstandarditemmodel.moc"