summaryrefslogtreecommitdiffstats
path: root/src/declarative/fx/qfxtransform.cpp
blob: 7b7636739dfc9db995e17e037d5cfced7e4d329f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QDebug>
#include "private/qfxitem_p.h"
#include "qfxtransform.h"
#include <QtDeclarative/qmlinfo.h>

#include <math.h>
#ifndef M_PI

QT_BEGIN_NAMESPACE
#define M_PI 3.14159265358979323846
#endif

QML_DEFINE_NOCREATE_TYPE(QFxTransform);

/*!
    \qmlclass Transform
    \brief A transformation.
*/
QFxTransform::QFxTransform(QObject *parent) :
    QObject(parent)
{
}

QFxTransform::~QFxTransform()
{
}

bool QFxTransform::isIdentity() const
{
    return true;
}

QSimpleCanvas::Matrix QFxTransform::transform() const
{
    return QSimpleCanvas::Matrix();
}

void QFxTransform::update()
{
    QFxItem *item = qobject_cast<QFxItem *>(parent());
    if (item)
        item->updateTransform();
}

/*!
    \qmlclass Axis
    \brief A Axis object defines an axis that can be used for rotation or translation.

    An axis is specified by 2 points in 3D space: a start point and
    an end point. While technically the axis is the line running through these two points
    (and thus many different sets of two points could define the same axis), the distance
    between the points does matter for translation along an axis.

    \image 3d-axis.png

    \qml
    Axis { startX: 20; startY: 0; endX: 40; endY: 60; endZ: 20 }
    \endqml
*/

QML_DEFINE_TYPE(QFxAxis, Axis);

QFxAxis::QFxAxis(QObject *parent)
: QObject(parent), _startX(0), _startY(0), _endX(0), _endY(0), _endZ(0)
{
}

QFxAxis::~QFxAxis()
{
}

/*!
    \qmlproperty real Axis::startX
    \qmlproperty real Axis::startY

    The start point of the axis. The z-position of the start point is assumed to be 0, and cannot
    be changed.
*/
qreal QFxAxis::startX() const
{
    return _startX;
}

void QFxAxis::setStartX(qreal x)
{
    _startX = x;
    emit updated();
}

qreal QFxAxis::startY() const
{
    return _startY;
}

void QFxAxis::setStartY(qreal y)
{
    _startY = y;
    emit updated();
}

/*!
    \qmlproperty real Axis::endX
    \qmlproperty real Axis::endY
    \qmlproperty real Axis::endZ

    The end point of the axis.
*/
qreal QFxAxis::endX() const
{
    return _endX;
}

void QFxAxis::setEndX(qreal x)
{
    _endX = x;
    emit updated();
}

qreal QFxAxis::endY() const
{
    return _endY;
}

void QFxAxis::setEndY(qreal y)
{
    _endY = y;
    emit updated();
}

qreal QFxAxis::endZ() const
{
    return _endZ;
}

void QFxAxis::setEndZ(qreal z)
{
    _endZ = z;
    emit updated();
}

QFxRotation::QFxRotation(QObject *parent)
: QFxTransform(parent), _originX(0), _originY(0), _angle(0), _dirty(true)
{
}

QFxRotation::~QFxRotation()
{
}

qreal QFxRotation::originX() const
{
    return _originX;
}

void QFxRotation::setOriginX(qreal ox)
{
    _originX = ox;
    update();
}

qreal QFxRotation::originY() const
{
    return _originY;
}

void QFxRotation::setOriginY(qreal oy)
{
    _originY = oy;
    update();
}

qreal QFxRotation::angle() const
{
    return _angle;
}

void QFxRotation::setAngle(qreal angle)
{
    _angle = angle;
    update();
}

bool QFxRotation::isIdentity() const
{
    return (_angle == 0.);
}

#if defined(QFX_RENDER_QPAINTER)
QTransform QFxRotation::transform() const
{
    if (_dirty) {
        _transform = QTransform();
        _dirty = false;
        _transform.translate(_originX, _originY);
        _transform.rotate(_angle);
        _transform.translate(-_originX, -_originY);
    }
    return _transform;
}
#elif defined(QFX_RENDER_OPENGL)
QMatrix4x4 QFxRotation::transform() const
{
    if (_dirty) {
        _transform = QMatrix4x4();
        _dirty = false;
        _transform.rotate(_angle, _originX, _originY);
    }
    return _transform;
}
#endif

void QFxRotation::update()
{
    _dirty = true;
    QFxTransform::update();
}

QML_DEFINE_TYPE(QFxRotation, Rotation);

/*!
    \qmlclass Rotation3D
    \brief A Rotation3D object provides a way to rotate an Item around an axis.

    Here is an example of various rotations applied to an \l Image.
    \snippet doc/src/snippets/declarative/rotation.qml 0

    \image axisrotation.png
*/

