forked from kutegram/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialogitemdelegate.cpp
65 lines (47 loc) · 2.09 KB
/
dialogitemdelegate.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
#include "dialogitemdelegate.h"
#include <QPainter>
#include <QApplication>
#include <QPixmap>
DialogItemDelegate::DialogItemDelegate(QObject *parent) :
QAbstractItemDelegate(parent)
{
}
void DialogItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
//TODO
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &option, painter);
qint32 x = option.rect.x();
qint32 y = option.rect.y();
qint32 w = option.rect.width();
qint32 h = option.rect.height();
QFontMetrics fm = QApplication::fontMetrics();
QFont font = QApplication::font();
qint32 padding = 4;
qint32 fontHeight = fm.height();
qint32 avatarSize = padding + fontHeight + padding + fontHeight + padding;
qint32 height = padding + avatarSize + padding;
QString title = index.data().toString();
qint64 id = index.data(Qt::UserRole).toLongLong();
x += padding;
y += padding;
QPixmap avatar = index.data(Qt::DecorationRole).value<QPixmap>();
if (!avatar.isNull()) painter->drawPixmap(QRect(x, y, avatarSize, avatarSize), avatar);
x += avatarSize + padding + padding;
y += padding;
painter->setPen((option.state & QStyle::State_HasFocus) == QStyle::State_HasFocus ? option.palette.brightText().color() : option.palette.text().color());
font.setBold(true);
painter->setFont(font);
painter->drawText(x, y, w, h, Qt::TextSingleLine, fm.elidedText(title, Qt::ElideRight, w - x - padding));
y += fontHeight + padding;
QString tooltip = index.data(Qt::ToolTipRole).toString();
font.setBold(false);
painter->setFont(font);
painter->drawText(x, y, w, h, Qt::TextSingleLine, fm.elidedText(tooltip, Qt::ElideRight, w - x - padding));
}
QSize DialogItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (!index.isValid()) return option.rect.size();
qint32 padding = 4;
qint32 fontHeight = QApplication::fontMetrics().height();
return QSize(option.rect.width(), padding + padding + fontHeight + padding + fontHeight + padding + padding);
}