~ubuntu-branches/ubuntu/karmic/psi/karmic

« back to all changes in this revision

Viewing changes to src/tools/contactlist/contactlistmodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <QAbstractItemModel>
 
2
#include <QModelIndex>
 
3
#include <QVariant>
 
4
#include <QColor>
 
5
 
 
6
#include "contactlist.h"
 
7
#include "contactlistitem.h"
 
8
#include "contactlistgroup.h"
 
9
#include "contactlistmodel.h"
 
10
#include "contactlistrootitem.h"
 
11
#include "contactlistcontact.h"
 
12
#include "contactlistgroupitem.h"
 
13
 
 
14
#define COLUMNS 3
 
15
 
 
16
// TEMPORARY
 
17
#define GROUPBG_COLOR QColor(0x96,0x96,0x96)
 
18
#define GROUPFG_COLOR QColor(0xFF,0xFF,0xFF)
 
19
#define AWAY_COLOR QColor(0x00,0x4b,0xb4)
 
20
#define OFFLINE_COLOR QColor(0x64,0x64,0x64)
 
21
#define DND_COLOR QColor(0x7e,0x00,0x00)
 
22
#define ONLINE_COLOR QColor(0x00,0x00,0x00)
 
23
 
 
24
ContactListModel::ContactListModel(ContactList* contactList) : contactList_(contactList), showStatus_(true)
 
25
{
 
26
        connect(contactList_,SIGNAL(dataChanged()),this,SLOT(contactList_changed()));
 
27
}
 
28
 
 
29
 
 
30
QVariant ContactListModel::data(const QModelIndex &index, int role) const
 
31
{
 
32
        if (!index.isValid())
 
33
                return QVariant();
 
34
 
 
35
        ContactListItem* item = static_cast<ContactListItem*>(index.internalPointer());
 
36
        ContactListGroup*     group     = 0;
 
37
        ContactListGroupItem* groupItem = 0;
 
38
        ContactListContact*   contact   = 0;
 
39
 
 
40
        if (role == Qt::DisplayRole && index.column() == NameColumn) {
 
41
                if ((contact = dynamic_cast<ContactListContact*>(item))) {
 
42
                        QString txt;
 
43
                        if (showStatus_ && !contact->status().message().isEmpty()) {
 
44
                                txt = QString("%1 (%2)").arg(contact->name()).arg(contact->status().message());
 
45
                        }
 
46
                        else
 
47
                                txt = contact->name();
 
48
                        return QVariant(txt);
 
49
                }
 
50
                else if ((group = dynamic_cast<ContactListGroup*>(item))) {
 
51
                        return QVariant(QString("%1 (%2/%3)").arg(group->name()).arg(group->countOnline()).arg(group->count()));
 
52
                }
 
53
        }
 
54
        else if (role == Qt::EditRole && index.column() == NameColumn) {
 
55
                if ((contact = dynamic_cast<ContactListContact*>(item))) {
 
56
                        return QVariant(contact->name());
 
57
                }
 
58
                else if ((group = dynamic_cast<ContactListGroup*>(item))) {
 
59
                        return QVariant(group->name());
 
60
                }
 
61
        }
 
62
        else if (role == Qt::DecorationRole && (index.column() == StatusIconColumn || index.column() == PictureColumn)) {
 
63
                if ((contact = dynamic_cast<ContactListContact*>(item))) {
 
64
                        if (index.column() == PictureColumn)
 
65
                                return QVariant(contact->picture());
 
66
                        else if (index.column() == StatusIconColumn)
 
67
                                return QVariant(contact->statusIcon());
 
68
                }
 
69
        }
 
70
        else if (role == Qt::BackgroundColorRole) {
 
71
                if ((group = dynamic_cast<ContactListGroup*>(item))) {
 
72
                        return qVariantFromValue(GROUPBG_COLOR);
 
73
                }
 
74
        }
 
75
        else if (role == Qt::TextColorRole) {
 
76
                if ((contact = dynamic_cast<ContactListContact*>(item))) {
 
77
                        if(contact->status().type() == Status::Away || contact->status().type() == Status::XA) {
 
78
                                return qVariantFromValue(AWAY_COLOR);
 
79
                        }
 
80
                        else if (contact->status().type() == Status::Offline) {
 
81
                                return qVariantFromValue(OFFLINE_COLOR);
 
82
                        }
 
83
                        else if (contact->status().type() == Status::DND) {
 
84
                                return qVariantFromValue(DND_COLOR);
 
85
                        }
 
86
                        else {
 
87
                                return qVariantFromValue(ONLINE_COLOR);
 
88
                        }
 
89
                }
 
90
                else if ((group = dynamic_cast<ContactListGroup*>(item))) {
 
91
                        return qVariantFromValue(GROUPFG_COLOR);
 
92
                }
 
93
        }
 
94
        else if (role == Qt::ToolTipRole) {
 
95
                return QVariant(item->toolTip());
 
96
        }
 
97
        else if (role == ExpandedRole) {
 
98
                if ((groupItem = dynamic_cast<ContactListGroupItem*>(item))) {
 
99
                        if (contactList_->search().isEmpty()) {
 
100
                                return QVariant(groupItem->expanded());
 
101
                        }
 
102
                        else {
 
103
                                return QVariant(true);
 
104
                        }
 
105
                }
 
106
                else {
 
107
                        return QVariant(false);
 
108
                }
 
109
        }
 
110
        return QVariant();
 
111
}
 
