Render image previews from prepared model data and preserve aspect ratios
This commit is contained in:
1 parent
04d9d1f2b9
commit
08e9b62e16
2 files changed
+8
-9
No files matched your search
@@ -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<QImage>();
|
||||
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"));
|
||||
}
|
||||
|
||||
/* бейдж: разрешение · размер */
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user