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