forked from lemirep/Models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListModel.cpp
468 lines (423 loc) · 12.3 KB
/
ListModel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/****************************************************************************
**
** Copyright (C) Paul Lemire, Tepee3DTeam and/or its subsidiary(-ies).
** Contact: paul.lemire@epitech.eu
** Contact: tepee3d_2014@labeip.epitech.eu
**
** This file is part of the Tepee3D project
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
****************************************************************************/
#include "ListModel.h"
// DEBUG
#include <QDebug>
/*!
* \namespace Models
*
* \brief The Models namespace contains all classes use for Data Model
* encapsulation within the application.
*
* \inmodule Tepee3D
*
*
*/
/*!
* \class Models::ListItem
*
* \code
* #include <ListItem.h>
* \endcode
*
* \brief The basic element Models::ListModel are composed of.
*
* The basic element Models::ListModel are composed of. It gives a representation
* of the data it contains throught the use of data retrieved by matching role names.
* Your model items should inherit from this class and implement the various methods to reflect
* what your item contains.
*
* \sa Models::ListModel
*
* \inmodule Tepee3D
*/
/*!
* \fn Models::ListItem::ListItem(QObject *parent)
*
* Constructs a new model item with the optionnal \a parent argument/
*/
/*!
* \fn Models::ListItem::~ListItem()
*
* Destroys a ListItem instance.
*/
/*!
* \fn int Models::ListItem::id() const
*
* Returns the id of the item.
*/
/*!
* \fn QVariant Models::ListItem::data(int role) const
*
* Returns the data associated to a given \a role.
*/
/*!
* \fn bool Models::ListItem::setData(int role, const QVariant& value)
*
* If you want your model to be editable, implement this method in your base class,
* updating the value of the field identified by \a role with \a value;
*/
/*!
* \fn QHash<int, QByteArray> Models::ListItem::roleNames() const
*
* Returns a QHash containing the roleNames and a corresponding role identifier.
*/
/*!
* \fn void Models::ListItem::triggerItemUpdate()
*
* Triggers an update of the data model to reflect the changes of the ListItem.
*/
/*!
* \fn void Models::ListItem::dataChanged()
*
* Triggered when an update of the ListItem is requested.
*/
/*!
* \class Models::ListModel
*
* \code
* #include <ListModel.h>
* \endcode
*
* \brief The Models::ListModel class is a utility model class
*
* The Models::ListModel class provides an easy way to create a C++ model
* and later expose it to the Qml View.
*
* \inmodule Tepee3D
*
* \sa Models::SubListedListModel
*/
/*!
* Instanciates a new ListModel and sets the list row prototype to \a prototype.
* The \a parent parameter is optional.
*
*/
Models::ListModel::ListModel(Models::ListItem *prototype, QObject *parent) : QAbstractListModel(parent)
{
// We need to register that metatype so that the QML engine
// knows about
qRegisterMetaType<Models::ListItem*>("ListItem*");
// So that we can return ListModel * from a Q_INVOKABLE to QML
// without having the QmlEngine take ownership and destroy our object
QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
this->m_prototype = prototype;
this->m_sortEnabled = false;
}
/*!
* Destroys a ListModel instance.
*/
Models::ListModel::~ListModel()
{
delete this->m_prototype;
this->m_prototype = NULL;
this->clear();
}
/*!
* \property Models::ListModel::count
*
* This property holds the number of rows of the model.
*/
/*!
* Returns number of rows in the model.
* \a index is not used but needed to reimplement the method.
*/
int Models::ListModel::rowCount(const QModelIndex &) const
{
return this->m_items.size();
}
/*!
* Returns a QVariant containing the data associed to \a role for row at \a index.
*/
QVariant Models::ListModel::data(const QModelIndex &index, int role) const
{
if (index.row() >= 0 && index.row() < this->m_items.size())
return this->m_items.at(index.row())->data(role);
return QVariant();
}
/*!
* Sets the data element specified by \a role to \a value
*/
bool Models::ListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.row() >= 0 && index.row() < this->m_items.size())
return this->m_items.at(index.row())->setData(role, value);
return false;
}
/*!
* Returns a hash containing the roleNames of the Model.
*/
QHash<int, QByteArray> Models::ListModel::roleNames() const
{
return this->m_prototype->roleNames();
}
/*!
* Returns the ListItem contained at \a row if it has been specified.
* Otherwise the first ListItem is returned.
*/
Models::ListItem *Models::ListModel::takeRow(int row, const QModelIndex &index)
{
if (row == -2) // IF ROW HAS NOT BEEN SPECIFIED TAKE FIRST ITEM
row = 0;
if (row >= 0 && row < this->m_items.size())
{
beginRemoveRows(index, row, row);
Models::ListItem *item = this->m_items.takeAt(row);
endRemoveRows();
emit (countChanged(this->rowCount()));
return item;
}
return NULL;
}
/*!
* Returns a list of items that are contained in the model from the row \a row
* to the row \a row + \a count. The items are removed from the model.
* If \a row is not specified, it starts from the first row.
* If count is not specified, returns items from \a row to the end of the model.
* In that case if row is not the first row, nothing will be returned.
*/
QList<Models::ListItem *> Models::ListModel::takeRows(int row, int count, const QModelIndex &index)
{
QList<Models::ListItem *> items;
if (row == -2) // IF ROW HAS NOT BEEN SPECIFIED TAKE FIRST ITEM
row = 0;
if (count == -1)
count = this->m_items.size();
if (row >= 0 && count > 0 && (row + count) <= this->m_items.size())
{
beginRemoveRows(index, row, row + count - 1);
for (int i = 0; i < count; i++)
items << this->m_items.takeAt(row);
endRemoveRows();
emit (countChanged(this->rowCount()));
}
return items;
}
/*!
* Appends a single row \a item to the Model.
*/
void Models::ListModel::appendRow(Models::ListItem *item)
{
if (item != NULL)
{
this->appendRows(QList<Models::ListItem *>() << item);
emit (countChanged(this->rowCount()));
}
}
/*!
* Appends several rows \a items to the Model.
*/
void Models::ListModel::appendRows(const QList<Models::ListItem *> &items)
{
if (items.size() == 0)
return ;
// NEEDED TO UPDATE VIEW
this->beginInsertRows(QModelIndex(), this->rowCount(), this->rowCount() + items.size() - 1);
foreach(Models::ListItem *item, items)
{
QObject::connect(item, SIGNAL(dataChanged()), this, SLOT(updateItem()));
this->m_items.append(item);
}
// NEEDED TO UPDATE VIEW
this->endInsertRows();
this->sort();
emit (countChanged(this->rowCount()));
}
/*!
* Insert new row described by \a item at position defined by \a row.
*/
void Models::ListModel::insertRow(int row, Models::ListItem *item)
{
if (item == NULL)
return ;
this->beginInsertRows(QModelIndex(), row, row);
QObject::connect(item, SIGNAL(dataChanged()), this, SLOT(updateItem()));
this->m_items.insert(row, item);
this->endInsertRows();
this->sort();
emit (countChanged(this->rowCount()));
}
/*!
* Removes a single row at position defined by \a row .
* The \a index argument is optional.
* Returns true if row was removed, false if row not found or \a row is invalid.
*/
bool Models::ListModel::removeRow(int row, const QModelIndex &index)
{
if (row >= 0 && row < this->m_items.size())
{
beginRemoveRows(index, row, row);
Models::ListItem *item = this->m_items.takeAt(row);
delete item;
item = NULL;
endRemoveRows();
emit (countChanged(this->rowCount()));
return true;
}
return false;
}
/*!
* Removes several rows starting at position defined by \a row until either \a count
* or the model's last row is reached. The \a index argument is optional.
* Returns true if the rows were removed, false if \a row is invalid.
*/
bool Models::ListModel::removeRows(int row, int count, const QModelIndex &index)
{
if (row >= 0 && count > 0 && (row + count) <= this->m_items.size())
{
beginRemoveRows(index, row, row + count - 1);
for (int i = 0; i < count; i++)
{
Models::ListItem *item = this->m_items.takeAt(row);
delete item;
item = NULL;
}
endRemoveRows();
emit (countChanged(this->rowCount()));
return true;
}
return false;
}
/*!
* Clears the whole model removing all rows.
*/
void Models::ListModel::clear()
{
if (this->m_items.size() == 0)
return ;
this->removeRows(0, this->m_items.size());
emit (countChanged(this->rowCount()));
}
bool Models::ListModel::sortingEnabled() const
{
return this->m_sortEnabled;
}
void Models::ListModel::setSorting(bool value)
{
if (value == this->sortingEnabled())
return;
this->m_sortEnabled = value;
emit sortingChanged(value);
this->sort();
}
/*!
* Returns the ListItem otype associated to the given model.
*/
Models::ListItem *Models::ListModel::getPrototype() const
{
return this->m_prototype;
}
/*!
* Sorts the elements of the models
*/
void Models::ListModel::sort()
{
if (this->m_sortEnabled)
{
qSort(this->m_items.begin(), this->m_items.end(), compareFunc);
foreach (Models::ListItem *item, this->m_items)
{
QModelIndex index = this->indexFromItem(item);
if (index.isValid())
emit dataChanged(index, index);
}
}
}
/*!
* Returns the index of the row in the model containing \a item.
*/
QModelIndex Models::ListModel::indexFromItem(Models::ListItem *item) const
{
if (item != NULL)
{
for (int i = 0; i < this->m_items.size(); i++)
if (this->m_items.at(i) == item)
return index(i);
}
return QModelIndex();
}
/*!
* Returns the item whose id matches \a itemId.
*/
Models::ListItem * Models::ListModel::find(int itemId) const
{
foreach(Models::ListItem *item, this->m_items)
if (item->id() == itemId)
return item;
return NULL;
}
/*!
* Returns row index at which \a item can be found in the model.
*/
int Models::ListModel::getRowFromItem(ListItem *item) const
{
if (item != NULL)
for (int i = 0; i < this->m_items.size(); i++)
if (this->m_items.at(i) == item)
return i;
return -1;
}
/*!
* Returns model as a QList.
*/
QList<Models::ListItem *> Models::ListModel::toList() const
{
return this->m_items;
}
/*!
* Slot triggered when a row item needs to be updated to reflect data changes.
*/
void Models::ListModel::updateItem()
{
Models::ListItem *item = static_cast<Models::ListItem *>(sender());
QModelIndex index = this->indexFromItem(item);
if (index.isValid())
emit dataChanged(index, index);
}
/*!
* Returns the row item at \a index in the model.
*/
Models::ListItem *Models::ListModel::get(int index) const
{
if (index >= this->m_items.size() || index < 0)
return Q_NULLPTR;
return this->m_items.at(index);
}
/*!
* Returns the index for item identified by \a id in the model.
*/
int Models::ListModel::rowIndexFromId(int id)
{
Models::ListItem* item = find(id);
if (item)
return indexFromItem(item).row();
return -1;
}
bool compareFunc(void *a, void *b)
{
return *static_cast<Models::ListItem *>(a) < *static_cast<Models::ListItem *>(b);
}