Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlvaluetype.cpp
Go to the documentation of this file.
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
5
6#include <QtCore/qmutex.h>
7#include <private/qqmlglobal_p.h>
8#include <QtCore/qdebug.h>
9#include <private/qqmlengine_p.h>
10#include <private/qmetaobjectbuilder_p.h>
11
13
14QQmlValueType::~QQmlValueType()
15{
16 ::free(m_dynamicMetaObject);
17}
18
19QQmlGadgetPtrWrapper *QQmlGadgetPtrWrapper::instance(QQmlEngine *engine, QMetaType type)
20{
21 return engine ? QQmlEnginePrivate::get(engine)->valueTypeInstance(type) : nullptr;
22}
23
24QQmlGadgetPtrWrapper::QQmlGadgetPtrWrapper(QQmlValueType *valueType, QObject *parent)
25 : QObject(parent), m_gadgetPtr(valueType->create())
26{
27 QObjectPrivate *d = QObjectPrivate::get(this);
28 Q_ASSERT(!d->metaObject);
29 d->metaObject = valueType;
30}
31
32QQmlGadgetPtrWrapper::~QQmlGadgetPtrWrapper()
33{
34 QObjectPrivate *d = QObjectPrivate::get(this);
35 static_cast<const QQmlValueType *>(d->metaObject)->destroy(m_gadgetPtr);
36 d->metaObject = nullptr;
37}
38
39void QQmlGadgetPtrWrapper::read(QObject *obj, int idx)
40{
41 Q_ASSERT(m_gadgetPtr);
42 void *a[] = { m_gadgetPtr, nullptr };
43 QMetaObject::metacall(obj, QMetaObject::ReadProperty, idx, a);
44}
45
46void QQmlGadgetPtrWrapper::write(
47 QObject *obj, int idx, QQmlPropertyData::WriteFlags flags, int internalIndex) const
48{
49 Q_ASSERT(m_gadgetPtr);
50 int status = -1;
51 void *a[] = { m_gadgetPtr, nullptr, &status, &flags, &internalIndex };
52 QMetaObject::metacall(obj, QMetaObject::WriteProperty, idx, a);
53}
54
55QVariant QQmlGadgetPtrWrapper::value() const
56{
57 Q_ASSERT(m_gadgetPtr);
58
59 const QMetaType m = metaType();
60 return m == QMetaType::fromType<QVariant>()
61 ? *static_cast<const QVariant *>(m_gadgetPtr)
62 : QVariant(m, m_gadgetPtr);
63}
64
65void QQmlGadgetPtrWrapper::setValue(const QVariant &value)
66{
67 Q_ASSERT(m_gadgetPtr);
68
69 const QMetaType m = metaType();
70 m.destruct(m_gadgetPtr);
71 if (m == QMetaType::fromType<QVariant>()) {
72 m.construct(m_gadgetPtr, &value);
73 } else {
74 Q_ASSERT(m == value.metaType());
75 m.construct(m_gadgetPtr, value.constData());
76 }
77}
78
79int QQmlGadgetPtrWrapper::metaCall(QMetaObject::Call type, int id, void **argv)
80{
81 Q_ASSERT(m_gadgetPtr);
82 const QMetaObject *metaObject = valueType()->staticMetaObject();
83 QQmlMetaObject::resolveGadgetMethodOrPropertyIndex(type, &metaObject, &id);
84 metaObject->d.static_metacall(static_cast<QObject *>(m_gadgetPtr), type, id, argv);
85 return id;
86}
87
88const QQmlValueType *QQmlGadgetPtrWrapper::valueType() const
89{
90 const QObjectPrivate *d = QObjectPrivate::get(this);
91 return static_cast<const QQmlValueType *>(d->metaObject);
92}
93
94#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0)
95const QMetaObject *QQmlValueType::toDynamicMetaObject(QObject *) const
96#else
97QMetaObject *QQmlValueType::toDynamicMetaObject(QObject *)
98#endif
99{
100 if (!m_dynamicMetaObject) {
101 QMetaObjectBuilder builder(m_staticMetaObject);
102
103 // Do not set PropertyAccessInStaticMetaCall here. QQmlGadgetPtrWrapper likes to
104 // to intercept the metacalls since it needs to use its gadgetPtr.
105 // For QQmlValueType::metaObject() we use the base type that has the flag.
106
107 m_dynamicMetaObject = builder.toMetaObject();
108 }
109 return m_dynamicMetaObject;
110}
111
112void QQmlValueType::objectDestroyed(QObject *)
113{
114}
115
116int QQmlValueType::metaCall(QObject *object, QMetaObject::Call type, int _id, void **argv)
117{
118 return static_cast<QQmlGadgetPtrWrapper *>(object)->metaCall(type, _id, argv);
119}
120
121QString QQmlPointFValueType::toString() const
122{
123 return QString::asprintf("QPointF(%g, %g)", QPointF::x(), QPointF::y());
124}
125
126qreal QQmlPointFValueType::x() const
127{
128 return QPointF::x();
129}
130
131qreal QQmlPointFValueType::y() const
132{
133 return QPointF::y();
134}
135
136void QQmlPointFValueType::setX(qreal x)
137{
138 QPointF::setX(x);
139}
140
141void QQmlPointFValueType::setY(qreal y)
142{
143 QPointF::setY(y);
144}
145
146
147QString QQmlPointValueType::toString() const
148{
149 return QString::asprintf("QPoint(%d, %d)", QPoint::x(), QPoint::y());
150}
151
152int QQmlPointValueType::x() const
153{
154 return QPoint::x();
155}
156
157int QQmlPointValueType::y() const
158{
159 return QPoint::y();
160}
161
162void QQmlPointValueType::setX(int x)
163{
164 QPoint::setX(x);
165}
166
167void QQmlPointValueType::setY(int y)
168{
169 QPoint::setY(y);
170}
171
172
173QString QQmlSizeFValueType::toString() const
174{
175 return QString::asprintf("QSizeF(%g, %g)", QSizeF::width(), QSizeF::height());
176}
177
178qreal QQmlSizeFValueType::width() const
179{
180 return QSizeF::width();
181}
182
183qreal QQmlSizeFValueType::height() const
184{
185 return QSizeF::height();
186}
187
188void QQmlSizeFValueType::setWidth(qreal w)
189{
190 QSizeF::setWidth(w);
191}
192
193void QQmlSizeFValueType::setHeight(qreal h)
194{
195 QSizeF::setHeight(h);
196}
197
198
199QString QQmlSizeValueType::toString() const
200{
201 return QString::asprintf("QSize(%d, %d)", QSize::width(), QSize::height());
202}
203
204int QQmlSizeValueType::width() const
205{
206 return QSize::width();
207}
208
209int QQmlSizeValueType::height() const
210{
211 return QSize::height();
212}
213
214void QQmlSizeValueType::setWidth(int w)
215{
216 QSize::setWidth(w);
217}
218
219void QQmlSizeValueType::setHeight(int h)
220{
221 QSize::setHeight(h);
222}
223
224QString QQmlRectFValueType::toString() const
225{
226 return QString::asprintf(
227 "QRectF(%g, %g, %g, %g)", QRectF::x(), QRectF::y(), QRectF::width(), QRectF::height());
228}
229
230qreal QQmlRectFValueType::x() const
231{
232 return QRectF::x();
233}
234
235qreal QQmlRectFValueType::y() const
236{
237 return QRectF::y();
238}
239
240void QQmlRectFValueType::setX(qreal x)
241{
242 QRectF::moveLeft(x);
243}
244
245void QQmlRectFValueType::setY(qreal y)
246{
247 QRectF::moveTop(y);
248}
249
250qreal QQmlRectFValueType::width() const
251{
252 return QRectF::width();
253}
254
255qreal QQmlRectFValueType::height() const
256{
257 return QRectF::height();
258}
259
260void QQmlRectFValueType::setWidth(qreal w)
261{
262 QRectF::setWidth(w);
263}
264
265void QQmlRectFValueType::setHeight(qreal h)
266{
267 QRectF::setHeight(h);
268}
269
270qreal QQmlRectFValueType::left() const
271{
272 return QRectF::left();
273}
274
275qreal QQmlRectFValueType::right() const
276{
277 return QRectF::right();
278}
279
280qreal QQmlRectFValueType::top() const
281{
282 return QRectF::top();
283}
284
285qreal QQmlRectFValueType::bottom() const
286{
287 return QRectF::bottom();
288}
289
290
291QString QQmlRectValueType::toString() const
292{
293 return QString::asprintf(
294 "QRect(%d, %d, %d, %d)", QRect::x(), QRect::y(), QRect::width(), QRect::height());
295}
296
297int QQmlRectValueType::x() const
298{
299 return QRect::x();
300}
301
302int QQmlRectValueType::y() const
303{
304 return QRect::y();
305}
306
307void QQmlRectValueType::setX(int x)
308{
309 QRect::moveLeft(x);
310}
311
312void QQmlRectValueType::setY(int y)
313{
314 QRect::moveTop(y);
315}
316
317int QQmlRectValueType::width() const
318{
319 return QRect::width();
320}
321
322int QQmlRectValueType::height() const
323{
324 return QRect::height();
325}
326
327void QQmlRectValueType::setWidth(int w)
328{
329 QRect::setWidth(w);
330}
331
332void QQmlRectValueType::setHeight(int h)
333{
334 QRect::setHeight(h);
335}
336
337int QQmlRectValueType::left() const
338{
339 return QRect::left();
340}
341
342int QQmlRectValueType::right() const
343{
344 return QRect::right();
345}
346
347int QQmlRectValueType::top() const
348{
349 return QRect::top();
350}
351
352int QQmlRectValueType::bottom() const
353{
354 return QRect::bottom();
355}
356
357QString QQmlMarginsFValueType::toString() const
358{
359 return QString::asprintf(
360 "QMarginsF(%g, %g, %g, %g)",
361 QMarginsF::left(), QMarginsF::top(), QMarginsF::right(), QMarginsF::bottom());
362}
363
364qreal QQmlMarginsFValueType::left() const
365{
366 return QMarginsF::left();
367}
368
369qreal QQmlMarginsFValueType::top() const
370{
371 return QMarginsF::top();
372}
373
374qreal QQmlMarginsFValueType::right() const
375{
376 return QMarginsF::right();
377}
378
379qreal QQmlMarginsFValueType::bottom() const
380{
381 return QMarginsF::bottom();
382}
383
384void QQmlMarginsFValueType::setLeft(qreal left)
385{
386 QMarginsF::setLeft(left);
387}
388
389void QQmlMarginsFValueType::setTop(qreal top)
390{
391 QMarginsF::setTop(top);
392}
393
394void QQmlMarginsFValueType::setRight(qreal right)
395{
396 QMarginsF::setRight(right);
397}
398
399void QQmlMarginsFValueType::setBottom(qreal bottom)
400{
401 QMarginsF::setBottom(bottom);
402}
403
404QString QQmlMarginsValueType::toString() const
405{
406 return QString::asprintf(
407 "QMargins(%d, %d, %d, %d)",
408 QMargins::left(), QMargins::top(), QMargins::right(), QMargins::bottom());
409}
410
411int QQmlMarginsValueType::left() const
412{
413 return QMargins::left();
414}
415
416int QQmlMarginsValueType::top() const
417{
418 return QMargins::top();
419}
420
421int QQmlMarginsValueType::right() const
422{
423 return QMargins::right();
424}
425
426int QQmlMarginsValueType::bottom() const
427{
428 return QMargins::bottom();
429}
430
431void QQmlMarginsValueType::setLeft(int left)
432{
433 QMargins::setLeft(left);
434}
435
436void QQmlMarginsValueType::setTop(int top)
437{
438 QMargins::setTop(top);
439}
440
441void QQmlMarginsValueType::setRight(int right)
442{
443 QMargins::setRight(right);
444}
445
446void QQmlMarginsValueType::setBottom(int bottom)
447{
448 QMargins::setBottom(bottom);
449}
450
451#if QT_CONFIG(easingcurve)
452qreal QQmlEasing::valueForProgress(Type type, qreal progress) const
453{
454 return QEasingCurve(static_cast<QEasingCurve::Type>(type)).valueForProgress(progress);
455}
456
457QQmlEasingValueType::QQmlEasingValueType(QQmlEasing::Type type)
458 : QEasingCurve(static_cast<QEasingCurve::Type>(type))
459{
460}
461
462qreal QQmlEasingValueType::valueForProgress(qreal progress)
463{
464 return QEasingCurve::valueForProgress(progress);
465}
466
467QQmlEasing::Type QQmlEasingValueType::type() const
468{
469 return (QQmlEasing::Type)QEasingCurve::type();
470}
471
472qreal QQmlEasingValueType::amplitude() const
473{
474 return QEasingCurve::amplitude();
475}
476
477qreal QQmlEasingValueType::overshoot() const
478{
479 return QEasingCurve::overshoot();
480}
481
482qreal QQmlEasingValueType::period() const
483{
484 return QEasingCurve::period();
485}
486
487void QQmlEasingValueType::setType(QQmlEasing::Type type)
488{
489 QEasingCurve::setType((QEasingCurve::Type)type);
490}
491
492void QQmlEasingValueType::setAmplitude(qreal amplitude)
493{
494 QEasingCurve::setAmplitude(amplitude);
495}
496
497void QQmlEasingValueType::setOvershoot(qreal overshoot)
498{
499 QEasingCurve::setOvershoot(overshoot);
500}
501
502void QQmlEasingValueType::setPeriod(qreal period)
503{
504 QEasingCurve::setPeriod(period);
505}
506
507void QQmlEasingValueType::setBezierCurve(const QList<qreal> &customCurveVariant)
508{
509 if (customCurveVariant.isEmpty())
510 return;
511
512 if ((customCurveVariant.size() % 6) != 0)
513 return;
514
515 QEasingCurve newEasingCurve(QEasingCurve::BezierSpline);
516 for (int i = 0, ei = customCurveVariant.size(); i < ei; i += 6) {
517 const qreal c1x = customCurveVariant.at(i );
518 const qreal c1y = customCurveVariant.at(i + 1);
519 const qreal c2x = customCurveVariant.at(i + 2);
520 const qreal c2y = customCurveVariant.at(i + 3);
521 const qreal c3x = customCurveVariant.at(i + 4);
522 const qreal c3y = customCurveVariant.at(i + 5);
523
524 const QPointF c1(c1x, c1y);
525 const QPointF c2(c2x, c2y);
526 const QPointF c3(c3x, c3y);
527
528 newEasingCurve.addCubicBezierSegment(c1, c2, c3);
529 }
530
531 *this = newEasingCurve;
532}
533
534QList<qreal> QQmlEasingValueType::bezierCurve() const
535{
536 QList<qreal> rv;
537 const QVector<QPointF> points = QEasingCurve::toCubicSpline();
538 rv.reserve(points.size() * 2);
539 for (const auto &point : points)
540 rv << point.x() << point.y();
541 return rv;
542}
543#endif // easingcurve
544
545
546QT_END_NAMESPACE
547
548#include "moc_qqmlvaluetype_p.cpp"