From d4f791f0ee2825eb9d6ee749266d8eacbe550460 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Fri, 4 Feb 2022 16:31:19 +0200 Subject: [PATCH] Fit Valentina's scale and an image resolution. --- src/libs/ifc/xml/vbackgroundpatternimage.cpp | 74 +++++++++++++++---- src/libs/ifc/xml/vbackgroundpatternimage.h | 4 +- .../backgroundimage/vbackgroundpixmapitem.cpp | 35 ++++++++- .../backgroundimage/vbackgroundsvgitem.cpp | 7 +- 4 files changed, 100 insertions(+), 20 deletions(-) diff --git a/src/libs/ifc/xml/vbackgroundpatternimage.cpp b/src/libs/ifc/xml/vbackgroundpatternimage.cpp index 5e1f331f4..5cde8b905 100644 --- a/src/libs/ifc/xml/vbackgroundpatternimage.cpp +++ b/src/libs/ifc/xml/vbackgroundpatternimage.cpp @@ -27,9 +27,9 @@ *************************************************************************/ #include "vbackgroundpatternimage.h" -#include "qglobal.h" #include "utils.h" #include "../vmisc/compatibility.h" +#include "../vmisc/defglobal.h" #include #include @@ -39,6 +39,7 @@ #include #include #include +#include //--------------------------------------------------------------------------------------------------------------------- auto VBackgroundPatternImage::FromFile(const QString &fileName, bool builtIn) -> VBackgroundPatternImage @@ -90,6 +91,7 @@ void VBackgroundPatternImage::SetContentData(const QByteArray &newContentData, c m_contentData = newContentData; m_contentType = newContentType; m_filePath.clear(); + m_size = QSize(); } //--------------------------------------------------------------------------------------------------------------------- @@ -181,6 +183,7 @@ void VBackgroundPatternImage::SetFilePath(const QString &newFilePath) m_filePath = newFilePath; m_contentData.clear(); m_contentType.clear(); + m_size = QSize(); } //--------------------------------------------------------------------------------------------------------------------- @@ -239,21 +242,66 @@ auto VBackgroundPatternImage::Size() const -> QSize return {}; } - if (not m_filePath.isEmpty()) + if (not m_size.isValid()) { - return QImageReader(m_filePath).size(); + auto ScaleRasterImage = [](QImageReader &imageReader) + { + const QImage image = imageReader.read(); + const double ratioX = PrintDPI / (image.dotsPerMeterX() / 100. * 2.54); + const double ratioY = PrintDPI / (image.dotsPerMeterY() / 100. * 2.54); + const QSize imageSize = image.size(); + return QSize(qRound(imageSize.width()*ratioX), qRound(imageSize.height()*ratioY)); + }; + + auto ScaleVectorImage = [](const QSvgRenderer &renderer) + { + const QSize imageSize = renderer.defaultSize(); + constexpr double ratio = PrintDPI / 90.; + return QSize(qRound(imageSize.width()*ratio), qRound(imageSize.height()*ratio)); + }; + + if (not m_filePath.isEmpty()) + { + if (Type() == PatternImage::Raster) + { + QImageReader imageReader(m_filePath); + m_size = ScaleRasterImage(imageReader); + return m_size; + } + + if (Type() == PatternImage::Vector) + { + QSvgRenderer renderer; + renderer.load(m_filePath); + m_size = ScaleVectorImage(renderer); + return m_size; + } + } + + if (not m_contentData.isEmpty()) + { + QByteArray array = QByteArray::fromBase64(m_contentData); + + if (Type() == PatternImage::Raster) + { + QBuffer buffer(&array); + buffer.open(QIODevice::ReadOnly); + QImageReader imageReader(&buffer); + m_size = ScaleRasterImage(imageReader); + return m_size; + } + + if (Type() == PatternImage::Vector) + { + QSvgRenderer renderer; + renderer.load(array); + m_size = ScaleVectorImage(renderer); + return m_size; + } + } } - if (not m_contentData.isEmpty()) - { - QByteArray array = QByteArray::fromBase64(m_contentData); - QBuffer buffer(&array); - buffer.open(QIODevice::ReadOnly); - - return QImageReader(&buffer).size(); - } - - return {}; + return m_size; } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/ifc/xml/vbackgroundpatternimage.h b/src/libs/ifc/xml/vbackgroundpatternimage.h index b0ce4ae5c..fbd60932a 100644 --- a/src/libs/ifc/xml/vbackgroundpatternimage.h +++ b/src/libs/ifc/xml/vbackgroundpatternimage.h @@ -47,7 +47,7 @@ enum class PatternImage class VBackgroundPatternImage { - Q_DECLARE_TR_FUNCTIONS(VBackgroundPatternImage) + Q_DECLARE_TR_FUNCTIONS(VBackgroundPatternImage) // NOLINT public: VBackgroundPatternImage() = default; @@ -79,7 +79,6 @@ public: void SetId(const QUuid &newId); auto Size() const -> QSize; - void SetSize(const QSize &newSize); auto ErrorString() const -> const QString &; @@ -108,6 +107,7 @@ private: bool m_hold{false}; bool m_visible{true}; qreal m_opacity{1.0}; + mutable QSize m_size{}; }; #endif // VBACKGROUNDPATTERNIMAGE_H diff --git a/src/libs/vtools/tools/backgroundimage/vbackgroundpixmapitem.cpp b/src/libs/vtools/tools/backgroundimage/vbackgroundpixmapitem.cpp index fcf8c21a5..71f6ddf50 100644 --- a/src/libs/vtools/tools/backgroundimage/vbackgroundpixmapitem.cpp +++ b/src/libs/vtools/tools/backgroundimage/vbackgroundpixmapitem.cpp @@ -213,11 +213,30 @@ auto VBackgroundPixmapItem::Pixmap() const -> QPixmap return m_pixmap; } + // Scale to Valentina resolution + auto ScaleImage = [](const QImage &image) + { + const double ratioX = PrintDPI / (image.dotsPerMeterX() / 100. * 2.54); + const double ratioY = PrintDPI / (image.dotsPerMeterY() / 100. * 2.54); + const QSize imageSize = image.size(); + return image.scaled(qRound(imageSize.width()*ratioX), + qRound(imageSize.height()*ratioY), + Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + }; + if (not image.FilePath().isEmpty()) { QImageReader imageReader(image.FilePath()); - m_pixmap = QPixmap::fromImageReader(&imageReader); - if (m_pixmap.isNull()) + QImage image = imageReader.read(); + if (not image.isNull()) + { + m_pixmap = QPixmap::fromImage(ScaleImage(image)); + if (m_pixmap.isNull()) + { + m_pixmap = InvalidImage(); + } + } + else { m_pixmap = InvalidImage(); } @@ -232,8 +251,16 @@ auto VBackgroundPixmapItem::Pixmap() const -> QPixmap buffer.open(QIODevice::ReadOnly); QImageReader imageReader(&buffer); - m_pixmap = QPixmap::fromImageReader(&imageReader); - if (m_pixmap.isNull()) + QImage image = imageReader.read(); + if (not image.isNull()) + { + m_pixmap = QPixmap::fromImage(ScaleImage(image)); + if (m_pixmap.isNull()) + { + m_pixmap = InvalidImage(); + } + } + else { m_pixmap = InvalidImage(); } diff --git a/src/libs/vtools/tools/backgroundimage/vbackgroundsvgitem.cpp b/src/libs/vtools/tools/backgroundimage/vbackgroundsvgitem.cpp index b6c5b15db..d45eac598 100644 --- a/src/libs/vtools/tools/backgroundimage/vbackgroundsvgitem.cpp +++ b/src/libs/vtools/tools/backgroundimage/vbackgroundsvgitem.cpp @@ -26,6 +26,7 @@ ** *************************************************************************/ #include "vbackgroundsvgitem.h" +#include #include #include @@ -52,7 +53,10 @@ VBackgroundSVGItem::~VBackgroundSVGItem() //--------------------------------------------------------------------------------------------------------------------- auto VBackgroundSVGItem::boundingRect() const -> QRectF { - return Image().Matrix().mapRect(QRectF(QPointF(0, 0), Renderer()->defaultSize())); + QSize size = Renderer()->defaultSize(); + constexpr double ratio = PrintDPI / 90.; + size = QSize(qRound(size.width()*ratio), qRound(size.height()*ratio)); + return Image().Matrix().mapRect(QRectF(QPointF(0, 0), size)); } //--------------------------------------------------------------------------------------------------------------------- @@ -68,6 +72,7 @@ void VBackgroundSVGItem::paint(QPainter *painter, const QStyleOptionGraphicsItem painter->save(); painter->setTransform(Image().Matrix(), true); painter->setOpacity(Image().Opacity()); + painter->scale(PrintDPI / 90., PrintDPI / 90.); renderer->render(painter, QRectF(QPointF(0, 0), renderer->defaultSize()));