Guest User

listmodel.h

a guest
Dec 11th, 2010
5,162
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Author: Christophe Dumez <dchris@gmail.com>
  3.  * License: Public domain (No attribution required)
  4.  * Website: http://cdumez.blogspot.com/
  5.  * Version: 1.0
  6.  */
  7.  
  8. #include "listmodel.h"
  9.  
  10. ListModel::ListModel(ListItem* prototype, QObject *parent) :
  11.     QAbstractListModel(parent), m_prototype(prototype)
  12. {
  13.   setRoleNames(m_prototype->roleNames());
  14. }
  15.  
  16. int ListModel::rowCount(const QModelIndex &parent) const
  17. {
  18.   Q_UNUSED(parent);
  19.   return m_list.size();
  20. }
  21.  
  22. QVariant ListModel::data(const QModelIndex &index, int role) const
  23. {
  24.   if(index.row() < 0 || index.row() >= m_list.size())
  25.     return QVariant();
  26.   return m_list.at(index.row())->data(role);
  27. }
  28.  
  29. ListModel::~ListModel() {
  30.   delete m_prototype;
  31.   clear();
  32. }
  33.  
  34. void ListModel::appendRow(ListItem *item)
  35. {
  36.   appendRows(QList<ListItem*>() << item);
  37. }
  38.  
  39. void ListModel::appendRows(const QList<ListItem *> &items)
  40. {
  41.   beginInsertRows(QModelIndex(), rowCount(), rowCount()+items.size()-1);
  42.   foreach(ListItem *item, items) {
  43.     connect(item, SIGNAL(dataChanged()), SLOT(handleItemChange()));
  44.     m_list.append(item);
  45.   }
  46.   endInsertRows();
  47. }
  48.  
  49. void ListModel::insertRow(int row, ListItem *item)
  50. {
  51.   beginInsertRows(QModelIndex(), row, row);
  52.   connect(item, SIGNAL(dataChanged()), SLOT(handleItemChange()));
  53.   m_list.insert(row, item);
  54.   endInsertRows();
  55. }
  56.  
  57. void ListModel::handleItemChange()
  58. {
  59.   ListItem* item = static_cast<ListItem*>(sender());
  60.   QModelIndex index = indexFromItem(item);
  61.   if(index.isValid())
  62.     emit dataChanged(index, index);
  63. }
  64.  
  65. ListItem * ListModel::find(const QString &id) const
  66. {
  67.   foreach(ListItem* item, m_list) {
  68.     if(item->id() == id) return item;
  69.   }
  70.   return 0;
  71. }
  72.  
  73. QModelIndex ListModel::indexFromItem(const ListItem *item) const
  74. {
  75.   Q_ASSERT(item);
  76.   for(int row=0; row<m_list.size(); ++row) {
  77.     if(m_list.at(row) == item) return index(row);
  78.   }
  79.   return QModelIndex();
  80. }
  81.  
  82. void ListModel::clear()
  83. {
  84.   qDeleteAll(m_list);
  85.   m_list.clear();
  86. }
  87.  
  88. bool ListModel::removeRow(int row, const QModelIndex &parent)
  89. {
  90.   Q_UNUSED(parent);
  91.   if(row < 0 || row >= m_list.size()) return false;
  92.   beginRemoveRows(QModelIndex(), row, row);
  93.   delete m_list.takeAt(row);
  94.   endRemoveRows();
  95.   return true;
  96. }
  97.  
  98. bool ListModel::removeRows(int row, int count, const QModelIndex &parent)
  99. {
  100.   Q_UNUSED(parent);
  101.   if(row < 0 || (row+count) >= m_list.size()) return false;
  102.   beginRemoveRows(QModelIndex(), row, row+count-1);
  103.   for(int i=0; i<count; ++i) {
  104.     delete m_list.takeAt(row);
  105.   }
  106.   endRemoveRows();
  107.   return true;
  108. }
  109.  
  110. ListItem * ListModel::takeRow(int row)
  111. {
  112.   beginRemoveRows(QModelIndex(), row, row);
  113.   ListItem* item = m_list.takeAt(row);
  114.   endRemoveRows();
  115.   return item;
  116. }
RAW Paste Data