QML_DEFINE_TYPE(QFxRotation3D,Rotation3D);

QFxRotation3D::QFxRotation3D(QObject *parent)
: QFxTransform(parent), _angle(0), _dirty(true)
{
    connect(&_axis, SIGNAL(updated()), this, SLOT(update()));
}

QFxRotation3D::~QFxRotation3D()
{
}

/*!
    \qmlproperty real Rotation3D::axis.startX
    \qmlproperty real Rotation3D::axis.startY
    \qmlproperty real Rotation3D::axis.endX
    \qmlproperty real Rotation3D::axis.endY
    \qmlproperty real Rotation3D::axis.endZ

    A rotation axis is specified by 2 points in 3D space: a start point and
    an end point. The z-position of the start point is assumed to be 0, and cannot
    be changed.

    \image 3d-rotation-axis.png

    \sa Axis
*/
QFxAxis *QFxRotation3D::axis()
{
    return &_axis;
}

/*!
    \qmlproperty real Rotation3D::angle

    The angle, in degrees, to rotate around the specified axis.
*/
qreal QFxRotation3D::angle() const
{
    return _angle;
}

void QFxRotation3D::setAngle(qreal angle)
{
    _angle = angle;
    update();
}

bool QFxRotation3D::isIdentity() const
{
    return (_angle == 0.) || (_axis.endZ() == 0. && _axis.endY() == _axis.startY() && _axis.endX() == _axis.startX());
}

#if defined(QFX_RENDER_QPAINTER)
const qreal inv_dist_to_plane = 1. / 1024.;
QTransform QFxRotation3D::transform() const
{
    if (_dirty) {
        _transform = QTransform();

        if (!isIdentity()) {
            if (angle() != 0.) {
                QTransform rotTrans;
                rotTrans.translate(-_axis.startX(), -_axis.startY());
                QTransform rotTrans2;
                rotTrans2.translate(_axis.startX(), _axis.startY());

                qreal rad = angle() * 2. * M_PI / 360.;
                qreal c = ::cos(rad);
                qreal s = ::sin(rad);

                qreal x = _axis.endX() - _axis.startX();
                qreal y = _axis.endY() - _axis.startY();
                qreal z = _axis.endZ();

                qreal len = x * x + y * y + z * z;
                if (len != 1.) {
                    len = ::sqrt(len);
                    x /= len;
                    y /= len;
                    z /= len;
                }

                QTransform rot(x*x*(1-c)+c, x*y*(1-c)-z*s, x*z*(1-c)+y*s*inv_dist_to_plane,
                               y*x*(1-c)+z*s, y*y*(1-c)+c, y*z*(1-c)-x*s*inv_dist_to_plane,
                               0, 0, 1);

                _transform *= rotTrans;
                _transform *= rot;
                _transform *= rotTrans2;
            }
        }

        _dirty = false;
    }

    return _transform;
}
#elif defined(QFX_RENDER_OPENGL)
QMatrix4x4 QFxRotation3D::transform() const
{
    if (_dirty) {
        _dirty = false;
        _transform = QMatrix4x4();

        if (!isIdentity()) {
            if (angle() != 0.) {
                qreal x = _axis.endX() - _axis.startX();
                qreal y = _axis.endY() - _axis.startY();
                qreal z = _axis.endZ();

                _transform.translate(_axis.startX(), _axis.startY(), 0);
                _transform.rotate(angle(), x, y, z);
                _transform.translate(-_axis.startX(), -_axis.startY(), 0);
            }
        }
    }

    return _transform;
}
#endif

void QFxRotation3D::update()
{
    _dirty = true;
    QFxTransform::update();
}

/*!
  \internal
  \qmlclass Translation3D
  \brief A Translation3D object provides a way to move an Item along an axis.

  The following example translates the image to 10, 3.
  \qml
Image {
    source: "logo.png"
    transform: [
        Translation3D {
            axis.startX: 0
            axis.startY: 0
            axis.endX: 1
            axis.endY: .3
            distance: 10
        }
    ]
}
  \endqml
*/

QML_DEFINE_TYPE(QFxTranslation3D,Translation3D);

QFxTranslation3D::QFxTranslation3D(QObject *parent)
: QFxTransform(parent), _distance(0), _dirty(true)
{
    connect(&_axis, SIGNAL(updated()), this, SLOT(update()));
}

QFxTranslation3D::~QFxTranslation3D()
{
}

/*!
  \qmlproperty real Translation3D::axis.startX
  \qmlproperty real Translation3D::axis.startY
  \qmlproperty real Translation3D::axis.endX
  \qmlproperty real Translation3D::axis.endY
  \qmlproperty real Translation3D::axis.endZ

  A translation axis is specified by 2 points in 3D space: a start
  point and an end point. The z-position of the start point is assumed
  to be 0, and cannot be changed. Changing the z-position of the end
  point is only valid when running under OpenGL.

  \sa Axis
*/
QFxAxis *QFxTranslation3D::axis()
{
    return &_axis;
}

