Skip to content

Commit 680b9f8

Browse files
committed
[layout] Convert scalebar and legent from 2.x
1 parent cd6e820 commit 680b9f8

File tree

19 files changed

+559
-10
lines changed

19 files changed

+559
-10
lines changed

src/core/composer/qgscomposerlegend.h

+1
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
350350

351351
//! Will be true if the legend should be resized automatically to fit contents
352352
bool mSizeToContents = true;
353+
353354
};
354355

355356
#endif

src/core/layout/qgscompositionconverter.cpp

+295-9
Large diffs are not rendered by default.

src/core/layout/qgscompositionconverter.h

+10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class QgsLayoutItemPicture;
3535
class QgsLayoutItemPolygon;
3636
class QgsLayoutItemPolyline;
3737
class QgsLayoutItemMap;
38+
class QgsLayoutItemScaleBar;
39+
class QgsLayoutItemLegend;
3840

3941
class CORE_EXPORT QgsCompositionConverter
4042
{
@@ -153,6 +155,14 @@ class CORE_EXPORT QgsCompositionConverter
153155
const QDomElement &itemElem,
154156
const QgsProject *project );
155157

158+
static bool readScaleBarXml( QgsLayoutItemScaleBar *layoutItem,
159+
const QDomElement &itemElem,
160+
const QgsProject *project );
161+
162+
static bool readLegendXml( QgsLayoutItemLegend *layoutItem,
163+
const QDomElement &itemElem,
164+
const QgsProject *project );
165+
156166

157167
/**
158168
* Sets item state from DOM element

src/core/layout/qgslayoutitemlegend.h

+3
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,9 @@ class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
505505

506506
//! Will be true if the legend should be resized automatically to fit contents
507507
bool mSizeToContents = true;
508+
509+
friend class QgsCompositionConverter;
510+
508511
};
509512

510513
#endif // QGSLAYOUTITEMLEGEND_H

src/core/layout/qgslayoutitemscalebar.h

+2
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@ class CORE_EXPORT QgsLayoutItemScaleBar: public QgsLayoutItem
456456

457457
QgsScaleBarRenderer::ScaleBarContext createScaleContext() const;
458458

459+
friend class QgsCompositionConverter;
460+
459461
};
460462

461463
#endif //QGSLAYOUTITEMSCALEBAR_H

tests/src/core/testqgscompositionconverter.cpp

+58-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "qgslayoutitempolygon.h"
3434
#include "qgslayoutitempolyline.h"
3535
#include "qgslayoutitemmap.h"
36+
#include "qgslayoutitemscalebar.h"
37+
#include "qgslayoutitemlegend.h"
3638

3739

3840
class TestQgsCompositionConverter: public QObject
@@ -85,6 +87,17 @@ class TestQgsCompositionConverter: public QObject
8587
*/
8688
void importComposerTemplateMap();
8789

90+
/**
91+
* Test import legend from a composer template
92+
*/
93+
void importComposerTemplateLegend();
94+
95+
/**
96+
* Test import scalebar from a composer template
97+
*/
98+
void importComposerTemplateScaleBar();
99+
100+
88101
private:
89102

90103

@@ -324,6 +337,50 @@ void TestQgsCompositionConverter::importComposerTemplateMap()
324337

325338
}
326339

