LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
listmodel.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2013 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #ifndef UTIL_LISTMODEL_H
31 #define UTIL_LISTMODEL_H
32 #include <QAbstractItemModel>
33 #include <QStringList>
34 
35 namespace LeechCraft
36 {
37  namespace Util
38  {
40  {
41  public:
42  virtual ~ListModelItem () { }
43 
44  virtual QVariant Data (int, int) const = 0;
45  };
46 
47  class ListModel : public QAbstractItemModel
48  {
49  Q_OBJECT
50 
51  QList<ListModelItem*> Items_;
52  QStringList Headers_;
53  public:
54  enum Roles
55  {
56  RolePointer = Qt::UserRole + 25
57  };
58 
59  ListModel (const QStringList& = QStringList (), QObject* = 0);
60  virtual ~ListModel ();
61 
62  int columnCount (const QModelIndex& = QModelIndex ()) const;
63  QVariant data (const QModelIndex&, int = Qt::DisplayRole) const;
64  Qt::ItemFlags flags (const QModelIndex&) const;
65  QVariant headerData (int, Qt::Orientation, int = Qt::DisplayRole) const;
66  QModelIndex index (int, int, const QModelIndex& = QModelIndex ()) const;
67  QModelIndex parent (const QModelIndex&) const;
68  int rowCount (const QModelIndex& = QModelIndex ()) const;
69 
70  void Insert (ListModelItem*, int = -1);
71  void Remove (ListModelItem*);
72  void Remove (int);
73  void Update (ListModelItem*);
74  void Update (int);
75 
76  void Clear ();
77 
78  void SetHeaders (const QStringList&);
79 
80  template<typename T>
81  QList<T*> GetItems () const
82  {
83  QList<T*> result;
84  Q_FOREACH (ListModelItem *item, Items_)
85  result << static_cast<T*> (item);
86  return result;
87  }
88 
89  template<typename T>
90  T* GetItem (const QModelIndex& index) const
91  {
92  return GetItem<T> (index.row ());
93  }
94 
95  template<typename T>
96  T* GetItem (int row) const
97  {
98  return static_cast<T*> (Items_.at (row));
99  }
100  };
101 
102  template<> QList<ListModelItem*> ListModel::GetItems () const;
103  };
104 };
105 
106 #endif
107 
ListModel(const QStringList &=QStringList(), QObject *=0)
Definition: listmodel.cpp:37
void Remove(ListModelItem *)
Definition: listmodel.cpp:104
void SetHeaders(const QStringList &)
Definition: listmodel.cpp:153
void Update(ListModelItem *)
Definition: listmodel.cpp:127
Qt::ItemFlags flags(const QModelIndex &) const
Definition: listmodel.cpp:61
int columnCount(const QModelIndex &=QModelIndex()) const
Definition: listmodel.cpp:48
void Insert(ListModelItem *, int=-1)
Definition: listmodel.cpp:94
QModelIndex index(int, int, const QModelIndex &=QModelIndex()) const
Definition: listmodel.cpp:75
QList< T * > GetItems() const
Definition: listmodel.h:81
T * GetItem(const QModelIndex &index) const
Definition: listmodel.h:90
int rowCount(const QModelIndex &=QModelIndex()) const
Definition: listmodel.cpp:89
T * GetItem(int row) const
Definition: listmodel.h:96
virtual QVariant Data(int, int) const =0
QModelIndex parent(const QModelIndex &) const
Definition: listmodel.cpp:84
QVariant headerData(int, Qt::Orientation, int=Qt::DisplayRole) const
Definition: listmodel.cpp:66
QVariant data(const QModelIndex &, int=Qt::DisplayRole) const
Definition: listmodel.cpp:53