File indexing completed on 2024-05-12 16:27:37

0001 /*
0002    SPDX-FileCopyrightText: 2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "switchchanneldelegate.h"
0008 #include "common/delegatepaintutil.h"
0009 #include "misc/avatarcachemanager.h"
0010 #include "model/switchchannelhistorymodel.h"
0011 #include "rocketchataccount.h"
0012 
0013 #include <QPainter>
0014 
0015 namespace
0016 {
0017 constexpr uint padding = 2;
0018 }
0019 
0020 SwitchChannelDelegate::SwitchChannelDelegate(QObject *parent)
0021     : QItemDelegate(parent)
0022     , mAvatarCacheManager(new AvatarCacheManager(Utils::AvatarType::Room, this))
0023 {
0024     mAvatarCacheManager->setMaxEntries(15);
0025 }
0026 
0027 SwitchChannelDelegate::~SwitchChannelDelegate() = default;
0028 
0029 void SwitchChannelDelegate::setCurrentRocketChatAccount(RocketChatAccount *currentRocketChatAccount)
0030 {
0031     mAvatarCacheManager->setCurrentRocketChatAccount(currentRocketChatAccount);
0032     mAvatarCacheManager->clearCache();
0033     mRocketChatAccount = currentRocketChatAccount;
0034 }
0035 
0036 void SwitchChannelDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0037 {
0038     // [M] <icon> [M] <name>
0039     drawBackground(painter, option, index);
0040     const QString text = index.data(SwitchChannelHistoryModel::Name).toString();
0041     const int margin = DelegatePaintUtil::margin();
0042     int xPos = 0;
0043     const Utils::AvatarInfo info = index.data(SwitchChannelHistoryModel::AvatarInfo).value<Utils::AvatarInfo>();
0044     if (info.isValid()) {
0045         const QRect displayRect(margin, option.rect.y(), option.rect.height(), option.rect.height());
0046         const QPixmap pix = mAvatarCacheManager->makeAvatarPixmap(option.widget, info, option.rect.height());
0047         if (!pix.isNull()) {
0048             drawDecoration(painter, option, displayRect, pix);
0049             xPos = margin + option.rect.height();
0050         }
0051     }
0052     const int xText = option.rect.x() + margin + xPos;
0053 
0054     const QRect displayRect(xText, option.rect.y(), option.rect.width() - xText, option.rect.height());
0055     drawDisplay(painter, option, displayRect, text);
0056 }
0057 
0058 QSize SwitchChannelDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
0059 {
0060     return QItemDelegate::sizeHint(option, index) + QSize(0, 2 * padding);
0061 }
0062 
0063 #include "moc_switchchanneldelegate.cpp"