Enhance image attachment handling by caching loaded images and improving scaling logic

This commit is contained in:
w15dev 2025-01-27 01:02:45 +03:00 committed by Jonathan White
parent 86f74a00d0
commit af2d0b1429
2 changed files with 17 additions and 4 deletions

View File

@ -64,6 +64,7 @@ void PreviewEntryAttachmentsDialog::setAttachment(const QString& name, const QBy
m_type = attachmentType(data);
m_data = data;
m_imageCache = QImage();
update();
}
@ -86,23 +87,31 @@ void PreviewEntryAttachmentsDialog::updateTextAttachment(const QByteArray& data)
void PreviewEntryAttachmentsDialog::updateImageAttachment(const QByteArray& data)
{
QImage image{};
if (!image.loadFromData(data)) {
if (m_imageCache.isNull() && !m_imageCache.loadFromData(data)) {
updateTextAttachment(tr("Image format not supported").toUtf8());
return;
}
updateImageAttachment(m_imageCache);
}
void PreviewEntryAttachmentsDialog::updateImageAttachment(const QImage& image)
{
m_ui->attachmentTextEdit->clear();
auto cursor = m_ui->attachmentTextEdit->textCursor();
cursor.insertImage(image.scaled(calculateImageSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
QSize PreviewEntryAttachmentsDialog::calculateImageSize()
{
// Scale the image to the contents rect minus another set of margins to avoid scrollbars
auto margins = m_ui->attachmentTextEdit->contentsMargins();
auto size = m_ui->attachmentTextEdit->contentsRect().size();
size.setWidth(size.width() - margins.left() - margins.right());
size.setHeight(size.height() - margins.top() - margins.bottom());
image = image.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
cursor.insertImage(image);
return size;
}
Tools::MimeType PreviewEntryAttachmentsDialog::attachmentType(const QByteArray& data) const

View File

@ -50,10 +50,14 @@ private:
void update();
void updateTextAttachment(const QByteArray& data);
void updateImageAttachment(const QByteArray& data);
void updateImageAttachment(const QImage& data);
QSize calculateImageSize();
QScopedPointer<Ui::EntryAttachmentsDialog> m_ui;
QString m_name;
QByteArray m_data;
QImage m_imageCache;
Tools::MimeType m_type{Tools::MimeType::Unknown};
};