on ec6ad0c help sign in
unity8/plugins/Unity/listmodelwrapper.h
147 lines 4.6 kB history raw
1/*
2 * Copyright (C) 2011 Canonical, Ltd.
3 *
4 * Authors:
5 * Florian Boucault <florian.boucault@canonical.com>
6 *
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.
10 *
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.
15 *
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/>.
18 */
20#ifndef LISTMODELWRAPPER_H
21#define LISTMODELWRAPPER_H
23// Qt
24#include <QAbstractListModel>
25#include <QList>
27#include <sigc++/signal.h>
28#include <vector>
30template <class WrapperItem, class ItemClass>
31class ListModelWrapper : public QAbstractListModel
33public:
34 explicit ListModelWrapper(const std::vector<ItemClass>& list,
35 sigc::signal<void, ItemClass> itemAddedSignal,
36 sigc::signal<void, ItemClass> itemRemovedSignal,
37 QObject *parent = 0);
38 ~ListModelWrapper();
40 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
41 int rowCount(const QModelIndex& parent = QModelIndex()) const;
43 const QList<WrapperItem*>& rawList() const;
45private:
46 void onItemAdded(ItemClass item);
47 void onItemRemoved(ItemClass item);
48 void onItemChanged(ItemClass item);
50 QList<WrapperItem*> m_list;
51 QMap<ItemClass, int> m_indexes;
53 void addItem(ItemClass item, int index);
54 void removeItem(ItemClass item, int index);
55};
58template <class WrapperItem, class ItemClass>
59ListModelWrapper<WrapperItem, ItemClass>::ListModelWrapper(const std::vector<ItemClass>& list,
60 sigc::signal<void, ItemClass> itemAddedSignal,
61 sigc::signal<void, ItemClass> itemRemovedSignal,
62 QObject *parent) :
63 QAbstractListModel(parent)
65 for (unsigned int i=0; i<list.size(); i++) {
66 addItem(list[i], i);
67 }
68 itemAddedSignal.connect(sigc::mem_fun(this, &ListModelWrapper::onItemAdded));
69 itemRemovedSignal.connect(sigc::mem_fun(this, &ListModelWrapper::onItemRemoved));
72template <class WrapperItem, class ItemClass>
73ListModelWrapper<WrapperItem, ItemClass>::~ListModelWrapper()
75 m_indexes.clear();
76 while (!m_list.isEmpty()) {
77 delete m_list.takeFirst();
78 }
81template <class WrapperItem, class ItemClass>
82int ListModelWrapper<WrapperItem, ItemClass>::rowCount(const QModelIndex& parent) const
84 Q_UNUSED(parent)
86 return m_list.count();
89template <class WrapperItem, class ItemClass>
90const QList<WrapperItem*>& ListModelWrapper<WrapperItem, ItemClass>::rawList() const
92 return m_list;
95template <class WrapperItem, class ItemClass>
96QVariant ListModelWrapper<WrapperItem, ItemClass>::data(const QModelIndex& index, int) const
98 if (!index.isValid()) {
99 return QVariant();
100 }
102 WrapperItem* item = m_list.at(index.row());
104 return QVariant::fromValue(item);
107template <class WrapperItem, class ItemClass>
108void ListModelWrapper<WrapperItem, ItemClass>::onItemAdded(ItemClass item)
110 /* FIXME: figure out actual index of item; for now items are appended */
111 int index = m_list.count();
112 beginInsertRows(QModelIndex(), index, index);
113 addItem(item, index);
114 endInsertRows();
117template <class WrapperItem, class ItemClass>
118void ListModelWrapper<WrapperItem, ItemClass>::onItemRemoved(ItemClass item)
120 int index = m_indexes[item];
121 beginInsertRows(QModelIndex(), index, index);
122 removeItem(item, index);
123 endInsertRows();
126template <class WrapperItem, class ItemClass>
127void ListModelWrapper<WrapperItem, ItemClass>::onItemChanged(ItemClass item)
129 QModelIndex itemIndex = index(m_indexes[item]);
130 Q_EMIT dataChanged(itemIndex, itemIndex);
133template <class WrapperItem, class ItemClass>
134void ListModelWrapper<WrapperItem, ItemClass>::addItem(ItemClass item, int index)
136 m_indexes.insert(item, index);
137 m_list.insert(index, new WrapperItem(item));
140template <class WrapperItem, class ItemClass>
141void ListModelWrapper<WrapperItem, ItemClass>::removeItem(ItemClass item, int index)
143 m_indexes.remove(item);
144 delete m_list.takeAt(index);
147#endif // LISTMODELWRAPPER_H