1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include <private/qquickvaluetypes_p.h> |
5 | |
6 | #include <qtquickglobal.h> |
7 | #include <private/qqmlvaluetype_p.h> |
8 | #include <private/qcolorspace_p.h> |
9 | #include <private/qfont_p.h> |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | QQuickColorValueType::QQuickColorValueType(const QString &string) |
14 | : v(QColor::fromString(name: string)) |
15 | { |
16 | } |
17 | |
18 | QVariant QQuickColorValueType::create(const QJSValue ¶ms) |
19 | { |
20 | return params.isString() ? QColor::fromString(name: params.toString()) : QVariant(); |
21 | } |
22 | |
23 | QString QQuickColorValueType::toString() const |
24 | { |
25 | return v.name(format: v.alpha() != 255 ? QColor::HexArgb : QColor::HexRgb); |
26 | } |
27 | |
28 | QVariant QQuickColorValueType::lighter(qreal factor) const |
29 | { |
30 | return QQml_colorProvider()->lighter(this->v, factor); |
31 | } |
32 | |
33 | QVariant QQuickColorValueType::darker(qreal factor) const |
34 | { |
35 | return QQml_colorProvider()->darker(this->v, factor); |
36 | } |
37 | |
38 | QVariant QQuickColorValueType::alpha(qreal value) const |
39 | { |
40 | return QQml_colorProvider()->alpha(this->v, value); |
41 | } |
42 | |
43 | QVariant QQuickColorValueType::tint(QVariant tintColor) const |
44 | { |
45 | return QQml_colorProvider()->tint(this->v, tintColor); |
46 | } |
47 | |
48 | qreal QQuickColorValueType::r() const |
49 | { |
50 | return v.redF(); |
51 | } |
52 | |
53 | qreal QQuickColorValueType::g() const |
54 | { |
55 | return v.greenF(); |
56 | } |
57 | |
58 | qreal QQuickColorValueType::b() const |
59 | { |
60 | return v.blueF(); |
61 | } |
62 | |
63 | qreal QQuickColorValueType::a() const |
64 | { |
65 | return v.alphaF(); |
66 | } |
67 | |
68 | qreal QQuickColorValueType::hsvHue() const |
69 | { |
70 | return v.hsvHueF(); |
71 | } |
72 | |
73 | qreal QQuickColorValueType::hsvSaturation() const |
74 | { |
75 | return v.hsvSaturationF(); |
76 | } |
77 | |
78 | qreal QQuickColorValueType::hsvValue() const |
79 | { |
80 | return v.valueF(); |
81 | } |
82 | |
83 | qreal QQuickColorValueType::hslHue() const |
84 | { |
85 | return v.hslHueF(); |
86 | } |
87 | |
88 | qreal QQuickColorValueType::hslSaturation() const |
89 | { |
90 | return v.hslSaturationF(); |
91 | } |
92 | |
93 | qreal QQuickColorValueType::hslLightness() const |
94 | { |
95 | return v.lightnessF(); |
96 | } |
97 | |
98 | bool QQuickColorValueType::isValid() const |
99 | { |
100 | return v.isValid(); |
101 | } |
102 | |
103 | void QQuickColorValueType::setR(qreal r) |
104 | { |
105 | v.setRedF(r); |
106 | } |
107 | |
108 | void QQuickColorValueType::setG(qreal g) |
109 | { |
110 | v.setGreenF(g); |
111 | } |
112 | |
113 | void QQuickColorValueType::setB(qreal b) |
114 | { |
115 | v.setBlueF(b); |
116 | } |
117 | |
118 | void QQuickColorValueType::setA(qreal a) |
119 | { |
120 | v.setAlphaF(a); |
121 | } |
122 | |
123 | void QQuickColorValueType::setHsvHue(qreal hsvHue) |
124 | { |
125 | float hue, saturation, value, alpha; |
126 | v.getHsvF(h: &hue, s: &saturation, v: &value, a: &alpha); |
127 | v.setHsvF(h: hsvHue, s: saturation, v: value, a: alpha); |
128 | } |
129 | |
130 | void QQuickColorValueType::setHsvSaturation(qreal hsvSaturation) |
131 | { |
132 | float hue, saturation, value, alpha; |
133 | v.getHsvF(h: &hue, s: &saturation, v: &value, a: &alpha); |
134 | v.setHsvF(h: hue, s: hsvSaturation, v: value, a: alpha); |
135 | } |
136 | |
137 | void QQuickColorValueType::setHsvValue(qreal hsvValue) |
138 | { |
139 | float hue, saturation, value, alpha; |
140 | v.getHsvF(h: &hue, s: &saturation, v: &value, a: &alpha); |
141 | v.setHsvF(h: hue, s: saturation, v: hsvValue, a: alpha); |
142 | } |
143 | |
144 | void QQuickColorValueType::setHslHue(qreal hslHue) |
145 | { |
146 | float hue, saturation, lightness, alpha; |
147 | v.getHslF(h: &hue, s: &saturation, l: &lightness, a: &alpha); |
148 | v.setHslF(h: hslHue, s: saturation, l: lightness, a: alpha); |
149 | } |
150 | |
151 | void QQuickColorValueType::setHslSaturation(qreal hslSaturation) |
152 | { |
153 | float hue, saturation, lightness, alpha; |
154 | v.getHslF(h: &hue, s: &saturation, l: &lightness, a: &alpha); |
155 | v.setHslF(h: hue, s: hslSaturation, l: lightness, a: alpha); |
156 | } |
157 | |
158 | void QQuickColorValueType::setHslLightness(qreal hslLightness) |
159 | { |
160 | float hue, saturation, lightness, alpha; |
161 | v.getHslF(h: &hue, s: &saturation, l: &lightness, a: &alpha); |
162 | v.setHslF(h: hue, s: saturation, l: hslLightness, a: alpha); |
163 | } |
164 | |
165 | template<typename T, int NumParams> |
166 | QVariant createValueTypeFromNumberString(const QString &s) |
167 | { |
168 | Q_STATIC_ASSERT_X(NumParams == 2 || NumParams == 3 || NumParams == 4 || NumParams == 16, |
169 | "Unsupported number of params; add an additional case below if necessary." ); |
170 | |
171 | if (s.count(c: u',') != NumParams - 1) |
172 | return QVariant(); |
173 | |
174 | QVarLengthArray<float, NumParams> parameters; |
175 | bool ok = true; |
176 | for (qsizetype prev = 0, next = s.indexOf(c: u','), length = s.size(); ok && prev < length;) { |
177 | parameters.append(s.mid(position: prev, n: next - prev).toFloat(ok: &ok)); |
178 | prev = next + 1; |
179 | next = (parameters.size() == NumParams - 1) ? length : s.indexOf(c: u',', from: prev); |
180 | } |
181 | |
182 | if (!ok) |
183 | return QVariant(); |
184 | |
185 | if constexpr (NumParams == 2) { |
186 | return T(parameters[0], parameters[1]); |
187 | } else if constexpr (NumParams == 3) { |
188 | return T(parameters[0], parameters[1], parameters[2]); |
189 | } else if constexpr (NumParams == 4) { |
190 | return T(parameters[0], parameters[1], parameters[2], parameters[3]); |
191 | } else if constexpr (NumParams == 16) { |
192 | return T(parameters[0], parameters[1], parameters[2], parameters[3], |
193 | parameters[4], parameters[5], parameters[6], parameters[7], |
194 | parameters[8], parameters[9], parameters[10], parameters[11], |
195 | parameters[12], parameters[13], parameters[14], parameters[15]); |
196 | } else { |
197 | Q_UNREACHABLE(); |
198 | } |
199 | |
200 | return QVariant(); |
201 | } |
202 | |
203 | QVariant QQuickVector2DValueType::create(const QJSValue ¶ms) |
204 | { |
205 | if (params.isString()) |
206 | return createValueTypeFromNumberString<QVector2D, 2>(s: params.toString()); |
207 | if (params.isArray()) |
208 | return QVector2D(params.property(arrayIndex: 0).toNumber(), params.property(arrayIndex: 1).toNumber()); |
209 | return QVariant(); |
210 | } |
211 | |
212 | QString QQuickVector2DValueType::toString() const |
213 | { |
214 | return QString(QLatin1String("QVector2D(%1, %2)" )).arg(a: v.x()).arg(a: v.y()); |
215 | } |
216 | |
217 | qreal QQuickVector2DValueType::x() const |
218 | { |
219 | return v.x(); |
220 | } |
221 | |
222 | qreal QQuickVector2DValueType::y() const |
223 | { |
224 | return v.y(); |
225 | } |
226 | |
227 | void QQuickVector2DValueType::setX(qreal x) |
228 | { |
229 | v.setX(x); |
230 | } |
231 | |
232 | void QQuickVector2DValueType::setY(qreal y) |
233 | { |
234 | v.setY(y); |
235 | } |
236 | |
237 | qreal QQuickVector2DValueType::dotProduct(const QVector2D &vec) const |
238 | { |
239 | return QVector2D::dotProduct(v1: v, v2: vec); |
240 | } |
241 | |
242 | QVector2D QQuickVector2DValueType::times(const QVector2D &vec) const |
243 | { |
244 | return v * vec; |
245 | } |
246 | |
247 | QVector2D QQuickVector2DValueType::times(qreal scalar) const |
248 | { |
249 | return v * scalar; |
250 | } |
251 | |
252 | QVector2D QQuickVector2DValueType::plus(const QVector2D &vec) const |
253 | { |
254 | return v + vec; |
255 | } |
256 | |
257 | QVector2D QQuickVector2DValueType::minus(const QVector2D &vec) const |
258 | { |
259 | return v - vec; |
260 | } |
261 | |
262 | QVector2D QQuickVector2DValueType::normalized() const |
263 | { |
264 | return v.normalized(); |
265 | } |
266 | |
267 | qreal QQuickVector2DValueType::length() const |
268 | { |
269 | return v.length(); |
270 | } |
271 | |
272 | QVector3D QQuickVector2DValueType::toVector3d() const |
273 | { |
274 | return v.toVector3D(); |
275 | } |
276 | |
277 | QVector4D QQuickVector2DValueType::toVector4d() const |
278 | { |
279 | return v.toVector4D(); |
280 | } |
281 | |
282 | bool QQuickVector2DValueType::fuzzyEquals(const QVector2D &vec, qreal epsilon) const |
283 | { |
284 | qreal absEps = qAbs(t: epsilon); |
285 | if (qAbs(t: v.x() - vec.x()) > absEps) |
286 | return false; |
287 | if (qAbs(t: v.y() - vec.y()) > absEps) |
288 | return false; |
289 | return true; |
290 | } |
291 | |
292 | bool QQuickVector2DValueType::fuzzyEquals(const QVector2D &vec) const |
293 | { |
294 | return qFuzzyCompare(v1: v, v2: vec); |
295 | } |
296 | |
297 | QVariant QQuickVector3DValueType::create(const QJSValue ¶ms) |
298 | { |
299 | if (params.isString()) |
300 | return createValueTypeFromNumberString<QVector3D, 3>(s: params.toString()); |
301 | |
302 | if (params.isArray()) { |
303 | return QVector3D(params.property(arrayIndex: 0).toNumber(), params.property(arrayIndex: 1).toNumber(), |
304 | params.property(arrayIndex: 2).toNumber()); |
305 | } |
306 | return QVariant(); |
307 | } |
308 | |
309 | QString QQuickVector3DValueType::toString() const |
310 | { |
311 | return QString(QLatin1String("QVector3D(%1, %2, %3)" )).arg(a: v.x()).arg(a: v.y()).arg(a: v.z()); |
312 | } |
313 | |
314 | qreal QQuickVector3DValueType::x() const |
315 | { |
316 | return v.x(); |
317 | } |
318 | |
319 | qreal QQuickVector3DValueType::y() const |
320 | { |
321 | return v.y(); |
322 | } |
323 | |
324 | qreal QQuickVector3DValueType::z() const |
325 | { |
326 | return v.z(); |
327 | } |
328 | |
329 | void QQuickVector3DValueType::setX(qreal x) |
330 | { |
331 | v.setX(x); |
332 | } |
333 | |
334 | void QQuickVector3DValueType::setY(qreal y) |
335 | { |
336 | v.setY(y); |
337 | } |
338 | |
339 | void QQuickVector3DValueType::setZ(qreal z) |
340 | { |
341 | v.setZ(z); |
342 | } |
343 | |
344 | QVector3D QQuickVector3DValueType::crossProduct(const QVector3D &vec) const |
345 | { |
346 | return QVector3D::crossProduct(v1: v, v2: vec); |
347 | } |
348 | |
349 | qreal QQuickVector3DValueType::dotProduct(const QVector3D &vec) const |
350 | { |
351 | return QVector3D::dotProduct(v1: v, v2: vec); |
352 | } |
353 | |
354 | QVector3D QQuickVector3DValueType::times(const QMatrix4x4 &m) const |
355 | { |
356 | return (QVector4D(v, 1) * m).toVector3DAffine(); |
357 | } |
358 | |
359 | QVector3D QQuickVector3DValueType::times(const QVector3D &vec) const |
360 | { |
361 | return v * vec; |
362 | } |
363 | |
364 | QVector3D QQuickVector3DValueType::times(qreal scalar) const |
365 | { |
366 | return v * scalar; |
367 | } |
368 | |
369 | QVector3D QQuickVector3DValueType::plus(const QVector3D &vec) const |
370 | { |
371 | return v + vec; |
372 | } |
373 | |
374 | QVector3D QQuickVector3DValueType::minus(const QVector3D &vec) const |
375 | { |
376 | return v - vec; |
377 | } |
378 | |
379 | QVector3D QQuickVector3DValueType::normalized() const |
380 | { |
381 | return v.normalized(); |
382 | } |
383 | |
384 | qreal QQuickVector3DValueType::length() const |
385 | { |
386 | return v.length(); |
387 | } |
388 | |
389 | QVector2D QQuickVector3DValueType::toVector2d() const |
390 | { |
391 | return v.toVector2D(); |
392 | } |
393 | |
394 | QVector4D QQuickVector3DValueType::toVector4d() const |
395 | { |
396 | return v.toVector4D(); |
397 | } |
398 | |
399 | bool QQuickVector3DValueType::fuzzyEquals(const QVector3D &vec, qreal epsilon) const |
400 | { |
401 | qreal absEps = qAbs(t: epsilon); |
402 | if (qAbs(t: v.x() - vec.x()) > absEps) |
403 | return false; |
404 | if (qAbs(t: v.y() - vec.y()) > absEps) |
405 | return false; |
406 | if (qAbs(t: v.z() - vec.z()) > absEps) |
407 | return false; |
408 | return true; |
409 | } |
410 | |
411 | bool QQuickVector3DValueType::fuzzyEquals(const QVector3D &vec) const |
412 | { |
413 | return qFuzzyCompare(v1: v, v2: vec); |
414 | } |
415 | |
416 | QVariant QQuickVector4DValueType::create(const QJSValue ¶ms) |
417 | { |
418 | if (params.isString()) |
419 | return createValueTypeFromNumberString<QVector4D, 4>(s: params.toString()); |
420 | |
421 | if (params.isArray()) { |
422 | return QVector4D(params.property(arrayIndex: 0).toNumber(), params.property(arrayIndex: 1).toNumber(), |
423 | params.property(arrayIndex: 2).toNumber(), params.property(arrayIndex: 3).toNumber()); |
424 | } |
425 | |
426 | return QVariant(); |
427 | } |
428 | |
429 | QString QQuickVector4DValueType::toString() const |
430 | { |
431 | return QString(QLatin1String("QVector4D(%1, %2, %3, %4)" )).arg(a: v.x()).arg(a: v.y()).arg(a: v.z()).arg(a: v.w()); |
432 | } |
433 | |
434 | qreal QQuickVector4DValueType::x() const |
435 | { |
436 | return v.x(); |
437 | } |
438 | |
439 | qreal QQuickVector4DValueType::y() const |
440 | { |
441 | return v.y(); |
442 | } |
443 | |
444 | qreal QQuickVector4DValueType::z() const |
445 | { |
446 | return v.z(); |
447 | } |
448 | |
449 | qreal QQuickVector4DValueType::w() const |
450 | { |
451 | return v.w(); |
452 | } |
453 | |
454 | void QQuickVector4DValueType::setX(qreal x) |
455 | { |
456 | v.setX(x); |
457 | } |
458 | |
459 | void QQuickVector4DValueType::setY(qreal y) |
460 | { |
461 | v.setY(y); |
462 | } |
463 | |
464 | void QQuickVector4DValueType::setZ(qreal z) |
465 | { |
466 | v.setZ(z); |
467 | } |
468 | |
469 | void QQuickVector4DValueType::setW(qreal w) |
470 | { |
471 | v.setW(w); |
472 | } |
473 | |
474 | qreal QQuickVector4DValueType::dotProduct(const QVector4D &vec) const |
475 | { |
476 | return QVector4D::dotProduct(v1: v, v2: vec); |
477 | } |
478 | |
479 | QVector4D QQuickVector4DValueType::times(const QVector4D &vec) const |
480 | { |
481 | return v * vec; |
482 | } |
483 | |
484 | QVector4D QQuickVector4DValueType::times(const QMatrix4x4 &m) const |
485 | { |
486 | return v * m; |
487 | } |
488 | |
489 | QVector4D QQuickVector4DValueType::times(qreal scalar) const |
490 | { |
491 | return v * scalar; |
492 | } |
493 | |
494 | QVector4D QQuickVector4DValueType::plus(const QVector4D &vec) const |
495 | { |
496 | return v + vec; |
497 | } |
498 | |
499 | QVector4D QQuickVector4DValueType::minus(const QVector4D &vec) const |
500 | { |
501 | return v - vec; |
502 | } |
503 | |
504 | QVector4D QQuickVector4DValueType::normalized() const |
505 | { |
506 | return v.normalized(); |
507 | } |
508 | |
509 | qreal QQuickVector4DValueType::length() const |
510 | { |
511 | return v.length(); |
512 | } |
513 | |
514 | QVector2D QQuickVector4DValueType::toVector2d() const |
515 | { |
516 | return v.toVector2D(); |
517 | } |
518 | |
519 | QVector3D QQuickVector4DValueType::toVector3d() const |
520 | { |
521 | return v.toVector3D(); |
522 | } |
523 | |
524 | bool QQuickVector4DValueType::fuzzyEquals(const QVector4D &vec, qreal epsilon) const |
525 | { |
526 | qreal absEps = qAbs(t: epsilon); |
527 | if (qAbs(t: v.x() - vec.x()) > absEps) |
528 | return false; |
529 | if (qAbs(t: v.y() - vec.y()) > absEps) |
530 | return false; |
531 | if (qAbs(t: v.z() - vec.z()) > absEps) |
532 | return false; |
533 | if (qAbs(t: v.w() - vec.w()) > absEps) |
534 | return false; |
535 | return true; |
536 | } |
537 | |
538 | bool QQuickVector4DValueType::fuzzyEquals(const QVector4D &vec) const |
539 | { |
540 | return qFuzzyCompare(v1: v, v2: vec); |
541 | } |
542 | |
543 | QVariant QQuickQuaternionValueType::create(const QJSValue ¶ms) |
544 | { |
545 | if (params.isString()) |
546 | return createValueTypeFromNumberString<QQuaternion, 4>(s: params.toString()); |
547 | |
548 | if (params.isArray()) { |
549 | return QQuaternion(params.property(arrayIndex: 0).toNumber(), params.property(arrayIndex: 1).toNumber(), |
550 | params.property(arrayIndex: 2).toNumber(), params.property(arrayIndex: 3).toNumber()); |
551 | } |
552 | |
553 | return QVariant(); |
554 | } |
555 | |
556 | QString QQuickQuaternionValueType::toString() const |
557 | { |
558 | return QString(QLatin1String("QQuaternion(%1, %2, %3, %4)" )).arg(a: v.scalar()).arg(a: v.x()).arg(a: v.y()).arg(a: v.z()); |
559 | } |
560 | |
561 | qreal QQuickQuaternionValueType::scalar() const |
562 | { |
563 | return v.scalar(); |
564 | } |
565 | |
566 | qreal QQuickQuaternionValueType::x() const |
567 | { |
568 | return v.x(); |
569 | } |
570 | |
571 | qreal QQuickQuaternionValueType::y() const |
572 | { |
573 | return v.y(); |
574 | } |
575 | |
576 | qreal QQuickQuaternionValueType::z() const |
577 | { |
578 | return v.z(); |
579 | } |
580 | |
581 | void QQuickQuaternionValueType::setScalar(qreal scalar) |
582 | { |
583 | v.setScalar(scalar); |
584 | } |
585 | |
586 | void QQuickQuaternionValueType::setX(qreal x) |
587 | { |
588 | v.setX(x); |
589 | } |
590 | |
591 | void QQuickQuaternionValueType::setY(qreal y) |
592 | { |
593 | v.setY(y); |
594 | } |
595 | |
596 | void QQuickQuaternionValueType::setZ(qreal z) |
597 | { |
598 | v.setZ(z); |
599 | } |
600 | |
601 | qreal QQuickQuaternionValueType::dotProduct(const QQuaternion &q) const |
602 | { |
603 | return QQuaternion::dotProduct(q1: v, q2: q); |
604 | } |
605 | |
606 | QQuaternion QQuickQuaternionValueType::times(const QQuaternion &q) const |
607 | { |
608 | return v * q; |
609 | } |
610 | |
611 | QVector3D QQuickQuaternionValueType::times(const QVector3D &vec) const |
612 | { |
613 | return v * vec; |
614 | } |
615 | |
616 | QQuaternion QQuickQuaternionValueType::times(qreal factor) const |
617 | { |
618 | return v * factor; |
619 | } |
620 | |
621 | QQuaternion QQuickQuaternionValueType::plus(const QQuaternion &q) const |
622 | { |
623 | return v + q; |
624 | } |
625 | |
626 | QQuaternion QQuickQuaternionValueType::minus(const QQuaternion &q) const |
627 | { |
628 | return v - q; |
629 | } |
630 | |
631 | QQuaternion QQuickQuaternionValueType::normalized() const |
632 | { |
633 | return v.normalized(); |
634 | } |
635 | |
636 | QQuaternion QQuickQuaternionValueType::inverted() const |
637 | { |
638 | return v.inverted(); |
639 | } |
640 | |
641 | QQuaternion QQuickQuaternionValueType::conjugated() const |
642 | { |
643 | return v.conjugated(); |
644 | } |
645 | |
646 | qreal QQuickQuaternionValueType::length() const |
647 | { |
648 | return v.length(); |
649 | } |
650 | |
651 | QVector3D QQuickQuaternionValueType::toEulerAngles() const |
652 | { |
653 | return v.toEulerAngles(); |
654 | } |
655 | |
656 | QVector4D QQuickQuaternionValueType::toVector4d() const |
657 | { |
658 | return v.toVector4D(); |
659 | } |
660 | |
661 | bool QQuickQuaternionValueType::fuzzyEquals(const QQuaternion &q, qreal epsilon) const |
662 | { |
663 | qreal absEps = qAbs(t: epsilon); |
664 | if (qAbs(t: v.scalar() - q.scalar()) > absEps) |
665 | return false; |
666 | if (qAbs(t: v.x() - q.x()) > absEps) |
667 | return false; |
668 | if (qAbs(t: v.y() - q.y()) > absEps) |
669 | return false; |
670 | if (qAbs(t: v.z() - q.z()) > absEps) |
671 | return false; |
672 | return true; |
673 | } |
674 | |
675 | bool QQuickQuaternionValueType::fuzzyEquals(const QQuaternion &q) const |
676 | { |
677 | return qFuzzyCompare(q1: v, q2: q); |
678 | } |
679 | |
680 | QVariant QQuickMatrix4x4ValueType::create(const QJSValue ¶ms) |
681 | { |
682 | if (params.isNull() || params.isUndefined()) |
683 | return QMatrix4x4(); |
684 | |
685 | if (params.isString()) |
686 | return createValueTypeFromNumberString<QMatrix4x4, 16>(s: params.toString()); |
687 | |
688 | if (params.isArray() && params.property(QStringLiteral("length" )).toInt() == 16) { |
689 | return QMatrix4x4(params.property(arrayIndex: 0).toNumber(), |
690 | params.property(arrayIndex: 1).toNumber(), |
691 | params.property(arrayIndex: 2).toNumber(), |
692 | params.property(arrayIndex: 3).toNumber(), |
693 | params.property(arrayIndex: 4).toNumber(), |
694 | params.property(arrayIndex: 5).toNumber(), |
695 | params.property(arrayIndex: 6).toNumber(), |
696 | params.property(arrayIndex: 7).toNumber(), |
697 | params.property(arrayIndex: 8).toNumber(), |
698 | params.property(arrayIndex: 9).toNumber(), |
699 | params.property(arrayIndex: 10).toNumber(), |
700 | params.property(arrayIndex: 11).toNumber(), |
701 | params.property(arrayIndex: 12).toNumber(), |
702 | params.property(arrayIndex: 13).toNumber(), |
703 | params.property(arrayIndex: 14).toNumber(), |
704 | params.property(arrayIndex: 15).toNumber()); |
705 | } |
706 | |
707 | return QVariant(); |
708 | } |
709 | |
710 | QMatrix4x4 QQuickMatrix4x4ValueType::times(const QMatrix4x4 &m) const |
711 | { |
712 | return v * m; |
713 | } |
714 | |
715 | QVector4D QQuickMatrix4x4ValueType::times(const QVector4D &vec) const |
716 | { |
717 | return v * vec; |
718 | } |
719 | |
720 | QVector3D QQuickMatrix4x4ValueType::times(const QVector3D &vec) const |
721 | { |
722 | return v.map(point: vec); |
723 | } |
724 | |
725 | QMatrix4x4 QQuickMatrix4x4ValueType::times(qreal factor) const |
726 | { |
727 | return v * factor; |
728 | } |
729 | |
730 | QMatrix4x4 QQuickMatrix4x4ValueType::plus(const QMatrix4x4 &m) const |
731 | { |
732 | return v + m; |
733 | } |
734 | |
735 | QMatrix4x4 QQuickMatrix4x4ValueType::minus(const QMatrix4x4 &m) const |
736 | { |
737 | return v - m; |
738 | } |
739 | |
740 | QVector4D QQuickMatrix4x4ValueType::row(int n) const |
741 | { |
742 | return v.row(index: n); |
743 | } |
744 | |
745 | QVector4D QQuickMatrix4x4ValueType::column(int m) const |
746 | { |
747 | return v.column(index: m); |
748 | } |
749 | |
750 | qreal QQuickMatrix4x4ValueType::determinant() const |
751 | { |
752 | return v.determinant(); |
753 | } |
754 | |
755 | QMatrix4x4 QQuickMatrix4x4ValueType::inverted() const |
756 | { |
757 | return v.inverted(); |
758 | } |
759 | |
760 | QMatrix4x4 QQuickMatrix4x4ValueType::transposed() const |
761 | { |
762 | return v.transposed(); |
763 | } |
764 | |
765 | QPointF QQuickMatrix4x4ValueType::map(const QPointF p) const |
766 | { |
767 | return v.map(point: p); |
768 | } |
769 | |
770 | QRectF QQuickMatrix4x4ValueType::mapRect(const QRectF r) const |
771 | { |
772 | return v.mapRect(rect: r); |
773 | } |
774 | |
775 | bool QQuickMatrix4x4ValueType::fuzzyEquals(const QMatrix4x4 &m, qreal epsilon) const |
776 | { |
777 | qreal absEps = qAbs(t: epsilon); |
778 | for (int i = 0; i < 4; ++i) { |
779 | for (int j = 0; j < 4; ++j) { |
780 | if (qAbs(t: v(i,j) - m(i,j)) > absEps) { |
781 | return false; |
782 | } |
783 | } |
784 | } |
785 | return true; |
786 | } |
787 | |
788 | bool QQuickMatrix4x4ValueType::fuzzyEquals(const QMatrix4x4 &m) const |
789 | { |
790 | return qFuzzyCompare(m1: v, m2: m); |
791 | } |
792 | |
793 | template<typename T> |
794 | void setFontProperty(QFont &font, void (QFont::*setter)(T value), QString name, |
795 | const QJSValue ¶ms, bool *ok) |
796 | { |
797 | const QJSValue value = params.property(name); |
798 | |
799 | if constexpr (std::is_same_v<T, bool>) { |
800 | if (value.isBool()) { |
801 | (font.*setter)(value.toBool()); |
802 | *ok = true; |
803 | } |
804 | } else if constexpr (std::is_same_v< |
805 | typename std::remove_cv<typename std::remove_reference<T>::type>::type, |
806 | QString>) { |
807 | if (value.isString()) { |
808 | (font.*setter)(value.toString()); |
809 | *ok = true; |
810 | } |
811 | } else if constexpr (std::is_integral_v<T> || std::is_enum_v<T>) { |
812 | if (value.isNumber()) { |
813 | (font.*setter)(T(value.toInt())); |
814 | *ok = true; |
815 | } |
816 | } else if constexpr (std::is_floating_point_v<T>) { |
817 | if (value.isNumber()) { |
818 | (font.*setter)(value.toNumber()); |
819 | *ok = true; |
820 | } |
821 | } |
822 | } |
823 | |
824 | QVariant QQuickFontValueType::create(const QJSValue ¶ms) |
825 | { |
826 | if (!params.isObject()) |
827 | return QVariant(); |
828 | |
829 | bool ok = false; |
830 | QFont ret; |
831 | |
832 | setFontProperty(font&: ret, setter: &QFont::setBold, QStringLiteral("bold" ), params, ok: &ok); |
833 | setFontProperty(font&: ret, setter: &QFont::setCapitalization, QStringLiteral("capitalization" ), params, ok: &ok); |
834 | setFontProperty(font&: ret, setter: &QFont::setFamily, QStringLiteral("family" ), params, ok: &ok); |
835 | setFontProperty(font&: ret, setter: &QFont::setItalic, QStringLiteral("italic" ), params, ok: &ok); |
836 | setFontProperty(font&: ret, setter: &QFont::setPixelSize, QStringLiteral("pixelSize" ), params, ok: &ok); |
837 | setFontProperty(font&: ret, setter: &QFont::setPointSize, QStringLiteral("pointSize" ), params, ok: &ok); |
838 | setFontProperty(font&: ret, setter: &QFont::setStrikeOut, QStringLiteral("strikeout" ), params, ok: &ok); |
839 | setFontProperty(font&: ret, setter: &QFont::setUnderline, QStringLiteral("underline" ), params, ok: &ok); |
840 | setFontProperty(font&: ret, setter: &QFont::setWeight, QStringLiteral("weight" ), params, ok: &ok); |
841 | setFontProperty(font&: ret, setter: &QFont::setWordSpacing, QStringLiteral("wordSpacing" ), params, ok: &ok); |
842 | setFontProperty(font&: ret, setter: &QFont::setHintingPreference, QStringLiteral("hintingPreference" ), params, ok: &ok); |
843 | setFontProperty(font&: ret, setter: &QFont::setKerning, QStringLiteral("kerning" ), params, ok: &ok); |
844 | |
845 | const QJSValue vlspac = params.property(QStringLiteral("letterSpacing" )); |
846 | if (vlspac.isNumber()) { |
847 | ret.setLetterSpacing(type: QFont::AbsoluteSpacing, spacing: vlspac.toNumber()); |
848 | ok = true; |
849 | } |
850 | |
851 | const QJSValue vshaping = params.property(QStringLiteral("preferShaping" )); |
852 | if (vshaping.isBool()) { |
853 | const bool enable = vshaping.toBool(); |
854 | const QFont::StyleStrategy strategy = ret.styleStrategy(); |
855 | if (enable) |
856 | ret.setStyleStrategy(QFont::StyleStrategy(strategy & ~QFont::PreferNoShaping)); |
857 | else |
858 | ret.setStyleStrategy(QFont::StyleStrategy(strategy | QFont::PreferNoShaping)); |
859 | ok = true; |
860 | } |
861 | |
862 | return ok ? ret : QVariant(); |
863 | } |
864 | |
865 | QString QQuickFontValueType::toString() const |
866 | { |
867 | return QString(QLatin1String("QFont(%1)" )).arg(a: v.toString()); |
868 | } |
869 | |
870 | QString QQuickFontValueType::family() const |
871 | { |
872 | return v.family(); |
873 | } |
874 | |
875 | void QQuickFontValueType::setFamily(const QString &family) |
876 | { |
877 | v.setFamily(family); |
878 | } |
879 | |
880 | QString QQuickFontValueType::styleName() const |
881 | { |
882 | return v.styleName(); |
883 | } |
884 | |
885 | void QQuickFontValueType::setStyleName(const QString &style) |
886 | { |
887 | v.setStyleName(style); |
888 | } |
889 | |
890 | bool QQuickFontValueType::bold() const |
891 | { |
892 | return v.bold(); |
893 | } |
894 | |
895 | void QQuickFontValueType::setBold(bool b) |
896 | { |
897 | v.setBold(b); |
898 | } |
899 | |
900 | int QQuickFontValueType::weight() const |
901 | { |
902 | return v.weight(); |
903 | } |
904 | |
905 | void QQuickFontValueType::setWeight(int w) |
906 | { |
907 | v.setWeight(QFont::Weight(w)); |
908 | } |
909 | |
910 | bool QQuickFontValueType::italic() const |
911 | { |
912 | return v.italic(); |
913 | } |
914 | |
915 | void QQuickFontValueType::setItalic(bool b) |
916 | { |
917 | v.setItalic(b); |
918 | } |
919 | |
920 | bool QQuickFontValueType::underline() const |
921 | { |
922 | return v.underline(); |
923 | } |
924 | |
925 | void QQuickFontValueType::setUnderline(bool b) |
926 | { |
927 | v.setUnderline(b); |
928 | } |
929 | |
930 | bool QQuickFontValueType::overline() const |
931 | { |
932 | return v.overline(); |
933 | } |
934 | |
935 | void QQuickFontValueType::setOverline(bool b) |
936 | { |
937 | v.setOverline(b); |
938 | } |
939 | |
940 | bool QQuickFontValueType::strikeout() const |
941 | { |
942 | return v.strikeOut(); |
943 | } |
944 | |
945 | void QQuickFontValueType::setStrikeout(bool b) |
946 | { |
947 | v.setStrikeOut(b); |
948 | } |
949 | |
950 | qreal QQuickFontValueType::pointSize() const |
951 | { |
952 | if (v.pointSizeF() == -1) { |
953 | return v.pixelSize() * qreal(72.) / qreal(qt_defaultDpi()); |
954 | } |
955 | return v.pointSizeF(); |
956 | } |
957 | |
958 | void QQuickFontValueType::setPointSize(qreal size) |
959 | { |
960 | if ((v.resolveMask() & QFont::SizeResolved) && v.pixelSize() != -1) { |
961 | qWarning() << "Both point size and pixel size set. Using pixel size." ; |
962 | return; |
963 | } |
964 | |
965 | if (size >= 0.0) { |
966 | v.setPointSizeF(size); |
967 | } |
968 | } |
969 | |
970 | int QQuickFontValueType::pixelSize() const |
971 | { |
972 | if (v.pixelSize() == -1) { |
973 | return (v.pointSizeF() * qt_defaultDpi()) / qreal(72.); |
974 | } |
975 | return v.pixelSize(); |
976 | } |
977 | |
978 | void QQuickFontValueType::setPixelSize(int size) |
979 | { |
980 | if (size >0) { |
981 | if ((v.resolveMask() & QFont::SizeResolved) && v.pointSizeF() != -1) |
982 | qWarning() << "Both point size and pixel size set. Using pixel size." ; |
983 | v.setPixelSize(size); |
984 | } |
985 | } |
986 | |
987 | QQuickFontEnums::Capitalization QQuickFontValueType::capitalization() const |
988 | { |
989 | return (QQuickFontEnums::Capitalization)v.capitalization(); |
990 | } |
991 | |
992 | void QQuickFontValueType::setCapitalization(QQuickFontEnums::Capitalization c) |
993 | { |
994 | v.setCapitalization((QFont::Capitalization)c); |
995 | } |
996 | |
997 | qreal QQuickFontValueType::letterSpacing() const |
998 | { |
999 | return v.letterSpacing(); |
1000 | } |
1001 | |
1002 | void QQuickFontValueType::setLetterSpacing(qreal size) |
1003 | { |
1004 | v.setLetterSpacing(type: QFont::AbsoluteSpacing, spacing: size); |
1005 | } |
1006 | |
1007 | qreal QQuickFontValueType::wordSpacing() const |
1008 | { |
1009 | return v.wordSpacing(); |
1010 | } |
1011 | |
1012 | void QQuickFontValueType::setWordSpacing(qreal size) |
1013 | { |
1014 | v.setWordSpacing(size); |
1015 | } |
1016 | |
1017 | QQuickFontEnums::HintingPreference QQuickFontValueType::hintingPreference() const |
1018 | { |
1019 | return QQuickFontEnums::HintingPreference(v.hintingPreference()); |
1020 | } |
1021 | |
1022 | void QQuickFontValueType::setHintingPreference(QQuickFontEnums::HintingPreference hintingPreference) |
1023 | { |
1024 | v.setHintingPreference(QFont::HintingPreference(hintingPreference)); |
1025 | } |
1026 | |
1027 | bool QQuickFontValueType::kerning() const |
1028 | { |
1029 | return v.kerning(); |
1030 | } |
1031 | |
1032 | void QQuickFontValueType::setKerning(bool b) |
1033 | { |
1034 | v.setKerning(b); |
1035 | } |
1036 | |
1037 | bool QQuickFontValueType::preferShaping() const |
1038 | { |
1039 | return (v.styleStrategy() & QFont::PreferNoShaping) == 0; |
1040 | } |
1041 | |
1042 | void QQuickFontValueType::setPreferShaping(bool enable) |
1043 | { |
1044 | if (enable) |
1045 | v.setStyleStrategy(static_cast<QFont::StyleStrategy>(v.styleStrategy() & ~QFont::PreferNoShaping)); |
1046 | else |
1047 | v.setStyleStrategy(static_cast<QFont::StyleStrategy>(v.styleStrategy() | QFont::PreferNoShaping)); |
1048 | } |
1049 | |
1050 | void QQuickFontValueType::setFeatures(const QVariantMap &features) |
1051 | { |
1052 | v.clearFeatures(); |
1053 | for (auto it = features.constBegin(); it != features.constEnd(); ++it) { |
1054 | QString featureName = it.key(); |
1055 | quint32 tag = QFont::stringToTag(tagString: featureName.toUtf8()); |
1056 | if (tag == 0) { |
1057 | qWarning() << "Invalid font feature" << featureName << "ignored" ; |
1058 | continue; |
1059 | } |
1060 | |
1061 | bool ok; |
1062 | quint32 value = it.value().toUInt(ok: &ok); |
1063 | if (!ok) { |
1064 | qWarning() << "Font feature value" << it.value() << "is not an integer." ; |
1065 | continue; |
1066 | } |
1067 | |
1068 | v.setFeature(tag, value); |
1069 | } |
1070 | } |
1071 | |
1072 | QVariantMap QQuickFontValueType::features() const |
1073 | { |
1074 | QVariantMap ret; |
1075 | for (quint32 tag : v.featureTags()) { |
1076 | QString featureName = QString::fromUtf8(ba: QFont::tagToString(tag)); |
1077 | |
1078 | ret.insert(key: featureName, value: v.featureValue(tag)); |
1079 | } |
1080 | |
1081 | return ret; |
1082 | } |
1083 | |
1084 | QVariant QQuickColorSpaceValueType::create(const QJSValue ¶ms) |
1085 | { |
1086 | if (!params.isObject()) |
1087 | return QVariant(); |
1088 | |
1089 | |
1090 | const QJSValue vName = params.property(QStringLiteral("namedColorSpace" )); |
1091 | if (vName.isNumber()) |
1092 | return QColorSpace((QColorSpace::NamedColorSpace)vName.toInt()); |
1093 | |
1094 | const QJSValue vPri = params.property(QStringLiteral("primaries" )); |
1095 | const QJSValue vTra = params.property(QStringLiteral("transferFunction" )); |
1096 | if (!vPri.isNumber() || !vTra.isNumber()) |
1097 | return QVariant(); |
1098 | |
1099 | QColorSpace::Primaries pri = static_cast<QColorSpace::Primaries>(vPri.toInt()); |
1100 | QColorSpace::TransferFunction tra = static_cast<QColorSpace::TransferFunction>(vTra.toInt()); |
1101 | float gamma = 0.0f; |
1102 | if (tra == QColorSpace::TransferFunction::Gamma) { |
1103 | const QJSValue vGam = params.property(QStringLiteral("gamma" )); |
1104 | if (!vGam.isNumber()) |
1105 | return QVariant(); |
1106 | gamma = vGam.toNumber(); |
1107 | } |
1108 | |
1109 | return QColorSpace(pri, tra, gamma); |
1110 | } |
1111 | |
1112 | QQuickColorSpaceEnums::NamedColorSpace QQuickColorSpaceValueType::namedColorSpace() const noexcept |
1113 | { |
1114 | if (const auto *p = QColorSpacePrivate::get(colorSpace: v)) |
1115 | return (QQuickColorSpaceEnums::NamedColorSpace)p->namedColorSpace; |
1116 | return QQuickColorSpaceEnums::Unknown; |
1117 | } |
1118 | void QQuickColorSpaceValueType::setNamedColorSpace(QQuickColorSpaceEnums::NamedColorSpace namedColorSpace) |
1119 | { |
1120 | v = { (QColorSpace::NamedColorSpace)namedColorSpace }; |
1121 | } |
1122 | |
1123 | QQuickColorSpaceEnums::Primaries QQuickColorSpaceValueType::primaries() const noexcept |
1124 | { |
1125 | return (QQuickColorSpaceEnums::Primaries)v.primaries(); |
1126 | } |
1127 | |
1128 | void QQuickColorSpaceValueType::setPrimaries(QQuickColorSpaceEnums::Primaries primariesId) |
1129 | { |
1130 | v.setPrimaries((QColorSpace::Primaries)primariesId); |
1131 | } |
1132 | |
1133 | QQuickColorSpaceEnums::TransferFunction QQuickColorSpaceValueType::transferFunction() const noexcept |
1134 | { |
1135 | return (QQuickColorSpaceEnums::TransferFunction)v.transferFunction(); |
1136 | } |
1137 | |
1138 | void QQuickColorSpaceValueType::setTransferFunction(QQuickColorSpaceEnums::TransferFunction transferFunction) |
1139 | { |
1140 | v.setTransferFunction(transferFunction: (QColorSpace::TransferFunction)transferFunction, gamma: v.gamma()); |
1141 | } |
1142 | |
1143 | float QQuickColorSpaceValueType::gamma() const noexcept |
1144 | { |
1145 | return v.gamma(); |
1146 | } |
1147 | |
1148 | void QQuickColorSpaceValueType::setGamma(float gamma) |
1149 | { |
1150 | v.setTransferFunction(transferFunction: v.transferFunction(), gamma); |
1151 | } |
1152 | |
1153 | QT_END_NAMESPACE |
1154 | |
1155 | #include "moc_qquickvaluetypes_p.cpp" |
1156 | |