Guest User

listmodel.h

a guest
Dec 11th, 2010
3,206
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. #ifndef LISTMODEL_H
  9. #define LISTMODEL_H
  10.  
  11. #include <QAbstractListModel>
  12. #include <QList>
  13. #include <QVariant>
  14.  
  15. class ListItem: public QObject {
  16.   Q_OBJECT
  17.  
  18. public:
  19.   ListItem(QObject* parent = 0) : QObject(parent) {}
  20.   virtual ~ListItem() {}
  21.   virtual QString id() const = 0;
  22.   virtual QVariant data(int role) const = 0;
  23.   virtual QHash<int, QByteArray> roleNames() const = 0;
  24.  
  25. signals:
  26.   void dataChanged();
  27. };
  28.  
  29. class ListModel : public QAbstractListModel
  30. {
  31.   Q_OBJECT
  32.  
  33. public:
  34.   explicit ListModel(ListItem* prototype, QObject* parent = 0);
  35.   ~ListModel();
  36.   int rowCount(const QModelIndex &parent = QModelIndex()) const;
  37.   QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
  38.   void appendRow(ListItem* item);
  39.   void appendRows(const QList<ListItem*> &items);
  40.   void insertRow(int row, ListItem* item);
  41.   bool removeRow(int row, const QModelIndex &parent = QModelIndex());
  42.   bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
  43.   ListItem* takeRow(int row);
  44.   ListItem* find(const QString &id) const;
  45.   QModelIndex indexFromItem( const ListItem* item) const;
  46.   void clear();
  47.  
  48. private slots:
  49.   void handleItemChange();
  50.  
  51. private:
  52.   ListItem* m_prototype;
  53.   QList<ListItem*> m_list;
  54. };
  55.  
  56. #endif // LISTMODEL_H
RAW Paste Data