#include "RosterDelegate.h" #include #include #include #include #include #include #include "QtTreeWidgetItem.h" namespace Swift { RosterDelegate::RosterDelegate() : nameFont_(QApplication::font()), statusFont_(QApplication::font()) { nameFont_.setPointSize(12); statusFont_.setStyle(QFont::StyleItalic); statusFont_.setPointSize(10); } QSize RosterDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index ) const { QtTreeWidgetItem* item = static_cast(index.internalPointer()); if (!item || !item->isContact()) { return QStyledItemDelegate::sizeHint(option, index); } int sizeByAvatar = avatarSize_ + margin_ * 2; QFontMetrics nameMetrics(nameFont_); QFontMetrics statusMetrics(statusFont_); int sizeByText = 2 * margin_ + nameMetrics.height() + 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 > sizeByAvatar ? sizeByText : sizeByAvatar); } void RosterDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QtTreeWidgetItem* item = static_cast(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()); } else { QColor nameColor = index.data(Qt::TextColorRole).value(); painter->setPen(QPen(nameColor)); } QRect presenceIconRegion(QPoint(margin_, fullRegion.top()), QSize(presenceIconWidth_, fullRegion.height())); //This overlaps the presenceIcon, so must be painted first QRect avatarRegion(QPoint(presenceIconRegion.right() - presenceIconWidth_ / 2, fullRegion.top()), QSize(avatarSize_, fullRegion.height())); QIcon avatar = index.data(AvatarRole).isValid() && !index.data(AvatarRole).value().isNull() ? index.data(AvatarRole).value() : QIcon(":/icons/avatar.png"); avatar.paint(painter, avatarRegion, Qt::AlignVCenter | Qt::AlignHCenter); //Paint the presence icon over the top of the avatar QIcon presenceIcon = index.data(PresenceIconRole).isValid() && !index.data(PresenceIconRole).value().isNull() ? index.data(PresenceIconRole).value() : QIcon(":/icons/offline.png"); presenceIcon.paint(painter, presenceIconRegion, Qt::AlignBottom | Qt::AlignHCenter); painter->setFont(nameFont_); QRect textRegion(fullRegion.adjusted(avatarRegion.right() + margin_ * 2, 0, 0, 0)); QFontMetrics nameMetrics(nameFont_); int nameHeight = nameMetrics.height() + margin_; QRect nameRegion(textRegion.adjusted(0, margin_, 0, 0)); painter->drawText(nameRegion, Qt::AlignTop, index.data(Qt::DisplayRole).toString()); 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(); } }