From f2aa375b43101291f43a72b919d36378b1ed23fa Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Sat, 20 May 2023 22:43:31 +0200 Subject: [PATCH] Rename AccountStickerModel to AccountEmoticonModel In preparation for using it for custom emojis as well --- src/CMakeLists.txt | 2 +- src/main.cpp | 4 +- ...ckermodel.cpp => accountemoticonmodel.cpp} | 50 +++++++++---------- ...tstickermodel.h => accountemoticonmodel.h} | 42 ++++++++-------- src/models/emoticonfiltermodel.cpp | 10 ++-- src/qml/Settings/StickerEditorPage.qml | 8 +-- src/qml/Settings/StickersPage.qml | 4 +- 7 files changed, 62 insertions(+), 58 deletions(-) rename src/models/{accountstickermodel.cpp => accountemoticonmodel.cpp} (70%) rename src/models/{accountstickermodel.h => accountemoticonmodel.h} (56%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d67b4f1c4..264937af6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,7 +15,7 @@ add_library(neochat STATIC models/messagefiltermodel.cpp models/roomlistmodel.cpp models/sortfilterspacelistmodel.cpp - models/accountstickermodel.cpp + models/accountemoticonmodel.cpp spacehierarchycache.cpp roommanager.cpp neochatroom.cpp diff --git a/src/main.cpp b/src/main.cpp index a0faf2a9f..01d9789e9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,7 +48,7 @@ #include "logger.h" #include "login.h" #include "matriximageprovider.h" -#include "models/accountstickermodel.h" +#include "models/accountemoticonmodel.h" #include "models/collapsestateproxymodel.h" #include "models/customemojimodel.h" #include "models/devicesmodel.h" @@ -252,7 +252,7 @@ int main(int argc, char *argv[]) qmlRegisterType("org.kde.neochat", 1, 0, "KeywordNotificationRuleModel"); qmlRegisterType("org.kde.neochat", 1, 0, "StickerModel"); qmlRegisterType("org.kde.neochat", 1, 0, "ImagePacksModel"); - qmlRegisterType("org.kde.neochat", 1, 0, "AccountStickerModel"); + qmlRegisterType("org.kde.neochat", 1, 0, "AccountEmoticonModel"); qmlRegisterType("org.kde.neochat", 1, 0, "EmoticonFilterModel"); qmlRegisterUncreatableType("org.kde.neochat", 1, 0, "RoomMessageEvent", "ENUM"); qmlRegisterUncreatableType("org.kde.neochat", 1, 0, "PushNotificationState", "ENUM"); diff --git a/src/models/accountstickermodel.cpp b/src/models/accountemoticonmodel.cpp similarity index 70% rename from src/models/accountstickermodel.cpp rename to src/models/accountemoticonmodel.cpp index f97b9f9f4..237c98d64 100644 --- a/src/models/accountstickermodel.cpp +++ b/src/models/accountemoticonmodel.cpp @@ -1,19 +1,19 @@ // SPDX-FileCopyrightText: 2021-2023 Tobias Fella // SPDX-License-Identifier: LGPL-2.0-or-later -#include "accountstickermodel.h" +#include "accountemoticonmodel.h" #include #include using namespace Quotient; -AccountStickerModel::AccountStickerModel(QObject *parent) +AccountEmoticonModel::AccountEmoticonModel(QObject *parent) : QAbstractListModel(parent) { } -int AccountStickerModel::rowCount(const QModelIndex &index) const +int AccountEmoticonModel::rowCount(const QModelIndex &index) const { Q_UNUSED(index); if (!m_images) { @@ -22,7 +22,7 @@ int AccountStickerModel::rowCount(const QModelIndex &index) const return m_images->images.size(); } -QVariant AccountStickerModel::data(const QModelIndex &index, int role) const +QVariant AccountEmoticonModel::data(const QModelIndex &index, int role) const { const auto &row = index.row(); const auto &image = m_images->images[row]; @@ -62,23 +62,23 @@ QVariant AccountStickerModel::data(const QModelIndex &index, int role) const return {}; } -QHash AccountStickerModel::roleNames() const +QHash AccountEmoticonModel::roleNames() const { return { - {AccountStickerModel::UrlRole, "url"}, - {AccountStickerModel::BodyRole, "body"}, - {AccountStickerModel::ShortCodeRole, "shortcode"}, - {AccountStickerModel::IsStickerRole, "isSticker"}, - {AccountStickerModel::IsEmojiRole, "isEmoji"}, + {AccountEmoticonModel::UrlRole, "url"}, + {AccountEmoticonModel::BodyRole, "body"}, + {AccountEmoticonModel::ShortCodeRole, "shortcode"}, + {AccountEmoticonModel::IsStickerRole, "isSticker"}, + {AccountEmoticonModel::IsEmojiRole, "isEmoji"}, }; } -Connection *AccountStickerModel::connection() const +Connection *AccountEmoticonModel::connection() const { return m_connection; } -void AccountStickerModel::setConnection(Connection *connection) +void AccountEmoticonModel::setConnection(Connection *connection) { if (m_connection) { disconnect(m_connection, nullptr, this, nullptr); @@ -88,13 +88,13 @@ void AccountStickerModel::setConnection(Connection *connection) Q_EMIT connectionChanged(); connect(m_connection, &Connection::accountDataChanged, this, [this](QString type) { if (type == QStringLiteral("im.ponies.user_emotes")) { - reloadStickers(); + reloadEmoticons(); } }); - reloadStickers(); + reloadEmoticons(); } -void AccountStickerModel::reloadStickers() +void AccountEmoticonModel::reloadEmoticons() { if (!m_connection->hasAccountData("im.ponies.user_emotes"_ls)) { return; @@ -106,7 +106,7 @@ void AccountStickerModel::reloadStickers() endResetModel(); } -void AccountStickerModel::deleteSticker(int index) +void AccountEmoticonModel::deleteEmoticon(int index) { QJsonObject data; m_images->images.removeAt(index); @@ -114,7 +114,7 @@ void AccountStickerModel::deleteSticker(int index) m_connection->setAccountData("im.ponies.user_emotes"_ls, data); } -void AccountStickerModel::setStickerBody(int index, const QString &text) +void AccountEmoticonModel::setEmoticonBody(int index, const QString &text) { m_images->images[index].body = text; QJsonObject data; @@ -122,7 +122,7 @@ void AccountStickerModel::setStickerBody(int index, const QString &text) m_connection->setAccountData("im.ponies.user_emotes"_ls, data); } -void AccountStickerModel::setStickerShortcode(int index, const QString &shortcode) +void AccountEmoticonModel::setEmoticonShortcode(int index, const QString &shortcode) { m_images->images[index].shortcode = shortcode; QJsonObject data; @@ -130,12 +130,12 @@ void AccountStickerModel::setStickerShortcode(int index, const QString &shortcod m_connection->setAccountData("im.ponies.user_emotes"_ls, data); } -void AccountStickerModel::setStickerImage(int index, const QUrl &source) +void AccountEmoticonModel::setEmoticonImage(int index, const QUrl &source) { - doSetStickerImage(index, source); + doSetEmoticonImage(index, source); } -QCoro::Task AccountStickerModel::doSetStickerImage(int index, QUrl source) +QCoro::Task AccountEmoticonModel::doSetEmoticonImage(int index, QUrl source) { auto job = m_connection->uploadFile(source.isLocalFile() ? source.toLocalFile() : source.toString()); co_await qCoro(job, &BaseJob::finished); @@ -153,7 +153,7 @@ QCoro::Task AccountStickerModel::doSetStickerImage(int index, QUrl source) m_connection->setAccountData("im.ponies.user_emotes"_ls, data); } -QCoro::Task AccountStickerModel::doAddSticker(QUrl source, QString shortcode, QString description) +QCoro::Task AccountEmoticonModel::doAddEmoticon(QUrl source, QString shortcode, QString description, QString type) { auto job = m_connection->uploadFile(source.isLocalFile() ? source.toLocalFile() : source.toString()); co_await qCoro(job, &BaseJob::finished); @@ -165,14 +165,14 @@ QCoro::Task AccountStickerModel::doAddSticker(QUrl source, QString shortco job->contentUri(), description, none, - QStringList{"sticker"_ls}, + QStringList{type}, }); QJsonObject data; m_images->fillJson(&data); m_connection->setAccountData("im.ponies.user_emotes"_ls, data); } -void AccountStickerModel::addSticker(const QUrl &source, const QString &shortcode, const QString &description) +void AccountEmoticonModel::addEmoticon(const QUrl &source, const QString &shortcode, const QString &description, const QString &type) { - doAddSticker(source, shortcode, description); + doAddEmoticon(source, shortcode, description, type); } diff --git a/src/models/accountstickermodel.h b/src/models/accountemoticonmodel.h similarity index 56% rename from src/models/accountstickermodel.h rename to src/models/accountemoticonmodel.h index 57273c5c6..4b9a56082 100644 --- a/src/models/accountstickermodel.h +++ b/src/models/accountemoticonmodel.h @@ -14,30 +14,30 @@ class ImagePacksModel; /** - * @class AccountStickerModel + * @class AccountEmoticonModel * - * This class defines the model for visualising the account stickers. + * This class defines the model for visualising the account stickers and emojis. * * This is based upon the im.ponies.user_emotes spec (MSC2545). */ -class AccountStickerModel : public QAbstractListModel +class AccountEmoticonModel : public QAbstractListModel { Q_OBJECT /** - * @brief The connection to get stickers from. + * @brief The connection to get emoticons from. */ Q_PROPERTY(Quotient::Connection *connection READ connection WRITE setConnection NOTIFY connectionChanged) public: enum Roles { - UrlRole = Qt::UserRole + 1, /**< The URL for the sticker. */ - ShortCodeRole, /**< The shortcode for the sticker. */ - BodyRole, //**< A textual description of the sticker */ + UrlRole = Qt::UserRole + 1, /**< The URL for the emoticon. */ + ShortCodeRole, /**< The shortcode for the emoticon. */ + BodyRole, //**< A textual description of the emoticon */ IsStickerRole, //**< Whether this emoticon is a sticker */ IsEmojiRole, //**< Whether this emoticon is an emoji */ }; - explicit AccountStickerModel(QObject *parent = nullptr); + explicit AccountEmoticonModel(QObject *parent = nullptr); /** * @brief Number of rows in the model. @@ -64,29 +64,29 @@ public: void setConnection(Quotient::Connection *connection); /** - * @brief Deletes the sticker at the given index. + * @brief Deletes the emoticon at the given index. */ - Q_INVOKABLE void deleteSticker(int index); + Q_INVOKABLE void deleteEmoticon(int index); /** - * @brief Changes the description for the sticker at the given index. + * @brief Changes the description for the emoticon at the given index. */ - Q_INVOKABLE void setStickerBody(int index, const QString &text); + Q_INVOKABLE void setEmoticonBody(int index, const QString &text); /** - * @brief Changes the shortcode for the sticker at the given index. + * @brief Changes the shortcode for the emoticon at the given index. */ - Q_INVOKABLE void setStickerShortcode(int index, const QString &shortCode); + Q_INVOKABLE void setEmoticonShortcode(int index, const QString &shortCode); /** - * @brief Changes the image for the sticker at the given index. + * @brief Changes the image for the emoticon at the given index. */ - Q_INVOKABLE void setStickerImage(int index, const QUrl &source); + Q_INVOKABLE void setEmoticonImage(int index, const QUrl &source); /** - * @brief Adds a sticker with the given parameters. + * @brief Add an emoticon with the given parameters. */ - Q_INVOKABLE void addSticker(const QUrl &source, const QString &shortcode, const QString &description); + Q_INVOKABLE void addEmoticon(const QUrl &source, const QString &shortcode, const QString &description, const QString &type); Q_SIGNALS: void connectionChanged(); @@ -94,8 +94,8 @@ Q_SIGNALS: private: std::optional m_images; QPointer m_connection; - QCoro::Task doSetStickerImage(int index, QUrl source); - QCoro::Task doAddSticker(QUrl source, QString shortcode, QString description); + QCoro::Task doSetEmoticonImage(int index, QUrl source); + QCoro::Task doAddEmoticon(QUrl source, QString shortcode, QString description, QString type); - void reloadStickers(); + void reloadEmoticons(); }; diff --git a/src/models/emoticonfiltermodel.cpp b/src/models/emoticonfiltermodel.cpp index 89060183b..e4c951feb 100644 --- a/src/models/emoticonfiltermodel.cpp +++ b/src/models/emoticonfiltermodel.cpp @@ -3,7 +3,7 @@ #include "emoticonfiltermodel.h" -#include "accountstickermodel.h" +#include "accountemoticonmodel.h" EmoticonFilterModel::EmoticonFilterModel(QObject *parent) : QSortFilterProxyModel(parent) @@ -12,8 +12,8 @@ EmoticonFilterModel::EmoticonFilterModel(QObject *parent) bool EmoticonFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const { - auto stickerUsage = sourceModel()->data(sourceModel()->index(sourceRow, 0), AccountStickerModel::IsStickerRole).toBool(); - auto emojiUsage = sourceModel()->data(sourceModel()->index(sourceRow, 0), AccountStickerModel::IsEmojiRole).toBool(); + auto stickerUsage = sourceModel()->data(sourceModel()->index(sourceRow, 0), AccountEmoticonModel::IsStickerRole).toBool(); + auto emojiUsage = sourceModel()->data(sourceModel()->index(sourceRow, 0), AccountEmoticonModel::IsEmojiRole).toBool(); return (stickerUsage && m_showStickers) || (emojiUsage && m_showEmojis); } @@ -24,7 +24,9 @@ bool EmoticonFilterModel::showStickers() const void EmoticonFilterModel::setShowStickers(bool showStickers) { + beginResetModel(); m_showStickers = showStickers; + endResetModel(); Q_EMIT showStickersChanged(); } @@ -35,6 +37,8 @@ bool EmoticonFilterModel::showEmojis() const void EmoticonFilterModel::setShowEmojis(bool showEmojis) { + beginResetModel(); m_showEmojis = showEmojis; + endResetModel(); Q_EMIT showEmojisChanged(); } diff --git a/src/qml/Settings/StickerEditorPage.qml b/src/qml/Settings/StickerEditorPage.qml index 38e165e59..ddfb75396 100644 --- a/src/qml/Settings/StickerEditorPage.qml +++ b/src/qml/Settings/StickerEditorPage.qml @@ -119,16 +119,16 @@ Kirigami.ScrollablePage { enabled: !root.newSticker || (image.source && shortcode.text && description.text) onClicked: { if (root.newSticker) { - model.addSticker(image.source, shortcode.text, description.text) + model.addEmoticon(image.source, shortcode.text, description.text, "sticker") } else { if (description.text !== root.description) { - root.model.setStickerBody(proxyModel.mapToSource(proxyModel.index(model.index, 0)).row, description.text) + root.model.setEmoticonBody(proxyModel.mapToSource(proxyModel.index(model.index, 0)).row, description.text) } if (shortcode.text !== root.shortcode) { - root.model.setStickerShortcode(proxyModel.mapToSource(proxyModel.index(model.index, 0)).row, shortcode.text) + root.model.setEmoticonShortcode(proxyModel.mapToSource(proxyModel.index(model.index, 0)).row, shortcode.text) } if (image.source + "" !== root.url) { - root.model.setStickerImage(proxyModel.mapToSource(proxyModel.index(model.index, 0)).row, image.source) + root.model.setEmoticonImage(proxyModel.mapToSource(proxyModel.index(model.index, 0)).row, image.source) } } root.closeDialog() diff --git a/src/qml/Settings/StickersPage.qml b/src/qml/Settings/StickersPage.qml index 16607339d..9a962b535 100644 --- a/src/qml/Settings/StickersPage.qml +++ b/src/qml/Settings/StickersPage.qml @@ -30,7 +30,7 @@ Kirigami.ScrollablePage { Repeater { model: EmoticonFilterModel { id: emoticonFilterModel - sourceModel: AccountStickerModel { + sourceModel: AccountEmoticonModel { id: stickerModel connection: Controller.activeConnection } @@ -84,7 +84,7 @@ Kirigami.ScrollablePage { anchors.right: parent.right anchors.margins: Kirigami.Units.smallSpacing z: 2 - onClicked: stickerModel.deleteSticker(emoticonFilterModel.mapToSource(emoticonFilterModel.index(model.index, 0)).row) + onClicked: stickerModel.deleteEmoticon(emoticonFilterModel.mapToSource(emoticonFilterModel.index(model.index, 0)).row) } } }