LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
listmodel.cpp
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 #include "listmodel.h"
31 #include <QtDebug>
32 
33 namespace LeechCraft
34 {
35  namespace Util
36  {
37  ListModel::ListModel (const QStringList& headers, QObject *parent)
38  : QAbstractItemModel (parent)
39  , Headers_ (headers)
40  {
41  }
42 
44  {
45  qDeleteAll (Items_);
46  }
47 
48  int ListModel::columnCount (const QModelIndex&) const
49  {
50  return Headers_.size ();
51  }
52 
53  QVariant ListModel::data (const QModelIndex& index, int role) const
54  {
55  if (role != RolePointer)
56  return Items_ [index.row ()]->Data (index.column (), role);
57  else
58  return QVariant::fromValue<void*> (Items_ [index.row ()]);
59  }
60 
61  Qt::ItemFlags ListModel::flags (const QModelIndex&) const
62  {
63  return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
64  }
65 
66  QVariant ListModel::headerData (int section, Qt::Orientation orient, int role) const
67  {
68  if (orient != Qt::Horizontal ||
69  role != Qt::DisplayRole)
70  return QVariant ();
71 
72  return Headers_.at (section);
73  }
74 
75  QModelIndex ListModel::index (int row, int column, const QModelIndex& parent) const
76  {
77  if (parent.isValid () ||
78  !hasIndex (row, column))
79  return QModelIndex ();
80 
81  return createIndex (row, column);
82  }
83 
84  QModelIndex ListModel::parent (const QModelIndex&) const
85  {
86  return QModelIndex ();
87  }
88 
89  int ListModel::rowCount (const QModelIndex& index) const
90  {
91  return index.isValid () ? 0 : Items_.size ();
92  }
93 
94  void ListModel::Insert (ListModelItem *item, int pos)
95  {
96  if (pos == -1)
97  pos = Items_.size ();
98 
99  beginInsertRows (QModelIndex (), pos, pos);
100  Items_.insert (pos, item);
101  endInsertRows ();
102  }
103 
105  {
106  int pos = Items_.indexOf (item);
107  if (pos == -1)
108  {
109  qWarning () << Q_FUNC_INFO
110  << "not found"
111  << item;
112  return;
113  }
114 
115  beginRemoveRows (QModelIndex (), pos, pos);
116  Items_.removeAt (pos);
117  endRemoveRows ();
118  }
119 
120  void ListModel::Remove (int pos)
121  {
122  beginRemoveRows (QModelIndex (), pos, pos);
123  Items_.removeAt (pos);
124  endRemoveRows ();
125  }
126 
128  {
129  int pos = Items_.indexOf (item);
130  if (pos == -1)
131  {
132  qWarning () << Q_FUNC_INFO
133  << "not found"
134  << item;
135  return;
136  }
137 
138  Update (pos);
139  }
140 
141  void ListModel::Update (int pos)
142  {
143  emit dataChanged (index (pos, 0),
144  index (pos, columnCount () - 1));
145  }
146 
148  {
149  Items_.clear ();
150  reset ();
151  }
152 
153  void ListModel::SetHeaders (const QStringList& headers)
154  {
155  Headers_ = headers;
156  }
157 
158  template<>
159  QList<ListModelItem*> ListModel::GetItems () const
160  {
161  return Items_;
162  }
163  };
164 };
165 
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
int rowCount(const QModelIndex &=QModelIndex()) const
Definition: listmodel.cpp:89
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