|
| 1 | +/*************************************************************************** |
| 2 | + testqgscomposermapoverview.cpp |
| 3 | + ---------------------- |
| 4 | + begin : September 2014 |
| 5 | + copyright : (C) 2014 by Nyall Dawson |
| 6 | + email : nyall dot dawson at gmail 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 "qgscomposermapoverview.h" |
| 23 | +#include "qgsmaplayerregistry.h" |
| 24 | +#include "qgsmaprenderer.h" |
| 25 | +#include "qgsmultibandcolorrenderer.h" |
| 26 | +#include "qgsrasterlayer.h" |
| 27 | +#include "qgsfontutils.h" |
| 28 | +#include <QObject> |
| 29 | +#include <QtTest> |
| 30 | + |
| 31 | +class TestQgsComposerMapOverview: public QObject |
| 32 | +{ |
| 33 | + Q_OBJECT; |
| 34 | + private slots: |
| 35 | + void initTestCase();// will be called before the first testfunction is executed. |
| 36 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 37 | + void init();// will be called before each testfunction is executed. |
| 38 | + void cleanup();// will be called after every testfunction. |
| 39 | + void overviewMap(); //test if overview map frame works |
| 40 | + void overviewMapRotated(); //test if overview map frame works with rotated overview |
| 41 | + void overviewMapRotated2(); //test if overview map frame works with rotated map |
| 42 | + void overviewMapBlending(); //test if blend modes with overview map frame works |
| 43 | + void overviewMapInvert(); //test if invert of overview map frame works |
| 44 | + void overviewMapCenter(); //test if centering of overview map frame works |
| 45 | + |
| 46 | + private: |
| 47 | + QgsComposition* mComposition; |
| 48 | + QgsComposerMap* mComposerMap; |
| 49 | + QgsMapSettings mMapSettings; |
| 50 | + QgsRasterLayer* mRasterLayer; |
| 51 | + QString mReport; |
| 52 | +}; |
| 53 | + |
| 54 | +void TestQgsComposerMapOverview::initTestCase() |
| 55 | +{ |
| 56 | + QgsApplication::init(); |
| 57 | + QgsApplication::initQgis(); |
| 58 | + |
| 59 | + //create maplayers from testdata and add to layer registry |
| 60 | + QFileInfo rasterFileInfo( QString( TEST_DATA_DIR ) + QDir::separator() + "landsat.tif" ); |
| 61 | + mRasterLayer = new QgsRasterLayer( rasterFileInfo.filePath(), |
| 62 | + rasterFileInfo.completeBaseName() ); |
| 63 | + QgsMultiBandColorRenderer* rasterRenderer = new QgsMultiBandColorRenderer( mRasterLayer->dataProvider(), 2, 3, 4 ); |
| 64 | + mRasterLayer->setRenderer( rasterRenderer ); |
| 65 | + |
| 66 | + QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer*>() << mRasterLayer ); |
| 67 | + |
| 68 | + //create composition with composer map |
| 69 | + mMapSettings.setLayers( QStringList() << mRasterLayer->id() ); |
| 70 | + mMapSettings.setCrsTransformEnabled( false ); |
| 71 | + mComposition = new QgsComposition( mMapSettings ); |
| 72 | + mComposition->setPaperSize( 297, 210 ); //A4 landscape |
| 73 | + mComposerMap = new QgsComposerMap( mComposition, 20, 20, 200, 100 ); |
| 74 | + mComposerMap->setFrameEnabled( true ); |
| 75 | + mComposition->addComposerMap( mComposerMap ); |
| 76 | + |
| 77 | + mReport = "<h1>Composer Map Overview Tests</h1>\n"; |
| 78 | +} |
| 79 | + |
| 80 | +void TestQgsComposerMapOverview::cleanupTestCase() |
| 81 | +{ |
| 82 | + delete mComposition; |
| 83 | + delete mRasterLayer; |
| 84 | + |
| 85 | + QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html"; |
| 86 | + QFile myFile( myReportFile ); |
| 87 | + if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) ) |
| 88 | + { |
| 89 | + QTextStream myQTextStream( &myFile ); |
| 90 | + myQTextStream << mReport; |
| 91 | + myFile.close(); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +void TestQgsComposerMapOverview::init() |
| 96 | +{ |
| 97 | +} |
| 98 | + |
| 99 | +void TestQgsComposerMapOverview::cleanup() |
| 100 | +{ |
| 101 | + |
| 102 | +} |
| 103 | + |
| 104 | +void TestQgsComposerMapOverview::overviewMap() |
| 105 | +{ |
| 106 | + QgsComposerMap* overviewMap = new QgsComposerMap( mComposition, 20, 130, 70, 70 ); |
| 107 | + overviewMap->setFrameEnabled( true ); |
| 108 | + mComposition->addComposerMap( overviewMap ); |
| 109 | + mComposerMap->setNewExtent( QgsRectangle( 785462.375, 3341423.125, 789262.375, 3343323.125 ) ); //zoom in |
| 110 | + overviewMap->setNewExtent( QgsRectangle( 781662.375, 3339523.125, 793062.375, 3350923.125 ) ); |
| 111 | + overviewMap->overview()->setFrameMap( mComposerMap->id() ); |
| 112 | + QgsCompositionChecker checker( "composermap_overview", mComposition ); |
| 113 | + |
| 114 | + bool testResult = checker.testComposition( mReport, 0, 100 ); |
| 115 | + mComposition->removeComposerItem( overviewMap ); |
| 116 | + QVERIFY( testResult ); |
| 117 | +} |
| 118 | + |
| 119 | +void TestQgsComposerMapOverview::overviewMapRotated() |
| 120 | +{ |
| 121 | + QgsComposerMap* overviewMap = new QgsComposerMap( mComposition, 20, 130, 70, 70 ); |
| 122 | + overviewMap->setFrameEnabled( true ); |
| 123 | + mComposition->addComposerMap( overviewMap ); |
| 124 | + mComposerMap->setNewExtent( QgsRectangle( 785462.375, 3341423.125, 789262.375, 3343323.125 ) ); //zoom in |
| 125 | + mComposerMap->setMapRotation( 30 ); |
| 126 | + overviewMap->setNewExtent( QgsRectangle( 781662.375, 3339523.125, 793062.375, 3350923.125 ) ); |
| 127 | + overviewMap->overview()->setFrameMap( mComposerMap->id() ); |
| 128 | + QgsCompositionChecker checker( "composermap_overview_rotated", mComposition ); |
| 129 | + |
| 130 | + bool testResult = checker.testComposition( mReport, 0, 100 ); |
| 131 | + mComposition->removeComposerItem( overviewMap ); |
| 132 | + mComposerMap->setMapRotation( 0 ); |
| 133 | + QVERIFY( testResult ); |
| 134 | +} |
| 135 | + |
| 136 | +void TestQgsComposerMapOverview::overviewMapRotated2() |
| 137 | +{ |
| 138 | + QgsComposerMap* overviewMap = new QgsComposerMap( mComposition, 20, 130, 70, 70 ); |
| 139 | + overviewMap->setFrameEnabled( true ); |
| 140 | + mComposition->addComposerMap( overviewMap ); |
| 141 | + mComposerMap->setNewExtent( QgsRectangle( 785462.375, 3341423.125, 789262.375, 3343323.125 ) ); //zoom in |
| 142 | + overviewMap->setMapRotation( 30 ); |
| 143 | + overviewMap->setNewExtent( QgsRectangle( 781662.375, 3339523.125, 793062.375, 3350923.125 ) ); |
| 144 | + overviewMap->overview()->setFrameMap( mComposerMap->id() ); |
| 145 | + QgsCompositionChecker checker( "composermap_overview_rotated2", mComposition ); |
| 146 | + |
| 147 | + bool testResult = checker.testComposition( mReport, 0, 100 ); |
| 148 | + mComposition->removeComposerItem( overviewMap ); |
| 149 | + QVERIFY( testResult ); |
| 150 | +} |
| 151 | + |
| 152 | +void TestQgsComposerMapOverview::overviewMapBlending() |
| 153 | +{ |
| 154 | + QgsComposerMap* overviewMapBlend = new QgsComposerMap( mComposition, 20, 130, 70, 70 ); |
| 155 | + overviewMapBlend->setFrameEnabled( true ); |
| 156 | + mComposition->addComposerMap( overviewMapBlend ); |
| 157 | + mComposerMap->setNewExtent( QgsRectangle( 785462.375, 3341423.125, 789262.375, 3343323.125 ) ); //zoom in |
| 158 | + overviewMapBlend->setNewExtent( QgsRectangle( 781662.375, 3339523.125, 793062.375, 3350923.125 ) ); |
| 159 | + overviewMapBlend->overview()->setFrameMap( mComposerMap->id() ); |
| 160 | + overviewMapBlend->overview()->setBlendMode( QPainter::CompositionMode_Multiply ); |
| 161 | + |
| 162 | + QgsCompositionChecker checker( "composermap_overview_blending", mComposition ); |
| 163 | + |
| 164 | + bool testResult = checker.testComposition( mReport, 0, 100 ); |
| 165 | + mComposition->removeComposerItem( overviewMapBlend ); |
| 166 | + QVERIFY( testResult ); |
| 167 | +} |
| 168 | + |
| 169 | +void TestQgsComposerMapOverview::overviewMapInvert() |
| 170 | +{ |
| 171 | + QgsComposerMap* overviewMapInvert = new QgsComposerMap( mComposition, 20, 130, 70, 70 ); |
| 172 | + overviewMapInvert->setFrameEnabled( true ); |
| 173 | + mComposition->addComposerMap( overviewMapInvert ); |
| 174 | + mComposerMap->setNewExtent( QgsRectangle( 785462.375, 3341423.125, 789262.375, 3343323.125 ) ); //zoom in |
| 175 | + overviewMapInvert->setNewExtent( QgsRectangle( 781662.375, 3339523.125, 793062.375, 3350923.125 ) ); |
| 176 | + overviewMapInvert->overview()->setFrameMap( mComposerMap->id() ); |
| 177 | + overviewMapInvert->overview()->setInverted( true ); |
| 178 | + |
| 179 | + QgsCompositionChecker checker( "composermap_overview_invert", mComposition ); |
| 180 | + |
| 181 | + bool testResult = checker.testComposition( mReport, 0, 100 ); |
| 182 | + mComposition->removeComposerItem( overviewMapInvert ); |
| 183 | + QVERIFY( testResult ); |
| 184 | +} |
| 185 | + |
| 186 | +void TestQgsComposerMapOverview::overviewMapCenter() |
| 187 | +{ |
| 188 | + QgsComposerMap* overviewMapCenter = new QgsComposerMap( mComposition, 20, 130, 70, 70 ); |
| 189 | + overviewMapCenter->setFrameEnabled( true ); |
| 190 | + mComposition->addComposerMap( overviewMapCenter ); |
| 191 | + mComposerMap->setNewExtent( QgsRectangle( 785462.375 + 5000, 3341423.125, 789262.375 + 5000, 3343323.125 ) ); //zoom in |
| 192 | + overviewMapCenter->setNewExtent( QgsRectangle( 781662.375, 3339523.125, 793062.375, 3350923.125 ) ); |
| 193 | + overviewMapCenter->overview()->setFrameMap( mComposerMap->id() ); |
| 194 | + overviewMapCenter->overview()->setCentered( true ); |
| 195 | + |
| 196 | + QgsCompositionChecker checker( "composermap_overview_center", mComposition ); |
| 197 | + |
| 198 | + bool testResult = checker.testComposition( mReport, 0, 100 ); |
| 199 | + mComposition->removeComposerItem( overviewMapCenter ); |
| 200 | + QVERIFY( testResult ); |
| 201 | +} |
| 202 | + |
| 203 | +QTEST_MAIN( TestQgsComposerMapOverview ) |
| 204 | +#include "moc_testqgscomposermapoverview.cxx" |
0 commit comments