From b7b70bc198ce0fcfb1a26db3c60ee3f10332f581 Mon Sep 17 00:00:00 2001 From: blue Date: Tue, 11 May 2021 00:06:40 +0300 Subject: [PATCH] segfault fix when trying to send something but the history isn't loaded yet, icon and for attached files which are not previewed --- shared/global.cpp | 4 +++ ui/models/messagefeed.cpp | 29 ++++++++++-------- ui/utils/messagedelegate.cpp | 59 +++++++++++++++++++++++++----------- 3 files changed, 61 insertions(+), 31 deletions(-) diff --git a/shared/global.cpp b/shared/global.cpp index 62843ed..25a1c87 100644 --- a/shared/global.cpp +++ b/shared/global.cpp @@ -112,6 +112,8 @@ Shared::Global::Global(): #endif } + +static const QSize defaultIconFileInfoHeight(50, 50); Shared::Global::FileInfo Shared::Global::getFileInfo(const QString& path) { std::map::const_iterator itr = instance->fileCache.find(path); @@ -131,6 +133,8 @@ Shared::Global::FileInfo Shared::Global::getFileInfo(const QString& path) p = FileInfo::Preview::picture; QImage img(path); size = img.size(); + } else { + size = defaultIconFileInfoHeight; } itr = instance->fileCache.insert(std::make_pair(path, FileInfo({info.fileName(), size, type, p}))).first; diff --git a/ui/models/messagefeed.cpp b/ui/models/messagefeed.cpp index 4187af8..d5fb3bc 100644 --- a/ui/models/messagefeed.cpp +++ b/ui/models/messagefeed.cpp @@ -532,20 +532,23 @@ QModelIndex Models::MessageFeed::modelIndexById(const QString& id) const QModelIndex Models::MessageFeed::modelIndexByTime(const QString& id, const QDateTime& time) const { - StorageByTime::const_iterator tItr = indexByTime.upper_bound(time); - StorageByTime::const_iterator tBeg = indexByTime.begin(); - bool found = false; - while (tItr != tBeg) { - if (id == (*tItr)->getId()) { - found = true; - break; + if (indexByTime.size() > 0) { + StorageByTime::const_iterator tItr = indexByTime.upper_bound(time); + StorageByTime::const_iterator tBeg = indexByTime.begin(); + StorageByTime::const_iterator tEnd = indexByTime.end(); + bool found = false; + while (tItr != tBeg) { + if (tItr != tEnd && id == (*tItr)->getId()) { + found = true; + break; + } + --tItr; + } + + if (found && tItr != tEnd && id == (*tItr)->getId()) { + int position = indexByTime.rank(tItr); + return createIndex(position, 0, *tItr); } - --tItr; - } - - if (found || id == (*tItr)->getId()) { - int position = indexByTime.rank(tItr); - return createIndex(position, 0, *tItr); } return QModelIndex(); diff --git a/ui/utils/messagedelegate.cpp b/ui/utils/messagedelegate.cpp index 8db024d..6b459f2 100644 --- a/ui/utils/messagedelegate.cpp +++ b/ui/utils/messagedelegate.cpp @@ -307,25 +307,48 @@ void MessageDelegate::paintBar(QProgressBar* bar, QPainter* painter, bool sentBy void MessageDelegate::paintPreview(const Models::FeedItem& data, QPainter* painter, QStyleOptionViewItem& option) const { Shared::Global::FileInfo info = Shared::Global::getFileInfo(data.attach.localPath); - if (info.preview == Shared::Global::FileInfo::Preview::picture) { - QSize size = constrainAttachSize(info.size, option.rect.size()); - - QPoint start; - if (data.sentByMe) { - start = {option.rect.width() - size.width(), option.rect.top()}; - start.rx() += margin; - } else { - start = option.rect.topLeft(); - } - QImage img(data.attach.localPath); - if (img.isNull()) { - emit invalidPath(data.id); - } else { - painter->drawImage(QRect(start, size), img); - } - - option.rect.adjust(0, size.height() + textMargin, 0, 0); + QSize size = constrainAttachSize(info.size, option.rect.size()); + + QPoint start; + if (data.sentByMe) { + start = {option.rect.width() - size.width(), option.rect.top()}; + start.rx() += margin; + } else { + start = option.rect.topLeft(); } + QRect rect(start, size); + switch (info.preview) { + case Shared::Global::FileInfo::Preview::picture: { + QImage img(data.attach.localPath); + if (img.isNull()) { + emit invalidPath(data.id); + } else { + painter->drawImage(rect, img); + } + } + break; + default: { + QIcon icon = QIcon::fromTheme(info.mime.iconName()); + + painter->save(); + + painter->setFont(bodyFont); + int labelWidth = option.rect.width() - size.width() - margin; + QString elidedName = bodyMetrics.elidedText(info.name, Qt::ElideMiddle, labelWidth); + QSize nameSize = bodyMetrics.boundingRect(QRect(start, QSize(labelWidth, 0)), 0, elidedName).size(); + if (data.sentByMe) { + start.rx() -= nameSize.width() + margin; + } + painter->drawPixmap({start, size}, icon.pixmap(info.size)); + start.rx() += size.width() + margin; + start.ry() += nameSize.height() + (size.height() - nameSize.height()) / 2; + painter->drawText(start, elidedName); + + painter->restore(); + } + } + + option.rect.adjust(0, size.height() + textMargin, 0, 0); } QPushButton * MessageDelegate::getButton(const Models::FeedItem& data) const