From 08e9b62e162f0366960fc486d0f422364f1d3cd9 Mon Sep 17 00:00:00 2001 From: evgeny Date: Wed, 28 Oct 2026 02:01:58 +0100 Subject: [PATCH] Render image previews from prepared model data and preserve aspect ratios --- tools/chatgui/src/messagedelegate.cpp | 15 ++++++--------- tools/chatgui/src/messagedelegate.h | 2 ++ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/tools/chatgui/src/messagedelegate.cpp b/tools/chatgui/src/messagedelegate.cpp index d7f00eaa7..88653d99f 100644 --- a/tools/chatgui/src/messagedelegate.cpp +++ b/tools/chatgui/src/messagedelegate.cpp @@ -600,6 +600,8 @@ MessageDelegate::Layout MessageDelegate::calcBodyLayout( if (L.isImage) { int barW = 3, barPad = 4; int thW = 220, thH = 160; + const int iw = index.data(MsgImageWidthRole).toInt(), ih = index.data(MsgImageHeightRole).toInt(); + if (iw > 0 && ih > 0) thH = qBound(80, qRound((double)thW * ih / iw), 300); L.bubbleWidth = thW; L.totalHeight = qMax(thH, kAvatar) + kItemGap; @@ -1332,14 +1334,8 @@ void MessageDelegate::paintBody(QPainter* painter, const QStyleOptionViewItem& o clip.addRoundedRect(L.imageThumbRect, kRadius, kRadius); painter->setClipPath(clip); - QPixmap img; - if (!filePath.isEmpty() && QFile::exists(filePath)) { - QImageReader reader(filePath); - reader.setAutoTransform(true); - reader.setScaledSize(L.imageThumbRect.size() * 2); - QImage im = reader.read(); - if (!im.isNull()) img = QPixmap::fromImage(im); - } + const QImage image = index.data(MsgImagePreviewRole).value(); + const QPixmap img = QPixmap::fromImage(image); if (!img.isNull()) { painter->fillRect(L.imageThumbRect, QColor(20, 20, 24)); @@ -1353,7 +1349,8 @@ void MessageDelegate::paintBody(QPainter* painter, const QStyleOptionViewItem& o iconFont.setPointSize(iconFont.pointSize() + 10); painter->setFont(iconFont); painter->setPen(QColor(150, 150, 150)); - painter->drawText(L.imageThumbRect, Qt::AlignCenter, QString::fromUtf8("\xF0\x9F\x93\xB7")); + painter->drawText(L.imageThumbRect, Qt::AlignCenter, index.data(MsgImageErrorRole).toBool() + ? QCoreApplication::translate("ChatGui", "Failed to load photo") : QString::fromUtf8("\xF0\x9F\x93\xB7")); } /* бейдж: разрешение · размер */ diff --git a/tools/chatgui/src/messagedelegate.h b/tools/chatgui/src/messagedelegate.h index ae71cf83f..2e19631fc 100644 --- a/tools/chatgui/src/messagedelegate.h +++ b/tools/chatgui/src/messagedelegate.h @@ -55,6 +55,8 @@ enum MessageDataRole { MsgHighlightRole = Qt::UserRole + 41, MsgEncryptedRole = Qt::UserRole + 42, MsgVersionRole = Qt::UserRole + 43, + MsgImagePreviewRole = Qt::UserRole + 44, /* QImage ограниченного размера, подготовленный worker. */ + MsgImageErrorRole = Qt::UserRole + 45, /* Локальный файл не декодируется; показать читаемую ошибку. */ }; class MessageDelegate : public QStyledItemDelegate {