mirror of https://github.com/Nheko-Reborn/nheko
parent
4a060b59b2
commit
0dfdba4316
@ -0,0 +1,88 @@ |
||||
// SPDX-FileCopyrightText: Nheko Contributors
|
||||
//
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "GridImagePackModel.h" |
||||
|
||||
#include "Cache_p.h" |
||||
#include "CompletionModelRoles.h" |
||||
|
||||
#include <algorithm> |
||||
|
||||
Q_DECLARE_METATYPE(StickerImage) |
||||
|
||||
GridImagePackModel::GridImagePackModel(const std::string &roomId, bool stickers, QObject *parent) |
||||
: QAbstractListModel(parent) |
||||
, room_id(roomId) |
||||
{ |
||||
[[maybe_unused]] static auto id = qRegisterMetaType<StickerImage>(); |
||||
|
||||
auto originalPacks = cache::client()->getImagePacks(room_id, stickers); |
||||
|
||||
for (auto &pack : originalPacks) { |
||||
PackDesc newPack{}; |
||||
newPack.packname = |
||||
pack.pack.pack ? QString::fromStdString(pack.pack.pack->display_name) : QString(); |
||||
newPack.room_id = pack.source_room; |
||||
newPack.state_key = pack.state_key; |
||||
|
||||
newPack.images.resize(pack.pack.images.size()); |
||||
std::ranges::transform(std::move(pack.pack.images), newPack.images.begin(), [](auto &&img) { |
||||
return std::pair(std::move(img.second), QString::fromStdString(img.first)); |
||||
}); |
||||
|
||||
size_t packRowCount = |
||||
(newPack.images.size() / columns) + (newPack.images.size() % columns ? 1 : 0); |
||||
newPack.firstRow = rowToPack.size(); |
||||
for (size_t i = 0; i < packRowCount; i++) |
||||
rowToPack.push_back(packs.size()); |
||||
packs.push_back(std::move(newPack)); |
||||
} |
||||
} |
||||
|
||||
int |
||||
GridImagePackModel::rowCount(const QModelIndex &) const |
||||
{ |
||||
return (int)rowToPack.size(); |
||||
} |
||||
|
||||
QHash<int, QByteArray> |
||||
GridImagePackModel::roleNames() const |
||||
{ |
||||
return { |
||||
{Roles::PackName, "packname"}, |
||||
{Roles::Row, "row"}, |
||||
}; |
||||
} |
||||
|
||||
QVariant |
||||
GridImagePackModel::data(const QModelIndex &index, int role) const |
||||
{ |
||||
if (index.row() < rowCount() && index.row() >= 0) { |
||||
const auto &pack = packs[rowToPack[index.row()]]; |
||||
switch (role) { |
||||
case Roles::PackName: |
||||
return pack.packname; |
||||
case Roles::Row: { |
||||
std::size_t offset = static_cast<std::size_t>(index.row()) - pack.firstRow; |
||||
QList<StickerImage> imgs; |
||||
auto endOffset = std::min((offset + 1) * 3, pack.images.size()); |
||||
for (std::size_t img = offset * 3; img < endOffset; img++) { |
||||
const auto &data = pack.images.at(img); |
||||
imgs.push_back({.url = QString::fromStdString(data.first.url), |
||||
.shortcode = data.second, |
||||
.body = QString::fromStdString(data.first.body), |
||||
.descriptor_ = std::vector{ |
||||
pack.room_id, |
||||
pack.state_key, |
||||
data.second.toStdString(), |
||||
}}); |
||||
} |
||||
return QVariant::fromValue(imgs); |
||||
} |
||||
default: |
||||
return {}; |
||||
} |
||||
} |
||||
return {}; |
||||
} |
@ -0,0 +1,72 @@ |
||||
// SPDX-FileCopyrightText: Nheko Contributors
|
||||
//
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once |
||||
|
||||
#include <QAbstractListModel> |
||||
#include <QObject> |
||||
#include <QString> |
||||
|
||||
#include <mtx/events/mscs/image_packs.hpp> |
||||
|
||||
struct StickerImage |
||||
{ |
||||
Q_GADGET |
||||
Q_PROPERTY(QString url MEMBER url CONSTANT) |
||||
Q_PROPERTY(QString shortcode MEMBER shortcode CONSTANT) |
||||
Q_PROPERTY(QString body MEMBER body CONSTANT) |
||||
Q_PROPERTY(QStringList descriptor READ descriptor CONSTANT) |
||||
|
||||
public: |
||||
QStringList descriptor() const |
||||
{ |
||||
if (descriptor_.size() == 3) |
||||
return QStringList{ |
||||
QString::fromStdString(descriptor_[0]), |
||||
QString::fromStdString(descriptor_[1]), |
||||
QString::fromStdString(descriptor_[2]), |
||||
}; |
||||
else |
||||
return {}; |
||||
} |
||||
|
||||
QString url; |
||||
QString shortcode; |
||||
QString body; |
||||
|
||||
std::vector<std::string> descriptor_; // roomid, statekey, shortcode
|
||||
}; |
||||
|
||||
class GridImagePackModel final : public QAbstractListModel |
||||
{ |
||||
Q_OBJECT |
||||
public: |
||||
enum Roles |
||||
{ |
||||
PackName = Qt::UserRole, |
||||
Row, |
||||
}; |
||||
|
||||
GridImagePackModel(const std::string &roomId, bool stickers, QObject *parent = nullptr); |
||||
QHash<int, QByteArray> roleNames() const override; |
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
||||
QVariant data(const QModelIndex &index, int role) const override; |
||||
|
||||
private: |
||||
std::string room_id; |
||||
|
||||
struct PackDesc |
||||
{ |
||||
QString packname; |
||||
QString packavatar; |
||||
std::string room_id, state_key; |
||||
|
||||
std::vector<std::pair<mtx::events::msc2545::PackImage, QString>> images; |
||||
std::size_t firstRow; |
||||
}; |
||||
|
||||
std::vector<PackDesc> packs; |
||||
std::vector<size_t> rowToPack; |
||||
int columns = 3; |
||||
}; |
Loading…
Reference in new issue