Commit f9a4bc85 authored by Filipe Azevedo's avatar Filipe Azevedo

Add support for favorites emojis

parent 105c8444
/*
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
* Copyright (C) 2019 Filipe Azevedo <pasnox@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -16,6 +17,11 @@
*/
#include "EmojiModel.h"
#include "Kaidan.h"
#include <QSettings>
#define FAVORITES_EMOJIS_SETTINGS_PATH "Emojis/Favorites"
static const QVector<Emoji> s_emojis {
// People
......@@ -1492,6 +1498,19 @@ QHash<int, QByteArray> EmojiModel::roleNames() const
return roles;
}
EmojiProxyModel::EmojiProxyModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
QSettings *settings = Kaidan::instance()->getSettings();
m_favoriteEmojis = settings->value(QLatin1String(FAVORITES_EMOJIS_SETTINGS_PATH)).toStringList().toSet();
}
EmojiProxyModel::~EmojiProxyModel()
{
QSettings *settings = Kaidan::instance()->getSettings();
settings->setValue(QLatin1String(FAVORITES_EMOJIS_SETTINGS_PATH), QStringList(m_favoriteEmojis.toList()));
}
Emoji::Group EmojiProxyModel::group() const
{
return m_group;
......@@ -1524,12 +1543,27 @@ void EmojiProxyModel::setFilter(const QString &filter)
emit filterChanged();
}
void EmojiProxyModel::addFavoriteEmoji(int proxyRow)
{
const Emoji emoji = index(proxyRow, 0).data(static_cast<int>(EmojiModel::Roles::Emoji)).value<Emoji>();
if (!m_favoriteEmojis.contains(emoji.unicode())) {
m_favoriteEmojis << emoji.unicode();
if (m_group == Emoji::Group::Favorites) {
invalidateFilter();
}
}
}
bool EmojiProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
const QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
const Emoji emoji = index.data(static_cast<int>(EmojiModel::Roles::Emoji)).value<Emoji>();
if (m_group != Emoji::Group::Invalid) {
if (m_group == Emoji::Group::Favorites) {
return m_favoriteEmojis.contains(emoji.unicode());
} else if (m_group != Emoji::Group::Invalid) {
return emoji.group() == m_group;
}
......
......@@ -4,6 +4,7 @@
#include <QAbstractListModel>
#include <QSortFilterProxyModel>
#include <QVector>
#include <QSet>
class Emoji
{
......@@ -16,6 +17,7 @@ class Emoji
public:
enum class Group {
Invalid = -1,
Favorites,
People,
Nature,
Food,
......@@ -70,7 +72,8 @@ class EmojiProxyModel : public QSortFilterProxyModel
Q_PROPERTY(QString filter READ filter WRITE setFilter NOTIFY filterChanged)
public:
using QSortFilterProxyModel::QSortFilterProxyModel;
explicit EmojiProxyModel(QObject *parent = nullptr);
~EmojiProxyModel() override;
Emoji::Group group() const;
void setGroup(Emoji::Group group);
......@@ -78,6 +81,9 @@ public:
QString filter() const;
void setFilter(const QString &filter);
public slots:
void addFavoriteEmoji(int proxyRow);
signals:
void groupChanged();
void filterChanged();
......@@ -87,6 +93,7 @@ protected:
private:
Emoji::Group m_group = Emoji::Group::Invalid;
QSet<QString> m_favoriteEmojis;
};
Q_DECLARE_METATYPE(Emoji)
......
......@@ -45,9 +45,14 @@
#include "PresenceCache.h"
#include "Utils.h"
Kaidan *Kaidan::s_instance = nullptr;
Kaidan::Kaidan(QGuiApplication *app, bool enableLogging, QObject *parent)
: QObject(parent), utils(new Utils(this)), database(new Database())
{
Q_ASSERT(!Kaidan::s_instance);
Kaidan::s_instance = this;
// Database setup
database->openDatabase();
if (database->needToConvert())
......@@ -92,6 +97,7 @@ Kaidan::~Kaidan()
{
delete caches;
delete database;
Kaidan::s_instance = nullptr;
}
void Kaidan::start()
......@@ -208,3 +214,8 @@ void Kaidan::addOpenUri(const QByteArray &uri)
openUriCache = QString::fromUtf8(uri);
}
}
Kaidan *Kaidan::instance()
{
return s_instance;
}
......@@ -66,6 +66,7 @@ class Kaidan : public QObject
Q_PROPERTY(AvatarFileStorage* avatarStorage READ getAvatarStorage NOTIFY avatarStorageChanged)
Q_PROPERTY(PresenceCache* presenceCache READ getPresenceCache CONSTANT)
Q_PROPERTY(TransferCache* transferCache READ getTransferCache CONSTANT)
Q_PROPERTY(QSettings* settings READ getSettings CONSTANT)
Q_PROPERTY(quint8 connectionState READ getConnectionState NOTIFY connectionStateChanged)
Q_PROPERTY(quint8 disconnReason READ getDisconnReason NOTIFY disconnReasonChanged)
Q_PROPERTY(QString jid READ getJid WRITE setJid NOTIFY jidChanged)
......@@ -197,6 +198,11 @@ public:
return caches->transferCache;
}
QSettings* getSettings() const
{
return caches->settings;
}
Utils* getUtils() const
{
return utils;
......@@ -215,6 +221,8 @@ public:
return uploadServiceFound;
}
static Kaidan *instance();
signals:
void avatarStorageChanged();
......@@ -410,6 +418,8 @@ private:
bool uploadServiceFound = false;
ConnectionState connectionState = ConnectionState::StateDisconnected;
DisconnReason disconnReason = DisconnReason::ConnNoError;
static Kaidan *s_instance;
};
#endif
......@@ -73,7 +73,10 @@ Popup {
ToolTip.text: model.shortName
ToolTip.visible: hovered
onClicked: textArea.insert(textArea.cursorPosition, model.unicode)
onClicked: {
GridView.view.model.addFavoriteEmoji(model.index);
textArea.insert(textArea.cursorPosition, model.unicode)
}
}
ScrollBar.vertical: ScrollBar {}
......@@ -89,6 +92,7 @@ Popup {
Row {
Repeater {
model: ListModel {
ListElement { label: "🔖"; group: Emoji.Group.Favorites }
ListElement { label: "😏"; group: Emoji.Group.People }
ListElement { label: "🌲"; group: Emoji.Group.Nature }
ListElement { label: "🍛"; group: Emoji.Group.Food }
......@@ -101,8 +105,8 @@ Popup {
}
delegate: ItemDelegate {
width: Kirigami.Units.gridUnit * 2
height: Kirigami.Units.gridUnit * 2
width: Kirigami.Units.gridUnit * 1.85
height: Kirigami.Units.gridUnit * 1.85
contentItem: Text {
horizontalAlignment: Text.AlignHCenter
......@@ -115,6 +119,8 @@ Popup {
hoverEnabled: true
ToolTip.text: {
switch (model.group) {
case Emoji.Group.Favorites:
return qsTr('Favorites');
case Emoji.Group.People:
return qsTr('People');
case Emoji.Group.Nature:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment