-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathusersmodel.cpp
executable file
·192 lines (167 loc) · 5.53 KB
/
usersmodel.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
/*
This file is part of Qween.
Copyright (C) 2009-2010 NOSE Takafumi <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
In addition, as a special exception, NOSE Takafumi
gives permission to link the code of its release of Qween with the
OpenSSL project's "OpenSSL" library (or with modified versions of it
that use the same license as the "OpenSSL" library), and distribute
the linked executables. You must obey the GNU General Public License
in all respects for all of the code used other than "OpenSSL". If you
modify this file, you may extend this exception to your version of the
file, but you are not obligated to do so. If you do not wish to do
so, delete this exception statement from your version.
*/
#include "usersmodel.h"
#include "iconmanager.h"
#include "twitter.h"
UsersModel::UsersModel(IconManager *iconMgr, QObject *parent) :
QAbstractItemModel(parent), m_iconMgr(iconMgr)
{
connect(m_iconMgr, SIGNAL(iconDownloaded(quint64,QIcon)),
this, SLOT(OnIconDownloaded(quint64,QIcon)));
}
int UsersModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_itemList.size();
}
int UsersModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 1;
}
QVariant UsersModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= m_itemList.size() || index.row() < 0)
return QVariant();
Twitter::TwitterItem *item = m_itemList.at(index.row());
if (role == Qt::DisplayRole || role == Qt::EditRole) {
switch(index.column()){
case 0:
return item->screenName();
break;
default:
return QVariant();
}
}else if(role == Qt::DecorationRole){
switch(index.column()){
case 0:
if(m_iconMgr->isIconAvailable(item->userId())){
return m_iconMgr->getIcon(item->userId());
}else{
m_iconMgr->fetchIcon(item->userId(), item->iconUri());
return QIcon();
}
default:
return QIcon();
}
}
return QVariant();
}
QVariant UsersModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("ID");
default:
return QVariant();
}
}
return QVariant();
}
void UsersModel::appendItem(Twitter::TwitterItem item)//, bool ignoreId)
{
int index;
index = m_itemList.count();
beginInsertRows(QModelIndex(), index, index);
Twitter::TwitterItem *internalItem = new Twitter::TwitterItem(item);
//internalItem->setParent(this);
m_itemList.append(internalItem);
endInsertRows();
}
Twitter::TwitterItem UsersModel::removeItem(int index)
{
beginRemoveRows(QModelIndex(), index, index);
Twitter::TwitterItem *p = m_itemList.takeAt(index);
Twitter::TwitterItem rv(*p);
rv.setParent(NULL);
delete p;
endRemoveRows();
return rv;
}
Twitter::TwitterItem UsersModel::itemAt(int index)
{
Twitter::TwitterItem *p = m_itemList.at(index);
//Twitter::TwitterItem rv(*p);
return *p;
}
bool UsersModel::userExists(quint64 id){
Twitter::TwitterItem *item;
foreach(item, m_itemList){
if(item->userId() == id){
return true;
}
}
return false;
}
bool UsersModel::userExists(const QString& screenName){
Twitter::TwitterItem *item;
foreach(item, m_itemList){
if(item->screenName() == screenName){
return true;
}
}
return false;
}
bool UsersModel::hasChildren(const QModelIndex &parent) const
{
if(parent.isValid())
return false;
else
return true;
}
QModelIndex UsersModel::index(int row, int column, const QModelIndex &parent)
const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
if (!parent.isValid())
return createIndex(row, column, m_itemList.at(row));
else
return QModelIndex(); //TLモデルでは子供はアイテムを持たないから
}
QModelIndex UsersModel::parent(const QModelIndex &child) const
{
return QModelIndex();
}
Qt::ItemFlags UsersModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
return QAbstractItemModel::flags(index);// | Qt::ItemIsEditable;
}
void UsersModel::OnIconDownloaded(quint64 userid, const QIcon &icon)
{
for(int i=0;i<m_itemList.count();i++){
Twitter::TwitterItem *item = m_itemList.at(i);
if(item->userId() == userid){
QModelIndex idx = index(i,0,QModelIndex());
emit dataChanged(idx, idx);
}
}
}