c0f7e62c1568d7b0cfa5170463e610514aacebb8
[profile/ivi/qtdeclarative.git] / src / quick / util / qquickvaluetypes.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <private/qquickvaluetypes_p.h>
43
44 #include <qtquickglobal.h>
45 #include <private/qqmlvaluetype_p.h>
46 #include <private/qfont_p.h>
47
48
49 QT_BEGIN_NAMESPACE
50
51 namespace QQuickValueTypes {
52     void registerValueTypes()
53     {
54         QQmlValueTypeFactory::registerValueTypes("QtQuick", 2, 0);
55         qmlRegisterValueTypeEnums<QQuickFontValueType>("QtQuick", 2, 0, "Font");
56     }
57 }
58
59 QQuickColorValueType::QQuickColorValueType(QObject *parent)
60     : QQmlValueTypeBase<QColor>(QMetaType::QColor, parent)
61 {
62 }
63
64 QString QQuickColorValueType::toString() const
65 {
66     // to maintain behaviour with QtQuick 1.0, we just output normal toString() value.
67     return QVariant(v).toString();
68 }
69
70 qreal QQuickColorValueType::r() const
71 {
72     return v.redF();
73 }
74
75 qreal QQuickColorValueType::g() const
76 {
77     return v.greenF();
78 }
79
80 qreal QQuickColorValueType::b() const
81 {
82     return v.blueF();
83 }
84
85 qreal QQuickColorValueType::a() const
86 {
87     return v.alphaF();
88 }
89
90 void QQuickColorValueType::setR(qreal r)
91 {
92     v.setRedF(r);
93 }
94
95 void QQuickColorValueType::setG(qreal g)
96 {
97     v.setGreenF(g);
98 }
99
100 void QQuickColorValueType::setB(qreal b)
101 {
102     v.setBlueF(b);
103 }
104
105 void QQuickColorValueType::setA(qreal a)
106 {
107     v.setAlphaF(a);
108 }
109
110
111 QQuickVector2DValueType::QQuickVector2DValueType(QObject *parent)
112     : QQmlValueTypeBase<QVector2D>(QMetaType::QVector2D, parent)
113 {
114 }
115
116 QString QQuickVector2DValueType::toString() const
117 {
118     return QString(QLatin1String("QVector2D(%1, %2)")).arg(v.x()).arg(v.y());
119 }
120
121 bool QQuickVector2DValueType::isEqual(const QVariant &other) const
122 {
123     if (other.userType() != QMetaType::QVector2D)
124         return false;
125
126     QVector2D otherVector = other.value<QVector2D>();
127     return (v == otherVector);
128 }
129
130 qreal QQuickVector2DValueType::x() const
131 {
132     return v.x();
133 }
134
135 qreal QQuickVector2DValueType::y() const
136 {
137     return v.y();
138 }
139
140 void QQuickVector2DValueType::setX(qreal x)
141 {
142     v.setX(x);
143 }
144
145 void QQuickVector2DValueType::setY(qreal y)
146 {
147     v.setY(y);
148 }
149
150 qreal QQuickVector2DValueType::dotProduct(const QVector2D &vec) const
151 {
152     return QVector2D::dotProduct(v, vec);
153 }
154
155 QVector2D QQuickVector2DValueType::times(const QVector2D &vec) const
156 {
157     return v * vec;
158 }
159
160 QVector2D QQuickVector2DValueType::times(qreal scalar) const
161 {
162     return v * scalar;
163 }
164
165 QVector2D QQuickVector2DValueType::plus(const QVector2D &vec) const
166 {
167     return v + vec;
168 }
169
170 QVector2D QQuickVector2DValueType::minus(const QVector2D &vec) const
171 {
172     return v - vec;
173 }
174
175 QVector2D QQuickVector2DValueType::normalized() const
176 {
177     return v.normalized();
178 }
179
180 qreal QQuickVector2DValueType::length() const
181 {
182     return v.length();
183 }
184
185 QVector3D QQuickVector2DValueType::toVector3d() const
186 {
187     return v.toVector3D();
188 }
189
190 QVector4D QQuickVector2DValueType::toVector4d() const
191 {
192     return v.toVector4D();
193 }
194
195 bool QQuickVector2DValueType::fuzzyEquals(const QVector2D &vec, qreal epsilon) const
196 {
197     qreal absEps = qAbs(epsilon);
198     if (qAbs(v.x() - vec.x()) > absEps)
199         return false;
200     if (qAbs(v.y() - vec.y()) > absEps)
201         return false;
202     return true;
203 }
204
205 bool QQuickVector2DValueType::fuzzyEquals(const QVector2D &vec) const
206 {
207     return qFuzzyCompare(v, vec);
208 }
209
210
211 QQuickVector3DValueType::QQuickVector3DValueType(QObject *parent)
212     : QQmlValueTypeBase<QVector3D>(QMetaType::QVector3D, parent)
213 {
214 }
215
216 QString QQuickVector3DValueType::toString() const
217 {
218     return QString(QLatin1String("QVector3D(%1, %2, %3)")).arg(v.x()).arg(v.y()).arg(v.z());
219 }
220
221 bool QQuickVector3DValueType::isEqual(const QVariant &other) const
222 {
223     if (other.userType() != QMetaType::QVector3D)
224         return false;
225
226     QVector3D otherVector = other.value<QVector3D>();
227     return (v == otherVector);
228 }
229
230 qreal QQuickVector3DValueType::x() const
231 {
232     return v.x();
233 }
234
235 qreal QQuickVector3DValueType::y() const
236 {
237     return v.y();
238 }
239
240 qreal QQuickVector3DValueType::z() const
241 {
242     return v.z();
243 }
244
245 void QQuickVector3DValueType::setX(qreal x)
246 {
247     v.setX(x);
248 }
249
250 void QQuickVector3DValueType::setY(qreal y)
251 {
252     v.setY(y);
253 }
254
255 void QQuickVector3DValueType::setZ(qreal z)
256 {
257     v.setZ(z);
258 }
259
260 QVector3D QQuickVector3DValueType::crossProduct(const QVector3D &vec) const
261 {
262     return QVector3D::crossProduct(v, vec);
263 }
264
265 qreal QQuickVector3DValueType::dotProduct(const QVector3D &vec) const
266 {
267     return QVector3D::dotProduct(v, vec);
268 }
269
270 QVector3D QQuickVector3DValueType::times(const QMatrix4x4 &m) const
271 {
272     return v * m;
273 }
274
275 QVector3D QQuickVector3DValueType::times(const QVector3D &vec) const
276 {
277     return v * vec;
278 }
279
280 QVector3D QQuickVector3DValueType::times(qreal scalar) const
281 {
282     return v * scalar;
283 }
284
285 QVector3D QQuickVector3DValueType::plus(const QVector3D &vec) const
286 {
287     return v + vec;
288 }
289
290 QVector3D QQuickVector3DValueType::minus(const QVector3D &vec) const
291 {
292     return v - vec;
293 }
294
295 QVector3D QQuickVector3DValueType::normalized() const
296 {
297     return v.normalized();
298 }
299
300 qreal QQuickVector3DValueType::length() const
301 {
302     return v.length();
303 }
304
305 QVector2D QQuickVector3DValueType::toVector2d() const
306 {
307     return v.toVector2D();
308 }
309
310 QVector4D QQuickVector3DValueType::toVector4d() const
311 {
312     return v.toVector4D();
313 }
314
315 bool QQuickVector3DValueType::fuzzyEquals(const QVector3D &vec, qreal epsilon) const
316 {
317     qreal absEps = qAbs(epsilon);
318     if (qAbs(v.x() - vec.x()) > absEps)
319         return false;
320     if (qAbs(v.y() - vec.y()) > absEps)
321         return false;
322     if (qAbs(v.z() - vec.z()) > absEps)
323         return false;
324     return true;
325 }
326
327 bool QQuickVector3DValueType::fuzzyEquals(const QVector3D &vec) const
328 {
329     return qFuzzyCompare(v, vec);
330 }
331
332
333 QQuickVector4DValueType::QQuickVector4DValueType(QObject *parent)
334     : QQmlValueTypeBase<QVector4D>(QMetaType::QVector4D, parent)
335 {
336 }
337
338 QString QQuickVector4DValueType::toString() const
339 {
340     return QString(QLatin1String("QVector4D(%1, %2, %3, %4)")).arg(v.x()).arg(v.y()).arg(v.z()).arg(v.w());
341 }
342
343 bool QQuickVector4DValueType::isEqual(const QVariant &other) const
344 {
345     if (other.userType() != QMetaType::QVector4D)
346         return false;
347
348     QVector4D otherVector = other.value<QVector4D>();
349     return (v == otherVector);
350 }
351
352 qreal QQuickVector4DValueType::x() const
353 {
354     return v.x();
355 }
356
357 qreal QQuickVector4DValueType::y() const
358 {
359     return v.y();
360 }
361
362 qreal QQuickVector4DValueType::z() const
363 {
364     return v.z();
365 }
366
367 qreal QQuickVector4DValueType::w() const
368 {
369     return v.w();
370 }
371
372 void QQuickVector4DValueType::setX(qreal x)
373 {
374     v.setX(x);
375 }
376
377 void QQuickVector4DValueType::setY(qreal y)
378 {
379     v.setY(y);
380 }
381
382 void QQuickVector4DValueType::setZ(qreal z)
383 {
384     v.setZ(z);
385 }
386
387 void QQuickVector4DValueType::setW(qreal w)
388 {
389     v.setW(w);
390 }
391
392 qreal QQuickVector4DValueType::dotProduct(const QVector4D &vec) const
393 {
394     return QVector4D::dotProduct(v, vec);
395 }
396
397 QVector4D QQuickVector4DValueType::times(const QVector4D &vec) const
398 {
399     return v * vec;
400 }
401
402 QVector4D QQuickVector4DValueType::times(const QMatrix4x4 &m) const
403 {
404     return v * m;
405 }
406
407 QVector4D QQuickVector4DValueType::times(qreal scalar) const
408 {
409     return v * scalar;
410 }
411
412 QVector4D QQuickVector4DValueType::plus(const QVector4D &vec) const
413 {
414     return v + vec;
415 }
416
417 QVector4D QQuickVector4DValueType::minus(const QVector4D &vec) const
418 {
419     return v - vec;
420 }
421
422 QVector4D QQuickVector4DValueType::normalized() const
423 {
424     return v.normalized();
425 }
426
427 qreal QQuickVector4DValueType::length() const
428 {
429     return v.length();
430 }
431
432 QVector2D QQuickVector4DValueType::toVector2d() const
433 {
434     return v.toVector2D();
435 }
436
437 QVector3D QQuickVector4DValueType::toVector3d() const
438 {
439     return v.toVector3D();
440 }
441
442 bool QQuickVector4DValueType::fuzzyEquals(const QVector4D &vec, qreal epsilon) const
443 {
444     qreal absEps = qAbs(epsilon);
445     if (qAbs(v.x() - vec.x()) > absEps)
446         return false;
447     if (qAbs(v.y() - vec.y()) > absEps)
448         return false;
449     if (qAbs(v.z() - vec.z()) > absEps)
450         return false;
451     if (qAbs(v.w() - vec.w()) > absEps)
452         return false;
453     return true;
454 }
455
456 bool QQuickVector4DValueType::fuzzyEquals(const QVector4D &vec) const
457 {
458     return qFuzzyCompare(v, vec);
459 }
460
461 QQuickQuaternionValueType::QQuickQuaternionValueType(QObject *parent)
462     : QQmlValueTypeBase<QQuaternion>(QMetaType::QQuaternion, parent)
463 {
464 }
465
466 QString QQuickQuaternionValueType::toString() const
467 {
468     return QString(QLatin1String("QQuaternion(%1, %2, %3, %4)")).arg(v.scalar()).arg(v.x()).arg(v.y()).arg(v.z());
469 }
470
471 qreal QQuickQuaternionValueType::scalar() const
472 {
473     return v.scalar();
474 }
475
476 qreal QQuickQuaternionValueType::x() const
477 {
478     return v.x();
479 }
480
481 qreal QQuickQuaternionValueType::y() const
482 {
483     return v.y();
484 }
485
486 qreal QQuickQuaternionValueType::z() const
487 {
488     return v.z();
489 }
490
491 void QQuickQuaternionValueType::setScalar(qreal scalar)
492 {
493     v.setScalar(scalar);
494 }
495
496 void QQuickQuaternionValueType::setX(qreal x)
497 {
498     v.setX(x);
499 }
500
501 void QQuickQuaternionValueType::setY(qreal y)
502 {
503     v.setY(y);
504 }
505
506 void QQuickQuaternionValueType::setZ(qreal z)
507 {
508     v.setZ(z);
509 }
510
511
512 QQuickMatrix4x4ValueType::QQuickMatrix4x4ValueType(QObject *parent)
513     : QQmlValueTypeBase<QMatrix4x4>(QMetaType::QMatrix4x4, parent)
514 {
515 }
516
517 QMatrix4x4 QQuickMatrix4x4ValueType::times(const QMatrix4x4 &m) const
518 {
519     return v * m;
520 }
521
522 QVector4D QQuickMatrix4x4ValueType::times(const QVector4D &vec) const
523 {
524     return v * vec;
525 }
526
527 QVector3D QQuickMatrix4x4ValueType::times(const QVector3D &vec) const
528 {
529     return v * vec;
530 }
531
532 QMatrix4x4 QQuickMatrix4x4ValueType::times(qreal factor) const
533 {
534     return v * factor;
535 }
536
537 QMatrix4x4 QQuickMatrix4x4ValueType::plus(const QMatrix4x4 &m) const
538 {
539     return v + m;
540 }
541
542 QMatrix4x4 QQuickMatrix4x4ValueType::minus(const QMatrix4x4 &m) const
543 {
544     return v - m;
545 }
546
547 QVector4D QQuickMatrix4x4ValueType::row(int n) const
548 {
549     return v.row(n);
550 }
551
552 QVector4D QQuickMatrix4x4ValueType::column(int m) const
553 {
554     return v.column(m);
555 }
556
557 qreal QQuickMatrix4x4ValueType::determinant() const
558 {
559     return v.determinant();
560 }
561
562 QMatrix4x4 QQuickMatrix4x4ValueType::inverted() const
563 {
564     return v.inverted();
565 }
566
567 QMatrix4x4 QQuickMatrix4x4ValueType::transposed() const
568 {
569     return v.transposed();
570 }
571
572 bool QQuickMatrix4x4ValueType::fuzzyEquals(const QMatrix4x4 &m, qreal epsilon) const
573 {
574     qreal absEps = qAbs(epsilon);
575     for (int i = 0; i < 4; ++i) {
576         for (int j = 0; j < 4; ++j) {
577             if (qAbs(v(i,j) - m(i,j)) > absEps) {
578                 return false;
579             }
580         }
581     }
582     return true;
583 }
584
585 bool QQuickMatrix4x4ValueType::fuzzyEquals(const QMatrix4x4 &m) const
586 {
587     return qFuzzyCompare(v, m);
588 }
589
590 QString QQuickMatrix4x4ValueType::toString() const
591 {
592     return QString(QLatin1String("QMatrix4x4(%1, %2, %3, %4, %5, %6, %7, %8, %9, %10, %11, %12, %13, %14, %15, %16)"))
593             .arg(v(0, 0)).arg(v(0, 1)).arg(v(0, 2)).arg(v(0, 3))
594             .arg(v(1, 0)).arg(v(1, 1)).arg(v(1, 2)).arg(v(1, 3))
595             .arg(v(2, 0)).arg(v(2, 1)).arg(v(2, 2)).arg(v(2, 3))
596             .arg(v(3, 0)).arg(v(3, 1)).arg(v(3, 2)).arg(v(3, 3));
597 }
598
599 bool QQuickMatrix4x4ValueType::isEqual(const QVariant &other) const
600 {
601     if (other.userType() != qMetaTypeId<QMatrix4x4>())
602         return false;
603
604     QMatrix4x4 otherMatrix = other.value<QMatrix4x4>();
605     return (v == otherMatrix);
606
607 }
608
609 QQuickFontValueType::QQuickFontValueType(QObject *parent)
610     : QQmlValueTypeBase<QFont>(QMetaType::QFont, parent),
611       pixelSizeSet(false),
612       pointSizeSet(false)
613 {
614 }
615
616 void QQuickFontValueType::onLoad()
617 {
618     pixelSizeSet = false;
619     pointSizeSet = false;
620 }
621
622 QString QQuickFontValueType::toString() const
623 {
624     return QString(QLatin1String("QFont(%1)")).arg(v.toString());
625 }
626
627 QString QQuickFontValueType::family() const
628 {
629     return v.family();
630 }
631
632 void QQuickFontValueType::setFamily(const QString &family)
633 {
634     v.setFamily(family);
635 }
636
637 bool QQuickFontValueType::bold() const
638 {
639     return v.bold();
640 }
641
642 void QQuickFontValueType::setBold(bool b)
643 {
644     v.setBold(b);
645 }
646
647 QQuickFontValueType::FontWeight QQuickFontValueType::weight() const
648 {
649     return (QQuickFontValueType::FontWeight)v.weight();
650 }
651
652 void QQuickFontValueType::setWeight(QQuickFontValueType::FontWeight w)
653 {
654     v.setWeight((QFont::Weight)w);
655 }
656
657 bool QQuickFontValueType::italic() const
658 {
659     return v.italic();
660 }
661
662 void QQuickFontValueType::setItalic(bool b)
663 {
664     v.setItalic(b);
665 }
666
667 bool QQuickFontValueType::underline() const
668 {
669     return v.underline();
670 }
671
672 void QQuickFontValueType::setUnderline(bool b)
673 {
674     v.setUnderline(b);
675 }
676
677 bool QQuickFontValueType::overline() const
678 {
679     return v.overline();
680 }
681
682 void QQuickFontValueType::setOverline(bool b)
683 {
684     v.setOverline(b);
685 }
686
687 bool QQuickFontValueType::strikeout() const
688 {
689     return v.strikeOut();
690 }
691
692 void QQuickFontValueType::setStrikeout(bool b)
693 {
694     v.setStrikeOut(b);
695 }
696
697 qreal QQuickFontValueType::pointSize() const
698 {
699     if (v.pointSizeF() == -1) {
700         if (dpi.isNull)
701             dpi = qt_defaultDpi();
702         return v.pixelSize() * qreal(72.) / qreal(dpi);
703     }
704     return v.pointSizeF();
705 }
706
707 void QQuickFontValueType::setPointSize(qreal size)
708 {
709     if (pixelSizeSet) {
710         qWarning() << "Both point size and pixel size set. Using pixel size.";
711         return;
712     }
713
714     if (size >= 0.0) {
715         pointSizeSet = true;
716         v.setPointSizeF(size);
717     } else {
718         pointSizeSet = false;
719     }
720 }
721
722 int QQuickFontValueType::pixelSize() const
723 {
724     if (v.pixelSize() == -1) {
725         if (dpi.isNull)
726             dpi = qt_defaultDpi();
727         return (v.pointSizeF() * dpi) / qreal(72.);
728     }
729     return v.pixelSize();
730 }
731
732 void QQuickFontValueType::setPixelSize(int size)
733 {
734     if (size >0) {
735         if (pointSizeSet)
736             qWarning() << "Both point size and pixel size set. Using pixel size.";
737         v.setPixelSize(size);
738         pixelSizeSet = true;
739     } else {
740         pixelSizeSet = false;
741     }
742 }
743
744 QQuickFontValueType::Capitalization QQuickFontValueType::capitalization() const
745 {
746     return (QQuickFontValueType::Capitalization)v.capitalization();
747 }
748
749 void QQuickFontValueType::setCapitalization(QQuickFontValueType::Capitalization c)
750 {
751     v.setCapitalization((QFont::Capitalization)c);
752 }
753
754 qreal QQuickFontValueType::letterSpacing() const
755 {
756     return v.letterSpacing();
757 }
758
759 void QQuickFontValueType::setLetterSpacing(qreal size)
760 {
761     v.setLetterSpacing(QFont::AbsoluteSpacing, size);
762 }
763
764 qreal QQuickFontValueType::wordSpacing() const
765 {
766     return v.wordSpacing();
767 }
768
769 void QQuickFontValueType::setWordSpacing(qreal size)
770 {
771     v.setWordSpacing(size);
772 }
773
774 QT_END_NAMESPACE