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 "qglobal.h"
#include "utils.h"
#include "../vmisc/compatibility.h"
#include "../vmisc/defglobal.h"
#include <QMimeType>
#include <QDebug>
@ -39,6 +39,7 @@
#include <QBuffer>
#include <QImageReader>
#include <ciso646>
#include <QSvgRenderer>
//---------------------------------------------------------------------------------------------------------------------
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;
}
//---------------------------------------------------------------------------------------------------------------------

View File

@ -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

View File

@ -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();
}

View File

@ -26,6 +26,7 @@
**
*************************************************************************/
#include "vbackgroundsvgitem.h"
#include <QSize>
#include <QStyleOptionGraphicsItem>
#include <QSvgRenderer>
@ -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()));