| 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 |
*/ |
| 16 |
|
| 17 |
#ifndef OBJECTLISTMODEL_H |
| 18 |
#define OBJECTLISTMODEL_H |
| 19 |
|
| 20 |
// Qt |
| 21 |
#include <QAbstractListModel> |
| 22 |
|
| 23 |
#include <QDebug> |
| 24 |
|
| 25 |
template<class TYPE> |
| 26 |
class ObjectListModel : public QAbstractListModel |
| 27 |
{ |
| 28 |
public: |
| 29 |
ObjectListModel(QObject *parent = 0) |
| 30 |
: QAbstractListModel(parent) |
| 31 |
{} |
| 32 |
|
| 33 |
enum Roles { |
| 34 |
RoleModelData = Qt::UserRole, |
| 35 |
}; |
| 36 |
|
| 37 |
const QList<TYPE*>& list() const { return m_items; } |
| 38 |
bool contains(TYPE* item) const { return m_items.contains(item); } |
| 39 |
|
| 40 |
void insert(uint index, TYPE* item) |
| 41 |
{ |
| 42 |
index = qMin(index, (uint)m_items.count()); |
| 43 |
|
| 44 |
int existingIndex = m_items.indexOf(item); |
| 45 |
if (existingIndex != -1) { |
| 46 |
move(existingIndex, qMin(index, (uint)(m_items.count()-1))); |
| 47 |
} else { |
| 48 |
beginInsertRows(QModelIndex(), index, index); |
| 49 |
m_items.insert(index, item); |
| 50 |
endInsertRows(); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
void remove(TYPE* item) |
| 55 |
{ |
| 56 |
int existingIndex = m_items.indexOf(item); |
| 57 |
if (existingIndex != -1) { |
| 58 |
beginRemoveRows(QModelIndex(), existingIndex, existingIndex); |
| 59 |
m_items.removeAt(existingIndex); |
| 60 |
endRemoveRows(); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// from QAbstractItemModel |
| 65 |
int rowCount(const QModelIndex& = QModelIndex()) const override |
| 66 |
{ |
| 67 |
return m_items.count(); |
| 68 |
} |
| 69 |
|
| 70 |
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override |
| 71 |
{ |
| 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); |
| 76 |
} |
| 77 |
} |
| 78 |
return QVariant(); |
| 79 |
} |
| 80 |
|
| 81 |
QHash<int, QByteArray> roleNames() const override |
| 82 |
{ |
| 83 |
QHash<int, QByteArray> roleNames; |
| 84 |
roleNames.insert(RoleModelData, "modelData"); |
| 85 |
return roleNames; |
| 86 |
} |
| 87 |
|
| 88 |
protected: |
| 89 |
void move(int from, int to) |
| 90 |
{ |
| 91 |
if (from == to) return; |
| 92 |
|
| 93 |
if (from >= 0 && from < m_items.size() && to >= 0 && to < m_items.size()) { |
| 94 |
QModelIndex parent; |
| 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 */ |
| 98 |
|
| 99 |
beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0)); |
| 100 |
m_items.move(from, to); |
| 101 |
endMoveRows(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
QList<TYPE*> m_items; |
| 106 |
}; |
| 107 |
|
| 108 |
#endif // OBJECTLISTMODEL_H |