/*!
  \qmlproperty real Translation3D::distance

  The distance to translate along the specified axis. distance is a
  multiplier; in the example below, a distance of 1 would translate to
  100, 50, while a distance of 0.5 would translate to 50, 25.

  \qml
  Translation3D { axis.startX: 0; axis.startY: 0; axis.endX: 100; axis.endY: 50 }
  \endqml
*/
qreal QFxTranslation3D::distance() const
{
    return _distance;
}

void QFxTranslation3D::setDistance(qreal distance)
{
    _distance = distance;
    update();
}

bool QFxTranslation3D::isIdentity() const
{
    return (_distance == 0.) || (_axis.endZ() == 0. && _axis.endY() == _axis.startY() && _axis.endX() == _axis.startX());
}

#if defined(QFX_RENDER_QPAINTER)
QTransform QFxTranslation3D::transform() const
{
    if (_dirty) {
        _transform = QTransform();

        if (!isIdentity()) {
            if (distance() != 0.) {
                QTransform trans;
                trans.translate((_axis.endX() - _axis.startX()) * distance(),
                        (_axis.endY() - _axis.startY()) * distance());
                _transform *= trans;
            }
        }

        _dirty = false;
    }

    return _transform;
}
#elif defined(QFX_RENDER_OPENGL)
QMatrix4x4 QFxTranslation3D::transform() const
{
    if (_dirty) {
        _dirty = false;
        _transform = QMatrix4x4();

        if (!isIdentity()) {
            if (distance() != 0.)
                _transform.translate((_axis.endX() - _axis.startX()) * distance(),
                                     (_axis.endY() - _axis.startY()) * distance(),
                                     (_axis.endZ()) * distance());

        }
    }

    return _transform;
}
#endif

void QFxTranslation3D::update()
{
    _dirty = true;

#if !defined(QFX_RENDER_OPENGL)
    if (_axis.endZ() != 0. && distance() != 0.) {
        qmlInfo(this) << "QTransform cannot translate along Z-axis.";
    }
#endif

    QFxTransform::update();
}

/*!
  \internal
  \qmlclass Perspective
  \brief A Perspective object specifies a perspective transformation.

  A Perspective transform only affects an item when running under
  OpenGL. When running under software rasterization it has no effect.
*/

QML_DEFINE_TYPE(QFxPerspective,Perspective);

QFxPerspective::QFxPerspective(QObject *parent)
    : QFxTransform(parent)
{
}

QFxPerspective::~QFxPerspective()
{
}

#if defined(QFX_RENDER_OPENGL)
bool QFxPerspective::isIdentity() const
{
    return false;
}

QMatrix4x4 QFxPerspective::transform() const
{
    QMatrix4x4 rv;
    rv.translate(_x, _y);
    rv.perspective(_angle, _aspect, 1, 1024 * 1024);
    rv.translate(-_x, -_y, -1);
    rv.scale(1, 1, 1. / _scale);

    return rv;
}
#endif

/*!
    \qmlproperty real Perspective::angle
*/

/*!
    \qmlproperty real Perspective::aspect
*/

/*!
    \qmlproperty real Perspective::x
*/

/*!
    \qmlproperty real Perspective::y
*/

/*!
    \qmlproperty real Perspective::scale
*/

/*!
    \qmlclass Squish
    \brief A Squish object allows you to distort an item's appearance by 'squishing' it.

    Conceptually, a Squish works by allowing you to move the four corners of an item,
    and distorting the item to fit into the newly created polygon.

    \image squish-transform.png

    Here is an example of various \l Image squishes.
    \qml
    Rect {
        id: Screen
        width: 360; height: 80
        color: "white"

        HorizontalLayout {
            margin: 10
            spacing: 10
            Image { source: "qt.png" }
            Image {
                source: "qt.png"
                transform: Squish {
                    x:0; y:0; width:60; height:60
                    topLeftX:0; topLeftY:0
                    topRightX:50; topRightY:10
                    bottomLeftX:0; bottomLeftY:60
                    bottomRightX: 60; bottomRightY:60
                }
            }
            Image {
                source: "qt.png"
                transform: Squish {
                    x:0; y:0; width:60; height:60
                    topLeftX:0; topLeftY:0
                    topRightX:50; topRightY:0
                    bottomLeftX:10; bottomLeftY:50
                    bottomRightX: 60; bottomRightY:60
                }
            }
            Image {
                source: "qt.png"
                transform: Squish {
                    x:0; y:0; width:60; height:60
                    topLeftX:0; topLeftY:10
                    topRightX:60; topRightY:10
                    bottomLeftX:0; bottomLeftY:50
                    bottomRightX: 60; bottomRightY:50
                }
            }
            Image {
                source: "qt.png"
                transform: Squish {
                    x:0; y:0; width:60; height:60
                    topLeftX:10; topLeftY:0
                    topRightX:50; topRightY:0
                    bottomLeftX:10; bottomLeftY:60
                    bottomRightX: 50; bottomRightY:60
                }
            }
        }
    }
    \endqml

    \image squish.png
*/
QML_DEFINE_TYPE(QFxSquish,Squish);

