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_type = attachmentType(data);
m_data = data; m_data = data;
m_imageCache = QImage();
update(); update();
} }
@ -86,23 +87,31 @@ void PreviewEntryAttachmentsDialog::updateTextAttachment(const QByteArray& data)
void PreviewEntryAttachmentsDialog::updateImageAttachment(const QByteArray& data) void PreviewEntryAttachmentsDialog::updateImageAttachment(const QByteArray& data)
{ {
QImage image{}; if (m_imageCache.isNull() && !m_imageCache.loadFromData(data)) {
if (!image.loadFromData(data)) {
updateTextAttachment(tr("Image format not supported").toUtf8()); updateTextAttachment(tr("Image format not supported").toUtf8());
return; return;
} }
updateImageAttachment(m_imageCache);
}
void PreviewEntryAttachmentsDialog::updateImageAttachment(const QImage& image)
{
m_ui->attachmentTextEdit->clear(); m_ui->attachmentTextEdit->clear();
auto cursor = m_ui->attachmentTextEdit->textCursor(); 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 // Scale the image to the contents rect minus another set of margins to avoid scrollbars
auto margins = m_ui->attachmentTextEdit->contentsMargins(); auto margins = m_ui->attachmentTextEdit->contentsMargins();
auto size = m_ui->attachmentTextEdit->contentsRect().size(); auto size = m_ui->attachmentTextEdit->contentsRect().size();
size.setWidth(size.width() - margins.left() - margins.right()); size.setWidth(size.width() - margins.left() - margins.right());
size.setHeight(size.height() - margins.top() - margins.bottom()); 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 Tools::MimeType PreviewEntryAttachmentsDialog::attachmentType(const QByteArray& data) const

View file

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