KTextAddons

emoticonitemdelegate.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "emoticonitemdelegate.h"
8#include <QAbstractItemView>
9#include <QMovie>
10#include <QPainter>
11#include <TextEmoticonsCore/EmojiModel>
12using namespace TextEmoticonsWidgets;
13EmoticonItemDelegate::EmoticonItemDelegate(QObject *parent)
14 : QStyledItemDelegate{parent}
15{
16}
17
18EmoticonItemDelegate::~EmoticonItemDelegate() = default;
19
20void EmoticonItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
21{
22 const bool imageAnimated = index.data(TextEmoticonsCore::EmojiModel::Animated).toBool();
23 if (imageAnimated) {
24 const QString imageAnimatedFilename = index.data(TextEmoticonsCore::EmojiModel::AnimatedFileName).toString();
25 auto it = findRunningAnimatedImage(index);
26 QPixmap scaledPixmap;
27 if (it != mRunningAnimatedImages.end()) {
28 scaledPixmap = (*it).movie->currentPixmap();
29 } else {
30 mRunningAnimatedImages.emplace_back(index);
31 auto &rai = mRunningAnimatedImages.back();
32 rai.movie->setFileName(imageAnimatedFilename);
33 rai.movie->setScaledSize(option.decorationSize);
34 auto view = qobject_cast<QAbstractItemView *>(const_cast<QWidget *>(option.widget));
35 const QPersistentModelIndex &idx = rai.index;
37 rai.movie,
39 view,
40 [view, idx, this]() {
41 if (view->viewport()->rect().contains(view->visualRect(idx))) {
42 view->update(idx);
43 } else {
44 removeRunningAnimatedImage(idx);
45 }
46 },
48 rai.movie->start();
49 scaledPixmap = rai.movie->currentPixmap();
50 }
51 scaledPixmap.setDevicePixelRatio(option.widget->devicePixelRatioF());
52 painter->drawPixmap(QRect(option.rect.topLeft(), option.decorationSize), scaledPixmap);
53 } else {
54 QStyledItemDelegate::paint(painter, option, index);
55 }
56}
57
58std::vector<TextEmoticonsCore::RunningAnimatedImage>::iterator EmoticonItemDelegate::findRunningAnimatedImage(const QModelIndex &index) const
59{
60 auto matchesIndex = [&](const TextEmoticonsCore::RunningAnimatedImage &rai) {
61 return rai.index == index;
62 };
63 return std::find_if(mRunningAnimatedImages.begin(), mRunningAnimatedImages.end(), matchesIndex);
64}
65
66void EmoticonItemDelegate::removeRunningAnimatedImage(const QModelIndex &index) const
67{
68 auto it = findRunningAnimatedImage(index);
69 if (it != mRunningAnimatedImages.end()) {
70 mRunningAnimatedImages.erase(it);
71 }
72}
73
74#include "moc_emoticonitemdelegate.cpp"
QVariant data(int role) const const
void frameChanged(int frameNumber)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
void drawPixmap(const QPoint &point, const QPixmap &pixmap)
void setDevicePixelRatio(qreal scaleFactor)
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const const override
QueuedConnection
bool toBool() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:49:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.