From 7d515b08c4a3131a9de1f4536acd01496a5fa851 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Thu, 19 Oct 2023 09:15:59 +0300 Subject: [PATCH] Fix pattern image proportions in pattern properties. --- .../dialogs/dialogpatternproperties.cpp | 11 ++++++--- src/libs/ifc/xml/vpatternimage.cpp | 23 +++++++++++++++++++ src/libs/ifc/xml/vpatternimage.h | 1 + 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/app/valentina/dialogs/dialogpatternproperties.cpp b/src/app/valentina/dialogs/dialogpatternproperties.cpp index e747aec4d..fdeaa028f 100644 --- a/src/app/valentina/dialogs/dialogpatternproperties.cpp +++ b/src/app/valentina/dialogs/dialogpatternproperties.cpp @@ -368,7 +368,7 @@ void DialogPatternProperties::ValidatePassmarkWidth() const void DialogPatternProperties::InitImage() { ui->imageLabel->setContextMenuPolicy(Qt::CustomContextMenu); - ui->imageLabel->setScaledContents(true); + ui->imageLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); connect(ui->imageLabel, &QWidget::customContextMenuRequested, this, [this]() { @@ -403,7 +403,9 @@ void DialogPatternProperties::InitImage() const VPatternImage image = m_doc->GetImage(); if (image.IsValid()) { - ui->imageLabel->setPixmap(image.GetPixmap(ui->imageLabel->width(), ui->imageLabel->height())); + QPixmap pixImage = image.GetPixmap(); + ui->imageLabel->setPixmap( + pixImage.scaled(ui->imageLabel->width(), ui->imageLabel->height(), Qt::KeepAspectRatio)); } else { @@ -430,7 +432,10 @@ void DialogPatternProperties::ChangeImage() } m_doc->SetImage(image); - ui->imageLabel->setPixmap(image.GetPixmap(ui->imageLabel->width(), ui->imageLabel->height())); + + QPixmap pixImage = image.GetPixmap(); + ui->imageLabel->setPixmap( + pixImage.scaled(ui->imageLabel->width(), ui->imageLabel->height(), Qt::KeepAspectRatio)); m_deleteAction->setEnabled(true); m_saveImageAction->setEnabled(true); diff --git a/src/libs/ifc/xml/vpatternimage.cpp b/src/libs/ifc/xml/vpatternimage.cpp index 3b073f730..2318f719c 100644 --- a/src/libs/ifc/xml/vpatternimage.cpp +++ b/src/libs/ifc/xml/vpatternimage.cpp @@ -135,6 +135,29 @@ auto VPatternImage::IsValid() const -> bool return true; } +//--------------------------------------------------------------------------------------------------------------------- +auto VPatternImage::GetPixmap() const -> QPixmap +{ + if (not IsValid()) + { + return {}; + } + + QByteArray array = QByteArray::fromBase64(m_contentData); + QBuffer buffer(&array); + buffer.open(QIODevice::ReadOnly); + + QImageReader imageReader(&buffer); + QImage image = imageReader.read(); + if (image.isNull()) + { + qCritical() << tr("Couldn't read the image. Error: %1").arg(imageReader.errorString()); + return {}; + } + + return QPixmap::fromImage(image); +} + //--------------------------------------------------------------------------------------------------------------------- auto VPatternImage::GetPixmap(int width, int height) const -> QPixmap { diff --git a/src/libs/ifc/xml/vpatternimage.h b/src/libs/ifc/xml/vpatternimage.h index cfbd5eff2..b68b8c7ae 100644 --- a/src/libs/ifc/xml/vpatternimage.h +++ b/src/libs/ifc/xml/vpatternimage.h @@ -50,6 +50,7 @@ public: auto IsNull() const -> bool; auto IsValid() const -> bool; + auto GetPixmap() const -> QPixmap; auto GetPixmap(int width, int height) const -> QPixmap; auto ErrorString() const -> const QString &;