Fit Valentina's scale and an image resolution.

This commit is contained in:
Roman Telezhynskyi 2022-02-04 16:31:19 +02:00
parent 10a2de6b1d
commit d4f791f0ee
4 changed files with 100 additions and 20 deletions

View File

@ -27,9 +27,9 @@
*************************************************************************/ *************************************************************************/
#include "vbackgroundpatternimage.h" #include "vbackgroundpatternimage.h"
#include "qglobal.h"
#include "utils.h" #include "utils.h"
#include "../vmisc/compatibility.h" #include "../vmisc/compatibility.h"
#include "../vmisc/defglobal.h"
#include <QMimeType> #include <QMimeType>
#include <QDebug> #include <QDebug>
@ -39,6 +39,7 @@
#include <QBuffer> #include <QBuffer>
#include <QImageReader> #include <QImageReader>
#include <ciso646> #include <ciso646>
#include <QSvgRenderer>
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
auto VBackgroundPatternImage::FromFile(const QString &fileName, bool builtIn) -> VBackgroundPatternImage auto VBackgroundPatternImage::FromFile(const QString &fileName, bool builtIn) -> VBackgroundPatternImage
@ -90,6 +91,7 @@ void VBackgroundPatternImage::SetContentData(const QByteArray &newContentData, c
m_contentData = newContentData; m_contentData = newContentData;
m_contentType = newContentType; m_contentType = newContentType;
m_filePath.clear(); m_filePath.clear();
m_size = QSize();
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -181,6 +183,7 @@ void VBackgroundPatternImage::SetFilePath(const QString &newFilePath)
m_filePath = newFilePath; m_filePath = newFilePath;
m_contentData.clear(); m_contentData.clear();
m_contentType.clear(); m_contentType.clear();
m_size = QSize();
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -239,21 +242,66 @@ auto VBackgroundPatternImage::Size() const -> QSize
return {}; 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()) return m_size;
{
QByteArray array = QByteArray::fromBase64(m_contentData);
QBuffer buffer(&array);
buffer.open(QIODevice::ReadOnly);
return QImageReader(&buffer).size();
}
return {};
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------

View File

@ -47,7 +47,7 @@ enum class PatternImage
class VBackgroundPatternImage class VBackgroundPatternImage
{ {
Q_DECLARE_TR_FUNCTIONS(VBackgroundPatternImage) Q_DECLARE_TR_FUNCTIONS(VBackgroundPatternImage) // NOLINT
public: public:
VBackgroundPatternImage() = default; VBackgroundPatternImage() = default;
@ -79,7 +79,6 @@ public:
void SetId(const QUuid &newId); void SetId(const QUuid &newId);
auto Size() const -> QSize; auto Size() const -> QSize;
void SetSize(const QSize &newSize);
auto ErrorString() const -> const QString &; auto ErrorString() const -> const QString &;
@ -108,6 +107,7 @@ private:
bool m_hold{false}; bool m_hold{false};
bool m_visible{true}; bool m_visible{true};
qreal m_opacity{1.0}; qreal m_opacity{1.0};
mutable QSize m_size{};
}; };
#endif // VBACKGROUNDPATTERNIMAGE_H #endif // VBACKGROUNDPATTERNIMAGE_H

View File

@ -213,11 +213,30 @@ auto VBackgroundPixmapItem::Pixmap() const -> QPixmap
return m_pixmap; 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()) if (not image.FilePath().isEmpty())
{ {
QImageReader imageReader(image.FilePath()); QImageReader imageReader(image.FilePath());
m_pixmap = QPixmap::fromImageReader(&imageReader); QImage image = imageReader.read();
if (m_pixmap.isNull()) if (not image.isNull())
{
m_pixmap = QPixmap::fromImage(ScaleImage(image));
if (m_pixmap.isNull())
{
m_pixmap = InvalidImage();
}
}
else
{ {
m_pixmap = InvalidImage(); m_pixmap = InvalidImage();
} }
@ -232,8 +251,16 @@ auto VBackgroundPixmapItem::Pixmap() const -> QPixmap
buffer.open(QIODevice::ReadOnly); buffer.open(QIODevice::ReadOnly);
QImageReader imageReader(&buffer); QImageReader imageReader(&buffer);
m_pixmap = QPixmap::fromImageReader(&imageReader); QImage image = imageReader.read();
if (m_pixmap.isNull()) if (not image.isNull())
{
m_pixmap = QPixmap::fromImage(ScaleImage(image));
if (m_pixmap.isNull())
{
m_pixmap = InvalidImage();
}
}
else
{ {
m_pixmap = InvalidImage(); m_pixmap = InvalidImage();
} }

View File

@ -26,6 +26,7 @@
** **
*************************************************************************/ *************************************************************************/
#include "vbackgroundsvgitem.h" #include "vbackgroundsvgitem.h"
#include <QSize>
#include <QStyleOptionGraphicsItem> #include <QStyleOptionGraphicsItem>
#include <QSvgRenderer> #include <QSvgRenderer>
@ -52,7 +53,10 @@ VBackgroundSVGItem::~VBackgroundSVGItem()
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
auto VBackgroundSVGItem::boundingRect() const -> QRectF 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->save();
painter->setTransform(Image().Matrix(), true); painter->setTransform(Image().Matrix(), true);
painter->setOpacity(Image().Opacity()); painter->setOpacity(Image().Opacity());
painter->scale(PrintDPI / 90., PrintDPI / 90.);
renderer->render(painter, QRectF(QPointF(0, 0), renderer->defaultSize())); renderer->render(painter, QRectF(QPointF(0, 0), renderer->defaultSize()));