2 * Copyright (C) 2014 Canonical, Ltd.
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.
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.
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/>.
17#ifndef OBJECTLISTMODEL_H
18#define OBJECTLISTMODEL_H
21#include <QAbstractListModel>
26class ObjectListModel : public QAbstractListModel
29 ObjectListModel(QObject *parent = 0)
30 : QAbstractListModel(parent)
34 RoleModelData = Qt::UserRole,
37 const QList<TYPE*>& list() const { return m_items; }
38 bool contains(TYPE* item) const { return m_items.contains(item); }
40 void insert(uint index, TYPE* item)
42 index = qMin(index, (uint)m_items.count());
44 int existingIndex = m_items.indexOf(item);
45 if (existingIndex != -1) {
46 move(existingIndex, qMin(index, (uint)(m_items.count()-1)));
48 beginInsertRows(QModelIndex(), index, index);
49 m_items.insert(index, item);
54 void remove(TYPE* item)
56 int existingIndex = m_items.indexOf(item);
57 if (existingIndex != -1) {
58 beginRemoveRows(QModelIndex(), existingIndex, existingIndex);
59 m_items.removeAt(existingIndex);
64 // from QAbstractItemModel
65 int rowCount(const QModelIndex& = QModelIndex()) const override
67 return m_items.count();
70 QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override
72 if (index.row() >= 0 && index.row() < m_items.count()) {
73 if (role == RoleModelData) {
74 TYPE *item = m_items.at(index.row());
75 return QVariant::fromValue(item);
81 QHash<int, QByteArray> roleNames() const override
83 QHash<int, QByteArray> roleNames;
84 roleNames.insert(RoleModelData, "modelData");
89 void move(int from, int to)
91 if (from == to) return;
93 if (from >= 0 && from < m_items.size() && to >= 0 && to < m_items.size()) {
95 /* When moving an item down, the destination index needs to be incremented
96 by one, as explained in the documentation:
97 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
99 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
100 m_items.move(from, to);
105 QList<TYPE*> m_items;
108#endif // OBJECTLISTMODEL_H
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list