mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-03-02 11:19:30 -05:00
Enhance image attachment handling by caching loaded images and improving scaling logic
This commit is contained in:
parent
86f74a00d0
commit
af2d0b1429
@ -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
|
||||
|
@ -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};
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user