summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEirik Aavitsland <eirik.aavitsland@qt.io>2020-01-27 14:22:19 +0100
committerEirik Aavitsland <eirik.aavitsland@qt.io>2020-01-28 10:21:12 +0100
commit84bc219fb1e9f586c1f7ac2c10172c4da2605f96 (patch)
tree99d22acb9c4924fa6cd7e3bad23aed4273321445 /src
parent69175cddec56c681b7873d6145cc29e461f1d8b1 (diff)
downloadqtsvg-84bc219fb1e9f586c1f7ac2c10172c4da2605f96.tar.gz
Add API to enable opt-in aspect ratio feature in QSvgRenderer
[ChangeLog][QSvgRenderer] Introduced aspect ratio mode property, so that rendering may be set to preserve the view box aspect ratio. Task-number: QTBUG-81259 Change-Id: I9802788cdaf2c30974c1ffc34ad5b67cdc74ed57 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/svg/qsvgrenderer.cpp35
-rw-r--r--src/svg/qsvgrenderer.h4
-rw-r--r--src/svg/qsvgtinydocument.cpp5
-rw-r--r--src/svg/qsvgtinydocument_p.h4
4 files changed, 47 insertions, 1 deletions
diff --git a/src/svg/qsvgrenderer.cpp b/src/svg/qsvgrenderer.cpp
index 40ae6ab..0097ec2 100644
--- a/src/svg/qsvgrenderer.cpp
+++ b/src/svg/qsvgrenderer.cpp
@@ -257,6 +257,41 @@ void QSvgRenderer::setFramesPerSecond(int num)
}
/*!
+ \property QSvgRenderer::aspectRatioMode
+
+ \brief how rendering adheres to the SVG view box aspect ratio
+
+ The accepted modes are:
+ \list
+ \li Qt::IgnoreAspectRatio (the default): the aspect ratio is ignored and the
+ rendering is stretched to the target bounds.
+ \li Qt::KeepAspectRatio: rendering is centered and scaled as large as possible
+ within the target bounds while preserving aspect ratio.
+ \endlist
+
+ \since 5.15
+*/
+
+Qt::AspectRatioMode QSvgRenderer::aspectRatioMode() const
+{
+ Q_D(const QSvgRenderer);
+ if (d->render && d->render->preserveAspectRatio())
+ return Qt::KeepAspectRatio;
+ return Qt::IgnoreAspectRatio;
+}
+
+void QSvgRenderer::setAspectRatioMode(Qt::AspectRatioMode mode)
+{
+ Q_D(QSvgRenderer);
+ if (d->render) {
+ if (mode == Qt::KeepAspectRatio)
+ d->render->setPreserveAspectRatio(true);
+ else if (mode == Qt::IgnoreAspectRatio)
+ d->render->setPreserveAspectRatio(false);
+ }
+}
+
+/*!
\property QSvgRenderer::currentFrame
\brief the current frame of the document's animation, or 0 if the document is not animated
\internal
diff --git a/src/svg/qsvgrenderer.h b/src/svg/qsvgrenderer.h
index c7da2fb..3703b1c 100644
--- a/src/svg/qsvgrenderer.h
+++ b/src/svg/qsvgrenderer.h
@@ -65,6 +65,7 @@ class Q_SVG_EXPORT QSvgRenderer : public QObject
Q_PROPERTY(QRectF viewBox READ viewBoxF WRITE setViewBox)
Q_PROPERTY(int framesPerSecond READ framesPerSecond WRITE setFramesPerSecond)
Q_PROPERTY(int currentFrame READ currentFrame WRITE setCurrentFrame)
+ Q_PROPERTY(Qt::AspectRatioMode aspectRatioMode READ aspectRatioMode WRITE setAspectRatioMode)
public:
QSvgRenderer(QObject *parent = nullptr);
QSvgRenderer(const QString &filename, QObject *parent = nullptr);
@@ -81,6 +82,9 @@ public:
void setViewBox(const QRect &viewbox);
void setViewBox(const QRectF &viewbox);
+ Qt::AspectRatioMode aspectRatioMode() const;
+ void setAspectRatioMode(Qt::AspectRatioMode mode);
+
bool animated() const;
int framesPerSecond() const;
void setFramesPerSecond(int num);
diff --git a/src/svg/qsvgtinydocument.cpp b/src/svg/qsvgtinydocument.cpp
index 8f6aac1..b4b9526 100644
--- a/src/svg/qsvgtinydocument.cpp
+++ b/src/svg/qsvgtinydocument.cpp
@@ -336,6 +336,11 @@ void QSvgTinyDocument::setHeight(int len, bool percent)
m_heightPercent = percent;
}
+void QSvgTinyDocument::setPreserveAspectRatio(bool on)
+{
+ m_preserveAspectRatio = on;
+}
+
void QSvgTinyDocument::setViewBox(const QRectF &rect)
{
m_viewBox = rect;
diff --git a/src/svg/qsvgtinydocument_p.h b/src/svg/qsvgtinydocument_p.h
index d0c5cae..d4b92e8 100644
--- a/src/svg/qsvgtinydocument_p.h
+++ b/src/svg/qsvgtinydocument_p.h
@@ -89,6 +89,7 @@ public:
bool heightPercent() const;
bool preserveAspectRatio() const;
+ void setPreserveAspectRatio(bool on);
QRectF viewBox() const;
void setViewBox(const QRectF &rect);
@@ -128,6 +129,7 @@ private:
mutable bool m_implicitViewBox = true;
mutable QRectF m_viewBox;
+ bool m_preserveAspectRatio = false;
QHash<QString, QSvgRefCounter<QSvgFont> > m_fonts;
QHash<QString, QSvgNode *> m_namedNodes;
@@ -185,7 +187,7 @@ inline QRectF QSvgTinyDocument::viewBox() const
inline bool QSvgTinyDocument::preserveAspectRatio() const
{
- return false;
+ return m_preserveAspectRatio;
}
inline int QSvgTinyDocument::currentElapsed() const