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
|
|
|
|
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),
|
2022-04-26 22:17:53 +00:00
|
|
|
bodyRenderer(new QTextDocument()),
|
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),
|
|
|
|
buttons(new std::map<QString, FeedButton*>()),
|
|
|
|
bars(new std::map<QString, QProgressBar*>()),
|
|
|
|
statusIcons(new std::map<QString, QLabel*>()),
|
2021-05-24 22:06:05 +00:00
|
|
|
pencilIcons(new std::map<QString, QLabel*>()),
|
2021-05-15 22:07:49 +00:00
|
|
|
previews(new std::map<QString, Preview*>()),
|
2021-05-03 11:23:41 +00:00
|
|
|
idsToKeep(new std::set<QString>()),
|
2022-05-01 20:19:52 +00:00
|
|
|
clearingWidgets(false),
|
|
|
|
currentId(""),
|
|
|
|
selection(0, 0)
|
2020-08-20 21:32:30 +00:00
|
|
|
{
|
2022-04-26 22:17:53 +00:00
|
|
|
bodyRenderer->setDocumentMargin(0);
|
2023-01-11 20:45:38 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
MessageDelegate::~MessageDelegate()
|
|
|
|
{
|
2021-02-01 22:55:15 +00:00
|
|
|
for (const std::pair<const QString, FeedButton*>& pair: *buttons){
|
|
|
|
delete pair.second;
|
|
|
|
}
|
|
|
|
|
2021-03-22 18:04:26 +00:00
|
|
|
for (const std::pair<const QString, QProgressBar*>& pair: *bars){
|
|
|
|
delete pair.second;
|
|
|
|
}
|
|
|
|
|
2021-04-26 16:37:36 +00:00
|
|
|
for (const std::pair<const QString, QLabel*>& pair: *statusIcons){
|
|
|
|
delete pair.second;
|
|
|
|
}
|
|
|
|
|
2021-05-24 22:06:05 +00:00
|
|
|
for (const std::pair<const QString, QLabel*>& pair: *pencilIcons){
|
|
|
|
delete pair.second;
|
|
|
|
}
|
|
|
|
|
2021-05-15 22:07:49 +00:00
|
|
|
for (const std::pair<const QString, Preview*>& pair: *previews){
|
|
|
|
delete pair.second;
|
|
|
|
}
|
|
|
|
|
2021-05-24 22:06:05 +00:00
|
|
|
delete statusIcons;
|
|
|
|
delete pencilIcons;
|
2021-02-01 22:55:15 +00:00
|
|
|
delete idsToKeep;
|
|
|
|
delete buttons;
|
2021-03-22 18:04:26 +00:00
|
|
|
delete bars;
|
2021-05-15 22:07:49 +00:00
|
|
|
delete previews;
|
2022-04-26 22:17:53 +00:00
|
|
|
delete bodyRenderer;
|
2020-08-20 21:32:30 +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);
|
|
|
|
if (!vi.isValid()) {
|
|
|
|
return;
|
2020-08-20 21:32:30 +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);
|
|
|
|
if (ntds || option.rect.y() < 1) {
|
|
|
|
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);
|
2021-01-07 21:50:12 +00:00
|
|
|
if (!data.sentByMe) {
|
2020-08-20 21:32:30 +00:00
|
|
|
opt.displayAlignment = Qt::AlignLeft | Qt::AlignTop;
|
|
|
|
} else {
|
|
|
|
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();
|
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);
|
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);
|
|
|
|
if (data.sentByMe) {
|
2022-01-09 14:32:23 +00:00
|
|
|
pencilIcon->move(opt.rect.left() + statusIconSize + margin, currentY);
|
2021-05-24 22:06:05 +00:00
|
|
|
} else {
|
2022-01-09 14:32:23 +00:00
|
|
|
pencilIcon->move(opt.rect.right() - statusIconSize - margin, currentY);
|
2021-05-24 22:06:05 +00:00
|
|
|
}
|
|
|
|
pencilIcon->show();
|
|
|
|
} else {
|
|
|
|
std::map<QString, QLabel*>::const_iterator itr = pencilIcons->find(data.id);
|
|
|
|
if (itr != pencilIcons->end()) {
|
|
|
|
delete itr->second;
|
|
|
|
pencilIcons->erase(itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 21:32:30 +00:00
|
|
|
painter->restore();
|
2021-02-01 22:55:15 +00:00
|
|
|
|
|
|
|
if (clearingWidgets) {
|
|
|
|
idsToKeep->insert(data.id);
|
|
|
|
}
|
2020-08-20 21:32:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-09 14:32:23 +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();
|
|
|
|
if (data.sentByMe) {
|
|
|
|
painter->setBrush(option.palette.brush(QPalette::Inactive, QPalette::Highlight));
|
|
|
|
} else {
|
|
|
|
painter->setBrush(option.palette.brush(QPalette::Window));
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-07 14:02:49 +00:00
|
|
|
void MessageDelegate::paintAvatar(const Models::FeedItem& data, const QModelIndex& index, const QStyleOptionViewItem& option, QPainter* painter) const
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2022-01-07 14:02:49 +00:00
|
|
|
if (data.sentByMe) {
|
2022-01-09 14:32:23 +00:00
|
|
|
ax = option.rect.x() + option.rect.width() + margin;
|
2022-01-07 14:02:49 +00:00
|
|
|
} else {
|
2022-01-08 22:28:29 +00:00
|
|
|
ax = margin;
|
2022-01-07 14:02:49 +00:00
|
|
|
}
|
2022-01-08 22:28:29 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool MessageDelegate::needToDrawAvatar(const QModelIndex& index, const Models::FeedItem& data, const QStyleOptionViewItem& option) const
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 21:32:30 +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) {
|
2022-04-26 22:17:53 +00:00
|
|
|
bodyRenderer->setPlainText(data.text);
|
|
|
|
bodyRenderer->setTextWidth(messageRect.size().width());
|
|
|
|
|
|
|
|
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;
|
|
|
|
if (data.correction.corrected) {
|
|
|
|
statusWidth += statusIconSize + margin;
|
|
|
|
}
|
|
|
|
messageSize.setWidth(std::max(statusWidth, messageSize.width()));
|
|
|
|
messageSize.rwidth() += 2 * bubbleMargin;
|
2020-08-20 21:32:30 +00:00
|
|
|
|
|
|
|
return messageSize;
|
|
|
|
}
|
|
|
|
|
2022-04-28 21:29:44 +00:00
|
|
|
QRect MessageDelegate::getHoveredMessageBodyRect(const QModelIndex& index, const Models::FeedItem& data, const QRect& sizeHint) const
|
2022-04-27 21:08:59 +00:00
|
|
|
{
|
2022-04-28 21:29:44 +00:00
|
|
|
QRect localHint = sizeHint.adjusted(bubbleMargin, bubbleMargin + margin, -bubbleMargin, -bubbleMargin / 2);
|
|
|
|
if (needToDrawSender(index, data)) {
|
|
|
|
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
|
|
|
|
2022-04-28 21:29:44 +00:00
|
|
|
QString MessageDelegate::getAnchor(const QPoint& point, const QModelIndex& index, const QRect& sizeHint) const
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
2022-04-28 21:29:44 +00:00
|
|
|
bodyRenderer->setHtml(Shared::processMessageBody(data.text));
|
2022-04-27 21:08:59 +00:00
|
|
|
bodyRenderer->setTextWidth(localHint.size().width());
|
|
|
|
|
2022-04-28 21:29:44 +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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageDelegate::leftClick(const QPoint& point, const QModelIndex& index, const QRect& sizeHint) const
|
|
|
|
{
|
|
|
|
QString anchor = getAnchor(point, index, sizeHint);
|
|
|
|
if (anchor.size() > 0) {
|
|
|
|
emit openLink(anchor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 09:17:08 +00:00
|
|
|
QString MessageDelegate::leftDoubleClick(const QPoint& point, const QModelIndex& index, const QRect& sizeHint)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
|
|
|
bodyRenderer->setHtml(Shared::processMessageBody(data.text));
|
|
|
|
bodyRenderer->setTextWidth(localHint.size().width());
|
|
|
|
|
|
|
|
QAbstractTextDocumentLayout* lay = bodyRenderer->documentLayout();
|
|
|
|
|
|
|
|
int position = lay->hitTest(translated, Qt::HitTestAccuracy::FuzzyHit);
|
|
|
|
QTextCursor cursor(bodyRenderer);
|
|
|
|
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 "";
|
|
|
|
}
|
|
|
|
|
2022-05-02 19:25:50 +00:00
|
|
|
Shared::Hover MessageDelegate::hoverType(const QPoint& point, const QModelIndex& index, const QRect& sizeHint) const
|
2022-04-28 21:29:44 +00:00
|
|
|
{
|
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();
|
|
|
|
|
|
|
|
bodyRenderer->setHtml(Shared::processMessageBody(data.text));
|
|
|
|
bodyRenderer->setTextWidth(localHint.size().width());
|
|
|
|
|
|
|
|
QAbstractTextDocumentLayout* lay = bodyRenderer->documentLayout();
|
|
|
|
QString anchor = lay->anchorAt(translated);
|
|
|
|
|
|
|
|
if (anchor.size() > 0) {
|
|
|
|
return Shared::Hover::anchor;
|
|
|
|
} else {
|
|
|
|
int position = lay->hitTest(translated, Qt::HitTestAccuracy::ExactHit);
|
2022-05-03 09:17:08 +00:00
|
|
|
if (position != -1) {
|
|
|
|
return Shared::Hover::text;
|
2022-05-02 19:25:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Shared::Hover::nothing;
|
2022-04-27 21:08:59 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 19:25:50 +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()));
|
|
|
|
|
2022-05-01 20:19:52 +00:00
|
|
|
bodyRenderer->setHtml(Shared::processMessageBody(data.text));
|
|
|
|
bodyRenderer->setTextWidth(localHint.size().width());
|
2022-05-02 19:25:50 +00:00
|
|
|
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) {
|
|
|
|
QTextCursor cursor(bodyRenderer);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
QString MessageDelegate::clearSelection()
|
|
|
|
{
|
|
|
|
QString lastSelectedId = currentId;
|
|
|
|
currentId = "";
|
|
|
|
selection = std::pair(0, 0);
|
|
|
|
return lastSelectedId;
|
|
|
|
}
|
|
|
|
|
2021-01-07 21:50:12 +00:00
|
|
|
bool MessageDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index)
|
|
|
|
{
|
|
|
|
//qDebug() << event->type();
|
2021-02-01 22:55:15 +00:00
|
|
|
|
|
|
|
|
2021-01-07 21:50:12 +00:00
|
|
|
return QStyledItemDelegate::editorEvent(event, model, option, index);
|
|
|
|
}
|
|
|
|
|
2022-01-08 22:28:29 +00:00
|
|
|
int MessageDelegate::paintButton(QPushButton* btn, QPainter* painter, bool sentByMe, QStyleOptionViewItem& option) const
|
2021-02-01 22:55:15 +00:00
|
|
|
{
|
|
|
|
QPoint start;
|
|
|
|
if (sentByMe) {
|
2022-01-08 22:28:29 +00:00
|
|
|
start = {option.rect.x() + option.rect.width() - btn->width(), option.rect.top()};
|
2021-02-01 22:55:15 +00:00
|
|
|
} else {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-08 22:28:29 +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
|
|
|
}
|
|
|
|
|
2022-01-08 22:28:29 +00:00
|
|
|
int MessageDelegate::paintBar(QProgressBar* bar, QPainter* painter, bool sentByMe, QStyleOptionViewItem& option) const
|
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
|
|
|
|
2022-01-08 22:28:29 +00:00
|
|
|
int MessageDelegate::paintPreview(const Models::FeedItem& data, QPainter* painter, QStyleOptionViewItem& option) const
|
2021-04-20 21:56:47 +00:00
|
|
|
{
|
2021-05-15 22:07:49 +00:00
|
|
|
Preview* preview = 0;
|
|
|
|
std::map<QString, Preview*>::iterator itr = previews->find(data.id);
|
|
|
|
|
|
|
|
QSize size = option.rect.size();
|
2022-02-18 21:27:09 +00:00
|
|
|
QString path = Shared::resolvePath(data.attach.localPath);
|
2021-05-15 22:07:49 +00:00
|
|
|
if (itr != previews->end()) {
|
|
|
|
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);
|
2021-05-15 22:07:49 +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
|
|
|
|
2021-05-16 21:52:59 +00:00
|
|
|
if (!preview->isFileReachable()) { //this is the situation when the file preview couldn't be painted because the file was moved
|
|
|
|
emit invalidPath(data.id); //or deleted. This signal notifies the model, and the model notifies the core, preview can
|
|
|
|
} //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
|
|
|
}
|
|
|
|
|
2021-02-01 22:55:15 +00:00
|
|
|
QPushButton * MessageDelegate::getButton(const Models::FeedItem& data) const
|
|
|
|
{
|
|
|
|
std::map<QString, FeedButton*>::const_iterator itr = buttons->find(data.id);
|
|
|
|
FeedButton* result = 0;
|
|
|
|
if (itr != buttons->end()) {
|
2021-05-14 19:49:38 +00:00
|
|
|
result = itr->second;
|
2021-03-22 18:04:26 +00:00
|
|
|
} else {
|
|
|
|
std::map<QString, QProgressBar*>::const_iterator barItr = bars->find(data.id);
|
|
|
|
if (barItr != bars->end()) {
|
|
|
|
delete barItr->second;
|
|
|
|
bars->erase(barItr);
|
|
|
|
}
|
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"));
|
2021-02-01 22:55:15 +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;
|
|
|
|
}
|
|
|
|
|
2021-03-22 18:04:26 +00:00
|
|
|
QProgressBar * MessageDelegate::getBar(const Models::FeedItem& data) const
|
|
|
|
{
|
|
|
|
std::map<QString, QProgressBar*>::const_iterator barItr = bars->find(data.id);
|
|
|
|
QProgressBar* result = 0;
|
|
|
|
if (barItr != bars->end()) {
|
|
|
|
result = barItr->second;
|
|
|
|
} else {
|
|
|
|
std::map<QString, FeedButton*>::const_iterator itr = buttons->find(data.id);
|
|
|
|
if (itr != buttons->end()) {
|
|
|
|
delete itr->second;
|
|
|
|
buttons->erase(itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == 0) {
|
|
|
|
result = new QProgressBar();
|
2021-04-19 21:49:24 +00:00
|
|
|
result->setRange(0, 100);
|
2021-03-22 18:04:26 +00:00
|
|
|
bars->insert(std::make_pair(data.id, result));
|
|
|
|
}
|
|
|
|
|
2021-04-19 21:49:24 +00:00
|
|
|
result->setValue(data.attach.progress * 100);
|
2021-03-22 18:04:26 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-04-26 16:37:36 +00:00
|
|
|
QLabel * MessageDelegate::getStatusIcon(const Models::FeedItem& data) const
|
|
|
|
{
|
|
|
|
std::map<QString, QLabel*>::const_iterator itr = statusIcons->find(data.id);
|
|
|
|
QLabel* result = 0;
|
|
|
|
|
2021-05-15 22:07:49 +00:00
|
|
|
if (itr != statusIcons->end()) {
|
|
|
|
result = itr->second;
|
|
|
|
} else {
|
|
|
|
result = new QLabel();
|
|
|
|
statusIcons->insert(std::make_pair(data.id, result));
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if (data.error > 0) {
|
|
|
|
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
|
|
|
|
2021-05-24 22:06:05 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-15 22:07:49 +00:00
|
|
|
template <typename T>
|
|
|
|
void removeElements(std::map<QString, T*>* elements, std::set<QString>* idsToKeep) {
|
|
|
|
std::set<QString> toRemove;
|
|
|
|
for (const std::pair<const QString, T*>& pair: *elements) {
|
|
|
|
if (idsToKeep->find(pair.first) == idsToKeep->end()) {
|
|
|
|
delete pair.second;
|
|
|
|
toRemove.insert(pair.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const QString& key : toRemove) {
|
|
|
|
elements->erase(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
bodyRenderer->setHtml(Shared::processMessageBody(data.text));
|
|
|
|
bodyRenderer->setTextWidth(option.rect.size().width());
|
|
|
|
painter->save();
|
|
|
|
painter->translate(option.rect.topLeft());
|
2022-05-01 20:19:52 +00:00
|
|
|
|
|
|
|
if (data.id == currentId) {
|
|
|
|
QTextCursor cursor(bodyRenderer);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-04-26 22:17:53 +00:00
|
|
|
bodyRenderer->drawContents(painter);
|
2022-05-01 20:19:52 +00:00
|
|
|
|
2022-04-26 22:17:53 +00:00
|
|
|
painter->restore();
|
2022-04-27 21:08:59 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageDelegate::beginClearWidgets()
|
|
|
|
{
|
|
|
|
idsToKeep->clear();
|
|
|
|
clearingWidgets = true;
|
|
|
|
}
|
|
|
|
|
2021-02-01 22:55:15 +00:00
|
|
|
void MessageDelegate::endClearWidgets()
|
|
|
|
{
|
|
|
|
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);
|
2021-05-15 22:07:49 +00:00
|
|
|
removeElements(previews, idsToKeep);
|
2021-02-01 22:55:15 +00:00
|
|
|
|
|
|
|
idsToKeep->clear();
|
|
|
|
clearingWidgets = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 11:02:42 +00:00
|
|
|
void MessageDelegate::onButtonPushed() const
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2021-03-22 18:04:26 +00:00
|
|
|
void MessageDelegate::clearHelperWidget(const Models::FeedItem& data) const
|
|
|
|
{
|
|
|
|
std::map<QString, FeedButton*>::const_iterator itr = buttons->find(data.id);
|
|
|
|
if (itr != buttons->end()) {
|
|
|
|
delete itr->second;
|
|
|
|
buttons->erase(itr);
|
|
|
|
} else {
|
|
|
|
std::map<QString, QProgressBar*>::const_iterator barItr = bars->find(data.id);
|
|
|
|
if (barItr != bars->end()) {
|
|
|
|
delete barItr->second;
|
|
|
|
bars->erase(barItr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|