|
| 1 | +/*************************************************************************** |
| 2 | + testqgscomposerscalebar.cpp |
| 3 | + --------------------------- |
| 4 | + begin : August 2012 |
| 5 | + copyright : (C) 2012 by Marco Hugentobler |
| 6 | + email : marco at sourcepole dot ch |
| 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 "qgscomposerscalebar.h" |
| 23 | +#include "qgsmaplayerregistry.h" |
| 24 | +#include "qgsmaprenderer.h" |
| 25 | +#include "qgsmultibandcolorrenderer.h" |
| 26 | +#include "qgsrasterlayer.h" |
| 27 | +#include <QObject> |
| 28 | +#include <QtTest> |
| 29 | + |
| 30 | +class TestQgsComposerScaleBar: public QObject |
| 31 | +{ |
| 32 | + Q_OBJECT; |
| 33 | + private slots: |
| 34 | + void initTestCase();// will be called before the first testfunction is executed. |
| 35 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 36 | + void init();// will be called before each testfunction is executed. |
| 37 | + void cleanup();// will be called after every testfunction. |
| 38 | + void singleBox(); |
| 39 | + void doubleBox(); |
| 40 | + void numeric(); |
| 41 | + void tick(); |
| 42 | + |
| 43 | + private: |
| 44 | + QgsComposition* mComposition; |
| 45 | + QgsComposerMap* mComposerMap; |
| 46 | + QgsComposerScaleBar* mComposerScaleBar; |
| 47 | + QgsRasterLayer* mRasterLayer; |
| 48 | + QgsMapRenderer* mMapRenderer; |
| 49 | +}; |
| 50 | + |
| 51 | +void TestQgsComposerScaleBar::initTestCase() |
| 52 | +{ |
| 53 | + QgsApplication::init(); |
| 54 | + QgsApplication::initQgis(); |
| 55 | + |
| 56 | + //create maplayers from testdata and add to layer registry |
| 57 | + QFileInfo rasterFileInfo( QString( TEST_DATA_DIR ) + QDir::separator() + "landsat.tif" ); |
| 58 | + mRasterLayer = new QgsRasterLayer( rasterFileInfo.filePath(), |
| 59 | + rasterFileInfo.completeBaseName() ); |
| 60 | + QgsMultiBandColorRenderer* rasterRenderer = new QgsMultiBandColorRenderer( mRasterLayer->dataProvider(), 2, 3, 4 ); |
| 61 | + mRasterLayer->setRenderer( rasterRenderer ); |
| 62 | + |
| 63 | + QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer*>() << mRasterLayer ); |
| 64 | + |
| 65 | + //create composition with composer map |
| 66 | + mMapRenderer = new QgsMapRenderer(); |
| 67 | + mMapRenderer->setLayerSet( QStringList() << mRasterLayer->id() ); |
| 68 | + |
| 69 | + //reproject to WGS84 |
| 70 | + QgsCoordinateReferenceSystem destCRS; |
| 71 | + destCRS.createFromId( 4326, QgsCoordinateReferenceSystem::EpsgCrsId ); |
| 72 | + mMapRenderer->setDestinationCrs( destCRS ); |
| 73 | + mMapRenderer->setProjectionsEnabled( true ); |
| 74 | + |
| 75 | + mComposition = new QgsComposition( mMapRenderer ); |
| 76 | + mComposition->setPaperSize( 297, 210 ); //A4 landscape |
| 77 | + mComposerMap = new QgsComposerMap( mComposition, 20, 20, 150, 150 ); |
| 78 | + mComposerMap->setFrameEnabled( true ); |
| 79 | + mComposition->addComposerMap( mComposerMap ); |
| 80 | + mComposerMap->setNewExtent( QgsRectangle( 17.923, 30.160, 18.023, 30.260 ) ); |
| 81 | + |
| 82 | + mComposerScaleBar = new QgsComposerScaleBar( mComposition ); |
| 83 | + mComposerScaleBar->setSceneRect( QRectF( 20, 180, 20, 20 ) ); |
| 84 | + mComposition->addComposerScaleBar( mComposerScaleBar ); |
| 85 | + mComposerScaleBar->setComposerMap( mComposerMap ); |
| 86 | + mComposerScaleBar->setUnits( QgsComposerScaleBar::Meters ); |
| 87 | + mComposerScaleBar->setNumUnitsPerSegment( 2000 ); |
| 88 | + mComposerScaleBar->setNumSegmentsLeft( 0 ); |
| 89 | + mComposerScaleBar->setNumSegments( 2 ); |
| 90 | +}; |
| 91 | + |
| 92 | +void TestQgsComposerScaleBar::cleanupTestCase() |
| 93 | +{ |
| 94 | + delete mComposition; |
| 95 | + delete mMapRenderer; |
| 96 | + delete mRasterLayer; |
| 97 | +}; |
| 98 | + |
| 99 | +void TestQgsComposerScaleBar::init() |
| 100 | +{ |
| 101 | + |
| 102 | +}; |
| 103 | + |
| 104 | +void TestQgsComposerScaleBar::cleanup() |
| 105 | +{ |
| 106 | + |
| 107 | +}; |
| 108 | + |
| 109 | +void TestQgsComposerScaleBar::singleBox() |
| 110 | +{ |
| 111 | + mComposerScaleBar->setStyle( "Single Box" ); |
| 112 | + QgsCompositionChecker checker( "Composer scalebar singleBox", mComposition, QString( QString( TEST_DATA_DIR ) + QDir::separator() + |
| 113 | + "control_images" + QDir::separator() + "expected_composerscalebar" + QDir::separator() + "composerscalebar_singlebox.png" ) ); |
| 114 | + QVERIFY( checker.testComposition() ); |
| 115 | +}; |
| 116 | + |
| 117 | +void TestQgsComposerScaleBar::doubleBox() |
| 118 | +{ |
| 119 | + mComposerScaleBar->setStyle( "Double Box" ); |
| 120 | + QgsCompositionChecker checker( "Composer scalebar doubleBox", mComposition, QString( QString( TEST_DATA_DIR ) + QDir::separator() + |
| 121 | + "control_images" + QDir::separator() + "expected_composerscalebar" + QDir::separator() + "composerscalebar_doublebox.png" ) ); |
| 122 | + QVERIFY( checker.testComposition() ); |
| 123 | +}; |
| 124 | + |
| 125 | +void TestQgsComposerScaleBar::numeric() |
| 126 | +{ |
| 127 | + mComposerScaleBar->setStyle( "Numeric" ); |
| 128 | + QgsCompositionChecker checker( "Composer scalebar numeric", mComposition, QString( QString( TEST_DATA_DIR ) + QDir::separator() + |
| 129 | + "control_images" + QDir::separator() + "expected_composerscalebar" + QDir::separator() + "composerscalebar_numeric.png" ) ); |
| 130 | + QVERIFY( checker.testComposition() ); |
| 131 | +}; |
| 132 | + |
| 133 | +void TestQgsComposerScaleBar::tick() |
| 134 | +{ |
| 135 | + mComposerScaleBar->setStyle( "Line Ticks Up" ); |
| 136 | + QgsCompositionChecker checker( "Composer scalebar numeric", mComposition, QString( QString( TEST_DATA_DIR ) + QDir::separator() + |
| 137 | + "control_images" + QDir::separator() + "expected_composerscalebar" + QDir::separator() + "composerscalebar_numeric.png" ) ); |
| 138 | + QVERIFY( checker.testComposition() ); |
| 139 | +}; |
| 140 | + |
| 141 | +QTEST_MAIN( TestQgsComposerScaleBar ) |
| 142 | +#include "moc_testqgscomposerscalebar.cxx" |
0 commit comments