File indexing completed on 2024-12-01 04:36:37

0001 /*
0002    SPDX-FileCopyrightText: 2020 David Faure <faure@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "emojicompletiondelegate.h"
0008 #include "common/delegatepaintutil.h"
0009 #include "model/emoticonmodel.h"
0010 #include "utils.h"
0011 
0012 #include <QPainter>
0013 
0014 EmojiCompletionDelegate::EmojiCompletionDelegate(QObject *parent)
0015     : QItemDelegate(parent)
0016     , mEmojiFont(Utils::emojiFontName())
0017 {
0018 }
0019 
0020 EmojiCompletionDelegate::~EmojiCompletionDelegate() = default;
0021 
0022 void EmojiCompletionDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0023 {
0024     // [M] <unicode emoji> <:identifier:>     ([M] = margin)
0025     drawBackground(painter, option, index);
0026     const int margin = DelegatePaintUtil::margin();
0027     const QFontMetricsF emojiFontMetrics(mEmojiFont);
0028 
0029     const QIcon icon = index.data(EmoticonModel::IconRole).value<QIcon>();
0030     const QString emojiText = index.data(EmoticonModel::UnicodeEmojiRole).toString();
0031     if (!icon.isNull()) {
0032         const int emojiWidth = emojiFontMetrics.horizontalAdvance(QStringLiteral("MM"));
0033         const QRect displayRect(margin, option.rect.y(), emojiWidth, option.rect.height());
0034         drawDecoration(painter, option, displayRect, icon.pixmap(emojiWidth, option.rect.height()));
0035         painter->drawText(margin + emojiWidth, option.rect.y() + emojiFontMetrics.ascent(), emojiText);
0036     } else {
0037         const int emojiWidth = emojiFontMetrics.horizontalAdvance(emojiText);
0038         painter->setFont(mEmojiFont);
0039         painter->drawText(margin, option.rect.y() + emojiFontMetrics.ascent(), emojiText);
0040         const QString text = index.data(EmoticonModel::IdentifierRole).toString();
0041         const int xText = option.rect.x() + margin + emojiWidth;
0042         const QRect displayRect(xText, option.rect.y(), option.rect.width() - xText, option.rect.height());
0043         drawDisplay(painter, option, displayRect, text);
0044     }
0045 }