Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[layout] Convert scalebar and legent from 2.x
  • Loading branch information
elpaso committed Jan 9, 2018
1 parent cd6e820 commit 680b9f8
Show file tree
Hide file tree
Showing 19 changed files with 559 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/core/composer/qgscomposerlegend.h
Expand Up @@ -350,6 +350,7 @@ class CORE_EXPORT QgsComposerLegend : public QgsComposerItem

//! Will be true if the legend should be resized automatically to fit contents
bool mSizeToContents = true;

};

#endif
Expand Down
304 changes: 295 additions & 9 deletions src/core/layout/qgscompositionconverter.cpp

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/core/layout/qgscompositionconverter.h
Expand Up @@ -35,6 +35,8 @@ class QgsLayoutItemPicture;
class QgsLayoutItemPolygon;
class QgsLayoutItemPolyline;
class QgsLayoutItemMap;
class QgsLayoutItemScaleBar;
class QgsLayoutItemLegend;

class CORE_EXPORT QgsCompositionConverter
{
Expand Down Expand Up @@ -153,6 +155,14 @@ class CORE_EXPORT QgsCompositionConverter
const QDomElement &itemElem,
const QgsProject *project );

static bool readScaleBarXml( QgsLayoutItemScaleBar *layoutItem,
const QDomElement &itemElem,
const QgsProject *project );

static bool readLegendXml( QgsLayoutItemLegend *layoutItem,
const QDomElement &itemElem,
const QgsProject *project );


/**
* Sets item state from DOM element
Expand Down
3 changes: 3 additions & 0 deletions src/core/layout/qgslayoutitemlegend.h
Expand Up @@ -505,6 +505,9 @@ class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem

//! Will be true if the legend should be resized automatically to fit contents
bool mSizeToContents = true;

friend class QgsCompositionConverter;

};

#endif // QGSLAYOUTITEMLEGEND_H
Expand Down
2 changes: 2 additions & 0 deletions src/core/layout/qgslayoutitemscalebar.h
Expand Up @@ -456,6 +456,8 @@ class CORE_EXPORT QgsLayoutItemScaleBar: public QgsLayoutItem

QgsScaleBarRenderer::ScaleBarContext createScaleContext() const;

friend class QgsCompositionConverter;

};

#endif //QGSLAYOUTITEMSCALEBAR_H
Expand Down
59 changes: 58 additions & 1 deletion tests/src/core/testqgscompositionconverter.cpp
Expand Up @@ -33,6 +33,8 @@
#include "qgslayoutitempolygon.h"
#include "qgslayoutitempolyline.h"
#include "qgslayoutitemmap.h"
#include "qgslayoutitemscalebar.h"
#include "qgslayoutitemlegend.h"


class TestQgsCompositionConverter: public QObject
Expand Down Expand Up @@ -85,6 +87,17 @@ class TestQgsCompositionConverter: public QObject
*/
void importComposerTemplateMap();

/**
* Test import legend from a composer template
*/
void importComposerTemplateLegend();

/**
* Test import scalebar from a composer template
*/
void importComposerTemplateScaleBar();


private:


Expand Down Expand Up @@ -324,6 +337,50 @@ void TestQgsCompositionConverter::importComposerTemplateMap()

}

void TestQgsCompositionConverter::importComposerTemplateLegend()
{
QDomElement docElem( loadComposition( "2x_template_legend.qpt" ) );
QVERIFY( !docElem.isNull() );
QgsProject project;
QgsLayout *layout = QgsCompositionConverter::createLayoutFromCompositionXml( docElem, &project );
QVERIFY( layout );
QCOMPARE( layout->pageCollection()->pageCount(), 1 );

QList<QgsLayoutItemLegend *> items;
layout->layoutItems<QgsLayoutItemLegend>( items );
QCOMPARE( items.size(), 1 );

QgsLayoutItemLegend *item = items.at( 0 );
QVERIFY( item->isVisible() );

checkRenderedImage( layout, QTest::currentTestFunction(), 0 );

qDeleteAll( items );

}

void TestQgsCompositionConverter::importComposerTemplateScaleBar()
{
QDomElement docElem( loadComposition( "2x_template_scalebar.qpt" ) );
QVERIFY( !docElem.isNull() );
QgsProject project;
QgsLayout *layout = QgsCompositionConverter::createLayoutFromCompositionXml( docElem, &project );
QVERIFY( layout );
QCOMPARE( layout->pageCollection()->pageCount(), 1 );

QList<QgsLayoutItemScaleBar *> items;
layout->layoutItems<QgsLayoutItemScaleBar>( items );
QCOMPARE( items.size(), 1 );

QgsLayoutItemScaleBar *item = items.at( 0 );
QVERIFY( item->isVisible() );

checkRenderedImage( layout, QTest::currentTestFunction(), 0 );

qDeleteAll( items );

}