112
 
 
113
bool ContactListModel::setData(const QModelIndex& index, const QVariant& data, int role)
 
114
{
 
115
        if (!index.isValid())
 
116
                return false;
 
117
 
 
118
        ContactListItem* item = static_cast<ContactListItem*>(index.internalPointer());
 
119
        if (role == ContextMenuRole) {
 
120
                item->showContextMenu(data.toPoint());
 
121
        }
 
122
 
 
123
        return true;
 
124
}
 
125
 
 
126
QVariant ContactListModel::headerData(int section, Qt::Orientation orientation, int role) const
 
127
{
 
128
        Q_UNUSED(section);
 
129
        Q_UNUSED(orientation);
 
130
        Q_UNUSED(role);
 
131
        return QVariant();
 
132
}
 
133
 
 
134
QModelIndex ContactListModel::index(int row, int column, const QModelIndex &parent) const
 
135
{
 
136
        ContactListGroupItem* parentItem;
 
137
        if (parent.isValid())
 
138
                parentItem = static_cast<ContactListGroupItem*>(parent.internalPointer());
 
139
        else
 
140
                parentItem = contactList_->rootItem();
 
141
 
 
142
        ContactListItem* item = parentItem->atIndex(row);
 
143
        return (item ? createIndex(row, column, item) : QModelIndex());
 
144
}
 
145
 
 
146
 
 
147
QModelIndex ContactListModel::parent(const QModelIndex &index) const
 
148
{
 
149
        if (!index.isValid())
 
150
                return QModelIndex();
 
151
 
 
152
        ContactListItem* item = static_cast<ContactListItem*>(index.internalPointer());
 
153
        ContactListGroupItem* parent = item->parent();
 
154
 
 
155
        return (parent == contactList_->rootItem() ? QModelIndex() : createIndex(parent->index(),0,parent));
 
156
}
 
157
 
 
158
 
 
159
int ContactListModel::rowCount(const QModelIndex &parent) const
 
160
{
 
161
        ContactListGroupItem* parentItem;
 
162
        if (parent.isValid()) {
 
163
                ContactListItem* item = static_cast<ContactListItem*>(parent.internalPointer());
 
164
                parentItem = dynamic_cast<ContactListGroupItem*>(item);
 
165
        }
 
166
        else {
 
167
                parentItem = contactList_->rootItem();
 
168
        }
 
169
 
 
170
        return (parentItem ? parentItem->items() : 0);
 
171
}
 
172
 
 
173
 
 
174
int ContactListModel::columnCount(const QModelIndex&) const
 
175
{
 
176
        return COLUMNS;
 
177
}
 
178
 
 
179
Qt::ItemFlags ContactListModel::flags(const QModelIndex& index) const
 
180
{
 
181
        Qt::ItemFlags f = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
 
182
        if (index.column() == NameColumn)
 
183
                f = f | Qt::ItemIsEditable;
 
184
        return f;
 
185
}
 
186
 
 
187
 
 
188
void ContactListModel::contactList_changed()
 
189
{
 
190
        reset();
 
191
}