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
|
/*
* Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swift/QtUI/Roster/DelegateCommons.h>
#include <QFileInfo>
#include <Swift/QtUI/QtScaledAvatarCache.h>
namespace Swift {
void DelegateCommons::drawElidedText(QPainter* painter, const QRect& region, const QString& text, int flags) {
QString adjustedText(painter->fontMetrics().elidedText(text, Qt::ElideRight, region.width(), Qt::TextShowMnemonic));
painter->setClipRect(region);
painter->drawText(region, flags, adjustedText.simplified());
painter->setClipping(false);
}
void DelegateCommons::paintContact(QPainter* painter, const QStyleOptionViewItem& option, const QColor& nameColor, const QString& avatarPath, const QIcon& presenceIcon, const QString& name, const QString& statusText, bool isIdle, int unreadCount, bool compact) const {
painter->save();
QRect fullRegion(option.rect);
if ( option.state & QStyle::State_Selected ) {
painter->fillRect(fullRegion, option.palette.highlight());
painter->setPen(option.palette.highlightedText().color());
} else {
painter->setPen(QPen(nameColor));
}
QRect presenceIconRegion(QPoint(farLeftMargin, fullRegion.top()), QSize(presenceIconWidth, fullRegion.height() - verticalMargin));
QRect idleIconRegion(QPoint(farLeftMargin, fullRegion.top()), QSize(presenceIconWidth*2, fullRegion.height() - verticalMargin));
int calculatedAvatarSize = presenceIconRegion.height();
//This overlaps the presenceIcon, so must be painted first
QRect avatarRegion(QPoint(presenceIconRegion.right() - presenceIconWidth / 2, presenceIconRegion.top()), QSize(calculatedAvatarSize, calculatedAvatarSize));
QPixmap avatarPixmap;
if (!compact && !avatarPath.isEmpty()) {
QString scaledAvatarPath = QtScaledAvatarCache(avatarRegion.height()).getScaledAvatarPath(avatarPath);
if (QFileInfo(scaledAvatarPath).exists()) {
avatarPixmap.load(scaledAvatarPath);
}
}
if (!compact && avatarPixmap.isNull()) {
avatarPixmap = QPixmap(":/icons/avatar.svg").scaled(avatarRegion.height(), avatarRegion.width(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
if (!compact) {
painter->drawPixmap(avatarRegion.topLeft() + QPoint(((avatarRegion.width() - avatarPixmap.width()) / 2), (avatarRegion.height() - avatarPixmap.height()) / 2), avatarPixmap);
}
//Paint the presence icon over the top of the avatar
presenceIcon.paint(painter, presenceIconRegion, Qt::AlignBottom | Qt::AlignHCenter);
if (isIdle) {
idleIcon.paint(painter, idleIconRegion, Qt::AlignBottom | Qt::AlignHCenter);
}
QFontMetrics nameMetrics(nameFont);
painter->setFont(nameFont);
int extraFontWidth = nameMetrics.width("H");
int leftOffset = (compact ? presenceIconRegion : avatarRegion).right() + horizontalMargin * 2 + extraFontWidth / 2;
QRect textRegion(fullRegion.adjusted(leftOffset, 0, 0/*-leftOffset*/, 0));
int nameHeight = nameMetrics.height() + verticalMargin;
QRect nameRegion(textRegion.adjusted(0, verticalMargin, 0, 0));
DelegateCommons::drawElidedText(painter, nameRegion, name);
if (!compact) {
painter->setFont(detailFont);
painter->setPen(QPen(QColor(160,160,160)));
QRect statusTextRegion(textRegion.adjusted(0, nameHeight, 0, 0));
DelegateCommons::drawElidedText(painter, statusTextRegion, statusText);
}
if (unreadCount > 0) {
QRect unreadRect(fullRegion.right() - unreadCountSize - horizontalMargin, fullRegion.top() + (fullRegion.height() - unreadCountSize) / 2, unreadCountSize, unreadCountSize);
QPen pen(QColor("black"));
pen.setWidth(1);
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setPen(pen);
painter->setBrush(QBrush(QColor("red"), Qt::SolidPattern));
//painter->setBackgroundMode(Qt::OpaqueMode);
painter->drawEllipse(unreadRect);
painter->setBackgroundMode(Qt::TransparentMode);
painter->setPen(QColor("white"));
drawElidedText(painter, unreadRect, QString("%1").arg(unreadCount), Qt::AlignCenter);
}
painter->restore();
}
QSize DelegateCommons::contactSizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/, bool compact ) const {
int heightByAvatar = (compact ? presenceIconHeight : avatarSize) + verticalMargin * 2;
QFontMetrics nameMetrics(nameFont);
QFontMetrics statusMetrics(detailFont);
int sizeByText = 2 * verticalMargin + nameMetrics.height() + (compact ? 0 : statusMetrics.height());
//Doesn't work, yay! FIXME: why?
//QSize size = (option.state & QStyle::State_Selected) ? QSize(150, 80) : QSize(150, avatarSize_ + margin_ * 2);
//qDebug() << "Returning size" << size;
return QSize(150, sizeByText > heightByAvatar ? sizeByText : heightByAvatar);
}
const int DelegateCommons::horizontalMargin = 2;
const int DelegateCommons::verticalMargin = 2;
const int DelegateCommons::farLeftMargin = 2;
const int DelegateCommons::avatarSize = 20;
const int DelegateCommons::presenceIconHeight = 16;
const int DelegateCommons::presenceIconWidth = 16;
const int DelegateCommons::unreadCountSize = 16;
}
|