void TestQgsCompositionConverter::importComposerTemplate()
{
QDomElement docElem( loadComposition( "2x_template.qpt" ) );
Expand All @@ -345,7 +402,7 @@ void TestQgsCompositionConverter::checkRenderedImage( QgsLayout *layout, const Q
QSize size( layout->pageCollection()->page( pageNumber )->sizeWithUnits().width() * 3.77, layout->pageCollection()->page( pageNumber )->sizeWithUnits().height() * 3.77 );
checker.setSize( size );
checker.setControlPathPrefix( QStringLiteral( "compositionconverter" ) );
QVERIFY( checker.testLayout( mReport, pageNumber ) );
QVERIFY( checker.testLayout( mReport, pageNumber, 0, true ) );
}

void TestQgsCompositionConverter::exportLayout( QgsLayout *layout, const QString testName )
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
106 changes: 106 additions & 0 deletions tests/testdata/layouts/2x_template_legend.qpt
@@ -0,0 +1,106 @@
<Composer>
<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">
<symbol type="fill" clip_to_extent="1" name="" alpha="1">
<layer pass="0" enabled="1" locked="0" class="SimpleFill">
<prop v="3x:0,0,0,0,0,0" k="border_width_map_unit_scale"/>
<prop v="241,244,199,255" k="color"/>
<prop v="bevel" k="joinstyle"/>
<prop v="0,0" k="offset"/>
<prop v="3x:0,0,0,0,0,0" k="offset_map_unit_scale"/>
<prop v="MM" k="offset_unit"/>
<prop v="175,179,138,255" k="outline_color"/>
<prop v="solid" k="outline_style"/>
<prop v="0.26" k="outline_width"/>
<prop v="MM" k="outline_width_unit"/>
<prop v="solid" k="style"/>
<data_defined_properties>
<Option type="Map">
<Option type="QString" value="" name="name"/>
<Option name="properties"/>
<Option type="QString" value="collection" name="type"/>
</Option>
</data_defined_properties>
</layer>
</symbol>
<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">
<styles>
<style name="title" marginBottom="6">
<styleFont description="MS Shell Dlg 2,36,-1,5,50,0,0,0,0,0" style=""/>
</style>
<style name="group" marginTop="5">
<styleFont description="MS Shell Dlg 2,14,-1,5,50,0,0,0,0,0" style=""/>
</style>
<style name="subgroup" marginTop="4">
<styleFont description="MS Shell Dlg 2,12,-1,5,50,0,0,0,0,0" style=""/>
</style>
<style name="symbol" marginTop="3">
<styleFont description="MS Shell Dlg 2,8.25,-1,5,50,0,0,0,0,0" style=""/>
</style>
<style marginLeft="4" name="symbolLabel" marginTop="3">
<styleFont description="MS Shell Dlg 2,12,-1,5,50,0,0,0,0,0" style=""/>
</style>
</styles>
<layer-tree-group>
<customproperties/>
<layer-tree-layer providerKey="ogr" name="points" expanded="1" checked="Qt::Checked" id="points20171212162310546" source="../../../dev/QGIS/tests/testdata/points.shp">
<customproperties/>
</layer-tree-layer>
<layer-tree-layer providerKey="ogr" name="polys" expanded="1" checked="Qt::Checked" id="polys20171212162309844" source="../../../dev/QGIS/tests/testdata/polys.shp">
<customproperties/>
</layer-tree-layer>
<custom-order enabled="0"/>
</layer-tree-group>
<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">
<FrameColor blue="0" green="0" red="0" alpha="255"/>
<BackgroundColor blue="255" green="255" red="255" alpha="255"/>
<dataDefinedProperties>
<Option type="Map">
<Option type="QString" value="" name="name"/>
<Option name="properties"/>
<Option type="QString" value="collection" name="type"/>
</Option>
</dataDefinedProperties>
<customproperties/>
</ComposerItem>
</ComposerLegend>
<dataDefinedProperties>
<Option type="Map">
<Option type="QString" value="" name="name"/>
<Option type="Map" name="properties">
<Option type="Map" name="dataDefinedHeight">
<Option type="bool" value="true" name="active"/>
<Option type="QString" value="28" name="expression"/>
<Option type="int" value="3" name="type"/>
</Option>
<Option type="Map" name="dataDefinedMapScale">
<Option type="bool" value="true" name="active"/>
<Option type="QString" value="24126145*1.1" name="expression"/>
<Option type="int" value="3" name="type"/>
</Option>
<Option type="Map" name="dataDefinedPositionX">
<Option type="bool" value="true" name="active"/>
<Option type="QString" value="200" name="expression"/>
<Option type="int" value="3" name="type"/>
</Option>
<Option type="Map" name="dataDefinedPositionY">
<Option type="bool" value="true" name="active"/>
<Option type="QString" value="70" name="expression"/>
<Option type="int" value="3" name="type"/>
</Option>
<Option type="Map" name="dataDefinedSource">
<Option type="bool" value="false" name="active"/>
<Option type="QString" value="" name="expression"/>
<Option type="int" value="3" name="type"/>
</Option>
<Option type="Map" name="dataDefinedWidth">
<Option type="bool" value="true" name="active"/>
<Option type="QString" value="24" name="expression"/>
<Option type="int" value="3" name="type"/>
</Option>
</Option>
<Option type="QString" value="collection" name="type"/>
</Option>
</dataDefinedProperties>
<customproperties/>
</Composition>
</Composer>
84 changes: 84 additions & 0 deletions tests/testdata/layouts/2x_template_scalebar.qpt
@@ -0,0 +1,84 @@
<Composer>
<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">
<symbol type="fill" clip_to_extent="1" name="" alpha="1">
<layer pass="0" enabled="1" locked="0" class="SimpleFill">
<prop v="3x:0,0,0,0,0,0" k="border_width_map_unit_scale"/>
<prop v="241,244,199,255" k="color"/>
<prop v="bevel" k="joinstyle"/>
<prop v="0,0" k="offset"/>
<prop v="3x:0,0,0,0,0,0" k="offset_map_unit_scale"/>
<prop v="MM" k="offset_unit"/>
<prop v="175,179,138,255" k="outline_color"/>
<prop v="solid" k="outline_style"/>
<prop v="0.26" k="outline_width"/>
<prop v="MM" k="outline_width_unit"/>
<prop v="solid" k="style"/>
<data_defined_properties>
<Option type="Map">
<Option type="QString" value="" name="name"/>
<Option name="properties"/>
<Option type="QString" value="collection" name="type"/>
</Option>
</data_defined_properties>
</layer>
</symbol>
<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">
<scaleBarFont description="MS Shell Dlg 2,12,-1,5,50,0,0,0,0,0" style=""/>
<fillColor blue="28" green="26" red="227" alpha="255"/>
<fillColor2 blue="111" green="191" red="253" alpha="255"/>
<strokeColor blue="0" green="127" red="255" alpha="255"/>
<textColor blue="232" green="98" red="226" alpha="255"/>
<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">
<FrameColor blue="0" green="0" red="0" alpha="255"/>
<BackgroundColor blue="255" green="255" red="255" alpha="255"/>
<dataDefinedProperties>
<Option type="Map">
<Option type="QString" value="" name="name"/>
<Option name="properties"/>
<Option type="QString" value="collection" name="type"/>
</Option>
</dataDefinedProperties>
<customproperties/>
</ComposerItem>
</ComposerScaleBar>
<dataDefinedProperties>
<Option type="Map">
<Option type="QString" value="" name="name"/>
<Option type="Map" name="properties">
<Option type="Map" name="dataDefinedHeight">
<Option type="bool" value="true" name="active"/>
<Option type="QString" value="28" name="expression"/>
<Option type="int" value="3" name="type"/>
</Option>
<Option type="Map" name="dataDefinedMapScale">
<Option type="bool" value="true" name="active"/>
<Option type="QString" value="24126145*1.1" name="expression"/>
<Option type="int" value="3" name="type"/>
</Option>
<Option type="Map" name="dataDefinedPositionX">
<Option type="bool" value="true" name="active"/>
<Option type="QString" value="200" name="expression"/>
<Option type="int" value="3" name="type"/>
</Option>
<Option type="Map" name="dataDefinedPositionY">
<Option type="bool" value="true" name="active"/>
<Option type="QString" value="70" name="expression"/>
<Option type="int" value="3" name="type"/>
</Option>
<Option type="Map" name="dataDefinedSource">
<Option type="bool" value="false" name="active"/>
<Option type="QString" value="" name="expression"/>
<Option type="int" value="3" name="type"/>
</Option>
<Option type="Map" name="dataDefinedWidth">
<Option type="bool" value="true" name="active"/>
<Option type="QString" value="24" name="expression"/>
<Option type="int" value="3" name="type"/>
</Option>
</Option>
<Option type="QString" value="collection" name="type"/>
</Option>
</dataDefinedProperties>
<customproperties/>
</Composition>
</Composer>

0 comments on commit 680b9f8

Please sign in to comment.