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>
24class ObjectListModel : public QAbstractListModel
27 ObjectListModel(QObject *parent = 0)
28 : QAbstractListModel(parent)
32 RoleModelData = Qt::UserRole,
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)
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)));
46 beginInsertRows(QModelIndex(), index, index);
47 m_items.insert(index, item);
52 void remove(TYPE* item)
54 int existingIndex = m_items.indexOf(item);
55 if (existingIndex != -1) {
56 beginRemoveRows(QModelIndex(), existingIndex, existingIndex);
57 m_items.removeAt(existingIndex);
62 // from QAbstractItemModel
63 int rowCount(const QModelIndex& = QModelIndex()) const override
65 return m_items.count();
68 QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override
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);
79 QHash<int, QByteArray> roleNames() const
81 QHash<int, QByteArray> roleNames;
82 roleNames.insert(RoleModelData, "modelData");
87 void move(int from, int to)
89 if (from == to) return;
91 if (from >= 0 && from < m_items.size() && to >= 0 && to < m_items.size()) {
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);
103 QList<TYPE*> m_items;
106#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