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>
30template <class WrapperItem, class ItemClass>
31class ListModelWrapper : public QAbstractListModel
34 explicit ListModelWrapper(const std::vector<ItemClass>& list,
35 sigc::signal<void, ItemClass> itemAddedSignal,
36 sigc::signal<void, ItemClass> itemRemovedSignal,
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;
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);
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,
63 QAbstractListModel(parent)
65 for (unsigned int i=0; i<list.size(); i++) {
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()
76 while (!m_list.isEmpty()) {
77 delete m_list.takeFirst();
81template <class WrapperItem, class ItemClass>
82int ListModelWrapper<WrapperItem, ItemClass>::rowCount(const QModelIndex& parent) const
86 return m_list.count();
89template <class WrapperItem, class ItemClass>
90const QList<WrapperItem*>& ListModelWrapper<WrapperItem, ItemClass>::rawList() const
95template <class WrapperItem, class ItemClass>
96QVariant ListModelWrapper<WrapperItem, ItemClass>::data(const QModelIndex& index, int) const
98 if (!index.isValid()) {
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);
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);
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
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