QFxSquish::QFxSquish(QObject *parent)
    : QFxTransform(parent)
{
}

QFxSquish::~QFxSquish()
{
}

/*!
    \qmlproperty real Squish::x
    \qmlproperty real Squish::y
    \qmlproperty real Squish::width
    \qmlproperty real Squish::height

    This is usually set to the original geometry of the item being squished.
*/
qreal QFxSquish::x() const
{
    return p.x();
}

void QFxSquish::setX(qreal v)
{
    p.setX(v);
    update();
}

qreal QFxSquish::y() const
{
    return p.y();
}

void QFxSquish::setY(qreal v)
{
    p.setY(v);
    update();
}

qreal QFxSquish::width() const
{
    return s.width();
}

void QFxSquish::setWidth(qreal v)
{
    s.setWidth(v);
    update();
}

qreal QFxSquish::height() const
{
    return s.height();
}

void QFxSquish::setHeight(qreal v)
{
    s.setHeight(v);
    update();
}

/*!
    \qmlproperty real Squish::topLeftX
    \qmlproperty real Squish::topLeftY

    The top left point for the squish.
*/
qreal QFxSquish::topLeft_x() const
{
    return p1.x();
}

void QFxSquish::settopLeft_x(qreal v)
{
    p1.setX(v);
    update();
}

qreal QFxSquish::topLeft_y() const
{
    return p1.y();
}

void QFxSquish::settopLeft_y(qreal v)
{
    p1.setY(v);
    update();
}

/*!
    \qmlproperty real Squish::topRightX
    \qmlproperty real Squish::topRightY

    The top right point for the squish.
*/
qreal QFxSquish::topRight_x() const
{
    return p2.x();
}

void QFxSquish::settopRight_x(qreal v)
{
    p2.setX(v);
    update();
}

qreal QFxSquish::topRight_y() const
{
    return p2.y();
}

void QFxSquish::settopRight_y(qreal v)
{
    p2.setY(v);
    update();
}

/*!
    \qmlproperty real Squish::bottomLeftX
    \qmlproperty real Squish::bottomLeftY

    The bottom left point for the squish.
*/
qreal QFxSquish::bottomLeft_x() const
{
    return p3.x();
}

void QFxSquish::setbottomLeft_x(qreal v)
{
    p3.setX(v);
    update();
}

qreal QFxSquish::bottomLeft_y() const
{
    return p3.y();
}

void QFxSquish::setbottomLeft_y(qreal v)
{
    p3.setY(v);
    update();
}

/*!
    \qmlproperty real Squish::bottomRightX
    \qmlproperty real Squish::bottomRightY

    The bottom right point for the squish.
*/
qreal QFxSquish::bottomRight_x() const
{
    return p4.x();
}

void QFxSquish::setbottomRight_x(qreal v)
{
    p4.setX(v);
    update();
}

qreal QFxSquish::bottomRight_y() const
{
    return p4.y();
}

void QFxSquish::setbottomRight_y(qreal v)
{
    p4.setY(v);
    update();
}

bool QFxSquish::isIdentity() const
{
    return false;
}

#if defined(QFX_RENDER_QPAINTER)
QTransform QFxSquish::transform() const
{
    QPolygonF poly;
    poly << p << QPointF(p.x() + s.width(), p.y()) << QPointF(p.x() + s.width(), p.y() + s.height()) << QPointF(p.x(), p.y() + s.height());
    QPolygonF poly2;
    poly2 << p1 << p2 << p4 << p3;

    QTransform t;
    QTransform::quadToQuad(poly, poly2, t);
    return t;
}
#elif defined(QFX_RENDER_OPENGL)
QMatrix4x4 QFxSquish::transform() const
{
    QPolygonF poly;
    poly << p << QPointF(p.x() + s.width(), p.y()) << QPointF(p.x() + s.width(), p.y() + s.height()) << QPointF(p.x(), p.y() + s.height());
    QPolygonF poly2;
    poly2 << p1 << p2 << p4 << p3;

    QTransform t;
    QMatrix4x4 rv;
    if (QTransform::quadToQuad(poly, poly2, t))
        rv = QMatrix4x4(t);

    return rv;
}
#endif

QT_END_NAMESPACE