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