340+
void TestQgsCompositionConverter::importComposerTemplateLegend()
341+
{
342+
QDomElement docElem( loadComposition( "2x_template_legend.qpt" ) );
343+
QVERIFY( !docElem.isNull() );
344+
QgsProject project;
345+
QgsLayout *layout = QgsCompositionConverter::createLayoutFromCompositionXml( docElem, &project );
346+
QVERIFY( layout );
347+
QCOMPARE( layout->pageCollection()->pageCount(), 1 );
348+
349+
QList<QgsLayoutItemLegend *> items;
350+
layout->layoutItems<QgsLayoutItemLegend>( items );
351+
QCOMPARE( items.size(), 1 );
352+
353+
QgsLayoutItemLegend *item = items.at( 0 );
354+
QVERIFY( item->isVisible() );
355+
356+
checkRenderedImage( layout, QTest::currentTestFunction(), 0 );
357+
358+
qDeleteAll( items );
359+
360+
}
361+
362+
void TestQgsCompositionConverter::importComposerTemplateScaleBar()
363+
{
364+
QDomElement docElem( loadComposition( "2x_template_scalebar.qpt" ) );
365+
QVERIFY( !docElem.isNull() );
366+
QgsProject project;
367+
QgsLayout *layout = QgsCompositionConverter::createLayoutFromCompositionXml( docElem, &project );
368+
QVERIFY( layout );
369+
QCOMPARE( layout->pageCollection()->pageCount(), 1 );
370+
371+
QList<QgsLayoutItemScaleBar *> items;
372+
layout->layoutItems<QgsLayoutItemScaleBar>( items );
373+
QCOMPARE( items.size(), 1 );
374+
375+
QgsLayoutItemScaleBar *item = items.at( 0 );
376+
QVERIFY( item->isVisible() );
377+
378+
checkRenderedImage( layout, QTest::currentTestFunction(), 0 );
379+
380+
qDeleteAll( items );
381+
382+
}
383+
327384
void TestQgsCompositionConverter::importComposerTemplate()
328385
{
329386
QDomElement docElem( loadComposition( "2x_template.qpt" ) );
@@ -345,7 +402,7 @@ void TestQgsCompositionConverter::checkRenderedImage( QgsLayout *layout, const Q
345402
QSize size( layout->pageCollection()->page( pageNumber )->sizeWithUnits().width() * 3.77, layout->pageCollection()->page( pageNumber )->sizeWithUnits().height() * 3.77 );
346403
checker.setSize( size );
347404
checker.setControlPathPrefix( QStringLiteral( "compositionconverter" ) );
348-
QVERIFY( checker.testLayout( mReport, pageNumber ) );
405+
QVERIFY( checker.testLayout( mReport, pageNumber, 0, true ) );
349406
}
350407

351408
void TestQgsCompositionConverter::exportLayout( QgsLayout *layout, const QString testName )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<Composer>
2+
<Composition snapGridOffsetY="0" numPages="1" name="composer title" printAsRaster="0" snapGridOffsetX="0" snapGridResolution="10" alignmentSnap="1" resizeToContentsMarginTop="0" paperHeight="210" paperWidth="297" generateWorldFile="0" worldFileMap="" resizeToContentsMarginRight="0" smartGuides="1" resizeToContentsMarginBottom="0" snapTolerancePixels="5" guidesVisible="1" resizeToContentsMarginLeft="0" showPages="1" printResolution="305" snapping="0" gridVisible="0">
3+
<symbol type="fill" clip_to_extent="1" name="" alpha="1">
4+
<layer pass="0" enabled="1" locked="0" class="SimpleFill">
5+
<prop v="3x:0,0,0,0,0,0" k="border_width_map_unit_scale"/>
6+
<prop v="241,244,199,255" k="color"/>
7+
<prop v="bevel" k="joinstyle"/>
8+
<prop v="0,0" k="offset"/>
9+
<prop v="3x:0,0,0,0,0,0" k="offset_map_unit_scale"/>
10+
<prop v="MM" k="offset_unit"/>
11+
<prop v="175,179,138,255" k="outline_color"/>
12+
<prop v="solid" k="outline_style"/>
13+
<prop v="0.26" k="outline_width"/>
14+
<prop v="MM" k="outline_width_unit"/>
15+
<prop v="solid" k="style"/>
16+
<data_defined_properties>
17+
<Option type="Map">
18+
<Option type="QString" value="" name="name"/>
19+
<Option name="properties"/>
20+
<Option type="QString" value="collection" name="type"/>
21+
</Option>
22+
</data_defined_properties>
23+
</layer>
24+
</symbol>
25+
<ComposerLegend rasterBorderWidth="0" rasterBorder="1" title="My Legend" resizeToContents="1" symbolWidth="12" map="0" columnSpace="1" wmsLegendHeight="25" splitLayer="1" rasterBorderColor="0,0,0,255" columnCount="2" titleAlignment="4" fontColor="#f065ec" wmsLegendWidth="50" symbolHeight="9" boxSpace="5" wrapChar=" " legendFilterByAtlas="0" lineSpacing="1" equalColumnWidth="1">
26+
<styles>
27+
<style name="title" marginBottom="6">
28+
<styleFont description="MS Shell Dlg 2,36,-1,5,50,0,0,0,0,0" style=""/>
29+
</style>
30+
<style name="group" marginTop="5">
31+
<styleFont description="MS Shell Dlg 2,14,-1,5,50,0,0,0,0,0" style=""/>
32+
</style>
33+
<style name="subgroup" marginTop="4">
34+
<styleFont description="MS Shell Dlg 2,12,-1,5,50,0,0,0,0,0" style=""/>
35+
</style>
36+
<style name="symbol" marginTop="3">
37+
<styleFont description="MS Shell Dlg 2,8.25,-1,5,50,0,0,0,0,0" style=""/>
38+
</style>
39+
<style marginLeft="4" name="symbolLabel" marginTop="3">
40+
<styleFont description="MS Shell Dlg 2,12,-1,5,50,0,0,0,0,0" style=""/>
41+
</style>
42+
</styles>
43+
<layer-tree-group>
44+
<customproperties/>
45+
<layer-tree-layer providerKey="ogr" name="points" expanded="1" checked="Qt::Checked" id="points20171212162310546" source="../../../dev/QGIS/tests/testdata/points.shp">
46+
<customproperties/>
47+
</layer-tree-layer>
48+
<layer-tree-layer providerKey="ogr" name="polys" expanded="1" checked="Qt::Checked" id="polys20171212162309844" source="../../../dev/QGIS/tests/testdata/polys.shp">
49+
<customproperties/>
50+
</layer-tree-layer>
51+
<custom-order enabled="0"/>
52+
</layer-tree-group>
53+
<ComposerItem y="79.6503" excludeFromExports="0" frame="false" background="true" uuid="{69fb50e1-e233-4105-a97f-f95d3fa822cc}" lastValidViewScaleFactor="-1" zValue="6" frameJoinStyle="miter" id="" positionLock="false" width="70.6219" positionMode="0" itemRotation="0" page="1" outlineWidth="0.3" blendMode="0" pagey="79.6503" x="139.64" pagex="139.64" visibility="1" opacity="1" height="89.5">
54+
<FrameColor blue="0" green="0" red="0" alpha="255"/>
55+
<BackgroundColor blue="255" green="255" red="255" alpha="255"/>
56+
<dataDefinedProperties>
57+
<Option type="Map">
58+
<Option type="QString" value="" name="name"/>
59+
<Option name="properties"/>
60+
<Option type="QString" value="collection" name="type"/>
61+
</Option>
62+
</dataDefinedProperties>
63+
<customproperties/>
64+
</ComposerItem>
65+
</ComposerLegend>
66+
<dataDefinedProperties>
67+
<Option type="Map">
68+
<Option type="QString" value="" name="name"/>
69+
<Option type="Map" name="properties">
70+
<Option type="Map" name="dataDefinedHeight">
71+
<Option type="bool" value="true" name="active"/>
72+
<Option type="QString" value="28" name="expression"/>
73+
<Option type="int" value="3" name="type"/>
74+
</Option>
75+
<Option type="Map" name="dataDefinedMapScale">
76+
<Option type="bool" value="true" name="active"/>
77+
<Option type="QString" value="24126145*1.1" name="expression"/>
78+
<Option type="int" value="3" name="type"/>
79+
</Option>
80+
<Option type="Map" name="dataDefinedPositionX">
81+
<Option type="bool" value="true" name="active"/>
82+
<Option type="QString" value="200" name="expression"/>
83+
<Option type="int" value="3" name="type"/>
84+
</Option>
85+
<Option type="Map" name="dataDefinedPositionY">
86+
<Option type="bool" value="true" name="active"/>
87+
<Option type="QString" value="70" name="expression"/>
88+
<Option type="int" value="3" name="type"/>
89+
</Option>
90+
<Option type="Map" name="dataDefinedSource">
91+
<Option type="bool" value="false" name="active"/>
92+
<Option type="QString" value="" name="expression"/>
93+
<Option type="int" value="3" name="type"/>
94+
</Option>
95+
<Option type="Map" name="dataDefinedWidth">
96+
<Option type="bool" value="true" name="active"/>
97+
<Option type="QString" value="24" name="expression"/>
98+
<Option type="int" value="3" name="type"/>
99+
</Option>
100+
</Option>
101+
<Option type="QString" value="collection" name="type"/>
102+
</Option>
103+
</dataDefinedProperties>
104+
<customproperties/>
105+
</Composition>
106+
</Composer>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<Composer>
2+
<Composition snapGridOffsetY="0" numPages="1" name="composer title" printAsRaster="0" snapGridOffsetX="0" snapGridResolution="10" alignmentSnap="1" resizeToContentsMarginTop="0" paperHeight="210" paperWidth="297" generateWorldFile="0" worldFileMap="" resizeToContentsMarginRight="0" smartGuides="1" resizeToContentsMarginBottom="0" snapTolerancePixels="5" guidesVisible="1" resizeToContentsMarginLeft="0" showPages="1" printResolution="305" snapping="0" gridVisible="0">
3+
<symbol type="fill" clip_to_extent="1" name="" alpha="1">
4+
<layer pass="0" enabled="1" locked="0" class="SimpleFill">
5+
<prop v="3x:0,0,0,0,0,0" k="border_width_map_unit_scale"/>
6+
<prop v="241,244,199,255" k="color"/>
7+
<prop v="bevel" k="joinstyle"/>
8+
<prop v="0,0" k="offset"/>
9+
<prop v="3x:0,0,0,0,0,0" k="offset_map_unit_scale"/>
10+
<prop v="MM" k="offset_unit"/>
11+
<prop v="175,179,138,255" k="outline_color"/>
12+
<prop v="solid" k="outline_style"/>
13+
<prop v="0.26" k="outline_width"/>
14+
<prop v="MM" k="outline_width_unit"/>
15+
<prop v="solid" k="style"/>
16+
<data_defined_properties>
17+
<Option type="Map">
18+
<Option type="QString" value="" name="name"/>
19+
<Option name="properties"/>
20+
<Option type="QString" value="collection" name="type"/>
21+
</Option>
22+
</data_defined_properties>
23+
</layer>
24+
</symbol>
25+
<ComposerScaleBar numUnitsPerSegment="250000" labelBarSpace="5" boxContentSpace="4" mapId="0" maxBarWidth="150" numSegments="6" segmentSizeMode="0" numSegmentsLeft="3" unitType="meters" lineJoinStyle="round" style="Double Box" alignment="1" numMapUnitsPerScaleBarUnit="2000" outlineWidth="0.4" minBarWidth="50" unitLabel="hh" lineCapStyle="square" height="3" segmentMillimeters="5.64513">
26+
<scaleBarFont description="MS Shell Dlg 2,12,-1,5,50,0,0,0,0,0" style=""/>
27+
<fillColor blue="28" green="26" red="227" alpha="255"/>
28+
<fillColor2 blue="111" green="191" red="253" alpha="255"/>
29+
<strokeColor blue="0" green="127" red="255" alpha="255"/>
30+
<textColor blue="232" green="98" red="226" alpha="255"/>
31+
<ComposerItem y="184.506" excludeFromExports="0" frame="false" background="false" uuid="{2988dc5e-ea88-428f-93dd-e6aaf719dc8e}" lastValidViewScaleFactor="-1" zValue="7" frameJoinStyle="miter" id="" positionLock="false" width="62.9878" positionMode="0" itemRotation="0" page="1" outlineWidth="0.3" blendMode="0" pagey="184.506" x="140.608" pagex="140.608" visibility="1" opacity="1" height="20.2">
32+
<FrameColor blue="0" green="0" red="0" alpha="255"/>
33+
<BackgroundColor blue="255" green="255" red="255" alpha="255"/>
34+
<dataDefinedProperties>
35+
<Option type="Map">
36+
<Option type="QString" value="" name="name"/>
37+
<Option name="properties"/>
38+
<Option type="QString" value="collection" name="type"/>
39+
</Option>
40+
</dataDefinedProperties>
41+
<customproperties/>
42+
</ComposerItem>
43+
</ComposerScaleBar>
44+
<dataDefinedProperties>
45+
<Option type="Map">
46+
<Option type="QString" value="" name="name"/>
47+
<Option type="Map" name="properties">
48+
<Option type="Map" name="dataDefinedHeight">
49+
<Option type="bool" value="true" name="active"/>
50+
<Option type="QString" value="28" name="expression"/>
51+
<Option type="int" value="3" name="type"/>
52+
</Option>
53+
<Option type="Map" name="dataDefinedMapScale">
54+
<Option type="bool" value="true" name="active"/>
55+
<Option type="QString" value="24126145*1.1" name="expression"/>
56+
<Option type="int" value="3" name="type"/>
57+
</Option>
58+
<Option type="Map" name="dataDefinedPositionX">
59+
<Option type="bool" value="true" name="active"/>
60+
<Option type="QString" value="200" name="expression"/>
61+
<Option type="int" value="3" name="type"/>
62+
</Option>
63+
<Option type="Map" name="dataDefinedPositionY">
64+
<Option type="bool" value="true" name="active"/>
65+
<Option type="QString" value="70" name="expression"/>
66+
<Option type="int" value="3" name="type"/>
67+
</Option>
68+
<Option type="Map" name="dataDefinedSource">
69+
<Option type="bool" value="false" name="active"/>
70+
<Option type="QString" value="" name="expression"/>
71+
<Option type="int" value="3" name="type"/>
72+
</Option>
73+
<Option type="Map" name="dataDefinedWidth">
74+
<Option type="bool" value="true" name="active"/>
75+
<Option type="QString" value="24" name="expression"/>
76+
<Option type="int" value="3" name="type"/>
77+
</Option>
78+
</Option>
79+
<Option type="QString" value="collection" name="type"/>
80+
</Option>
81+
</dataDefinedProperties>
82+
<customproperties/>
83+
</Composition>
84+
</Composer>

0 commit comments

Comments
 (0)