Remove ImageItem, Paintable and use unified image provider.
This commit is contained in:
@@ -1,108 +0,0 @@
|
||||
#include "imageitem.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QBitmap>
|
||||
#include <QGraphicsOpacityEffect>
|
||||
#include <QRect>
|
||||
#include <QScreen>
|
||||
|
||||
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<Paintable> 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();
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
#ifndef IMAGEITEM_H
|
||||
#define IMAGEITEM_H
|
||||
|
||||
#include <QPointer>
|
||||
#include <QImage>
|
||||
#include <QObject>
|
||||
#include <QPainter>
|
||||
#include <QQuickItem>
|
||||
#include <QQuickPaintedItem>
|
||||
|
||||
#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<Paintable> m_paintable;
|
||||
QString m_hint = "H";
|
||||
QString m_color;
|
||||
bool m_round = true;
|
||||
};
|
||||
|
||||
#endif // IMAGEITEM_H
|
||||
@@ -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<MediaThumbnailJob*>();
|
||||
#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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<qqsfpm::QQmlSortFilterProxyModel>("SortFilterProxyModel", 0,
|
||||
2, "SortFilterProxyModel");
|
||||
qmlRegisterType<ImageItem>("Spectral", 0, 1, "ImageItem");
|
||||
qmlRegisterType<Controller>("Spectral", 0, 1, "Controller");
|
||||
qmlRegisterType<AccountListModel>("Spectral", 0, 1, "AccountListModel");
|
||||
qmlRegisterType<RoomListModel>("Spectral", 0, 1, "RoomListModel");
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
#include "paintable.h"
|
||||
|
||||
Paintable::Paintable(QObject *parent) : QObject(parent) {}
|
||||
@@ -1,20 +0,0 @@
|
||||
#ifndef PAINTABLE_H
|
||||
#define PAINTABLE_H
|
||||
|
||||
#include <QImage>
|
||||
#include <QObject>
|
||||
|
||||
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
|
||||
@@ -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<int, QByteArray> RoomListModel::roleNames() const {
|
||||
QHash<int, QByteArray> roles;
|
||||
roles[NameRole] = "name";
|
||||
roles[PaintableRole] = "paintable";
|
||||
roles[AvatarRole] = "avatar";
|
||||
roles[TopicRole] = "topic";
|
||||
roles[CategoryRole] = "category";
|
||||
roles[UnreadCountRole] = "unreadCount";
|
||||
|
||||
@@ -31,7 +31,7 @@ class RoomListModel : public QAbstractListModel {
|
||||
public:
|
||||
enum EventRoles {
|
||||
NameRole = Qt::UserRole + 1,
|
||||
PaintableRole,
|
||||
AvatarRole,
|
||||
TopicRole,
|
||||
CategoryRole,
|
||||
UnreadCountRole,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,44 +1,18 @@
|
||||
#ifndef SpectralUser_H
|
||||
#define SpectralUser_H
|
||||
|
||||
#include "paintable.h"
|
||||
#include "room.h"
|
||||
#include "user.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QPointer>
|
||||
|
||||
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
|
||||
|
||||
@@ -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<SpectralUser*>(user))->paintable());
|
||||
if (role == AvatarRole) {
|
||||
return user->avatarUrl();
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
@@ -115,7 +115,7 @@ void UserListModel::refresh(QMatrixClient::User* user, QVector<int> 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<int, QByteArray> UserListModel::roleNames() const {
|
||||
QHash<int, QByteArray> roles;
|
||||
roles[NameRole] = "name";
|
||||
roles[UserIDRole] = "userId";
|
||||
roles[PaintableRole] = "paintable";
|
||||
roles[AvatarRole] = "avatar";
|
||||
return roles;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user