forked from Aseman-Land/TelegramQML
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegramabstractlistmodel.cpp
More file actions
112 lines (94 loc) · 3.06 KB
/
telegramabstractlistmodel.cpp
File metadata and controls
112 lines (94 loc) · 3.06 KB
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
#include "telegramabstractlistmodel.h"
TelegramAbstractListModel::TelegramAbstractListModel(QObject *parent) :
QAbstractListModel(parent),
_cacheCount(0)
{
connect(this, &TelegramAbstractListModel::countChanged, this, [this](){
int changed = ((_cacheCount == 0 && count() != 0) ||
(_cacheCount != 0 && count() == 0));
_cacheCount = count();
if(changed)
Q_EMIT isEmptyChanged();
});
}
QStringList TelegramAbstractListModel::roles() const
{
QStringList result;
const QHash<int,QByteArray> &roles = roleNames();
QHashIterator<int,QByteArray> i(roles);
while(i.hasNext())
{
i.next();
result << i.value();
}
qSort(result.begin(), result.end());
return result;
}
QQmlListProperty<QObject> TelegramAbstractListModel::items()
{
return QQmlListProperty<QObject>(this, &_items, QQmlListProperty<QObject>::AppendFunction(append),
QQmlListProperty<QObject>::CountFunction(count),
QQmlListProperty<QObject>::AtFunction(at),
QQmlListProperty<QObject>::ClearFunction(clear) );
}
QVariant TelegramAbstractListModel::get(int row, int role) const
{
if(row >= count() || row < 0)
return QVariant();
const QModelIndex &idx = index(row,0);
return data(idx , role);
}
QVariant TelegramAbstractListModel::get(int index, const QString &roleName) const
{
const int role = roleNames().key(roleName.toUtf8());
return get(index, role);
}
QVariantMap TelegramAbstractListModel::get(int index) const
{
if(index >= count())
return QVariantMap();
QVariantMap result;
const QHash<int,QByteArray> &roles = roleNames();
QHashIterator<int,QByteArray> i(roles);
while(i.hasNext())
{
i.next();
result[i.value()] = get(index, i.key());
}
return result;
}
int TelegramAbstractListModel::indexOf(int role, const QVariant &value)
{
int idx = -1;
for(int i=0 ;i<count(); i++)
if(get(i, role) == value) {
idx = i;
break;
}
return idx;
}
void TelegramAbstractListModel::append(QQmlListProperty<QObject> *p, QObject *v)
{
TelegramAbstractListModel *aobj = static_cast<TelegramAbstractListModel*>(p->object);
aobj->_items.append(v);
Q_EMIT aobj->itemsChanged();
}
int TelegramAbstractListModel::count(QQmlListProperty<QObject> *p)
{
TelegramAbstractListModel *aobj = static_cast<TelegramAbstractListModel*>(p->object);
return aobj->_items.count();
}
QObject *TelegramAbstractListModel::at(QQmlListProperty<QObject> *p, int idx)
{
TelegramAbstractListModel *aobj = static_cast<TelegramAbstractListModel*>(p->object);
return aobj->_items.at(idx);
}
void TelegramAbstractListModel::clear(QQmlListProperty<QObject> *p)
{
TelegramAbstractListModel *aobj = static_cast<TelegramAbstractListModel*>(p->object);
aobj->_items.clear();
Q_EMIT aobj->itemsChanged();
}
TelegramAbstractListModel::~TelegramAbstractListModel()
{
}