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 "userandchannelcompletiondelegate.h" 0008 #include "common/delegatepaintutil.h" 0009 #include "misc/avatarcachemanager.h" 0010 #include "model/inputcompletermodel.h" 0011 0012 #include <KLocalizedString> 0013 0014 #include <QFontMetricsF> 0015 #include <QPainter> 0016 0017 UserAndChannelCompletionDelegate::UserAndChannelCompletionDelegate(QObject *parent) 0018 : QItemDelegate{parent} 0019 , mAvatarCacheManager(new AvatarCacheManager(Utils::AvatarType::UserAndRoom, this)) 0020 { 0021 } 0022 0023 UserAndChannelCompletionDelegate::~UserAndChannelCompletionDelegate() = default; 0024 0025 void UserAndChannelCompletionDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 0026 { 0027 // [M] icon ? status name (username) (description if necessary) 0028 drawBackground(painter, option, index); 0029 0030 if (option.state & QStyle::State_Selected) { 0031 painter->fillRect(option.rect, option.palette.highlight()); 0032 } 0033 0034 const int margin = DelegatePaintUtil::margin(); 0035 const QFont oldFont = painter->font(); 0036 0037 QFont boldFont = oldFont; 0038 boldFont.setBold(true); 0039 painter->setFont(boldFont); 0040 0041 int xPos = -1; 0042 const Utils::AvatarInfo info = index.data(InputCompleterModel::AvatarInfo).value<Utils::AvatarInfo>(); 0043 if (info.isValid()) { 0044 const QRect displayRect(margin, option.rect.y(), option.rect.height(), option.rect.height()); 0045 const QPixmap pix = mAvatarCacheManager->makeAvatarPixmap(option.widget, info, option.rect.height()); 0046 if (!pix.isNull()) { 0047 drawDecoration(painter, option, displayRect, pix); 0048 xPos = margin + option.rect.height(); 0049 } 0050 } 0051 0052 const QIcon iconStatus = index.data(InputCompleterModel::IconStatus).value<QIcon>(); 0053 if (!iconStatus.isNull()) { 0054 const QRect displayRect(margin + xPos, option.rect.y(), option.rect.height(), option.rect.height()); 0055 drawDecoration(painter, option, displayRect, iconStatus.pixmap(option.rect.height(), option.rect.height())); 0056 xPos += margin + option.rect.height(); 0057 } 0058 0059 QFontMetrics fontMetrics(boldFont); 0060 const QString name = index.data(InputCompleterModel::DisplayName).toString(); 0061 const QString userName = index.data(InputCompleterModel::UserName).toString(); 0062 int nameWidth = -1; 0063 const int defaultCharHeight = option.rect.y() + fontMetrics.ascent(); 0064 if (name.isEmpty()) { 0065 nameWidth = fontMetrics.horizontalAdvance(userName); 0066 painter->drawText(xPos + margin, defaultCharHeight, userName); 0067 xPos += nameWidth; 0068 } else { 0069 nameWidth = fontMetrics.horizontalAdvance(name); 0070 painter->drawText(xPos + margin, defaultCharHeight, name); 0071 xPos += nameWidth; 0072 if (!userName.isEmpty()) { 0073 painter->setFont(oldFont); 0074 fontMetrics = QFontMetrics(oldFont); 0075 nameWidth = fontMetrics.horizontalAdvance(userName); 0076 painter->drawText(xPos + margin * 2, defaultCharHeight, userName); 0077 xPos += nameWidth; 0078 } 0079 } 0080 0081 const QString description = index.data(InputCompleterModel::Description).toString(); 0082 if (!description.isEmpty()) { 0083 QFont italicFont = oldFont; 0084 italicFont.setItalic(true); 0085 painter->setFont(italicFont); 0086 0087 painter->drawText(xPos + margin * 2, defaultCharHeight, description); 0088 } 0089 0090 painter->setFont(oldFont); 0091 0092 if (index.data(InputCompleterModel::OutsideRoom).toBool()) { 0093 fontMetrics = QFontMetrics(oldFont); 0094 const QString inRoomStr = i18n("Not in channel"); 0095 const int inRoomStrWidth = fontMetrics.horizontalAdvance(inRoomStr); 0096 0097 painter->drawText(option.rect.width() - inRoomStrWidth - margin, defaultCharHeight, inRoomStr); 0098 } 0099 } 0100 0101 void UserAndChannelCompletionDelegate::setRocketChatAccount(RocketChatAccount *newRocketChatAccount) 0102 { 0103 mAvatarCacheManager->setCurrentRocketChatAccount(newRocketChatAccount); 0104 }