| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <QAbstractListModel> |
| 8 |
#include <QMultiMap> |
| 9 |
#include <QObject> |
| 10 |
#include <QString> |
| 11 |
|
| 12 |
#include <mtx/events/mscs/image_packs.hpp> |
| 13 |
|
| 14 |
#include "CompletionProxyModel.h" |
| 15 |
|
| 16 |
struct StickerImage |
| 17 |
{ |
| 18 |
Q_GADGET |
| 19 |
Q_PROPERTY(QString url MEMBER url CONSTANT) |
| 20 |
Q_PROPERTY(QString shortcode MEMBER shortcode CONSTANT) |
| 21 |
Q_PROPERTY(QString body MEMBER body CONSTANT) |
| 22 |
Q_PROPERTY(QStringList descriptor READ descriptor CONSTANT) |
| 23 |
Q_PROPERTY(QString markdown READ markdown CONSTANT) |
| 24 |
|
| 25 |
public: |
| 26 |
QStringList descriptor() const |
| 27 |
{ |
| 28 |
if (descriptor_.size() == 3) |
| 29 |
return QStringList{ |
| 30 |
QString::fromStdString(descriptor_[0]), |
| 31 |
QString::fromStdString(descriptor_[1]), |
| 32 |
QString::fromStdString(descriptor_[2]), |
| 33 |
}; |
| 34 |
else |
| 35 |
return {}; |
| 36 |
} |
| 37 |
|
| 38 |
QString markdown() const |
| 39 |
{ |
| 40 |
return QStringLiteral( |
| 41 |
"<img data-mx-emoticon height=\"32\" src=\"%1\" alt=\"%2\" title=\"%2\">") |
| 42 |
.arg(url.toHtmlEscaped(), !body.isEmpty() ? body : shortcode); |
| 43 |
} |
| 44 |
|
| 45 |
QString url; |
| 46 |
QString shortcode; |
| 47 |
QString body; |
| 48 |
|
| 49 |
std::vector<std::string> descriptor_; |
| 50 |
}; |
| 51 |
|
| 52 |
struct SectionDescription |
| 53 |
{ |
| 54 |
Q_GADGET |
| 55 |
Q_PROPERTY(QString url MEMBER url CONSTANT) |
| 56 |
Q_PROPERTY(QString name MEMBER name CONSTANT) |
| 57 |
Q_PROPERTY(int firstRowWith MEMBER firstRowWith CONSTANT) |
| 58 |
|
| 59 |
public: |
| 60 |
QString name; |
| 61 |
QString url; |
| 62 |
int firstRowWith = 0; |
| 63 |
}; |
| 64 |
|
| 65 |
struct TextEmoji |
| 66 |
{ |
| 67 |
Q_GADGET |
| 68 |
Q_PROPERTY(QString unicode MEMBER unicode CONSTANT) |
| 69 |
Q_PROPERTY(QString unicodeName MEMBER unicodeName CONSTANT) |
| 70 |
Q_PROPERTY(QString shortcode MEMBER shortcode CONSTANT) |
| 71 |
|
| 72 |
public: |
| 73 |
QString unicode; |
| 74 |
QString unicodeName; |
| 75 |
QString shortcode; |
| 76 |
}; |
| 77 |
|
| 78 |
class GridImagePackModel final : public QAbstractListModel |
| 79 |
{ |
| 80 |
Q_OBJECT |
| 81 |
Q_PROPERTY(QString searchString READ searchString WRITE setSearchString NOTIFY newSearchString) |
| 82 |
Q_PROPERTY(QList<SectionDescription> sections READ sections NOTIFY newSearchString) |
| 83 |
|
| 84 |
public: |
| 85 |
enum Roles |
| 86 |
{ |
| 87 |
PackName = Qt::UserRole, |
| 88 |
Row, |
| 89 |
}; |
| 90 |
|
| 91 |
GridImagePackModel(const std::string &roomId, bool stickers, QObject *parent = nullptr); |
| 92 |
QHash<int, QByteArray> roleNames() const override; |
| 93 |
int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
| 94 |
QVariant data(const QModelIndex &index, int role) const override; |
| 95 |
|
| 96 |
QString searchString() const { return searchString_; } |
| 97 |
void setSearchString(QString newValue); |
| 98 |
|
| 99 |
QList<SectionDescription> sections() const; |
| 100 |
|
| 101 |
signals: |
| 102 |
void newSearchString(); |
| 103 |
|
| 104 |
private: |
| 105 |
std::string room_id; |
| 106 |
|
| 107 |
struct PackDesc |
| 108 |
{ |
| 109 |
QString packname; |
| 110 |
QString packavatar; |
| 111 |
std::string room_id, state_key; |
| 112 |
|
| 113 |
std::vector<std::pair<mtx::events::msc2545::PackImage, QString>> images; |
| 114 |
std::vector<TextEmoji> emojis; |
| 115 |
std::size_t firstRow; |
| 116 |
}; |
| 117 |
|
| 118 |
std::vector<PackDesc> packs; |
| 119 |
std::vector<size_t> rowToPack; |
| 120 |
int columns = 3; |
| 121 |
|
| 122 |
QString searchString_; |
| 123 |
trie<uint, std::pair<std::uint32_t, std::uint32_t>> trie_; |
| 124 |
std::vector<std::pair<std::uint32_t, std::uint32_t>> currentSearchResult; |
| 125 |
std::vector<std::size_t> rowToFirstRowEntryFromSearch; |
| 126 |
|
| 127 |
QString nameFromPack(const PackDesc &pack) const; |
| 128 |
QString avatarFromPack(const PackDesc &pack) const; |
| 129 |
}; |