1
0
forked from blue/squawk

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*>()),
bars(new std::map<QString, QProgressBar*>()),
statusIcons(new std::map<QString, QLabel*>()),
pencilIcons(new std::map<QString, QLabel*>()),
bodies(new std::map<QString, QLabel*>()),
previews(new std::map<QString, Preview*>()),
idsToKeep(new std::set<QString>()),
@ -68,6 +69,10 @@ MessageDelegate::~MessageDelegate()
delete pair.second;
}
for (const std::pair<const QString, QLabel*>& pair: *pencilIcons){
delete pair.second;
}
for (const std::pair<const QString, QLabel*>& 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<QWidget*>(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<QString, QLabel*>::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<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
{
std::map<QString, QLabel*>::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);

View File

@ -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<QString, FeedButton*>* buttons;
std::map<QString, QProgressBar*>* bars;
std::map<QString, QLabel*>* statusIcons;
std::map<QString, QLabel*>* pencilIcons;
std::map<QString, QLabel*>* bodies;
std::map<QString, Preview*>* previews;
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());
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;

View File

@ -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<MessageRoles> detectChanges(const Shared::Message& msg, const QMap<QString, QVariant>& 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