matrixion/src/GridImagePackModel.h

130 lines
3.4 KiB
C
Raw Normal View History

2023-05-19 04:15:55 +03:00
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <QAbstractListModel>
2023-05-19 23:05:14 +03:00
#include <QMultiMap>
2023-05-19 04:15:55 +03:00
#include <QObject>
#include <QString>
#include <mtx/events/mscs/image_packs.hpp>
2023-05-19 23:05:14 +03:00
#include "CompletionProxyModel.h"
2023-05-19 04:15:55 +03:00
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)
2023-05-25 20:07:13 +03:00
Q_PROPERTY(QString markdown READ markdown CONSTANT)
2023-05-19 04:15:55 +03:00
public:
QStringList descriptor() const
{
if (descriptor_.size() == 3)
return QStringList{
QString::fromStdString(descriptor_[0]),
QString::fromStdString(descriptor_[1]),
QString::fromStdString(descriptor_[2]),
};
else
return {};
}
2023-05-25 20:07:13 +03:00
QString markdown() const
{
return QStringLiteral(
"<img data-mx-emoticon height=\"32\" src=\"%1\" alt=\"%2\" title=\"%2\">")
.arg(url.toHtmlEscaped(), !body.isEmpty() ? body : shortcode);
}
2023-05-19 04:15:55 +03:00
QString url;
QString shortcode;
QString body;
std::vector<std::string> descriptor_; // roomid, statekey, shortcode
};
struct SectionDescription
{
Q_GADGET
Q_PROPERTY(QString url MEMBER url CONSTANT)
Q_PROPERTY(QString name MEMBER name CONSTANT)
Q_PROPERTY(int firstRowWith MEMBER firstRowWith CONSTANT)
public:
QString name;
QString url;
int firstRowWith = 0;
};
2023-05-25 20:07:13 +03:00
struct TextEmoji
{
Q_GADGET
Q_PROPERTY(QString unicode MEMBER unicode CONSTANT)
Q_PROPERTY(QString unicodeName MEMBER unicodeName CONSTANT)
Q_PROPERTY(QString shortcode MEMBER shortcode CONSTANT)
public:
QString unicode;
QString unicodeName;
QString shortcode;
};
2023-05-19 04:15:55 +03:00
class GridImagePackModel final : public QAbstractListModel
{
Q_OBJECT
2023-05-19 23:05:14 +03:00
Q_PROPERTY(QString searchString READ searchString WRITE setSearchString NOTIFY newSearchString)
Q_PROPERTY(QList<SectionDescription> sections READ sections NOTIFY newSearchString)
2023-05-19 23:05:14 +03:00
2023-05-19 04:15:55 +03:00
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;
2023-05-19 23:05:14 +03:00
QString searchString() const { return searchString_; }
void setSearchString(QString newValue);
QList<SectionDescription> sections() const;
2023-05-19 23:05:14 +03:00
signals:
void newSearchString();
2023-05-19 04:15:55 +03:00
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;
2023-05-25 20:07:13 +03:00
std::vector<TextEmoji> emojis;
2023-05-19 04:15:55 +03:00
std::size_t firstRow;
};
std::vector<PackDesc> packs;
std::vector<size_t> rowToPack;
int columns = 3;
2023-05-19 23:05:14 +03:00
QString searchString_;
trie<uint, std::pair<std::uint32_t, std::uint32_t>> trie_;
std::vector<std::pair<std::uint32_t, std::uint32_t>> currentSearchResult;
std::vector<std::size_t> rowToFirstRowEntryFromSearch;
QString nameFromPack(const PackDesc &pack) const;
QString avatarFromPack(const PackDesc &pack) const;
2023-05-19 04:15:55 +03:00
};