2
* Author: Christophe Dumez <dchris@gmail.com>
3
* License: Public domain (No attribution required)
4
* Website: http://cdumez.blogspot.com/
11
ListModel::ListModel(ListItem* prototype, QObject *parent) :
12
QAbstractListModel(parent), m_prototype(prototype)
16
int ListModel::rowCount(const QModelIndex &parent) const
22
QVariant ListModel::data(const QModelIndex &index, int role) const
24
if(index.row() < 0 || index.row() >= m_list.size())
26
return m_list.at(index.row())->data(role);
29
bool ListModel::setData(const QModelIndex & index, const QVariant & value, int role){
30
if(index.row() < 0 || index.row() >= m_list.size())
32
m_list.at(index.row())->setData(value, role);
36
Qt::ItemFlags ListModel::flags(const QModelIndex &index) const
39
return (QAbstractListModel::flags(index)|Qt::ItemIsSelectable);
41
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
44
ListModel::~ListModel() {
49
void ListModel::appendRow(ListItem *item)
51
appendRows(QList<ListItem*>() << item);
54
void ListModel::appendRows(const QList<ListItem *> &items)
56
beginInsertRows(QModelIndex(), rowCount(), rowCount()+items.size()-1);
57
foreach(ListItem *item, items) {
58
connect(item, SIGNAL(dataChanged()), SLOT(handleItemChange()));
64
void ListModel::insertRow(int row, ListItem *item)
66
beginInsertRows(QModelIndex(), row, row);
67
connect(item, SIGNAL(dataChanged()), SLOT(handleItemChange()));
68
m_list.insert(row, item);
72
void ListModel::handleItemChange()
74
ListItem* item = static_cast<ListItem*>(sender());
75
QModelIndex index = indexFromItem(item);
77
emit dataChanged(index, index);
81
ListItem * ListModel::find(const QString &id) const
83
foreach(ListItem* item, m_list) {
84
if(item->id() == id) return item;
89
QModelIndex ListModel::indexFromItem(const ListItem *item) const
92
for(int row=0; row<m_list.size(); ++row) {
93
if(m_list.at(row) == item) return index(row);
98
void ListModel::clear()
104
bool ListModel::removeRow(int row, const QModelIndex &parent)
107
if(row < 0 || row >= m_list.size()) return false;
108
beginRemoveRows(QModelIndex(), row, row);
109
delete m_list.takeAt(row);
114
bool ListModel::removeRows(int row, int count, const QModelIndex &parent)
117
if(row < 0 || (row+count) >= m_list.size()) return false;
118
beginRemoveRows(QModelIndex(), row, row+count-1);
119
for(int i=0; i<count; ++i) {
120
delete m_list.takeAt(row);
126
ListItem * ListModel::takeRow(int row)
128
beginRemoveRows(QModelIndex(), row, row);
129
ListItem* item = m_list.takeAt(row);
134
QHash<int, QByteArray> ListModel::roleNames() const{
135
return m_prototype->roleNames();
138
QVariant ListModel::get(int row)
140
ListItem * item = m_list.at(row);
141
QMap<QString, QVariant> itemData;
142
QHashIterator<int, QByteArray> hashItr(item->roleNames());
143
while(hashItr.hasNext()){
145
itemData.insert(hashItr.value(),item->data(hashItr.key()).toString());
148
return QVariant(itemData);
151
void ListModel::setProperty(int idx, const QString& property, const QVariant& value)
153
if (rowCount() == 0 || idx >= rowCount() || idx < 0) {
157
QHashIterator<int, QByteArray> hashItr(roleNames());
159
if ( hashItr.findNext(property.toUtf8()) ){
160
m_list[idx]->setData(value, hashItr.key());