From 5f925217fc1f2470ccd04b18936f08b879be81f9 Mon Sep 17 00:00:00 2001 From: blue Date: Tue, 25 May 2021 01:06:05 +0300 Subject: [PATCH] edit icon next to the message showing if the message was edited and what was there a first --- ui/widgets/messageline/messagedelegate.cpp | 66 ++++++++++++++++++++-- ui/widgets/messageline/messagedelegate.h | 2 + ui/widgets/messageline/messagefeed.cpp | 12 +++- ui/widgets/messageline/messagefeed.h | 11 +++- 4 files changed, 84 insertions(+), 7 deletions(-) diff --git a/ui/widgets/messageline/messagedelegate.cpp b/ui/widgets/messageline/messagedelegate.cpp index 9b46b7a..8728ba3 100644 --- a/ui/widgets/messageline/messagedelegate.cpp +++ b/ui/widgets/messageline/messagedelegate.cpp @@ -42,6 +42,7 @@ MessageDelegate::MessageDelegate(QObject* parent): buttons(new std::map()), bars(new std::map()), statusIcons(new std::map()), + pencilIcons(new std::map()), bodies(new std::map()), previews(new std::map()), idsToKeep(new std::set()), @@ -68,6 +69,10 @@ MessageDelegate::~MessageDelegate() delete pair.second; } + for (const std::pair& pair: *pencilIcons){ + delete pair.second; + } + for (const std::pair& pair: *bodies){ delete pair.second; } @@ -76,6 +81,8 @@ MessageDelegate::~MessageDelegate() delete pair.second; } + delete statusIcons; + delete pencilIcons; delete idsToKeep; delete buttons; delete bars; @@ -128,6 +135,18 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio if (senderSize.width() > messageSize.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 { messageSize.setWidth(opt.rect.width()); } @@ -170,6 +189,7 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio painter->restore(); int messageLeft = INT16_MAX; + int messageRight = opt.rect.x() + messageSize.width(); QWidget* vp = static_cast(painter->device()); if (data.text.size() > 0) { QLabel* body = getBody(data); @@ -192,18 +212,35 @@ void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& optio q.setAlpha(180); painter->setPen(q); painter->drawText(opt.rect, opt.displayAlignment, data.date.toLocalTime().toString(), &rect); + int currentY = opt.rect.y(); if (data.sentByMe) { - if (messageLeft > rect.x() - statusIconSize - margin) { - messageLeft = rect.x() - statusIconSize - margin; - } QLabel* statusIcon = getStatusIcon(data); statusIcon->setParent(vp); - statusIcon->move(messageLeft, opt.rect.y()); + statusIcon->move(opt.rect.topRight().x() - messageSize.width(), currentY); statusIcon->show(); + 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::const_iterator itr = pencilIcons->find(data.id); + if (itr != pencilIcons->end()) { + delete itr->second; + pencilIcons->erase(itr); + } + } + painter->restore(); if (clearingWidgets) { @@ -439,6 +476,26 @@ QLabel * MessageDelegate::getStatusIcon(const Models::FeedItem& data) const return result; } +QLabel * MessageDelegate::getPencilIcon(const Models::FeedItem& data) const +{ + std::map::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 { std::map::const_iterator itr = bodies->find(data.id); @@ -486,6 +543,7 @@ void MessageDelegate::endClearWidgets() removeElements(buttons, idsToKeep); removeElements(bars, idsToKeep); removeElements(statusIcons, idsToKeep); + removeElements(pencilIcons, idsToKeep); removeElements(bodies, idsToKeep); removeElements(previews, idsToKeep); diff --git a/ui/widgets/messageline/messagedelegate.h b/ui/widgets/messageline/messagedelegate.h index 5c2989d..7403285 100644 --- a/ui/widgets/messageline/messagedelegate.h +++ b/ui/widgets/messageline/messagedelegate.h @@ -68,6 +68,7 @@ protected: QPushButton* getButton(const Models::FeedItem& data) const; QProgressBar* getBar(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; void clearHelperWidget(const Models::FeedItem& data) const; @@ -93,6 +94,7 @@ private: std::map* buttons; std::map* bars; std::map* statusIcons; + std::map* pencilIcons; std::map* bodies; std::map* previews; std::set* idsToKeep; diff --git a/ui/widgets/messageline/messagefeed.cpp b/ui/widgets/messageline/messagefeed.cpp index 9537ea5..733cf1d 100644 --- a/ui/widgets/messageline/messagefeed.cpp +++ b/ui/widgets/messageline/messagefeed.cpp @@ -262,7 +262,7 @@ QVariant Models::MessageFeed::data(const QModelIndex& index, int role) const answer = static_cast(msg->getState()); break; case Correction: - answer = msg->getEdited(); + answer.setValue(fillCorrection(*msg));; break; case SentByMe: answer = sentByMe(*msg); @@ -311,7 +311,7 @@ QVariant Models::MessageFeed::data(const QModelIndex& index, int role) const item.date = msg->getTime(); item.state = msg->getState(); item.error = msg->getErrorText(); - item.correction = msg->getEdited(); + item.correction = fillCorrection(*msg); QString body = msg->getBody(); if (body != msg->getOutOfBandUrl()) { @@ -480,6 +480,14 @@ Models::Attachment Models::MessageFeed::fillAttach(const Shared::Message& msg) c 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) { bool notify = false; diff --git a/ui/widgets/messageline/messagefeed.h b/ui/widgets/messageline/messagefeed.h index b368a3d..2273b15 100644 --- a/ui/widgets/messageline/messagefeed.h +++ b/ui/widgets/messageline/messagefeed.h @@ -37,6 +37,7 @@ namespace Models { class Element; struct Attachment; + struct Edition; class MessageFeed : public QAbstractListModel { @@ -106,6 +107,7 @@ public: protected: bool sentByMe(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 modelIndexByTime(const QString& id, const QDateTime& time) const; std::set detectChanges(const Shared::Message& msg, const QMap& data) const; @@ -180,6 +182,12 @@ struct Attachment { QString error; }; +struct Edition { + bool corrected; + QString original; + QDateTime lastCorrection; +}; + struct FeedItem { QString id; QString text; @@ -187,7 +195,7 @@ struct FeedItem { QString avatar; QString error; bool sentByMe; - bool correction; + Edition correction; QDateTime date; Shared::Message::State state; Attachment attach; @@ -195,6 +203,7 @@ struct FeedItem { }; Q_DECLARE_METATYPE(Models::Attachment); +Q_DECLARE_METATYPE(Models::Edition); Q_DECLARE_METATYPE(Models::FeedItem); #endif // MESSAGEFEED_H