matrixion/src/CombinedImagePackModel.cpp

106 lines
3.6 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: Nheko Contributors
2021-07-15 21:37:52 +03:00
//
// SPDX-License-Identifier: GPL-3.0-or-later
2021-07-21 02:03:38 +03:00
#include "CombinedImagePackModel.h"
2021-07-15 21:37:52 +03:00
#include "Cache_p.h"
#include "CompletionModelRoles.h"
#include "emoji/Provider.h"
2021-07-15 21:37:52 +03:00
CombinedImagePackModel::CombinedImagePackModel(const std::string &roomId, QObject *parent)
2021-07-15 21:37:52 +03:00
: QAbstractListModel(parent)
, room_id(roomId)
{
auto packs = cache::client()->getImagePacks(room_id, false);
2021-07-15 21:37:52 +03:00
2021-09-18 01:22:33 +03:00
for (const auto &pack : packs) {
QString packname =
pack.pack.pack ? QString::fromStdString(pack.pack.pack->display_name) : QString();
2021-07-15 21:37:52 +03:00
2021-09-18 01:22:33 +03:00
for (const auto &img : pack.pack.images) {
ImageDesc i{};
i.shortcode = QString::fromStdString(img.first);
i.packname = packname;
i.image = img.second;
images.push_back(std::move(i));
2021-07-15 21:37:52 +03:00
}
2021-09-18 01:22:33 +03:00
}
2021-07-15 21:37:52 +03:00
}
2021-07-19 13:43:16 +03:00
int
2021-07-21 02:03:38 +03:00
CombinedImagePackModel::rowCount(const QModelIndex &) const
2021-07-19 13:43:16 +03:00
{
return (int)(emoji::Provider::emoji.size() + images.size());
2021-07-19 13:43:16 +03:00
}
2021-07-15 21:37:52 +03:00
QHash<int, QByteArray>
2021-07-21 02:03:38 +03:00
CombinedImagePackModel::roleNames() const
2021-07-15 21:37:52 +03:00
{
2021-09-18 01:22:33 +03:00
return {
{CompletionModel::CompletionRole, "completionRole"},
{CompletionModel::SearchRole, "searchRole"},
{CompletionModel::SearchRole2, "searchRole2"},
{Roles::Url, "url"},
{Roles::ShortCode, "shortcode"},
{Roles::Body, "body"},
{Roles::PackName, "packname"},
{Roles::Unicode, "unicode"},
2021-09-18 01:22:33 +03:00
};
2021-07-15 21:37:52 +03:00
}
QVariant
2021-07-21 02:03:38 +03:00
CombinedImagePackModel::data(const QModelIndex &index, int role) const
2021-07-15 21:37:52 +03:00
{
using emoji::Provider;
2021-09-18 01:22:33 +03:00
if (hasIndex(index.row(), index.column(), index.parent())) {
if (index.row() < (int)emoji::Provider::emoji.size()) {
switch (role) {
case CompletionModel::CompletionRole:
case Roles::Unicode:
return emoji::Provider::emoji[index.row()].unicode();
case Qt::ToolTipRole:
return Provider::emoji[index.row()].shortName() + ", " +
Provider::emoji[index.row()].unicodeName();
case CompletionModel::SearchRole2:
case Roles::Body:
return Provider::emoji[index.row()].unicodeName();
case CompletionModel::SearchRole:
case Roles::ShortCode:
return Provider::emoji[index.row()].shortName();
case Roles::PackName:
return emoji::categoryToName(Provider::emoji[index.row()].category);
default:
return {};
}
} else {
int row = index.row() - static_cast<int>(emoji::Provider::emoji.size());
switch (role) {
case CompletionModel::CompletionRole:
return QStringLiteral(
"<img data-mx-emoticon height=\"32\" src=\"%1\" alt=\"%2\" title=\"%2\">")
.arg(QString::fromStdString(images[row].image.url).toHtmlEscaped(),
!images[row].image.body.empty()
? QString::fromStdString(images[row].image.body)
: images[row].shortcode);
case Roles::Url:
return QString::fromStdString(images[row].image.url);
case CompletionModel::SearchRole:
case Roles::ShortCode:
return images[row].shortcode;
case CompletionModel::SearchRole2:
case Roles::Body:
return QString::fromStdString(images[row].image.body);
case Roles::PackName:
return images[row].packname;
case Roles::Unicode:
return QString();
default:
return {};
}
2021-07-15 21:37:52 +03:00
}
2021-09-18 01:22:33 +03:00
}
return {};
2021-07-15 21:37:52 +03:00
}