2020-08-20 21:32:30 +00:00
|
|
|
/*
|
|
|
|
* Squawk messenger.
|
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-01-07 21:50:12 +00:00
|
|
|
#include <QDebug>
|
2020-08-20 21:32:30 +00:00
|
|
|
#include <QPainter>
|
2022-01-08 22:28:29 +00:00
|
|
|
#include <QPainterPath>
|
2020-08-20 21:32:30 +00:00
|
|
|
#include <QApplication>
|
2021-02-01 22:55:15 +00:00
|
|
|
#include <QMouseEvent>
|
2022-01-07 14:02:49 +00:00
|
|
|
#include <QAbstractItemView>
|
2022-04-27 21:08:59 +00:00
|
|
|
#include <QAbstractTextDocumentLayout>
|
|
|
|
#include <QTextBlock>
|
|
|
|
#include <cmath>
|
2021-01-07 21:50:12 +00:00
|
|
|
|
2020-08-20 21:32:30 +00:00
|
|
|
#include "messagedelegate.h"
|
2021-05-15 22:07:49 +00:00
|
|
|
#include "messagefeed.h"
|
2020-08-20 21:32:30 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
#include "shared/defines.h"
|
|
|
|
|
2021-04-22 22:41:32 +00:00
|
|
|
constexpr int textMargin = 2;
|
|
|
|
constexpr int statusIconSize = 16;
|
2022-01-08 22:28:29 +00:00
|
|
|
|
|
|
|
constexpr int bubbleMargin = 6;
|
|
|
|
constexpr int bubbleBorderRadius = 3;
|
2020-08-20 21:32:30 +00:00
|
|
|
|
2023-01-11 20:45:38 +00:00
|
|
|
int MessageDelegate::avatarHeight(50);
|
|
|
|
int MessageDelegate::margin(6);
|
|
|
|
|
2020-08-20 21:32:30 +00:00
|
|
|
MessageDelegate::MessageDelegate(QObject* parent):
|
2021-05-03 11:23:41 +00:00
|
|
|
QStyledItemDelegate(parent),
|
2023-01-12 17:56:01 +00:00
|
|
|
bodyFont(Shared::Global::getInstance()->defaultFont),
|
|
|
|
nickFont(Shared::Global::getInstance()->headerFont),
|
|
|
|
dateFont(Shared::Global::getInstance()->smallFont),
|
|
|
|
nickMetrics(Shared::Global::getInstance()->headerFontMetrics),
|
|
|
|
dateMetrics(Shared::Global::getInstance()->smallFontMetrics),
|
2023-11-06 23:57:08 +00:00
|
|
|
bodyRenderer(),
|
2021-05-03 11:23:41 +00:00
|
|
|
buttonHeight(0),
|
2022-01-09 14:32:23 +00:00
|
|
|
buttonWidth(0),
|
2021-05-03 11:23:41 +00:00
|
|
|
barHeight(0),
|
2023-11-06 23:57:08 +00:00
|
|
|
buttons(),
|
|
|
|
bars(),
|
|
|
|
statusIcons(),
|
|
|
|
pencilIcons(),
|
|
|
|
encryptionIcons(),
|
|
|
|
previews(),
|
|
|
|
idsToKeep(),
|
2022-05-01 20:19:52 +00:00
|
|
|
clearingWidgets(false),
|
|
|
|
currentId(""),
|
|
|
|
selection(0, 0)
|
2020-08-20 21:32:30 +00:00
|
|
|
{
|
2023-11-06 23:57:08 +00:00
|
|
|
bodyRenderer.setDocumentMargin(0);
|
|
|
|
bodyRenderer.setDefaultFont(bodyFont);
|
2022-04-26 22:17:53 +00:00
|
|
|
|
2022-01-09 14:32:23 +00:00
|
|
|
QPushButton btn(QCoreApplication::translate("MessageLine", "Download"));
|
2021-02-01 22:55:15 +00:00
|
|
|
buttonHeight = btn.sizeHint().height();
|
2022-01-09 14:32:23 +00:00
|
|
|
buttonWidth = btn.sizeHint().width();
|
2021-03-22 18:04:26 +00:00
|
|
|
|
|
|
|
QProgressBar bar;
|
|
|
|
barHeight = bar.sizeHint().height();
|
2020-08-20 21:32:30 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
MessageDelegate::~MessageDelegate() {
|
2023-11-06 23:57:08 +00:00
|
|
|
for (const std::pair<const QString, FeedButton*>& pair: buttons)
|
2021-02-01 22:55:15 +00:00
|
|
|
delete pair.second;
|
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
for (const std::pair<const QString, QProgressBar*>& pair: bars)
|
2021-03-22 18:04:26 +00:00
|
|
|
delete pair.second;
|
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
for (const std::pair<const QString, QLabel*>& pair: statusIcons)
|
2021-04-26 16:37:36 +00:00
|
|
|
delete pair.second;
|
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
for (const std::pair<const QString, QLabel*>& pair: pencilIcons)
|
2021-05-24 22:06:05 +00:00
|
|
|
delete pair.second;
|
2023-11-06 23:57:08 +00:00
|
|
|
|
|
|
|
for (const std::pair<const QString, QLabel*>& pair: encryptionIcons)
|
2021-05-15 22:07:49 +00:00
|
|
|
delete pair.second;
|
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
for (const std::pair<const QString, Preview*>& pair: previews)
|
|
|
|
delete pair.second;
|
2020-08-20 21:32:30 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void MessageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
|
2021-01-07 21:50:12 +00:00
|
|
|
QVariant vi = index.data(Models::MessageFeed::Bulk);
|
2023-08-15 15:28:25 +00:00
|
|
|
if (!vi.isValid())
|
2021-01-07 21:50:12 +00:00
|
|
|
return;
|
2023-08-15 15:28:25 +00:00
|
|
|
|
2021-01-07 21:50:12 +00:00
|
|
|
Models::FeedItem data = qvariant_cast<Models::FeedItem>(vi);
|
2020-08-20 21:32:30 +00:00
|
|
|
painter->save();
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing, true);
|
|
|
|
|
2022-01-09 14:32:23 +00:00
|
|
|
paintBubble(data, painter, option);
|
2022-01-07 14:02:49 +00:00
|
|
|
bool ntds = needToDrawSender(index, data);
|
2023-08-15 15:28:25 +00:00
|
|
|
if (ntds || option.rect.y() < 1)
|
2022-01-07 14:02:49 +00:00
|
|
|
paintAvatar(data, index, option, painter);
|
2020-08-20 21:32:30 +00:00
|
|
|
|
|
|
|
QStyleOptionViewItem opt = option;
|
2022-01-09 14:32:23 +00:00
|
|
|
opt.rect = option.rect.adjusted(bubbleMargin, bubbleMargin, -bubbleMargin, -bubbleMargin / 2);
|
2023-08-15 15:28:25 +00:00
|
|
|
if (!data.sentByMe)
|
2020-08-20 21:32:30 +00:00
|
|
|
opt.displayAlignment = Qt::AlignLeft | Qt::AlignTop;
|
2023-08-15 15:28:25 +00:00
|
|
|
else
|
2020-08-20 21:32:30 +00:00
|
|
|
opt.displayAlignment = Qt::AlignRight | Qt::AlignTop;
|
2022-01-08 22:28:29 +00:00
|
|
|
|
2022-01-09 14:32:23 +00:00
|
|
|
QRect rect;
|
2022-01-07 14:02:49 +00:00
|
|
|
if (ntds) {
|
2022-01-09 14:32:23 +00:00
|
|
|
painter->setFont(nickFont);
|
|
|
|
painter->drawText(opt.rect, opt.displayAlignment, data.sender, &rect);
|
2022-01-08 22:28:29 +00:00
|
|
|
opt.rect.adjust(0, nickMetrics.lineSpacing() + textMargin, 0, 0);
|
2022-01-07 14:02:49 +00:00
|
|
|
}
|
2022-01-08 22:28:29 +00:00
|
|
|
|
2022-01-09 14:32:23 +00:00
|
|
|
painter->save();
|
2021-01-14 11:22:02 +00:00
|
|
|
switch (data.attach.state) {
|
|
|
|
case Models::none:
|
2021-03-22 18:04:26 +00:00
|
|
|
clearHelperWidget(data); //i can't imagine the situation where it's gonna be needed
|
|
|
|
break; //but it's a possible performance problem
|
2021-01-14 11:22:02 +00:00
|
|
|
case Models::uploading:
|
2022-01-09 14:32:23 +00:00
|
|
|
paintPreview(data, painter, opt);
|
2022-01-05 19:29:34 +00:00
|
|
|
[[fallthrough]];
|
2021-01-14 11:22:02 +00:00
|
|
|
case Models::downloading:
|
2022-01-09 14:32:23 +00:00
|
|
|
paintBar(getBar(data), painter, data.sentByMe, opt);
|
2021-01-14 11:22:02 +00:00
|
|
|
break;
|
|
|
|
case Models::remote:
|
2022-01-09 14:32:23 +00:00
|
|
|
paintButton(getButton(data), painter, data.sentByMe, opt);
|
2021-01-14 11:22:02 +00:00
|
|
|
break;
|
|
|
|
case Models::ready:
|
2021-05-14 19:49:38 +00:00
|
|
|
case Models::local:
|
2021-04-20 21:56:47 +00:00
|
|
|
clearHelperWidget(data);
|
2022-01-09 14:32:23 +00:00
|
|
|
paintPreview(data, painter, opt);
|
2021-04-20 21:56:47 +00:00
|
|
|
break;
|
2021-05-14 19:49:38 +00:00
|
|
|
case Models::errorDownload: {
|
2022-01-09 14:32:23 +00:00
|
|
|
paintButton(getButton(data), painter, data.sentByMe, opt);
|
|
|
|
paintComment(data, painter, opt);
|
2021-05-14 19:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case Models::errorUpload:{
|
|
|
|
clearHelperWidget(data);
|
2022-01-09 14:32:23 +00:00
|
|
|
paintPreview(data, painter, opt);
|
|
|
|
paintComment(data, painter, opt);
|
2021-05-14 19:49:38 +00:00
|
|
|
}
|
2021-01-14 11:22:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
painter->restore();
|
|
|
|
|
2021-05-03 11:23:41 +00:00
|
|
|
QWidget* vp = static_cast<QWidget*>(painter->device());
|
2022-04-26 22:17:53 +00:00
|
|
|
paintBody(data, painter, opt);
|
2020-08-20 21:32:30 +00:00
|
|
|
painter->setFont(dateFont);
|
|
|
|
QColor q = painter->pen().color();
|
2022-01-09 14:32:23 +00:00
|
|
|
QString dateString = data.date.toLocalTime().toString("hh:mm");
|
2020-08-20 21:32:30 +00:00
|
|
|
q.setAlpha(180);
|
|
|
|
painter->setPen(q);
|
2021-09-21 22:17:43 +00:00
|
|
|
painter->drawText(opt.rect, opt.displayAlignment, dateString, &rect);
|
2021-05-24 22:06:05 +00:00
|
|
|
int currentY = opt.rect.y();
|
2023-11-06 23:57:08 +00:00
|
|
|
int statusOffset = statusIconSize;
|
2021-04-22 22:41:32 +00:00
|
|
|
if (data.sentByMe) {
|
2021-04-26 16:37:36 +00:00
|
|
|
QLabel* statusIcon = getStatusIcon(data);
|
|
|
|
|
|
|
|
statusIcon->setParent(vp);
|
2022-01-09 14:32:23 +00:00
|
|
|
statusIcon->move(opt.rect.left(), currentY);
|
2021-04-26 16:37:36 +00:00
|
|
|
statusIcon->show();
|
2021-05-24 22:06:05 +00:00
|
|
|
|
2021-04-26 16:37:36 +00:00
|
|
|
opt.rect.adjust(0, statusIconSize + textMargin, 0, 0);
|
2023-11-06 23:57:08 +00:00
|
|
|
statusOffset = statusIconSize + margin;
|
2021-04-22 22:41:32 +00:00
|
|
|
}
|
2020-08-20 21:32:30 +00:00
|
|
|
|
2021-05-24 22:06:05 +00:00
|
|
|
if (data.correction.corrected) {
|
|
|
|
QLabel* pencilIcon = getPencilIcon(data);
|
|
|
|
pencilIcon->setParent(vp);
|
2023-08-15 15:28:25 +00:00
|
|
|
if (data.sentByMe)
|
2023-11-06 23:57:08 +00:00
|
|
|
pencilIcon->move(opt.rect.left() + statusOffset, currentY);
|
2023-08-15 15:28:25 +00:00
|
|
|
else
|
2023-11-06 23:57:08 +00:00
|
|
|
pencilIcon->move(opt.rect.right() - statusOffset, currentY);
|
2023-08-15 15:28:25 +00:00
|
|
|
|
2021-05-24 22:06:05 +00:00
|
|
|
pencilIcon->show();
|
2023-11-06 23:57:08 +00:00
|
|
|
statusOffset += statusIconSize + margin;
|
2021-05-24 22:06:05 +00:00
|
|
|
} else {
|
2023-11-06 23:57:08 +00:00
|
|
|
std::map<QString, QLabel*>::const_iterator itr = pencilIcons.find(data.id);
|
|
|
|
if (itr != pencilIcons.end()) {
|
2021-05-24 22:06:05 +00:00
|
|
|
delete itr->second;
|
2023-11-06 23:57:08 +00:00
|
|
|
pencilIcons.erase(itr);
|
2021-05-24 22:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-06 23:57:08 +00:00
|
|
|
|
|
|
|
if (data.encryption != Shared::EncryptionProtocol::none) {
|
|
|
|
QLabel* shieldIcon = getEncryptionIcon(data);
|
|
|
|
shieldIcon->setParent(vp);
|
|
|
|
if (data.sentByMe)
|
|
|
|
shieldIcon->move(opt.rect.left() + statusOffset, currentY);
|
|
|
|
else
|
|
|
|
shieldIcon->move(opt.rect.right() - statusOffset, currentY);
|
|
|
|
|
|
|
|
shieldIcon->show();
|
|
|
|
statusOffset += statusIconSize + margin;
|
|
|
|
}
|
2021-05-24 22:06:05 +00:00
|
|
|
|
2020-08-20 21:32:30 +00:00
|
|
|
painter->restore();
|
2021-02-01 22:55:15 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
if (clearingWidgets)
|
2023-11-06 23:57:08 +00:00
|
|
|
idsToKeep.insert(data.id);
|
2020-08-20 21:32:30 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void MessageDelegate::paintBubble(const Models::FeedItem& data, QPainter* painter, const QStyleOptionViewItem& option) const {
|
2022-01-08 22:28:29 +00:00
|
|
|
painter->save();
|
2023-08-15 15:28:25 +00:00
|
|
|
if (data.sentByMe)
|
2022-01-08 22:28:29 +00:00
|
|
|
painter->setBrush(option.palette.brush(QPalette::Inactive, QPalette::Highlight));
|
2023-08-15 15:28:25 +00:00
|
|
|
else
|
2022-01-08 22:28:29 +00:00
|
|
|
painter->setBrush(option.palette.brush(QPalette::Window));
|
2023-08-15 15:28:25 +00:00
|
|
|
|
2022-01-08 22:28:29 +00:00
|
|
|
painter->setPen(Qt::NoPen);
|
2022-01-09 14:32:23 +00:00
|
|
|
painter->drawRoundedRect(option.rect, bubbleBorderRadius, bubbleBorderRadius);
|
2022-01-08 22:28:29 +00:00
|
|
|
painter->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void MessageDelegate::paintAvatar(const Models::FeedItem& data, const QModelIndex& index, const QStyleOptionViewItem& option, QPainter* painter) const {
|
2022-01-07 14:02:49 +00:00
|
|
|
int currentRow = index.row();
|
|
|
|
int y = option.rect.y();
|
|
|
|
bool firstAttempt = true;
|
|
|
|
QIcon icon(data.avatar);
|
|
|
|
while (y < 0 && currentRow > 0) {
|
|
|
|
QRect rect;
|
|
|
|
if (firstAttempt) {
|
|
|
|
firstAttempt = false;
|
|
|
|
rect = option.rect;
|
|
|
|
} else {
|
|
|
|
QModelIndex ci = index.siblingAtRow(currentRow);
|
|
|
|
if (
|
|
|
|
(ci.data(Models::MessageFeed::Sender).toString() != data.sender) ||
|
|
|
|
(ci.data(Models::MessageFeed::Date).toDateTime().daysTo(data.date) != 0)
|
|
|
|
) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//TODO this is really bad, but for now I have no idea how else can I access the view;
|
|
|
|
const QAbstractItemView* view = static_cast<const QAbstractItemView*>(option.styleObject);
|
|
|
|
rect = view->visualRect(ci);
|
|
|
|
}
|
|
|
|
y = std::min(0, rect.bottom() - margin - avatarHeight);
|
|
|
|
--currentRow;
|
|
|
|
}
|
2022-01-08 22:28:29 +00:00
|
|
|
|
|
|
|
QPixmap pixmap = icon.pixmap(avatarHeight, avatarHeight);
|
|
|
|
QPainterPath path;
|
|
|
|
int ax;
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
if (data.sentByMe)
|
2022-01-09 14:32:23 +00:00
|
|
|
ax = option.rect.x() + option.rect.width() + margin;
|
2023-08-15 15:28:25 +00:00
|
|
|
else
|
2022-01-08 22:28:29 +00:00
|
|
|
ax = margin;
|
|
|
|
|
|
|
|
path.addEllipse(ax, y + margin / 2, avatarHeight, avatarHeight);
|
|
|
|
painter->save();
|
|
|
|
painter->setClipPath(path);
|
|
|
|
painter->drawPixmap(ax, y + margin / 2, pixmap);
|
|
|
|
painter->restore();
|
2022-01-07 14:02:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
bool MessageDelegate::needToDrawAvatar(const QModelIndex& index, const Models::FeedItem& data, const QStyleOptionViewItem& option) const {
|
2022-01-07 14:02:49 +00:00
|
|
|
return (option.rect.y() < 1) || needToDrawSender(index, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MessageDelegate::needToDrawSender(const QModelIndex& index, const Models::FeedItem& data) const
|
|
|
|
{
|
|
|
|
if (index.row() == index.model()->rowCount() - 1) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
QModelIndex prevIndex = index.siblingAtRow(index.row() + 1);
|
|
|
|
|
|
|
|
return (prevIndex.data(Models::MessageFeed::Sender).toString() != data.sender) ||
|
|
|
|
(prevIndex.data(Models::MessageFeed::Date).toDateTime().daysTo(data.date) != 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
QSize MessageDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const {
|
2022-01-08 22:28:29 +00:00
|
|
|
QRect messageRect = option.rect.adjusted(bubbleMargin, margin / 2 + bubbleMargin, -(avatarHeight + 3 * margin + bubbleMargin), -(margin + bubbleMargin) / 2);
|
2020-08-20 21:32:30 +00:00
|
|
|
QStyleOptionViewItem opt = option;
|
|
|
|
opt.rect = messageRect;
|
2022-01-07 14:02:49 +00:00
|
|
|
QVariant vi = index.data(Models::MessageFeed::Bulk);
|
|
|
|
Models::FeedItem data = qvariant_cast<Models::FeedItem>(vi);
|
2021-02-01 22:55:15 +00:00
|
|
|
QSize messageSize(0, 0);
|
2022-01-07 14:02:49 +00:00
|
|
|
if (data.text.size() > 0) {
|
2023-11-06 23:57:08 +00:00
|
|
|
bodyRenderer.setPlainText(data.text);
|
|
|
|
bodyRenderer.setTextWidth(messageRect.size().width());
|
2022-04-26 22:17:53 +00:00
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
QSizeF size = bodyRenderer.size();
|
|
|
|
size.setWidth(bodyRenderer.idealWidth());
|
2022-04-27 21:08:59 +00:00
|
|
|
messageSize = QSize(std::ceil(size.width()), std::ceil(size.height()));
|
2021-04-22 22:41:32 +00:00
|
|
|
messageSize.rheight() += textMargin;
|
2021-02-01 22:55:15 +00:00
|
|
|
}
|
2020-08-20 21:32:30 +00:00
|
|
|
|
2022-01-07 14:02:49 +00:00
|
|
|
switch (data.attach.state) {
|
2021-01-14 11:22:02 +00:00
|
|
|
case Models::none:
|
|
|
|
break;
|
|
|
|
case Models::uploading:
|
2022-02-18 21:27:09 +00:00
|
|
|
messageSize.rheight() += Preview::calculateAttachSize(Shared::resolvePath(data.attach.localPath), messageRect).height() + textMargin;
|
2022-01-05 19:29:34 +00:00
|
|
|
[[fallthrough]];
|
2021-01-14 11:22:02 +00:00
|
|
|
case Models::downloading:
|
2021-04-22 22:41:32 +00:00
|
|
|
messageSize.rheight() += barHeight + textMargin;
|
2022-01-09 14:32:23 +00:00
|
|
|
messageSize.setWidth(messageRect.width());
|
2021-01-14 11:22:02 +00:00
|
|
|
break;
|
|
|
|
case Models::remote:
|
2021-04-22 22:41:32 +00:00
|
|
|
messageSize.rheight() += buttonHeight + textMargin;
|
2022-01-09 14:32:23 +00:00
|
|
|
messageSize.setWidth(std::max(messageSize.width(), buttonWidth));
|
2021-01-14 11:22:02 +00:00
|
|
|
break;
|
|
|
|
case Models::ready:
|
2022-01-09 14:32:23 +00:00
|
|
|
case Models::local: {
|
2022-02-18 21:27:09 +00:00
|
|
|
QSize aSize = Preview::calculateAttachSize(Shared::resolvePath(data.attach.localPath), messageRect);
|
2022-01-09 14:32:23 +00:00
|
|
|
messageSize.rheight() += aSize.height() + textMargin;
|
|
|
|
messageSize.setWidth(std::max(messageSize.width(), aSize.width()));
|
|
|
|
}
|
2021-01-14 11:22:02 +00:00
|
|
|
break;
|
2022-01-09 14:32:23 +00:00
|
|
|
case Models::errorDownload: {
|
|
|
|
QSize commentSize = dateMetrics.boundingRect(messageRect, Qt::TextWordWrap, data.attach.error).size();
|
|
|
|
messageSize.rheight() += commentSize.height() + buttonHeight + textMargin * 2;
|
|
|
|
messageSize.setWidth(std::max(messageSize.width(), std::max(commentSize.width(), buttonWidth)));
|
|
|
|
}
|
2021-05-14 19:49:38 +00:00
|
|
|
break;
|
2022-01-09 14:32:23 +00:00
|
|
|
case Models::errorUpload: {
|
2022-02-18 21:27:09 +00:00
|
|
|
QSize aSize = Preview::calculateAttachSize(Shared::resolvePath(data.attach.localPath), messageRect);
|
2022-01-09 14:32:23 +00:00
|
|
|
QSize commentSize = dateMetrics.boundingRect(messageRect, Qt::TextWordWrap, data.attach.error).size();
|
|
|
|
messageSize.rheight() += aSize.height() + commentSize.height() + textMargin * 2;
|
|
|
|
messageSize.setWidth(std::max(messageSize.width(), std::max(commentSize.width(), aSize.width())));
|
|
|
|
}
|
2021-04-19 21:49:24 +00:00
|
|
|
break;
|
2021-01-14 11:22:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 14:02:49 +00:00
|
|
|
if (needToDrawSender(index, data)) {
|
2022-01-09 14:32:23 +00:00
|
|
|
QSize senderSize = nickMetrics.boundingRect(messageRect, 0, data.sender).size();
|
|
|
|
messageSize.rheight() += senderSize.height() + textMargin;
|
|
|
|
messageSize.setWidth(std::max(senderSize.width(), messageSize.width()));
|
2022-01-07 14:02:49 +00:00
|
|
|
}
|
|
|
|
|
2022-01-09 14:32:23 +00:00
|
|
|
QString dateString = data.date.toLocalTime().toString("hh:mm");
|
|
|
|
QSize dateSize = dateMetrics.boundingRect(messageRect, 0, dateString).size();
|
|
|
|
messageSize.rheight() += bubbleMargin * 1.5;
|
|
|
|
messageSize.rheight() += dateSize.height() > statusIconSize ? dateSize.height() : statusIconSize;
|
|
|
|
|
|
|
|
int statusWidth = dateSize.width() + statusIconSize + margin;
|
2023-11-06 23:57:08 +00:00
|
|
|
if (data.correction.corrected)
|
2022-01-09 14:32:23 +00:00
|
|
|
statusWidth += statusIconSize + margin;
|
2023-11-06 23:57:08 +00:00
|
|
|
|
|
|
|
if (data.encryption != Shared::EncryptionProtocol::none)
|
|
|
|
statusWidth += statusIconSize + margin;
|
|
|
|
|
2022-01-09 14:32:23 +00:00
|
|
|
messageSize.setWidth(std::max(statusWidth, messageSize.width()));
|
|
|
|
messageSize.rwidth() += 2 * bubbleMargin;
|
2020-08-20 21:32:30 +00:00
|
|
|
|
|
|
|
return messageSize;
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
QRect MessageDelegate::getHoveredMessageBodyRect(const QModelIndex& index, const Models::FeedItem& data, const QRect& sizeHint) const {
|
2022-04-28 21:29:44 +00:00
|
|
|
QRect localHint = sizeHint.adjusted(bubbleMargin, bubbleMargin + margin, -bubbleMargin, -bubbleMargin / 2);
|
2023-08-15 15:28:25 +00:00
|
|
|
if (needToDrawSender(index, data))
|
2022-04-28 21:29:44 +00:00
|
|
|
localHint.adjust(0, nickMetrics.lineSpacing() + textMargin, 0, 0);
|
2022-04-27 21:08:59 +00:00
|
|
|
|
2022-04-28 21:29:44 +00:00
|
|
|
int attachHeight = 0;
|
|
|
|
switch (data.attach.state) {
|
|
|
|
case Models::none:
|
|
|
|
break;
|
|
|
|
case Models::uploading:
|
|
|
|
attachHeight += Preview::calculateAttachSize(Shared::resolvePath(data.attach.localPath), localHint).height() + textMargin;
|
|
|
|
[[fallthrough]];
|
|
|
|
case Models::downloading:
|
|
|
|
attachHeight += barHeight + textMargin;
|
|
|
|
break;
|
|
|
|
case Models::remote:
|
|
|
|
attachHeight += buttonHeight + textMargin;
|
|
|
|
break;
|
|
|
|
case Models::ready:
|
|
|
|
case Models::local: {
|
|
|
|
QSize aSize = Preview::calculateAttachSize(Shared::resolvePath(data.attach.localPath), localHint);
|
|
|
|
attachHeight += aSize.height() + textMargin;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Models::errorDownload: {
|
|
|
|
QSize commentSize = dateMetrics.boundingRect(localHint, Qt::TextWordWrap, data.attach.error).size();
|
|
|
|
attachHeight += commentSize.height() + buttonHeight + textMargin * 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Models::errorUpload: {
|
|
|
|
QSize aSize = Preview::calculateAttachSize(Shared::resolvePath(data.attach.localPath), localHint);
|
|
|
|
QSize commentSize = dateMetrics.boundingRect(localHint, Qt::TextWordWrap, data.attach.error).size();
|
|
|
|
attachHeight += aSize.height() + commentSize.height() + textMargin * 2;
|
2022-04-27 21:08:59 +00:00
|
|
|
}
|
2022-04-28 21:29:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bottomSize = std::max(dateMetrics.lineSpacing(), statusIconSize);
|
|
|
|
localHint.adjust(0, attachHeight, 0, -(bottomSize + textMargin));
|
|
|
|
|
|
|
|
return localHint;
|
|
|
|
}
|
2022-04-27 21:08:59 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
QString MessageDelegate::getAnchor(const QPoint& point, const QModelIndex& index, const QRect& sizeHint) const {
|
2022-04-28 21:29:44 +00:00
|
|
|
QVariant vi = index.data(Models::MessageFeed::Bulk);
|
|
|
|
Models::FeedItem data = qvariant_cast<Models::FeedItem>(vi);
|
|
|
|
if (data.text.size() > 0) {
|
|
|
|
QRect localHint = getHoveredMessageBodyRect(index, data, sizeHint);
|
2022-04-27 21:08:59 +00:00
|
|
|
|
|
|
|
if (localHint.contains(point)) {
|
|
|
|
QPoint translated = point - localHint.topLeft();
|
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
bodyRenderer.setHtml(Shared::processMessageBody(data.text));
|
|
|
|
bodyRenderer.setTextWidth(localHint.size().width());
|
2022-04-27 21:08:59 +00:00
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
return bodyRenderer.documentLayout()->anchorAt(translated);
|
2022-04-27 21:08:59 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-28 21:29:44 +00:00
|
|
|
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void MessageDelegate::leftClick(const QPoint& point, const QModelIndex& index, const QRect& sizeHint) const {
|
2022-04-28 21:29:44 +00:00
|
|
|
QString anchor = getAnchor(point, index, sizeHint);
|
2023-08-15 15:28:25 +00:00
|
|
|
if (anchor.size() > 0)
|
2022-04-28 21:29:44 +00:00
|
|
|
emit openLink(anchor);
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
QString MessageDelegate::leftDoubleClick(const QPoint& point, const QModelIndex& index, const QRect& sizeHint) {
|
2022-05-03 09:17:08 +00:00
|
|
|
QVariant vi = index.data(Models::MessageFeed::Bulk);
|
|
|
|
Models::FeedItem data = qvariant_cast<Models::FeedItem>(vi);
|
|
|
|
if (data.text.size() > 0) {
|
|
|
|
QRect localHint = getHoveredMessageBodyRect(index, data, sizeHint);
|
|
|
|
|
|
|
|
if (localHint.contains(point)) {
|
|
|
|
QPoint translated = point - localHint.topLeft();
|
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
bodyRenderer.setHtml(Shared::processMessageBody(data.text));
|
|
|
|
bodyRenderer.setTextWidth(localHint.size().width());
|
2022-05-03 09:17:08 +00:00
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
QAbstractTextDocumentLayout* lay = bodyRenderer.documentLayout();
|
2022-05-03 09:17:08 +00:00
|
|
|
|
|
|
|
int position = lay->hitTest(translated, Qt::HitTestAccuracy::FuzzyHit);
|
2023-11-06 23:57:08 +00:00
|
|
|
QTextCursor cursor(&bodyRenderer);
|
2022-05-03 09:17:08 +00:00
|
|
|
cursor.setPosition(position, QTextCursor::MoveAnchor);
|
|
|
|
cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
|
|
|
|
cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
|
|
|
|
|
|
|
|
selection.first = cursor.anchor();
|
|
|
|
selection.second = cursor.position();
|
|
|
|
currentId = data.id;
|
|
|
|
|
|
|
|
if (selection.first != selection.second) {
|
|
|
|
return cursor.selectedText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
Shared::Hover MessageDelegate::hoverType(const QPoint& point, const QModelIndex& index, const QRect& sizeHint) const {
|
2022-05-02 19:25:50 +00:00
|
|
|
QVariant vi = index.data(Models::MessageFeed::Bulk);
|
|
|
|
Models::FeedItem data = qvariant_cast<Models::FeedItem>(vi);
|
|
|
|
if (data.text.size() > 0) {
|
|
|
|
QRect localHint = getHoveredMessageBodyRect(index, data, sizeHint);
|
|
|
|
|
|
|
|
if (localHint.contains(point)) {
|
|
|
|
QPoint translated = point - localHint.topLeft();
|
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
bodyRenderer.setHtml(Shared::processMessageBody(data.text));
|
|
|
|
bodyRenderer.setTextWidth(localHint.size().width());
|
2022-05-02 19:25:50 +00:00
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
QAbstractTextDocumentLayout* lay = bodyRenderer.documentLayout();
|
2022-05-02 19:25:50 +00:00
|
|
|
QString anchor = lay->anchorAt(translated);
|
|
|
|
|
|
|
|
if (anchor.size() > 0) {
|
|
|
|
return Shared::Hover::anchor;
|
|
|
|
} else {
|
|
|
|
int position = lay->hitTest(translated, Qt::HitTestAccuracy::ExactHit);
|
2023-08-15 15:28:25 +00:00
|
|
|
if (position != -1)
|
2022-05-03 09:17:08 +00:00
|
|
|
return Shared::Hover::text;
|
2022-05-02 19:25:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Shared::Hover::nothing;
|
2022-04-27 21:08:59 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
QString MessageDelegate::mouseDrag(const QPoint& start, const QPoint& end, const QModelIndex& index, const QRect& sizeHint) {
|
2022-05-01 20:19:52 +00:00
|
|
|
QVariant vi = index.data(Models::MessageFeed::Bulk);
|
|
|
|
Models::FeedItem data = qvariant_cast<Models::FeedItem>(vi);
|
|
|
|
if (data.text.size() > 0) {
|
|
|
|
QRect localHint = getHoveredMessageBodyRect(index, data, sizeHint);
|
|
|
|
|
|
|
|
if (localHint.contains(start)) {
|
2022-05-02 19:25:50 +00:00
|
|
|
QPoint tl = localHint.topLeft();
|
|
|
|
QPoint first = start - tl;
|
|
|
|
QPoint last = end - tl;
|
|
|
|
last.setX(std::max(last.x(), 0));
|
|
|
|
last.setX(std::min(last.x(), localHint.width() - 1));
|
|
|
|
last.setY(std::max(last.y(), 0));
|
|
|
|
last.setY(std::min(last.y(), localHint.height()));
|
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
bodyRenderer.setHtml(Shared::processMessageBody(data.text));
|
|
|
|
bodyRenderer.setTextWidth(localHint.size().width());
|
|
|
|
selection.first = bodyRenderer.documentLayout()->hitTest(first, Qt::HitTestAccuracy::FuzzyHit);
|
|
|
|
selection.second = bodyRenderer.documentLayout()->hitTest(last, Qt::HitTestAccuracy::FuzzyHit);
|
2022-05-01 20:19:52 +00:00
|
|
|
|
|
|
|
currentId = data.id;
|
|
|
|
|
2022-05-02 19:25:50 +00:00
|
|
|
if (selection.first != selection.second) {
|
2023-11-06 23:57:08 +00:00
|
|
|
QTextCursor cursor(&bodyRenderer);
|
2022-05-02 19:25:50 +00:00
|
|
|
cursor.setPosition(selection.first, QTextCursor::MoveAnchor);
|
|
|
|
cursor.setPosition(selection.second, QTextCursor::KeepAnchor);
|
|
|
|
return cursor.selectedText();
|
|
|
|
}
|
2022-05-01 20:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-02 19:25:50 +00:00
|
|
|
return "";
|
2022-05-01 20:19:52 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
QString MessageDelegate::clearSelection() {
|
2022-05-01 20:19:52 +00:00
|
|
|
QString lastSelectedId = currentId;
|
|
|
|
currentId = "";
|
|
|
|
selection = std::pair(0, 0);
|
|
|
|
return lastSelectedId;
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
bool MessageDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) {
|
2021-01-07 21:50:12 +00:00
|
|
|
//qDebug() << event->type();
|
|
|
|
return QStyledItemDelegate::editorEvent(event, model, option, index);
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
int MessageDelegate::paintButton(QPushButton* btn, QPainter* painter, bool sentByMe, QStyleOptionViewItem& option) const {
|
2021-02-01 22:55:15 +00:00
|
|
|
QPoint start;
|
2023-08-15 15:28:25 +00:00
|
|
|
if (sentByMe)
|
2022-01-08 22:28:29 +00:00
|
|
|
start = {option.rect.x() + option.rect.width() - btn->width(), option.rect.top()};
|
2023-08-15 15:28:25 +00:00
|
|
|
else
|
2021-02-01 22:55:15 +00:00
|
|
|
start = option.rect.topLeft();
|
|
|
|
|
|
|
|
QWidget* vp = static_cast<QWidget*>(painter->device());
|
|
|
|
btn->setParent(vp);
|
|
|
|
btn->move(start);
|
|
|
|
btn->show();
|
|
|
|
|
2021-04-22 22:41:32 +00:00
|
|
|
option.rect.adjust(0, buttonHeight + textMargin, 0, 0);
|
2022-01-08 22:28:29 +00:00
|
|
|
return btn->width();
|
2021-02-01 22:55:15 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
int MessageDelegate::paintComment(const Models::FeedItem& data, QPainter* painter, QStyleOptionViewItem& option) const {
|
2021-05-15 22:07:49 +00:00
|
|
|
painter->setFont(dateFont);
|
|
|
|
QColor q = painter->pen().color();
|
|
|
|
q.setAlpha(180);
|
|
|
|
painter->setPen(q);
|
|
|
|
QRect rect;
|
|
|
|
painter->drawText(option.rect, option.displayAlignment, data.attach.error, &rect);
|
|
|
|
option.rect.adjust(0, rect.height() + textMargin, 0, 0);
|
2022-01-08 22:28:29 +00:00
|
|
|
|
|
|
|
return rect.width();
|
2021-05-15 22:07:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
int MessageDelegate::paintBar(QProgressBar* bar, QPainter* painter, bool sentByMe, QStyleOptionViewItem& option) const {
|
|
|
|
SHARED_UNUSED(sentByMe);
|
2021-03-22 18:04:26 +00:00
|
|
|
QPoint start = option.rect.topLeft();
|
2021-05-03 11:23:41 +00:00
|
|
|
bar->resize(option.rect.width(), barHeight);
|
2021-04-20 21:56:47 +00:00
|
|
|
|
|
|
|
painter->translate(start);
|
|
|
|
bar->render(painter, QPoint(), QRegion(), QWidget::DrawChildren);
|
2021-03-22 18:04:26 +00:00
|
|
|
|
2021-04-22 22:41:32 +00:00
|
|
|
option.rect.adjust(0, barHeight + textMargin, 0, 0);
|
2022-01-08 22:28:29 +00:00
|
|
|
|
|
|
|
return option.rect.width();
|
2021-03-22 18:04:26 +00:00
|
|
|
}
|
2021-02-01 22:55:15 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
int MessageDelegate::paintPreview(const Models::FeedItem& data, QPainter* painter, QStyleOptionViewItem& option) const {
|
2021-05-15 22:07:49 +00:00
|
|
|
Preview* preview = 0;
|
2023-11-06 23:57:08 +00:00
|
|
|
std::map<QString, Preview*>::iterator itr = previews.find(data.id);
|
2021-05-15 22:07:49 +00:00
|
|
|
|
|
|
|
QSize size = option.rect.size();
|
2022-02-18 21:27:09 +00:00
|
|
|
QString path = Shared::resolvePath(data.attach.localPath);
|
2023-11-06 23:57:08 +00:00
|
|
|
if (itr != previews.end()) {
|
2021-05-15 22:07:49 +00:00
|
|
|
preview = itr->second;
|
2022-02-18 21:27:09 +00:00
|
|
|
preview->actualize(path, size, option.rect.topLeft());
|
2021-05-10 21:06:40 +00:00
|
|
|
} else {
|
2021-05-15 22:07:49 +00:00
|
|
|
QWidget* vp = static_cast<QWidget*>(painter->device());
|
2022-02-18 21:27:09 +00:00
|
|
|
preview = new Preview(path, size, option.rect.topLeft(), vp);
|
2023-11-06 23:57:08 +00:00
|
|
|
previews.insert(std::make_pair(data.id, preview));
|
2021-04-20 21:56:47 +00:00
|
|
|
}
|
2021-05-10 21:06:40 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
if (!preview->isFileReachable()) //this is the situation when the file preview couldn't be painted because the file was moved
|
2021-05-16 21:52:59 +00:00
|
|
|
emit invalidPath(data.id); //or deleted. This signal notifies the model, and the model notifies the core, preview can
|
2023-08-15 15:28:25 +00:00
|
|
|
//handle being invalid for as long as I need and can be even become valid again with a new path
|
2022-01-08 22:28:29 +00:00
|
|
|
QSize pSize(preview->size());
|
|
|
|
option.rect.adjust(0, pSize.height() + textMargin, 0, 0);
|
|
|
|
|
|
|
|
return pSize.width();
|
2021-04-20 21:56:47 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
QPushButton * MessageDelegate::getButton(const Models::FeedItem& data) const {
|
2023-11-06 23:57:08 +00:00
|
|
|
std::map<QString, FeedButton*>::const_iterator itr = buttons.find(data.id);
|
2021-02-01 22:55:15 +00:00
|
|
|
FeedButton* result = 0;
|
2023-11-06 23:57:08 +00:00
|
|
|
if (itr != buttons.end()) {
|
2021-05-14 19:49:38 +00:00
|
|
|
result = itr->second;
|
2021-03-22 18:04:26 +00:00
|
|
|
} else {
|
2023-11-06 23:57:08 +00:00
|
|
|
std::map<QString, QProgressBar*>::const_iterator barItr = bars.find(data.id);
|
|
|
|
if (barItr != bars.end()) {
|
2021-03-22 18:04:26 +00:00
|
|
|
delete barItr->second;
|
2023-11-06 23:57:08 +00:00
|
|
|
bars.erase(barItr);
|
2021-03-22 18:04:26 +00:00
|
|
|
}
|
2021-02-01 22:55:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result == 0) {
|
|
|
|
result = new FeedButton();
|
|
|
|
result->messageId = data.id;
|
2021-05-14 19:49:38 +00:00
|
|
|
result->setText(QCoreApplication::translate("MessageLine", "Download"));
|
2023-11-06 23:57:08 +00:00
|
|
|
buttons.insert(std::make_pair(data.id, result));
|
2021-02-06 11:02:42 +00:00
|
|
|
connect(result, &QPushButton::clicked, this, &MessageDelegate::onButtonPushed);
|
2021-02-01 22:55:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
QProgressBar * MessageDelegate::getBar(const Models::FeedItem& data) const {
|
2023-11-06 23:57:08 +00:00
|
|
|
std::map<QString, QProgressBar*>::const_iterator barItr = bars.find(data.id);
|
2021-03-22 18:04:26 +00:00
|
|
|
QProgressBar* result = 0;
|
2023-11-06 23:57:08 +00:00
|
|
|
if (barItr != bars.end()) {
|
2021-03-22 18:04:26 +00:00
|
|
|
result = barItr->second;
|
|
|
|
} else {
|
2023-11-06 23:57:08 +00:00
|
|
|
std::map<QString, FeedButton*>::const_iterator itr = buttons.find(data.id);
|
|
|
|
if (itr != buttons.end()) {
|
2021-03-22 18:04:26 +00:00
|
|
|
delete itr->second;
|
2023-11-06 23:57:08 +00:00
|
|
|
buttons.erase(itr);
|
2021-03-22 18:04:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == 0) {
|
|
|
|
result = new QProgressBar();
|
2021-04-19 21:49:24 +00:00
|
|
|
result->setRange(0, 100);
|
2023-11-06 23:57:08 +00:00
|
|
|
bars.insert(std::make_pair(data.id, result));
|
2021-03-22 18:04:26 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 21:49:24 +00:00
|
|
|
result->setValue(data.attach.progress * 100);
|
2021-03-22 18:04:26 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
QLabel * MessageDelegate::getStatusIcon(const Models::FeedItem& data) const {
|
2023-11-06 23:57:08 +00:00
|
|
|
std::map<QString, QLabel*>::const_iterator itr = statusIcons.find(data.id);
|
2021-04-26 16:37:36 +00:00
|
|
|
QLabel* result = 0;
|
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
if (itr != statusIcons.end()) {
|
2021-05-15 22:07:49 +00:00
|
|
|
result = itr->second;
|
|
|
|
} else {
|
|
|
|
result = new QLabel();
|
2023-11-06 23:57:08 +00:00
|
|
|
statusIcons.insert(std::make_pair(data.id, result));
|
2021-05-15 22:07:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 16:37:36 +00:00
|
|
|
QIcon q(Shared::icon(Shared::messageStateThemeIcons[static_cast<uint8_t>(data.state)]));
|
|
|
|
QString tt = Shared::Global::getName(data.state);
|
|
|
|
if (data.state == Shared::Message::State::error) {
|
2023-08-15 15:28:25 +00:00
|
|
|
if (data.error > 0)
|
2021-04-26 16:37:36 +00:00
|
|
|
tt += ": " + data.error;
|
|
|
|
}
|
2021-05-15 22:07:49 +00:00
|
|
|
if (result->toolTip() != tt) { //If i just assign pixmap every time unconditionally
|
|
|
|
result->setPixmap(q.pixmap(statusIconSize)); //it invokes an infinite cycle of repaint
|
|
|
|
result->setToolTip(tt); //may be it's better to subclass and store last condition in int?
|
2021-05-07 18:26:02 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 16:37:36 +00:00
|
|
|
return result;
|
|
|
|
}
|
2021-02-01 22:55:15 +00:00
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
QLabel* MessageDelegate::getPencilIcon(const Models::FeedItem& data) const {
|
|
|
|
std::map<QString, QLabel*>::const_iterator itr = pencilIcons.find(data.id);
|
2021-05-24 22:06:05 +00:00
|
|
|
QLabel* result = 0;
|
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
if (itr != pencilIcons.end()) {
|
2021-05-24 22:06:05 +00:00
|
|
|
result = itr->second;
|
|
|
|
} else {
|
|
|
|
result = new QLabel();
|
|
|
|
QIcon icon = Shared::icon("edit-rename");
|
|
|
|
result->setPixmap(icon.pixmap(statusIconSize));
|
2023-11-06 23:57:08 +00:00
|
|
|
pencilIcons.insert(std::make_pair(data.id, result));
|
2021-05-24 22:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result->setToolTip("Last time edited: " + data.correction.lastCorrection.toLocalTime().toString()
|
|
|
|
+ "\nOriginal message: " + data.correction.original);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
QLabel* MessageDelegate::getEncryptionIcon(const Models::FeedItem& data) const {
|
|
|
|
std::map<QString, QLabel*>::const_iterator itr = encryptionIcons.find(data.id);
|
|
|
|
QLabel* result = 0;
|
|
|
|
|
|
|
|
if (itr != encryptionIcons.end()) {
|
|
|
|
result = itr->second;
|
|
|
|
} else {
|
|
|
|
result = new QLabel();
|
|
|
|
QIcon icon = Shared::icon("secure");
|
|
|
|
result->setPixmap(icon.pixmap(statusIconSize));
|
|
|
|
encryptionIcons.insert(std::make_pair(data.id, result));
|
|
|
|
result->setToolTip("Encrypted: " + Shared::Global::getName(data.encryption));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-15 22:07:49 +00:00
|
|
|
template <typename T>
|
2023-11-06 23:57:08 +00:00
|
|
|
void removeElements(std::map<QString, T*>& elements, std::set<QString>& idsToKeep) {
|
2021-05-15 22:07:49 +00:00
|
|
|
std::set<QString> toRemove;
|
2023-11-06 23:57:08 +00:00
|
|
|
for (const std::pair<const QString, T*>& pair: elements) {
|
|
|
|
if (idsToKeep.find(pair.first) == idsToKeep.end()) {
|
2021-05-15 22:07:49 +00:00
|
|
|
delete pair.second;
|
|
|
|
toRemove.insert(pair.first);
|
|
|
|
}
|
|
|
|
}
|
2023-11-06 23:57:08 +00:00
|
|
|
for (const QString& key : toRemove)
|
|
|
|
elements.erase(key);
|
2021-05-15 22:07:49 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 22:17:53 +00:00
|
|
|
int MessageDelegate::paintBody(const Models::FeedItem& data, QPainter* painter, QStyleOptionViewItem& option) const
|
|
|
|
{
|
|
|
|
if (data.text.size() > 0) {
|
2023-11-06 23:57:08 +00:00
|
|
|
bodyRenderer.setHtml(Shared::processMessageBody(data.text));
|
|
|
|
bodyRenderer.setTextWidth(option.rect.size().width());
|
2022-04-26 22:17:53 +00:00
|
|
|
painter->save();
|
|
|
|
painter->translate(option.rect.topLeft());
|
2022-05-01 20:19:52 +00:00
|
|
|
|
|
|
|
if (data.id == currentId) {
|
2023-11-06 23:57:08 +00:00
|
|
|
QTextCursor cursor(&bodyRenderer);
|
2022-05-01 20:19:52 +00:00
|
|
|
cursor.setPosition(selection.first, QTextCursor::MoveAnchor);
|
|
|
|
cursor.setPosition(selection.second, QTextCursor::KeepAnchor);
|
|
|
|
QTextCharFormat format = cursor.charFormat();
|
|
|
|
format.setBackground(option.palette.color(QPalette::Active, QPalette::Highlight));
|
|
|
|
format.setForeground(option.palette.color(QPalette::Active, QPalette::HighlightedText));
|
|
|
|
cursor.setCharFormat(format);
|
|
|
|
}
|
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
bodyRenderer.drawContents(painter);
|
2022-05-01 20:19:52 +00:00
|
|
|
|
2022-04-26 22:17:53 +00:00
|
|
|
painter->restore();
|
2023-11-06 23:57:08 +00:00
|
|
|
QSize bodySize(std::ceil(bodyRenderer.idealWidth()), std::ceil(bodyRenderer.size().height()));
|
2022-04-26 22:17:53 +00:00
|
|
|
|
|
|
|
option.rect.adjust(0, bodySize.height() + textMargin, 0, 0);
|
|
|
|
return bodySize.width();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void MessageDelegate::beginClearWidgets() {
|
2023-11-06 23:57:08 +00:00
|
|
|
idsToKeep.clear();
|
2022-04-26 22:17:53 +00:00
|
|
|
clearingWidgets = true;
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void MessageDelegate::endClearWidgets() {
|
2021-02-01 22:55:15 +00:00
|
|
|
if (clearingWidgets) {
|
2021-05-15 22:07:49 +00:00
|
|
|
removeElements(buttons, idsToKeep);
|
|
|
|
removeElements(bars, idsToKeep);
|
|
|
|
removeElements(statusIcons, idsToKeep);
|
2021-05-24 22:06:05 +00:00
|
|
|
removeElements(pencilIcons, idsToKeep);
|
2023-11-06 23:57:08 +00:00
|
|
|
removeElements(encryptionIcons, idsToKeep);
|
2021-05-15 22:07:49 +00:00
|
|
|
removeElements(previews, idsToKeep);
|
2021-02-01 22:55:15 +00:00
|
|
|
|
2023-11-06 23:57:08 +00:00
|
|
|
idsToKeep.clear();
|
2021-02-01 22:55:15 +00:00
|
|
|
clearingWidgets = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void MessageDelegate::onButtonPushed() const {
|
2021-02-06 11:02:42 +00:00
|
|
|
FeedButton* btn = static_cast<FeedButton*>(sender());
|
2021-05-14 19:49:38 +00:00
|
|
|
emit buttonPushed(btn->messageId);
|
2021-02-06 11:02:42 +00:00
|
|
|
}
|
2020-08-20 21:32:30 +00:00
|
|
|
|
2023-08-15 15:28:25 +00:00
|
|
|
void MessageDelegate::clearHelperWidget(const Models::FeedItem& data) const {
|
2023-11-06 23:57:08 +00:00
|
|
|
std::map<QString, FeedButton*>::const_iterator itr = buttons.find(data.id);
|
|
|
|
if (itr != buttons.end()) {
|
2021-03-22 18:04:26 +00:00
|
|
|
delete itr->second;
|
2023-11-06 23:57:08 +00:00
|
|
|
buttons.erase(itr);
|
2021-03-22 18:04:26 +00:00
|
|
|
} else {
|
2023-11-06 23:57:08 +00:00
|
|
|
std::map<QString, QProgressBar*>::const_iterator barItr = bars.find(data.id);
|
|
|
|
if (barItr != bars.end()) {
|
2021-03-22 18:04:26 +00:00
|
|
|
delete barItr->second;
|
2023-11-06 23:57:08 +00:00
|
|
|
bars.erase(barItr);
|
2021-03-22 18:04:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|