File indexing completed on 2024-12-01 04:36:37
0001 /* 0002 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "usercompletiondelegate.h" 0008 #include "common/delegatepaintutil.h" 0009 #include "misc/avatarcachemanager.h" 0010 #include "model/usercompletermodel.h" 0011 0012 #include <QFontMetricsF> 0013 #include <QPainter> 0014 0015 UserCompletionDelegate::UserCompletionDelegate(QObject *parent) 0016 : QItemDelegate{parent} 0017 , mAvatarCacheManager(new AvatarCacheManager(Utils::AvatarType::User, this)) 0018 { 0019 } 0020 0021 UserCompletionDelegate::~UserCompletionDelegate() = default; 0022 0023 void UserCompletionDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 0024 { 0025 // [M] icon ? status name (username) 0026 drawBackground(painter, option, index); 0027 0028 if (option.state & QStyle::State_Selected) { 0029 painter->fillRect(option.rect, option.palette.highlight()); 0030 } 0031 0032 const int margin = DelegatePaintUtil::margin(); 0033 const QFont oldFont = painter->font(); 0034 0035 QFont boldFont = oldFont; 0036 boldFont.setBold(true); 0037 painter->setFont(boldFont); 0038 0039 int xPos = -1; 0040 const Utils::AvatarInfo info = index.data(UserCompleterModel::AvatarInfo).value<Utils::AvatarInfo>(); 0041 if (info.isValid()) { 0042 const QRect displayRect(margin, option.rect.y(), option.rect.height(), option.rect.height()); 0043 const QPixmap pix = mAvatarCacheManager->makeAvatarPixmap(option.widget, info, option.rect.height()); 0044 if (!pix.isNull()) { 0045 drawDecoration(painter, option, displayRect, pix); 0046 xPos = margin + option.rect.height(); 0047 } 0048 } 0049 0050 const QIcon iconStatus = index.data(UserCompleterModel::UserIconStatus).value<QIcon>(); 0051 if (!iconStatus.isNull()) { 0052 const QRect displayRect(margin + xPos, option.rect.y(), option.rect.height(), option.rect.height()); 0053 drawDecoration(painter, option, displayRect, iconStatus.pixmap(option.rect.height(), option.rect.height())); 0054 xPos += margin + option.rect.height(); 0055 } 0056 0057 QFontMetrics fontMetrics(boldFont); 0058 const QString name = index.data(UserCompleterModel::DisplayName).toString(); 0059 const QString userName = index.data(UserCompleterModel::UserName).toString(); 0060 int nameWidth = -1; 0061 const int defaultCharHeight = option.rect.y() + fontMetrics.ascent(); 0062 if (name.isEmpty()) { 0063 // nameWidth = fontMetrics.horizontalAdvance(userName); 0064 painter->drawText(xPos + margin, defaultCharHeight, userName); 0065 } else { 0066 nameWidth = fontMetrics.horizontalAdvance(name); 0067 painter->drawText(xPos + margin, defaultCharHeight, name); 0068 xPos += nameWidth; 0069 if (!userName.isEmpty()) { 0070 painter->setFont(oldFont); 0071 // fontMetrics = QFontMetrics(oldFont); 0072 // nameWidth = fontMetrics.horizontalAdvance(userName); 0073 painter->drawText(xPos + margin * 2, defaultCharHeight, userName); 0074 } 0075 } 0076 painter->setFont(oldFont); 0077 } 0078 0079 void UserCompletionDelegate::setRocketChatAccount(RocketChatAccount *newRocketChatAccount) 0080 { 0081 mAvatarCacheManager->setCurrentRocketChatAccount(newRocketChatAccount); 0082 }