]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemmodel.h
Fix selection rect after porting from QFontMetrics::width()
[dolphin.git] / src / kitemviews / kstandarditemmodel.h
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 #ifndef KSTANDARDITEMMODEL_H
21 #define KSTANDARDITEMMODEL_H
22
23 #include "dolphin_export.h"
24 #include "kitemviews/kitemmodelbase.h"
25
26 #include <QHash>
27 #include <QList>
28
29 class KStandardItem;
30
31 /**
32 * @brief Model counterpart for KStandardItemListView.
33 *
34 * Allows to add items to the model in an easy way by the
35 * class KStandardItem.
36 *
37 * @see KStandardItem
38 */
39 class DOLPHIN_EXPORT KStandardItemModel : public KItemModelBase
40 {
41 Q_OBJECT
42
43 public:
44 explicit KStandardItemModel(QObject* parent = nullptr);
45 ~KStandardItemModel() override;
46
47 /**
48 * Inserts the item \a item at the index \a index. If the index
49 * is equal to the number of items of the model, the item
50 * gets appended as last element. KStandardItemModel takes
51 * the ownership of the item. If the index is invalid, the item
52 * gets deleted.
53 */
54 void insertItem(int index, KStandardItem* item);
55
56 /**
57 * Changes the item on the index \a index to \a item.
58 * KStandardItemModel takes the ownership of the item. The
59 * old item gets deleted. If the index is invalid, the item
60 * gets deleted.
61 */
62 void changeItem(int index, KStandardItem* item);
63
64 void removeItem(int index);
65 KStandardItem* item(int index) const;
66 int index(const KStandardItem* item) const;
67
68 /**
69 * Convenience method for insertItem(count(), item).
70 */
71 void appendItem(KStandardItem* item);
72
73 int count() const override;
74 QHash<QByteArray, QVariant> data(int index) const override;
75 bool setData(int index, const QHash<QByteArray, QVariant>& values) override;
76 QMimeData* createMimeData(const KItemSet& indexes) const override;
77 int indexForKeyboardSearch(const QString& text, int startFromIndex = 0) const override;
78 bool supportsDropping(int index) const override;
79 QString roleDescription(const QByteArray& role) const override;
80 QList<QPair<int, QVariant> > groups() const override;
81
82 virtual void clear();
83 protected:
84 /**
85 * Is invoked after an item has been inserted and before the signal
86 * itemsInserted() gets emitted.
87 */
88 virtual void onItemInserted(int index);
89
90 /**
91 * Is invoked after an item or one of its roles has been changed and
92 * before the signal itemsChanged() gets emitted.
93 */
94 virtual void onItemChanged(int index, const QSet<QByteArray>& changedRoles);
95
96 /**
97 * Is invoked after an item has been removed and before the signal
98 * itemsRemoved() gets emitted. The item \a removedItem has already
99 * been removed from the model and will get deleted after the
100 * execution of onItemRemoved().
101 */
102 virtual void onItemRemoved(int index, KStandardItem* removedItem);
103
104 private:
105 QList<KStandardItem*> m_items;
106 QHash<const KStandardItem*, int> m_indexesForItems;
107
108 friend class KStandardItem;
109 friend class KStandardItemModelTest; // For unit testing
110 };
111
112 #endif
113
114