Page MenuHomePhorge

matrix-sticker-pack.cpp
No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None

matrix-sticker-pack.cpp

/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2020-2023 tusooa <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <libkazv-config.hpp>
#include <immer/config.hpp>
#include <cursorutil.hpp>
#include "matrix-sticker.hpp"
#include "matrix-sticker-pack.hpp"
using namespace Kazv;
MatrixStickerPack::MatrixStickerPack(lager::reader<MatrixStickerPackSource> source, QObject *parent)
: QAbstractListModel(parent)
, m_event(source[&MatrixStickerPackSource::event])
, m_images(m_event.xform(
eventContent
| zug::map([](const JsonWrap &content) {
if (content.get().contains("images")
&& content.get()["images"].is_object()) {
auto size = content.get()["images"].size();
auto items = content.get()["images"].items();
auto array = json(size, json::object());
std::transform(
items.begin(), items.end(),
array.begin(), [](const auto &it) {
return json::array({it.key(), it.value()});
}
);
return array;
} else {
return json::array();
}
})))
, m_internalCount(0)
, LAGER_QT(count)(m_images.map([](const JsonWrap &images) -> int {
return images.get().size();
}))
{
m_internalCount = count();
connect(this, &MatrixStickerPack::countChanged, this, &MatrixStickerPack::updateInternalCount);
}
MatrixStickerPack::~MatrixStickerPack() = default;
MatrixSticker *MatrixStickerPack::at(int index) const
{
using namespace nlohmann::literals;
auto image = m_images.map([index](const json &j) {
if (j.size() > std::size_t(index)) {
return j[index];
} else {
return json::array();
}
}).make();
return new MatrixSticker(
image.xform(jsonAtOr("/0"_json_pointer, json(std::string())) | zug::map([](const json &j) {
return j.template get<std::string>();
})),
image.xform(jsonAtOr("/1"_json_pointer, json::object()))
);
}
QVariant MatrixStickerPack::data(const QModelIndex &/* index */, int /* role */) const
{
return QVariant();
}
int MatrixStickerPack::rowCount(const QModelIndex &parent = QModelIndex()) const
{
if (parent.isValid()) {
return 0;
} else {
return count();
}
}
void MatrixStickerPack::updateInternalCount()
{
auto curCount = count();
auto oldCount = m_internalCount;
auto diff = std::abs(curCount - oldCount);
if (curCount > oldCount) {
beginInsertRows(QModelIndex(), 0, diff - 1);
m_internalCount = curCount;
endInsertRows();
} else if (curCount < oldCount) {
beginRemoveRows(QModelIndex(), 0, diff - 1);
m_internalCount = curCount;
endRemoveRows();
}
}

File Metadata

Mime Type
text/x-c
Expires
Sun, Dec 7, 7:59 PM (1 d, 16 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
699458
Default Alt Text
matrix-sticker-pack.cpp (2 KB)

Event Timeline