123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #include "listmodel.h"
- #include <QDebug>
- ListModel::ListModel(ListItem* prototype, QObject *parent) : QAbstractListModel(parent), m_prototype(prototype)
- {
- setRoleNames(m_prototype->roleNames());
- }
- int ListModel::rowCount(const QModelIndex &parent) const
- {
- Q_UNUSED(parent);
- return m_list.size();
- }
- QVariant ListModel::data(const QModelIndex &index, int role) const
- {
- if(index.row() < 0 || index.row() >= m_list.size())
- return QVariant();
- return m_list.at(index.row())->data(role);
- }
- ListModel::~ListModel() {
- delete m_prototype;
- clear();
- }
- void ListModel::appendRow(ListItem *item)
- {
- appendRows(QList<ListItem*>() << item);
- }
- void ListModel::appendRows(const QList<ListItem *> &items)
- {
- beginInsertRows(QModelIndex(), rowCount(), rowCount()+items.size()-1);
- foreach(ListItem *item, items) {
- connect(item, SIGNAL(dataChanged()), SLOT(handleItemChange()));
- m_list.append(item);
- }
- endInsertRows();
- }
- void ListModel::insertRow(int row, ListItem *item)
- {
- beginInsertRows(QModelIndex(), row, row);
- connect(item, SIGNAL(dataChanged()), SLOT(handleItemChange()));
- m_list.insert(row, item);
- endInsertRows();
- }
- void ListModel::handleItemChange()
- {
- ListItem* item = static_cast<ListItem*>(sender());
- QModelIndex index = indexFromItem(item);
- if(index.isValid())
- emit dataChanged(index, index);
- }
- ListItem * ListModel::find(const QString &id) const
- {
- foreach(ListItem* item, m_list) {
- if(item->id() == id) return item;
- }
- return 0;
- }
- ListItem * ListModel::find(const QString &id, int &row) const
- {
- row = 0;
- foreach(ListItem* item, m_list) {
- if(item->id() == id) { /*qDebug()<<item->id()<<" - "<<row;*/ return item;}
- row++;
- }
- row = -1;
- return 0;
- }
- QModelIndex ListModel::indexFromItem(const ListItem *item) const
- {
- Q_ASSERT(item);
- for(int row=0; row<m_list.size(); ++row) {
- if(m_list.at(row) == item) return index(row);
- }
- return QModelIndex();
- }
- void ListModel::clear()
- {
- qDeleteAll(m_list);
- m_list.clear();
- }
- bool ListModel::removeRow(int row, const QModelIndex &parent)
- {
- Q_UNUSED(parent);
- //qDebug() << "removeRow: m_list.size()="<<m_list.size();
- if(row < 0 || row >= m_list.size()) return false;
- beginRemoveRows(QModelIndex(), row, row);
- delete m_list.takeAt(row);
- endRemoveRows();
- return true;
- }
- bool ListModel::removeRows(int row, int count, const QModelIndex &parent)
- {
- Q_UNUSED(parent);
- //qDebug() << "removeRows: m_list.size()="<<m_list.size();
- if(row < 0 || (row+count) > m_list.size()) return false; /* !!! */
- beginRemoveRows(QModelIndex(), row, row+count-1);
- for(int i=0; i<count; ++i) {
- delete m_list.takeAt(row);
- }
- endRemoveRows();
- return true;
- }
- ListItem * ListModel::takeRow(int row)
- {
- beginRemoveRows(QModelIndex(), row, row);
- ListItem* item = m_list.takeAt(row);
- endRemoveRows();
- return item;
- }
- bool ListModel::takeRows(int row, int count, const QModelIndex &parent)
- {
- Q_UNUSED(parent);
- if(row < 0 || (row+count) > m_list.size()) return false; /* !!! */
- beginRemoveRows(QModelIndex(), row, row+count-1);
- for(int i=0; i<count; ++i) {
- m_list.takeAt(row);
- }
- endRemoveRows();
- return true;
- }
- ListItem* ListModel::value( int row )
- {
- ListItem* item = m_list.value(row);
- return item;
- }
|