matrixion/src/CombinedImagePackModel.cpp

80 lines
2.5 KiB
C++
Raw Normal View History

2021-07-15 21:37:52 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// 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"
2021-07-21 02:03:38 +03:00
CombinedImagePackModel::CombinedImagePackModel(const std::string &roomId,
bool stickers,
QObject *parent)
2021-07-15 21:37:52 +03:00
: QAbstractListModel(parent)
, room_id(roomId)
{
2021-09-18 01:22:33 +03:00
auto packs = cache::client()->getImagePacks(room_id, stickers);
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) : "";
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
{
2021-09-18 01:22:33 +03:00
return (int)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::OriginalRow, "originalRow"},
};
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
{
2021-09-18 01:22:33 +03:00
if (hasIndex(index.row(), index.column(), index.parent())) {
switch (role) {
case CompletionModel::CompletionRole:
return QString("<img data-mx-emoticon height=32 src=\"%1\" alt=\"%2\" title=\"%2\">")
.arg(QString::fromStdString(images[index.row()].image.url).toHtmlEscaped(),
2021-12-27 21:08:13 +03:00
QString::fromStdString(images[index.row()].image.body));
2021-09-18 01:22:33 +03:00
case Roles::Url:
return QString::fromStdString(images[index.row()].image.url);
case CompletionModel::SearchRole:
case Roles::ShortCode:
return images[index.row()].shortcode;
case CompletionModel::SearchRole2:
case Roles::Body:
return QString::fromStdString(images[index.row()].image.body);
case Roles::PackName:
return images[index.row()].packname;
case Roles::OriginalRow:
return index.row();
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
}