Store last used emojis in the state config file
Instead of crumming up the main settings file with the last used emojis, they now live in the state (where they belong.)
This commit is contained in:
committed by
Tobias Fella
parent
82d11f79d6
commit
bc7530eaa1
@@ -14,6 +14,8 @@
|
|||||||
|
|
||||||
EmojiModel::EmojiModel(QObject *parent)
|
EmojiModel::EmojiModel(QObject *parent)
|
||||||
: QAbstractListModel(parent)
|
: QAbstractListModel(parent)
|
||||||
|
, m_config(KSharedConfig::openStateConfig())
|
||||||
|
, m_configGroup(KConfigGroup(m_config, QStringLiteral("Editor")))
|
||||||
{
|
{
|
||||||
if (_emojis.isEmpty()) {
|
if (_emojis.isEmpty()) {
|
||||||
#include "emojis.h"
|
#include "emojis.h"
|
||||||
@@ -61,9 +63,9 @@ QHash<int, QByteArray> EmojiModel::roleNames() const
|
|||||||
return {{ShortNameRole, "shortName"}, {UnicodeRole, "unicode"}};
|
return {{ShortNameRole, "shortName"}, {UnicodeRole, "unicode"}};
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariantList EmojiModel::history() const
|
QStringList EmojiModel::lastUsedEmojis() const
|
||||||
{
|
{
|
||||||
return m_settings.value(QStringLiteral("Editor/emojis"), QVariantList()).toList();
|
return m_configGroup.readEntry(QStringLiteral("lastUsedEmojis"), QStringList());
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariantList EmojiModel::filterModel(const QString &filter, bool limit)
|
QVariantList EmojiModel::filterModel(const QString &filter, bool limit)
|
||||||
@@ -93,19 +95,21 @@ QVariantList EmojiModel::filterModelNoCustom(const QString &filter, bool limit)
|
|||||||
|
|
||||||
void EmojiModel::emojiUsed(const QVariant &modelData)
|
void EmojiModel::emojiUsed(const QVariant &modelData)
|
||||||
{
|
{
|
||||||
QVariantList list = history();
|
auto list = lastUsedEmojis();
|
||||||
|
const auto emoji = modelData.value<Emoji>();
|
||||||
|
|
||||||
auto it = list.begin();
|
auto it = list.begin();
|
||||||
while (it != list.end()) {
|
while (it != list.end()) {
|
||||||
if ((*it).value<Emoji>().unicode == modelData.value<Emoji>().unicode) {
|
if (*it == emoji.shortName) {
|
||||||
it = list.erase(it);
|
it = list.erase(it);
|
||||||
} else {
|
} else {
|
||||||
it++;
|
it++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
list.push_front(modelData);
|
list.push_front(emoji.shortName);
|
||||||
m_settings.setValue(QStringLiteral("Editor/emojis"), list);
|
|
||||||
|
m_configGroup.writeEntry(QStringLiteral("lastUsedEmojis"), list);
|
||||||
|
|
||||||
Q_EMIT historyChanged();
|
Q_EMIT historyChanged();
|
||||||
}
|
}
|
||||||
@@ -113,11 +117,11 @@ void EmojiModel::emojiUsed(const QVariant &modelData)
|
|||||||
QVariantList EmojiModel::emojis(Category category) const
|
QVariantList EmojiModel::emojis(Category category) const
|
||||||
{
|
{
|
||||||
if (category == History) {
|
if (category == History) {
|
||||||
return history();
|
return emojiHistory();
|
||||||
}
|
}
|
||||||
if (category == HistoryNoCustom) {
|
if (category == HistoryNoCustom) {
|
||||||
QVariantList list;
|
QVariantList list;
|
||||||
for (const auto &e : history()) {
|
for (const auto &e : emojiHistory()) {
|
||||||
auto emoji = qvariant_cast<Emoji>(e);
|
auto emoji = qvariant_cast<Emoji>(e);
|
||||||
if (!emoji.isCustom) {
|
if (!emoji.isCustom) {
|
||||||
list.append(e);
|
list.append(e);
|
||||||
@@ -217,4 +221,19 @@ QVariantList EmojiModel::categoriesWithCustom() const
|
|||||||
return cats;
|
return cats;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QVariantList EmojiModel::emojiHistory() const
|
||||||
|
{
|
||||||
|
QVariantList list;
|
||||||
|
for (const auto &historicEmoji : lastUsedEmojis()) {
|
||||||
|
for (const auto &emojiCategory : _emojis) {
|
||||||
|
for (const auto &emoji : emojiCategory) {
|
||||||
|
if (qvariant_cast<Emoji>(emoji).shortName == historicEmoji) {
|
||||||
|
list.append(emoji);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
#include "moc_emojimodel.cpp"
|
#include "moc_emojimodel.cpp"
|
||||||
|
|||||||
@@ -3,10 +3,11 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <KConfigGroup>
|
||||||
|
#include <KSharedConfig>
|
||||||
#include <QAbstractListModel>
|
#include <QAbstractListModel>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QQmlEngine>
|
#include <QQmlEngine>
|
||||||
#include <QSettings>
|
|
||||||
|
|
||||||
struct Emoji {
|
struct Emoji {
|
||||||
Emoji(QString unicode, QString shortname, bool isCustom = false)
|
Emoji(QString unicode, QString shortname, bool isCustom = false)
|
||||||
@@ -63,11 +64,6 @@ class EmojiModel : public QAbstractListModel
|
|||||||
QML_ELEMENT
|
QML_ELEMENT
|
||||||
QML_SINGLETON
|
QML_SINGLETON
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Return a list of recently used emojis.
|
|
||||||
*/
|
|
||||||
Q_PROPERTY(QVariantList history READ history NOTIFY historyChanged)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Return a list of emoji categories.
|
* @brief Return a list of emoji categories.
|
||||||
*
|
*
|
||||||
@@ -177,7 +173,11 @@ public:
|
|||||||
*/
|
*/
|
||||||
Q_INVOKABLE QVariantList tones(const QString &baseEmoji) const;
|
Q_INVOKABLE QVariantList tones(const QString &baseEmoji) const;
|
||||||
|
|
||||||
Q_INVOKABLE QVariantList history() const;
|
/**
|
||||||
|
* @brief Return a list of the last used emoji shortnames
|
||||||
|
*/
|
||||||
|
QStringList lastUsedEmojis() const;
|
||||||
|
|
||||||
QVariantList categories() const;
|
QVariantList categories() const;
|
||||||
QVariantList categoriesWithCustom() const;
|
QVariantList categoriesWithCustom() const;
|
||||||
|
|
||||||
@@ -190,7 +190,10 @@ public Q_SLOTS:
|
|||||||
private:
|
private:
|
||||||
static QHash<Category, QVariantList> _emojis;
|
static QHash<Category, QVariantList> _emojis;
|
||||||
|
|
||||||
// TODO: Port away from QSettings
|
/// Returns QVariants containing the last used Emojis
|
||||||
QSettings m_settings;
|
QVariantList emojiHistory() const;
|
||||||
|
|
||||||
|
KSharedConfig::Ptr m_config;
|
||||||
|
KConfigGroup m_configGroup;
|
||||||
EmojiModel(QObject *parent = nullptr);
|
EmojiModel(QObject *parent = nullptr);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user