-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathtestqgslayoutitem.cpp
2119 lines (1849 loc) · 87.7 KB
/
testqgslayoutitem.cpp
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
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
testqgslayoutitem.cpp
-----------------------
begin : June 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgslayoutitem.h"
#include "qgslayoutitemregistry.h"
#include "qgslayout.h"
#include "qgstest.h"
#include "qgsproject.h"
#include "qgsreadwritecontext.h"
#include "qgslayoutitemmap.h"
#include "qgslayoutitemlabel.h"
#include "qgslayoutitemshape.h"
#include "qgslayouteffect.h"
#include "qgsfillsymbollayer.h"
#include "qgslayoutpagecollection.h"
#include "qgslayoutundostack.h"
#include "qgsvectorlayer.h"
#include "qgsexpressioncontextutils.h"
#include "qgsfillsymbol.h"
#include "qgslayoutrendercontext.h"
#include <QObject>
#include <QPainter>
#include <QImage>
#include <QtTest/QSignalSpy>
//simple item for testing, since some methods in QgsLayoutItem are pure virtual
class TestItem : public QgsLayoutItem
{
Q_OBJECT
public:
TestItem( QgsLayout *layout ) : QgsLayoutItem( layout )
{
setFrameEnabled( false );
setBackgroundEnabled( false );
}
//implement pure virtual methods
int type() const override { return QgsLayoutItemRegistry::LayoutItem + 101; }
bool forceResize = false;
protected:
void draw( QgsLayoutItemRenderContext &context ) override
{
QPainter *painter = context.renderContext().painter();
painter->save();
painter->setRenderHint( QPainter::Antialiasing, false );
painter->setPen( Qt::NoPen );
painter->setBrush( QColor( 255, 100, 100, 200 ) );
painter->drawRect( rect() );
painter->restore();
}
QSizeF applyItemSizeConstraint( QSizeF targetSize ) override
{
if ( !forceResize )
return targetSize;
return QSizeF( 17, 27 );
}
};
//item with minimum size
class MinSizedItem : public TestItem
{
Q_OBJECT
public:
MinSizedItem( QgsLayout *layout ) : TestItem( layout )
{
setMinimumSize( QgsLayoutSize( 5.0, 10.0, Qgis::LayoutUnit::Centimeters ) );
}
void updateMinSize( QgsLayoutSize size )
{
setMinimumSize( size );
}
};
//item with fixed size
class FixedSizedItem : public TestItem
{
Q_OBJECT
public:
FixedSizedItem( QgsLayout *layout ) : TestItem( layout )
{
setFixedSize( QgsLayoutSize( 2.0, 4.0, Qgis::LayoutUnit::Inches ) );
}
void updateFixedSize( QgsLayoutSize size )
{
setFixedSize( size );
}
};
//item with both conflicting fixed and minimum size
class FixedMinSizedItem : public TestItem
{
Q_OBJECT
public:
FixedMinSizedItem( QgsLayout *layout ) : TestItem( layout )
{
setFixedSize( QgsLayoutSize( 2.0, 4.0, Qgis::LayoutUnit::Centimeters ) );
setMinimumSize( QgsLayoutSize( 5.0, 9.0, Qgis::LayoutUnit::Centimeters ) );
}
};
class TestQgsLayoutItem: public QgsTest
{
Q_OBJECT
public:
TestQgsLayoutItem() : QgsTest( QStringLiteral( "Layout Item Tests" ) ) {}
private slots:
void cleanupTestCase();
void creation(); //test creation of QgsLayoutItem
void uuid();
void id();
void registry();
void shouldDrawDebug();
void shouldDrawAntialiased();
void preparePainter();
void debugRect();
void draw();
void resize();
void referencePoint();
void itemPositionReferencePoint();
void adjustPointForReference();
void positionAtReferencePoint();
void fixedSize();
void minSize();
void move();
void positionWithUnits();
void sizeWithUnits();
void dataDefinedPosition();
void dataDefinedSize();
void combinedDataDefinedPositionAndSize();
void rotation();
void writeXml();
void readXml();
void writeReadXmlProperties();
void undoRedo();
void multiItemUndo();
void overlappingUndo();
void blendMode();
void opacity();
void excludeFromExports();
void setSceneRect();
void page();
void itemVariablesFunction();
void variables();
void mapCreditsFunction();
private:
std::unique_ptr< QgsLayoutItem > createCopyViaXml( QgsLayout *layout, QgsLayoutItem *original );
};
void TestQgsLayoutItem::cleanupTestCase()
{
QgsApplication::exitQgis();
}
void TestQgsLayoutItem::creation()
{
QgsProject p;
QgsLayout *layout = new QgsLayout( &p );
TestItem *item = new TestItem( layout );
QVERIFY( item );
delete item;
delete layout;
}
void TestQgsLayoutItem::uuid()
{
QgsProject p;
QgsLayout l( &p );
//basic test of uuid
const TestItem item( &l );
const TestItem item2( &l );
QVERIFY( item.uuid() != item2.uuid() );
}
void TestQgsLayoutItem::id()
{
QgsProject p;
QgsLayout l( &p );
TestItem item( &l );
item.setId( QStringLiteral( "test" ) );
QCOMPARE( item.id(), QStringLiteral( "test" ) );
}
void TestQgsLayoutItem::registry()
{
// test QgsLayoutItemRegistry
QgsLayoutItemRegistry registry;
// empty registry
QVERIFY( !registry.itemMetadata( -1 ) );
QVERIFY( registry.itemTypes().isEmpty() );
QVERIFY( !registry.createItem( 1, nullptr ) );
auto create = []( QgsLayout * layout )->QgsLayoutItem *
{
return new TestItem( layout );
};
auto resolve = []( QVariantMap & props, const QgsPathResolver &, bool )
{
props.clear();
};
const QSignalSpy spyTypeAdded( ®istry, &QgsLayoutItemRegistry::typeAdded );
QgsLayoutItemMetadata *metadata = new QgsLayoutItemMetadata( 2, QStringLiteral( "my type" ), QStringLiteral( "my types" ), create, resolve );
QVERIFY( registry.addLayoutItemType( metadata ) );
QCOMPARE( spyTypeAdded.count(), 1 );
QCOMPARE( spyTypeAdded.value( 0 ).at( 0 ).toInt(), 2 );
QCOMPARE( spyTypeAdded.value( 0 ).at( 1 ).toString(), QStringLiteral( "my type" ) );
// duplicate type id
QVERIFY( !registry.addLayoutItemType( metadata ) );
QCOMPARE( spyTypeAdded.count(), 1 );
//retrieve metadata
QVERIFY( !registry.itemMetadata( -1 ) );
QCOMPARE( registry.itemMetadata( 2 )->visibleName(), QStringLiteral( "my type" ) );
QCOMPARE( registry.itemMetadata( 2 )->visiblePluralName(), QStringLiteral( "my types" ) );
QCOMPARE( registry.itemTypes().count(), 1 );
QCOMPARE( registry.itemTypes().value( 2 ), QStringLiteral( "my type" ) );
QgsLayoutItem *item = registry.createItem( 2, nullptr );
QVERIFY( item );
QVERIFY( dynamic_cast< TestItem *>( item ) );
delete item;
QVariantMap props;
props.insert( QStringLiteral( "a" ), 5 );
registry.resolvePaths( 1, props, QgsPathResolver(), true );
QCOMPARE( props.size(), 1 );
registry.resolvePaths( 2, props, QgsPathResolver(), true );
QVERIFY( props.isEmpty() );
//test populate
QgsLayoutItemRegistry reg2;
QVERIFY( reg2.itemTypes().isEmpty() );
QVERIFY( reg2.populate() );
QVERIFY( !reg2.itemTypes().isEmpty() );
QVERIFY( !reg2.populate() );
}
void TestQgsLayoutItem::shouldDrawDebug()
{
QgsProject p;
QgsLayout l( &p );
TestItem *item = new TestItem( &l );
l.renderContext().setFlag( QgsLayoutRenderContext::FlagDebug, true );
QVERIFY( item->shouldDrawDebugRect() );
l.renderContext().setFlag( QgsLayoutRenderContext::FlagDebug, false );
QVERIFY( !item->shouldDrawDebugRect() );
delete item;
}
void TestQgsLayoutItem::shouldDrawAntialiased()
{
QgsProject p;
QgsLayout l( &p );
TestItem *item = new TestItem( &l );
l.renderContext().setFlag( QgsLayoutRenderContext::FlagAntialiasing, false );
QVERIFY( !item->shouldDrawAntialiased() );
l.renderContext().setFlag( QgsLayoutRenderContext::FlagAntialiasing, true );
QVERIFY( item->shouldDrawAntialiased() );
delete item;
}
void TestQgsLayoutItem::preparePainter()
{
QgsProject p;
QgsLayout l( &p );
TestItem *item = new TestItem( &l );
//test with no painter
item->preparePainter( nullptr );
//test antialiasing correctly set for painter
QImage image( QSize( 100, 100 ), QImage::Format_ARGB32 );
QPainter painter;
painter.begin( &image );
l.renderContext().setFlag( QgsLayoutRenderContext::FlagAntialiasing, false );
item->preparePainter( &painter );
QVERIFY( !( painter.renderHints() & QPainter::Antialiasing ) );
l.renderContext().setFlag( QgsLayoutRenderContext::FlagAntialiasing, true );
item->preparePainter( &painter );
QVERIFY( painter.renderHints() & QPainter::Antialiasing );
delete item;
}
void TestQgsLayoutItem::debugRect()
{
QgsProject p;
QgsLayout l( &p );
TestItem *item = new TestItem( &l );
l.addItem( item );
item->setPos( 100, 100 );
item->setRect( 0, 0, 200, 200 );
l.setSceneRect( 0, 0, 400, 400 );
l.renderContext().setFlag( QgsLayoutRenderContext::FlagDebug, true );
QImage image( l.sceneRect().size().toSize(), QImage::Format_ARGB32 );
image.fill( 0 );
QPainter painter( &image );
l.render( &painter );
painter.end();
mControlPathPrefix = QStringLiteral( "layouts" );
QGSVERIFYIMAGECHECK( QStringLiteral( "layoutitem_debugrect" ), QStringLiteral( "layoutitem_debugrect" ), image, QString(), 0 );
}
void TestQgsLayoutItem::draw()
{
QgsProject p;
QgsLayout l( &p );
TestItem *item = new TestItem( &l );
l.addItem( item );
item->setPos( 100, 100 );
item->setRect( 0, 0, 200, 200 );
l.setSceneRect( 0, 0, 400, 400 );
l.renderContext().setFlag( QgsLayoutRenderContext::FlagAntialiasing, false ); //disable antialiasing to limit cross platform differences
QImage image( l.sceneRect().size().toSize(), QImage::Format_ARGB32 );
image.fill( 0 );
QPainter painter( &image );
l.render( &painter );
painter.end();
mControlPathPrefix = QStringLiteral( "layouts" );
QGSVERIFYIMAGECHECK( QStringLiteral( "layoutitem_draw" ), QStringLiteral( "layoutitem_draw" ), image, QString(), 0 );
}
void TestQgsLayoutItem::positionWithUnits()
{
QgsProject p;
QgsLayout l( &p );
std::unique_ptr< TestItem > item( new TestItem( &l ) );
item->attemptMove( QgsLayoutPoint( 60.0, 15.0, Qgis::LayoutUnit::Millimeters ) );
QCOMPARE( item->positionWithUnits().x(), 60.0 );
QCOMPARE( item->positionWithUnits().y(), 15.0 );
QCOMPARE( item->positionWithUnits().units(), Qgis::LayoutUnit::Millimeters );
item->attemptMove( QgsLayoutPoint( 50.0, 100.0, Qgis::LayoutUnit::Pixels ) );
QCOMPARE( item->positionWithUnits().x(), 50.0 );
QCOMPARE( item->positionWithUnits().y(), 100.0 );
QCOMPARE( item->positionWithUnits().units(), Qgis::LayoutUnit::Pixels );
}
void TestQgsLayoutItem::sizeWithUnits()
{
QgsProject p;
QgsLayout l( &p );
TestItem *item = new TestItem( &l );
item->attemptResize( QgsLayoutSize( 60.0, 15.0, Qgis::LayoutUnit::Millimeters ) );
QCOMPARE( item->sizeWithUnits().width(), 60.0 );
QCOMPARE( item->sizeWithUnits().height(), 15.0 );
QCOMPARE( item->sizeWithUnits().units(), Qgis::LayoutUnit::Millimeters );
item->attemptResize( QgsLayoutSize( 50.0, 100.0, Qgis::LayoutUnit::Pixels ) );
QCOMPARE( item->sizeWithUnits().width(), 50.0 );
QCOMPARE( item->sizeWithUnits().height(), 100.0 );
QCOMPARE( item->sizeWithUnits().units(), Qgis::LayoutUnit::Pixels );
delete item;
}
void TestQgsLayoutItem::dataDefinedPosition()
{
QgsProject p;
QgsLayout l( &p );
//test setting data defined position
TestItem *item = new TestItem( &l );
l.setUnits( Qgis::LayoutUnit::Millimeters );
item->attemptMove( QgsLayoutPoint( 6.0, 1.50, Qgis::LayoutUnit::Centimeters ) );
item->attemptResize( QgsLayoutSize( 2.0, 4.0, Qgis::LayoutUnit::Centimeters ) );
// position x
item->dataDefinedProperties().setProperty( QgsLayoutObject::PositionX, QgsProperty::fromExpression( QStringLiteral( "4+7" ) ) );
item->refreshDataDefinedProperty( QgsLayoutObject::PositionX );
QCOMPARE( item->positionWithUnits().x(), 11.0 );
QCOMPARE( item->positionWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->scenePos().x(), 110.0 ); //mm
//position y
item->dataDefinedProperties().setProperty( QgsLayoutObject::PositionY, QgsProperty::fromExpression( QStringLiteral( "2+3" ) ) );
item->refreshDataDefinedProperty( QgsLayoutObject::PositionY );
QCOMPARE( item->positionWithUnits().y(), 5.0 );
QCOMPARE( item->positionWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->scenePos().y(), 50.0 ); //mm
//refreshPosition should also respect data defined positioning
item->setPos( 0, 0 );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PositionX, QgsProperty::fromExpression( QStringLiteral( "4+8" ) ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PositionY, QgsProperty::fromExpression( QStringLiteral( "2+4" ) ) );
item->refreshItemPosition();
QCOMPARE( item->positionWithUnits().x(), 12.0 );
QCOMPARE( item->positionWithUnits().y(), 6.0 );
QCOMPARE( item->positionWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->scenePos().x(), 120.0 ); //mm
QCOMPARE( item->scenePos().y(), 60.0 ); //mm
//also check that data defined position overrides when attempting to move
item->attemptMove( QgsLayoutPoint( 6.0, 1.50, Qgis::LayoutUnit::Centimeters ) );
QCOMPARE( item->positionWithUnits().x(), 12.0 );
QCOMPARE( item->positionWithUnits().y(), 6.0 );
QCOMPARE( item->positionWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->scenePos().x(), 120.0 ); //mm
QCOMPARE( item->scenePos().y(), 60.0 ); //mm
//restriction only for x position
item->dataDefinedProperties().setProperty( QgsLayoutObject::PositionX, QgsProperty::fromExpression( QStringLiteral( "4+8" ) ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PositionY, QgsProperty() );
item->attemptMove( QgsLayoutPoint( 6.0, 1.5, Qgis::LayoutUnit::Centimeters ) );
QCOMPARE( item->positionWithUnits().x(), 12.0 );
QCOMPARE( item->positionWithUnits().y(), 1.5 );
QCOMPARE( item->positionWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->scenePos().x(), 120.0 ); //mm
QCOMPARE( item->scenePos().y(), 15.0 ); //mm
//restriction only for y position
item->dataDefinedProperties().setProperty( QgsLayoutObject::PositionX, QgsProperty() );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PositionY, QgsProperty::fromExpression( QStringLiteral( "2+4" ) ) );
item->attemptMove( QgsLayoutPoint( 7.0, 1.5, Qgis::LayoutUnit::Centimeters ) );
QCOMPARE( item->positionWithUnits().x(), 7.0 );
QCOMPARE( item->positionWithUnits().y(), 6.0 );
QCOMPARE( item->positionWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->scenePos().x(), 70.0 ); //mm
QCOMPARE( item->scenePos().y(), 60.0 ); //mm
//check change of units should apply to data defined position
item->dataDefinedProperties().setProperty( QgsLayoutObject::PositionX, QgsProperty::fromExpression( QStringLiteral( "4+8" ) ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PositionY, QgsProperty::fromExpression( QStringLiteral( "2+4" ) ) );
//first set to same as existing position, but with different units
item->attemptMove( QgsLayoutPoint( 120.0, 60.0, Qgis::LayoutUnit::Millimeters ) );
//data defined position should utilize new units
QCOMPARE( item->positionWithUnits().x(), 12.0 ); //mm
QCOMPARE( item->positionWithUnits().y(), 6.0 ); //mm
QCOMPARE( item->positionWithUnits().units(), Qgis::LayoutUnit::Millimeters );
QCOMPARE( item->scenePos().x(), 12.0 ); //mm
QCOMPARE( item->scenePos().y(), 6.0 ); //mm
//test that data defined position applies to item's reference point
item->attemptMove( QgsLayoutPoint( 12.0, 6.0, Qgis::LayoutUnit::Centimeters ) );
item->setReferencePoint( QgsLayoutItem::LowerRight );
QCOMPARE( item->positionWithUnits().x(), 12.0 ); //cm
QCOMPARE( item->positionWithUnits().y(), 6.0 ); //cm
QCOMPARE( item->positionWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->scenePos().x(), 100.0 ); //mm
QCOMPARE( item->scenePos().y(), 20.0 ); //mm
//also check setting data defined position AFTER setting reference point
item->setPos( 0, 0 );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PositionX, QgsProperty::fromExpression( QStringLiteral( "6+10" ) ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PositionY, QgsProperty::fromExpression( QStringLiteral( "2+6" ) ) );
item->refreshItemPosition();
QCOMPARE( item->positionWithUnits().x(), 16.0 ); //cm
QCOMPARE( item->positionWithUnits().y(), 8.0 ); //cm
QCOMPARE( item->positionWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->scenePos().x(), 140.0 ); //mm
QCOMPARE( item->scenePos().y(), 40.0 ); //mm
delete item;
}
void TestQgsLayoutItem::dataDefinedSize()
{
QgsProject p;
QgsLayout l( &p );
//test setting data defined size
TestItem *item = new TestItem( &l );
l.setUnits( Qgis::LayoutUnit::Millimeters );
item->attemptMove( QgsLayoutPoint( 6.0, 1.50, Qgis::LayoutUnit::Centimeters ) );
item->attemptResize( QgsLayoutSize( 2.0, 4.0, Qgis::LayoutUnit::Centimeters ) );
//width
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty::fromExpression( QStringLiteral( "4+7" ) ) );
item->refreshDataDefinedProperty( QgsLayoutObject::ItemWidth );
QCOMPARE( item->sizeWithUnits().width(), 11.0 );
QCOMPARE( item->sizeWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->rect().width(), 110.0 ); //mm
//height
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty::fromExpression( QStringLiteral( "2+3" ) ) );
item->refreshDataDefinedProperty( QgsLayoutObject::ItemHeight );
QCOMPARE( item->sizeWithUnits().height(), 5.0 );
QCOMPARE( item->sizeWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->rect().height(), 50.0 ); //mm
//refreshSize should also respect data defined size
item->setRect( 0.0, 0.0, 9.0, 8.0 );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty::fromExpression( QStringLiteral( "4+8" ) ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty::fromExpression( QStringLiteral( "2+4" ) ) );
item->refreshItemSize();
QCOMPARE( item->sizeWithUnits().width(), 12.0 );
QCOMPARE( item->sizeWithUnits().height(), 6.0 );
QCOMPARE( item->sizeWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->rect().width(), 120.0 ); //mm
QCOMPARE( item->rect().height(), 60.0 ); //mm
//also check that data defined size overrides when attempting to resize
item->attemptResize( QgsLayoutSize( 6.0, 1.50, Qgis::LayoutUnit::Centimeters ) );
QCOMPARE( item->sizeWithUnits().width(), 12.0 );
QCOMPARE( item->sizeWithUnits().height(), 6.0 );
QCOMPARE( item->sizeWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->rect().width(), 120.0 ); //mm
QCOMPARE( item->rect().height(), 60.0 ); //mm
//restriction only for width
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty::fromExpression( QStringLiteral( "4+8" ) ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty() );
item->attemptResize( QgsLayoutSize( 6.0, 1.50, Qgis::LayoutUnit::Centimeters ) );
QCOMPARE( item->sizeWithUnits().width(), 12.0 );
QCOMPARE( item->sizeWithUnits().height(), 1.5 );
QCOMPARE( item->sizeWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->rect().width(), 120.0 ); //mm
QCOMPARE( item->rect().height(), 15.0 ); //mm
//restriction only for y position
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty() );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty::fromExpression( QStringLiteral( "2+4" ) ) );
item->attemptResize( QgsLayoutSize( 7.0, 1.50, Qgis::LayoutUnit::Centimeters ) );
QCOMPARE( item->sizeWithUnits().width(), 7.0 );
QCOMPARE( item->sizeWithUnits().height(), 6.0 );
QCOMPARE( item->sizeWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->rect().width(), 70.0 ); //mm
QCOMPARE( item->rect().height(), 60.0 ); //mm
// data defined page size
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty() );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty() );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PresetPaperSize, QgsProperty::fromValue( QStringLiteral( "A5" ) ) );
item->attemptResize( QgsLayoutSize( 7.0, 1.50, Qgis::LayoutUnit::Centimeters ) );
QCOMPARE( item->sizeWithUnits().width(), 14.8 );
QCOMPARE( item->sizeWithUnits().height(), 21.0 );
QCOMPARE( item->sizeWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->rect().width(), 148.0 ); //mm
QCOMPARE( item->rect().height(), 210.0 ); //mm
// data defined height/width should override page size
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty::fromValue( "13.0" ) );
item->attemptResize( QgsLayoutSize( 7.0, 1.50, Qgis::LayoutUnit::Centimeters ) );
QCOMPARE( item->sizeWithUnits().width(), 13.0 );
QCOMPARE( item->sizeWithUnits().height(), 21.0 );
QCOMPARE( item->sizeWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->rect().width(), 130.0 ); //mm
QCOMPARE( item->rect().height(), 210.0 ); //mm
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty::fromValue( "3.0" ) );
item->attemptResize( QgsLayoutSize( 7.0, 1.50, Qgis::LayoutUnit::Centimeters ) );
QCOMPARE( item->sizeWithUnits().width(), 13.0 );
QCOMPARE( item->sizeWithUnits().height(), 3.0 );
QCOMPARE( item->sizeWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->rect().width(), 130.0 ); //mm
QCOMPARE( item->rect().height(), 30.0 ); //mm
// data defined orientation
item->dataDefinedProperties().setProperty( QgsLayoutObject::PaperOrientation, QgsProperty::fromValue( "portrait" ) );
item->attemptResize( QgsLayoutSize( 7.0, 1.50, Qgis::LayoutUnit::Centimeters ) );
QCOMPARE( item->sizeWithUnits().width(), 3.0 );
QCOMPARE( item->sizeWithUnits().height(), 13.0 );
QCOMPARE( item->sizeWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->rect().width(), 30.0 ); //mm
QCOMPARE( item->rect().height(), 130.0 ); //mm
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty() );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty() );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PresetPaperSize, QgsProperty() );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PaperOrientation, QgsProperty::fromValue( "landscape" ) );
item->attemptResize( QgsLayoutSize( 1.0, 1.50, Qgis::LayoutUnit::Centimeters ) );
QCOMPARE( item->sizeWithUnits().width(), 1.5 );
QCOMPARE( item->sizeWithUnits().height(), 1.0 );
QCOMPARE( item->sizeWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->rect().width(), 15.0 ); //mm
QCOMPARE( item->rect().height(), 10.0 ); //mm
item->dataDefinedProperties().setProperty( QgsLayoutObject::PaperOrientation, QgsProperty() );
//check change of units should apply to data defined size
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty::fromExpression( QStringLiteral( "4+8" ) ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty::fromExpression( QStringLiteral( "2+4" ) ) );
//first set to same as existing size, but with different units
item->attemptResize( QgsLayoutSize( 120.0, 60.0, Qgis::LayoutUnit::Millimeters ) );
//data defined size should utilize new units
QCOMPARE( item->sizeWithUnits().width(), 12.0 ); //mm
QCOMPARE( item->sizeWithUnits().height(), 6.0 ); //mm
QCOMPARE( item->sizeWithUnits().units(), Qgis::LayoutUnit::Millimeters );
QCOMPARE( item->rect().width(), 12.0 ); //mm
QCOMPARE( item->rect().height(), 6.0 ); //mm
//test that data defined size applies to item's reference point
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty() );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty() );
item->attemptResize( QgsLayoutSize( 10.0, 5.0, Qgis::LayoutUnit::Millimeters ) );
item->attemptMove( QgsLayoutPoint( 20.0, 10.0, Qgis::LayoutUnit::Millimeters ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty::fromExpression( QStringLiteral( "5" ) ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty::fromExpression( QStringLiteral( "6" ) ) );
item->setReferencePoint( QgsLayoutItem::LowerRight );
item->refreshItemSize();
QCOMPARE( item->scenePos().x(), 25.0 ); //mm
QCOMPARE( item->scenePos().y(), 9.0 ); //mm
//test that data defined size applied after setting item's reference point respects reference
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty() );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty() );
item->setReferencePoint( QgsLayoutItem::UpperLeft );
item->attemptResize( QgsLayoutSize( 10.0, 5.0, Qgis::LayoutUnit::Millimeters ) );
item->attemptMove( QgsLayoutPoint( 20.0, 10.0, Qgis::LayoutUnit::Millimeters ) );
item->setReferencePoint( QgsLayoutItem::LowerRight );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty::fromExpression( QStringLiteral( "7" ) ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty::fromExpression( QStringLiteral( "9" ) ) );
item->refreshItemSize();
QCOMPARE( item->scenePos().x(), 23.0 ); //mm
QCOMPARE( item->scenePos().y(), 6.0 ); //mm
delete item;
}
void TestQgsLayoutItem::combinedDataDefinedPositionAndSize()
{
QgsProject p;
QgsLayout l( &p );
//test setting data defined size
TestItem *item = new TestItem( &l );
l.setUnits( Qgis::LayoutUnit::Millimeters );
item->attemptMove( QgsLayoutPoint( 6.0, 1.50, Qgis::LayoutUnit::Centimeters ) );
item->attemptResize( QgsLayoutSize( 2.0, 4.0, Qgis::LayoutUnit::Centimeters ) );
//test item with all of data defined x, y, width, height set
item->dataDefinedProperties().setProperty( QgsLayoutObject::PositionX, QgsProperty::fromExpression( QStringLiteral( "4+7" ) ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PositionY, QgsProperty::fromExpression( QStringLiteral( "2+3" ) ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty::fromExpression( QStringLiteral( "4+9" ) ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty::fromExpression( QStringLiteral( "2+4" ) ) );
item->refreshDataDefinedProperty( QgsLayoutObject::AllProperties );
QCOMPARE( item->positionWithUnits().x(), 11.0 );
QCOMPARE( item->positionWithUnits().y(), 5.0 );
QCOMPARE( item->positionWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->sizeWithUnits().width(), 13.0 );
QCOMPARE( item->sizeWithUnits().height(), 6.0 );
QCOMPARE( item->sizeWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->scenePos().x(), 110.0 ); //mm
QCOMPARE( item->scenePos().y(), 50.0 ); //mm
QCOMPARE( item->rect().width(), 130.0 ); //mm
QCOMPARE( item->rect().height(), 60.0 ); //mm
//also try with reference point set
item->setReferencePoint( QgsLayoutItem::Middle );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PositionX, QgsProperty::fromExpression( QStringLiteral( "4+8" ) ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::PositionY, QgsProperty::fromExpression( QStringLiteral( "2+4" ) ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemWidth, QgsProperty::fromExpression( QStringLiteral( "3+7" ) ) );
item->dataDefinedProperties().setProperty( QgsLayoutObject::ItemHeight, QgsProperty::fromExpression( QStringLiteral( "1+3" ) ) );
item->refreshDataDefinedProperty( QgsLayoutObject::AllProperties );
QCOMPARE( item->positionWithUnits().x(), 12.0 );
QCOMPARE( item->positionWithUnits().y(), 6.0 );
QCOMPARE( item->positionWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->sizeWithUnits().width(), 10.0 );
QCOMPARE( item->sizeWithUnits().height(), 4.0 );
QCOMPARE( item->sizeWithUnits().units(), Qgis::LayoutUnit::Centimeters );
QCOMPARE( item->scenePos().x(), 70.0 ); //mm
QCOMPARE( item->scenePos().y(), 40.0 ); //mm
QCOMPARE( item->rect().width(), 100.0 ); //mm
QCOMPARE( item->rect().height(), 40.0 ); //mm
delete item;
}
//TODO rotation
void TestQgsLayoutItem::resize()
{
QgsProject p;
QgsLayout l( &p );
//resize test item (no restrictions), same units as layout
l.setUnits( Qgis::LayoutUnit::Millimeters );
std::unique_ptr< TestItem > item( new TestItem( &l ) );
const QSignalSpy spySizeChanged( item.get(), &QgsLayoutItem::sizePositionChanged );
item->setRect( 0, 0, 55, 45 );
item->attemptMove( QgsLayoutPoint( 27, 29 ) );
QCOMPARE( spySizeChanged.count(), 1 );
item->attemptResize( QgsLayoutSize( 100.0, 200.0, Qgis::LayoutUnit::Millimeters ) );
QCOMPARE( spySizeChanged.count(), 2 );
QCOMPARE( item->rect().width(), 100.0 );
QCOMPARE( item->rect().height(), 200.0 );
QCOMPARE( item->scenePos().x(), 27.0 ); //item should not move
QCOMPARE( item->scenePos().y(), 29.0 );
//test conversion of units
l.setUnits( Qgis::LayoutUnit::Centimeters );
item->setRect( 0, 0, 100, 200 );
item->attemptResize( QgsLayoutSize( 0.30, 0.45, Qgis::LayoutUnit::Meters ) );
QCOMPARE( item->rect().width(), 30.0 );
QCOMPARE( item->rect().height(), 45.0 );
QCOMPARE( spySizeChanged.count(), 4 );
//test pixel -> page conversion
l.setUnits( Qgis::LayoutUnit::Inches );
l.renderContext().setDpi( 100.0 );
item->refresh();
QCOMPARE( spySizeChanged.count(), 6 );
item->setRect( 0, 0, 1, 2 );
item->attemptResize( QgsLayoutSize( 140, 280, Qgis::LayoutUnit::Pixels ) );
QCOMPARE( item->rect().width(), 1.4 );
QCOMPARE( item->rect().height(), 2.8 );
QCOMPARE( spySizeChanged.count(), 7 );
//changing the dpi should resize the item
l.renderContext().setDpi( 200.0 );
item->refresh();
QCOMPARE( item->rect().width(), 0.7 );
QCOMPARE( item->rect().height(), 1.4 );
QCOMPARE( spySizeChanged.count(), 8 );
//test page -> pixel conversion
l.setUnits( Qgis::LayoutUnit::Pixels );
l.renderContext().setDpi( 100.0 );
item->refresh();
item->setRect( 0, 0, 2, 2 );
QCOMPARE( spySizeChanged.count(), 10 );
item->attemptResize( QgsLayoutSize( 1, 3, Qgis::LayoutUnit::Inches ) );
QCOMPARE( item->rect().width(), 100.0 );
QCOMPARE( item->rect().height(), 300.0 );
QCOMPARE( spySizeChanged.count(), 11 );
//changing dpi results in item resize
l.renderContext().setDpi( 200.0 );
item->refresh();
QCOMPARE( item->rect().width(), 200.0 );
QCOMPARE( item->rect().height(), 600.0 );
QCOMPARE( spySizeChanged.count(), 13 );
l.setUnits( Qgis::LayoutUnit::Millimeters );
// try override item size in item
item->forceResize = true;
item->attemptResize( QgsLayoutSize( 10.0, 15.0, Qgis::LayoutUnit::Millimeters ) );
QCOMPARE( item->rect().width(), 17.0 );
QCOMPARE( item->rect().height(), 27.0 );
}
void TestQgsLayoutItem::referencePoint()
{
QgsProject p;
QgsLayout l( &p );
//test setting/getting reference point
std::unique_ptr< TestItem > item( new TestItem( &l ) );
item->setReferencePoint( QgsLayoutItem::LowerMiddle );
QCOMPARE( item->referencePoint(), QgsLayoutItem::LowerMiddle );
//test that setting reference point results in positionWithUnits returning position at new reference
//point
item->setReferencePoint( QgsLayoutItem::UpperLeft );
item->attemptResize( QgsLayoutSize( 2, 4 ) );
item->attemptMove( QgsLayoutPoint( 1, 2 ) );
QCOMPARE( item->positionWithUnits().x(), 1.0 );
QCOMPARE( item->positionWithUnits().y(), 2.0 );
item->setReferencePoint( QgsLayoutItem::UpperLeft );
QCOMPARE( item->positionWithUnits().x(), 1.0 );
QCOMPARE( item->positionWithUnits().y(), 2.0 );
item->setReferencePoint( QgsLayoutItem::UpperMiddle );
QCOMPARE( item->positionWithUnits().x(), 2.0 );
QCOMPARE( item->positionWithUnits().y(), 2.0 );
item->setReferencePoint( QgsLayoutItem::UpperRight );
QCOMPARE( item->positionWithUnits().x(), 3.0 );
QCOMPARE( item->positionWithUnits().y(), 2.0 );
item->setReferencePoint( QgsLayoutItem::MiddleLeft );
QCOMPARE( item->positionWithUnits().x(), 1.0 );
QCOMPARE( item->positionWithUnits().y(), 4.0 );
item->setReferencePoint( QgsLayoutItem::Middle );
QCOMPARE( item->positionWithUnits().x(), 2.0 );
QCOMPARE( item->positionWithUnits().y(), 4.0 );
item->setReferencePoint( QgsLayoutItem::MiddleRight );
QCOMPARE( item->positionWithUnits().x(), 3.0 );
QCOMPARE( item->positionWithUnits().y(), 4.0 );
item->setReferencePoint( QgsLayoutItem::LowerLeft );
QCOMPARE( item->positionWithUnits().x(), 1.0 );
QCOMPARE( item->positionWithUnits().y(), 6.0 );
item->setReferencePoint( QgsLayoutItem::LowerMiddle );
QCOMPARE( item->positionWithUnits().x(), 2.0 );
QCOMPARE( item->positionWithUnits().y(), 6.0 );
item->setReferencePoint( QgsLayoutItem::LowerRight );
QCOMPARE( item->positionWithUnits().x(), 3.0 );
QCOMPARE( item->positionWithUnits().y(), 6.0 );
item.reset( new TestItem( &l ) );
//test that setting item position is done relative to reference point
l.setUnits( Qgis::LayoutUnit::Millimeters );
item->attemptResize( QgsLayoutSize( 2, 4 ) );
item->setReferencePoint( QgsLayoutItem::UpperLeft );
item->attemptMove( QgsLayoutPoint( 1, 2 ) );
QCOMPARE( item->scenePos().x(), 1.0 );
QCOMPARE( item->scenePos().y(), 2.0 );
item->setReferencePoint( QgsLayoutItem::UpperMiddle );
item->attemptMove( QgsLayoutPoint( 1, 2 ) );
QCOMPARE( item->scenePos().x(), 0.0 );
QCOMPARE( item->scenePos().y(), 2.0 );
item->setReferencePoint( QgsLayoutItem::UpperRight );
item->attemptMove( QgsLayoutPoint( 1, 2 ) );
QCOMPARE( item->scenePos().x(), -1.0 );
QCOMPARE( item->scenePos().y(), 2.0 );
item->setReferencePoint( QgsLayoutItem::MiddleLeft );
item->attemptMove( QgsLayoutPoint( 1, 2 ) );
QCOMPARE( item->scenePos().x(), 1.0 );
QCOMPARE( item->scenePos().y(), 0.0 );
item->setReferencePoint( QgsLayoutItem::Middle );
item->attemptMove( QgsLayoutPoint( 1, 2 ) );
QCOMPARE( item->scenePos().x(), 0.0 );
QCOMPARE( item->scenePos().y(), 0.0 );
item->setReferencePoint( QgsLayoutItem::MiddleRight );
item->attemptMove( QgsLayoutPoint( 1, 2 ) );
QCOMPARE( item->scenePos().x(), -1.0 );
QCOMPARE( item->scenePos().y(), 0.0 );
item->setReferencePoint( QgsLayoutItem::LowerLeft );
item->attemptMove( QgsLayoutPoint( 1, 2 ) );
QCOMPARE( item->scenePos().x(), 1.0 );
QCOMPARE( item->scenePos().y(), -2.0 );
item->setReferencePoint( QgsLayoutItem::LowerMiddle );
item->attemptMove( QgsLayoutPoint( 1, 2 ) );
QCOMPARE( item->scenePos().x(), 0.0 );
QCOMPARE( item->scenePos().y(), -2.0 );
item->setReferencePoint( QgsLayoutItem::LowerRight );
item->attemptMove( QgsLayoutPoint( 1, 2 ) );
QCOMPARE( item->scenePos().x(), -1.0 );
QCOMPARE( item->scenePos().y(), -2.0 );
item.reset( new TestItem( &l ) );
//test that resizing is done relative to reference point
item->attemptResize( QgsLayoutSize( 2, 4 ) );
item->setReferencePoint( QgsLayoutItem::UpperLeft );
item->attemptMove( QgsLayoutPoint( 1, 2 ) );
item->attemptResize( QgsLayoutSize( 4, 6 ) );
QCOMPARE( item->scenePos().x(), 1.0 );
QCOMPARE( item->scenePos().y(), 2.0 );
item->setReferencePoint( QgsLayoutItem::UpperMiddle );
item->attemptResize( QgsLayoutSize( 6, 4 ) );
QCOMPARE( item->scenePos().x(), 0.0 );
QCOMPARE( item->scenePos().y(), 2.0 );
item->setReferencePoint( QgsLayoutItem::UpperRight );
item->attemptResize( QgsLayoutSize( 4, 6 ) );
QCOMPARE( item->scenePos().x(), 2.0 );
QCOMPARE( item->scenePos().y(), 2.0 );
item->setReferencePoint( QgsLayoutItem::MiddleLeft );
item->attemptResize( QgsLayoutSize( 6, 4 ) );
QCOMPARE( item->scenePos().x(), 2.0 );
QCOMPARE( item->scenePos().y(), 3.0 );
item->setReferencePoint( QgsLayoutItem::Middle );
item->attemptResize( QgsLayoutSize( 4, 6 ) );
QCOMPARE( item->scenePos().x(), 3.0 );
QCOMPARE( item->scenePos().y(), 2.0 );
item->setReferencePoint( QgsLayoutItem::MiddleRight );
item->attemptResize( QgsLayoutSize( 6, 4 ) );
QCOMPARE( item->scenePos().x(), 1.0 );
QCOMPARE( item->scenePos().y(), 3.0 );
item->setReferencePoint( QgsLayoutItem::LowerLeft );
item->attemptResize( QgsLayoutSize( 4, 6 ) );
QCOMPARE( item->scenePos().x(), 1.0 );
QCOMPARE( item->scenePos().y(), 1.0 );
item->setReferencePoint( QgsLayoutItem::LowerMiddle );
item->attemptResize( QgsLayoutSize( 6, 4 ) );
QCOMPARE( item->scenePos().x(), 0.0 );
QCOMPARE( item->scenePos().y(), 3.0 );
item->setReferencePoint( QgsLayoutItem::LowerRight );
item->attemptResize( QgsLayoutSize( 4, 6 ) );
QCOMPARE( item->scenePos().x(), 2.0 );
QCOMPARE( item->scenePos().y(), 1.0 );
item.reset( new TestItem( &l ) );
//item with frame
item->attemptResize( QgsLayoutSize( 2, 4 ) );
item->attemptMove( QgsLayoutPoint( 1, 2 ) );
item->setFrameEnabled( true );
item->setFrameStrokeWidth( QgsLayoutMeasurement( 1 ) );
item->attemptResize( QgsLayoutSize( 4, 6 ) );
QCOMPARE( item->sizeWithUnits().width(), 4.0 );
QCOMPARE( item->sizeWithUnits().height(), 6.0 );
item->attemptResize( QgsLayoutSize( 4, 6 ), true );
QCOMPARE( item->sizeWithUnits().width(), 3.0 );
QCOMPARE( item->sizeWithUnits().height(), 5.0 );
}
void TestQgsLayoutItem::itemPositionReferencePoint()
{
QgsProject p;
QgsLayout l( &p );
TestItem *item = new TestItem( &l );
QPointF result = item->itemPositionAtReferencePoint( QgsLayoutItem::UpperLeft, QSizeF( 2, 4 ) );
QCOMPARE( result.x(), 0.0 );
QCOMPARE( result.y(), 0.0 );
result = item->itemPositionAtReferencePoint( QgsLayoutItem::UpperMiddle, QSizeF( 2, 4 ) );
QCOMPARE( result.x(), 1.0 );
QCOMPARE( result.y(), 0.0 );
result = item->itemPositionAtReferencePoint( QgsLayoutItem::UpperRight, QSizeF( 2, 4 ) );
QCOMPARE( result.x(), 2.0 );
QCOMPARE( result.y(), 0.0 );
result = item->itemPositionAtReferencePoint( QgsLayoutItem::MiddleLeft, QSizeF( 2, 4 ) );
QCOMPARE( result.x(), 0.0 );
QCOMPARE( result.y(), 2.0 );
result = item->itemPositionAtReferencePoint( QgsLayoutItem::Middle, QSizeF( 2, 4 ) );
QCOMPARE( result.x(), 1.0 );
QCOMPARE( result.y(), 2.0 );
result = item->itemPositionAtReferencePoint( QgsLayoutItem::MiddleRight, QSizeF( 2, 4 ) );
QCOMPARE( result.x(), 2.0 );
QCOMPARE( result.y(), 2.0 );
result = item->itemPositionAtReferencePoint( QgsLayoutItem::LowerLeft, QSizeF( 2, 4 ) );
QCOMPARE( result.x(), 0.0 );
QCOMPARE( result.y(), 4.0 );
result = item->itemPositionAtReferencePoint( QgsLayoutItem::LowerMiddle, QSizeF( 2, 4 ) );
QCOMPARE( result.x(), 1.0 );
QCOMPARE( result.y(), 4.0 );
result = item->itemPositionAtReferencePoint( QgsLayoutItem::LowerRight, QSizeF( 2, 4 ) );
QCOMPARE( result.x(), 2.0 );
QCOMPARE( result.y(), 4.0 );
delete item;
}
void TestQgsLayoutItem::adjustPointForReference()
{
QgsProject p;
QgsLayout l( &p );
std::unique_ptr< TestItem > item( new TestItem( &l ) );
QPointF result = item->adjustPointForReferencePosition( QPointF( 5, 7 ), QSizeF( 2, 4 ), QgsLayoutItem::UpperLeft );
QCOMPARE( result.x(), 5.0 );
QCOMPARE( result.y(), 7.0 );
result = item->adjustPointForReferencePosition( QPointF( 5, 7 ), QSizeF( 2, 4 ), QgsLayoutItem::UpperMiddle );
QCOMPARE( result.x(), 4.0 );
QCOMPARE( result.y(), 7.0 );
result = item->adjustPointForReferencePosition( QPointF( 5, 7 ), QSizeF( 2, 4 ), QgsLayoutItem::UpperRight );
QCOMPARE( result.x(), 3.0 );
QCOMPARE( result.y(), 7.0 );
result = item->adjustPointForReferencePosition( QPointF( 5, 7 ), QSizeF( 2, 4 ), QgsLayoutItem::MiddleLeft );
QCOMPARE( result.x(), 5.0 );
QCOMPARE( result.y(), 5.0 );
result = item->adjustPointForReferencePosition( QPointF( 5, 7 ), QSizeF( 2, 4 ), QgsLayoutItem::Middle );
QCOMPARE( result.x(), 4.0 );
QCOMPARE( result.y(), 5.0 );
result = item->adjustPointForReferencePosition( QPointF( 5, 7 ), QSizeF( 2, 4 ), QgsLayoutItem::MiddleRight );
QCOMPARE( result.x(), 3.0 );
QCOMPARE( result.y(), 5.0 );
result = item->adjustPointForReferencePosition( QPointF( 5, 7 ), QSizeF( 2, 4 ), QgsLayoutItem::LowerLeft );
QCOMPARE( result.x(), 5.0 );
QCOMPARE( result.y(), 3.0 );
result = item->adjustPointForReferencePosition( QPointF( 5, 7 ), QSizeF( 2, 4 ), QgsLayoutItem::LowerMiddle );
QCOMPARE( result.x(), 4.0 );
QCOMPARE( result.y(), 3.0 );
result = item->adjustPointForReferencePosition( QPointF( 5, 7 ), QSizeF( 2, 4 ), QgsLayoutItem::LowerRight );
QCOMPARE( result.x(), 3.0 );
QCOMPARE( result.y(), 3.0 );
}
void TestQgsLayoutItem::positionAtReferencePoint()
{
QgsProject p;
QgsLayout l( &p );
TestItem *item = new TestItem( &l );
item->setPos( 8.0, 6.0 );
item->setRect( 0.0, 0.0, 4.0, 6.0 );
QPointF result = item->positionAtReferencePoint( QgsLayoutItem::UpperLeft );
QCOMPARE( result.x(), 8.0 );
QCOMPARE( result.y(), 6.0 );
result = item->positionAtReferencePoint( QgsLayoutItem::UpperMiddle );
QCOMPARE( result.x(), 10.0 );
QCOMPARE( result.y(), 6.0 );
result = item->positionAtReferencePoint( QgsLayoutItem::UpperRight );
QCOMPARE( result.x(), 12.0 );
QCOMPARE( result.y(), 6.0 );
result = item->positionAtReferencePoint( QgsLayoutItem::MiddleLeft );
QCOMPARE( result.x(), 8.0 );
QCOMPARE( result.y(), 9.0 );
result = item->positionAtReferencePoint( QgsLayoutItem::Middle );
QCOMPARE( result.x(), 10.0 );
QCOMPARE( result.y(), 9.0 );
result = item->positionAtReferencePoint( QgsLayoutItem::MiddleRight );
QCOMPARE( result.x(), 12.0 );