some bug fixes, initial work on avatars in dialog windows

This commit is contained in:
Blue 2019-12-20 18:41:20 +03:00
parent 867c3a18e9
commit f13b43d38b
18 changed files with 125 additions and 42 deletions

View file

@ -19,12 +19,17 @@
#include <QDebug>
#include "image.h"
Image::Image(const QString& path, quint16 p_minWidth, QWidget* parent):
Image::Image(const QString& p_path, quint16 p_minWidth, QWidget* parent):
QLabel(parent),
pixmap(path),
pixmap(p_path),
path(p_path),
aspectRatio(0),
minWidth(p_minWidth)
minWidth(p_minWidth),
svgMode(false)
{
if (path.contains(".svg")) {
svgMode = true;
}
setScaledContents(true);
recalculateAspectRatio();
}
@ -55,7 +60,9 @@ void Image::recalculateAspectRatio()
qreal height = pixmap.height();
qreal width = pixmap.width();
aspectRatio = width / height;
setPixmap(pixmap);
if (!svgMode) {
setPixmap(pixmap);
}
setMinimumHeight(minWidth / aspectRatio);
setMinimumWidth(minWidth);
}
@ -68,8 +75,27 @@ void Image::setMinWidth(quint16 p_minWidth)
}
}
void Image::setPath(const QString& path)
void Image::setPath(const QString& p_path)
{
pixmap = QPixmap(path);
recalculateAspectRatio();
if (path != p_path) {
path = p_path;
if (path.contains(".svg")) {
svgMode = true;
}
pixmap = QPixmap(path);
recalculateAspectRatio();
}
}
void Image::resizeEvent(QResizeEvent* event)
{
if (svgMode) {
QIcon ico;
QSize size = event->size();
ico.addFile(path, size);
setPixmap(ico.pixmap(size));
}
QLabel::resizeEvent(event);
}

View file

@ -21,6 +21,8 @@
#include <QLabel>
#include <QPixmap>
#include <QResizeEvent>
#include <QIcon>
/**
* @todo write docs
@ -29,7 +31,6 @@ class Image : public QLabel
{
public:
Image(const QString& path, quint16 minWidth = 50, QWidget* parent = nullptr);
~Image();
int heightForWidth(int width) const override;
@ -40,11 +41,14 @@ public:
private:
QPixmap pixmap;
QString path;
qreal aspectRatio;
quint16 minWidth;
bool svgMode;
private:
void recalculateAspectRatio();
void resizeEvent(QResizeEvent * event) override;
};
#endif // IMAGE_H

View file

@ -26,7 +26,7 @@
const QRegularExpression urlReg("(?<!<a\\shref=['\"])(?<!<img\\ssrc=['\"])((?:https?|ftp)://\\S+)"); //finds all hypertext references which are not wrapped in a or img tags
const QRegularExpression imgReg("((?:https?|ftp)://\\S+\\.(?:jpg|jpeg|png|svg|gif))");
Message::Message(const Shared::Message& source, bool outgoing, const QString& p_sender, QWidget* parent):
Message::Message(const Shared::Message& source, bool outgoing, const QString& p_sender, const QString& avatarPath, QWidget* parent):
QHBoxLayout(parent),
msg(source),
body(new QWidget()),
@ -39,6 +39,7 @@ Message::Message(const Shared::Message& source, bool outgoing, const QString& p_
file(0),
progress(0),
fileComment(new QLabel()),
avatar(new Image(avatarPath.size() == 0 ? Shared::iconPath("user", true) : avatarPath, 60)),
hasButton(false),
hasProgress(false),
hasFile(false),
@ -77,13 +78,21 @@ Message::Message(const Shared::Message& source, bool outgoing, const QString& p_
shadow->setYOffset(1);
shadow->setColor(Qt::black);
body->setGraphicsEffect(shadow);
avatar->setMaximumHeight(60);
avatar->setMaximumWidth(60);
QVBoxLayout* aLay = new QVBoxLayout();
aLay->addWidget(avatar);
aLay->addStretch();
if (outgoing) {
sender->setAlignment(Qt::AlignRight);
date->setAlignment(Qt::AlignRight);
addStretch();
addWidget(body);
addItem(aLay);
} else {
addItem(aLay);
addWidget(body);
addStretch();
}
@ -95,6 +104,7 @@ Message::~Message()
delete fileComment;
}
delete body;
delete avatar;
}
QString Message::getId() const
@ -243,3 +253,7 @@ const Shared::Message & Message::getMessage() const
return msg;
}
void Message::setAvatarPath(const QString& p_path)
{
avatar->setPath(p_path);
}

View file

@ -41,7 +41,7 @@ class Message : public QHBoxLayout
{
Q_OBJECT
public:
Message(const Shared::Message& source, bool outgoing, const QString& sender, QWidget* parent = nullptr);
Message(const Shared::Message& source, bool outgoing, const QString& sender, const QString& avatarPath = "", QWidget* parent = nullptr);
~Message();
void setSender(const QString& sender);
@ -54,6 +54,7 @@ public:
void hideComment();
void showFile(const QString& path);
void setProgress(qreal value);
void setAvatarPath(const QString& p_path);
signals:
void buttonClicked();
@ -70,6 +71,7 @@ private:
QLabel* file;
QProgressBar* progress;
QLabel* fileComment;
Image* avatar;
bool hasButton;
bool hasProgress;
bool hasFile;

View file

@ -29,6 +29,7 @@ MessageLine::MessageLine(bool p_room, QWidget* parent):
uploadPaths(),
layout(new QVBoxLayout(this)),
myName(),
myAvatarPath(),
palNames(),
uploading(),
downloading(),
@ -57,15 +58,18 @@ MessageLine::Position MessageLine::message(const Shared::Message& msg, bool forc
}
QString sender;
QString aPath;
bool outgoing;
if (forceOutgoing) {
sender = myName;
aPath = myAvatarPath;
outgoing = true;
} else {
if (room) {
if (msg.getFromResource() == myName) {
sender = myName;
aPath = myAvatarPath;
outgoing = true;
} else {
sender = msg.getFromResource();
@ -74,6 +78,7 @@ MessageLine::Position MessageLine::message(const Shared::Message& msg, bool forc
} else {
if (msg.getOutgoing()) {
sender = myName;
aPath = myAvatarPath;
outgoing = true;
} else {
QString jid = msg.getFromJid();
@ -88,7 +93,7 @@ MessageLine::Position MessageLine::message(const Shared::Message& msg, bool forc
}
}
Message* message = new Message(msg, outgoing, sender);
Message* message = new Message(msg, outgoing, sender, aPath);
std::pair<Order::const_iterator, bool> result = messageOrder.insert(std::make_pair(msg.getTime(), message));
if (!result.second) {
@ -348,3 +353,13 @@ void MessageLine::onUpload()
{
//TODO retry
}
void MessageLine::setMyAvatarPath(const QString& p_path)
{
if (myAvatarPath != p_path) {
myAvatarPath = p_path;
for (std::pair<QString, Message*> pair : myMessages) {
pair.second->setAvatarPath(myAvatarPath);
}
}
}

View file

@ -54,6 +54,7 @@ public:
void fileProgress(const QString& messageId, qreal progress);
void appendMessageWithUpload(const Shared::Message& msg, const QString& path);
void removeMessage(const QString& messageId);
void setMyAvatarPath(const QString& p_path);
signals:
void resize(int amount);
@ -87,6 +88,7 @@ private:
QVBoxLayout* layout;
QString myName;
QString myAvatarPath;
std::map<QString, QString> palNames;
Index uploading;
Index downloading;