|
| 1 | +/*************************************************************************** |
| 2 | + testqgscomposermapatlas.cpp |
| 3 | + --------------------------- |
| 4 | + begin : Sept 2012 |
| 5 | + copyright : (C) 2012 by Hugo Mercier |
| 6 | + email : hugo dot mercier at oslandia dot com |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | +#include "qgsapplication.h" |
| 19 | +#include "qgscomposition.h" |
| 20 | +#include "qgscompositionchecker.h" |
| 21 | +#include "qgscomposermap.h" |
| 22 | +#include "qgscomposerlabel.h" |
| 23 | +#include "qgsmaplayerregistry.h" |
| 24 | +#include "qgsmaprenderer.h" |
| 25 | +#include "qgsvectorlayer.h" |
| 26 | +#include "qgsvectordataprovider.h" |
| 27 | +#include "qgssymbolv2.h" |
| 28 | +#include "qgssinglesymbolrendererv2.h" |
| 29 | +#include <QObject> |
| 30 | +#include <QtTest> |
| 31 | + |
| 32 | +class TestQgsComposerMapAtlas: public QObject |
| 33 | +{ |
| 34 | + Q_OBJECT; |
| 35 | +private slots: |
| 36 | + void initTestCase();// will be called before the first testfunction is executed. |
| 37 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 38 | + void init();// will be called before each testfunction is executed. |
| 39 | + void cleanup();// will be called after every testfunction. |
| 40 | + |
| 41 | + // test filename pattern evaluation |
| 42 | + void filename(); |
| 43 | + // test rendering with an autoscale atlas |
| 44 | + void autoscale_render(); |
| 45 | + // test rendering with a fixed scale atlas |
| 46 | + void fixedscale_render(); |
| 47 | + private: |
| 48 | + QgsComposition* mComposition; |
| 49 | + QgsComposerLabel* mLabel1; |
| 50 | + QgsComposerLabel* mLabel2; |
| 51 | + QgsComposerMap* mAtlasMap; |
| 52 | + QgsComposerMap* mOverview; |
| 53 | + QgsMapRenderer* mMapRenderer; |
| 54 | + QgsVectorLayer* mVectorLayer; |
| 55 | +}; |
| 56 | + |
| 57 | +void TestQgsComposerMapAtlas::initTestCase() |
| 58 | +{ |
| 59 | + QgsApplication::init(); |
| 60 | + QgsApplication::initQgis(); |
| 61 | + |
| 62 | + //create maplayers from testdata and add to layer registry |
| 63 | + QFileInfo vectorFileInfo( QString( TEST_DATA_DIR ) + QDir::separator() + "france_parts.shp" ); |
| 64 | + mVectorLayer = new QgsVectorLayer( vectorFileInfo.filePath(), |
| 65 | + vectorFileInfo.completeBaseName(), |
| 66 | + "ogr" ); |
| 67 | + |
| 68 | + QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer*>() << mVectorLayer ); |
| 69 | + |
| 70 | + //create composition with composer map |
| 71 | + mMapRenderer = new QgsMapRenderer(); |
| 72 | + mMapRenderer->setLayerSet( QStringList() << mVectorLayer->id() ); |
| 73 | + mMapRenderer->setProjectionsEnabled( true ); |
| 74 | + |
| 75 | + // select epsg:2154 |
| 76 | + QgsCoordinateReferenceSystem crs; |
| 77 | + crs.createFromSrid( 2154 ); |
| 78 | + mMapRenderer->setDestinationCrs( crs ); |
| 79 | + mComposition = new QgsComposition( mMapRenderer ); |
| 80 | + mComposition->setPaperSize( 297, 210 ); //A4 landscape |
| 81 | + |
| 82 | + // fix the renderer, fill with green |
| 83 | + QgsStringMap props; |
| 84 | + props.insert( "color", "0,127,0" ); |
| 85 | + QgsFillSymbolV2* fillSymbol = QgsFillSymbolV2::createSimple( props ); |
| 86 | + QgsSingleSymbolRendererV2* renderer = new QgsSingleSymbolRendererV2( fillSymbol ); |
| 87 | + mVectorLayer->setRendererV2( renderer ); |
| 88 | + |
| 89 | + // the atlas map |
| 90 | + mAtlasMap = new QgsComposerMap( mComposition, 20, 20, 130, 130 ); |
| 91 | + mAtlasMap->setFrameEnabled( true ); |
| 92 | + mAtlasMap->setAtlasCoverageLayer( mVectorLayer ); |
| 93 | + mComposition->addComposerMap( mAtlasMap ); |
| 94 | + mComposition->setAtlasMap( mAtlasMap ); |
| 95 | + |
| 96 | + // an overview |
| 97 | + mOverview = new QgsComposerMap( mComposition, 180, 20, 50, 50 ); |
| 98 | + mOverview->setFrameEnabled( true ); |
| 99 | + mOverview->setOverviewFrameMap( mAtlasMap->id() ); |
| 100 | + mComposition->addComposerMap( mOverview ); |
| 101 | + mOverview->setNewExtent( QgsRectangle( 49670.718, 6415139.086, 699672.519, 7065140.887 ) ); |
| 102 | + |
| 103 | + // header label |
| 104 | + mLabel1 = new QgsComposerLabel( mComposition ); |
| 105 | + mComposition->addComposerLabel( mLabel1 ); |
| 106 | + mLabel1->setText( "[% \"NAME_1\" %] area" ); |
| 107 | + mLabel1->adjustSizeToText(); |
| 108 | + mLabel1->setItemPosition( 150, 5 ); |
| 109 | + |
| 110 | + // feature number label |
| 111 | + mLabel2 = new QgsComposerLabel( mComposition ); |
| 112 | + mComposition->addComposerLabel( mLabel2 ); |
| 113 | + mLabel2->setText( "# [%$feature || ' / ' || $numfeatures%]" ); |
| 114 | + mLabel2->adjustSizeToText(); |
| 115 | + mLabel2->setItemPosition( 150, 200 ); |
| 116 | +} |
| 117 | + |
| 118 | +void TestQgsComposerMapAtlas::cleanupTestCase() |
| 119 | +{ |
| 120 | + /* delete mComposition; |
| 121 | + delete mComposerMap; |
| 122 | + delete mLabel1; |
| 123 | + delete mLabel2; |
| 124 | + delete mMapRenderer; |
| 125 | + delete mVectorLayer; |
| 126 | + */ |
| 127 | +} |
| 128 | + |
| 129 | +void TestQgsComposerMapAtlas::init() |
| 130 | +{ |
| 131 | + |
| 132 | +} |
| 133 | + |
| 134 | +void TestQgsComposerMapAtlas::cleanup() |
| 135 | +{ |
| 136 | + |
| 137 | +} |
| 138 | + |
| 139 | +void TestQgsComposerMapAtlas::filename() |
| 140 | +{ |
| 141 | + QgsAtlasRendering atlasRender( mComposition ); |
| 142 | + atlasRender.begin( "'output_' || $feature" ); |
| 143 | + for ( size_t fi = 0; fi < atlasRender.numFeatures(); ++fi ) |
| 144 | + { |
| 145 | + atlasRender.prepareForFeature( fi ); |
| 146 | + QString expected = QString( "output_%1" ).arg( (int)(fi+1) ); |
| 147 | + QCOMPARE( atlasRender.currentFilename(), expected ); |
| 148 | + } |
| 149 | + atlasRender.end(); |
| 150 | +} |
| 151 | + |
| 152 | +void TestQgsComposerMapAtlas::autoscale_render() |
| 153 | +{ |
| 154 | + mAtlasMap->setAtlasFixedScale( false ); |
| 155 | + mAtlasMap->setAtlasMargin( 0.10 ); |
| 156 | + |
| 157 | + QgsAtlasRendering atlasRender( mComposition ); |
| 158 | + |
| 159 | + atlasRender.begin(); |
| 160 | + |
| 161 | + for ( size_t fit = 0; fit < 2; ++fit ) |
| 162 | + { |
| 163 | + atlasRender.prepareForFeature( fit ); |
| 164 | + mLabel1->adjustSizeToText(); |
| 165 | + |
| 166 | + QgsCompositionChecker checker( "Atlas autoscale test", mComposition, |
| 167 | + QString( TEST_DATA_DIR ) + QDir::separator() + "control_images" + QDir::separator() + |
| 168 | + "expected_composermapatlas" + QDir::separator() + |
| 169 | + QString( "autoscale_%1.png" ).arg((int)fit) ); |
| 170 | + QVERIFY( checker.testComposition( 0 ) ); |
| 171 | + } |
| 172 | + atlasRender.end(); |
| 173 | +} |
| 174 | + |
| 175 | +void TestQgsComposerMapAtlas::fixedscale_render() |
| 176 | +{ |
| 177 | + mAtlasMap->setNewExtent( QgsRectangle( 209838.166, 6528781.020, 610491.166, 6920530.620 ) ); |
| 178 | + mAtlasMap->setAtlasFixedScale( true ); |
| 179 | + |
| 180 | + QgsAtlasRendering atlasRender( mComposition ); |
| 181 | + |
| 182 | + atlasRender.begin(); |
| 183 | + |
| 184 | + for ( size_t fit = 0; fit < 2; ++fit ) |
| 185 | + { |
| 186 | + atlasRender.prepareForFeature( fit ); |
| 187 | + mLabel1->adjustSizeToText(); |
| 188 | + |
| 189 | + QgsCompositionChecker checker( "Atlas fixedscale test", mComposition, |
| 190 | + QString( TEST_DATA_DIR ) + QDir::separator() + "control_images" + QDir::separator() + |
| 191 | + "expected_composermapatlas" + QDir::separator() + |
| 192 | + QString( "fixedscale_%1.png" ).arg((int)fit) ); |
| 193 | + QVERIFY( checker.testComposition( 0 ) ); |
| 194 | + } |
| 195 | + atlasRender.end(); |
| 196 | + |
| 197 | +} |
| 198 | + |
| 199 | +QTEST_MAIN( TestQgsComposerMapAtlas ) |
| 200 | +#include "moc_testqgscomposermapatlas.cxx" |
0 commit comments