@@ -0,0 +1,161 @@
2+ * Copyright (C) 2011 Canonical, Ltd.
5+ * Florian Boucault <florian.boucault@canonical.com>
7+ * This program is free software; you can redistribute it and/or modify
8+ * it under the terms of the GNU General Public License as published by
9+ * the Free Software Foundation; version 3.
11+ * This program is distributed in the hope that it will be useful,
12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+ * GNU General Public License for more details.
16+ * You should have received a copy of the GNU General Public License
17+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
20+#ifndef LISTMODELWRAPPER_H
21+#define LISTMODELWRAPPER_H
24+#include <QAbstractListModel>
27+#include <sigc++/signal.h>
30+template <class WrapperItem, class ItemClass>
31+class ListModelWrapper : public QAbstractListModel
36+ explicit ListModelWrapper(const std::vector<ItemClass>& list,
37+ sigc::signal<void, ItemClass> itemAddedSignal,
38+ sigc::signal<void, ItemClass> itemRemovedSignal,
39+ QObject *parent = 0);
46+ QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
47+ int rowCount(const QModelIndex& parent = QModelIndex()) const;
49+ const QList<WrapperItem*>& rawList() const;
52+ void onItemAdded(ItemClass item);
53+ void onItemRemoved(ItemClass item);
54+ void onItemChanged(ItemClass item);
56+ QList<WrapperItem*> m_list;
57+ QMap<ItemClass, int> m_indexes;
59+ void addItem(ItemClass item, int index);
60+ void removeItem(ItemClass item, int index);
64+template <class WrapperItem, class ItemClass>
65+ListModelWrapper<WrapperItem, ItemClass>::ListModelWrapper(const std::vector<ItemClass>& list,
66+ sigc::signal<void, ItemClass> itemAddedSignal,
67+ sigc::signal<void, ItemClass> itemRemovedSignal,
69+ QAbstractListModel(parent)
71+// QHash<int, QByteArray> roles;
72+// roles[ListModelWrapper::RoleItem] = "item";
73+// setRoleNames(roles);
75+ for (unsigned int i=0; i<list.size(); i++) {
78+ itemAddedSignal.connect(sigc::mem_fun(this, &ListModelWrapper::onItemAdded));
79+ itemRemovedSignal.connect(sigc::mem_fun(this, &ListModelWrapper::onItemRemoved));
82+template <class WrapperItem, class ItemClass>
83+ListModelWrapper<WrapperItem, ItemClass>::~ListModelWrapper()
86+ while (!m_list.isEmpty()) {
87+ delete m_list.takeFirst();
91+template <class WrapperItem, class ItemClass>
92+int ListModelWrapper<WrapperItem, ItemClass>::rowCount(const QModelIndex& parent) const
96+ return m_list.count();
99+template <class WrapperItem, class ItemClass>
100+const QList<WrapperItem*>& ListModelWrapper<WrapperItem, ItemClass>::rawList() const
105+template <class WrapperItem, class ItemClass>
106+QVariant ListModelWrapper<WrapperItem, ItemClass>::data(const QModelIndex& index, int) const
108+ if (!index.isValid()) {
112+ WrapperItem* item = m_list.at(index.row());
114+// if (role == ListModelWrapper::RoleItem) {
115+ return QVariant::fromValue(item);
117+// return QVariant();
121+template <class WrapperItem, class ItemClass>
122+void ListModelWrapper<WrapperItem, ItemClass>::onItemAdded(ItemClass item)
124+ /* FIXME: figure out actual index of item; for now items are appended */
125+ int index = m_list.count();
126+ beginInsertRows(QModelIndex(), index, index);
127+ addItem(item, index);
131+template <class WrapperItem, class ItemClass>
132+void ListModelWrapper<WrapperItem, ItemClass>::onItemRemoved(ItemClass item)
134+ int index = m_indexes[item];
135+ beginInsertRows(QModelIndex(), index, index);
136+ removeItem(item, index);
140+template <class WrapperItem, class ItemClass>
141+void ListModelWrapper<WrapperItem, ItemClass>::onItemChanged(ItemClass item)
143+ QModelIndex itemIndex = index(m_indexes[item]);
144+ Q_EMIT dataChanged(itemIndex, itemIndex);
147+template <class WrapperItem, class ItemClass>
148+void ListModelWrapper<WrapperItem, ItemClass>::addItem(ItemClass item, int index)
150+ m_indexes.insert(item, index);
151+ m_list.insert(index, new WrapperItem(item));
154+template <class WrapperItem, class ItemClass>
155+void ListModelWrapper<WrapperItem, ItemClass>::removeItem(ItemClass item, int index)
157+ m_indexes.remove(item);
158+ delete m_list.takeAt(index);
161+#endif // LISTMODELWRAPPER_H