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