Fix pattern image proportions in pattern properties.

This commit is contained in:
Roman Telezhynskyi 2023-10-19 09:15:59 +03:00
parent df51f1a873
commit 7d515b08c4
3 changed files with 32 additions and 3 deletions

View File

@ -368,7 +368,7 @@ void DialogPatternProperties::ValidatePassmarkWidth() const
void DialogPatternProperties::InitImage() void DialogPatternProperties::InitImage()
{ {
ui->imageLabel->setContextMenuPolicy(Qt::CustomContextMenu); ui->imageLabel->setContextMenuPolicy(Qt::CustomContextMenu);
ui->imageLabel->setScaledContents(true); ui->imageLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
connect(ui->imageLabel, &QWidget::customContextMenuRequested, this, connect(ui->imageLabel, &QWidget::customContextMenuRequested, this,
[this]() [this]()
{ {
@ -403,7 +403,9 @@ void DialogPatternProperties::InitImage()
const VPatternImage image = m_doc->GetImage(); const VPatternImage image = m_doc->GetImage();
if (image.IsValid()) 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 else
{ {
@ -430,7 +432,10 @@ void DialogPatternProperties::ChangeImage()
} }
m_doc->SetImage(image); 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_deleteAction->setEnabled(true);
m_saveImageAction->setEnabled(true); m_saveImageAction->setEnabled(true);

View File

@ -135,6 +135,29 @@ auto VPatternImage::IsValid() const -> bool
return true; 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 auto VPatternImage::GetPixmap(int width, int height) const -> QPixmap
{ {

View File

@ -50,6 +50,7 @@ public:
auto IsNull() const -> bool; auto IsNull() const -> bool;
auto IsValid() const -> bool; auto IsValid() const -> bool;
auto GetPixmap() const -> QPixmap;
auto GetPixmap(int width, int height) const -> QPixmap; auto GetPixmap(int width, int height) const -> QPixmap;
auto ErrorString() const -> const QString &; auto ErrorString() const -> const QString &;