Create better EmojiModel
This commit is contained in:
@@ -3,13 +3,14 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QObject>
|
||||
#include <QSettings>
|
||||
|
||||
struct Emoji {
|
||||
Emoji(QString u, QString s, bool isCustom = false)
|
||||
: unicode(std::move(std::move(u)))
|
||||
, shortname(std::move(std::move(s)))
|
||||
, shortName(std::move(std::move(s)))
|
||||
, isCustom(isCustom)
|
||||
{
|
||||
}
|
||||
@@ -18,53 +19,74 @@ struct Emoji {
|
||||
friend QDataStream &operator<<(QDataStream &arch, const Emoji &object)
|
||||
{
|
||||
arch << object.unicode;
|
||||
arch << object.shortname;
|
||||
arch << object.shortName;
|
||||
return arch;
|
||||
}
|
||||
|
||||
friend QDataStream &operator>>(QDataStream &arch, Emoji &object)
|
||||
{
|
||||
arch >> object.unicode;
|
||||
arch >> object.shortname;
|
||||
arch >> object.shortName;
|
||||
object.isCustom = object.unicode.startsWith("image://");
|
||||
return arch;
|
||||
}
|
||||
|
||||
QString unicode;
|
||||
QString shortname;
|
||||
QString shortName;
|
||||
bool isCustom = false;
|
||||
|
||||
Q_GADGET
|
||||
Q_PROPERTY(QString unicode MEMBER unicode)
|
||||
Q_PROPERTY(QString shortname MEMBER shortname)
|
||||
Q_PROPERTY(QString shortName MEMBER shortName)
|
||||
Q_PROPERTY(bool isCustom MEMBER isCustom)
|
||||
};
|
||||
|
||||
Q_DECLARE_METATYPE(Emoji)
|
||||
|
||||
class EmojiModel : public QObject
|
||||
class EmojiModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(QVariantList history READ history NOTIFY historyChanged)
|
||||
|
||||
Q_PROPERTY(QVariantList people MEMBER people CONSTANT)
|
||||
Q_PROPERTY(QVariantList nature MEMBER nature CONSTANT)
|
||||
Q_PROPERTY(QVariantList food MEMBER food CONSTANT)
|
||||
Q_PROPERTY(QVariantList activity MEMBER activity CONSTANT)
|
||||
Q_PROPERTY(QVariantList travel MEMBER travel CONSTANT)
|
||||
Q_PROPERTY(QVariantList objects MEMBER objects CONSTANT)
|
||||
Q_PROPERTY(QVariantList symbols MEMBER symbols CONSTANT)
|
||||
Q_PROPERTY(QVariantList flags MEMBER flags CONSTANT)
|
||||
Q_PROPERTY(QVariantList categories READ categories CONSTANT)
|
||||
|
||||
public:
|
||||
explicit EmojiModel(QObject *parent = nullptr)
|
||||
: QObject(parent)
|
||||
{
|
||||
}
|
||||
explicit EmojiModel(QObject *parent = nullptr);
|
||||
|
||||
Q_INVOKABLE QVariantList history();
|
||||
Q_INVOKABLE static QVariantList filterModel(const QString &filter);
|
||||
enum RoleNames {
|
||||
TextRole = Qt::DisplayRole,
|
||||
UnicodeRole,
|
||||
};
|
||||
Q_ENUM(RoleNames);
|
||||
|
||||
enum Category {
|
||||
Custom,
|
||||
Search,
|
||||
History,
|
||||
Smileys,
|
||||
People,
|
||||
Nature,
|
||||
Food,
|
||||
Activities,
|
||||
Travel,
|
||||
Objects,
|
||||
Symbols,
|
||||
Flags,
|
||||
Component,
|
||||
};
|
||||
Q_ENUM(Category)
|
||||
|
||||
[[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
[[nodiscard]] QVariant data(const QModelIndex &idx, int role = Qt::DisplayRole) const override;
|
||||
[[nodiscard]] QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
Q_INVOKABLE QVariantList history() const;
|
||||
Q_INVOKABLE static QVariantList filterModel(const QString &filter, bool limit = true);
|
||||
Q_INVOKABLE QVariantList emojis(Category category) const;
|
||||
|
||||
Q_INVOKABLE QVariantList tones(const QString &baseEmoji) const;
|
||||
|
||||
QVariantList categories() const;
|
||||
|
||||
Q_SIGNALS:
|
||||
void historyChanged();
|
||||
@@ -73,14 +95,8 @@ public Q_SLOTS:
|
||||
void emojiUsed(const QVariant &modelData);
|
||||
|
||||
private:
|
||||
static const QVariantList people;
|
||||
static const QVariantList nature;
|
||||
static const QVariantList food;
|
||||
static const QVariantList activity;
|
||||
static const QVariantList travel;
|
||||
static const QVariantList objects;
|
||||
static const QVariantList symbols;
|
||||
static const QVariantList flags;
|
||||
static QHash<Category, QVariantList> _emojis;
|
||||
static QMultiHash<QString, QVariant> _tones;
|
||||
|
||||
// TODO: Port away from QSettings
|
||||
QSettings m_settings;
|
||||
|
||||
Reference in New Issue
Block a user