8#include <private/qsgcurveprocessor_p.h>
9#include <private/qquickshape_p.h>
10#include <private/qquadpath_p.h>
11#include <private/qquickitem_p.h>
12#include <private/qquickimagebase_p_p.h>
13#include <private/qquicktext_p.h>
14#include <private/qquicktranslate_p.h>
15#include <private/qquickimage_p.h>
17#include <QtCore/qloggingcategory.h>
18#include <QtCore/qdir.h>
19#include <QtCore/qstandardpaths.h>
23using namespace Qt::StringLiterals;
28 s.replace(QLatin1Char(
'\\'), QLatin1String(
"\\\\"));
29 s.replace(QLatin1Char(
'"'), QLatin1String(
"\\\""));
34QQuickAnimatedProperty::PropertyAnimation QQuickAnimatedProperty::PropertyAnimation::simplified()
const
36 QQuickAnimatedProperty::PropertyAnimation res = *
this;
37 int consecutiveEquals = 0;
38 int prevTimePoint = -1;
40 for (
const auto &[timePoint, value] : frames.asKeyValueRange()) {
41 if (value != prevValue) {
42 consecutiveEquals = 1;
44 }
else if (consecutiveEquals < 2) {
48 res.frames.remove(prevTimePoint);
49 res.easingPerFrame.remove(prevTimePoint);
51 prevTimePoint = timePoint;
57QQuickQmlGenerator::QQuickQmlGenerator(
const QString fileName, QQuickVectorImageGenerator::GeneratorFlags flags,
const QString &outFileName)
58 : QQuickGenerator(fileName, flags)
59 , outputFileName(outFileName)
61 m_result.open(QIODevice::ReadWrite);
62 m_oldIndentLevels.push(0);
64 if (outFileName.isEmpty()) {
65 setRetainFilePaths(
true);
66 setAssetFileDirectory(QStandardPaths::writableLocation(QStandardPaths::TempLocation));
67 setAssetFilePrefix(QStringLiteral(
"_qt_vectorimage_"));
68 setUrlPrefix(QStringLiteral(
"file:"));
72QQuickQmlGenerator::~QQuickQmlGenerator()
76bool QQuickQmlGenerator::save()
78 if (Q_UNLIKELY(errorState()))
82 if (!outputFileName.isEmpty()) {
83 QFileInfo fileInfo(outputFileName);
84 QDir dir(fileInfo.absolutePath());
85 if (!dir.exists() && !dir.mkpath(QStringLiteral(
"."))) {
86 qCWarning(lcQuickVectorImage) <<
"Failed to create path" << dir.absolutePath();
89 QFile outFile(outputFileName);
90 if (outFile.open(QIODevice::WriteOnly)) {
91 outFile.write(m_result.data());
94 qCWarning(lcQuickVectorImage) <<
"Failed to write to file" << outFile.fileName();
100 if (lcQuickVectorImage().isDebugEnabled())
101 qCDebug(lcQuickVectorImage).noquote() << m_result.data().left(300);
106void QQuickQmlGenerator::setShapeTypeName(
const QString &name)
108 m_shapeTypeName = name.toLatin1();
111QString QQuickQmlGenerator::shapeTypeName()
const
113 return QString::fromLatin1(m_shapeTypeName);
116void QQuickQmlGenerator::setCommentString(
const QString commentString)
118 m_commentString = commentString;
121QString QQuickQmlGenerator::commentString()
const
123 return m_commentString;
126QString QQuickQmlGenerator::generateNodeBase(
const NodeInfo &info,
const QString &idSuffix)
128 static qint64 maxNodes = qEnvironmentVariableIntegerValue(
"QT_QUICKVECTORIMAGE_MAX_NODES").value_or(10000);
129 if (Q_UNLIKELY(!checkSanityLimit(++m_nodeCounter, maxNodes,
"nodes"_L1)))
132 if (!info.nodeId.isEmpty())
133 stream() <<
"objectName: \"" << info.nodeId <<
"\"";
135 if (!info.id.isEmpty())
136 stream() <<
"id: " << info.id << idSuffix;
138 if (!info.bounds.isNull() || !info.boundsReferenceId.isEmpty()) {
139 stream() <<
"property var originalBounds: ";
140 if (!info.bounds.isNull()) {
141 stream(SameLine) <<
"Qt.rect(" << info.bounds.x() <<
", " << info.bounds.y() <<
", "
142 << info.bounds.width() <<
", " << info.bounds.height() <<
")";
144 stream(SameLine) << info.boundsReferenceId <<
".originalBounds";
146 stream() <<
"width: originalBounds.width";
147 stream() <<
"height: originalBounds.height";
150 stream() <<
"transformOrigin: Item.TopLeft";
152 if (info.filterId.isEmpty() && info.maskId.isEmpty()) {
153 if (!info.isDefaultOpacity)
154 stream() <<
"opacity: " << info.opacity.defaultValue().toReal();
155 generateItemAnimations(info.id, info);
161void QQuickQmlGenerator::generateNodeEnd(
const NodeInfo &info)
163 if (Q_UNLIKELY(errorState()))
167 generateShaderUse(info);
170void QQuickQmlGenerator::generateItemAnimations(
const QString &idString,
const NodeInfo &info)
172 const bool hasTransform = info.transform.isAnimated()
173 || !info.maskId.isEmpty()
174 || !info.filterId.isEmpty()
175 || !info.isDefaultTransform
176 || !info.transformReferenceId.isEmpty()
177 || info.motionPath.isAnimated();
180 stream() <<
"transform: TransformGroup {";
183 bool hasNonConstantTransform =
false;
184 int earliestOverrideGroup = -1;
186 if (!idString.isEmpty()) {
187 stream() <<
"id: " << idString <<
"_transform_base_group";
189 if (!info.maskId.isEmpty() || !info.filterId.isEmpty())
190 stream() <<
"Translate { x: " << idString <<
".sourceX; y: " << idString <<
".sourceY }";
192 if (info.transform.isAnimated()) {
193 for (
int groupIndex = 0; groupIndex < info.transform.animationGroupCount(); ++groupIndex) {
194 stream() <<
"TransformGroup {";
197 if (!idString.isEmpty())
198 stream() <<
"id: " << idString <<
"_transform_group_" << groupIndex;
200 int animationStart = info.transform.animationGroup(groupIndex);
201 int nextAnimationStart = groupIndex + 1 < info.transform.animationGroupCount()
202 ? info.transform.animationGroup(groupIndex + 1)
203 : info.transform.animationCount();
205 const QQuickAnimatedProperty::PropertyAnimation &firstAnimation = info.transform.animation(animationStart);
206 const bool replace = firstAnimation.flags & QQuickAnimatedProperty::PropertyAnimation::ReplacePreviousAnimations;
207 if (replace && earliestOverrideGroup < 0)
208 earliestOverrideGroup = groupIndex;
210 for (
int i = nextAnimationStart - 1; i >= animationStart; --i) {
211 const QQuickAnimatedProperty::PropertyAnimation &animation = info.transform.animation(i);
212 if (animation.frames.isEmpty())
215 const QVariantList ¶meters = animation.frames.first().value<QVariantList>();
216 switch (animation.subtype) {
217 case QTransform::TxTranslate:
218 if (animation.isConstant()) {
219 const QPointF translation = parameters.value(0).value<QPointF>();
220 if (!translation.isNull())
221 stream() <<
"Translate { x: " << translation.x() <<
"; y: " << translation.y() <<
" }";
223 hasNonConstantTransform =
true;
224 stream() <<
"Translate { id: " << idString <<
"_transform_" << groupIndex <<
"_" << i <<
" }";
227 case QTransform::TxScale:
228 if (animation.isConstant()) {
229 const QPointF scale = parameters.value(0).value<QPointF>();
230 if (scale != QPointF(1, 1))
231 stream() <<
"Scale { xScale: " << scale.x() <<
"; yScale: " << scale.y() <<
" }";
233 hasNonConstantTransform =
true;
234 stream() <<
"Scale { id: " << idString <<
"_transform_" << groupIndex <<
"_" << i <<
"}";
237 case QTransform::TxRotate:
238 if (animation.isConstant()) {
239 const QPointF center = parameters.value(0).value<QPointF>();
240 const qreal angle = parameters.value(1).toReal();
241 if (!qFuzzyIsNull(angle))
242 stream() <<
"Rotation { angle: " << angle <<
"; origin.x: " << center.x() <<
"; origin.y: " << center.y() <<
" }";
244 hasNonConstantTransform =
true;
245 stream() <<
"Rotation { id: " << idString <<
"_transform_" << groupIndex <<
"_" << i <<
" }";
248 case QTransform::TxShear:
249 if (animation.isConstant()) {
250 const QPointF skew = parameters.value(0).value<QPointF>();
252 stream() <<
"Shear { xAngle: " << skew.x() <<
"; yAngle: " << skew.y() <<
" }";
254 hasNonConstantTransform =
true;
255 stream() <<
"Shear { id: " << idString <<
"_transform_" << groupIndex <<
"_" << i <<
" }";
268 if (info.motionPath.isAnimated()) {
269 QVariantList defaultProps = info.motionPath.defaultValue().value<QVariantList>();
270 const bool adaptAngle = defaultProps.value(1).toBool();
271 const qreal baseRotation = defaultProps.value(2).toReal();
272 QString interpolatorId = idString + QStringLiteral(
"_motion_interpolator");
273 if (adaptAngle || !qFuzzyIsNull(baseRotation)) {
274 stream() <<
"Rotation {";
278 stream() <<
"angle: " << interpolatorId <<
".angle";
279 if (!qFuzzyIsNull(baseRotation))
280 stream(SameLine) <<
" + " << baseRotation;
282 stream() <<
"angle: " << baseRotation;
289 stream() <<
"Translate {";
292 stream() <<
"x: " << interpolatorId <<
".x";
293 stream() <<
"y: " << interpolatorId <<
".y";
300 if (!info.isDefaultTransform) {
301 QTransform xf = info.transform.defaultValue().value<QTransform>();
302 if (xf.type() <= QTransform::TxTranslate) {
303 stream() <<
"Translate { x: " << xf.dx() <<
"; y: " << xf.dy() <<
"}";
305 stream() <<
"Matrix4x4 { matrix: ";
306 generateTransform(xf);
307 stream(SameLine) <<
"}";
311 if (!info.transformReferenceId.isEmpty())
312 stream() <<
"Matrix4x4 { matrix: " << info.transformReferenceId <<
".transformMatrix }";
317 if (hasNonConstantTransform) {
318 generateAnimateTransform(idString, info);
319 }
else if (info.transform.isAnimated() && earliestOverrideGroup >= 0) {
322 stream() <<
"Component.onCompleted: {";
325 stream() << idString <<
"_transform_base_group.activateOverride("
326 << idString <<
"_transform_group_" << earliestOverrideGroup <<
")";
333 generateAnimateMotionPath(idString, info.motionPath);
335 generatePropertyAnimation(info.opacity, idString, QStringLiteral(
"opacity"));
338void QQuickQmlGenerator::generateShaderUse(
const NodeInfo &info)
340 const bool hasMask = !info.maskId.isEmpty();
341 const bool hasFilters = !info.filterId.isEmpty();
342 if (!hasMask && !hasFilters)
345 const QString effectId = hasFilters
346 ? info.filterId + QStringLiteral(
"_") + info.id + QStringLiteral(
"_effect")
349 QString animatedItemId;
351 stream() <<
"ShaderEffectSource {";
354 const QString seId = info.id + QStringLiteral(
"_se");
355 stream() <<
"id: " << seId;
357 stream() <<
"ItemSpy {";
359 stream() <<
"id: " << info.id <<
"_itemspy";
360 stream() <<
"anchors.fill: parent";
364 stream() <<
"hideSource: true";
365 stream() <<
"wrapMode: " << info.filterId <<
"_filterParameters.wrapMode";
366 stream() <<
"sourceItem: " << info.id;
367 stream() <<
"sourceRect: " << info.filterId
368 <<
"_filterParameters.adaptToFilterRect("
369 << info.id <<
".originalBounds.x, "
370 << info.id <<
".originalBounds.y, "
371 << info.id <<
".originalBounds.width, "
372 << info.id <<
".originalBounds.height)";
373 stream() <<
"textureSize: " << info.id <<
"_itemspy.requiredTextureSize";
374 stream() <<
"width: sourceRect.width";
375 stream() <<
"height: sourceRect.height";
376 stream() <<
"visible: false";
381 stream() <<
"Loader {";
384 animatedItemId = effectId;
385 stream() <<
"id: " << effectId;
387 stream() <<
"property var filterSourceItem: " << seId;
388 stream() <<
"sourceComponent: " << info.filterId <<
"_container";
389 stream() <<
"property real sourceX: " << info.id <<
".originalBounds.x";
390 stream() <<
"property real sourceY: " << info.id <<
".originalBounds.y";
391 stream() <<
"width: " << info.id <<
".originalBounds.width";
392 stream() <<
"height: " << info.id <<
".originalBounds.height";
402 stream() <<
"ShaderEffectSource {";
405 const QString maskId = info.maskId + QStringLiteral(
"_") + info.id + QStringLiteral(
"_mask");
406 stream() <<
"id: " << maskId;
407 stream() <<
"sourceItem: " << info.maskId;
408 stream() <<
"visible: false";
409 stream() <<
"hideSource: true";
411 stream() <<
"ItemSpy {";
413 stream() <<
"id: " << maskId <<
"_itemspy";
414 stream() <<
"anchors.fill: parent";
417 stream() <<
"textureSize: " << maskId <<
"_itemspy.requiredTextureSize";
419 stream() <<
"sourceRect: " << info.maskId <<
".maskRect("
420 << info.id <<
".originalBounds.x,"
421 << info.id <<
".originalBounds.y,"
422 << info.id <<
".originalBounds.width,"
423 << info.id <<
".originalBounds.height)";
425 stream() <<
"width: sourceRect.width";
426 stream() <<
"height: sourceRect.height";
432 stream() <<
"ShaderEffectSource {";
435 const QString seId = info.id + QStringLiteral(
"_masked_se");
436 stream() <<
"id: " << seId;
438 stream() <<
"ItemSpy {";
440 stream() <<
"id: " << info.id <<
"_masked_se_itemspy";
441 stream() <<
"anchors.fill: parent";
445 stream() <<
"hideSource: true";
447 stream() <<
"sourceItem: " << effectId;
449 stream() <<
"sourceItem: " << info.id;
450 stream() <<
"textureSize: " << info.id <<
"_masked_se_itemspy.requiredTextureSize";
452 stream() <<
"sourceRect: " << info.maskId <<
".maskRect("
453 << info.id <<
".originalBounds.x,"
454 << info.id <<
".originalBounds.y,"
455 << info.id <<
".originalBounds.width,"
456 << info.id <<
".originalBounds.height)";
458 stream() <<
"sourceRect: " << info.maskId <<
".maskRect(0, 0,"
459 << info.id <<
".originalBounds.width,"
460 << info.id <<
".originalBounds.height)";
462 stream() <<
"width: sourceRect.width";
463 stream() <<
"height: sourceRect.height";
464 stream() <<
"smooth: false";
465 stream() <<
"visible: false";
470 stream() <<
"ShaderEffect {";
473 const QString maskShaderId = maskId + QStringLiteral(
"_se");
474 animatedItemId = maskShaderId;
476 stream() <<
"id:" << maskShaderId;
478 stream() <<
"property real sourceX: " << maskId <<
".sourceRect.x";
479 stream() <<
"property real sourceY: " << maskId <<
".sourceRect.y";
480 stream() <<
"width: " << maskId <<
".sourceRect.width";
481 stream() <<
"height: " << maskId <<
".sourceRect.height";
483 stream() <<
"fragmentShader: \"qrc:/qt-project.org/quickvectorimage/helpers/shaders_ng/genericmask.frag.qsb\"";
484 stream() <<
"property var source: " << seId;
485 stream() <<
"property var maskSource: " << maskId;
486 stream() <<
"property bool isAlpha: " << (info.isMaskAlpha ?
"true" :
"false");
487 stream() <<
"property bool isInverted: " << (info.isMaskInverted ?
"true" :
"false");
490 if (!info.isDefaultOpacity)
491 stream() <<
"opacity: " << info.opacity.defaultValue().toReal();
493 generateItemAnimations(animatedItemId, info);
499bool QQuickQmlGenerator::generateDefsNode(
const StructureNodeInfo &info)
501 if (Q_UNLIKELY(errorState()))
504 if (info.stage == StructureNodeStage::Start) {
505 m_oldIndentLevels.push(m_indentLevel);
507 stream() <<
"Component {";
510 stream() <<
"id: " << info.id <<
"_container";
512 stream() <<
"Item {";
515 generateTimelineFields(info);
516 if (!info.transformReferenceChildId.isEmpty()) {
517 stream() <<
"property alias transformMatrix: "
518 << info.transformReferenceChildId <<
".transformMatrix";
521 generateNodeBase(info, QStringLiteral(
"_defs"));
523 generateNodeEnd(info);
528 stream() << m_defsSuffix;
529 m_defsSuffix.clear();
531 m_indentLevel = m_oldIndentLevels.pop();
537void QQuickQmlGenerator::generateImageNode(
const ImageNodeInfo &info)
539 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
542 const QFileInfo outputFileInfo(outputFileName);
543 const QDir outputDir(outputFileInfo.absolutePath());
547 if (!m_retainFilePaths || info.externalFileReference.isEmpty()) {
548 filePath = m_assetFileDirectory;
549 if (filePath.isEmpty())
550 filePath = outputDir.absolutePath();
552 if (!filePath.isEmpty() && !filePath.endsWith(u'/'))
555 QDir fileDir(filePath);
556 if (!fileDir.exists()) {
557 if (!fileDir.mkpath(QStringLiteral(
".")))
558 qCWarning(lcQuickVectorImage) <<
"Failed to create image resource directory:" << filePath;
561 filePath += QStringLiteral(
"%1%2.png").arg(m_assetFilePrefix.isEmpty()
562 ? QStringLiteral(
"svg_asset_")
564 .arg(info.image.cacheKey());
566 if (!info.image.save(filePath))
567 qCWarning(lcQuickVectorImage) <<
"Unabled to save image resource" << filePath;
568 qCDebug(lcQuickVectorImage) <<
"Saving copy of IMAGE" << filePath;
570 filePath = info.externalFileReference;
573 const QFileInfo assetFileInfo(filePath);
575 stream() <<
"Image {";
578 generateNodeBase(info);
579 stream() <<
"x: " << info.rect.x();
580 stream() <<
"y: " << info.rect.y();
581 stream() <<
"width: " << info.rect.width();
582 stream() <<
"height: " << info.rect.height();
583 stream() <<
"source: \"" << m_urlPrefix << outputDir.relativeFilePath(assetFileInfo.absoluteFilePath()) <<
"\"";
584 generateNodeEnd(info);
587void QQuickQmlGenerator::generateMarkers(
const PathNodeInfo &info)
589 const QPainterPath path = info.path.defaultValue().value<QPainterPath>();
590 for (
int i = 0; i < path.elementCount(); ++i) {
591 const QPainterPath::Element element = path.elementAt(i);
596 auto getMeanAngle = [](QPointF p0, QPointF p1, QPointF p2) -> qreal {
597 QPointF t1 = p1 - p0;
598 QPointF t2 = p2 - p1;
599 qreal hyp1 = hypot(t1.x(), t1.y());
604 qreal hyp2 = hypot(t2.x(), t2.y());
609 QPointF tangent = t1 + t2;
610 return -atan2(tangent.y(), tangent.x()) / M_PI * 180.;
614 markerId = info.markerStartId;
615 angle = path.angleAtPercent(0.0);
616 }
else if (i == path.elementCount() - 1) {
617 markerId = info.markerEndId;
618 angle = path.angleAtPercent(1.0);
619 }
else if (path.elementAt(i + 1).type != QPainterPath::CurveToDataElement) {
620 markerId = info.markerMidId;
622 const QPainterPath::Element prevElement = path.elementAt(i - 1);
623 const QPainterPath::Element nextElement = path.elementAt(i + 1);
625 QPointF p1(prevElement.x, prevElement.y);
626 QPointF p2(element.x, element.y);
627 QPointF p3(nextElement.x, nextElement.y);
629 angle = getMeanAngle(p1, p2, p3);
632 if (!markerId.isEmpty()) {
633 stream() <<
"Loader {";
637 stream() <<
"sourceComponent: " << markerId <<
"_container";
638 stream() <<
"property real strokeWidth: " << info.strokeStyle.width.defaultValue().toReal();
639 stream() <<
"transform: [";
642 stream() <<
"Scale { "
643 <<
"xScale: " << markerId <<
"_markerParameters.startReversed ? -1 : 1; "
644 <<
"yScale: " << markerId <<
"_markerParameters.startReversed ? -1 : 1 },";
646 stream() <<
"Rotation { angle: " << markerId <<
"_markerParameters.autoAngle(" << -angle <<
") },";
647 stream() <<
"Translate { x: " << element.x <<
"; y: " << element.y <<
"}";
658void QQuickQmlGenerator::generatePath(
const PathNodeInfo &info,
const QRectF &overrideBoundingRect)
660 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
663 if (m_inShapeItemLevel > 0) {
664 if (!info.isDefaultTransform)
665 qWarning() <<
"Skipped transform for node" << info.nodeId <<
"type" << info.typeName <<
"(this is not supposed to happen)";
666 optimizePaths(info, overrideBoundingRect);
668 m_inShapeItemLevel++;
669 stream() << shapeName() <<
" {";
672 generateNodeBase(info);
674 if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::CurveRenderer))
675 stream() <<
"preferredRendererType: Shape.CurveRenderer";
676 if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::AsyncShapes))
677 stream() <<
"asynchronous: true";
678 optimizePaths(info, overrideBoundingRect);
681 if (!info.markerStartId.isEmpty()
682 || !info.markerMidId.isEmpty()
683 || !info.markerEndId.isEmpty()) {
684 generateMarkers(info);
687 generateNodeEnd(info);
688 m_inShapeItemLevel--;
692void QQuickQmlGenerator::generateGradient(
const QGradient *grad,
693 const QString &propertyName,
694 const QRectF &coordinateConversion)
696 const QSizeF &scale = coordinateConversion.size();
697 const QPointF &translation = coordinateConversion.topLeft();
699 if (grad->type() == QGradient::LinearGradient) {
700 auto *linGrad =
static_cast<
const QLinearGradient *>(grad);
701 stream() << propertyName <<
": LinearGradient {";
704 QRectF gradRect(linGrad->start(), linGrad->finalStop());
706 stream() <<
"x1: " << (gradRect.left() * scale.width()) + translation.x();
707 stream() <<
"y1: " << (gradRect.top() * scale.height()) + translation.y();
708 stream() <<
"x2: " << (gradRect.right() * scale.width()) + translation.x();
709 stream() <<
"y2: " << (gradRect.bottom() * scale.height()) + translation.y();
710 for (
auto &stop : linGrad->stops())
711 stream() <<
"GradientStop { position: " << QString::number(stop.first,
'g', 7)
712 <<
"; color: \"" << stop.second.name(QColor::HexArgb) <<
"\" }";
713 }
else if (grad->type() == QGradient::RadialGradient) {
714 auto *radGrad =
static_cast<
const QRadialGradient*>(grad);
715 stream() << propertyName <<
": RadialGradient {";
718 stream() <<
"centerX: " << (radGrad->center().x() * scale.width()) + translation.x();
719 stream() <<
"centerY: " << (radGrad->center().y() * scale.height()) + translation.y();
720 stream() <<
"centerRadius: " << (radGrad->radius() * scale.width());
721 stream() <<
"focalX:" << (radGrad->focalPoint().x() * scale.width()) + translation.x();
722 stream() <<
"focalY:" << (radGrad->focalPoint().y() * scale.height()) + translation.y();
723 for (
auto &stop : radGrad->stops())
724 stream() <<
"GradientStop { position: " << QString::number(stop.first,
'g', 7)
725 <<
"; color: \"" << stop.second.name(QColor::HexArgb) <<
"\" }";
728 stream() <<
"spread: ShapeGradient.";
729 switch (grad->spread()) {
730 case QGradient::PadSpread:
731 stream(SameLine) <<
"PadSpread";
733 case QGradient::ReflectSpread:
734 stream(SameLine) <<
"ReflectSpread";
736 case QGradient::RepeatSpread:
737 stream(SameLine) <<
"RepeatSpread";
745void QQuickQmlGenerator::generateAnimationBindings()
748 if (Q_UNLIKELY(!isRuntimeGenerator()))
749 prefix = QStringLiteral(
".animations");
751 stream() <<
"loops: " << m_topLevelIdString << prefix <<
".loops";
752 stream() <<
"paused: " << m_topLevelIdString << prefix <<
".paused";
753 stream() <<
"running: true";
756 stream() <<
"onLoopsChanged: { if (running) { restart() } }";
759void QQuickQmlGenerator::generateEasing(
const QQuickAnimatedProperty::PropertyAnimation &animation,
760 int time,
int streamFlags)
762 if (animation.easingPerFrame.contains(time)) {
763 QBezier bezier = animation.easingPerFrame.value(time);
764 QPointF c1 = bezier.pt2();
765 QPointF c2 = bezier.pt3();
767 bool isLinear = (c1 == c1.transposed() && c2 == c2.transposed());
769 int nextIdx = m_easings.size();
770 QString &id = m_easings[{c1.x(), c1.y(), c2.x(), c2.y()}];
772 id = QString(QLatin1String(
"easing_%1")).arg(nextIdx, 2, 10, QLatin1Char(
'0'));
773 if (streamFlags & SameLine)
774 stream(streamFlags) <<
"; ";
775 stream(streamFlags) <<
"easing: " << m_topLevelIdString <<
"." << id;
782 static qreal multiplier = qreal(qEnvironmentVariable(
"QT_QUICKVECTORIMAGE_TIME_DILATION", QStringLiteral(
"1.0"))
784 return std::round(multiplier * time);
787void QQuickQmlGenerator::generatePropertyAnimation(
const QQuickAnimatedProperty &property,
788 const QString &targetName,
789 const QString &propertyName,
790 AnimationType animationType)
792 if (!property.isAnimated())
795 if (usingTimelineAnimation())
796 return generatePropertyTimeline(property, targetName, propertyName, animationType);
798 QString mainAnimationId = targetName
799 + QStringLiteral(
"_")
801 + QStringLiteral(
"_animation");
802 mainAnimationId.replace(QLatin1Char(
'.'), QLatin1Char(
'_'));
805 if (Q_UNLIKELY(!isRuntimeGenerator()))
806 prefix = QStringLiteral(
".animations");
808 stream() <<
"Connections { target: " << m_topLevelIdString << prefix <<
"; function onRestart() {" << mainAnimationId <<
".restart() } }";
810 stream() <<
"ParallelAnimation {";
813 stream() <<
"id: " << mainAnimationId;
815 generateAnimationBindings();
817 for (
int i = 0; i < property.animationCount(); ++i) {
818 const QQuickAnimatedProperty::PropertyAnimation &animation = property.animation(i);
820 stream() <<
"SequentialAnimation {";
823 const int startOffset = processAnimationTime(animation.startOffset);
825 stream() <<
"PauseAnimation { duration: " << startOffset <<
" }";
827 stream() <<
"SequentialAnimation {";
830 const int repeatCount = animation.repeatCount;
832 stream() <<
"loops: Animation.Infinite";
834 stream() <<
"loops: " << repeatCount;
836 int previousTime = 0;
837 QVariant previousValue;
838 for (
auto it = animation.frames.constBegin(); it != animation.frames.constEnd(); ++it) {
839 const int time = it.key();
840 const int frameTime = processAnimationTime(time - previousTime);
841 const QVariant &value = it.value();
843 if (previousValue.isValid() && previousValue == value) {
845 stream() <<
"PauseAnimation { duration: " << frameTime <<
" }";
846 }
else if (animationType == AnimationType::Auto && value.typeId() == QMetaType::Bool) {
849 stream() <<
"PauseAnimation { duration: " << frameTime <<
" }";
850 stream() <<
"ScriptAction {";
853 stream() <<
"script:" << targetName <<
"." << propertyName <<
" = " << value.toString();
858 generateAnimatedPropertySetter(targetName,
868 previousValue = value;
871 if (!(animation.flags & QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd)) {
872 stream() <<
"ScriptAction {";
874 stream() <<
"script: ";
876 switch (animationType) {
877 case AnimationType::Auto:
878 stream(SameLine) << targetName <<
"." << propertyName <<
" = ";
880 case AnimationType::ColorOpacity:
881 stream(SameLine) << targetName <<
"." << propertyName <<
".a = ";
885 QVariant value = property.defaultValue();
886 if (value.typeId() == QMetaType::QColor)
887 stream(SameLine) <<
"\"" << value.toString() <<
"\"";
889 stream(SameLine) << value.toReal();
906void QQuickQmlGenerator::generateTimelinePropertySetter(
907 const QString &targetName,
908 const QString &propertyName,
909 const QQuickAnimatedProperty::PropertyAnimation &animation,
910 std::function<QVariant(
const QVariant &)>
const& extractValue,
913 if (animation.repeatCount != 1 || animation.startOffset
914 || animation.flags != QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd) {
915 qCWarning(lcQuickVectorImage) <<
"Animation feature not implemented in timeline mode, for"
916 << targetName << propertyName;
919 stream() <<
"KeyframeGroup {";
921 stream() <<
"target: " << targetName;
922 stream() <<
"property: \"" << propertyName <<
"\"";
924 for (
const auto &[frame, rawValue] : animation.frames.asKeyValueRange()) {
926 if (rawValue.typeId() == QMetaType::QVariantList)
927 value = extractValue(rawValue.toList().value(valueIndex));
929 value = extractValue(rawValue);
931 stream() <<
"Keyframe { frame: " << frame <<
"; value: ";
932 if (value.typeId() == QMetaType::QVector3D) {
933 const QVector3D &v = value.value<QVector3D>();
934 stream(SameLine) <<
"Qt.vector3d(" << v.x() <<
", " << v.y() <<
", " << v.z() <<
")";
935 }
else if (value.typeId() == QMetaType::QColor) {
936 stream(SameLine) <<
"\"" << value.toString() <<
"\"";
938 stream(SameLine) << value.toReal();
940 generateEasing(animation, frame, SameLine);
941 stream(SameLine) <<
" }";
948void QQuickQmlGenerator::generatePropertyTimeline(
const QQuickAnimatedProperty &property,
949 const QString &targetName,
950 const QString &propertyName,
951 AnimationType animationType)
953 if (animationType == QQuickQmlGenerator::AnimationType::ColorOpacity) {
954 qCWarning(lcQuickVectorImage) <<
"ColorOpacity animation not available in timeline mode";
958 if (property.animationGroupCount() > 1 || property.animationCount() > 1) {
959 qCWarning(lcQuickVectorImage) <<
"Property feature not implemented in timeline mode, for"
960 << targetName << propertyName;
963 stream() <<
"Timeline {";
965 stream() <<
"currentFrame: " << property.timelineReferenceId() <<
".frameCounter";
966 stream() <<
"enabled: true";
968 auto extractor = [](
const QVariant &value) {
return value; };
969 generateTimelinePropertySetter(targetName, propertyName, property.animation(0), extractor);
975void QQuickQmlGenerator::generateTransform(
const QTransform &xf)
978 stream(SameLine) <<
"PlanarTransform.fromAffineMatrix("
979 << xf.m11() <<
", " << xf.m12() <<
", "
980 << xf.m21() <<
", " << xf.m22() <<
", "
981 << xf.dx() <<
", " << xf.dy() <<
")";
984 stream(SameLine) <<
"Qt.matrix4x4(";
986 const auto *data = m.data();
987 for (
int i = 0; i < 4; i++) {
988 stream() << data[i] <<
", " << data[i+4] <<
", " << data[i+8] <<
", " << data[i+12];
990 stream(SameLine) <<
", ";
992 stream(SameLine) <<
")";
997void QQuickQmlGenerator::outputShapePath(
const PathNodeInfo &info,
const QPainterPath *painterPath,
const QQuadPath *quadPath, QQuickVectorImageGenerator::PathSelector pathSelector,
const QRectF &boundingRect)
999 Q_UNUSED(pathSelector)
1000 Q_ASSERT(painterPath || quadPath);
1002 if (Q_UNLIKELY(errorState()))
1005 const bool invalidGradientBounds = info.strokeGrad.coordinateMode() == QGradient::ObjectMode
1006 && (qFuzzyIsNull(boundingRect.width()) ||
1007 qFuzzyIsNull(boundingRect.height()));
1009 const QColor strokeColor = info.strokeStyle.color.defaultValue().value<QColor>();
1010 const bool noPen = (strokeColor == QColorConstants::Transparent || !strokeColor.isValid())
1011 && !info.strokeStyle.color.isAnimated()
1012 && !info.strokeStyle.opacity.isAnimated()
1013 && (info.strokeGrad.type() == QGradient::NoGradient
1014 || invalidGradientBounds);
1015 if (pathSelector == QQuickVectorImageGenerator::StrokePath && noPen)
1018 const QColor fillColor = info.fillColor.defaultValue().value<QColor>();
1019 const bool noFill = info.grad.type() == QGradient::NoGradient
1020 && fillColor == QColorConstants::Transparent
1021 && !info.fillColor.isAnimated()
1022 && !info.fillOpacity.isAnimated();
1023 if (pathSelector == QQuickVectorImageGenerator::FillPath && noFill)
1026 if (noPen && noFill)
1028 auto fillRule = QQuickShapePath::FillRule(painterPath ? painterPath->fillRule() : quadPath->fillRule());
1029 stream() <<
"ShapePath {";
1032 QString shapePathId = info.id;
1033 if (pathSelector & QQuickVectorImageGenerator::FillPath)
1034 shapePathId += QStringLiteral(
"_fill");
1035 if (pathSelector & QQuickVectorImageGenerator::StrokePath)
1036 shapePathId += QStringLiteral(
"_stroke");
1038 stream() <<
"id: " << shapePathId;
1040 if (!info.nodeId.isEmpty()) {
1041 switch (pathSelector) {
1042 case QQuickVectorImageGenerator::FillPath:
1043 stream() <<
"objectName: \"svg_fill_path:" << info.nodeId <<
"\"";
1045 case QQuickVectorImageGenerator::StrokePath:
1046 stream() <<
"objectName: \"svg_stroke_path:" << info.nodeId <<
"\"";
1048 case QQuickVectorImageGenerator::FillAndStroke:
1049 stream() <<
"objectName: \"svg_path:" << info.nodeId <<
"\"";
1054 if (noPen || !(pathSelector & QQuickVectorImageGenerator::StrokePath)) {
1055 stream() <<
"strokeColor: \"transparent\"";
1057 if (info.strokeGrad.type() != QGradient::NoGradient && !invalidGradientBounds) {
1058 QRectF coordinateSys = info.strokeGrad.coordinateMode() == QGradient::ObjectMode
1060 : QRectF(0.0, 0.0, 1.0, 1.0);
1061 generateGradient(&info.strokeGrad, QStringLiteral(
"strokeGradient"), coordinateSys);
1062 }
else if (info.strokeStyle.opacity.isAnimated()) {
1063 stream() <<
"property color strokeBase: \"" << strokeColor.name(QColor::HexArgb) <<
"\"";
1064 stream() <<
"property real strokeOpacity: " << info.strokeStyle.opacity.defaultValue().toReal();
1065 stream() <<
"strokeColor: Qt.rgba(strokeBase.r, strokeBase.g, strokeBase.b, strokeOpacity)";
1067 stream() <<
"strokeColor: \"" << strokeColor.name(QColor::HexArgb) <<
"\"";
1069 stream() <<
"strokeWidth: " << info.strokeStyle.width.defaultValue().toReal();
1070 stream() <<
"capStyle: " << QQuickVectorImageGenerator::Utils::strokeCapStyleString(info.strokeStyle.lineCapStyle);
1071 stream() <<
"joinStyle: " << QQuickVectorImageGenerator::Utils::strokeJoinStyleString(info.strokeStyle.lineJoinStyle);
1072 stream() <<
"miterLimit: " << info.strokeStyle.miterLimit;
1073 if (info.strokeStyle.cosmetic)
1074 stream() <<
"cosmeticStroke: true";
1075 if (info.strokeStyle.dashArray.length() != 0) {
1076 stream() <<
"strokeStyle: " <<
"ShapePath.DashLine";
1077 stream() <<
"dashPattern: " << QQuickVectorImageGenerator::Utils::listString(info.strokeStyle.dashArray);
1078 stream() <<
"dashOffset: " << info.strokeStyle.dashOffset.defaultValue().toReal();
1082 QTransform fillTransform = info.fillTransform;
1083 if (!(pathSelector & QQuickVectorImageGenerator::FillPath)) {
1084 stream() <<
"fillColor: \"transparent\"";
1085 }
else if (info.grad.type() != QGradient::NoGradient) {
1086 generateGradient(&info.grad, QStringLiteral(
"fillGradient"));
1089 if (info.grad.coordinateMode() == QGradient::ObjectMode) {
1090 QTransform objectToUserSpace;
1091 objectToUserSpace.translate(boundingRect.x(), boundingRect.y());
1092 objectToUserSpace.scale(boundingRect.width(), boundingRect.height());
1093 fillTransform *= objectToUserSpace;
1096 if (info.fillOpacity.isAnimated()) {
1097 stream() <<
"property color fillBase: \"" << fillColor.name(QColor::HexArgb) <<
"\"";
1098 stream() <<
"property real fillOpacity:" << info.fillOpacity.defaultValue().toReal();
1099 stream() <<
"fillColor: Qt.rgba(fillBase.r, fillBase.g, fillBase.b, fillOpacity)";
1101 stream() <<
"fillColor: \"" << fillColor.name(QColor::HexArgb) <<
"\"";
1105 if (!info.patternId.isEmpty()) {
1106 stream() <<
"fillItem: ShaderEffectSource {";
1109 stream() <<
"parent: " << info.id;
1110 stream() <<
"sourceItem: " << info.patternId;
1111 stream() <<
"hideSource: true";
1112 stream() <<
"visible: false";
1113 stream() <<
"width: " << info.patternId <<
".width";
1114 stream() <<
"height: " << info.patternId <<
".height";
1115 stream() <<
"wrapMode: ShaderEffectSource.Repeat";
1116 stream() <<
"textureSize: Qt.size(width * __qt_toplevel_scale_itemspy.requiredTextureSize.width, "
1117 <<
"height * __qt_toplevel_scale_itemspy.requiredTextureSize.height)";;
1118 stream() <<
"sourceRect: " << info.patternId <<
".sourceRect("
1119 << info.id <<
".width, "
1120 << info.id <<
".height)";
1127 stream() <<
"function calculateFillTransform(xScale, yScale) {";
1130 stream() <<
"var m = ";
1131 generateTransform(fillTransform);
1133 stream() <<
"m.translate(" << info.patternId <<
".sourceOffset("
1134 << info.id <<
".width, "
1135 << info.id <<
".height))";
1137 stream() <<
"m.scale(1.0 / xScale, 1.0 / yScale, 1.0)";
1138 stream() <<
"return m";
1143 stream() <<
"fillTransform: calculateFillTransform(__qt_toplevel_scale_itemspy.requiredTextureSize.width, "
1144 <<
"__qt_toplevel_scale_itemspy.requiredTextureSize.height)";
1146 }
else if (!fillTransform.isIdentity()) {
1147 const QTransform &xf = fillTransform;
1148 stream() <<
"fillTransform: ";
1149 if (info.fillTransform.type() == QTransform::TxTranslate)
1150 stream(SameLine) <<
"PlanarTransform.fromTranslate(" << xf.dx() <<
", " << xf.dy() <<
")";
1151 else if (info.fillTransform.type() == QTransform::TxScale && !xf.dx() && !xf.dy())
1152 stream(SameLine) <<
"PlanarTransform.fromScale(" << xf.m11() <<
", " << xf.m22() <<
")";
1154 generateTransform(xf);
1157 if (info.trim.enabled) {
1158 stream() <<
"trim.start: " << info.trim.start.defaultValue().toReal();
1159 stream() <<
"trim.end: " << info.trim.end.defaultValue().toReal();
1160 stream() <<
"trim.offset: " << info.trim.offset.defaultValue().toReal();
1164 if (fillRule == QQuickShapePath::WindingFill)
1165 stream() <<
"fillRule: ShapePath.WindingFill";
1167 stream() <<
"fillRule: ShapePath.OddEvenFill";
1171 hintStr = QQuickVectorImageGenerator::Utils::pathHintString(*quadPath);
1172 if (!hintStr.isEmpty())
1173 stream() << hintStr;
1175 QQuickAnimatedProperty pathFactor(QVariant::fromValue(0));
1176 pathFactor.setTimelineReferenceId(info.path.timelineReferenceId());
1177 QString pathId = shapePathId +
"_ip"_L1;
1178 if (!info.path.isAnimated() || (info.path.animation(0).startOffset == 0 && info.path.animation(0).isConstant())) {
1179 QString svgPathString = painterPath ? QQuickVectorImageGenerator::Utils::toSvgString(*painterPath) : QQuickVectorImageGenerator::Utils::toSvgString(*quadPath);
1180 stream() <<
"PathSvg { path: \"" << svgPathString <<
"\" }";
1182 stream() <<
"PathInterpolated {";
1184 stream() <<
"id: " << pathId;
1185 stream() <<
"svgPaths: [";
1187 QQuickAnimatedProperty::PropertyAnimation pathFactorAnim = info.path.animation(0);
1188 auto &frames = pathFactorAnim.frames;
1191 for (
auto it = frames.begin(); it != frames.end(); ++it) {
1192 QString svg = QQuickVectorImageGenerator::Utils::toSvgString(it->value<QPainterPath>());
1193 if (svg != lastSvg) {
1195 stream(SameLine) <<
",";
1196 stream() <<
"\"" << svg <<
"\"";
1200 *it = QVariant::fromValue(pathIdx);
1202 pathFactor.addAnimation(pathFactorAnim);
1212 if (pathFactor.isAnimated())
1213 generatePropertyAnimation(pathFactor, pathId,
"factor"_L1);
1215 if (info.trim.enabled) {
1216 generatePropertyAnimation(info.trim.start, shapePathId + QStringLiteral(
".trim"), QStringLiteral(
"start"));
1217 generatePropertyAnimation(info.trim.end, shapePathId + QStringLiteral(
".trim"), QStringLiteral(
"end"));
1218 generatePropertyAnimation(info.trim.offset, shapePathId + QStringLiteral(
".trim"), QStringLiteral(
"offset"));
1221 if (info.strokeStyle.opacity.isAnimated()) {
1222 generatePropertyAnimation(info.strokeStyle.color, shapePathId, QStringLiteral(
"strokeBase"));
1223 generatePropertyAnimation(info.strokeStyle.opacity, shapePathId, QStringLiteral(
"strokeOpacity"));
1225 generatePropertyAnimation(info.strokeStyle.color, shapePathId, QStringLiteral(
"strokeColor"));
1227 if (info.strokeStyle.width.isAnimated())
1228 generatePropertyAnimation(info.strokeStyle.width, shapePathId, QStringLiteral(
"strokeWidth"));
1229 if (info.strokeStyle.dashOffset.isAnimated())
1230 generatePropertyAnimation(info.strokeStyle.dashOffset, shapePathId, QStringLiteral(
"dashOffset"));
1232 if (info.fillOpacity.isAnimated()) {
1233 generatePropertyAnimation(info.fillColor, shapePathId, QStringLiteral(
"fillBase"));
1234 generatePropertyAnimation(info.fillOpacity, shapePathId, QStringLiteral(
"fillOpacity"));
1236 generatePropertyAnimation(info.fillColor, shapePathId, QStringLiteral(
"fillColor"));
1240void QQuickQmlGenerator::generateNode(
const NodeInfo &info)
1242 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
1245 stream() <<
"// Missing Implementation for SVG Node: " << info.typeName;
1246 stream() <<
"// Adding an empty Item and skipping";
1247 stream() <<
"Item {";
1249 generateNodeBase(info);
1250 generateNodeEnd(info);
1253void QQuickQmlGenerator::generateTextNode(
const TextNodeInfo &info)
1255 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
1258 stream() <<
"Item {";
1260 generateNodeBase(info);
1262 if (!info.isTextArea)
1263 stream() <<
"Item { id: textAlignItem_" << m_textNodeCounter <<
"; x: " << info.position.x() <<
"; y: " << info.position.y() <<
"}";
1265 stream() <<
"Text {";
1269 const QString textItemId = QStringLiteral(
"_qt_textItem_%1").arg(m_textNodeCounter);
1270 stream() <<
"id: " << textItemId;
1272 generatePropertyAnimation(info.fillColor, textItemId, QStringLiteral(
"color"));
1273 generatePropertyAnimation(info.fillOpacity, textItemId, QStringLiteral(
"color"), AnimationType::ColorOpacity);
1274 generatePropertyAnimation(info.strokeColor, textItemId, QStringLiteral(
"styleColor"));
1275 generatePropertyAnimation(info.strokeOpacity, textItemId, QStringLiteral(
"styleColor"), AnimationType::ColorOpacity);
1277 if (info.isTextArea) {
1278 stream() <<
"x: " << info.position.x();
1279 stream() <<
"y: " << info.position.y();
1280 if (info.size.width() > 0)
1281 stream() <<
"width: " << info.size.width();
1282 if (info.size.height() > 0)
1283 stream() <<
"height: " << info.size.height();
1284 stream() <<
"wrapMode: Text.Wrap";
1285 stream() <<
"clip: true";
1287 QString hAlign = QStringLiteral(
"left");
1288 stream() <<
"anchors.baseline: textAlignItem_" << m_textNodeCounter <<
".top";
1289 switch (info.alignment) {
1290 case Qt::AlignHCenter:
1291 hAlign = QStringLiteral(
"horizontalCenter");
1293 case Qt::AlignRight:
1294 hAlign = QStringLiteral(
"right");
1297 qCDebug(lcQuickVectorImage) <<
"Unexpected text alignment" << info.alignment;
1302 stream() <<
"anchors." << hAlign <<
": textAlignItem_" << m_textNodeCounter <<
".left";
1304 m_textNodeCounter++;
1306 stream() <<
"color: \"" << info.fillColor.defaultValue().value<QColor>().name(QColor::HexArgb) <<
"\"";
1307 stream() <<
"textFormat:" << (info.needsRichText ?
"Text.RichText" :
"Text.StyledText");
1309 stream() <<
"text: \"" << sanitizeString(info.text) <<
"\"";
1310 stream() <<
"font.family: \"" << sanitizeString(info.font.family()) <<
"\"";
1311 if (info.font.pixelSize() > 0)
1312 stream() <<
"font.pixelSize:" << info.font.pixelSize();
1313 else if (info.font.pointSize() > 0)
1314 stream() <<
"font.pixelSize:" << info.font.pointSizeF();
1315 if (info.font.underline())
1316 stream() <<
"font.underline: true";
1317 if (info.font.weight() != QFont::Normal)
1318 stream() <<
"font.weight: " <<
int(info.font.weight());
1319 if (info.font.italic())
1320 stream() <<
"font.italic: true";
1321 switch (info.font.hintingPreference()) {
1322 case QFont::PreferFullHinting:
1323 stream() <<
"font.hintingPreference: Font.PreferFullHinting";
1325 case QFont::PreferVerticalHinting:
1326 stream() <<
"font.hintingPreference: Font.PreferVerticalHinting";
1328 case QFont::PreferNoHinting:
1329 stream() <<
"font.hintingPreference: Font.PreferNoHinting";
1331 case QFont::PreferDefaultHinting:
1332 stream() <<
"font.hintingPreference: Font.PreferDefaultHinting";
1336 const QColor strokeColor = info.strokeColor.defaultValue().value<QColor>();
1337 if (strokeColor != QColorConstants::Transparent || info.strokeColor.isAnimated()) {
1338 stream() <<
"styleColor: \"" << strokeColor.name(QColor::HexArgb) <<
"\"";
1339 stream() <<
"style: Text.Outline";
1345 generateNodeEnd(info);
1348void QQuickQmlGenerator::generateUseNode(
const UseNodeInfo &info)
1350 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
1353 if (info.stage == StructureNodeStage::Start) {
1354 stream() <<
"Item {";
1356 generateNodeBase(info);
1358 generateNodeEnd(info);
1362void QQuickQmlGenerator::generatePathContainer(
const StructureNodeInfo &info)
1365 stream() << shapeName() <<
" {";
1367 if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::CurveRenderer))
1368 stream() <<
"preferredRendererType: Shape.CurveRenderer";
1369 if (m_flags.testFlag(QQuickVectorImageGenerator::GeneratorFlag::AsyncShapes))
1370 stream() <<
"asynchronous: true";
1373 m_inShapeItemLevel++;
1376void QQuickQmlGenerator::generateAnimateMotionPath(
const QString &targetName,
1377 const QQuickAnimatedProperty &property)
1379 if (!property.isAnimated())
1382 QPainterPath path = property.defaultValue().value<QVariantList>().value(0).value<QPainterPath>();
1383 const QString mainAnimationId = targetName + QStringLiteral(
"_motion_interpolator");
1384 stream() <<
"PathInterpolator {";
1386 stream() <<
"id: " << mainAnimationId;
1387 const QString svgPathString = QQuickVectorImageGenerator::Utils::toSvgString(path);
1388 stream() <<
"path: Path { PathSvg { path: \"" << svgPathString <<
"\" } }";
1392 generatePropertyAnimation(property, mainAnimationId, QStringLiteral(
"progress"));
1395void QQuickQmlGenerator::generateAnimatedPropertySetter(
const QString &targetName,
1396 const QString &propertyName,
1397 const QVariant &value,
1398 const QQuickAnimatedProperty::PropertyAnimation &animation,
1401 AnimationType animationType)
1403 if (frameTime > 0) {
1404 switch (animationType) {
1405 case AnimationType::Auto:
1406 if (value.typeId() == QMetaType::QColor)
1407 stream() <<
"ColorAnimation {";
1409 stream() <<
"PropertyAnimation {";
1411 case AnimationType::ColorOpacity:
1412 stream() <<
"ColorOpacityAnimation {";
1417 stream() <<
"duration: " << frameTime;
1418 stream() <<
"target: " << targetName;
1419 stream() <<
"property: \"" << propertyName <<
"\"";
1421 if (value.typeId() == QMetaType::QVector3D) {
1422 const QVector3D &v = value.value<QVector3D>();
1423 stream(SameLine) <<
"Qt.vector3d(" << v.x() <<
", " << v.y() <<
", " << v.z() <<
")";
1424 }
else if (value.typeId() == QMetaType::QColor) {
1425 stream(SameLine) <<
"\"" << value.toString() <<
"\"";
1427 stream(SameLine) << value.toReal();
1429 generateEasing(animation, time);
1433 stream() <<
"ScriptAction {";
1435 stream() <<
"script:" << targetName <<
"." << propertyName;
1436 if (animationType == AnimationType::ColorOpacity)
1437 stream(SameLine) <<
".a";
1439 stream(SameLine) <<
" = ";
1440 if (value.typeId() == QMetaType::QVector3D) {
1441 const QVector3D &v = value.value<QVector3D>();
1442 stream(SameLine) <<
"Qt.vector3d(" << v.x() <<
", " << v.y() <<
", " << v.z() <<
")";
1443 }
else if (value.typeId() == QMetaType::QColor) {
1444 stream(SameLine) <<
"\"" << value.toString() <<
"\"";
1446 stream(SameLine) << value.toReal();
1453void QQuickQmlGenerator::generateAnimateTransform(
const QString &targetName,
const NodeInfo &info)
1455 if (!info.transform.isAnimated())
1458 if (usingTimelineAnimation())
1459 return generateTransformTimeline(targetName, info);
1461 const QString mainAnimationId = targetName
1462 + QStringLiteral(
"_transform_animation");
1465 if (Q_UNLIKELY(!isRuntimeGenerator()))
1466 prefix = QStringLiteral(
".animations");
1467 stream() <<
"Connections { target: " << m_topLevelIdString << prefix <<
"; function onRestart() {" << mainAnimationId <<
".restart() } }";
1469 stream() <<
"ParallelAnimation {";
1472 stream() <<
"id:" << mainAnimationId;
1474 generateAnimationBindings();
1475 for (
int groupIndex = 0; groupIndex < info.transform.animationGroupCount(); ++groupIndex) {
1476 int animationStart = info.transform.animationGroup(groupIndex);
1477 int nextAnimationStart = groupIndex + 1 < info.transform.animationGroupCount()
1478 ? info.transform.animationGroup(groupIndex + 1)
1479 : info.transform.animationCount();
1482 const QQuickAnimatedProperty::PropertyAnimation &firstAnimation = info.transform.animation(animationStart);
1483 const bool freeze = firstAnimation.flags & QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd;
1484 const bool replace = firstAnimation.flags & QQuickAnimatedProperty::PropertyAnimation::ReplacePreviousAnimations;
1486 stream() <<
"SequentialAnimation {";
1489 const int startOffset = processAnimationTime(firstAnimation.startOffset);
1490 if (startOffset > 0)
1491 stream() <<
"PauseAnimation { duration: " << startOffset <<
" }";
1493 const int repeatCount = firstAnimation.repeatCount;
1494 if (repeatCount < 0)
1495 stream() <<
"loops: Animation.Infinite";
1497 stream() <<
"loops: " << repeatCount;
1500 stream() <<
"ScriptAction {";
1503 stream() <<
"script: " << targetName <<
"_transform_base_group"
1504 <<
".activateOverride(" << targetName <<
"_transform_group_" << groupIndex <<
")";
1510 stream() <<
"ParallelAnimation {";
1513 for (
int i = animationStart; i < nextAnimationStart; ++i) {
1514 const QQuickAnimatedProperty::PropertyAnimation &animation = info.transform.animation(i);
1515 if (animation.isConstant())
1517 bool hasRotationCenter =
false;
1518 if (animation.subtype == QTransform::TxRotate) {
1519 for (
auto it = animation.frames.constBegin(); it != animation.frames.constEnd(); ++it) {
1520 const QPointF center = it->value<QVariantList>().value(0).value<QPointF>();
1521 if (!center.isNull()) {
1522 hasRotationCenter =
true;
1528 stream() <<
"SequentialAnimation {";
1531 int previousTime = 0;
1532 QVariantList previousParameters;
1533 for (
auto it = animation.frames.constBegin(); it != animation.frames.constEnd(); ++it) {
1534 const int time = it.key();
1535 const int frameTime = processAnimationTime(time - previousTime);
1536 const QVariantList ¶meters = it.value().value<QVariantList>();
1537 if (parameters.isEmpty())
1540 if (parameters == previousParameters) {
1542 stream() <<
"PauseAnimation { duration: " << frameTime <<
" }";
1544 stream() <<
"ParallelAnimation {";
1547 const QString propertyTargetName = targetName
1548 + QStringLiteral(
"_transform_")
1549 + QString::number(groupIndex)
1550 + QStringLiteral(
"_")
1551 + QString::number(i);
1553 switch (animation.subtype) {
1554 case QTransform::TxTranslate:
1556 const QPointF translation = parameters.first().value<QPointF>();
1558 generateAnimatedPropertySetter(propertyTargetName,
1559 QStringLiteral(
"x"),
1564 generateAnimatedPropertySetter(propertyTargetName,
1565 QStringLiteral(
"y"),
1572 case QTransform::TxScale:
1574 const QPointF scale = parameters.first().value<QPointF>();
1575 generateAnimatedPropertySetter(propertyTargetName,
1576 QStringLiteral(
"xScale"),
1581 generateAnimatedPropertySetter(propertyTargetName,
1582 QStringLiteral(
"yScale"),
1589 case QTransform::TxRotate:
1591 Q_ASSERT(parameters.size() == 2);
1592 const qreal angle = parameters.value(1).toReal();
1593 if (hasRotationCenter) {
1594 const QPointF center = parameters.value(0).value<QPointF>();
1595 generateAnimatedPropertySetter(propertyTargetName,
1596 QStringLiteral(
"origin"),
1597 QVector3D(center.x(), center.y(), 0.0),
1602 generateAnimatedPropertySetter(propertyTargetName,
1603 QStringLiteral(
"angle"),
1610 case QTransform::TxShear:
1612 const QPointF skew = parameters.first().value<QPointF>();
1614 generateAnimatedPropertySetter(propertyTargetName,
1615 QStringLiteral(
"xAngle"),
1621 generateAnimatedPropertySetter(propertyTargetName,
1622 QStringLiteral(
"yAngle"),
1637 previousTime = time;
1638 previousParameters = parameters;
1650 if (firstAnimation.repeatCount >= 0) {
1651 stream() <<
"ScriptAction {";
1654 stream() <<
"script: {";
1658 stream() << targetName <<
"_transform_base_group.deactivate("
1659 << targetName <<
"_transform_group_" << groupIndex <<
")";
1660 }
else if (!replace) {
1661 stream() << targetName <<
"_transform_base_group.deactivateOverride("
1662 << targetName <<
"_transform_group_" << groupIndex <<
")";
1680void QQuickQmlGenerator::generateTransformTimeline(
const QString &targetName,
const NodeInfo &info)
1682 stream() <<
"Timeline {";
1684 stream() <<
"currentFrame: " << info.transform.timelineReferenceId() <<
".frameCounter";
1685 stream() <<
"enabled: true";
1687 const int groupIndex = 0;
1688 for (
int i = 0; i < info.transform.animationCount(); ++i) {
1689 const QQuickAnimatedProperty::PropertyAnimation &animation = info.transform.animation(i);
1690 if (animation.isConstant())
1692 if (info.transform.animationGroupCount() > 1
1693 || animation.repeatCount != 1 || animation.startOffset
1694 || animation.flags != QQuickAnimatedProperty::PropertyAnimation::FreezeAtEnd) {
1695 qCWarning(lcQuickVectorImage) <<
"Feature not implemented in timeline xf animation mode, for"
1696 << targetName <<
"subtype" << animation.subtype;
1699 bool hasRotationCenter =
false;
1700 if (animation.subtype == QTransform::TxRotate) {
1701 for (
auto it = animation.frames.constBegin(); it != animation.frames.constEnd(); ++it) {
1702 const QPointF center = it->value<QVariantList>().value(0).value<QPointF>();
1703 if (!center.isNull()) {
1704 hasRotationCenter =
true;
1710 auto pointFxExtractor = [](
const QVariant &value) {
return value.toPointF().x(); };
1711 auto pointFyExtractor = [](
const QVariant &value) {
return value.toPointF().y(); };
1712 auto realExtractor = [](
const QVariant &value) {
return value.toReal(); };
1713 auto pointFtoVector3dExtractor = [](
const QVariant &v) {
return QVector3D(v.toPointF()); };
1715 const QString propertyTargetName = targetName
1716 + QStringLiteral(
"_transform_")
1717 + QString::number(groupIndex)
1718 + QStringLiteral(
"_")
1719 + QString::number(i);
1721 switch (animation.subtype) {
1722 case QTransform::TxTranslate:
1723 generateTimelinePropertySetter(propertyTargetName,
"x"_L1, animation, pointFxExtractor);
1724 generateTimelinePropertySetter(propertyTargetName,
"y"_L1, animation, pointFyExtractor);
1726 case QTransform::TxScale:
1727 generateTimelinePropertySetter(propertyTargetName,
"xScale"_L1, animation, pointFxExtractor);
1728 generateTimelinePropertySetter(propertyTargetName,
"yScale"_L1, animation, pointFyExtractor);
1730 case QTransform::TxRotate:
1731 if (hasRotationCenter)
1732 generateTimelinePropertySetter(propertyTargetName,
"origin"_L1, animation, pointFtoVector3dExtractor);
1733 generateTimelinePropertySetter(propertyTargetName,
"angle"_L1, animation, realExtractor, 1);
1735 case QTransform::TxShear:
1736 generateTimelinePropertySetter(propertyTargetName,
"xAngle"_L1, animation, pointFxExtractor);
1737 generateTimelinePropertySetter(propertyTargetName,
"yAngle"_L1, animation, pointFyExtractor);
1746bool QQuickQmlGenerator::generateStructureNode(
const StructureNodeInfo &info)
1748 if (Q_UNLIKELY(errorState() || !isNodeVisible(info)))
1751 const bool isPathContainer = !info.forceSeparatePaths && info.isPathContainer;
1752 if (info.stage == StructureNodeStage::Start) {
1753 if (!info.clipBox.isEmpty()) {
1754 stream() <<
"Item { // Clip";
1757 stream() <<
"width: " << info.clipBox.width();
1758 stream() <<
"height: " << info.clipBox.height();
1759 stream() <<
"clip: true";
1762 if (isPathContainer) {
1763 generatePathContainer(info);
1764 }
else if (!info.customItemType.isEmpty()) {
1765 stream() << info.customItemType <<
" {";
1767 stream() <<
"Item { // Structure node";
1771 generateTimelineFields(info);
1773 if (!info.viewBox.isEmpty()) {
1774 stream() <<
"transform: [";
1776 bool translate = !qFuzzyIsNull(info.viewBox.x()) || !qFuzzyIsNull(info.viewBox.y());
1778 stream() <<
"Translate { x: " << -info.viewBox.x() <<
"; y: " << -info.viewBox.y() <<
" },";
1779 stream() <<
"Scale { xScale: width / " << info.viewBox.width() <<
"; yScale: height / " << info.viewBox.height() <<
" }";
1784 generateNodeBase(info);
1786 generateNodeEnd(info);
1787 if (isPathContainer)
1788 m_inShapeItemLevel--;
1790 if (!info.clipBox.isEmpty()) {
1799bool QQuickQmlGenerator::generateMaskNode(
const MaskNodeInfo &info)
1801 if (Q_UNLIKELY(errorState()))
1805 if (info.stage == StructureNodeStage::End) {
1807 startDefsSuffixBlock();
1808 stream() <<
"Loader {";
1811 stream() <<
"id: " << info.id;
1812 stream() <<
"visible: false";
1813 stream() <<
"sourceComponent: " << info.id <<
"_container";
1814 stream() <<
"width: item !== null ? item.originalBounds.width : 0";
1815 stream() <<
"height: item !== null ? item.originalBounds.height : 0";
1817 if (info.boundsReferenceId.isEmpty()) {
1818 stream() <<
"property real maskX: " << info.maskRect.left();
1819 stream() <<
"property real maskY: " << info.maskRect.top();
1820 stream() <<
"property real maskWidth: " << info.maskRect.width();
1821 stream() <<
"property real maskHeight: " << info.maskRect.height();
1824 stream() <<
"function maskRect(otherX, otherY, otherWidth, otherHeight) {";
1827 stream() <<
"return ";
1828 if (!info.boundsReferenceId.isEmpty()) {
1829 stream(SameLine) << info.boundsReferenceId <<
".originalBounds";
1830 }
else if (info.isMaskRectRelativeCoordinates) {
1833 << info.id <<
".maskX * otherWidth + otherX,"
1834 << info.id <<
".maskY * otherHeight + otherY,"
1835 << info.id <<
".maskWidth * otherWidth,"
1836 << info.id <<
".maskHeight * otherHeight)";
1840 << info.id <<
".maskX, "
1841 << info.id <<
".maskY, "
1842 << info.id <<
".maskWidth, "
1843 << info.id <<
".maskHeight)";
1852 endDefsSuffixBlock();
1858void QQuickQmlGenerator::generateFilterNode(
const FilterNodeInfo &info)
1860 if (Q_UNLIKELY(errorState()))
1863 stream() <<
"Item {";
1866 generateNodeBase(info);
1868 stream() <<
"property real originalWidth: filterSourceItem.sourceItem.originalBounds.width";
1869 stream() <<
"property real originalHeight: filterSourceItem.sourceItem.originalBounds.height";
1870 stream() <<
"property rect filterRect: " << info.id <<
"_filterParameters"
1871 <<
".adaptToFilterRect(0, 0, originalWidth, originalHeight)";
1873 for (qsizetype i = 0; i < info.steps.size();)
1874 i = generateFilterStep(info, i);
1877 startDefsSuffixBlock();
1878 stream() <<
"QtObject {";
1881 stream() <<
"id: " << info.id <<
"_filterParameters";
1882 stream() <<
"property int wrapMode: ";
1883 if (info.wrapMode == QSGTexture::Repeat)
1884 stream(SameLine) <<
"ShaderEffectSource.Repeat";
1886 stream(SameLine) <<
"ShaderEffectSource.ClampToEdge";
1888 stream() <<
"property rect filterRect: Qt.rect("
1889 << info.filterRect.x() <<
", "
1890 << info.filterRect.y() <<
", "
1891 << info.filterRect.width() <<
", "
1892 << info.filterRect.height() <<
")";
1894 stream() <<
"function adaptToFilterRect(sx, sy, sw, sh) {";
1897 if (info.csFilterRect == FilterNodeInfo::CoordinateSystem::Absolute) {
1898 stream() <<
"return Qt.rect(filterRect.x, filterRect.y, filterRect.width, filterRect.height)";
1900 stream() <<
"return Qt.rect(sx + sw * filterRect.x, sy + sh * filterRect.y, sw * filterRect.width, sh * filterRect.height)";
1908 endDefsSuffixBlock();
1910 generateNodeEnd(info);
1913qsizetype QQuickQmlGenerator::generateFilterStep(
const FilterNodeInfo &info,
1914 qsizetype stepIndex)
1916 const FilterNodeInfo::FilterStep &step = info.steps.at(stepIndex);
1917 const QString primitiveId = info.id + QStringLiteral(
"_primitive") + QString::number(stepIndex);
1921 QString inputId = step.input1 != FilterNodeInfo::FilterInput::SourceColor
1923 : QStringLiteral(
"filterSourceItem");
1925 bool isComposite =
false;
1926 switch (step.filterType) {
1927 case FilterNodeInfo::Type::Merge:
1929 const int maxNodeCount = 8;
1932 QList<std::pair<FilterNodeInfo::FilterInput, QString> > inputs;
1933 for (; stepIndex < info.steps.size(); ++stepIndex) {
1934 const FilterNodeInfo::FilterStep &nodeStep = info.steps.at(stepIndex);
1935 if (nodeStep.filterType != FilterNodeInfo::Type::MergeNode)
1938 inputs.emplace_back(nodeStep.input1, nodeStep.namedInput1);
1941 if (inputs.size() > maxNodeCount) {
1942 qCWarning(lcQuickVectorImage) <<
"Maximum of" << maxNodeCount
1943 <<
"nodes exceeded in merge effect.";
1946 if (inputs.isEmpty()) {
1947 qCWarning(lcQuickVectorImage) <<
"Merge effect requires at least one node.";
1951 stream() <<
"ShaderEffect {";
1954 stream() <<
"id: " << primitiveId;
1955 stream() <<
"visible: false";
1957 stream() <<
"fragmentShader: \"qrc:/qt-project.org/quickvectorimage/helpers/shaders_ng/femerge.frag.qsb\"";
1958 stream() <<
"width: source1.width";
1959 stream() <<
"height: source1.height";
1960 stream() <<
"property int sourceCount: " << std::min(qsizetype(8), inputs.size());
1962 for (
int i = 0; i < maxNodeCount; ++i) {
1963 auto input = i < inputs.size()
1965 : std::pair(FilterNodeInfo::FilterInput::None, u"null"_s);
1967 QString inputId = input.first != FilterNodeInfo::FilterInput::SourceColor
1969 : QStringLiteral(
"filterSourceItem");
1971 stream() <<
"property var source" << (i + 1) <<
": " << inputId;
1979 case FilterNodeInfo::Type::CompositeOver:
1980 case FilterNodeInfo::Type::CompositeOut:
1981 case FilterNodeInfo::Type::CompositeIn:
1982 case FilterNodeInfo::Type::CompositeXor:
1983 case FilterNodeInfo::Type::CompositeAtop:
1984 case FilterNodeInfo::Type::CompositeArithmetic:
1985 case FilterNodeInfo::Type::CompositeLighter:
1989 case FilterNodeInfo::Type::BlendNormal:
1990 case FilterNodeInfo::Type::BlendMultiply:
1991 case FilterNodeInfo::Type::BlendScreen:
1992 case FilterNodeInfo::Type::BlendDarken:
1993 case FilterNodeInfo::Type::BlendLighten:
1995 stream() <<
"ShaderEffect {";
1998 QString input2Id = step.input2 != FilterNodeInfo::FilterInput::SourceColor
2000 : QStringLiteral(
"filterSourceItem");
2002 stream() <<
"id: " << primitiveId;
2003 stream() <<
"visible: false";
2006 switch (step.filterType) {
2007 case FilterNodeInfo::Type::CompositeOver:
2008 shader = QStringLiteral(
"fecompositeover");
2010 case FilterNodeInfo::Type::CompositeOut:
2011 shader = QStringLiteral(
"fecompositeout");
2013 case FilterNodeInfo::Type::CompositeIn:
2014 shader = QStringLiteral(
"fecompositein");
2016 case FilterNodeInfo::Type::CompositeXor:
2017 shader = QStringLiteral(
"fecompositexor");
2019 case FilterNodeInfo::Type::CompositeAtop:
2020 shader = QStringLiteral(
"fecompositeatop");
2022 case FilterNodeInfo::Type::CompositeArithmetic:
2023 shader = QStringLiteral(
"fecompositearithmetic");
2025 case FilterNodeInfo::Type::CompositeLighter:
2026 shader = QStringLiteral(
"fecompositelighter");
2028 case FilterNodeInfo::Type::BlendNormal:
2029 shader = QStringLiteral(
"feblendnormal");
2031 case FilterNodeInfo::Type::BlendMultiply:
2032 shader = QStringLiteral(
"feblendmultiply");
2034 case FilterNodeInfo::Type::BlendScreen:
2035 shader = QStringLiteral(
"feblendscreen");
2037 case FilterNodeInfo::Type::BlendDarken:
2038 shader = QStringLiteral(
"feblenddarken");
2040 case FilterNodeInfo::Type::BlendLighten:
2041 shader = QStringLiteral(
"feblendlighten");
2047 stream() <<
"fragmentShader: \"qrc:/qt-project.org/quickvectorimage/helpers/shaders_ng/"
2048 << shader <<
".frag.qsb\"";
2049 stream() <<
"property var source: " << inputId;
2050 stream() <<
"property var source2: " << input2Id;
2051 stream() <<
"width: source.width";
2052 stream() <<
"height: source.height";
2055 QVector4D k = step.filterParameter.value<QVector4D>();
2056 stream() <<
"property var k: Qt.vector4d("
2069 case FilterNodeInfo::Type::Flood:
2071 stream() <<
"Rectangle {";
2074 stream() <<
"id: " << primitiveId;
2075 stream() <<
"visible: false";
2077 stream() <<
"width: " << inputId <<
".width";
2078 stream() <<
"height: " << inputId <<
".height";
2080 QColor floodColor = step.filterParameter.value<QColor>();
2081 stream() <<
"color: \"" << floodColor.name(QColor::HexArgb) <<
"\"";
2088 case FilterNodeInfo::Type::ColorMatrix:
2090 stream() <<
"ShaderEffect {";
2093 stream() <<
"id: " << primitiveId;
2094 stream() <<
"visible: false";
2096 stream() <<
"fragmentShader: \"qrc:/qt-project.org/quickvectorimage/helpers/shaders_ng/fecolormatrix.frag.qsb\"";
2097 stream() <<
"property var source: " << inputId;
2098 stream() <<
"width: source.width";
2099 stream() <<
"height: source.height";
2101 QGenericMatrix<5, 5, qreal> matrix = step.filterParameter.value<QGenericMatrix<5, 5, qreal> >();
2102 for (
int row = 0; row < 4; ++row) {
2105 for (
int col = 0; col < 5; ++col)
2106 stream() <<
"property real m_" << row <<
"_" << col <<
": " << matrix(col, row);
2115 case FilterNodeInfo::Type::Offset:
2117 stream() <<
"ShaderEffectSource {";
2120 stream() <<
"id: " << primitiveId;
2121 stream() <<
"visible: false";
2122 stream() <<
"sourceItem: " << inputId;
2123 stream() <<
"width: sourceItem.width + offset.x";
2124 stream() <<
"height: sourceItem.height + offset.y";
2126 QVector2D offset = step.filterParameter.value<QVector2D>();
2127 stream() <<
"property vector2d offset: Qt.vector2d(";
2128 if (step.csFilterParameter == FilterNodeInfo::CoordinateSystem::Absolute)
2129 stream(SameLine) << offset.x() <<
" / width, " << offset.y() <<
" / height)";
2131 stream(SameLine) << offset.x() <<
", " << offset.y() <<
")";
2133 stream() <<
"sourceRect: Qt.rect(-offset.x, -offset.y, width, height)";
2135 stream() <<
"ItemSpy {";
2137 stream() <<
"id: " << primitiveId <<
"_offset_itemspy";
2138 stream() <<
"anchors.fill: parent";
2142 stream() <<
"textureSize: " << primitiveId <<
"_offset_itemspy.requiredTextureSize";
2151 case FilterNodeInfo::Type::GaussianBlur:
2154 stream() <<
"MultiEffect {";
2157 stream() <<
"id: " << primitiveId;
2158 stream() <<
"visible: false";
2160 stream() <<
"source: " << inputId;
2161 stream() <<
"blurEnabled: true";
2162 stream() <<
"width: source.width";
2163 stream() <<
"height: source.height";
2165 const qreal maxDeviation(12.0);
2166 const qreal deviation = step.filterParameter.toReal();
2167 if (step.csFilterParameter == FilterNodeInfo::CoordinateSystem::Relative)
2168 stream() <<
"blur: Math.min(1.0, " << deviation <<
" * filterSourceItem.width / " << maxDeviation <<
")";
2170 stream() <<
"blur: " << std::min(qreal(1.0), deviation / maxDeviation);
2171 stream() <<
"blurMax: 64";
2179 qCWarning(lcQuickVectorImage) <<
"Unhandled filter type: " <<
int(step.filterType);
2181 stream() <<
"Item { id: " << primitiveId <<
" }";
2186 stream() <<
"ShaderEffectSource {";
2189 stream() <<
"id: " << step.outputName;
2190 if (stepIndex < info.steps.size())
2191 stream() <<
"visible: false";
2193 qreal x1, x2, y1, y2;
2194 step.filterPrimitiveRect.getCoords(&x1, &y1, &x2, &y2);
2195 if (step.csFilterParameter == FilterNodeInfo::CoordinateSystem::Absolute) {
2196 stream() <<
"property real fpx1: " << x1;
2197 stream() <<
"property real fpy1: " << y1;
2198 stream() <<
"property real fpx2: " << x2;
2199 stream() <<
"property real fpy2: " << y2;
2200 }
else if (step.csFilterParameter == FilterNodeInfo::CoordinateSystem::Relative) {
2204 stream() <<
"property real fpx1: " << x1 <<
" * filterSourceItem.sourceItem.originalBounds.width";
2205 stream() <<
"property real fpy1: " << y1 <<
" * filterSourceItem.sourceItem.originalBounds.height";
2206 stream() <<
"property real fpx2: " << x2 <<
" * filterSourceItem.sourceItem.originalBounds.width";
2207 stream() <<
"property real fpy2: " << y2 <<
" * filterSourceItem.sourceItem.originalBounds.height";
2209 stream() <<
"property real fpx1: parent.filterRect.x";
2210 stream() <<
"property real fpy1: parent.filterRect.y";
2211 stream() <<
"property real fpx2: parent.filterRect.x + parent.filterRect.width";
2212 stream() <<
"property real fpy2: parent.filterRect.y + parent.filterRect.height";
2215 stream() <<
"sourceItem: " << primitiveId;
2216 stream() <<
"sourceRect: Qt.rect(fpx1 - parent.filterRect.x, fpy1 - parent.filterRect.y, width, height)";
2218 stream() <<
"x: fpx1";
2219 stream() <<
"y: fpy1";
2220 stream() <<
"width: " <<
"fpx2 - fpx1";
2221 stream() <<
"height: " <<
"fpy2 - fpy1";
2223 stream() <<
"ItemSpy {";
2225 stream() <<
"id: " << primitiveId <<
"_itemspy";
2226 stream() <<
"anchors.fill: parent";
2230 stream() <<
"textureSize: " << primitiveId <<
"_itemspy.requiredTextureSize";
2238void QQuickQmlGenerator::generateTimelineFields(
const StructureNodeInfo &info)
2240 if (usingTimelineAnimation() && info.timelineInfo) {
2241 QString frameCounterRef = info.timelineInfo->frameCounterReference
2242 + QStringLiteral(
".frameCounter");
2244 if (info.timelineInfo->generateVisibility) {
2245 stream() <<
"visible: " << frameCounterRef <<
" >= " << info.timelineInfo->startFrame
2246 <<
" && " << frameCounterRef <<
" < " << info.timelineInfo->endFrame;
2249 if (info.timelineInfo->generateFrameCounter) {
2250 stream() <<
"property real frameCounter: ";
2251 if (info.timelineInfo->frameCounterMapper.isAnimated()) {
2253 stream(SameLine) <<
"0";
2254 generatePropertyTimeline(info.timelineInfo->frameCounterMapper, info.id,
"frameCounter"_L1);
2256 const auto offset = info.timelineInfo->frameCounterOffset;
2257 const auto multiplier = info.timelineInfo->frameCounterMultiplier;
2258 const bool needsParens = (offset && multiplier);
2260 stream(SameLine) <<
"(";
2261 stream(SameLine) << frameCounterRef;
2263 stream(SameLine) << (offset > 0 ?
" + " :
" - ") << qAbs(offset);
2265 stream(SameLine) <<
")";
2267 stream(SameLine) <<
" * " << multiplier;
2273bool QQuickQmlGenerator::generatePatternNode(
const PatternNodeInfo &info)
2275 if (info.stage == StructureNodeStage::Start) {
2278 startDefsSuffixBlock();
2279 stream() <<
"Loader {";
2282 stream() <<
"id: " << info.id;
2283 stream() <<
"sourceComponent: " << info.id <<
"_container";
2284 stream() <<
"width: item !== null ? item.originalBounds.width : 0";
2285 stream() <<
"height: item !== null ? item.originalBounds.height : 0";
2286 stream() <<
"visible: false";
2287 stream() <<
"function sourceRect(targetWidth, targetHeight) {";
2290 stream() <<
"return Qt.rect(0, 0, ";
2291 if (!info.isPatternRectRelativeCoordinates) {
2292 stream(SameLine) << info.patternRect.width() <<
", "
2293 << info.patternRect.height();
2295 stream(SameLine) << info.patternRect.width() <<
" * targetWidth, "
2296 << info.patternRect.height() <<
" * targetHeight";
2298 stream(SameLine) <<
")";
2302 stream() <<
"function sourceOffset(targetWidth, targetHeight) {";
2305 stream() <<
"return Qt.vector3d(";
2306 if (!info.isPatternRectRelativeCoordinates) {
2307 stream(SameLine) << info.patternRect.x() <<
", "
2308 << info.patternRect.y() <<
", ";
2310 stream(SameLine) << info.patternRect.x() <<
" * targetWidth, "
2311 << info.patternRect.y() <<
" * targetHeight, ";
2313 stream(SameLine) <<
"0.0)";
2321 endDefsSuffixBlock();
2327void QQuickQmlGenerator::generateDefsInstantiationNode(
const StructureNodeInfo &info)
2329 if (Q_UNLIKELY(errorState()))
2332 if (info.stage == StructureNodeStage::Start) {
2333 stream() <<
"Loader {";
2336 stream() <<
"sourceComponent: " << info.defsId <<
"_container";
2337 generateNodeBase(info);
2338 generateTimelineFields(info);
2345bool QQuickQmlGenerator::generateMarkerNode(
const MarkerNodeInfo &info)
2347 if (info.stage == StructureNodeStage::Start) {
2348 startDefsSuffixBlock();
2349 stream() <<
"QtObject {";
2352 stream() <<
"id: " << info.id <<
"_markerParameters";
2354 stream() <<
"property bool startReversed: ";
2355 if (info.orientation == MarkerNodeInfo::Orientation::AutoStartReverse)
2356 stream(SameLine) <<
"true";
2358 stream(SameLine) <<
"false";
2360 stream() <<
"function autoAngle(adaptedAngle) {";
2362 if (info.orientation == MarkerNodeInfo::Orientation::Value)
2363 stream() <<
"return " << info.angle;
2365 stream() <<
"return adaptedAngle";
2371 endDefsSuffixBlock();
2373 if (!info.clipBox.isEmpty()) {
2374 stream() <<
"Item {";
2377 stream() <<
"x: " << info.clipBox.x();
2378 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2379 stream(SameLine) <<
" * strokeWidth";
2380 stream() <<
"y: " << info.clipBox.y();
2381 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2382 stream(SameLine) <<
" * strokeWidth";
2383 stream() <<
"width: " << info.clipBox.width();
2384 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2385 stream(SameLine) <<
" * strokeWidth";
2386 stream() <<
"height: " << info.clipBox.height();
2387 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2388 stream(SameLine) <<
" * strokeWidth";
2389 stream() <<
"clip: true";
2392 stream() <<
"Item {";
2395 if (!info.clipBox.isEmpty()) {
2396 stream() <<
"x: " << -info.clipBox.x();
2397 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2398 stream(SameLine) <<
" * strokeWidth";
2399 stream() <<
"y: " << -info.clipBox.y();
2400 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2401 stream(SameLine) <<
" * strokeWidth";
2404 stream() <<
"id: " << info.id;
2406 stream() <<
"property real markerWidth: " << info.markerSize.width();
2407 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2408 stream(SameLine) <<
" * strokeWidth";
2410 stream() <<
"property real markerHeight: " << info.markerSize.height();
2411 if (info.markerUnits == MarkerNodeInfo::MarkerUnits::StrokeWidth)
2412 stream(SameLine) <<
" * strokeWidth";
2414 stream() <<
"function calculateMarkerScale(w, h) {";
2417 stream() <<
"var scaleX = 1.0";
2418 stream() <<
"var scaleY = 1.0";
2419 stream() <<
"var offsetX = 0.0";
2420 stream() <<
"var offsetY = 0.0";
2421 if (info.viewBox.width() > 0)
2422 stream() <<
"if (w > 0) scaleX = w / " << info.viewBox.width();
2423 if (info.viewBox.height() > 0)
2424 stream() <<
"if (h > 0) scaleY = h / " << info.viewBox.height();
2426 if (info.preserveAspectRatio & MarkerNodeInfo::xyMask) {
2427 stream() <<
"if (scaleX != scaleY) {";
2430 if (info.preserveAspectRatio & MarkerNodeInfo::meet)
2431 stream() <<
"scaleX = scaleY = Math.min(scaleX, scaleY)";
2433 stream() <<
"scaleX = scaleY = Math.max(scaleX, scaleY)";
2435 QString overflowX = QStringLiteral(
"scaleX * %1 - w").arg(info.viewBox.width());
2436 QString overflowY = QStringLiteral(
"scaleY * %1 - h").arg(info.viewBox.height());
2438 const quint8 xRatio = info.preserveAspectRatio & MarkerNodeInfo::xMask;
2439 if (xRatio == MarkerNodeInfo::xMid)
2440 stream() <<
"offsetX -= " << overflowX <<
" / 2";
2441 else if (xRatio == MarkerNodeInfo::xMax)
2442 stream() <<
"offsetX -= " << overflowX;
2444 const quint8 yRatio = info.preserveAspectRatio & MarkerNodeInfo::yMask;
2445 if (yRatio == MarkerNodeInfo::yMid)
2446 stream() <<
"offsetY -= " << overflowY <<
" / 2";
2447 else if (yRatio == MarkerNodeInfo::yMax)
2448 stream() <<
"offsetY -= " << overflowY;
2454 stream() <<
"return Qt.vector4d("
2455 <<
"offsetX - " << info.anchorPoint.x() <<
" * scaleX, "
2456 <<
"offsetY - " << info.anchorPoint.y() <<
" * scaleY, "
2463 stream() <<
"property vector4d markerScale: calculateMarkerScale(markerWidth, markerHeight)";
2465 stream() <<
"transform: [";
2468 stream() <<
"Scale { xScale: " << info.id <<
".markerScale.z; yScale: " << info.id <<
".markerScale.w },";
2469 stream() <<
"Translate { x: " << info.id <<
".markerScale.x; y: " << info.id <<
".markerScale.y }";
2475 generateNodeEnd(info);
2477 if (!info.clipBox.isEmpty()) {
2486bool QQuickQmlGenerator::generateRootNode(
const StructureNodeInfo &info)
2488 if (Q_UNLIKELY(errorState()))
2491 const QStringList comments = m_commentString.split(u'\n');
2493 if (!isNodeVisible(info)) {
2496 if (comments.isEmpty()) {
2497 stream() <<
"// Generated from SVG";
2499 for (
const auto &comment : comments)
2500 stream() <<
"// " << comment;
2503 stream() <<
"import QtQuick";
2504 stream() <<
"import QtQuick.Shapes" << Qt::endl;
2505 stream() <<
"Item {";
2508 double w = info.size.width();
2509 double h = info.size.height();
2511 stream() <<
"implicitWidth: " << w;
2513 stream() <<
"implicitHeight: " << h;
2521 if (info.stage == StructureNodeStage::Start) {
2524 if (comments.isEmpty())
2525 stream() <<
"// Generated from SVG";
2527 for (
const auto &comment : comments)
2528 stream() <<
"// " << comment;
2530 stream() <<
"import QtQuick";
2531 stream() <<
"import QtQuick.VectorImage";
2532 stream() <<
"import QtQuick.VectorImage.Helpers";
2533 stream() <<
"import QtQuick.Shapes";
2534 stream() <<
"import QtQuick.Effects";
2535 if (usingTimelineAnimation())
2536 stream() <<
"import QtQuick.Timeline";
2538 for (
const auto &import : std::as_const(m_extraImports))
2539 stream() <<
"import " << import;
2541 stream() << Qt::endl <<
"Item {";
2544 double w = info.size.width();
2545 double h = info.size.height();
2547 stream() <<
"implicitWidth: " << w;
2549 stream() <<
"implicitHeight: " << h;
2551 if (Q_UNLIKELY(!isRuntimeGenerator())) {
2552 stream() <<
"component AnimationsInfo : QtObject";
2557 stream() <<
"property bool paused: false";
2558 stream() <<
"property int loops: 1";
2559 stream() <<
"signal restart()";
2561 if (Q_UNLIKELY(!isRuntimeGenerator())) {
2564 stream() <<
"property AnimationsInfo animations : AnimationsInfo {}";
2567 stream() <<
"Item {";
2569 stream() <<
"width: 1";
2570 stream() <<
"height: 1";
2572 stream() <<
"ItemSpy { id: __qt_toplevel_scale_itemspy; anchors.fill: parent }";
2577 if (!info.viewBox.isEmpty()) {
2578 stream() <<
"transform: [";
2580 bool translate = !qFuzzyIsNull(info.viewBox.x()) || !qFuzzyIsNull(info.viewBox.y());
2582 stream() <<
"Translate { x: " << -info.viewBox.x() <<
"; y: " << -info.viewBox.y() <<
" },";
2583 stream() <<
"Scale { xScale: width / " << info.viewBox.width() <<
"; yScale: height / " << info.viewBox.height() <<
" }";
2588 if (!info.forceSeparatePaths && info.isPathContainer) {
2589 m_topLevelIdString = QStringLiteral(
"__qt_toplevel");
2590 stream() <<
"id: " << m_topLevelIdString;
2592 generatePathContainer(info);
2595 generateNodeBase(info);
2597 m_topLevelIdString = generateNodeBase(info);
2598 if (m_topLevelIdString.isEmpty())
2599 qCWarning(lcQuickVectorImage) <<
"No ID specified for top level item";
2602 if (usingTimelineAnimation() && info.timelineInfo) {
2603 stream() <<
"property real startFrame: " << info.timelineInfo->startFrame;
2604 stream() <<
"property real endFrame: " << info.timelineInfo->endFrame;
2605 stream() <<
"property real frameRate: " << info.timelineInfo->frameRate;
2606 stream() <<
"property real frameCounter: " << info.timelineInfo->startFrame;
2607 if (!info.timelineInfo->markers.isEmpty()) {
2608 QStringList markerEntries;
2609 markerEntries.reserve(info.timelineInfo->markers.size());
2610 for (
const LottieMarkerInfo &marker : info.timelineInfo->markers) {
2611 markerEntries << u'"' + sanitizeString(marker.name) + u"\": ["
2612 + QString::number(marker.frame) + u", "
2613 + QString::number(marker.duration) + u']';
2615 stream() <<
"property var markers: ({" << markerEntries.join(
", "_L1) <<
"})";
2617 stream() <<
"NumberAnimation on frameCounter {";
2619 stream() <<
"objectName: \"_qt_frameCounterAnimation\"";
2620 stream() <<
"from: " << m_topLevelIdString <<
".startFrame";
2621 stream() <<
"to: " << m_topLevelIdString <<
".endFrame";
2622 stream() <<
"duration: " << processAnimationTime(1000) <<
" * Math.abs(to - from) / "
2623 <<
"Math.max(" << m_topLevelIdString <<
".frameRate, 1)";
2624 generateAnimationBindings();
2627 stream() <<
"visible: frameCounter >= " << info.timelineInfo->startFrame
2628 <<
" && frameCounter < " << info.timelineInfo->endFrame;
2631 if (m_inShapeItemLevel > 0) {
2632 m_inShapeItemLevel--;
2637 for (
const auto [coords, id] : m_easings.asKeyValueRange()) {
2638 stream() <<
"readonly property easingCurve " << id <<
": ({ type: Easing.BezierSpline, bezierCurve: [ ";
2639 for (
auto coord : coords)
2640 stream(SameLine) << coord <<
", ";
2641 stream(SameLine) <<
"1, 1 ] })";
2644 generateNodeEnd(info);
2651void QQuickQmlGenerator::startDefsSuffixBlock()
2653 int tmp = m_oldIndentLevels.top();
2654 m_oldIndentLevels.push(m_indentLevel);
2655 m_indentLevel = tmp;
2656 m_stream.setString(&m_defsSuffix);
2659void QQuickQmlGenerator::endDefsSuffixBlock()
2661 m_indentLevel = m_oldIndentLevels.pop();
2662 m_stream.setDevice(&m_result);
2665QStringView QQuickQmlGenerator::indent()
2667 int indentWidth = m_indentLevel * 4;
2668 if (indentWidth > m_indentString.size())
2669 m_indentString.fill(QLatin1Char(
' '), indentWidth * 2);
2670 return QStringView(m_indentString).first(indentWidth);
2673QTextStream &QQuickQmlGenerator::stream(
int flags)
2675 if (m_stream.device() ==
nullptr && m_stream.string() ==
nullptr)
2676 m_stream.setDevice(&m_result);
2677 else if (!(flags & StreamFlags::SameLine))
2678 m_stream << Qt::endl << indent();
2680 static qint64 maxBufferSize = qEnvironmentVariableIntegerValue(
"QT_QUICKVECTORIMAGE_MAX_BUFFER").value_or(64 << 20);
2681 if (m_stream.device()) {
2682 if (Q_UNLIKELY(!checkSanityLimit(m_stream.device()->size(), maxBufferSize,
"buffer size"_L1)))
2683 m_stream.device()->reset();
2685 if (Q_UNLIKELY(!checkSanityLimit(m_stream.string()->size(), maxBufferSize,
"buffer string size"_L1)))
2686 m_stream.string()->clear();
2692const char *QQuickQmlGenerator::shapeName()
const
2694 return m_shapeTypeName.isEmpty() ?
"Shape" : m_shapeTypeName.constData();
Combined button and popup list for selecting options.
static QString sanitizeString(const QString &input)
static int processAnimationTime(int time)