Use ImageItem in MessageDelegate.

This commit is contained in:
Black Hat 2018-09-10 09:51:02 +08:00
parent 13a8d6b889
commit 0f3d7db0d1
11 changed files with 91 additions and 18 deletions

View File

@ -30,7 +30,8 @@ SOURCES += src/main.cpp \
src/matriqueroom.cpp \ src/matriqueroom.cpp \
src/userlistmodel.cpp \ src/userlistmodel.cpp \
src/imageitem.cpp \ src/imageitem.cpp \
src/accountlistmodel.cpp src/accountlistmodel.cpp \
src/matriqueuser.cpp
RESOURCES += \ RESOURCES += \
res.qrc res.qrc
@ -91,4 +92,5 @@ HEADERS += \
src/matriqueroom.h \ src/matriqueroom.h \
src/userlistmodel.h \ src/userlistmodel.h \
src/imageitem.h \ src/imageitem.h \
src/accountlistmodel.h src/accountlistmodel.h \
src/matriqueuser.h

View File

@ -4,6 +4,7 @@ import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.2 import QtQuick.Controls.Material 2.2
import Matrique 0.1 import Matrique 0.1
import Matrique.Settings 0.1 import Matrique.Settings 0.1
import "qrc:/js/util.js" as Util
RowLayout { RowLayout {
readonly property bool avatarVisible: !(sentByMe || (aboveAuthor === author && section === aboveSection)) readonly property bool avatarVisible: !(sentByMe || (aboveAuthor === author && section === aboveSection))
@ -22,15 +23,16 @@ RowLayout {
spacing: 6 spacing: 6
ImageStatus { ImageItem {
Layout.preferredWidth: 40 Layout.preferredWidth: 40
Layout.preferredHeight: 40 Layout.preferredHeight: 40
Layout.alignment: Qt.AlignTop Layout.alignment: Qt.AlignTop
round: false round: false
visible: avatarVisible visible: avatarVisible
source: author.avatarUrl != "" ? "image://mxc/" + author.avatarUrl : null hint: author.displayName
displayText: author.displayName defaultColor: Util.stringToColor(author.displayName)
image: author.avatar
} }
Rectangle { Rectangle {
@ -61,6 +63,11 @@ RowLayout {
Material.foreground: Material.accent Material.foreground: Material.accent
coloredBackground: highlighted coloredBackground: highlighted
font.bold: true font.bold: true
MouseArea {
anchors.fill: parent
onClicked: inputField.insert(inputField.cursorPosition, author.displayName)
}
} }
AutoLabel { AutoLabel {

View File

@ -24,13 +24,14 @@ Drawer {
anchors.fill: parent anchors.fill: parent
anchors.margins: 32 anchors.margins: 32
ImageStatus { ImageItem {
Layout.preferredWidth: 64 Layout.preferredWidth: 64
Layout.preferredHeight: 64 Layout.preferredHeight: 64
Layout.alignment: Qt.AlignHCenter Layout.alignment: Qt.AlignHCenter
source: room && room.avatarUrl != "" ? "image://mxc/" + room.avatarUrl : null hint: room ? room.displayName : "No name"
displayText: room ? room.displayName : "" defaultColor: Util.stringToColor(room ? room.displayName : "No name")
image: matriqueController.safeImage(room ? room.avatar : null)
} }
Label { Label {

View File

@ -9,6 +9,7 @@ import Matrique.Settings 0.1
import "../component" import "../component"
import "qrc:/js/md.js" as Markdown import "qrc:/js/md.js" as Markdown
import "qrc:/js/util.js" as Util
Item { Item {
property var currentRoom: null property var currentRoom: null
@ -57,12 +58,13 @@ Item {
spacing: 12 spacing: 12
ImageStatus { ImageItem {
Layout.preferredWidth: height Layout.preferredWidth: height
Layout.fillHeight: true Layout.fillHeight: true
source: currentRoom && currentRoom.avatarUrl != "" ? "image://mxc/" + currentRoom.avatarUrl : null hint: currentRoom ? currentRoom.displayName : "No name"
displayText: currentRoom ? currentRoom.displayName : "" defaultColor: Util.stringToColor(currentRoom ? currentRoom.displayName : "No name")
image: matriqueController.safeImage(currentRoom ? currentRoom.avatar : null)
} }
ColumnLayout { ColumnLayout {

View File

@ -1,6 +1,7 @@
#include "controller.h" #include "controller.h"
#include "matriqueroom.h" #include "matriqueroom.h"
#include "matriqueuser.h"
#include "settings.h" #include "settings.h"
#include "events/eventcontent.h" #include "events/eventcontent.h"
@ -39,6 +40,7 @@ Controller::Controller(QObject* parent) : QObject(parent) {
tray->show(); tray->show();
Connection::setRoomType<MatriqueRoom>(); Connection::setRoomType<MatriqueRoom>();
Connection::setUserType<MatriqueUser>();
QTimer::singleShot(0, this, SLOT(invokeLogin())); QTimer::singleShot(0, this, SLOT(invokeLogin()));
} }
@ -215,3 +217,8 @@ void Controller::showMessage(const QString& title, const QString& msg,
const QIcon& icon) { const QIcon& icon) {
tray->showMessage(title, msg, icon); tray->showMessage(title, msg, icon);
} }
QImage Controller::safeImage(QImage image) {
if (image.isNull()) return QImage();
return image;
}

View File

@ -72,6 +72,8 @@ class Controller : public QObject {
void copyToClipboard(const QString& text); void copyToClipboard(const QString& text);
void playAudio(QUrl localFile); void playAudio(QUrl localFile);
void showMessage(const QString& title, const QString& msg, const QIcon& icon); void showMessage(const QString& title, const QString& msg, const QIcon& icon);
static QImage safeImage(QImage image);
}; };
#endif // CONTROLLER_H #endif // CONTROLLER_H

View File

@ -14,8 +14,12 @@ void ImageItem::paint(QPainter *painter) {
if (m_image.isNull()) { if (m_image.isNull()) {
painter->setPen(Qt::NoPen); painter->setPen(Qt::NoPen);
painter->setBrush(QColor(m_color)); painter->setBrush(QColor(m_color));
painter->drawEllipse(0, 0, int(bounding_rect.width()), if (m_round)
int(bounding_rect.height())); painter->drawEllipse(0, 0, int(bounding_rect.width()),
int(bounding_rect.height()));
else
painter->drawRect(0, 0, int(bounding_rect.width()),
int(bounding_rect.height()));
painter->setPen(QPen(Qt::white, 2)); painter->setPen(QPen(Qt::white, 2));
QFont font; QFont font;
font.setPixelSize(22); font.setPixelSize(22);
@ -33,11 +37,13 @@ void ImageItem::paint(QPainter *painter) {
QPointF center = bounding_rect.center() - scaled.rect().center(); QPointF center = bounding_rect.center() - scaled.rect().center();
QPainterPath clip; if (m_round) {
clip.addEllipse( QPainterPath clip;
0, 0, bounding_rect.width(), clip.addEllipse(
bounding_rect.height()); // this is the shape we want to clip to 0, 0, bounding_rect.width(),
painter->setClipPath(clip); bounding_rect.height()); // this is the shape we want to clip to
painter->setClipPath(clip);
}
if (center.x() < 0) center.setX(0); if (center.x() < 0) center.setX(0);
if (center.y() < 0) center.setY(0); if (center.y() < 0) center.setY(0);
@ -66,3 +72,11 @@ void ImageItem::setDefaultColor(QString color) {
update(); update();
} }
} }
void ImageItem::setRound(bool value) {
if (m_round != value) {
m_round = value;
emit roundChanged();
update();
}
}

View File

@ -13,6 +13,7 @@ class ImageItem : public QQuickPaintedItem {
Q_PROPERTY(QString hint READ hint WRITE setHint NOTIFY hintChanged) Q_PROPERTY(QString hint READ hint WRITE setHint NOTIFY hintChanged)
Q_PROPERTY(QString defaultColor READ defaultColor WRITE setDefaultColor NOTIFY Q_PROPERTY(QString defaultColor READ defaultColor WRITE setDefaultColor NOTIFY
defaultColorChanged) defaultColorChanged)
Q_PROPERTY(bool round READ round WRITE setRound NOTIFY roundChanged)
public: public:
ImageItem(QQuickItem *parent = nullptr); ImageItem(QQuickItem *parent = nullptr);
@ -28,15 +29,20 @@ class ImageItem : public QQuickPaintedItem {
QString defaultColor() { return m_color; } QString defaultColor() { return m_color; }
void setDefaultColor(QString color); void setDefaultColor(QString color);
bool round() { return m_round; }
void setRound(bool value);
signals: signals:
void imageChanged(); void imageChanged();
void hintChanged(); void hintChanged();
void defaultColorChanged(); void defaultColorChanged();
void roundChanged();
private: private:
QImage m_image; QImage m_image;
QString m_hint = "H"; QString m_hint = "H";
QString m_color = "#000000"; QString m_color = "#000000";
bool m_round = true;
}; };
#endif // IMAGEITEM_H #endif // IMAGEITEM_H

View File

@ -10,6 +10,7 @@ using namespace QMatrixClient;
class MatriqueRoom : public Room { class MatriqueRoom : public Room {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QImage avatar READ getAvatar NOTIFY avatarChanged)
Q_PROPERTY(bool hasUsersTyping READ hasUsersTyping NOTIFY typingChanged) Q_PROPERTY(bool hasUsersTyping READ hasUsersTyping NOTIFY typingChanged)
Q_PROPERTY(QString usersTyping READ getUsersTyping NOTIFY typingChanged) Q_PROPERTY(QString usersTyping READ getUsersTyping NOTIFY typingChanged)
Q_PROPERTY(QString cachedInput READ cachedInput WRITE setCachedInput NOTIFY Q_PROPERTY(QString cachedInput READ cachedInput WRITE setCachedInput NOTIFY
@ -18,6 +19,8 @@ class MatriqueRoom : public Room {
explicit MatriqueRoom(Connection* connection, QString roomId, explicit MatriqueRoom(Connection* connection, QString roomId,
JoinState joinState = {}); JoinState joinState = {});
QImage getAvatar() { return avatar(64); }
const QString& cachedInput() const { return m_cachedInput; } const QString& cachedInput() const { return m_cachedInput; }
void setCachedInput(const QString& input) { void setCachedInput(const QString& input) {
if (input != m_cachedInput) { if (input != m_cachedInput) {

6
src/matriqueuser.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "matriqueuser.h"
MatriqueUser::MatriqueUser(QString userId, Connection* connection)
: User(userId, connection) {
connect(this, &User::avatarChanged, this, &MatriqueUser::inheritedAvatarChanged);
}

23
src/matriqueuser.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef MATRIQUEUSER_H
#define MATRIQUEUSER_H
#include "user.h"
#include "room.h"
#include <QObject>
using namespace QMatrixClient;
class MatriqueUser : public User {
Q_OBJECT
Q_PROPERTY(QImage avatar READ getAvatar NOTIFY inheritedAvatarChanged)
public:
MatriqueUser(QString userId, Connection* connection);
QImage getAvatar() { return avatar(64); }
signals:
void inheritedAvatarChanged(User* user, const Room* roomContext);
};
#endif // MATRIQUEUSER_H