/
qgscompositionconverter.cpp
1725 lines (1518 loc) · 86.3 KB
/
qgscompositionconverter.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
/***************************************************************************
qgscompositionconverter.cpp - QgsCompositionConverter
---------------------
begin : 13.12.2017
copyright : (C) 2017 by Alessandro Pasotti
email : elpaso at itopen dot it
***************************************************************************
* *
* 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 <QObject>
#include "qgscompositionconverter.h"
#include "qgsreadwritecontext.h"
#include "qgslayertree.h"
#include "qgslayoutmodel.h"
#include "qgslayoutitemgroup.h"
#include "qgslayoutobject.h"
#include "qgsfontutils.h"
#include "qgspainting.h"
#include "qgsproperty.h"
#include "qgssymbollayerutils.h"
#include "qgssymbollayer.h"
#include "qgsproject.h"
#include "qgsmaplayerstylemanager.h"
#include "qgsprintlayout.h"
#include "qgslayoutatlas.h"
#include "qgslayoutundostack.h"
#include "qgslayoutpagecollection.h"
#include "qgslayoutitemregistry.h"
#include "qgslayoutitemlabel.h"
#include "qgslayoutitemshape.h"
#include "qgslayoutitempicture.h"
#include "qgslayoutitempolygon.h"
#include "qgslayoutitempolyline.h"
#include "qgslayoutitemmap.h"
#include "qgslayoutitemmapgrid.h"
#include "qgslayoutitemscalebar.h"
#include "qgslayoutitemlegend.h"
#include "qgslayoutitemhtml.h"
#include "qgslayouttable.h"
#include "qgslayoutitemattributetable.h"
#include "qgslayouttablecolumn.h"
#include "qgslayoutmultiframe.h"
#include "qgslayoutframe.h"
#include "qgslayoutguidecollection.h"
QgsPropertiesDefinition QgsCompositionConverter::sPropertyDefinitions;
void QgsCompositionConverter::initPropertyDefinitions()
{
if ( !sPropertyDefinitions.isEmpty() )
return;
sPropertyDefinitions = QgsPropertiesDefinition
{
{ QgsCompositionConverter::TestProperty, QgsPropertyDefinition( "dataDefinedProperty", QgsPropertyDefinition::DataTypeString, "invalid property", QString() ) },
{
QgsCompositionConverter::PresetPaperSize, QgsPropertyDefinition( "dataDefinedPaperSize", QgsPropertyDefinition::DataTypeString, QObject::tr( "Paper size" ), QObject::tr( "string " ) + QStringLiteral( "[<b>A5</b>|<b>A4</b>|<b>A3</b>|<b>A2</b>|<b>A1</b>|<b>A0</b>"
"<b>B5</b>|<b>B4</b>|<b>B3</b>|<b>B2</b>|<b>B1</b>|<b>B0</b>"
"<b>Legal</b>|<b>Ansi A</b>|<b>Ansi B</b>|<b>Ansi C</b>|<b>Ansi D</b>|<b>Ansi E</b>"
"<b>Arch A</b>|<b>Arch B</b>|<b>Arch C</b>|<b>Arch D</b>|<b>Arch E</b>|<b>Arch E1</b>]"
) )
},
{ QgsCompositionConverter::PaperWidth, QgsPropertyDefinition( "dataDefinedPaperWidth", QObject::tr( "Page width" ), QgsPropertyDefinition::DoublePositive ) },
{ QgsCompositionConverter::PaperHeight, QgsPropertyDefinition( "dataDefinedPaperHeight", QObject::tr( "Page height" ), QgsPropertyDefinition::DoublePositive ) },
{ QgsCompositionConverter::NumPages, QgsPropertyDefinition( "dataDefinedNumPages", QObject::tr( "Number of pages" ), QgsPropertyDefinition::IntegerPositive ) },
{ QgsCompositionConverter::PaperOrientation, QgsPropertyDefinition( "dataDefinedPaperOrientation", QgsPropertyDefinition::DataTypeString, QObject::tr( "Symbol size" ), QObject::tr( "string " ) + QStringLiteral( "[<b>portrait</b>|<b>landscape</b>]" ) ) },
{ QgsCompositionConverter::PageNumber, QgsPropertyDefinition( "dataDefinedPageNumber", QObject::tr( "Page number" ), QgsPropertyDefinition::IntegerPositive ) },
{ QgsCompositionConverter::PositionX, QgsPropertyDefinition( "dataDefinedPositionX", QObject::tr( "Position (X)" ), QgsPropertyDefinition::Double ) },
{ QgsCompositionConverter::PositionY, QgsPropertyDefinition( "dataDefinedPositionY", QObject::tr( "Position (Y)" ), QgsPropertyDefinition::Double ) },
{ QgsCompositionConverter::ItemWidth, QgsPropertyDefinition( "dataDefinedWidth", QObject::tr( "Width" ), QgsPropertyDefinition::DoublePositive ) },
{ QgsCompositionConverter::ItemHeight, QgsPropertyDefinition( "dataDefinedHeight", QObject::tr( "Height" ), QgsPropertyDefinition::DoublePositive ) },
{ QgsCompositionConverter::ItemRotation, QgsPropertyDefinition( "dataDefinedRotation", QObject::tr( "Rotation angle" ), QgsPropertyDefinition::Rotation ) },
{ QgsCompositionConverter::Transparency, QgsPropertyDefinition( "dataDefinedTransparency", QObject::tr( "Transparency" ), QgsPropertyDefinition::Opacity ) },
{ QgsCompositionConverter::Opacity, QgsPropertyDefinition( "dataDefinedOpacity", QObject::tr( "Opacity" ), QgsPropertyDefinition::Opacity ) },
{ QgsCompositionConverter::BlendMode, QgsPropertyDefinition( "dataDefinedBlendMode", QObject::tr( "Blend mode" ), QgsPropertyDefinition::BlendMode ) },
{ QgsCompositionConverter::ExcludeFromExports, QgsPropertyDefinition( "dataDefinedExcludeExports", QObject::tr( "Exclude item from exports" ), QgsPropertyDefinition::Boolean ) },
{ QgsCompositionConverter::FrameColor, QgsPropertyDefinition( "dataDefinedFrameColor", QObject::tr( "Frame color" ), QgsPropertyDefinition::ColorWithAlpha ) },
{ QgsCompositionConverter::BackgroundColor, QgsPropertyDefinition( "dataDefinedBackgroundColor", QObject::tr( "Background color" ), QgsPropertyDefinition::ColorWithAlpha ) },
{ QgsCompositionConverter::MapRotation, QgsPropertyDefinition( "dataDefinedMapRotation", QObject::tr( "Map rotation" ), QgsPropertyDefinition::Rotation ) },
{ QgsCompositionConverter::MapScale, QgsPropertyDefinition( "dataDefinedMapScale", QObject::tr( "Map scale" ), QgsPropertyDefinition::DoublePositive ) },
{ QgsCompositionConverter::MapXMin, QgsPropertyDefinition( "dataDefinedMapXMin", QObject::tr( "Extent minimum X" ), QgsPropertyDefinition::Double ) },
{ QgsCompositionConverter::MapYMin, QgsPropertyDefinition( "dataDefinedMapYMin", QObject::tr( "Extent minimum Y" ), QgsPropertyDefinition::Double ) },
{ QgsCompositionConverter::MapXMax, QgsPropertyDefinition( "dataDefinedMapXMax", QObject::tr( "Extent maximum X" ), QgsPropertyDefinition::Double ) },
{ QgsCompositionConverter::MapYMax, QgsPropertyDefinition( "dataDefinedMapYMax", QObject::tr( "Extent maximum Y" ), QgsPropertyDefinition::Double ) },
{ QgsCompositionConverter::MapAtlasMargin, QgsPropertyDefinition( "dataDefinedMapAtlasMargin", QObject::tr( "Atlas margin" ), QgsPropertyDefinition::DoublePositive ) },
{ QgsCompositionConverter::MapLayers, QgsPropertyDefinition( "dataDefinedMapLayers", QgsPropertyDefinition::DataTypeString, QObject::tr( "Symbol size" ), QObject::tr( "list of map layer names separated by | characters" ) ) },
{ QgsCompositionConverter::MapStylePreset, QgsPropertyDefinition( "dataDefinedMapStylePreset", QgsPropertyDefinition::DataTypeString, QObject::tr( "Symbol size" ), QObject::tr( "list of map layer names separated by | characters" ) ) },
{ QgsCompositionConverter::PictureSource, QgsPropertyDefinition( "dataDefinedSource", QObject::tr( "Picture source (URL)" ), QgsPropertyDefinition::String ) },
{ QgsCompositionConverter::SourceUrl, QgsPropertyDefinition( "dataDefinedSourceUrl", QObject::tr( "Source URL" ), QgsPropertyDefinition::String ) },
{ QgsCompositionConverter::PictureSvgBackgroundColor, QgsPropertyDefinition( "dataDefinedSvgBackgroundColor", QObject::tr( "SVG background color" ), QgsPropertyDefinition::ColorWithAlpha ) },
{ QgsCompositionConverter::PictureSvgStrokeColor, QgsPropertyDefinition( "dataDefinedSvgStrokeColor", QObject::tr( "SVG stroke color" ), QgsPropertyDefinition::ColorWithAlpha ) },
{ QgsCompositionConverter::PictureSvgStrokeWidth, QgsPropertyDefinition( "dataDefinedSvgStrokeWidth", QObject::tr( "SVG stroke width" ), QgsPropertyDefinition::StrokeWidth ) },
{ QgsCompositionConverter::LegendTitle, QgsPropertyDefinition( "dataDefinedLegendTitle", QObject::tr( "Legend title" ), QgsPropertyDefinition::String ) },
{ QgsCompositionConverter::LegendColumnCount, QgsPropertyDefinition( "dataDefinedLegendColumns", QObject::tr( "Number of columns" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) },
{ QgsCompositionConverter::ScalebarFillColor, QgsPropertyDefinition( "dataDefinedScalebarFill", QObject::tr( "Fill color" ), QgsPropertyDefinition::ColorWithAlpha ) },
{ QgsCompositionConverter::ScalebarFillColor2, QgsPropertyDefinition( "dataDefinedScalebarFill2", QObject::tr( "Secondary fill color" ), QgsPropertyDefinition::ColorWithAlpha ) },
{ QgsCompositionConverter::ScalebarLineColor, QgsPropertyDefinition( "dataDefinedScalebarLineColor", QObject::tr( "Line color" ), QgsPropertyDefinition::ColorWithAlpha ) },
{ QgsCompositionConverter::ScalebarLineWidth, QgsPropertyDefinition( "dataDefinedScalebarLineWidth", QObject::tr( "Line width" ), QgsPropertyDefinition::StrokeWidth ) },
};
}
QgsPropertiesDefinition QgsCompositionConverter::propertyDefinitions()
{
QgsCompositionConverter::initPropertyDefinitions();
return sPropertyDefinitions;
}
std::unique_ptr< QgsPrintLayout > QgsCompositionConverter::createLayoutFromCompositionXml( const QDomElement &composerElement, QgsProject *project )
{
initPropertyDefinitions();
QDomElement parentElement = composerElement.parentNode().toElement();
std::unique_ptr< QgsPrintLayout > layout = qgis::make_unique< QgsPrintLayout >( project );
layout->undoStack()->blockCommands( true );
// Guides
layout->guides().setVisible( composerElement.attribute( QStringLiteral( "guidesVisible" ), QStringLiteral( "1" ) ).toInt() != 0 );
int printResolution = composerElement.attribute( "printResolution", "300" ).toInt();
layout->renderContext().setDpi( printResolution );
// Create pages
int pages = composerElement.attribute( QStringLiteral( "numPages" ) ).toInt( );
float paperHeight = composerElement.attribute( QStringLiteral( "paperHeight" ) ).toDouble( );
float paperWidth = composerElement.attribute( QStringLiteral( "paperWidth" ) ).toDouble( );
if ( composerElement.elementsByTagName( QStringLiteral( "symbol" ) ).size() )
{
QDomElement symbolElement = composerElement.elementsByTagName( QStringLiteral( "symbol" ) ).at( 0 ).toElement();
QgsReadWriteContext context;
if ( project )
context.setPathResolver( project->pathResolver() );
QgsFillSymbol *symbol = QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElement, context );
if ( symbol )
layout->pageCollection()->setPageStyleSymbol( symbol );
}
QString name = composerElement.attribute( QStringLiteral( "name" ) );
// Try title
if ( name.isEmpty() )
name = composerElement.attribute( QStringLiteral( "title" ) );
// Try title on parent element
if ( name.isEmpty() )
name = parentElement.attribute( QStringLiteral( "title" ) );
layout->setName( name );
QgsLayoutSize pageSize( paperWidth, paperHeight );
for ( int j = 0; j < pages; j++ )
{
QgsLayoutItemPage *page = QgsLayoutItemPage::create( layout.get() );
page->setPageSize( pageSize );
layout->pageCollection()->addPage( page );
//custom snap lines
QDomNodeList snapLineNodes = composerElement.elementsByTagName( QStringLiteral( "SnapLine" ) );
for ( int i = 0; i < snapLineNodes.size(); ++i )
{
QDomElement snapLineElem = snapLineNodes.at( i ).toElement();
double x1 = snapLineElem.attribute( QStringLiteral( "x1" ) ).toDouble();
double y1 = snapLineElem.attribute( QStringLiteral( "y1" ) ).toDouble();
double x2 = snapLineElem.attribute( QStringLiteral( "x2" ) ).toDouble();
// Not necessary: double y2 = snapLineElem.attribute( QStringLiteral( "y2" ) ).toDouble();
Qt::Orientation orientation( x1 == x2 ? Qt::Orientation::Vertical : Qt::Orientation::Horizontal );
QgsLayoutMeasurement position( x1 == x2 ? x1 : y1 );
std::unique_ptr< QgsLayoutGuide > guide = qgis::make_unique< QgsLayoutGuide >( orientation, position, page );
layout->guides().addGuide( guide.release() );
}
}
addItemsFromCompositionXml( layout.get(), composerElement );
// Read atlas from the parent element (Composer)
if ( parentElement.elementsByTagName( QStringLiteral( "Atlas" ) ).size() )
{
QDomElement atlasElement = parentElement.elementsByTagName( QStringLiteral( "Atlas" ) ).at( 0 ).toElement();
readAtlasXml( layout->atlas(), atlasElement, layout->project() );
}
layout->undoStack()->blockCommands( false );
return layout;
}
void QgsCompositionConverter::adjustPos( QgsPrintLayout *layout, QgsLayoutItem *layoutItem, QPointF *position, bool &pasteInPlace, int zOrderOffset, QPointF &pasteShiftPos, int &pageNumber )
{
if ( position )
{
if ( pasteInPlace )
{
layoutItem->attemptMove( QgsLayoutPoint( *position ), true, false, pageNumber );
}
else
{
layoutItem->attemptMoveBy( pasteShiftPos.x(), pasteShiftPos.y() );
}
}
layout->addLayoutItem( layoutItem );
layoutItem->setZValue( layoutItem->zValue() + zOrderOffset );
}
void QgsCompositionConverter::restoreGeneralComposeItemProperties( QgsLayoutItem *layoutItem, const QDomElement &itemElem )
{
//restore general composer item properties
QDomNodeList composerItemList = itemElem.elementsByTagName( QStringLiteral( "ComposerItem" ) );
if ( !composerItemList.isEmpty() )
{
QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
//rotation
if ( !qgsDoubleNear( composerItemElem.attribute( QStringLiteral( "rotation" ), QStringLiteral( "0" ) ).toDouble(), 0.0 ) )
{
//check for old (pre 2.1) rotation attribute
layoutItem->setItemRotation( composerItemElem.attribute( QStringLiteral( "rotation" ), QStringLiteral( "0" ) ).toDouble(), false );
}
QgsCompositionConverter::readXml( layoutItem, composerItemElem );
}
}
QRectF QgsCompositionConverter::itemPosition( QgsLayoutItem *layoutItem, const QDomElement &itemElem )
{
int page;
double x, y, pagex, pagey, width, height;
bool xOk, yOk, pageOk, pagexOk, pageyOk, widthOk, heightOk, positionModeOk;
x = itemElem.attribute( QStringLiteral( "x" ) ).toDouble( &xOk );
y = itemElem.attribute( QStringLiteral( "y" ) ).toDouble( &yOk );
page = itemElem.attribute( QStringLiteral( "page" ) ).toInt( &pageOk );
pagex = itemElem.attribute( QStringLiteral( "pagex" ) ).toDouble( &pagexOk );
pagey = itemElem.attribute( QStringLiteral( "pagey" ) ).toDouble( &pageyOk );
width = itemElem.attribute( QStringLiteral( "width" ) ).toDouble( &widthOk );
height = itemElem.attribute( QStringLiteral( "height" ) ).toDouble( &heightOk );
layoutItem->mReferencePoint = static_cast< QgsLayoutItem::ReferencePoint >( itemElem.attribute( QStringLiteral( "positionMode" ) ).toInt( &positionModeOk ) );
if ( !positionModeOk )
{
layoutItem->setReferencePoint( QgsLayoutItem::ReferencePoint::UpperLeft );
}
if ( pageOk && pagexOk && pageyOk )
{
xOk = true;
yOk = true;
x = pagex;
// position in the page (1-based)
if ( page <= layoutItem->layout()->pageCollection()->pageCount() )
{
QgsLayoutItemPage *pageObject = layoutItem->layout()->pageCollection()->pages().at( page - 1 );
y = ( page - 1 )
* ( pageObject->sizeWithUnits().height()
+ layoutItem->layout()->pageCollection()->spaceBetweenPages() )
+ pagey;
}
else
{
y = pagey;
}
}
return QRectF( x, y, width, height );
}
QPointF QgsCompositionConverter::minPointFromXml( const QDomElement &elem )
{
double minX = std::numeric_limits<double>::max();
double minY = std::numeric_limits<double>::max();
QDomNodeList composerItemList = elem.elementsByTagName( QStringLiteral( "ComposerItem" ) );
for ( int i = 0; i < composerItemList.size(); ++i )
{
QDomElement currentComposerItemElem = composerItemList.at( i ).toElement();
double x, y;
bool xOk, yOk;
x = currentComposerItemElem.attribute( QStringLiteral( "x" ) ).toDouble( &xOk );
y = currentComposerItemElem.attribute( QStringLiteral( "y" ) ).toDouble( &yOk );
if ( !xOk || !yOk )
{
continue;
}
minX = std::min( minX, x );
minY = std::min( minY, y );
}
if ( minX < std::numeric_limits<double>::max() )
{
return QPointF( minX, minY );
}
else
{
return QPointF( 0, 0 );
}
}
QList<QgsLayoutObject *> QgsCompositionConverter::addItemsFromCompositionXml( QgsPrintLayout *layout, const QDomElement &parentElement, QPointF *position, bool pasteInPlace )
{
initPropertyDefinitions();
QList< QgsLayoutObject * > newItems;
//if we are adding items to a layout which already contains items, we need to make sure
//these items are placed at the top of the layout and that zValues are not duplicated
//so, calculate an offset which needs to be added to the zValue of created items
int zOrderOffset = layout->mItemsModel->zOrderListSize();
QPointF pasteShiftPos;
int pageNumber = -1;
if ( position )
{
//If we are placing items relative to a certain point, then calculate how much we need
//to shift the items by so that they are placed at this point
//First, calculate the minimum position from the xml
QPointF minItemPos = minPointFromXml( parentElement );
//next, calculate how much each item needs to be shifted from its original position
//so that it's placed at the correct relative position
pasteShiftPos = *position - minItemPos;
if ( pasteInPlace )
{
pageNumber = layout->mPageCollection->pageNumberForPoint( *position );
}
}
QgsStringMap mapIdUiidMap;
// Map (this needs to come first to build the uuid <-> ID map for map composer items
for ( int i = 0; i < parentElement.elementsByTagName( QStringLiteral( "ComposerMap" ) ).size(); i++ )
{
QDomNode itemNode( parentElement.elementsByTagName( QStringLiteral( "ComposerMap" ) ).at( i ) );
QgsLayoutItemMap *layoutItem = new QgsLayoutItemMap( layout );
readMapXml( layoutItem, itemNode.toElement(), layout->project(), mapIdUiidMap );
adjustPos( layout, layoutItem, position, pasteInPlace, zOrderOffset, pasteShiftPos, pageNumber );
newItems << layoutItem ;
}
// Label
for ( int i = 0; i < parentElement.elementsByTagName( QStringLiteral( "ComposerLabel" ) ).size(); i++ )
{
QDomNode itemNode( parentElement.elementsByTagName( QStringLiteral( "ComposerLabel" ) ).at( i ) );
QgsLayoutItemLabel *layoutItem = new QgsLayoutItemLabel( layout );
readLabelXml( layoutItem, itemNode.toElement(), layout->project() );
adjustPos( layout, layoutItem, position, pasteInPlace, zOrderOffset, pasteShiftPos, pageNumber );
newItems << layoutItem ;
}
// Shape
for ( int i = 0; i < parentElement.elementsByTagName( QStringLiteral( "ComposerShape" ) ).size(); i++ )
{
QDomNode itemNode( parentElement.elementsByTagName( QStringLiteral( "ComposerShape" ) ).at( i ) );
QgsLayoutItemShape *layoutItem = new QgsLayoutItemShape( layout );
readShapeXml( layoutItem, itemNode.toElement(), layout->project() );
adjustPos( layout, layoutItem, position, pasteInPlace, zOrderOffset, pasteShiftPos, pageNumber );
newItems << layoutItem ;
}
// Picture
for ( int i = 0; i < parentElement.elementsByTagName( QStringLiteral( "ComposerPicture" ) ).size(); i++ )
{
QDomNode itemNode( parentElement.elementsByTagName( QStringLiteral( "ComposerPicture" ) ).at( i ) );
QgsLayoutItemPicture *layoutItem = new QgsLayoutItemPicture( layout );
readPictureXml( layoutItem, itemNode.toElement(), layout->project(), mapIdUiidMap );
adjustPos( layout, layoutItem, position, pasteInPlace, zOrderOffset, pasteShiftPos, pageNumber );
newItems << layoutItem ;
}
// Polygon
for ( int i = 0; i < parentElement.elementsByTagName( QStringLiteral( "ComposerPolygon" ) ).size(); i++ )
{
QDomNode itemNode( parentElement.elementsByTagName( QStringLiteral( "ComposerPolygon" ) ).at( i ) );
QgsLayoutItemPolygon *layoutItem = new QgsLayoutItemPolygon( layout );
readPolyXml<QgsLayoutItemPolygon, QgsFillSymbol>( layoutItem, itemNode.toElement(), layout->project() );
adjustPos( layout, layoutItem, position, pasteInPlace, zOrderOffset, pasteShiftPos, pageNumber );
newItems << layoutItem ;
}
// Polyline
for ( int i = 0; i < parentElement.elementsByTagName( QStringLiteral( "ComposerPolyline" ) ).size(); i++ )
{
QDomNode itemNode( parentElement.elementsByTagName( QStringLiteral( "ComposerPolyline" ) ).at( i ) );
QgsLayoutItemPolyline *layoutItem = new QgsLayoutItemPolyline( layout );
readPolyXml<QgsLayoutItemPolyline, QgsLineSymbol>( layoutItem, itemNode.toElement(), layout->project() );
adjustPos( layout, layoutItem, position, pasteInPlace, zOrderOffset, pasteShiftPos, pageNumber );
newItems << layoutItem ;
}
// Arrow
for ( int i = 0; i < parentElement.elementsByTagName( QStringLiteral( "ComposerArrow" ) ).size(); i++ )
{
QDomNode itemNode( parentElement.elementsByTagName( QStringLiteral( "ComposerArrow" ) ).at( i ) );
QgsLayoutItemPolyline *layoutItem = new QgsLayoutItemPolyline( layout );
readArrowXml( layoutItem, itemNode.toElement(), layout->project() );
adjustPos( layout, layoutItem, position, pasteInPlace, zOrderOffset, pasteShiftPos, pageNumber );
newItems << layoutItem ;
}
// Scalebar
for ( int i = 0; i < parentElement.elementsByTagName( QStringLiteral( "ComposerScaleBar" ) ).size(); i++ )
{
QDomNode itemNode( parentElement.elementsByTagName( QStringLiteral( "ComposerScaleBar" ) ).at( i ) );
QgsLayoutItemScaleBar *layoutItem = new QgsLayoutItemScaleBar( layout );
readScaleBarXml( layoutItem, itemNode.toElement(), layout->project(), mapIdUiidMap );
adjustPos( layout, layoutItem, position, pasteInPlace, zOrderOffset, pasteShiftPos, pageNumber );
newItems << layoutItem ;
}
// Legend
for ( int i = 0; i < parentElement.elementsByTagName( QStringLiteral( "ComposerLegend" ) ).size(); i++ )
{
QDomNode itemNode( parentElement.elementsByTagName( QStringLiteral( "ComposerLegend" ) ).at( i ) );
QgsLayoutItemLegend *layoutItem = new QgsLayoutItemLegend( layout );
readLegendXml( layoutItem, itemNode.toElement(), layout->project(), mapIdUiidMap );
adjustPos( layout, layoutItem, position, pasteInPlace, zOrderOffset, pasteShiftPos, pageNumber );
newItems << layoutItem ;
}
// Html
for ( int i = 0; i < parentElement.elementsByTagName( QStringLiteral( "ComposerHtml" ) ).size(); i++ )
{
QDomNode itemNode( parentElement.elementsByTagName( QStringLiteral( "ComposerHtml" ) ).at( i ) );
QgsLayoutItemHtml *layoutItem = new QgsLayoutItemHtml( layout );
readHtmlXml( layoutItem, itemNode.toElement(), layout->project() );
// Adjust position for frames
const QList<QgsLayoutFrame *> framesList( layoutItem->frames() );
for ( const auto &frame : framesList )
{
adjustPos( layout, frame, position, pasteInPlace, zOrderOffset, pasteShiftPos, pageNumber );
}
newItems << layoutItem ;
}
// Attribute Table
for ( int i = 0; i < parentElement.elementsByTagName( QStringLiteral( "ComposerAttributeTableV2" ) ).size(); i++ )
{
QDomNode itemNode( parentElement.elementsByTagName( QStringLiteral( "ComposerAttributeTableV2" ) ).at( i ) );
QgsLayoutItemAttributeTable *layoutItem = new QgsLayoutItemAttributeTable( layout );
readTableXml( layoutItem, itemNode.toElement(), layout->project() );
// Adjust position for frames
const QList<QgsLayoutFrame *> framesList( layoutItem->frames() );
for ( const auto &frame : framesList )
{
adjustPos( layout, frame, position, pasteInPlace, zOrderOffset, pasteShiftPos, pageNumber );
}
newItems << layoutItem ;
}
return newItems;
}
bool QgsCompositionConverter::isCompositionTemplate( const QDomDocument &document )
{
return document.elementsByTagName( QStringLiteral( "Composition" ) ).count() > 0;
}
QDomDocument QgsCompositionConverter::convertCompositionTemplate( const QDomDocument &document, QgsProject *project )
{
QDomDocument doc;
QgsReadWriteContext context;
if ( project )
context.setPathResolver( project->pathResolver() );
if ( document.elementsByTagName( QStringLiteral( "Composition" ) ).count( ) > 0 )
{
QDomElement composerElem = document.elementsByTagName( QStringLiteral( "Composition" ) ).at( 0 ).toElement( );
std::unique_ptr<QgsLayout> layout = createLayoutFromCompositionXml( composerElem,
project );
QDomElement elem = layout->writeXml( doc, context );
doc.appendChild( elem );
}
return doc;
}
bool QgsCompositionConverter::readLabelXml( QgsLayoutItemLabel *layoutItem, const QDomElement &itemElem, const QgsProject *project )
{
Q_UNUSED( project );
if ( itemElem.isNull() )
{
return false;
}
restoreGeneralComposeItemProperties( layoutItem, itemElem );
//text
layoutItem->setText( itemElem.attribute( QStringLiteral( "labelText" ) ) );
//html state
layoutItem->setMode( itemElem.attribute( QStringLiteral( "htmlState" ) ).toInt() == Qt::Checked ? QgsLayoutItemLabel::Mode::ModeHtml : QgsLayoutItemLabel::Mode::ModeFont );
//margin
bool marginXOk = false;
bool marginYOk = false;
double marginX = itemElem.attribute( QStringLiteral( "marginX" ) ).toDouble( &marginXOk );
double marginY = itemElem.attribute( QStringLiteral( "marginY" ) ).toDouble( &marginYOk );
if ( !marginXOk || !marginYOk )
{
//upgrade old projects where margins where stored in a single attribute
double margin = itemElem.attribute( QStringLiteral( "margin" ), QStringLiteral( "1.0" ) ).toDouble();
marginX = margin;
marginY = margin;
}
layoutItem->setMarginX( marginX );
layoutItem->setMarginY( marginY );
//Horizontal alignment
layoutItem->setHAlign( static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "halign" ) ).toInt() ) );
//Vertical alignment
layoutItem->setVAlign( static_cast< Qt::AlignmentFlag >( itemElem.attribute( QStringLiteral( "valign" ) ).toInt() ) );
QFont font;
//font
QgsFontUtils::setFromXmlChildNode( font, itemElem, QStringLiteral( "LabelFont" ) );
layoutItem->setFont( font );
//font color
QDomNodeList fontColorList = itemElem.elementsByTagName( QStringLiteral( "FontColor" ) );
if ( !fontColorList.isEmpty() )
{
QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
int red = fontColorElem.attribute( QStringLiteral( "red" ), QStringLiteral( "0" ) ).toInt();
int green = fontColorElem.attribute( QStringLiteral( "green" ), QStringLiteral( "0" ) ).toInt();
int blue = fontColorElem.attribute( QStringLiteral( "blue" ), QStringLiteral( "0" ) ).toInt();
layoutItem->setFontColor( QColor( red, green, blue ) );
}
else
{
layoutItem->setFontColor( QColor( 0, 0, 0 ) );
}
return true;
}
bool QgsCompositionConverter::readShapeXml( QgsLayoutItemShape *layoutItem, const QDomElement &itemElem, const QgsProject *project )
{
Q_UNUSED( project );
layoutItem->setShapeType( static_cast<QgsLayoutItemShape::Shape>( itemElem.attribute( QStringLiteral( "shapeType" ), QStringLiteral( "0" ) ).toInt() ) );
layoutItem->setCornerRadius( QgsLayoutMeasurement( itemElem.attribute( QStringLiteral( "cornerRadius" ), QStringLiteral( "0" ) ).toDouble() ) );
restoreGeneralComposeItemProperties( layoutItem, itemElem );
QgsReadWriteContext context;
if ( project )
context.setPathResolver( project->pathResolver() );
if ( itemElem.elementsByTagName( QStringLiteral( "symbol" ) ).size() )
{
QDomElement symbolElement = itemElem.elementsByTagName( QStringLiteral( "symbol" ) ).at( 0 ).toElement();
QgsFillSymbol *shapeStyleSymbol = QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElement, context );
if ( shapeStyleSymbol )
layoutItem->setSymbol( shapeStyleSymbol );
}
else
{
//upgrade project file from 2.0 to use symbol styling
QgsStringMap properties;
properties.insert( QStringLiteral( "color" ), QgsSymbolLayerUtils::encodeColor( layoutItem->brush().color() ) );
if ( layoutItem->hasBackground() )
{
properties.insert( QStringLiteral( "style" ), QStringLiteral( "solid" ) );
}
else
{
properties.insert( QStringLiteral( "style" ), QStringLiteral( "no" ) );
}
if ( layoutItem->frameEnabled() )
{
properties.insert( QStringLiteral( "style_border" ), QStringLiteral( "solid" ) );
}
else
{
properties.insert( QStringLiteral( "style_border" ), QStringLiteral( "no" ) );
}
properties.insert( QStringLiteral( "color_border" ), QgsSymbolLayerUtils::encodeColor( layoutItem->pen().color() ) );
properties.insert( QStringLiteral( "width_border" ), QString::number( layoutItem->pen().widthF() ) );
//for pre 2.0 projects, shape color and outline were specified in a different element...
QDomNodeList outlineColorList = itemElem.elementsByTagName( QStringLiteral( "OutlineColor" ) );
if ( !outlineColorList.isEmpty() )
{
QDomElement frameColorElem = outlineColorList.at( 0 ).toElement();
bool redOk, greenOk, blueOk, alphaOk, widthOk;
int penRed, penGreen, penBlue, penAlpha;
double penWidth;
penWidth = itemElem.attribute( QStringLiteral( "outlineWidth" ) ).toDouble( &widthOk );
penRed = frameColorElem.attribute( QStringLiteral( "red" ) ).toInt( &redOk );
penGreen = frameColorElem.attribute( QStringLiteral( "green" ) ).toInt( &greenOk );
penBlue = frameColorElem.attribute( QStringLiteral( "blue" ) ).toInt( &blueOk );
penAlpha = frameColorElem.attribute( QStringLiteral( "alpha" ) ).toInt( &alphaOk );
if ( redOk && greenOk && blueOk && alphaOk && widthOk )
{
properties.insert( QStringLiteral( "color_border" ), QgsSymbolLayerUtils::encodeColor( QColor( penRed, penGreen, penBlue, penAlpha ) ) );
properties.insert( QStringLiteral( "width_border" ), QString::number( penWidth ) );
}
}
QDomNodeList fillColorList = itemElem.elementsByTagName( QStringLiteral( "FillColor" ) );
if ( !fillColorList.isEmpty() )
{
QDomElement fillColorElem = fillColorList.at( 0 ).toElement();
bool redOk, greenOk, blueOk, alphaOk;
int fillRed, fillGreen, fillBlue, fillAlpha;
fillRed = fillColorElem.attribute( QStringLiteral( "red" ) ).toInt( &redOk );
fillGreen = fillColorElem.attribute( QStringLiteral( "green" ) ).toInt( &greenOk );
fillBlue = fillColorElem.attribute( QStringLiteral( "blue" ) ).toInt( &blueOk );
fillAlpha = fillColorElem.attribute( QStringLiteral( "alpha" ) ).toInt( &alphaOk );
if ( redOk && greenOk && blueOk && alphaOk )
{
properties.insert( QStringLiteral( "color" ), QgsSymbolLayerUtils::encodeColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) ) );
properties.insert( QStringLiteral( "style" ), QStringLiteral( "solid" ) );
}
}
if ( itemElem.hasAttribute( QStringLiteral( "transparentFill" ) ) )
{
//old style (pre 2.0) of specifying that shapes had no fill
bool hasOldTransparentFill = itemElem.attribute( QStringLiteral( "transparentFill" ), QStringLiteral( "0" ) ).toInt();
if ( hasOldTransparentFill )
{
properties.insert( QStringLiteral( "style" ), QStringLiteral( "no" ) );
}
}
layoutItem->setSymbol( QgsFillSymbol::createSimple( properties ) );
}
// Disable frame for shapes
layoutItem->setFrameEnabled( false );
layoutItem->setBackgroundEnabled( false );
return true;
}
bool QgsCompositionConverter::readPictureXml( QgsLayoutItemPicture *layoutItem, const QDomElement &itemElem, const QgsProject *project, const QgsStringMap &mapId2Uuid )
{
restoreGeneralComposeItemProperties( layoutItem, itemElem );
layoutItem->mResizeMode = QgsLayoutItemPicture::ResizeMode( itemElem.attribute( QStringLiteral( "resizeMode" ), QStringLiteral( "0" ) ).toInt() );
//when loading from xml, default to anchor point of middle to match pre 2.4 behavior
bool positionModeOk = false;
layoutItem->mReferencePoint = static_cast< QgsLayoutItem::ReferencePoint >( itemElem.attribute( QStringLiteral( "positionMode" ) ).toInt( &positionModeOk ) );
if ( !positionModeOk )
{
layoutItem->mReferencePoint = QgsLayoutItem::ReferencePoint::UpperLeft;
}
bool anchorPointOk = false;
layoutItem->setPictureAnchor( static_cast< QgsLayoutItem::ReferencePoint >( itemElem.attribute( QStringLiteral( "anchorPoint" ), QString::number( QgsLayoutItem::ReferencePoint::Middle ) ).toInt( &anchorPointOk ) ) );
if ( !anchorPointOk )
{
layoutItem->mPictureAnchor = QgsLayoutItem::ReferencePoint::UpperLeft;
}
layoutItem->mSvgFillColor = QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "svgFillColor" ), QgsSymbolLayerUtils::encodeColor( QColor( 255, 255, 255 ) ) ) );
layoutItem->mSvgStrokeColor = QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "svgBorderColor" ), QgsSymbolLayerUtils::encodeColor( QColor( 0, 0, 0 ) ) ) );
layoutItem->mSvgStrokeWidth = itemElem.attribute( QStringLiteral( "svgBorderWidth" ), QStringLiteral( "0.2" ) ).toDouble();
QString imagePath = itemElem.attribute( QStringLiteral( "file" ) );
if ( project )
{
// convert from relative path to absolute. For SVG we also need to consider system SVG paths
QgsPathResolver pathResolver = project->pathResolver();
if ( imagePath.endsWith( QLatin1String( ".svg" ), Qt::CaseInsensitive ) )
imagePath = QgsSymbolLayerUtils::svgSymbolNameToPath( imagePath, pathResolver );
else
imagePath = pathResolver.readPath( imagePath );
}
layoutItem->setPicturePath( imagePath );
layoutItem->mPictureHeight = itemElem.attribute( QStringLiteral( "pictureHeight" ), QStringLiteral( "10" ) ).toDouble();
layoutItem->mPictureWidth = itemElem.attribute( QStringLiteral( "pictureWidth" ), QStringLiteral( "10" ) ).toDouble();
//picture rotation
if ( !qgsDoubleNear( itemElem.attribute( QStringLiteral( "pictureRotation" ), QStringLiteral( "0" ) ).toDouble(), 0.0 ) )
{
layoutItem->mPictureRotation = itemElem.attribute( QStringLiteral( "pictureRotation" ), QStringLiteral( "0" ) ).toDouble();
}
//rotation map
layoutItem->mNorthMode = static_cast< QgsLayoutItemPicture::NorthMode >( itemElem.attribute( QStringLiteral( "northMode" ), QStringLiteral( "0" ) ).toInt() );
layoutItem->mNorthOffset = itemElem.attribute( QStringLiteral( "northOffset" ), QStringLiteral( "0" ) ).toDouble();
QString rotationMapId = itemElem.attribute( QStringLiteral( "mapId" ), QStringLiteral( "-1" ) );
if ( rotationMapId != QStringLiteral( "-1" ) )
{
// Find uuid for map with given id
QgsLayoutItemMap *mapInstance = qobject_cast<QgsLayoutItemMap *>( layoutItem->layout()->itemByUuid( mapId2Uuid[ rotationMapId ] ) );
if ( mapInstance )
{
layoutItem->setLinkedMap( mapInstance );
}
}
return true;
}
bool QgsCompositionConverter::readArrowXml( QgsLayoutItemPolyline *layoutItem, const QDomElement &itemElem, const QgsProject *project )
{
readPolyXml<QgsLayoutItemPolyline, QgsLineSymbol>( layoutItem, itemElem, project );
QPolygonF polygon;
QDomNodeList startPointList = itemElem.elementsByTagName( QStringLiteral( "StartPoint" ) );
if ( ! startPointList.isEmpty() )
{
QDomElement node = startPointList.at( 0 ).toElement();
polygon.append( QPointF( node.attribute( QStringLiteral( "x" ) ).toDouble( ), node.attribute( QStringLiteral( "y" ) ).toDouble() ) );
}
QDomNodeList stopPointList = itemElem.elementsByTagName( QStringLiteral( "StopPoint" ) );
if ( ! stopPointList.isEmpty() )
{
QDomElement node = stopPointList.at( 0 ).toElement();
polygon.append( QPointF( node.attribute( QStringLiteral( "x" ) ).toDouble( ), node.attribute( QStringLiteral( "y" ) ).toDouble() ) );
}
QgsCompositionConverter::MarkerMode markerMode = static_cast< QgsCompositionConverter::MarkerMode>( itemElem.attribute( QStringLiteral( "markerMode" ), QStringLiteral( "0" ) ).toInt( ) );
if ( markerMode == QgsCompositionConverter::MarkerMode::DefaultMarker )
{
layoutItem->setEndMarker( QgsLayoutItemPolyline::MarkerMode::ArrowHead );
layoutItem->setStartMarker( QgsLayoutItemPolyline::MarkerMode::NoMarker );
layoutItem->setArrowHeadFillColor( QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "arrowHeadFillColor" ), QgsSymbolLayerUtils::encodeColor( QColor( 255, 255, 255 ) ) ) ) );
layoutItem->setArrowHeadStrokeColor( QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "arrowHeadOutlineColor" ), QgsSymbolLayerUtils::encodeColor( QColor( 255, 255, 255 ) ) ) ) );
layoutItem->setArrowHeadStrokeWidth( itemElem.attribute( QStringLiteral( "outlineWidth" ), QStringLiteral( "1.0" ) ).toDouble( ) );
layoutItem->setArrowHeadWidth( itemElem.attribute( QStringLiteral( "arrowHeadWidth" ), QStringLiteral( "1.0" ) ).toDouble( ) );
}
else if ( markerMode == QgsCompositionConverter::MarkerMode::SVGMarker )
{
QString endMarkerFile = itemElem.attribute( QStringLiteral( "endMarkerFile" ) );
QString startMarkerFile = itemElem.attribute( QStringLiteral( "endMarkerFile" ) );
// Fix the paths
if ( project )
{
// convert from relative path to absolute. For SVG we also need to consider system SVG paths
QgsPathResolver pathResolver = project->pathResolver();
if ( !endMarkerFile.isEmpty() )
{
if ( endMarkerFile.endsWith( QLatin1String( ".svg" ), Qt::CaseInsensitive ) )
endMarkerFile = QgsSymbolLayerUtils::svgSymbolNameToPath( endMarkerFile, pathResolver );
else
endMarkerFile = pathResolver.readPath( endMarkerFile );
}
if ( !startMarkerFile.isEmpty() )
{
if ( startMarkerFile.endsWith( QLatin1String( ".svg" ), Qt::CaseInsensitive ) )
startMarkerFile = QgsSymbolLayerUtils::svgSymbolNameToPath( startMarkerFile, pathResolver );
else
startMarkerFile = pathResolver.readPath( startMarkerFile );
}
}
if ( !endMarkerFile.isEmpty() )
{
layoutItem->setEndMarker( QgsLayoutItemPolyline::MarkerMode::SvgMarker );
layoutItem->setEndSvgMarkerPath( endMarkerFile );
}
if ( !startMarkerFile.isEmpty() )
{
layoutItem->setStartMarker( QgsLayoutItemPolyline::MarkerMode::SvgMarker );
layoutItem->setStartSvgMarkerPath( startMarkerFile );
}
}
else // NoMarker
{
layoutItem->setEndMarker( QgsLayoutItemPolyline::MarkerMode::NoMarker );
layoutItem->setStartMarker( QgsLayoutItemPolyline::MarkerMode::NoMarker );
}
// Calculate the margin
double margin = polygon.boundingRect().left() - layoutItem->pos().x();
polygon.translate( - polygon.boundingRect().left() + margin, - polygon.boundingRect().top() + margin );
layoutItem->setNodes( polygon );
return true;
}
bool QgsCompositionConverter::readMapXml( QgsLayoutItemMap *layoutItem, const QDomElement &itemElem, const QgsProject *project, QgsStringMap &mapId2Uuid )
{
restoreGeneralComposeItemProperties( layoutItem, itemElem );
mapId2Uuid[ itemElem.attribute( QStringLiteral( "id" ) ) ] = layoutItem->uuid();
// TODO: Unused but all the layouts readXML require it (I'd suggest to remove it from the API)
QDomDocument doc;
QgsReadWriteContext context;
if ( project )
context.setPathResolver( project->pathResolver() );
//extent
QDomNodeList extentNodeList = itemElem.elementsByTagName( QStringLiteral( "Extent" ) );
if ( !extentNodeList.isEmpty() )
{
QDomElement extentElem = extentNodeList.at( 0 ).toElement();
double xmin, xmax, ymin, ymax;
xmin = extentElem.attribute( QStringLiteral( "xmin" ) ).toDouble();
xmax = extentElem.attribute( QStringLiteral( "xmax" ) ).toDouble();
ymin = extentElem.attribute( QStringLiteral( "ymin" ) ).toDouble();
ymax = extentElem.attribute( QStringLiteral( "ymax" ) ).toDouble();
layoutItem->setExtent( QgsRectangle( xmin, ymin, xmax, ymax ) );
}
QDomNodeList crsNodeList = itemElem.elementsByTagName( QStringLiteral( "crs" ) );
if ( !crsNodeList.isEmpty() )
{
QDomElement crsElem = crsNodeList.at( 0 ).toElement();
layoutItem->crs().readXml( crsElem );
}
else
{
layoutItem->setCrs( QgsCoordinateReferenceSystem() );
}
//map rotation
if ( !qgsDoubleNear( itemElem.attribute( QStringLiteral( "mapRotation" ), QStringLiteral( "0" ) ).toDouble(), 0.0 ) )
{
layoutItem->setMapRotation( itemElem.attribute( QStringLiteral( "mapRotation" ), QStringLiteral( "0" ) ).toDouble() );
}
// follow map theme
layoutItem->setFollowVisibilityPreset( itemElem.attribute( QStringLiteral( "followPreset" ) ).compare( QLatin1String( "true" ) ) == 0 );
layoutItem->setFollowVisibilityPresetName( itemElem.attribute( QStringLiteral( "followPresetName" ) ) );
//mKeepLayerSet flag
QString keepLayerSetFlag = itemElem.attribute( QStringLiteral( "keepLayerSet" ) );
if ( keepLayerSetFlag.compare( QLatin1String( "true" ), Qt::CaseInsensitive ) == 0 )
{
layoutItem->setKeepLayerSet( true );
}
else
{
layoutItem->setKeepLayerSet( false );
}
QString drawCanvasItemsFlag = itemElem.attribute( QStringLiteral( "drawCanvasItems" ), QStringLiteral( "true" ) );
if ( drawCanvasItemsFlag.compare( QLatin1String( "true" ), Qt::CaseInsensitive ) == 0 )
{
layoutItem->setDrawAnnotations( true );
}
else
{
layoutItem->setDrawAnnotations( false );
}
layoutItem->mLayerStyleOverrides.clear();
//mLayers
layoutItem->mLayers.clear();
QDomNodeList layerSetNodeList = itemElem.elementsByTagName( QStringLiteral( "LayerSet" ) );
if ( !layerSetNodeList.isEmpty() )
{
QDomElement layerSetElem = layerSetNodeList.at( 0 ).toElement();
QDomNodeList layerIdNodeList = layerSetElem.elementsByTagName( QStringLiteral( "Layer" ) );
layoutItem->mLayers.reserve( layerIdNodeList.size() );
for ( int i = 0; i < layerIdNodeList.size(); ++i )
{
QDomElement layerElem = layerIdNodeList.at( i ).toElement();
QString layerId = layerElem.text();
QString layerName = layerElem.attribute( QStringLiteral( "name" ) );
QString layerSource = layerElem.attribute( QStringLiteral( "source" ) );
QString layerProvider = layerElem.attribute( QStringLiteral( "provider" ) );
QgsMapLayerRef ref( layerId, layerName, layerSource, layerProvider );
ref.resolveWeakly( project );
layoutItem->mLayers << ref;
}
}
// override styles
QDomNodeList layerStylesNodeList = itemElem.elementsByTagName( QStringLiteral( "LayerStyles" ) );
layoutItem->mKeepLayerStyles = !layerStylesNodeList.isEmpty();
if ( layoutItem->mKeepLayerStyles )
{
QDomElement layerStylesElem = layerStylesNodeList.at( 0 ).toElement();
QDomNodeList layerStyleNodeList = layerStylesElem.elementsByTagName( QStringLiteral( "LayerStyle" ) );
for ( int i = 0; i < layerStyleNodeList.size(); ++i )
{
const QDomElement &layerStyleElement = layerStyleNodeList.at( i ).toElement();
QString layerId = layerStyleElement.attribute( QStringLiteral( "layerid" ) );
QString layerName = layerStyleElement.attribute( QStringLiteral( "name" ) );
QString layerSource = layerStyleElement.attribute( QStringLiteral( "source" ) );
QString layerProvider = layerStyleElement.attribute( QStringLiteral( "provider" ) );
QgsMapLayerRef ref( layerId, layerName, layerSource, layerProvider );
ref.resolveWeakly( project );
QgsMapLayerStyle style;
style.readXml( layerStyleElement );
layoutItem->mLayerStyleOverrides.insert( ref.layerId, style.xmlData() );
}
}
layoutItem->mDrawing = false;
layoutItem->mNumCachedLayers = 0;
layoutItem->mCacheInvalidated = true;
//overviews
//read overview stack
QDomNodeList mapOverviewNodeList = itemElem.elementsByTagName( QStringLiteral( "ComposerMapOverview" ) );
for ( int i = 0; i < mapOverviewNodeList.size(); ++i )
{
QDomElement mapOverviewElem = mapOverviewNodeList.at( i ).toElement();
std::unique_ptr<QgsLayoutItemMapOverview> mapOverview( new QgsLayoutItemMapOverview( mapOverviewElem.attribute( QStringLiteral( "name" ) ), layoutItem ) );
mapOverview->readXml( mapOverviewElem, doc, context );
QString frameMapId = mapOverviewElem.attribute( QStringLiteral( "frameMap" ), QStringLiteral( "-1" ) );
if ( frameMapId != QStringLiteral( "-1" ) && mapId2Uuid.contains( frameMapId ) )
{
QgsLayoutItemMap *mapInstance = qobject_cast<QgsLayoutItemMap *>( layoutItem->layout()->itemByUuid( mapId2Uuid[ frameMapId ] ) );
if ( mapInstance )
{
mapOverview->setLinkedMap( mapInstance );
}
layoutItem->mOverviewStack->addOverview( mapOverview.release() );
}
}
//grids
layoutItem->mGridStack->readXml( itemElem, doc, context );
//load grid / grid annotation in old xml format
//only do this if the grid stack didn't load any grids, otherwise this will
//be the dummy element created by QGIS >= 2.5 (refs #10905)
QDomNodeList gridNodeList = itemElem.elementsByTagName( QStringLiteral( "Grid" ) );
if ( layoutItem->mGridStack->size() == 0 && !gridNodeList.isEmpty() )
{
QDomElement gridElem = gridNodeList.at( 0 ).toElement();
QgsLayoutItemMapGrid *mapGrid = new QgsLayoutItemMapGrid( QObject::tr( "Grid %1" ).arg( 1 ), layoutItem );
mapGrid->setEnabled( gridElem.attribute( QStringLiteral( "show" ), QStringLiteral( "0" ) ) != QLatin1String( "0" ) );
mapGrid->setStyle( QgsLayoutItemMapGrid::GridStyle( gridElem.attribute( QStringLiteral( "gridStyle" ), QStringLiteral( "0" ) ).toInt() ) );
mapGrid->setIntervalX( gridElem.attribute( QStringLiteral( "intervalX" ), QStringLiteral( "0" ) ).toDouble() );
mapGrid->setIntervalY( gridElem.attribute( QStringLiteral( "intervalY" ), QStringLiteral( "0" ) ).toDouble() );
mapGrid->setOffsetX( gridElem.attribute( QStringLiteral( "offsetX" ), QStringLiteral( "0" ) ).toDouble() );
mapGrid->setOffsetY( gridElem.attribute( QStringLiteral( "offsetY" ), QStringLiteral( "0" ) ).toDouble() );
mapGrid->setCrossLength( gridElem.attribute( QStringLiteral( "crossLength" ), QStringLiteral( "3" ) ).toDouble() );
mapGrid->setFrameStyle( static_cast< QgsLayoutItemMapGrid::FrameStyle >( gridElem.attribute( QStringLiteral( "gridFrameStyle" ), QStringLiteral( "0" ) ).toInt() ) );
mapGrid->setFrameWidth( gridElem.attribute( QStringLiteral( "gridFrameWidth" ), QStringLiteral( "2.0" ) ).toDouble() );
mapGrid->setFramePenSize( gridElem.attribute( QStringLiteral( "gridFramePenThickness" ), QStringLiteral( "0.5" ) ).toDouble() );
mapGrid->setFramePenColor( QgsSymbolLayerUtils::decodeColor( gridElem.attribute( QStringLiteral( "framePenColor" ), QStringLiteral( "0,0,0" ) ) ) );
mapGrid->setFrameFillColor1( QgsSymbolLayerUtils::decodeColor( gridElem.attribute( QStringLiteral( "frameFillColor1" ), QStringLiteral( "255,255,255,255" ) ) ) );
mapGrid->setFrameFillColor2( QgsSymbolLayerUtils::decodeColor( gridElem.attribute( QStringLiteral( "frameFillColor2" ), QStringLiteral( "0,0,0,255" ) ) ) );
mapGrid->setBlendMode( QgsPainting::getCompositionMode( static_cast< QgsPainting::BlendMode >( itemElem.attribute( QStringLiteral( "gridBlendMode" ), QStringLiteral( "0" ) ).toUInt() ) ) );
QDomElement gridSymbolElem = gridElem.firstChildElement( QStringLiteral( "symbol" ) );
QgsLineSymbol *lineSymbol = nullptr;
if ( gridSymbolElem.isNull() )
{
//old project file, read penWidth /penColorRed, penColorGreen, penColorBlue
lineSymbol = QgsLineSymbol::createSimple( QgsStringMap() );
lineSymbol->setWidth( gridElem.attribute( QStringLiteral( "penWidth" ), QStringLiteral( "0" ) ).toDouble() );
lineSymbol->setColor( QColor( gridElem.attribute( QStringLiteral( "penColorRed" ), QStringLiteral( "0" ) ).toInt(),
gridElem.attribute( QStringLiteral( "penColorGreen" ), QStringLiteral( "0" ) ).toInt(),
gridElem.attribute( QStringLiteral( "penColorBlue" ), QStringLiteral( "0" ) ).toInt() ) );
}
else
{
lineSymbol = QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( gridSymbolElem, context );
}
mapGrid->setLineSymbol( lineSymbol );
//annotation
QDomNodeList annotationNodeList = gridElem.elementsByTagName( QStringLiteral( "Annotation" ) );
if ( !annotationNodeList.isEmpty() )
{
QDomElement annotationElem = annotationNodeList.at( 0 ).toElement();
mapGrid->setAnnotationEnabled( annotationElem.attribute( QStringLiteral( "show" ), QStringLiteral( "0" ) ) != QLatin1String( "0" ) );
mapGrid->setAnnotationFormat( QgsLayoutItemMapGrid::AnnotationFormat( annotationElem.attribute( QStringLiteral( "format" ), QStringLiteral( "0" ) ).toInt() ) );
mapGrid->setAnnotationPosition( QgsLayoutItemMapGrid::AnnotationPosition( annotationElem.attribute( QStringLiteral( "leftPosition" ), QStringLiteral( "0" ) ).toInt() ), QgsLayoutItemMapGrid::Left );
mapGrid->setAnnotationPosition( QgsLayoutItemMapGrid::AnnotationPosition( annotationElem.attribute( QStringLiteral( "rightPosition" ), QStringLiteral( "0" ) ).toInt() ), QgsLayoutItemMapGrid::Right );
mapGrid->setAnnotationPosition( QgsLayoutItemMapGrid::AnnotationPosition( annotationElem.attribute( QStringLiteral( "topPosition" ), QStringLiteral( "0" ) ).toInt() ), QgsLayoutItemMapGrid::Top );
mapGrid->setAnnotationPosition( QgsLayoutItemMapGrid::AnnotationPosition( annotationElem.attribute( QStringLiteral( "bottomPosition" ), QStringLiteral( "0" ) ).toInt() ), QgsLayoutItemMapGrid::Bottom );
mapGrid->setAnnotationDirection( QgsLayoutItemMapGrid::AnnotationDirection( annotationElem.attribute( QStringLiteral( "leftDirection" ), QStringLiteral( "0" ) ).toInt() ), QgsLayoutItemMapGrid::Left );
mapGrid->setAnnotationDirection( QgsLayoutItemMapGrid::AnnotationDirection( annotationElem.attribute( QStringLiteral( "rightDirection" ), QStringLiteral( "0" ) ).toInt() ), QgsLayoutItemMapGrid::Right );
mapGrid->setAnnotationDirection( QgsLayoutItemMapGrid::AnnotationDirection( annotationElem.attribute( QStringLiteral( "topDirection" ), QStringLiteral( "0" ) ).toInt() ), QgsLayoutItemMapGrid::Top );
mapGrid->setAnnotationDirection( QgsLayoutItemMapGrid::AnnotationDirection( annotationElem.attribute( QStringLiteral( "bottomDirection" ), QStringLiteral( "0" ) ).toInt() ), QgsLayoutItemMapGrid::Bottom );
mapGrid->setAnnotationFrameDistance( annotationElem.attribute( QStringLiteral( "frameDistance" ), QStringLiteral( "0" ) ).toDouble() );
QFont annotationFont;
annotationFont.fromString( annotationElem.attribute( QStringLiteral( "font" ), QLatin1String( "" ) ) );
mapGrid->setAnnotationFont( annotationFont );
mapGrid->setAnnotationFontColor( QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "fontColor" ), QStringLiteral( "0,0,0,255" ) ) ) );
mapGrid->setAnnotationPrecision( annotationElem.attribute( QStringLiteral( "precision" ), QStringLiteral( "3" ) ).toInt() );
}
layoutItem->mGridStack->addGrid( mapGrid );
}
//atlas
QDomNodeList atlasNodeList = itemElem.elementsByTagName( QStringLiteral( "AtlasMap" ) );
if ( !atlasNodeList.isEmpty() )
{
QDomElement atlasElem = atlasNodeList.at( 0 ).toElement();
layoutItem->mAtlasDriven = ( atlasElem.attribute( QStringLiteral( "atlasDriven" ), QStringLiteral( "0" ) ) != QLatin1String( "0" ) );
if ( atlasElem.hasAttribute( QStringLiteral( "fixedScale" ) ) ) // deprecated XML
{
layoutItem->setAtlasScalingMode( atlasElem.attribute( QStringLiteral( "fixedScale" ), QStringLiteral( "0" ) ) != QLatin1String( "0" ) ? QgsLayoutItemMap::AtlasScalingMode::Fixed : QgsLayoutItemMap::AtlasScalingMode::Auto );
}
else if ( atlasElem.hasAttribute( QStringLiteral( "scalingMode" ) ) )
{
layoutItem->setAtlasScalingMode( static_cast<QgsLayoutItemMap::AtlasScalingMode>( atlasElem.attribute( QStringLiteral( "scalingMode" ) ).toInt() ) );
}
layoutItem->setAtlasMargin( atlasElem.attribute( QStringLiteral( "margin" ), QStringLiteral( "0.1" ) ).toDouble() );