#include "accountdelegate.h" #include "flagpainter.h" #include "memberlistmodel.h" #include #include AccountDelegate::AccountDelegate(QObject *parent) : QStyledItemDelegate(parent) {} void AccountDelegate::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()); else painter->fillRect(option.rect, option.palette.alternateBase()); const int margin = 4; const int iconSz = 24; QIcon icon = qvariant_cast(index.data(Qt::DecorationRole)); if (!icon.isNull()) icon.paint(painter, option.rect.left() + margin, option.rect.top() + (option.rect.height() - iconSz) / 2, iconSz, iconSz); const int x = option.rect.left() + iconSz + margin * 3; const int w = option.rect.right() - x - margin; const int yMid = option.rect.top() + option.rect.height() / 2; uint8_t flags = (uint8_t)(index.data(Qt::UserRole + 3).toInt() & 0xFF); QFont nameFont = painter->font(); nameFont.setBold(true); nameFont.setPixelSize(13); if (flags & MEMBER_FLAG_DELETED) nameFont.setStrikeOut(true); painter->setFont(nameFont); QColor textColor = option.palette.color(QPalette::Text); if (option.state & QStyle::State_Selected) textColor = option.palette.color(QPalette::HighlightedText); if (index.data(Qt::UserRole + 2).toBool()) textColor = QColor("#4CAF50"); painter->setPen(textColor); const uint8_t visualFlags = flags & (MEMBER_FLAG_SUPERNODE | MEMBER_FLAG_ADMIN | MEMBER_FLAG_MODER); int flagX = x; if (visualFlags) { FlagPainter::draw(painter, flags, flagX, option.rect.top() + (option.rect.height() - nameFont.pixelSize()) / 2, nameFont.pixelSize()); int n = __builtin_popcount(visualFlags); flagX += nameFont.pixelSize() * n + (n - 1) * 2; } if (index.data(Qt::UserRole + 4).toBool()) { FlagPainter::drawStorage(painter, flagX, option.rect.top() + (option.rect.height() - nameFont.pixelSize()) / 2, nameFont.pixelSize()); flagX += nameFont.pixelSize() + 2; } QString name = index.data(Qt::DisplayRole).toString(); painter->drawText(QRect(flagX, option.rect.top() + 2, w - (flagX - x), yMid - option.rect.top() - 2), Qt::AlignLeft | Qt::AlignBottom, name); QFont subFont = painter->font(); subFont.setBold(false); subFont.setPixelSize(10); painter->setFont(subFont); painter->setPen(QColor(130, 130, 130)); QString sub = index.data(Qt::UserRole + 1).toString(); if (!sub.isEmpty()) painter->drawText(QRect(flagX, yMid + 2, w - (flagX - x), option.rect.bottom() - yMid - 2), Qt::AlignLeft | Qt::AlignTop, sub); painter->restore(); } QSize AccountDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { (void)option; (void)index; return QSize(160, 36); }