summaryrefslogtreecommitdiffstats
path: root/src/gui/graphicsview/qgraphicstransform.cpp
blob: 95f137d6db53a8d6a18fe68d88be49c2ec1ebb81 (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
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (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 http://qt.nokia.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \class QGraphicsTransform
    \brief The QGraphicsTransform class is an abstract base class for building
    advanced transformations on QGraphicsItems.
    \since 4.6

    As an alternative to QGraphicsItem::transform, QGraphicsTransform lets you
    create and control advanced transformations that can be configured
    independently using specialized properties.

    QGraphicsItem allows you to assign any number of QGraphicsTransform
    instances to one QGraphicsItem. Each QGraphicsTransform is applied in
    order, one at a time, to the QGraphicsItem it's assigned to.

    QGraphicsTransform is particularily useful for animations. Whereas
    QGraphicsItem::setTransform() lets you assign any transform directly to an
    item, there is no direct way to interpolate between two different
    transformations (e.g., when transitioning between two states, each for
    which the item has a different arbitrary transform assigned). Using
    QGraphicsTransform you can interpolate the property values of each
    independent transformation. The resulting operation is then combined into a
    single transform which is applied to QGraphicsItem.

    If you want to create your own configurable transformation, you can create
    a subclass of QGraphicsTransform (or any or the existing subclasses), and
    reimplement the pure virtual applyTo() function, which takes a pointer to a
    QTransform. Each operation you would like to apply should be exposed as
    properties (e.g., customTransform->setVerticalShear(2.5)). Inside you
    reimplementation of applyTo(), you can modify the provided transform
    respectively.

    QGraphicsTransform can be used together with QGraphicsItem::setTransform(),
    QGraphicsItem::setRotation(), and QGraphicsItem::setScale().

    \sa QGraphicsItem::transform(), QGraphicsScale, QGraphicsRotation
*/

#include "qgraphicstransform.h"
#include "qgraphicsitem_p.h"
#include "qgraphicstransform_p.h"
#include <QDebug>
#include <QtCore/qmath.h>

QT_BEGIN_NAMESPACE

void QGraphicsTransformPrivate::setItem(QGraphicsItem *i)
{
    if (item == i)
        return;

    if (item) {
        Q_Q(QGraphicsTransform);
        QGraphicsItemPrivate *d_ptr = item->d_ptr;

        item->prepareGeometryChange();
        Q_ASSERT(d_ptr->transformData);
        d_ptr->transformData->graphicsTransforms.removeAll(q);
        d_ptr->dirtySceneTransform = 1;
        item = 0;
    }

    item = i;
}

void QGraphicsTransformPrivate::updateItem(QGraphicsItem *item)
{
    item->prepareGeometryChange();
    item->d_ptr->dirtySceneTransform = 1;
}

/*!
    Constructs a new QGraphicsTransform with the given \a parent.
*/
QGraphicsTransform::QGraphicsTransform(QObject *parent)
    : QObject(*new QGraphicsTransformPrivate, parent)
{
}

/*!
    Destroys the graphics transform.
*/
QGraphicsTransform::~QGraphicsTransform()
{
    Q_D(QGraphicsTransform);
    d->setItem(0);
}

/*!
    \internal
*/
QGraphicsTransform::QGraphicsTransform(QGraphicsTransformPrivate &p, QObject *parent)
    : QObject(p, parent)
{
}

/*!
    Applies this transformation to an identity transform, and returns the
    resulting transform.

    This is equivalent to passing an identity transform to applyTo().

    \sa applyTo()
*/
QTransform QGraphicsTransform::transform() const
{
    QTransform t;
    applyTo(&t);
    return t;
}

/*!
    \fn void QGraphicsTransform::applyTo(QTransform *transform) const

    This pure virtual method has to be reimplemented in derived classes.

    It applies this transformation to \a transform.

    \sa QGraphicsItem::transform()
*/

/*!
    Notifies that this transform operation has changed its parameters in such a
    way that applyTo() will return a different result than before.

    When implementing you own custom graphics transform, you must call this
    function every time you change a parameter, to let QGraphicsItem know that
    its transformation needs to be updated.

    \sa applyTo()
*/
void QGraphicsTransform::update()
{
    Q_D(QGraphicsTransform);
    if (d->item)
        d->updateItem(d->item);
}

/*!
  \class QGraphicsScale
  \brief The QGraphicsScale class provides a scale transformation.
  \since 4.6

  QGraphicsScene provides certain parameters to help control how the scale
  should be applied.

  The origin is the point that the item is scaled from (i.e., it stays fixed
  relative to the parent as the rest of the item grows). By default the
  origin is QPointF(0, 0).

  The two parameters xScale and yScale describe the scale factors to apply in
  horizontal and vertical direction. They can take on any value, including 0
  (to collapse the item to a point) or negativate value. A negative xScale
  value will mirror the item horizontally. A negative yScale value will flip
  the item vertically.

  \sa QGraphicsTransform, QGraphicsItem::setScale(), QTransform::scale()
*/

class QGraphicsScalePrivate : public QGraphicsTransformPrivate
{
public:
    QGraphicsScalePrivate()
        : xScale(1), yScale(1) {}
    QPointF origin;
    qreal xScale;
    qreal yScale;
};

/*!
    Constructs an empty QGraphicsScale object with the given \a parent.
*/
QGraphicsScale::QGraphicsScale(QObject *parent)
    : QGraphicsTransform(*new QGraphicsScalePrivate, parent)
{
}

/*!
    Destroys the graphics scale.
*/
QGraphicsScale::~QGraphicsScale()
{
}

/*!
    \property QGraphicsScale::origin
    \brief The QGraphicsScene class provides the origin of the scale.

    All scaling will be done relative to this point (i.e., this point
    will stay fixed, relative to the parent, when the item is scaled).

    \sa xScale, yScale
*/
QPointF QGraphicsScale::origin() const
{
    Q_D(const QGraphicsScale);
    return d->origin;
}
void QGraphicsScale::setOrigin(const QPointF &point)
{
    Q_D(QGraphicsScale);
    d->origin = point;
    update();
    emit originChanged();
}

/*!
    \property QGraphicsScale::xScale
    \brief the horizontal scale factor.

    The scale factor can be any real number; the default value is 1.0. If you
    set the factor to 0.0, the item will be collapsed to a single point. If you
    provide a negative value, the item will be mirrored horizontally around its
    origin.

    \sa yScale, origin
*/
qreal QGraphicsScale::xScale() const
{
    Q_D(const QGraphicsScale);
    return d->xScale;
}
void QGraphicsScale::setXScale(qreal scale)
{
    Q_D(QGraphicsScale);
    if (d->xScale == scale)
        return;
    d->xScale = scale;
    update();
    emit scaleChanged();
}

/*!
    \property QGraphicsScale::yScale
    \brief the vertical scale factor.

    The scale factor can be any real number; the default value is 1.0. If you
    set the factor to 0.0, the item will be collapsed to a single point. If you
    provide a negative value, the item will be flipped vertically around its
    origin.

    \sa xScale, origin
*/
qreal QGraphicsScale::yScale() const
{
    Q_D(const QGraphicsScale);
    return d->yScale;
}
void QGraphicsScale::setYScale(qreal scale)
{
    Q_D(QGraphicsScale);
    if (d->yScale == scale)
        return;
    d->yScale = scale;
    update();
    emit scaleChanged();
}

/*!
    \reimp
*/
void QGraphicsScale::applyTo(QTransform *transform) const
{
    Q_D(const QGraphicsScale);
    transform->translate(d->origin.x(), d->origin.y());
    transform->scale(d->xScale, d->yScale);
    transform->translate(-d->origin.x(), -d->origin.y());
}

/*!
    \fn QGraphicsScale::originChanged()

    QGraphicsScale emits this signal when its origin changes.

    \sa QGraphicsScale::origin
*/

/*!
    \fn QGraphicsScale::scaleChanged()

    This signal is emitted whenever the xScale or yScale of the object
    changes.

    \sa QGraphicsScale::xScale, QGraphicsScale::yScale
*/

/*!
    \class QGraphicsRotation
    \brief The QGraphicsRotation class provides a rotation transformation around
    a given axis.
    \since 4.6

    You can provide the desired axis by assigning a QVector3D to the axis property
    or by passing a member if Qt::Axis to the setAxis convenience function.
    By default the axis is (0, 0, 1) i.e., rotation around the Z axis.

    The angle property, which is provided by QGraphicsRotation, now
    describes the number of degrees to rotate around this axis.

    QGraphicsRotation provides certain parameters to help control how the
    rotation should be applied.

    The origin is the point that the item is rotated around (i.e., it stays
    fixed relative to the parent as the rest of the item is rotated). By
    default the origin is QPointF(0, 0).

    The angle property provides the number of degrees to rotate the item
    clockwise around the origin. This value also be negative, indicating a
    counter-clockwise rotation. For animation purposes it may also be useful to
    provide rotation angles exceeding (-360, 360) degrees, for instance to
    animate how an item rotates several times.

    Note: the final rotation is the combined effect of a rotation in
    3D space followed by a projection back to 2D.  If several rotations
    are performed in succession, they will not behave as expected unless
    they were all around the Z axis.

    \sa QGraphicsTransform, QGraphicsItem::setRotation(), QTransform::rotate()
*/

#define VECTOR_FOR_AXIS_X QVector3D(1, 0, 0)
#define VECTOR_FOR_AXIS_Y QVector3D(0, 1, 0)
#define VECTOR_FOR_AXIS_Z QVector3D(0, 0, 1)


class QGraphicsRotationPrivate : public QGraphicsTransformPrivate
{
public:
    QGraphicsRotationPrivate()
        : angle(0), axis(VECTOR_FOR_AXIS_Z), simpleAxis(Qt::ZAxis) {}
    QPointF origin;
    qreal angle;
    QVector3D axis;
    int simpleAxis;
};

/*!
    Constructs a new QGraphicsRotation with the given \a parent.
*/
QGraphicsRotation::QGraphicsRotation(QObject *parent)
    : QGraphicsTransform(*new QGraphicsRotationPrivate, parent)
{
}

/*!
    Destroys the graphics rotation.
*/
QGraphicsRotation::~QGraphicsRotation()
{
}

/*!
    \property QGraphicsRotation::origin
    \brief the origin of the rotation.

    All rotations will be done relative to this point (i.e., this point
    will stay fixed, relative to the parent, when the item is rotated).

    \sa angle
*/
QPointF QGraphicsRotation::origin() const
{
    Q_D(const QGraphicsRotation);
    return d->origin;
}
void QGraphicsRotation::setOrigin(const QPointF &point)
{
    Q_D(QGraphicsRotation);
    d->origin = point;
    update();
    emit originChanged();
}

/*!
    \property QGraphicsRotation::angle
    \brief the angle for clockwise rotation, in degrees.

    The angle can be any real number; the default value is 0.0. A value of 180
    will rotate 180 degrees, clockwise. If you provide a negative number, the
    item will be rotated counter-clockwise. Normally the rotation angle will be
    in the range (-360, 360), but you can also provide numbers outside of this
    range (e.g., a angle of 370 degrees gives the same result as 10 degrees).

    \sa origin
*/
qreal QGraphicsRotation::angle() const
{
    Q_D(const QGraphicsRotation);
    return d->angle;
}
void QGraphicsRotation::setAngle(qreal angle)
{
    Q_D(QGraphicsRotation);
    if (d->angle == angle)
        return;
    d->angle = angle;
    update();
    emit angleChanged();
}

/*!
    \fn QGraphicsRotation::originChanged()

    This signal is emitted whenever the origin has changed.

    \sa QGraphicsRotation::origin
*/

/*!
  \fn void QGraphicsRotation::angleChanged()

  This signal is emitted whenever the angle has changed.

  \sa QGraphicsRotation::angle
*/

/*!
    \property QGraphicsRotation::axis
    \brief a rotation axis, specified by a vector in 3D space.

    This can be any axis in 3D space. By default the axis is (0, 0, 1),
    which is aligned with the Z axis. If you provide another axis,
    QGraphicsRotation will provide a transformation that rotates
    around this axis. For example, if you would like to rotate an item
    around its X axis, you could pass (1, 0, 0) as the axis.

    \sa QTransform, QGraphicsRotation::angle
*/
QVector3D QGraphicsRotation::axis() const
{
    Q_D(const QGraphicsRotation);
    return d->axis;
}
void QGraphicsRotation::setAxis(const QVector3D &axis)
{
    Q_D(QGraphicsRotation);
     if (d->axis == axis)
         return;
     d->axis = axis;
    if (axis == VECTOR_FOR_AXIS_X) {
        d->simpleAxis = Qt::XAxis;
    } else if (axis == VECTOR_FOR_AXIS_Y) {
        d->simpleAxis = Qt::YAxis;
    } else if (axis == VECTOR_FOR_AXIS_Z) {
        d->simpleAxis = Qt::ZAxis;
    } else {
        d->simpleAxis = -1; // no predefined axis
    }
    update();
    emit axisChanged();
}

/*!
    \fn void QGraphicsRotation::setAxis(Qt::Axis axis)

    Convenience function to set the axis to \a axis.
*/

void QGraphicsRotation::setAxis(Qt::Axis axis)
{
    switch (axis)
    {
    case Qt::XAxis:
        setAxis(VECTOR_FOR_AXIS_X);
        break;
    case Qt::YAxis:
        setAxis(VECTOR_FOR_AXIS_Y);
        break;
    case Qt::ZAxis:
        setAxis(VECTOR_FOR_AXIS_Z);
        break;
    }
}


const qreal deg2rad = qreal(0.017453292519943295769);        // pi/180
static const qreal inv_dist_to_plane = 1. / 1024.;

/*!
    \reimp
*/
void QGraphicsRotation::applyTo(QTransform *t) const
{
    Q_D(const QGraphicsRotation);

    qreal a = d->angle;

    if (a == 0.)
        return;

    if (d->simpleAxis != -1) {
        //that's an optimization for simple axis
        t->translate(d->origin.x(), d->origin.y());
        t->rotate(a, Qt::Axis(d->simpleAxis));
        t->translate(-d->origin.x(), -d->origin.y());
        return;
    }

    qreal x = d->axis.x();
    qreal y = d->axis.y();
    qreal z = d->axis.z();

    if (x == 0. && y == 0 && z == 0)
        return;

    qreal c, s;
    if (a == 90. || a == -270.) {
        s = 1.;
        c = 0.;
    } else if (a == 270. || a == -90.) {
        s = -1.;
        c = 0.;
    } else if (a == 180.) {
        s = 0.;
        c = -1.;
    } else {
        qreal b = deg2rad*a;
        s = qSin(b);
        c = qCos(b);
    }

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

    t->translate(d->origin.x(), d->origin.y());
    *t = QTransform(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) * *t;
    t->translate(-d->origin.x(), -d->origin.y());
}

/*!
    \fn void QGraphicsRotation::axisChanged()

    This signal is emitted whenever the axis of the object changes.
*/

#include "moc_qgraphicstransform.cpp"

QT_END_NAMESPACE