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

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

View File

@@ -14,8 +14,12 @@ void ImageItem::paint(QPainter *painter) {
if (m_image.isNull()) {
painter->setPen(Qt::NoPen);
painter->setBrush(QColor(m_color));
painter->drawEllipse(0, 0, int(bounding_rect.width()),
int(bounding_rect.height()));
if (m_round)
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));
QFont font;
font.setPixelSize(22);
@@ -33,11 +37,13 @@ void ImageItem::paint(QPainter *painter) {
QPointF center = bounding_rect.center() - scaled.rect().center();
QPainterPath clip;
clip.addEllipse(
0, 0, bounding_rect.width(),
bounding_rect.height()); // this is the shape we want to clip to
painter->setClipPath(clip);
if (m_round) {
QPainterPath clip;
clip.addEllipse(
0, 0, bounding_rect.width(),
bounding_rect.height()); // this is the shape we want to clip to
painter->setClipPath(clip);
}
if (center.x() < 0) center.setX(0);
if (center.y() < 0) center.setY(0);
@@ -66,3 +72,11 @@ void ImageItem::setDefaultColor(QString color) {
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 defaultColor READ defaultColor WRITE setDefaultColor NOTIFY
defaultColorChanged)
Q_PROPERTY(bool round READ round WRITE setRound NOTIFY roundChanged)
public:
ImageItem(QQuickItem *parent = nullptr);
@@ -28,15 +29,20 @@ class ImageItem : public QQuickPaintedItem {
QString defaultColor() { return m_color; }
void setDefaultColor(QString color);
bool round() { return m_round; }
void setRound(bool value);
signals:
void imageChanged();
void hintChanged();
void defaultColorChanged();
void roundChanged();
private:
QImage m_image;
QString m_hint = "H";
QString m_color = "#000000";
bool m_round = true;
};
#endif // IMAGEITEM_H

View File

@@ -10,6 +10,7 @@ using namespace QMatrixClient;
class MatriqueRoom : public Room {
Q_OBJECT
Q_PROPERTY(QImage avatar READ getAvatar NOTIFY avatarChanged)
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
@@ -18,6 +19,8 @@ class MatriqueRoom : public Room {
explicit MatriqueRoom(Connection* connection, QString roomId,
JoinState joinState = {});
QImage getAvatar() { return avatar(64); }
const QString& cachedInput() const { return m_cachedInput; }
void setCachedInput(const QString& input) {
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