Skip to content

Commit af2d0b1

Browse files
w15eacredroidmonkey
authored andcommitted
Enhance image attachment handling by caching loaded images and improving scaling logic
1 parent 86f74a0 commit af2d0b1

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/gui/entry/PreviewEntryAttachmentsDialog.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void PreviewEntryAttachmentsDialog::setAttachment(const QString& name, const QBy
6464

6565
m_type = attachmentType(data);
6666
m_data = data;
67+
m_imageCache = QImage();
6768

6869
update();
6970
}
@@ -86,23 +87,31 @@ void PreviewEntryAttachmentsDialog::updateTextAttachment(const QByteArray& data)
8687

8788
void PreviewEntryAttachmentsDialog::updateImageAttachment(const QByteArray& data)
8889
{
89-
QImage image{};
90-
if (!image.loadFromData(data)) {
90+
if (m_imageCache.isNull() && !m_imageCache.loadFromData(data)) {
9191
updateTextAttachment(tr("Image format not supported").toUtf8());
9292
return;
9393
}
9494

95+
updateImageAttachment(m_imageCache);
96+
}
97+
98+
void PreviewEntryAttachmentsDialog::updateImageAttachment(const QImage& image)
99+
{
95100
m_ui->attachmentTextEdit->clear();
96101
auto cursor = m_ui->attachmentTextEdit->textCursor();
97102

103+
cursor.insertImage(image.scaled(calculateImageSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
104+
}
105+
106+
QSize PreviewEntryAttachmentsDialog::calculateImageSize()
107+
{
98108
// Scale the image to the contents rect minus another set of margins to avoid scrollbars
99109
auto margins = m_ui->attachmentTextEdit->contentsMargins();
100110
auto size = m_ui->attachmentTextEdit->contentsRect().size();
101111
size.setWidth(size.width() - margins.left() - margins.right());
102112
size.setHeight(size.height() - margins.top() - margins.bottom());
103-
image = image.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
104113

105-
cursor.insertImage(image);
114+
return size;
106115
}
107116

108117
Tools::MimeType PreviewEntryAttachmentsDialog::attachmentType(const QByteArray& data) const

src/gui/entry/PreviewEntryAttachmentsDialog.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ class PreviewEntryAttachmentsDialog : public QDialog
5050
void update();
5151
void updateTextAttachment(const QByteArray& data);
5252
void updateImageAttachment(const QByteArray& data);
53+
void updateImageAttachment(const QImage& data);
54+
55+
QSize calculateImageSize();
5356

5457
QScopedPointer<Ui::EntryAttachmentsDialog> m_ui;
5558

5659
QString m_name;
5760
QByteArray m_data;
61+
QImage m_imageCache;
5862
Tools::MimeType m_type{Tools::MimeType::Unknown};
5963
};

0 commit comments

Comments
 (0)