ui logick of changing message, not connected yet

This commit is contained in:
Blue 2020-02-08 14:44:15 +03:00
parent ed56cca2e7
commit 6d1b83d0f8
18 changed files with 246 additions and 19 deletions

View file

@ -35,8 +35,9 @@ const QRegularExpression urlReg("(?<!<a\\shref=['\"])(?<!<img\\ssrc=['\"])("
")");
const QRegularExpression imgReg("((?:https?|ftp)://\\S+\\.(?:jpg|jpeg|png|svg|gif))");
Message::Message(const Shared::Message& source, bool outgoing, const QString& p_sender, const QString& avatarPath, QWidget* parent):
Message::Message(const Shared::Message& source, bool p_outgoing, const QString& p_sender, const QString& avatarPath, QWidget* parent):
QWidget(parent),
outgoing(p_outgoing),
msg(source),
body(new QWidget()),
statusBar(new QWidget()),
@ -50,11 +51,15 @@ Message::Message(const Shared::Message& source, bool outgoing, const QString& p_
file(0),
progress(0),
fileComment(new QLabel()),
statusIcon(0),
editedLabel(0),
avatar(new Image(avatarPath.size() == 0 ? Shared::iconPath("user", true) : avatarPath, 60)),
hasButton(false),
hasProgress(false),
hasFile(false),
commentAdded(false)
commentAdded(false),
hasStatusIcon(false),
hasEditedLabel(false)
{
setContentsMargins(0, 0, 0, 0);
layout->setContentsMargins(10, 5, 10, 5);
@ -102,8 +107,8 @@ Message::Message(const Shared::Message& source, bool outgoing, const QString& p_
if (outgoing) {
sender->setAlignment(Qt::AlignRight);
date->setAlignment(Qt::AlignRight);
statusIcon = new QLabel();
QIcon q(Shared::icon(Shared::messageStateThemeIcons[static_cast<uint8_t>(source.getState())]));
QLabel* statusIcon = new QLabel();
statusIcon->setToolTip(QCoreApplication::translate("Global", Shared::messageStateNames[static_cast<uint8_t>(source.getState())].toLatin1()));
statusIcon->setPixmap(q.pixmap(12, 12));
statusLay->addWidget(statusIcon);
@ -111,6 +116,7 @@ Message::Message(const Shared::Message& source, bool outgoing, const QString& p_
layout->addStretch();
layout->addWidget(body);
layout->addWidget(avatar);
hasStatusIcon = true;
} else {
layout->addWidget(avatar);
layout->addWidget(body);
@ -127,8 +133,8 @@ Message::~Message()
if (!commentAdded) {
delete fileComment;
}
delete body;
delete avatar;
//delete body; //not sure if I should delete it here, it's probably already owned by the infrastructure and gonna die with the rest of the widget
//delete avatar;
}
QString Message::getId() const
@ -136,6 +142,16 @@ QString Message::getId() const
return msg.getId();
}
QString Message::getSenderJid() const
{
return msg.getFromJid();
}
QString Message::getSenderResource() const
{
return msg.getFromResource();
}
QString Message::getFileUrl() const
{
return msg.getOutOfBandUrl();
@ -286,3 +302,42 @@ void Message::setAvatarPath(const QString& p_path)
avatar->setPath(p_path);
}
}
bool Message::change(const QMap<QString, QVariant>& data)
{
bool idChanged = msg.change(data);
QString bd = msg.getBody();
//bd.replace(imgReg, "<img src=\"\\1\"/>");
bd.replace(urlReg, "<a href=\"\\1\">\\1</a>");
text->setText(bd);
if (bd.size() > 0) {
text->show();
} else {
text->hide();
}
if (msg.getEdited()) {
if (!hasEditedLabel) {
editedLabel = new QLabel();
QFont eFont = editedLabel->font();
eFont.setItalic(true);
eFont.setPointSize(eFont.pointSize() - 2);
editedLabel->setFont(eFont);
hasEditedLabel = true;
QHBoxLayout* statusLay = static_cast<QHBoxLayout*>(statusBar->layout());
if (hasStatusIcon) {
statusLay->insertWidget(1, editedLabel);
} else {
statusLay->insertWidget(0, editedLabel);
}
}
}
if (hasStatusIcon) {
QIcon q(Shared::icon(Shared::messageStateThemeIcons[static_cast<uint8_t>(msg.getState())]));
statusIcon->setToolTip(QCoreApplication::translate("Global", Shared::messageStateNames[static_cast<uint8_t>(msg.getState())].toLatin1()));
statusIcon->setPixmap(q.pixmap(12, 12));
}
return idChanged;
}

View file

@ -29,6 +29,7 @@
#include <QAction>
#include <QDesktopServices>
#include <QUrl>
#include <QMap>
#include "global.h"
#include "resizer.h"
@ -46,6 +47,8 @@ public:
void setSender(const QString& sender);
QString getId() const;
QString getSenderJid() const;
QString getSenderResource() const;
QString getFileUrl() const;
const Shared::Message& getMessage() const;
@ -55,6 +58,9 @@ public:
void showFile(const QString& path);
void setProgress(qreal value);
void setAvatarPath(const QString& p_path);
bool change(const QMap<QString, QVariant>& data);
bool const outgoing;
signals:
void buttonClicked();
@ -73,11 +79,15 @@ private:
QLabel* file;
QProgressBar* progress;
QLabel* fileComment;
QLabel* statusIcon;
QLabel* editedLabel;
Image* avatar;
bool hasButton;
bool hasProgress;
bool hasFile;
bool commentAdded;
bool hasStatusIcon;
bool hasEditedLabel;
private:
void hideButton();

View file

@ -122,7 +122,7 @@ MessageLine::Position MessageLine::message(const Shared::Message& msg, bool forc
if (room) {
senderId = sender;
} else {
QString jid = msg.getFromJid();
senderId = msg.getFromJid();
}
std::map<QString, Index>::iterator pItr = palMessages.find(senderId);
@ -161,6 +161,48 @@ MessageLine::Position MessageLine::message(const Shared::Message& msg, bool forc
return res;
}
void MessageLine::changeMessage(const QString& id, const QMap<QString, QVariant>& data)
{
Index::const_iterator itr = messageIndex.find(id);
if (itr != messageIndex.end()) {
Message* msg = itr->second;
if (msg->change(data)) { //if ID changed (stanza in replace of another)
QString newId = msg->getId(); //need to updated IDs of that message in all maps
messageIndex.erase(itr);
messageIndex.insert(std::make_pair(newId, msg));
if (msg->outgoing) {
QString senderId;
if (room) {
senderId = msg->getSenderResource();
} else {
senderId = msg->getSenderJid();
}
std::map<QString, Index>::iterator pItr = palMessages.find(senderId);
if (pItr != palMessages.end()) {
Index::const_iterator sItr = pItr->second.find(id);
if (sItr != pItr->second.end()) {
pItr->second.erase(sItr);
pItr->second.insert(std::make_pair(newId, msg));
} else {
qDebug() << "Was trying to replace message in open conversations, couldn't find it among pal's messages, probably an error";
}
} else {
qDebug() << "Was trying to replace message in open conversations, couldn't find pal messages, probably an error";
}
} else {
Index::const_iterator mItr = myMessages.find(id);
if (mItr != myMessages.end()) {
myMessages.erase(mItr);
myMessages.insert(std::make_pair(newId, msg));
} else {
qDebug() << "Was trying to replace message in open conversations, couldn't find it among my messages, probably an error";
}
}
}
}
}
void MessageLine::onDownload()
{
Message* msg = static_cast<Message*>(sender());

View file

@ -26,7 +26,7 @@
#include <QResizeEvent>
#include <QIcon>
#include "../../global.h"
#include "global.h"
#include "message.h"
#include "progress.h"
@ -57,6 +57,7 @@ public:
void setMyAvatarPath(const QString& p_path);
void setPalAvatar(const QString& jid, const QString& path);
void dropPalAvatar(const QString& jid);
void changeMessage(const QString& id, const QMap<QString, QVariant>& data);
signals:
void resize(int amount);