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>
|
|
|
|
#include <QApplication>
|
2021-02-01 22:55:15 +00:00
|
|
|
#include <QMouseEvent>
|
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
|
|
|
|
|
|
|
constexpr int avatarHeight = 50;
|
|
|
|
constexpr int margin = 6;
|
2021-04-22 22:41:32 +00:00
|
|
|
constexpr int textMargin = 2;
|
|
|
|
constexpr int statusIconSize = 16;
|
2020-08-20 21:32:30 +00:00
|
|
|
|
|
|
|
MessageDelegate::MessageDelegate(QObject* parent):
|
2021-05-03 11:23:41 +00:00
|
|
|
QStyledItemDelegate(parent),
|
|
|
|
bodyFont(),
|
|
|
|
nickFont(),
|
|
|
|
dateFont(),
|
|
|
|
bodyMetrics(bodyFont),
|
|
|
|
nickMetrics(nickFont),
|
|
|
|
dateMetrics(dateFont),
|
|
|
|
buttonHeight(0),
|
|
|
|
barHeight(0),
|
|
|
|
buttons(new std::map<QString, FeedButton*>()),
|
|
|
|
bars(new std::map<QString, QProgressBar*>()),
|
|
|
|
statusIcons(new std::map<QString, QLabel*>()),
|
|
|
|
bodies(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>()),
|
|
|
|
clearingWidgets(false)
|
2020-08-20 21:32:30 +00:00
|
|
|
{
|
2021-02-01 22:55:15 +00:00
|
|
|
QPushButton btn;
|
|
|
|
buttonHeight = btn.sizeHint().height();
|
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-03 11:23:41 +00:00
|
|
|
for (const std::pair<const QString, QLabel*>& pair: *bodies){
|
|
|
|
delete pair.second;
|
|
|
|
}
|
|
|
|
|
2021-05-15 22:07:49 +00:00
|
|
|
for (const std::pair<const QString, Preview*>& pair: *previews){
|
|
|
|
delete pair.second;
|
|
|
|
}
|
|
|
|
|
2021-02-01 22:55:15 +00:00
|
|
|
delete idsToKeep;
|
|
|
|
delete buttons;
|
2021-03-22 18:04:26 +00:00
|
|
|
delete bars;
|
2021-05-03 11:23:41 +00:00
|
|
|
delete bodies;
|
2021-05-15 22:07:49 +00:00
|
|
|
delete previews;
|
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);
|
|
|
|
|
2021-01-07 21:50:12 +00:00
|
|
|
if (option.state & QStyle::State_MouseOver) {
|
|
|
|
painter->fillRect(option.rect, option.palette.brush(QPalette::Inactive, QPalette::Highlight));
|
|
|
|
}
|
|
|
|
|
|
|
|
QIcon icon(data.avatar);
|
|
|
|
|
|
|
|
if (data.sentByMe) {
|
2020-08-20 21:32:30 +00:00
|
|
|
painter->drawPixmap(option.rect.width() - avatarHeight - margin, option.rect.y() + margin / 2, icon.pixmap(avatarHeight, avatarHeight));
|
|
|
|
} else {
|
|
|
|
painter->drawPixmap(margin, option.rect.y() + margin / 2, icon.pixmap(avatarHeight, avatarHeight));
|
|
|
|
}
|
|
|
|
|
|
|
|
QStyleOptionViewItem opt = option;
|
|
|
|
QRect messageRect = option.rect.adjusted(margin, margin / 2, -(avatarHeight + 2 * margin), -margin / 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;
|
|
|
|
messageRect.adjust(avatarHeight + margin, 0, avatarHeight + margin, 0);
|
|
|
|
} else {
|
|
|
|
opt.displayAlignment = Qt::AlignRight | Qt::AlignTop;
|
|
|
|
}
|
|
|
|
opt.rect = messageRect;
|
|
|
|
|
2021-02-01 22:55:15 +00:00
|
|
|
QSize messageSize(0, 0);
|
2021-05-03 11:23:41 +00:00
|
|
|
QSize bodySize(0, 0);
|
2021-02-01 22:55:15 +00:00
|
|
|
if (data.text.size() > 0) {
|
|
|
|
messageSize = bodyMetrics.boundingRect(messageRect, Qt::TextWordWrap, data.text).size();
|
2021-05-03 11:23:41 +00:00
|
|
|
bodySize = messageSize;
|
2021-02-01 22:55:15 +00:00
|
|
|
}
|
2021-01-07 21:50:12 +00:00
|
|
|
messageSize.rheight() += nickMetrics.lineSpacing();
|
|
|
|
messageSize.rheight() += dateMetrics.height();
|
|
|
|
if (messageSize.width() < opt.rect.width()) {
|
|
|
|
QSize senderSize = nickMetrics.boundingRect(messageRect, 0, data.sender).size();
|
|
|
|
if (senderSize.width() > messageSize.width()) {
|
|
|
|
messageSize.setWidth(senderSize.width());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
messageSize.setWidth(opt.rect.width());
|
|
|
|
}
|
|
|
|
|
2020-08-20 21:32:30 +00:00
|
|
|
QRect rect;
|
|
|
|
painter->setFont(nickFont);
|
2021-01-07 21:50:12 +00:00
|
|
|
painter->drawText(opt.rect, opt.displayAlignment, data.sender, &rect);
|
2021-04-22 22:41:32 +00:00
|
|
|
opt.rect.adjust(0, rect.height() + textMargin, 0, 0);
|
2021-01-14 11:22:02 +00:00
|
|
|
painter->save();
|
|
|
|
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:
|
2021-05-14 19:49:38 +00:00
|
|
|
paintPreview(data, painter, opt);
|
2021-01-14 11:22:02 +00:00
|
|
|
case Models::downloading:
|
2021-03-22 18:04:26 +00:00
|
|
|
paintBar(getBar(data), painter, data.sentByMe, opt);
|
2021-01-14 11:22:02 +00:00
|
|
|
break;
|
|
|
|
case Models::remote:
|
2021-02-01 22:55:15 +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);
|
|
|
|
paintPreview(data, painter, opt);
|
|
|
|
break;
|
2021-05-14 19:49:38 +00:00
|
|
|
case Models::errorDownload: {
|
|
|
|
paintButton(getButton(data), painter, data.sentByMe, opt);
|
2021-05-15 22:07:49 +00:00
|
|
|
paintComment(data, painter, opt);
|
2021-05-14 19:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case Models::errorUpload:{
|
|
|
|
clearHelperWidget(data);
|
|
|
|
paintPreview(data, painter, opt);
|
2021-05-15 22:07:49 +00:00
|
|
|
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
|
|
|
int messageLeft = INT16_MAX;
|
|
|
|
QWidget* vp = static_cast<QWidget*>(painter->device());
|
2021-02-01 22:55:15 +00:00
|
|
|
if (data.text.size() > 0) {
|
2021-05-03 11:23:41 +00:00
|
|
|
QLabel* body = getBody(data);
|
|
|
|
body->setParent(vp);
|
|
|
|
body->setMaximumWidth(bodySize.width());
|
|
|
|
body->setMinimumWidth(bodySize.width());
|
2021-05-15 22:07:49 +00:00
|
|
|
body->setMinimumHeight(bodySize.height());
|
|
|
|
body->setMaximumHeight(bodySize.height());
|
2021-05-03 11:23:41 +00:00
|
|
|
body->setAlignment(opt.displayAlignment);
|
|
|
|
messageLeft = opt.rect.x();
|
|
|
|
if (data.sentByMe) {
|
|
|
|
messageLeft = opt.rect.topRight().x() - bodySize.width();
|
|
|
|
}
|
|
|
|
body->move(messageLeft, opt.rect.y());
|
|
|
|
body->show();
|
|
|
|
opt.rect.adjust(0, bodySize.height() + textMargin, 0, 0);
|
2021-02-01 22:55:15 +00:00
|
|
|
}
|
2020-08-20 21:32:30 +00:00
|
|
|
painter->setFont(dateFont);
|
|
|
|
QColor q = painter->pen().color();
|
|
|
|
q.setAlpha(180);
|
|
|
|
painter->setPen(q);
|
2021-01-07 21:50:12 +00:00
|
|
|
painter->drawText(opt.rect, opt.displayAlignment, data.date.toLocalTime().toString(), &rect);
|
2021-04-22 22:41:32 +00:00
|
|
|
if (data.sentByMe) {
|
|
|
|
if (messageLeft > rect.x() - statusIconSize - margin) {
|
|
|
|
messageLeft = rect.x() - statusIconSize - margin;
|
|
|
|
}
|
2021-04-26 16:37:36 +00:00
|
|
|
QLabel* statusIcon = getStatusIcon(data);
|
|
|
|
|
|
|
|
statusIcon->setParent(vp);
|
|
|
|
statusIcon->move(messageLeft, opt.rect.y());
|
|
|
|
statusIcon->show();
|
|
|
|
opt.rect.adjust(0, statusIconSize + textMargin, 0, 0);
|
2021-04-22 22:41:32 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
QSize MessageDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
2021-01-07 21:50:12 +00:00
|
|
|
QRect messageRect = option.rect.adjusted(0, margin / 2, -(avatarHeight + 3 * margin), -margin / 2);
|
2020-08-20 21:32:30 +00:00
|
|
|
QStyleOptionViewItem opt = option;
|
|
|
|
opt.rect = messageRect;
|
2021-01-14 11:22:02 +00:00
|
|
|
QVariant va = index.data(Models::MessageFeed::Attach);
|
|
|
|
Models::Attachment attach = qvariant_cast<Models::Attachment>(va);
|
2021-02-01 22:55:15 +00:00
|
|
|
QString body = index.data(Models::MessageFeed::Text).toString();
|
|
|
|
QSize messageSize(0, 0);
|
|
|
|
if (body.size() > 0) {
|
|
|
|
messageSize = bodyMetrics.boundingRect(messageRect, Qt::TextWordWrap, body).size();
|
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
|
|
|
|
2021-01-14 11:22:02 +00:00
|
|
|
switch (attach.state) {
|
|
|
|
case Models::none:
|
|
|
|
break;
|
|
|
|
case Models::uploading:
|
2021-05-15 22:07:49 +00:00
|
|
|
messageSize.rheight() += Preview::calculateAttachSize(attach.localPath, messageRect).height() + textMargin;
|
2021-01-14 11:22:02 +00:00
|
|
|
case Models::downloading:
|
2021-04-22 22:41:32 +00:00
|
|
|
messageSize.rheight() += barHeight + textMargin;
|
2021-01-14 11:22:02 +00:00
|
|
|
break;
|
|
|
|
case Models::remote:
|
2021-04-22 22:41:32 +00:00
|
|
|
messageSize.rheight() += buttonHeight + textMargin;
|
2021-01-14 11:22:02 +00:00
|
|
|
break;
|
|
|
|
case Models::ready:
|
2021-05-14 19:49:38 +00:00
|
|
|
case Models::local:
|
2021-05-15 22:07:49 +00:00
|
|
|
messageSize.rheight() += Preview::calculateAttachSize(attach.localPath, messageRect).height() + textMargin;
|
2021-01-14 11:22:02 +00:00
|
|
|
break;
|
2021-04-19 21:49:24 +00:00
|
|
|
case Models::errorDownload:
|
2021-05-14 19:49:38 +00:00
|
|
|
messageSize.rheight() += buttonHeight + textMargin;
|
|
|
|
messageSize.rheight() += dateMetrics.boundingRect(messageRect, Qt::TextWordWrap, attach.error).size().height() + textMargin;
|
|
|
|
break;
|
2021-04-19 21:49:24 +00:00
|
|
|
case Models::errorUpload:
|
2021-05-15 22:07:49 +00:00
|
|
|
messageSize.rheight() += Preview::calculateAttachSize(attach.localPath, messageRect).height() + textMargin;
|
2021-05-14 19:49:38 +00:00
|
|
|
messageSize.rheight() += dateMetrics.boundingRect(messageRect, Qt::TextWordWrap, attach.error).size().height() + textMargin;
|
2021-04-19 21:49:24 +00:00
|
|
|
break;
|
2021-01-14 11:22:02 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 21:32:30 +00:00
|
|
|
messageSize.rheight() += nickMetrics.lineSpacing();
|
2021-04-22 22:41:32 +00:00
|
|
|
messageSize.rheight() += textMargin;
|
|
|
|
messageSize.rheight() += dateMetrics.height() > statusIconSize ? dateMetrics.height() : statusIconSize;
|
2020-08-20 21:32:30 +00:00
|
|
|
|
|
|
|
if (messageSize.height() < avatarHeight) {
|
|
|
|
messageSize.setHeight(avatarHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
messageSize.rheight() += margin;
|
|
|
|
|
|
|
|
return messageSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageDelegate::initializeFonts(const QFont& font)
|
|
|
|
{
|
|
|
|
bodyFont = font;
|
|
|
|
nickFont = font;
|
|
|
|
dateFont = font;
|
|
|
|
|
|
|
|
nickFont.setBold(true);
|
2021-04-22 22:41:32 +00:00
|
|
|
|
|
|
|
float ndps = nickFont.pointSizeF();
|
|
|
|
if (ndps != -1) {
|
|
|
|
nickFont.setPointSizeF(ndps * 1.2);
|
|
|
|
} else {
|
|
|
|
nickFont.setPointSize(nickFont.pointSize() + 2);
|
|
|
|
}
|
|
|
|
|
2020-08-20 21:32:30 +00:00
|
|
|
dateFont.setItalic(true);
|
|
|
|
float dps = dateFont.pointSizeF();
|
|
|
|
if (dps != -1) {
|
2021-04-22 22:41:32 +00:00
|
|
|
dateFont.setPointSizeF(dps * 0.8);
|
2020-08-20 21:32:30 +00:00
|
|
|
} else {
|
|
|
|
dateFont.setPointSize(dateFont.pointSize() - 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
bodyMetrics = QFontMetrics(bodyFont);
|
|
|
|
nickMetrics = QFontMetrics(nickFont);
|
|
|
|
dateMetrics = QFontMetrics(dateFont);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-02-01 22:55:15 +00:00
|
|
|
void MessageDelegate::paintButton(QPushButton* btn, QPainter* painter, bool sentByMe, QStyleOptionViewItem& option) const
|
|
|
|
{
|
|
|
|
QPoint start;
|
|
|
|
if (sentByMe) {
|
|
|
|
start = {option.rect.width() - btn->width(), option.rect.top()};
|
|
|
|
} 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);
|
2021-02-01 22:55:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 22:07:49 +00:00
|
|
|
void MessageDelegate::paintComment(const Models::FeedItem& data, QPainter* painter, QStyleOptionViewItem& option) const
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-03-22 18:04:26 +00:00
|
|
|
void MessageDelegate::paintBar(QProgressBar* bar, QPainter* painter, bool sentByMe, QStyleOptionViewItem& option) const
|
|
|
|
{
|
|
|
|
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);
|
2021-03-22 18:04:26 +00:00
|
|
|
}
|
2021-02-01 22:55:15 +00:00
|
|
|
|
2021-04-20 21:56:47 +00:00
|
|
|
void MessageDelegate::paintPreview(const Models::FeedItem& data, QPainter* painter, QStyleOptionViewItem& option) const
|
|
|
|
{
|
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();
|
|
|
|
if (itr != previews->end()) {
|
|
|
|
preview = itr->second;
|
|
|
|
preview->actualize(data.attach.localPath, 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());
|
|
|
|
preview = new Preview(data.attach.localPath, size, option.rect.topLeft(), data.sentByMe, vp);
|
|
|
|
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-15 22:07:49 +00:00
|
|
|
option.rect.adjust(0, preview->size().height() + textMargin, 0, 0);
|
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-03 11:23:41 +00:00
|
|
|
QLabel * MessageDelegate::getBody(const Models::FeedItem& data) const
|
|
|
|
{
|
|
|
|
std::map<QString, QLabel*>::const_iterator itr = bodies->find(data.id);
|
|
|
|
QLabel* result = 0;
|
|
|
|
|
|
|
|
if (itr != bodies->end()) {
|
|
|
|
result = itr->second;
|
|
|
|
} else {
|
|
|
|
result = new QLabel();
|
|
|
|
result->setFont(bodyFont);
|
|
|
|
result->setWordWrap(true);
|
|
|
|
result->setOpenExternalLinks(true);
|
|
|
|
result->setTextInteractionFlags(result->textInteractionFlags() | Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse);
|
|
|
|
bodies->insert(std::make_pair(data.id, result));
|
|
|
|
}
|
|
|
|
|
|
|
|
result->setText(Shared::processMessageBody(data.text));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-02-01 22:55:15 +00:00
|
|
|
void MessageDelegate::beginClearWidgets()
|
|
|
|
{
|
|
|
|
idsToKeep->clear();
|
|
|
|
clearingWidgets = true;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
removeElements(bodies, idsToKeep);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 21:32:30 +00:00
|
|
|
// void MessageDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
|
|
|
|
// {
|
|
|
|
//
|
|
|
|
// }
|