You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
3.8 KiB
95 lines
3.8 KiB
#include "channeldelegate.h" |
|
#include <QPainter> |
|
#include <QApplication> |
|
|
|
ChannelDelegate::ChannelDelegate(QObject *parent) |
|
: QStyledItemDelegate(parent) {} |
|
|
|
void ChannelDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, |
|
const QModelIndex &index) const { |
|
painter->save(); |
|
|
|
if (option.state & QStyle::State_Selected) { |
|
painter->fillRect(option.rect, option.palette.highlight()); |
|
} else if (option.state & QStyle::State_MouseOver) { |
|
painter->fillRect(option.rect, option.palette.light()); |
|
} |
|
|
|
const int margin = 8; |
|
const int iconSize = 32; |
|
QRect iconRect(option.rect.left() + margin, |
|
option.rect.top() + (option.rect.height() - iconSize) / 2, |
|
iconSize, iconSize); |
|
|
|
QIcon icon = qvariant_cast<QIcon>(index.data(ChannelIconRole)); |
|
if (!icon.isNull()) { |
|
icon.paint(painter, iconRect); |
|
} |
|
|
|
QRect textRect(iconRect.right() + margin, option.rect.top() + margin, |
|
option.rect.right() - iconRect.right() - 2 * margin, |
|
option.rect.height() - 2 * margin); |
|
|
|
QString name = index.data(ChannelNameRole).toString(); |
|
QFont nameFont = option.font; |
|
nameFont.setBold(true); |
|
nameFont.setPointSize(nameFont.pointSize() + 1); |
|
painter->setFont(nameFont); |
|
|
|
QColor textColor = (option.state & QStyle::State_Selected) |
|
? option.palette.highlightedText().color() |
|
: option.palette.text().color(); |
|
painter->setPen(textColor); |
|
|
|
QRect nameRect = textRect; |
|
nameRect.setHeight(QFontMetrics(nameFont).height()); |
|
painter->drawText(nameRect, Qt::AlignLeft | Qt::AlignTop, |
|
painter->fontMetrics().elidedText(name, Qt::ElideRight, nameRect.width())); |
|
|
|
QString lastMsg = index.data(ChannelLastMessageRole).toString(); |
|
if (!lastMsg.isEmpty()) { |
|
QFont msgFont = option.font; |
|
msgFont.setPointSize(msgFont.pointSize() - 1); |
|
QColor nickColor(0x33, 0x90, 0xEC); |
|
QColor msgColor = (option.state & QStyle::State_Selected) |
|
? option.palette.highlightedText().color().darker(120) |
|
: QColor(128, 128, 128); |
|
if (option.state & QStyle::State_Selected) nickColor = nickColor.lighter(140); |
|
|
|
QRect msgRect(textRect.left(), nameRect.bottom() + 4, |
|
textRect.width(), option.rect.bottom() - nameRect.bottom() - margin); |
|
QFontMetrics msgFm(msgFont); |
|
|
|
int colon = lastMsg.indexOf(QString::fromUtf8(": ")); |
|
if (colon >= 0) { |
|
QString nick = lastMsg.left(colon + 1); |
|
QString body = lastMsg.mid(colon + 2); |
|
|
|
painter->setFont(msgFont); |
|
painter->setPen(nickColor); |
|
QRect nickRect = msgRect; |
|
nickRect.setWidth(msgFm.horizontalAdvance(nick)); |
|
painter->drawText(nickRect, Qt::AlignLeft | Qt::AlignTop, nick); |
|
|
|
painter->setPen(msgColor); |
|
QRect bodyRect(msgRect.left() + nickRect.width(), msgRect.top(), |
|
msgRect.width() - nickRect.width(), msgRect.height()); |
|
QString elidedBody = msgFm.elidedText(body, Qt::ElideRight, bodyRect.width()); |
|
painter->drawText(bodyRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, elidedBody); |
|
} else { |
|
painter->setFont(msgFont); |
|
painter->setPen(msgColor); |
|
QString elided = msgFm.elidedText(lastMsg, Qt::ElideRight, msgRect.width()); |
|
painter->drawText(msgRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, elided); |
|
} |
|
} |
|
|
|
painter->restore(); |
|
} |
|
|
|
QSize ChannelDelegate::sizeHint(const QStyleOptionViewItem &option, |
|
const QModelIndex &index) const { |
|
Q_UNUSED(option); |
|
Q_UNUSED(index); |
|
return QSize(200, 56); |
|
}
|
|
|