edit icon next to the message showing if the message was edited and what was there a first

This commit is contained in:
Blue 2021-05-25 01:06:05 +03:00
parent 3f1fba4de2
commit 5f925217fc
Signed by untrusted user: blue
GPG Key ID: 9B203B252A63EE38
4 changed files with 84 additions and 7 deletions

View File

@ -42,6 +42,7 @@ MessageDelegate::MessageDelegate(QObject* parent):
buttons(new std::map<QString, FeedButton*>()), buttons(new std::map<QString, FeedButton*>()),
bars(new std::map<QString, QProgressBar*>()), bars(new std::map<QString, QProgressBar*>()),
statusIcons(new std::map<QString, QLabel*>()), statusIcons(new std::map<QString, QLabel*>()),
pencilIcons(new std::map<QString, QLabel*>()),
bodies(new std::map<QString, QLabel*>()), bodies(new std::map<QString, QLabel*>()),
previews(new std::map<QString, Preview*>()), previews(new std::map<QString, Preview*>()),
idsToKeep(new std::set<QString>()), idsToKeep(new std::set<QString>()),
@ -68,6 +69,10 @@ MessageDelegate::~MessageDelegate()
delete pair.second; delete pair.second;
} }
for (const std::pair<const QString, QLabel*>& pair: *pencilIcons){
delete pair.second;
}
for (const std::pair<const QString, QLabel*>& pair: *bodies){ for (const std::pair<const QString, QLabel*>& pair: *bodies){
delete pair.second; delete pair.second;
} }
@ -76,6 +81,8 @@ MessageDelegate::~MessageDelegate()
delete pair.second; delete pair.second;
} }
delete statusIcons;
delete pencilIcons;
delete idsToKeep; delete idsToKeep;
delete buttons; delete buttons;
delete bars; delete bars;
@ -128,6 +135,18 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
if (senderSize.width() > messageSize.width()) { if (senderSize.width() > messageSize.width()) {
messageSize.setWidth(senderSize.width()); messageSize.setWidth(senderSize.width());
} }
QSize dateSize = dateMetrics.boundingRect(messageRect, 0, data.date.toLocalTime().toString()).size();
int addition = 0;
if (data.correction.corrected) {
addition += margin + statusIconSize;
}
if (data.sentByMe) {
addition += margin + statusIconSize;
}
if (dateSize.width() + addition > messageSize.width()) {
messageSize.setWidth(dateSize.width() + addition);
}
} else { } else {
messageSize.setWidth(opt.rect.width()); messageSize.setWidth(opt.rect.width());
} }
@ -170,6 +189,7 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
painter->restore(); painter->restore();
int messageLeft = INT16_MAX; int messageLeft = INT16_MAX;
int messageRight = opt.rect.x() + messageSize.width();
QWidget* vp = static_cast<QWidget*>(painter->device()); QWidget* vp = static_cast<QWidget*>(painter->device());
if (data.text.size() > 0) { if (data.text.size() > 0) {
QLabel* body = getBody(data); QLabel* body = getBody(data);
@ -192,18 +212,35 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio
q.setAlpha(180); q.setAlpha(180);
painter->setPen(q); painter->setPen(q);
painter->drawText(opt.rect, opt.displayAlignment, data.date.toLocalTime().toString(), &rect); painter->drawText(opt.rect, opt.displayAlignment, data.date.toLocalTime().toString(), &rect);
int currentY = opt.rect.y();
if (data.sentByMe) { if (data.sentByMe) {
if (messageLeft > rect.x() - statusIconSize - margin) {
messageLeft = rect.x() - statusIconSize - margin;
}
QLabel* statusIcon = getStatusIcon(data); QLabel* statusIcon = getStatusIcon(data);
statusIcon->setParent(vp); statusIcon->setParent(vp);
statusIcon->move(messageLeft, opt.rect.y()); statusIcon->move(opt.rect.topRight().x() - messageSize.width(), currentY);
statusIcon->show(); statusIcon->show();
opt.rect.adjust(0, statusIconSize + textMargin, 0, 0); opt.rect.adjust(0, statusIconSize + textMargin, 0, 0);
} }
if (data.correction.corrected) {
QLabel* pencilIcon = getPencilIcon(data);
pencilIcon->setParent(vp);
if (data.sentByMe) {
pencilIcon->move(opt.rect.topRight().x() - messageSize.width() + statusIconSize + margin, currentY);
} else {
pencilIcon->move(messageRight - statusIconSize - margin, currentY);
}
pencilIcon->show();
} else {
std::map<QString, QLabel*>::const_iterator itr = pencilIcons->find(data.id);
if (itr != pencilIcons->end()) {
delete itr->second;
pencilIcons->erase(itr);
}
}
painter->restore(); painter->restore();
if (clearingWidgets) { if (clearingWidgets) {
@ -439,6 +476,26 @@ QLabel * MessageDelegate::getStatusIcon(const Models::FeedItem& data) const
return result; return result;
} }
QLabel * MessageDelegate::getPencilIcon(const Models::FeedItem& data) const
{
std::map<QString, QLabel*>::const_iterator itr = pencilIcons->find(data.id);
QLabel* result = 0;
if (itr != pencilIcons->end()) {
result = itr->second;
} else {
result = new QLabel();
QIcon icon = Shared::icon("edit-rename");
result->setPixmap(icon.pixmap(statusIconSize));
pencilIcons->insert(std::make_pair(data.id, result));
}
result->setToolTip("Last time edited: " + data.correction.lastCorrection.toLocalTime().toString()
+ "\nOriginal message: " + data.correction.original);
return result;
}
QLabel * MessageDelegate::getBody(const Models::FeedItem& data) const QLabel * MessageDelegate::getBody(const Models::FeedItem& data) const
{ {
std::map<QString, QLabel*>::const_iterator itr = bodies->find(data.id); std::map<QString, QLabel*>::const_iterator itr = bodies->find(data.id);
@ -486,6 +543,7 @@ void MessageDelegate::endClearWidgets()
removeElements(buttons, idsToKeep); removeElements(buttons, idsToKeep);
removeElements(bars, idsToKeep); removeElements(bars, idsToKeep);
removeElements(statusIcons, idsToKeep); removeElements(statusIcons, idsToKeep);
removeElements(pencilIcons, idsToKeep);
removeElements(bodies, idsToKeep); removeElements(bodies, idsToKeep);
removeElements(previews, idsToKeep); removeElements(previews, idsToKeep);

View File

@ -68,6 +68,7 @@ protected:
QPushButton* getButton(const Models::FeedItem& data) const; QPushButton* getButton(const Models::FeedItem& data) const;
QProgressBar* getBar(const Models::FeedItem& data) const; QProgressBar* getBar(const Models::FeedItem& data) const;
QLabel* getStatusIcon(const Models::FeedItem& data) const; QLabel* getStatusIcon(const Models::FeedItem& data) const;
QLabel* getPencilIcon(const Models::FeedItem& data) const;
QLabel* getBody(const Models::FeedItem& data) const; QLabel* getBody(const Models::FeedItem& data) const;
void clearHelperWidget(const Models::FeedItem& data) const; void clearHelperWidget(const Models::FeedItem& data) const;
@ -93,6 +94,7 @@ private:
std::map<QString, FeedButton*>* buttons; std::map<QString, FeedButton*>* buttons;
std::map<QString, QProgressBar*>* bars; std::map<QString, QProgressBar*>* bars;
std::map<QString, QLabel*>* statusIcons; std::map<QString, QLabel*>* statusIcons;
std::map<QString, QLabel*>* pencilIcons;
std::map<QString, QLabel*>* bodies; std::map<QString, QLabel*>* bodies;
std::map<QString, Preview*>* previews; std::map<QString, Preview*>* previews;
std::set<QString>* idsToKeep; std::set<QString>* idsToKeep;

View File

@ -262,7 +262,7 @@ QVariant Models::MessageFeed::data(const QModelIndex& index, int role) const
answer = static_cast<unsigned int>(msg->getState()); answer = static_cast<unsigned int>(msg->getState());
break; break;
case Correction: case Correction:
answer = msg->getEdited(); answer.setValue(fillCorrection(*msg));;
break; break;
case SentByMe: case SentByMe:
answer = sentByMe(*msg); answer = sentByMe(*msg);
@ -311,7 +311,7 @@ QVariant Models::MessageFeed::data(const QModelIndex& index, int role) const
item.date = msg->getTime(); item.date = msg->getTime();
item.state = msg->getState(); item.state = msg->getState();
item.error = msg->getErrorText(); item.error = msg->getErrorText();
item.correction = msg->getEdited(); item.correction = fillCorrection(*msg);
QString body = msg->getBody(); QString body = msg->getBody();
if (body != msg->getOutOfBandUrl()) { if (body != msg->getOutOfBandUrl()) {
@ -480,6 +480,14 @@ Models::Attachment Models::MessageFeed::fillAttach(const Shared::Message& msg) c
return att; return att;
} }
Models::Edition Models::MessageFeed::fillCorrection(const Shared::Message& msg) const
{
::Models::Edition ed({msg.getEdited(), msg.getOriginalBody(), msg.getLastModified()});
return ed;
}
void Models::MessageFeed::downloadAttachment(const QString& messageId) void Models::MessageFeed::downloadAttachment(const QString& messageId)
{ {
bool notify = false; bool notify = false;

View File

@ -37,6 +37,7 @@
namespace Models { namespace Models {
class Element; class Element;
struct Attachment; struct Attachment;
struct Edition;
class MessageFeed : public QAbstractListModel class MessageFeed : public QAbstractListModel
{ {
@ -106,6 +107,7 @@ public:
protected: protected:
bool sentByMe(const Shared::Message& msg) const; bool sentByMe(const Shared::Message& msg) const;
Attachment fillAttach(const Shared::Message& msg) const; Attachment fillAttach(const Shared::Message& msg) const;
Edition fillCorrection(const Shared::Message& msg) const;
QModelIndex modelIndexById(const QString& id) const; QModelIndex modelIndexById(const QString& id) const;
QModelIndex modelIndexByTime(const QString& id, const QDateTime& time) const; QModelIndex modelIndexByTime(const QString& id, const QDateTime& time) const;
std::set<MessageRoles> detectChanges(const Shared::Message& msg, const QMap<QString, QVariant>& data) const; std::set<MessageRoles> detectChanges(const Shared::Message& msg, const QMap<QString, QVariant>& data) const;
@ -180,6 +182,12 @@ struct Attachment {
QString error; QString error;
}; };
struct Edition {
bool corrected;
QString original;
QDateTime lastCorrection;
};
struct FeedItem { struct FeedItem {
QString id; QString id;
QString text; QString text;
@ -187,7 +195,7 @@ struct FeedItem {
QString avatar; QString avatar;
QString error; QString error;
bool sentByMe; bool sentByMe;
bool correction; Edition correction;
QDateTime date; QDateTime date;
Shared::Message::State state; Shared::Message::State state;
Attachment attach; Attachment attach;
@ -195,6 +203,7 @@ struct FeedItem {
}; };
Q_DECLARE_METATYPE(Models::Attachment); Q_DECLARE_METATYPE(Models::Attachment);
Q_DECLARE_METATYPE(Models::Edition);
Q_DECLARE_METATYPE(Models::FeedItem); Q_DECLARE_METATYPE(Models::FeedItem);
#endif // MESSAGEFEED_H #endif // MESSAGEFEED_H