From 8959be2cb8300fb53fc4da69160080176338defa Mon Sep 17 00:00:00 2001 From: Black Hat Date: Mon, 3 Dec 2018 19:55:42 +0800 Subject: [PATCH 1/2] Remove ImageItem, Paintable and use unified image provider. --- imports/Spectral/Component/Avatar.qml | 63 ++++++++++ imports/Spectral/Component/ScrollHelper.qml | 3 +- .../Component/Timeline/ImageDelegate.qml | 4 +- .../Component/Timeline/MessageDelegate.qml | 8 +- imports/Spectral/Component/qmldir | 1 + imports/Spectral/Panel/RoomDrawer.qml | 10 +- imports/Spectral/Panel/RoomHeader.qml | 7 +- imports/Spectral/Panel/RoomListPanel.qml | 14 +-- imports/Spectral/Panel/RoomPanel.qml | 7 +- imports/Spectral/Panel/RoomPanelInput.qml | 8 +- js/util.js | 12 -- qml/main.qml | 2 - qtquickcontrols2.conf | 4 +- res.qrc | 2 +- spectral.pro | 8 +- src/imageitem.cpp | 108 ------------------ src/imageitem.h | 52 --------- src/imageprovider.cpp | 36 +++--- src/imageprovider.h | 2 + src/main.cpp | 2 - src/paintable.cpp | 3 - src/paintable.h | 20 ---- src/roomlistmodel.cpp | 4 +- src/roomlistmodel.h | 2 +- src/spectralroom.cpp | 2 +- src/spectralroom.h | 27 ----- src/spectraluser.h | 28 +---- src/userlistmodel.cpp | 8 +- src/userlistmodel.h | 2 +- 29 files changed, 125 insertions(+), 324 deletions(-) create mode 100644 imports/Spectral/Component/Avatar.qml delete mode 100644 js/util.js delete mode 100644 src/imageitem.cpp delete mode 100644 src/imageitem.h delete mode 100644 src/paintable.cpp delete mode 100644 src/paintable.h diff --git a/imports/Spectral/Component/Avatar.qml b/imports/Spectral/Component/Avatar.qml new file mode 100644 index 0000000..be95df5 --- /dev/null +++ b/imports/Spectral/Component/Avatar.qml @@ -0,0 +1,63 @@ +import QtQuick 2.9 +import QtQuick.Controls 2.2 +import QtGraphicalEffects 1.0 + +Item { + property string hint: "H" + property string source: "" + readonly property url realSource: source ? "image://mxc/" + source : "" + + id: root + + Image { + anchors.fill: parent + + id: image + visible: realSource + source: realSource + sourceSize.width: width + sourceSize.height: width + fillMode: Image.PreserveAspectCrop + layer.enabled: true + layer.effect: OpacityMask { + maskSource: Rectangle { + width: image.width + height: image.width + + radius: width / 2 + } + } + } + + Rectangle { + anchors.fill: parent + + visible: !realSource || image.status != Image.Ready + + radius: height / 2 + + color: stringToColor(hint) + + Label { + anchors.centerIn: parent + + color: "white" + text: hint[0].toUpperCase() + font.pixelSize: root.width / 2 + font.bold: true + } + } + + function stringToColor(str) { + var hash = 0; + for (var i = 0; i < str.length; i++) { + hash = str.charCodeAt(i) + ((hash << 5) - hash); + } + var colour = '#'; + for (var j = 0; j < 3; j++) { + var value = (hash >> (j * 8)) & 0xFF; + colour += ('00' + value.toString(16)).substr(-2); + } + return colour; + } +} diff --git a/imports/Spectral/Component/ScrollHelper.qml b/imports/Spectral/Component/ScrollHelper.qml index 923a141..25714f0 100644 --- a/imports/Spectral/Component/ScrollHelper.qml +++ b/imports/Spectral/Component/ScrollHelper.qml @@ -66,14 +66,13 @@ MouseArea { } onWheel: { - var newPos = calculateNewPosition(flickable, wheel); // console.warn("Delta: ", wheel.pixelDelta.y); // console.warn("Old position: ", flickable.contentY); // console.warn("New position: ", newPos); // Show the scrollbars flickable.flick(0, 0); - flickable.contentY = newPos; + flickable.contentY = calculateNewPosition(flickable, wheel); cancelFlickStateTimer.start() } diff --git a/imports/Spectral/Component/Timeline/ImageDelegate.qml b/imports/Spectral/Component/Timeline/ImageDelegate.qml index 465ff30..362882a 100644 --- a/imports/Spectral/Component/Timeline/ImageDelegate.qml +++ b/imports/Spectral/Component/Timeline/ImageDelegate.qml @@ -43,14 +43,14 @@ ColumnLayout { spacing: 4 - ImageItem { + Avatar { Layout.preferredWidth: 32 Layout.preferredHeight: 32 Layout.alignment: Qt.AlignTop visible: avatarVisible hint: author.displayName - source: author.paintable + source: author.avatarUrl } Label { diff --git a/imports/Spectral/Component/Timeline/MessageDelegate.qml b/imports/Spectral/Component/Timeline/MessageDelegate.qml index 263d334..e58ed3e 100644 --- a/imports/Spectral/Component/Timeline/MessageDelegate.qml +++ b/imports/Spectral/Component/Timeline/MessageDelegate.qml @@ -42,14 +42,14 @@ ColumnLayout { spacing: 4 - ImageItem { + Avatar { Layout.preferredWidth: 32 Layout.preferredHeight: 32 Layout.alignment: Qt.AlignTop visible: avatarVisible hint: author.displayName - source: author.paintable + source: author.avatarUrl } Label { @@ -119,12 +119,12 @@ ColumnLayout { contentItem: RowLayout { spacing: 8 - ImageItem { + Avatar { Layout.preferredWidth: 36 Layout.preferredHeight: 36 Layout.alignment: Qt.AlignTop - source: replyAuthor ? replyAuthor.paintable : null + source: replyAuthor ? replyAuthor.avatarUrl : "" } ColumnLayout { diff --git a/imports/Spectral/Component/qmldir b/imports/Spectral/Component/qmldir index e051620..be035c4 100644 --- a/imports/Spectral/Component/qmldir +++ b/imports/Spectral/Component/qmldir @@ -6,3 +6,4 @@ ScrollHelper 2.0 ScrollHelper.qml AutoListView 2.0 AutoListView.qml AutoTextField 2.0 AutoTextField.qml SplitView 2.0 SplitView.qml +Avatar 2.0 Avatar.qml diff --git a/imports/Spectral/Panel/RoomDrawer.qml b/imports/Spectral/Panel/RoomDrawer.qml index 77e3a26..2221b5b 100644 --- a/imports/Spectral/Panel/RoomDrawer.qml +++ b/imports/Spectral/Panel/RoomDrawer.qml @@ -7,8 +7,6 @@ import Spectral.Component 2.0 import Spectral 0.1 -import "qrc:/js/util.js" as Util - Drawer { property var room @@ -20,13 +18,13 @@ Drawer { anchors.fill: parent anchors.margins: 32 - ImageItem { + Avatar { Layout.preferredWidth: 96 Layout.preferredHeight: 96 Layout.alignment: Qt.AlignHCenter hint: room ? room.displayName : "No name" - source: room ? room.paintable : null + source: room ? room.avatarUrl : null } Label { @@ -117,11 +115,11 @@ Drawer { anchors.margins: 8 spacing: 12 - ImageItem { + Avatar { Layout.preferredWidth: height Layout.fillHeight: true - source: paintable + source: avatar hint: name } diff --git a/imports/Spectral/Panel/RoomHeader.qml b/imports/Spectral/Panel/RoomHeader.qml index 6b02202..e3c95ff 100644 --- a/imports/Spectral/Panel/RoomHeader.qml +++ b/imports/Spectral/Panel/RoomHeader.qml @@ -5,9 +5,10 @@ import QtQuick.Controls.Material 2.2 import Spectral 0.1 import Spectral.Effect 2.0 +import Spectral.Component 2.0 Control { - property alias paintable: headerImage.source + property alias avatar: headerImage.source property alias topic: headerTopicLabel.text property bool atTop: false signal clicked() @@ -31,13 +32,13 @@ Control { spacing: 12 - ImageItem { + Avatar { Layout.preferredWidth: height Layout.fillHeight: true id: headerImage - source: currentRoom.paintable + source: currentRoom.avatarUrl hint: currentRoom ? currentRoom.displayName : "No name" } diff --git a/imports/Spectral/Panel/RoomListPanel.qml b/imports/Spectral/Panel/RoomListPanel.qml index a022131..7d9dc3d 100644 --- a/imports/Spectral/Panel/RoomListPanel.qml +++ b/imports/Spectral/Panel/RoomListPanel.qml @@ -110,13 +110,13 @@ Rectangle { contentItem: ColumnLayout { spacing: 4 - ImageItem { + Avatar { Layout.preferredWidth: 200 Layout.preferredHeight: 200 Layout.margins: 12 Layout.alignment: Qt.AlignHCenter - source: root.user ? root.user.paintable : null + source: root.user ? root.user.avatarUrl : null hint: root.user ? root.user.displayName : "?" } @@ -646,14 +646,14 @@ Rectangle { background: Item {} } - ImageItem { + Avatar { Layout.preferredWidth: height Layout.fillHeight: true Layout.alignment: Qt.AlignRight visible: !searchField.active - source: root.user ? root.user.paintable : null + source: root.user ? root.user.avatarUrl : null hint: root.user ? root.user.displayName : "?" MouseArea { @@ -765,13 +765,11 @@ Rectangle { spacing: 12 - ImageItem { - id: imageItem - + Avatar { Layout.preferredWidth: height Layout.fillHeight: true - source: paintable + source: avatar hint: name || "No Name" } diff --git a/imports/Spectral/Panel/RoomPanel.qml b/imports/Spectral/Panel/RoomPanel.qml index d59e8b6..ad8c5da 100644 --- a/imports/Spectral/Panel/RoomPanel.qml +++ b/imports/Spectral/Panel/RoomPanel.qml @@ -14,7 +14,6 @@ import Spectral.Setting 0.1 import SortFilterProxyModel 0.2 import "qrc:/js/md.js" as Markdown -import "qrc:/js/util.js" as Util Item { property var currentRoom: null @@ -63,7 +62,7 @@ Item { id: roomHeader - paintable: currentRoom ? currentRoom.paintable : null + avatar: currentRoom ? currentRoom.avatarUrl : "" topic: currentRoom ? (currentRoom.topic).replace(/(\r\n\t|\n|\r\t)/gm,"") : "" atTop: messageListView.atYBeginning @@ -295,11 +294,11 @@ Item { anchors.margins: 8 spacing: 12 - ImageItem { + Avatar { Layout.preferredWidth: height Layout.fillHeight: true - source: modelData.paintable + source: modelData.avatar hint: modelData.displayName } diff --git a/imports/Spectral/Panel/RoomPanelInput.qml b/imports/Spectral/Panel/RoomPanelInput.qml index 89ef04a..c558745 100644 --- a/imports/Spectral/Panel/RoomPanelInput.qml +++ b/imports/Spectral/Panel/RoomPanelInput.qml @@ -50,11 +50,11 @@ Control { spacing: 8 - ImageItem { + Avatar { Layout.preferredWidth: 32 Layout.preferredHeight: 32 - source: replyUser ? replyUser.paintable : null + source: replyUser ? replyUser.avatarUrl : "" hint: replyUser ? replyUser.displayName : "No name" } @@ -125,11 +125,11 @@ Control { verticalAlignment: Text.AlignVCenter horizontalAlignment: Text.AlignHCenter } - ImageItem { + Avatar { width: 20 height: 20 visible: !isEmoji - source: modelData.paintable || null + source: modelData.avatarUrl || null } Label { height: parent.height diff --git a/js/util.js b/js/util.js deleted file mode 100644 index 6b0ade3..0000000 --- a/js/util.js +++ /dev/null @@ -1,12 +0,0 @@ -.pragma library - -function pushToStack(stack, page) { - if(page && stack.currentItem !== page) { - if(stack.depth === 1) { - stack.replace(page) - } else { - stack.pop(null) - stack.replace(page) - } - } -} diff --git a/qml/main.qml b/qml/main.qml index 390e58b..dd7ca62 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -13,8 +13,6 @@ import Spectral.Effect 2.0 import Spectral 0.1 import Spectral.Setting 0.1 -import "qrc:/js/util.js" as Util - ApplicationWindow { Material.theme: MSettings.darkTheme ? Material.Dark : Material.Light diff --git a/qtquickcontrols2.conf b/qtquickcontrols2.conf index bb09e95..b4a0454 100644 --- a/qtquickcontrols2.conf +++ b/qtquickcontrols2.conf @@ -9,6 +9,6 @@ Style=Material Theme=Light Primary=#344955 Accent=#673AB7 -;Foreground=#1D333E -;Background=#161616 +Foreground=#1D333E +Background=#161616 Font/Family="Roboto,Noto Sans,Noto Color Emoji" diff --git a/res.qrc b/res.qrc index a1be77d..9ae861e 100644 --- a/res.qrc +++ b/res.qrc @@ -3,7 +3,6 @@ qtquickcontrols2.conf qml/main.qml js/md.js - js/util.js imports/Spectral/Component/Emoji/EmojiPicker.qml imports/Spectral/Component/Emoji/qmldir imports/Spectral/Component/Timeline/DownloadableContent.qml @@ -47,5 +46,6 @@ imports/Spectral/Effect/CircleMask.qml assets/img/roompanel-dark.svg imports/Spectral/Component/Timeline/ImageDelegate.qml + imports/Spectral/Component/Avatar.qml diff --git a/spectral.pro b/spectral.pro index c27eda3..2620c0b 100644 --- a/spectral.pro +++ b/spectral.pro @@ -103,12 +103,10 @@ HEADERS += \ src/emojimodel.h \ src/spectralroom.h \ src/userlistmodel.h \ - src/imageitem.h \ src/accountlistmodel.h \ src/spectraluser.h \ src/notifications/manager.h \ - src/utils.h \ - src/paintable.h + src/utils.h SOURCES += src/main.cpp \ src/controller.cpp \ @@ -118,11 +116,9 @@ SOURCES += src/main.cpp \ src/emojimodel.cpp \ src/spectralroom.cpp \ src/userlistmodel.cpp \ - src/imageitem.cpp \ src/accountlistmodel.cpp \ src/spectraluser.cpp \ - src/utils.cpp \ - src/paintable.cpp + src/utils.cpp unix:!mac { SOURCES += src/notifications/managerlinux.cpp diff --git a/src/imageitem.cpp b/src/imageitem.cpp deleted file mode 100644 index 22047fa..0000000 --- a/src/imageitem.cpp +++ /dev/null @@ -1,108 +0,0 @@ -#include "imageitem.h" - -#include -#include -#include -#include -#include - -ImageItem::ImageItem(QQuickItem *parent) : QQuickPaintedItem(parent) {} - -inline static QString stringtoColor(QString string) { - int hash = 0; - for (int i = 0; i < string.length(); i++) - hash = string.at(i).unicode() + ((hash << 5) - hash); - QString colour = "#"; - for (int j = 0; j < 3; j++) - colour += ("00" + QString::number((hash >> (j * 8)) & 0xFF, 16)).right(2); - return colour; -} - -inline static QImage getImageFromPaintable(QPointer p, int width, - int height) { - if (p.isNull()) return {}; - qreal dpi = QApplication::primaryScreen()->devicePixelRatio(); - QImage image(p->image(width * dpi, height * dpi)); - if (image.isNull()) return {}; - return image; -} - -void ImageItem::paint(QPainter *painter) { - QRectF bounding_rect(boundingRect()); - - painter->setRenderHint(QPainter::Antialiasing, true); - - QImage image(getImageFromPaintable(m_paintable, bounding_rect.width(), - bounding_rect.height())); - - if (image.isNull()) { - painter->setPen(Qt::NoPen); - if (m_color.isEmpty()) - painter->setBrush(QColor(stringtoColor(m_hint))); - else - painter->setBrush(QColor(m_color)); - if (m_round) - painter->drawEllipse(bounding_rect); - else - painter->drawRect(bounding_rect); - painter->setPen(QPen(Qt::white, 2)); - QFont font; - font.setStyleHint(QFont::SansSerif); - - font.setPixelSize(int(bounding_rect.width() / 2)); - font.setBold(true); - painter->setFont(font); - painter->drawText(bounding_rect, Qt::AlignCenter, m_hint.at(0).toUpper()); - } else { - QImage scaled; - if (image.width() > image.height()) { - scaled = image.copy((image.width() - image.height()) / 2, 0, - image.height(), image.height()); - } else { - scaled = image.copy(0, (image.height() - image.width()) / 2, - image.width(), image.width()); - } - - if (m_round) { - QPainterPath clip; - clip.addEllipse(bounding_rect); // this is the shape we want to clip to - painter->setClipPath(clip); - } - - painter->drawImage(bounding_rect, scaled); - } -} - -void ImageItem::setPaintable(Paintable *paintable) { - if (!paintable) return; - if (!m_paintable.isNull()) m_paintable->disconnect(this); - m_paintable = paintable; - connect(m_paintable, &Paintable::paintableChanged, this, - [=] { this->update(); }); - emit paintableChanged(); - update(); -} - -void ImageItem::setHint(QString newHint) { - if (!m_hint.isNull() && m_hint != newHint) { - m_hint = newHint; - emit hintChanged(); - update(); - } -} - -void ImageItem::setDefaultColor(QString color) { - if (color != m_color) { - m_color = color; - emit defaultColorChanged(); - update(); - } -} - -void ImageItem::setRound(bool value) { - if (m_round != value) { - m_round = value; - emit roundChanged(); - update(); - } -} diff --git a/src/imageitem.h b/src/imageitem.h deleted file mode 100644 index a0c6816..0000000 --- a/src/imageitem.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef IMAGEITEM_H -#define IMAGEITEM_H - -#include -#include -#include -#include -#include -#include - -#include "paintable.h" - -class ImageItem : public QQuickPaintedItem { - Q_OBJECT - Q_PROPERTY(Paintable* source READ paintable WRITE setPaintable NOTIFY - paintableChanged) - Q_PROPERTY(QString hint READ hint WRITE setHint NOTIFY hintChanged) - Q_PROPERTY(QString defaultColor READ defaultColor WRITE setDefaultColor NOTIFY - defaultColorChanged) - Q_PROPERTY(bool round READ round WRITE setRound NOTIFY roundChanged) - - public: - ImageItem(QQuickItem* parent = nullptr); - - void paint(QPainter* painter); - - Paintable* paintable() { return m_paintable; } - void setPaintable(Paintable* paintable); - - QString hint() { return m_hint; } - void setHint(QString hint); - - QString defaultColor() { return m_color; } - void setDefaultColor(QString color); - - bool round() { return m_round; } - void setRound(bool value); - - signals: - void paintableChanged(); - void hintChanged(); - void defaultColorChanged(); - void roundChanged(); - - private: - QPointer m_paintable; - QString m_hint = "H"; - QString m_color; - bool m_round = true; -}; - -#endif // IMAGEITEM_H diff --git a/src/imageprovider.cpp b/src/imageprovider.cpp index 85aad34..c4abc1f 100644 --- a/src/imageprovider.cpp +++ b/src/imageprovider.cpp @@ -16,11 +16,7 @@ ImageProvider::ImageProvider(QObject* parent) : QObject(parent), QQuickImageProvider( QQmlImageProviderBase::Image, - QQmlImageProviderBase::ForceAsynchronousImageLoading) { -#if (QT_VERSION < QT_VERSION_CHECK(5, 10, 0)) - qRegisterMetaType(); -#endif -} + QQmlImageProviderBase::ForceAsynchronousImageLoading) {} QImage ImageProvider::requestImage(const QString& id, QSize* pSize, const QSize& requestedSize) { @@ -32,31 +28,33 @@ QImage ImageProvider::requestImage(const QString& id, QSize* pSize, QUrl mxcUri{id}; + QImage result = image(mxcUri, requestedSize); + if (!requestedSize.isEmpty() && result.size() != requestedSize) { + QImage scaled = result.scaled(requestedSize, Qt::KeepAspectRatio, + Qt::SmoothTransformation); + if (pSize != nullptr) *pSize = scaled.size(); + return scaled; + } + if (pSize != nullptr) *pSize = result.size(); + return result; +} + +QImage ImageProvider::image(const QUrl& mxc, const QSize& size) { QUrl tempfilePath = QUrl::fromLocalFile( QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/" + - mxcUri.fileName() + "-" + QString::number(requestedSize.width()) + "x" + - QString::number(requestedSize.height()) + ".png"); - + mxc.fileName() + ".png"); QImage cachedImage; if (cachedImage.load(tempfilePath.toLocalFile())) { - if (pSize != nullptr) *pSize = cachedImage.size(); return cachedImage; } MediaThumbnailJob* job = nullptr; QReadLocker locker(&m_lock); -#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) QMetaObject::invokeMethod( - m_connection, - [=] { return m_connection->getThumbnail(mxcUri, requestedSize); }, + m_connection, [=] { return m_connection->getThumbnail(mxc, size); }, Qt::BlockingQueuedConnection, &job); -#else - QMetaObject::invokeMethod(m_connection, "getThumbnail", - Qt::BlockingQueuedConnection, - Q_RETURN_ARG(MediaThumbnailJob*, job), - Q_ARG(QUrl, mxcUri), Q_ARG(QSize, requestedSize)); -#endif + if (!job) { qDebug() << "ImageProvider: failed to send a request"; return {}; @@ -71,8 +69,6 @@ QImage ImageProvider::requestImage(const QString& id, QSize* pSize, condition.wait(&m_lock); } - if (pSize != nullptr) *pSize = result.size(); - result.save(tempfilePath.toLocalFile()); return result; diff --git a/src/imageprovider.h b/src/imageprovider.h index 1e668bc..815c4fc 100644 --- a/src/imageprovider.h +++ b/src/imageprovider.h @@ -33,6 +33,8 @@ class ImageProvider : public QObject, public QQuickImageProvider { private: QReadWriteLock m_lock; QMatrixClient::Connection* m_connection = nullptr; + + QImage image(const QUrl& mxc, const QSize& size); }; #endif // IMAGEPROVIDER_H diff --git a/src/main.cpp b/src/main.cpp index 126de59..66346f7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,6 @@ #include "accountlistmodel.h" #include "controller.h" #include "emojimodel.h" -#include "imageitem.h" #include "imageprovider.h" #include "messageeventmodel.h" #include "room.h" @@ -45,7 +44,6 @@ int main(int argc, char *argv[]) { qmlRegisterType("SortFilterProxyModel", 0, 2, "SortFilterProxyModel"); - qmlRegisterType("Spectral", 0, 1, "ImageItem"); qmlRegisterType("Spectral", 0, 1, "Controller"); qmlRegisterType("Spectral", 0, 1, "AccountListModel"); qmlRegisterType("Spectral", 0, 1, "RoomListModel"); diff --git a/src/paintable.cpp b/src/paintable.cpp deleted file mode 100644 index a44b826..0000000 --- a/src/paintable.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include "paintable.h" - -Paintable::Paintable(QObject *parent) : QObject(parent) {} diff --git a/src/paintable.h b/src/paintable.h deleted file mode 100644 index 5eb56ef..0000000 --- a/src/paintable.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef PAINTABLE_H -#define PAINTABLE_H - -#include -#include - -class Paintable : public QObject { - Q_OBJECT - public: - Paintable(QObject* parent = nullptr); - virtual ~Paintable() = default; - - virtual QImage image(int) = 0; - virtual QImage image(int, int) = 0; - - signals: - void paintableChanged(); -}; - -#endif // PAINTABLE_H diff --git a/src/roomlistmodel.cpp b/src/roomlistmodel.cpp index 09ec260..b34cee7 100644 --- a/src/roomlistmodel.cpp +++ b/src/roomlistmodel.cpp @@ -153,7 +153,7 @@ QVariant RoomListModel::data(const QModelIndex& index, int role) const { } SpectralRoom* room = m_rooms.at(index.row()); if (role == NameRole) return room->displayName(); - if (role == PaintableRole) return QVariant::fromValue(room->paintable()); + if (role == AvatarRole) return room->avatarUrl(); if (role == TopicRole) return room->topic(); if (role == CategoryRole) { if (room->joinState() == JoinState::Invite) return RoomType::Invited; @@ -193,7 +193,7 @@ void RoomListModel::unreadMessagesChanged(SpectralRoom* room) { QHash RoomListModel::roleNames() const { QHash roles; roles[NameRole] = "name"; - roles[PaintableRole] = "paintable"; + roles[AvatarRole] = "avatar"; roles[TopicRole] = "topic"; roles[CategoryRole] = "category"; roles[UnreadCountRole] = "unreadCount"; diff --git a/src/roomlistmodel.h b/src/roomlistmodel.h index c42e956..40b0f04 100644 --- a/src/roomlistmodel.h +++ b/src/roomlistmodel.h @@ -31,7 +31,7 @@ class RoomListModel : public QAbstractListModel { public: enum EventRoles { NameRole = Qt::UserRole + 1, - PaintableRole, + AvatarRole, TopicRole, CategoryRole, UnreadCountRole, diff --git a/src/spectralroom.cpp b/src/spectralroom.cpp index ee7aa44..e003d61 100644 --- a/src/spectralroom.cpp +++ b/src/spectralroom.cpp @@ -18,7 +18,7 @@ SpectralRoom::SpectralRoom(Connection* connection, QString roomId, JoinState joinState) - : Room(connection, std::move(roomId), joinState), m_paintable(this) { + : Room(connection, std::move(roomId), joinState) { connect(this, &SpectralRoom::notificationCountChanged, this, &SpectralRoom::countChanged); connect(this, &SpectralRoom::highlightCountChanged, this, diff --git a/src/spectralroom.h b/src/spectralroom.h index 155cb44..21474ca 100644 --- a/src/spectralroom.h +++ b/src/spectralroom.h @@ -1,7 +1,6 @@ #ifndef SpectralRoom_H #define SpectralRoom_H -#include "paintable.h" #include "room.h" #include "spectraluser.h" @@ -11,29 +10,8 @@ using namespace QMatrixClient; -class RoomPaintable : public Paintable { - Q_OBJECT - public: - RoomPaintable(Room* parent) : Paintable(parent), m_room(parent) { - connect(m_room, &Room::avatarChanged, [=] { emit paintableChanged(); }); - } - - QImage image(int dimension) override { - if (!m_room) return {}; - return m_room->avatar(dimension); - } - QImage image(int width, int height) override { - if (!m_room) return {}; - return m_room->avatar(width, height); - } - - private: - Room* m_room; -}; - class SpectralRoom : public Room { Q_OBJECT - Q_PROPERTY(Paintable* paintable READ paintable CONSTANT) Q_PROPERTY(bool hasUsersTyping READ hasUsersTyping NOTIFY typingChanged) Q_PROPERTY(QString usersTyping READ getUsersTyping NOTIFY typingChanged) Q_PROPERTY(QString cachedInput READ cachedInput WRITE setCachedInput NOTIFY @@ -48,8 +26,6 @@ class SpectralRoom : public Room { explicit SpectralRoom(Connection* connection, QString roomId, JoinState joinState = {}); - Paintable* paintable() { return &m_paintable; } - const QString& cachedInput() const { return m_cachedInput; } void setCachedInput(const QString& input) { if (input != m_cachedInput) { @@ -132,9 +108,6 @@ class SpectralRoom : public Room { void sendTypingNotification(bool isTyping); void sendReply(QString userId, QString eventId, QString replyContent, QString sendContent); - - private: - RoomPaintable m_paintable; }; #endif // SpectralRoom_H diff --git a/src/spectraluser.h b/src/spectraluser.h index 4fe459e..e62e51e 100644 --- a/src/spectraluser.h +++ b/src/spectraluser.h @@ -1,44 +1,18 @@ #ifndef SpectralUser_H #define SpectralUser_H -#include "paintable.h" #include "room.h" #include "user.h" #include -#include using namespace QMatrixClient; -class UserPaintable : public Paintable { - Q_OBJECT - public: - UserPaintable(User* parent) : Paintable(parent), m_user(parent) {} - - QImage image(int dimension) override { - if (!m_user) return {}; - return m_user->avatar(dimension); - } - QImage image(int width, int height) override { - if (!m_user) return {}; - return m_user->avatar(width, height); - } - - private: - User* m_user; -}; - class SpectralUser : public User { Q_OBJECT - Q_PROPERTY(Paintable* paintable READ paintable CONSTANT) public: SpectralUser(QString userId, Connection* connection) - : User(userId, connection), m_paintable(this) {} - - Paintable* paintable() { return &m_paintable; } - - private: - UserPaintable m_paintable; + : User(userId, connection) {} }; #endif // SpectralUser_H diff --git a/src/userlistmodel.cpp b/src/userlistmodel.cpp index eb242d9..4fc65e8 100644 --- a/src/userlistmodel.cpp +++ b/src/userlistmodel.cpp @@ -72,8 +72,8 @@ QVariant UserListModel::data(const QModelIndex& index, int role) const { if (role == UserIDRole) { return user->id(); } - if (role == PaintableRole) { - return QVariant::fromValue((static_cast(user))->paintable()); + if (role == AvatarRole) { + return user->avatarUrl(); } return QVariant(); @@ -115,7 +115,7 @@ void UserListModel::refresh(QMatrixClient::User* user, QVector roles) { void UserListModel::avatarChanged(QMatrixClient::User* user, const QMatrixClient::Room* context) { - if (context == m_currentRoom) refresh(user, {PaintableRole}); + if (context == m_currentRoom) refresh(user, {AvatarRole}); } int UserListModel::findUserPos(User* user) const { @@ -130,6 +130,6 @@ QHash UserListModel::roleNames() const { QHash roles; roles[NameRole] = "name"; roles[UserIDRole] = "userId"; - roles[PaintableRole] = "paintable"; + roles[AvatarRole] = "avatar"; return roles; } diff --git a/src/userlistmodel.h b/src/userlistmodel.h index 88146ad..fd01a5e 100644 --- a/src/userlistmodel.h +++ b/src/userlistmodel.h @@ -17,7 +17,7 @@ class UserListModel : public QAbstractListModel { Q_PROPERTY( QMatrixClient::Room* room READ room WRITE setRoom NOTIFY roomChanged) public: - enum EventRoles { NameRole = Qt::UserRole + 1, UserIDRole, PaintableRole }; + enum EventRoles { NameRole = Qt::UserRole + 1, UserIDRole, AvatarRole }; using User = QMatrixClient::User; From 92b1458c4a28738514e7ababce614a19b0d5ed93 Mon Sep 17 00:00:00 2001 From: Black Hat Date: Mon, 3 Dec 2018 20:11:24 +0800 Subject: [PATCH 2/2] Fix avatar changes. --- src/roomlistmodel.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/roomlistmodel.cpp b/src/roomlistmodel.cpp index b34cee7..6247f4b 100644 --- a/src/roomlistmodel.cpp +++ b/src/roomlistmodel.cpp @@ -70,6 +70,8 @@ void RoomListModel::connectRoomSignals(SpectralRoom* room) { [=] { unreadMessagesChanged(room); }); connect(room, &Room::notificationCountChanged, this, [=] { unreadMessagesChanged(room); }); + connect(room, &Room::avatarChanged, this, + [this, room] { refresh(room, {AvatarRole}); }); connect(room, &Room::tagsChanged, this, [=] { refresh(room); }); connect(room, &Room::joinStateChanged, this, [=] { refresh(room); }); connect(room, &Room::addedMessages, this,