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
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** 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 Technology Preview License Agreement accompanying
** this package.
**
** 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.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QDebug>
#include <QCoreApplication>
#include "qml.h"
#include "qmlstate.h"
#include "qmlstategroup.h"
#include "qmlstateoperations.h"
#include "private/qfxperf_p.h"
#include "qfxpositioners.h"
#include "qfxpositioners_p.h"
QT_BEGIN_NAMESPACE
/*!
\internal
\class QFxBasePositioner
\ingroup group_layouts
\brief The QFxBasePositioner class provides a base for QFx layouts.
To create a QFx Positioner, simply subclass QFxBasePositioner and implement
doLayout(), which is automatically called when the layout might need
updating.
It is strongly recommended that in your implementation of doLayout()
that you use the move, remove and add transitions when those conditions
arise. You can use the applyAdd, applyMove and applyRemove functions
to do this easily.
Note also that the subclass is responsible for adding the
spacing in between items.
*/
QFxBasePositioner::QFxBasePositioner(AutoUpdateType at, QFxItem *parent)
: QFxItem(*(new QFxBasePositionerPrivate), parent)
{
Q_D(QFxBasePositioner);
d->init(at);
}
QFxBasePositioner::QFxBasePositioner(QFxBasePositionerPrivate &dd, AutoUpdateType at, QFxItem *parent)
: QFxItem(dd, parent)
{
Q_D(QFxBasePositioner);
d->init(at);
}
int QFxBasePositioner::spacing() const
{
Q_D(const QFxBasePositioner);
return d->_spacing;
}
void QFxBasePositioner::setSpacing(int s)
{
Q_D(QFxBasePositioner);
if (s==d->_spacing)
return;
d->_spacing = s;
prePositioning();
emit spacingChanged();
}
QmlTransition *QFxBasePositioner::move() const
{
Q_D(const QFxBasePositioner);
return d->moveTransition;
}
void QFxBasePositioner::setMove(QmlTransition *mt)
{
Q_D(QFxBasePositioner);
d->moveTransition = mt;
}
QmlTransition *QFxBasePositioner::add() const
{
Q_D(const QFxBasePositioner);
return d->addTransition;
}
void QFxBasePositioner::setAdd(QmlTransition *add)
{
Q_D(QFxBasePositioner);
d->addTransition = add;
}
QmlTransition *QFxBasePositioner::remove() const
{
Q_D(const QFxBasePositioner);
return d->removeTransition;
}
void QFxBasePositioner::setRemove(QmlTransition *remove)
{
Q_D(QFxBasePositioner);
d->removeTransition = remove;
}
void QFxBasePositioner::componentComplete()
{
QFxItem::componentComplete();
#ifdef Q_ENABLE_PERFORMANCE_LOG
QFxPerfTimer<QFxPerf::BasepositionerComponentComplete> cc;
#endif
prePositioning();
}
QVariant QFxBasePositioner::itemChange(GraphicsItemChange change,
const QVariant &value)
{
if (change == ItemChildAddedChange ||
change == ItemChildRemovedChange) {
prePositioning();
}
return QFxItem::itemChange(change, value);
}
bool QFxBasePositioner::event(QEvent *e)
{
Q_D(QFxBasePositioner);
if (e->type() == QEvent::User) {
d->_ep = false;
d->_stableItems += d->_newItems;
d->_leavingItems.clear();
d->_newItems.clear();
return true;
}
return QFxItem::event(e);
}
/*!
Items that have just been added to the positioner. This includes invisible items
that have turned visible.
*/
QSet<QFxItem *>* QFxBasePositioner::newItems()
{
Q_D(QFxBasePositioner);
return &d->_newItems;
}
/*!
Items that are visible in the positioner, not including ones that have just been added.
*/
QSet<QFxItem *>* QFxBasePositioner::items()
{
Q_D(QFxBasePositioner);
return &d->_stableItems;
}
/*!
Items that have just left the positioner. This includes visible items
that have turned invisible.
*/
QSet<QFxItem *>* QFxBasePositioner::leavingItems()
{
Q_D(QFxBasePositioner);
return &d->_leavingItems;
}
void QFxBasePositioner::prePositioning()
{
Q_D(QFxBasePositioner);
if (!isComponentComplete() || d->_movingItem)
return;
if (!d->_ep) {
d->_ep = true;
QCoreApplication::postEvent(this, new QEvent(QEvent::User));
}
QSet<QFxItem *> allItems;
QList<QGraphicsItem *> children = childItems();
for (int ii = 0; ii < children.count(); ++ii) {
QFxItem *child = qobject_cast<QFxItem *>(children.at(ii));
if (!child)
continue;
if (!d->_items.contains(child)){
QObject::connect(child, SIGNAL(visibleChanged()),
this, SLOT(prePositioning()));
QObject::connect(child, SIGNAL(opacityChanged()),
this, SLOT(prePositioning()));
QObject::connect(child, SIGNAL(heightChanged()),
this, SLOT(prePositioning()));
QObject::connect(child, SIGNAL(widthChanged()),
this, SLOT(prePositioning()));
d->_items += child;
}
if (child->opacity() == 0.0){
if (d->_stableItems.contains(child)){
d->_leavingItems += child;
d->_stableItems -= child;
}
}else if (!d->_stableItems.contains(child)){
d->_newItems+=child;
}
allItems += child;
}
QSet<QFxItem *> deletedItems = d->_items - allItems;
foreach(QFxItem *child, d->_items){
if (!allItems.contains(child)){
if (!deletedItems.contains(child)) {
QObject::disconnect(child, SIGNAL(opacityChanged()),
this, SLOT(prePositioning()));
QObject::disconnect(child, SIGNAL(heightChanged()),
this, SLOT(prePositioning()));
QObject::disconnect(child, SIGNAL(widthChanged()),
this, SLOT(prePositioning()));
}
d->_items -= child;
}
}
d->_animated.clear();
doPositioning();
finishApplyTransitions();
//Set implicit size to the size of its children
//###To keep this valid, do we need to update on pos change as well?
qreal h = 0.0f;
qreal w = 0.0f;
foreach(QFxItem *child, d->_items){
if(!child->isVisible() || child->opacity() <= 0)
continue;
h = qMax(h, child->y() + child->height());
w = qMax(w, child->x() + child->width());
}
setImplicitHeight(h);
setImplicitWidth(w);
}
void QFxBasePositioner::applyTransition(const QList<QPair<QString, QVariant> >& changes, QFxItem* target, QmlStateOperation::ActionList &actions)
{
Q_D(QFxBasePositioner);
if (!target)
return;
for (int ii=0; ii<changes.size(); ++ii){
QVariant val = changes[ii].second;
actions << Action(target, changes[ii].first, val);
}
d->_animated << target;
}
void QFxBasePositioner::finishApplyTransitions()
{
Q_D(QFxBasePositioner);
// Note that if a transition is not set the transition manager will
// apply the changes directly, in the case someone uses applyAdd/Move/Remove
// without testing add()/move()/remove().
d->addTransitionManager.transition(d->addActions, d->addTransition);
d->moveTransitionManager.transition(d->moveActions, d->moveTransition);
d->removeTransitionManager.transition(d->removeActions, d->removeTransition);
d->addActions.clear();
d->moveActions.clear();
d->removeActions.clear();
}
void QFxBasePositioner::setMovingItem(QFxItem *i)
{
Q_D(QFxBasePositioner);
d->_movingItem = i;
}
/*!
Applies the positioner's add transition to the \a target item.\a changes is a list of property,value
pairs which will be changed on the target using the add transition.
*/
void QFxBasePositioner::applyAdd(const QList<QPair<QString, QVariant> >& changes, QFxItem* target)
{
Q_D(QFxBasePositioner);
applyTransition(changes,target, d->addActions);
}
/*!
Applies the positioner's move transition to the \a target.\a changes is a list of property,value pairs
which will be changed on the target using the move transition.
*/
void QFxBasePositioner::applyMove(const QList<QPair<QString, QVariant> >& changes, QFxItem* target)
{
Q_D(QFxBasePositioner);
applyTransition(changes,target, d->moveActions);
}
/*!
Applies the positioner's remove transition to the \a target item.\a changes is a list of
property,value pairs which will be changed on the target using the remove transition.
*/
void QFxBasePositioner::applyRemove(const QList<QPair<QString, QVariant> >& changes, QFxItem* target)
{
Q_D(QFxBasePositioner);
applyTransition(changes,target, d->removeActions);
}
QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,Column,QFxColumn)
/*!
\qmlclass Column
\brief The Column item lines up its children vertically.
\inherits Item
The Column item positions its child items so that they are vertically
aligned and not overlapping. Spacing between items can be added.
The below example positions differently shaped rectangles using a Column.
\table
\row
\o \image verticalpositioner_example.png
\o
\qml
Column {
spacing: 2
Rectangle { color: "red"; width: 50; height: 50 }
Rectangle { color: "green"; width: 20; height: 50 }
Rectangle { color: "blue"; width: 50; height: 20 }
}
\endqml
\endtable
Column also provides for transitions to be set when items are added, moved,
or removed in the positioner. Adding and removing apply both to items which are deleted
or have their position in the document changed so as to no longer be children of the positioner,
as well as to items which have their opacity set to or from zero so as to appear or disappear.
\table
\row
\o \image verticalpositioner_transition.gif
\o
\qml
Column {
spacing: 2
remove: ...
add: ...
move: ...
...
}
\endqml
\endtable
Note that the positioner assumes that the x and y positions of its children
will not change. If you manually change the x or y properties in script, bind
the x or y properties, or use anchors on a child of a positioner, then the
positioner may exhibit strange behaviour.
*/
/*!
\qmlproperty Transition Column::remove
This property holds the transition to apply when removing an item from the positioner. The transition is only applied to the removed items.
Removed can mean that either the object has been deleted or reparented, and thus is now longer a child of the positioner, or that the object has had its opacity set to zero, and thus is no longer visible.
Note that if the item counts as removed because its opacity is zero it will not be visible during the transition unless you set the opacity in the transition, like in the below example.
\table
\row
\o \image positioner-remove.gif
\o
\qml
Column {
remove: Transition {
NumberAnimation {
properties: "opacity"
from: 1
to: 0
duration: 500
}
}
}
\endqml
\endtable
*/
/*!
\qmlproperty Transition Column::add
This property holds the transition to be applied when adding an item to the positioner. The transition will only be applied to the added item(s).
Added can mean that either the object has been created or reparented, and thus is now a child or the positioner, or that the object has had its opacity increased from zero, and thus is now visible.
\table
\row
\o \image positioner-add.gif
\o
\qml
Column {
add: Transition {
NumberAnimation {
properties: "opacity"
from: 0
to: 1
duration: 500
}
}
}
\endqml
\endtable
*/
/*!
\qmlproperty Transition Column::move
This property holds the transition to apply when moving an item within the positioner.
This can happen when other items are added or removed from the positioner, or when items resize themselves.
\table
\row
\o \image positioner-move.gif
\o
\qml
Column {
move: Transition {
NumberAnimation {
properties: "y"
ease: "easeOutBounce"
}
}
}
\endqml
\endtable
*/
/*!
\qmlproperty int Column::spacing
spacing is the amount in pixels left empty between each adjacent
item, and defaults to 0.
The below example places a Grid containing a red, a blue and a
green rectangle on a gray background. The area the grid positioner
occupies is colored white. The top positioner has the default of no spacing,
and the bottom positioner has its spacing set to 2.
\image spacing_a.png
\image spacing_b.png
*/
/*!
\internal
\class QFxColumn
\brief The QFxColumn class lines up items vertically.
\ingroup group_positioners
*/
QFxColumn::QFxColumn(QFxItem *parent)
: QFxBasePositioner(Vertical, parent)
{
}
void QFxColumn::doPositioning()
{
int voffset = 0;
foreach(QFxItem* item, *leavingItems()){
if (remove()){
QList<QPair<QString,QVariant> > changes;
applyRemove(changes, item);
}
}
QList<QGraphicsItem *> children = childItems();
for (int ii = 0; ii < children.count(); ++ii) {
QFxItem *child = qobject_cast<QFxItem *>(children.at(ii));
if (!child || child->opacity() == 0.0)
continue;
bool needMove = (child->y() != voffset || child->x());
QList<QPair<QString, QVariant> > changes;
changes << qMakePair(QString(QLatin1String("y")),QVariant(voffset));
changes << qMakePair(QString(QLatin1String("x")),QVariant(0));
if (needMove && items()->contains(child) && move()) {
applyMove(changes,child);
} else if (!items()->contains(child) && add()) {
applyAdd(changes,child);
} else if (needMove) {
setMovingItem(child);
child->setY(voffset);
setMovingItem(0);
}
voffset += child->height();
voffset += spacing();
}
}
QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,Row,QFxRow)
/*!
\qmlclass Row
\brief The Row item lines up its children horizontally.
\inherits Item
The Row item positions its child items so that they are
horizontally aligned and not overlapping. Spacing can be added between the
items, and a margin around all items can also be added. It also provides for
transitions to be set when items are added, moved, or removed in the
positioner. Adding and removing apply both to items which are deleted or have
their position in the document changed so as to no longer be children of the
positioner, as well as to items which have their opacity set to or from zero
so as to appear or disappear.
The below example lays out differently shaped rectangles using a Row.
\qml
Row {
spacing: 2
Rectangle { color: "red"; width: 50; height: 50 }
Rectangle { color: "green"; width: 20; height: 50 }
Rectangle { color: "blue"; width: 50; height: 20 }
}
\endqml
\image horizontalpositioner_example.png
Note that the positioner assumes that the x and y positions of its children
will not change. If you manually change the x or y properties in script, bind
the x or y properties, or use anchors on a child of a positioner, then the
positioner may exhibit strange behaviour.
*/
/*!
\qmlproperty Transition Row::remove
This property holds the transition to apply when removing an item from the positioner.
The transition will only be applied to the removed item(s).
Removed can mean that either the object has been deleted or reparented, and thus is now longer a child of the positioner, or that the object has had its opacity set to zero, and thus is no longer visible.
Note that if the item counts as removed because its opacity is zero it will not be visible during the transition unless you set the opacity in the transition, like in the below example.
\qml
Row {
remove: Transition {
NumberAnimation {
properties: "opacity"
from: 1
to: 0
duration: 500
}
}
}
\endqml
*/
/*!
\qmlproperty Transition Row::add
This property holds the transition to apply when adding an item to the positioner.
The transition will only be applied to the added item(s).
Added can mean that either the object has been created or reparented, and thus is now a child or the positioner, or that the object has had its opacity increased from zero, and thus is now visible.
\qml
Row {
add: Transition {
NumberAnimation {
properties: "opacity"
from: 0
to: 1
duration: 500
}
}
}
\endqml
*/
/*!
\qmlproperty Transition Row::move
This property holds the transition to apply when moving an item within the positioner.
This can happen when other items are added or removed from the positioner, or when items resize themselves.
\qml
Row {
id: positioner
move: Transition {
NumberAnimation {
properties: "x"
ease: "easeOutBounce"
}
}
}
\endqml
*/
/*!
\qmlproperty int Row::spacing
spacing is the amount in pixels left empty between each adjacent
item, and defaults to 0.
The below example places a Grid containing a red, a blue and a
green rectangle on a gray background. The area the grid positioner
occupies is colored white. The top positioner has the default of no spacing,
and the bottom positioner has its spacing set to 2.
\image spacing_a.png
\image spacing_b.png
*/
/*!
\internal
\class QFxRow
\brief The QFxRow class lines up items horizontally.
\ingroup group_positioners
*/
QFxRow::QFxRow(QFxItem *parent)
: QFxBasePositioner(Horizontal, parent)
{
}
void QFxRow::doPositioning()
{
int hoffset = 0;
foreach(QFxItem* item, *leavingItems()){
if (remove()){
QList<QPair<QString,QVariant> > changes;
applyRemove(changes, item);
}
}
QList<QGraphicsItem *> children = childItems();
for (int ii = 0; ii < children.count(); ++ii) {
QFxItem *child = qobject_cast<QFxItem *>(children.at(ii));
if (!child || child->opacity() == 0.0)
continue;
bool needMove = (child->x() != hoffset || child->y());
QList<QPair<QString, QVariant> > changes;
changes << qMakePair(QString(QLatin1String("x")),QVariant(hoffset));
changes << qMakePair(QString(QLatin1String("y")),QVariant(0));
if (needMove && items()->contains(child) && move()) {
applyMove(changes,child);
} else if (!items()->contains(child) && add()) {
applyAdd(changes,child);
} else if (needMove) {
setMovingItem(child);
child->setX(hoffset);
setMovingItem(0);
}
if(child->width() && child->height()){//don't advance for invisible children
hoffset += child->width();
hoffset += spacing();
}
}
}
QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,Grid,QFxGrid)
/*!
\qmlclass Grid QFxGrid
\brief The Grid item positions its children in a grid.
\inherits Item
The Grid item positions its child items so that they are
aligned in a grid and are not overlapping. Spacing can be added
between the items. It also provides for transitions to be set when items are
added, moved, or removed in the positioner. Adding and removing apply
both to items which are deleted or have their position in the
document changed so as to no longer be children of the positioner, as
well as to items which have their opacity set to or from zero so
as to appear or disappear.
The Grid defaults to using four columns, and as many rows as
are necessary to fit all the child items. The number of rows
and/or the number of columns can be constrained by setting the rows
or columns properties. The grid positioner calculates a grid with
rectangular cells of sufficient size to hold all items, and then
places the items in the cells, going across then down, and
positioning each item at the (0,0) corner of the cell. The below
example demonstrates this.
\table
\row
\o \image gridLayout_example.png
\o
\qml
Grid {
columns: 3
spacing: 2
Rectangle { color: "red"; width: 50; height: 50 }
Rectangle { color: "green"; width: 20; height: 50 }
Rectangle { color: "blue"; width: 50; height: 20 }
Rectangle { color: "cyan"; width: 50; height: 50 }
Rectangle { color: "magenta"; width: 10; height: 10 }
}
\endqml
\endtable
Note that the positioner assumes that the x and y positions of its children
will not change. If you manually change the x or y properties in script, bind
the x or y properties, or use anchors on a child of a positioner, then the
positioner may exhibit strange behaviour.
*/
/*!
\qmlproperty Transition Grid::remove
This property holds the transition to apply when removing an item from the positioner.
The transition is only applied to the removed item(s).
Removed can mean that either the object has been deleted or
reparented, and thus is now longer a child of the positioner, or that
the object has had its opacity set to zero, and thus is no longer
visible.
Note that if the item counts as removed because its opacity is
zero it will not be visible during the transition unless you set
the opacity in the transition, like in the below example.
\qml
Grid {
remove: Transition {
NumberAnimation {
properties: "opacity"
from: 1
to: 0
duration: 500
}
}
}
\endqml
*/
/*!
\qmlproperty Transition Grid::add
This property holds the transition to apply when adding an item to the positioner.
The transition is only applied to the added item(s).
Added can mean that either the object has been created or
reparented, and thus is now a child or the positioner, or that the
object has had its opacity increased from zero, and thus is now
visible.
\qml
Grid {
add: Transition {
NumberAnimation {
properties: "opacity"
from: 0
to: 1
duration: 500
}
}
}
\endqml
*/
/*!
\qmlproperty Transition Grid::move
This property holds the transition to apply when moving an item within the positioner.
This can happen when other items are added or removed from the positioner, or
when items resize themselves.
\qml
Grid {
move: Transition {
NumberAnimation {
properties: "x,y"
ease: "easeOutBounce"
}
}
}
\endqml
*/
/*!
\qmlproperty int Grid::spacing
spacing is the amount in pixels left empty between each adjacent
item, and defaults to 0.
The below example places a Grid containing a red, a blue and a
green rectangle on a gray background. The area the grid positioner
occupies is colored white. The top positioner has the default of no spacing,
and the bottom positioner has its spacing set to 2.
\image spacing_a.png
\image spacing_b.png
*/
/*!
\internal
\class QFxGrid
\brief The QFxGrid class lays out items in a grid.
\ingroup group_layouts
*/
QFxGrid::QFxGrid(QFxItem *parent) :
QFxBasePositioner(Both, parent)
{
_columns=-1;
_rows=-1;
}
/*!
\qmlproperty int Grid::columns
This property holds the number of columns in the grid.
When the columns property is set the Grid will always have
that many columns. Note that if you do not have enough items to
fill this many columns some columns will be of zero width.
*/
/*!
\qmlproperty int Grid::rows
This property holds the number of rows in the grid.
When the rows property is set the Grid will always have that
many rows. Note that if you do not have enough items to fill this
many rows some rows will be of zero width.
*/
void QFxGrid::doPositioning()
{
int c=_columns,r=_rows;//Actual number of rows/columns
int numVisible = items()->size() + newItems()->size();
if (_columns==-1 && _rows==-1){
c = 4;
r = (numVisible+3)/4;
}else if (_rows==-1){
r = (numVisible+(_columns-1))/_columns;
}else if (_columns==-1){
c = (numVisible+(_rows-1))/_rows;
}
QList<int> maxColWidth;
QList<int> maxRowHeight;
int childIndex =0;
QList<QGraphicsItem *> children = childItems();
for (int i=0; i<r; i++){
for (int j=0; j<c; j++){
if (j==0)
maxRowHeight << 0;
if (i==0)
maxColWidth << 0;
if (childIndex == children.count())
continue;
QFxItem *child = qobject_cast<QFxItem *>(children.at(childIndex++));
if (!child || child->opacity() == 0.0)
continue;
if (child->width() > maxColWidth[j])
maxColWidth[j] = child->width();
if (child->height() > maxRowHeight[i])
maxRowHeight[i] = child->height();
}
}
int xoffset=0;
int yoffset=0;
int curRow =0;
int curCol =0;
foreach(QFxItem* item, *leavingItems()){
if (remove()){
QList<QPair<QString,QVariant> > changes;
applyRemove(changes, item);
}
}
foreach(QGraphicsItem* schild, children){
QFxItem *child = qobject_cast<QFxItem *>(schild);
if (!child || child->opacity() == 0.0)
continue;
bool needMove = (child->x()!=xoffset)||(child->y()!=yoffset);
QList<QPair<QString, QVariant> > changes;
changes << qMakePair(QString(QLatin1String("x")),QVariant(xoffset));
changes << qMakePair(QString(QLatin1String("y")),QVariant(yoffset));
if (newItems()->contains(child) && add()) {
applyAdd(changes,child);
} else if (needMove) {
if (move()){
applyMove(changes,child);
}else{
setMovingItem(child);
child->setPos(QPointF(xoffset, yoffset));
setMovingItem(0);
}
}
xoffset+=maxColWidth[curCol]+spacing();
curCol++;
curCol%=c;
if (!curCol){
yoffset+=maxRowHeight[curRow]+spacing();
xoffset=0;
curRow++;
if (curRow>=r)
break;
}
}
}
QT_END_NAMESPACE
|