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
|
#include "RosterDelegate.h"
#include <QPainter>
#include <QColor>
#include <QBrush>
#include <QFontMetrics>
#include <qdebug.h>
#include "QtTreeWidgetItem.h"
namespace Swift {
QSize RosterDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index ) const {
QtTreeWidgetItem* item = static_cast<QtTreeWidgetItem*>(index.internalPointer());
if (item && !item->isContact()) {
return QStyledItemDelegate::sizeHint(option, index);
}
//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 size;
}
void RosterDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
QtTreeWidgetItem* item = static_cast<QtTreeWidgetItem*>(index.internalPointer());
if (item && !item->isContact()) {
QStyledItemDelegate::paint(painter, option, index);
return;
}
//qDebug() << "painting" << index.data(Qt::DisplayRole).toString();
painter->save();
//QStyledItemDelegate::paint(painter, option, index);
//initStyleOption(option, index);
QRect fullRegion(option.rect);
if ( option.state & QStyle::State_Selected ) {
painter->fillRect(fullRegion, option.palette.highlight());
painter->setPen(option.palette.highlightedText().color());
}
QRect avatarRegion(QPoint(margin_, fullRegion.top() + margin_), QSize(avatarSize_, avatarSize_));
QIcon icon = index.data(AvatarRole).isValid() && !index.data(AvatarRole).value<QIcon>().isNull()
? index.data(AvatarRole).value<QIcon>()
: QIcon(":/icons/avatar.png");
icon.paint(painter, avatarRegion, Qt::AlignVCenter | Qt::AlignHCenter);
QFont nameFont = painter->font();
QFont statusFont = painter->font();
painter->setFont(nameFont);
QRect textRegion(fullRegion.adjusted(avatarSize_ + margin_ * 2, 0, 0, 0));
QFontMetrics nameMetrics(nameFont);
int nameHeight = nameMetrics.height() + margin_;
QRect nameRegion(textRegion.adjusted(0, margin_, 0, 0));
QColor nameColor = index.data(Qt::TextColorRole).value<QColor>();
painter->setPen(QPen(nameColor));
painter->drawText(nameRegion, Qt::AlignTop, index.data(Qt::DisplayRole).toString());
statusFont.setStyle(QFont::StyleItalic);
statusFont.setPointSize(10);
painter->setFont(statusFont);
painter->setPen(QPen(QColor(160,160,160)));
QRect statusTextRegion(textRegion.adjusted(0, nameHeight, 0, 0));
painter->drawText(statusTextRegion, Qt::AlignTop, index.data(StatusTextRole).toString());
painter->restore();
}
}
|