2023-01-20 16:43:29 +00:00
|
|
|
// Copyright (C) 2023 The Qt Company Ltd.
|
|
|
|
// Copyright (C) 2019 Alexey Edelev <semlanik@gmail.com>
|
2024-03-15 13:09:34 +00:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2023-01-20 16:43:29 +00:00
|
|
|
|
|
|
|
#include <QImage>
|
|
|
|
#include <QBuffer>
|
|
|
|
|
|
|
|
#include "chatmessagemodel.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
enum Role {
|
|
|
|
Content = Qt::UserRole + 1,
|
|
|
|
Type,
|
|
|
|
From,
|
|
|
|
};
|
|
|
|
|
|
|
|
QString getImage(const QByteArray &data)
|
|
|
|
{
|
|
|
|
return QString("data:image/png;base64,") + data.toBase64();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString getImageScaled(const QByteArray &data)
|
|
|
|
{
|
|
|
|
QImage img = QImage::fromData(data, "PNG");
|
|
|
|
img = img.scaled(300, 300, Qt::KeepAspectRatio);
|
|
|
|
QByteArray scaledData;
|
|
|
|
QBuffer buffer(&scaledData);
|
|
|
|
img.save(&buffer, "PNG");
|
|
|
|
return getImage(scaledData);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString getText(const QByteArray &data)
|
|
|
|
{
|
|
|
|
return QString::fromUtf8(data);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
ChatMessageModel::ChatMessageModel(QObject *parent) : QAbstractListModel(parent) { }
|
|
|
|
|
|
|
|
QHash<int, QByteArray> ChatMessageModel::roleNames() const
|
|
|
|
{
|
|
|
|
static QHash<int, QByteArray> s_roleNames;
|
|
|
|
if (s_roleNames.isEmpty()) {
|
|
|
|
s_roleNames.insert(Content, "content");
|
|
|
|
s_roleNames.insert(Type, "type");
|
|
|
|
s_roleNames.insert(From, "from");
|
|
|
|
}
|
|
|
|
return s_roleNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ChatMessageModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
int row = index.row();
|
|
|
|
|
2023-05-19 17:11:39 +00:00
|
|
|
if (row < 0 || row >= m_container.count())
|
2023-01-20 16:43:29 +00:00
|
|
|
return QVariant();
|
|
|
|
|
2023-05-19 17:11:39 +00:00
|
|
|
const auto &data = m_container.at(row);
|
2023-01-20 16:43:29 +00:00
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case Content:
|
2023-08-05 15:10:53 +00:00
|
|
|
if (data.type() == qtgrpc::examples::chat::ChatMessage::ContentType::Image)
|
2023-05-19 17:11:39 +00:00
|
|
|
return QVariant::fromValue(getImageScaled(data.content()));
|
2023-01-20 16:43:29 +00:00
|
|
|
else
|
2023-05-19 17:11:39 +00:00
|
|
|
return QVariant::fromValue(getText(data.content()));
|
2023-01-20 16:43:29 +00:00
|
|
|
case Type:
|
2023-05-19 17:11:39 +00:00
|
|
|
return QVariant::fromValue(data.type());
|
2023-01-20 16:43:29 +00:00
|
|
|
case From:
|
2023-05-19 17:11:39 +00:00
|
|
|
return QVariant::fromValue(data.from());
|
2023-01-20 16:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ChatMessageModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return m_container.count();
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:11:39 +00:00
|
|
|
void ChatMessageModel::append(const qtgrpc::examples::chat::ChatMessageRepeated &messages)
|
2023-01-20 16:43:29 +00:00
|
|
|
{
|
|
|
|
if (messages.size() > 0) {
|
|
|
|
beginInsertRows(QModelIndex(), m_container.size(),
|
|
|
|
m_container.size() + messages.size() - 1);
|
|
|
|
m_container.append(messages);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
}
|