From 5c8d916752767348c1e4c7f8fa565ae1ff982da3 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Tue, 29 Dec 2020 14:28:32 +0000 Subject: [PATCH] Add support for stickers Fix #130 --- .../Component/Timeline/ImageDelegate.qml | 39 +++++-------------- imports/NeoChat/Page/RoomPage.qml | 33 +++++++++++++++- src/messageeventmodel.cpp | 12 ++++++ src/neochatroom.cpp | 4 ++ 4 files changed, 57 insertions(+), 31 deletions(-) diff --git a/imports/NeoChat/Component/Timeline/ImageDelegate.qml b/imports/NeoChat/Component/Timeline/ImageDelegate.qml index 62376f9e8..c57746187 100644 --- a/imports/NeoChat/Component/Timeline/ImageDelegate.qml +++ b/imports/NeoChat/Component/Timeline/ImageDelegate.qml @@ -17,6 +17,8 @@ import NeoChat.Dialog 1.0 import NeoChat.Menu.Timeline 1.0 Image { + id: img + readonly property bool isAnimated: contentType === "image/gif" property bool openOnFinished: false @@ -26,8 +28,7 @@ Image { // readonly property var info: isThumbnail ? content.info.thumbnail_info : content.info readonly property var info: content.info readonly property string mediaId: isThumbnail ? content.thumbnailMediaId : content.mediaId - - id: img + property bool readonly: false source: "image://mxc/" + mediaId @@ -36,34 +37,12 @@ Image { fillMode: Image.PreserveAspectFit - Control { - anchors.bottom: parent.bottom - anchors.bottomMargin: 8 - anchors.right: parent.right - anchors.rightMargin: 8 + ToolTip.text: display + ToolTip.visible: hoverHandler.hovered - horizontalPadding: 8 - verticalPadding: 4 - - contentItem: RowLayout { - Label { - text: Qt.formatTime(time) - color: "white" - font.pixelSize: 12 - } - - Label { - text: author.displayName - color: "white" - font.pixelSize: 12 - } - } - - background: Rectangle { - radius: 2 - color: "black" - opacity: 0.3 - } + HoverHandler { + id: hoverHandler + enabled: img.readonly } Rectangle { @@ -87,6 +66,8 @@ Image { MouseArea { id: messageMouseArea + enabled: !img.readonly + anchors.fill: parent acceptedButtons: Qt.LeftButton | Qt.RightButton diff --git a/imports/NeoChat/Page/RoomPage.qml b/imports/NeoChat/Page/RoomPage.qml index ff0332eff..b440127ae 100644 --- a/imports/NeoChat/Page/RoomPage.qml +++ b/imports/NeoChat/Page/RoomPage.qml @@ -318,7 +318,7 @@ Kirigami.ScrollablePage { ReactionDelegate { Layout.fillWidth: true Layout.topMargin: 0 - Layout.bottomMargin: Kirigami.Units.largeSpacing * 2 + Layout.bottomMargin: Kirigami.Units.largeSpacing } ] } @@ -362,7 +362,35 @@ Kirigami.ScrollablePage { Layout.fillWidth: true Layout.topMargin: 0 Layout.maximumHeight: 320 - Layout.bottomMargin: 8 + Layout.bottomMargin: Kirigami.Units.largeSpacing + } + ] + } + } + } + + DelegateChoice { + roleValue: "sticker" + delegate: TimelineContainer { + width: messageListView.width + + innerObject: MessageDelegate { + Layout.fillWidth: true + onReplyClicked: goToEvent(eventID) + onReplyToMessageClicked: replyToMessage(replyUser, replyContent, eventId); + + innerObject: [ + ImageDelegate { + readonly: true + Layout.maximumWidth: parent.width / 2 + Layout.minimumWidth: 320 + Layout.preferredHeight: info.h / info.w * width + }, + ReactionDelegate { + Layout.fillWidth: true + Layout.topMargin: 0 + Layout.maximumHeight: 320 + Layout.bottomMargin: Kirigami.Units.largeSpacing } ] } @@ -443,6 +471,7 @@ Kirigami.ScrollablePage { } } + DelegateChoice { roleValue: "other" delegate: Item {} diff --git a/src/messageeventmodel.cpp b/src/messageeventmodel.cpp index 8c21575f7..d17f6a657 100644 --- a/src/messageeventmodel.cpp +++ b/src/messageeventmodel.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -324,6 +325,7 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const auto reason = evt.redactedBecause()->reason(); return (reason.isEmpty()) ? i18n("[This message was deleted]") : i18n("[This message was deleted: %1]").arg(evt.redactedBecause()->reason()); } + return m_currentRoom->eventToString(evt, Qt::RichText); } @@ -357,6 +359,9 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const return "message"; } + if (is(evt)) { + return "sticker"; + } if (evt.isStateEvent()) { return "state"; } @@ -393,6 +398,10 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const // content JSON stored in EventContent::Base return e->hasFileContent() ? QVariant::fromValue(e->content()->originalJson) : QVariant(); }; + + if (auto e = eventCast(&evt)) { + return QVariant::fromValue(e->image().originalJson); + } } if (role == HighlightRole) { @@ -454,6 +463,9 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const return QVariant::fromValue(m_currentRoom->fileTransferInfo(e->id())); } } + if (auto e = eventCast(&evt)) { + return QVariant::fromValue(m_currentRoom->fileTransferInfo(e->id())); + } } if (role == AnnotationRole) { diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index 7f30a59c4..75a9a12be 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -27,6 +27,7 @@ #include "events/roomcanonicalaliasevent.h" #include "events/roommessageevent.h" #include "events/roompowerlevelsevent.h" +#include "events/stickerevent.h" #include "events/typingevent.h" #include "jobs/downloadfilejob.h" #include "neochatconfig.h" @@ -345,6 +346,9 @@ QString NeoChatRoom::eventToString(const RoomEvent &evt, Qt::TextFormat format, } return plainBody; }, + [](const StickerEvent &e) { + return e.body(); + }, [this](const RoomMemberEvent &e) { // FIXME: Rewind to the name that was at the time of this event auto subjectName = this->user(e.userId())->displayname();