on daa5708 help sign in
unity8/tests/mocks/Unity/Application/ObjectListModel.h
106 lines 3.2 kB history raw
1/*
2 * Copyright (C) 2014 Canonical, Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License version 3, as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#ifndef OBJECTLISTMODEL_H
18#define OBJECTLISTMODEL_H
20// Qt
21#include <QAbstractListModel>
23template<class TYPE>
24class ObjectListModel : public QAbstractListModel
26public:
27 ObjectListModel(QObject *parent = 0)
28 : QAbstractListModel(parent)
29 {}
31 enum Roles {
32 RoleModelData = Qt::UserRole,
33 };
35 const QList<TYPE*>& list() const { return m_items; }
36 bool contains(TYPE* item) const { return m_items.contains(item); }
38 void insert(uint index, TYPE* item)
39 {
40 index = qMin(index, (uint)m_items.count());
42 int existingIndex = m_items.indexOf(item);
43 if (existingIndex != -1) {
44 move(existingIndex, qMin(index, (uint)(m_items.count()-1)));
45 } else {
46 beginInsertRows(QModelIndex(), index, index);
47 m_items.insert(index, item);
48 endInsertRows();
49 }
50 }
52 void remove(TYPE* item)
53 {
54 int existingIndex = m_items.indexOf(item);
55 if (existingIndex != -1) {
56 beginRemoveRows(QModelIndex(), existingIndex, existingIndex);
57 m_items.removeAt(existingIndex);
58 endRemoveRows();
59 }
60 }
62 // from QAbstractItemModel
63 int rowCount(const QModelIndex& = QModelIndex()) const override
64 {
65 return m_items.count();
66 }
68 QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override
69 {
70 if (index.row() >= 0 && index.row() < m_items.count()) {
71 if (role == RoleModelData) {
72 TYPE *item = m_items.at(index.row());
73 return QVariant::fromValue(item);
74 }
75 }
76 return QVariant();
77 }
79 QHash<int, QByteArray> roleNames() const
80 {
81 QHash<int, QByteArray> roleNames;
82 roleNames.insert(RoleModelData, "modelData");
83 return roleNames;
84 }
86protected:
87 void move(int from, int to)
88 {
89 if (from == to) return;
91 if (from >= 0 && from < m_items.size() && to >= 0 && to < m_items.size()) {
92 QModelIndex parent;
93 /* When moving an item down, the destination index needs to be incremented
94 by one, as explained in the documentation:
95 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
97 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
98 m_items.move(from, to);
99 endMoveRows();
100 }
101 }
103 QList<TYPE*> m_items;
104};
106#endif // OBJECTLISTMODEL_H