|
| 1 | + |
| 2 | +/*************************************************************************** |
| 3 | + testqgslayoutscalebar.cpp |
| 4 | + --------------------------- |
| 5 | + begin : November 2017 |
| 6 | + copyright : (C) 2017 by Nyall Dawson |
| 7 | + email : nyall dot dawson at gmail dot com |
| 8 | + ***************************************************************************/ |
| 9 | + |
| 10 | +/*************************************************************************** |
| 11 | + * * |
| 12 | + * This program is free software; you can redistribute it and/or modify * |
| 13 | + * it under the terms of the GNU General Public License as published by * |
| 14 | + * the Free Software Foundation; either version 2 of the License, or * |
| 15 | + * (at your option) any later version. * |
| 16 | + * * |
| 17 | + ***************************************************************************/ |
| 18 | + |
| 19 | +#include "qgsapplication.h" |
| 20 | +#include "qgslayout.h" |
| 21 | +#include "qgsmultirenderchecker.h" |
| 22 | +#include "qgslayoutitemmap.h" |
| 23 | +#include "qgslayoutitemscalebar.h" |
| 24 | +#include "qgsmultibandcolorrenderer.h" |
| 25 | +#include "qgsrasterlayer.h" |
| 26 | +#include "qgsrasterdataprovider.h" |
| 27 | +#include "qgsfontutils.h" |
| 28 | +#include "qgsproperty.h" |
| 29 | +#include "qgsproject.h" |
| 30 | + |
| 31 | +#include <QLocale> |
| 32 | +#include <QObject> |
| 33 | +#include "qgstest.h" |
| 34 | + |
| 35 | +class TestQgsLayoutScaleBar : public QObject |
| 36 | +{ |
| 37 | + Q_OBJECT |
| 38 | + |
| 39 | + public: |
| 40 | + TestQgsLayoutScaleBar() = default; |
| 41 | + |
| 42 | + private slots: |
| 43 | + void initTestCase();// will be called before the first testfunction is executed. |
| 44 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 45 | + void init();// will be called before each testfunction is executed. |
| 46 | + void cleanup();// will be called after every testfunction. |
| 47 | + void singleBox(); |
| 48 | + void singleBoxAlpha(); |
| 49 | + void doubleBox(); |
| 50 | + void numeric(); |
| 51 | + void tick(); |
| 52 | + void dataDefined(); |
| 53 | + |
| 54 | + private: |
| 55 | + QString mReport; |
| 56 | +}; |
| 57 | + |
| 58 | +void TestQgsLayoutScaleBar::initTestCase() |
| 59 | +{ |
| 60 | + QgsApplication::init(); |
| 61 | + QgsApplication::initQgis(); |
| 62 | + |
| 63 | + // the scale denominator is formatted in a locale aware manner |
| 64 | + // so 10000 is rendered as "10,000" in C (or en_US) locale, however |
| 65 | + // other locales may render the number differently (e.g. "10 000" in cs_CZ) |
| 66 | + // so we enforce C locale to make sure we get expected result |
| 67 | + QLocale::setDefault( QLocale::c() ); |
| 68 | + |
| 69 | + //reproject to WGS84 |
| 70 | + QgsCoordinateReferenceSystem destCRS; |
| 71 | + destCRS.createFromId( 4326, QgsCoordinateReferenceSystem::EpsgCrsId ); |
| 72 | + QgsProject::instance()->setCrs( destCRS ); |
| 73 | + QgsProject::instance()->setEllipsoid( QStringLiteral( "WGS84" ) ); |
| 74 | + |
| 75 | + mReport = QStringLiteral( "<h1>Layout Scalebar Tests</h1>\n" ); |
| 76 | +} |
| 77 | + |
| 78 | +void TestQgsLayoutScaleBar::cleanupTestCase() |
| 79 | +{ |
| 80 | + QString myReportFile = QDir::tempPath() + "/qgistest.html"; |
| 81 | + QFile myFile( myReportFile ); |
| 82 | + if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) ) |
| 83 | + { |
| 84 | + QTextStream myQTextStream( &myFile ); |
| 85 | + myQTextStream << mReport; |
| 86 | + myFile.close(); |
| 87 | + } |
| 88 | + |
| 89 | + QgsApplication::exitQgis(); |
| 90 | +} |
| 91 | + |
| 92 | +void TestQgsLayoutScaleBar::init() |
| 93 | +{ |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +void TestQgsLayoutScaleBar::cleanup() |
| 98 | +{ |
| 99 | + |
| 100 | +} |
| 101 | + |
| 102 | +void TestQgsLayoutScaleBar::singleBox() |
| 103 | +{ |
| 104 | + QgsLayout l( QgsProject::instance() ); |
| 105 | + l.initializeDefaults(); |
| 106 | + QgsLayoutItemMap *map = new QgsLayoutItemMap( &l ); |
| 107 | + map->attemptSetSceneRect( QRectF( 20, 20, 150, 150 ) ); |
| 108 | + map->setFrameEnabled( true ); |
| 109 | + l.addLayoutItem( map ); |
| 110 | + map->setExtent( QgsRectangle( 17.923, 30.160, 18.023, 30.260 ) ); |
| 111 | + |
| 112 | + QgsLayoutItemScaleBar *scalebar = new QgsLayoutItemScaleBar( &l ); |
| 113 | + scalebar->attemptSetSceneRect( QRectF( 20, 180, 50, 20 ) ); |
| 114 | + l.addLayoutItem( scalebar ); |
| 115 | + scalebar->setMap( map ); |
| 116 | + scalebar->setFont( QgsFontUtils::getStandardTestFont() ); |
| 117 | + scalebar->setUnits( QgsUnitTypes::DistanceMeters ); |
| 118 | + scalebar->setUnitsPerSegment( 2000 ); |
| 119 | + scalebar->setNumberOfSegmentsLeft( 0 ); |
| 120 | + scalebar->setNumberOfSegments( 2 ); |
| 121 | + scalebar->setHeight( 5 ); |
| 122 | + scalebar->setLineWidth( 1.0 ); |
| 123 | + |
| 124 | + scalebar->setStyle( QStringLiteral( "Single Box" ) ); |
| 125 | + QgsLayoutChecker checker( QStringLiteral( "layoutscalebar_singlebox" ), &l ); |
| 126 | + checker.setControlPathPrefix( QStringLiteral( "layout_scalebar" ) ); |
| 127 | + QVERIFY( checker.testLayout( mReport, 0, 0 ) ); |
| 128 | +} |
| 129 | + |
| 130 | +void TestQgsLayoutScaleBar::singleBoxAlpha() |
| 131 | +{ |
| 132 | + QgsLayout l( QgsProject::instance() ); |
| 133 | + l.initializeDefaults(); |
| 134 | + QgsLayoutItemMap *map = new QgsLayoutItemMap( &l ); |
| 135 | + map->attemptSetSceneRect( QRectF( 20, 20, 150, 150 ) ); |
| 136 | + map->setFrameEnabled( true ); |
| 137 | + l.addLayoutItem( map ); |
| 138 | + map->setExtent( QgsRectangle( 17.923, 30.160, 18.023, 30.260 ) ); |
| 139 | + |
| 140 | + QgsLayoutItemScaleBar *scalebar = new QgsLayoutItemScaleBar( &l ); |
| 141 | + scalebar->attemptSetSceneRect( QRectF( 20, 180, 50, 20 ) ); |
| 142 | + l.addLayoutItem( scalebar ); |
| 143 | + scalebar->setMap( map ); |
| 144 | + scalebar->setFont( QgsFontUtils::getStandardTestFont() ); |
| 145 | + scalebar->setUnits( QgsUnitTypes::DistanceMeters ); |
| 146 | + scalebar->setUnitsPerSegment( 2000 ); |
| 147 | + scalebar->setNumberOfSegmentsLeft( 0 ); |
| 148 | + scalebar->setNumberOfSegments( 2 ); |
| 149 | + scalebar->setHeight( 5 ); |
| 150 | + scalebar->setLineWidth( 1.0 ); |
| 151 | + |
| 152 | + scalebar->setStyle( QStringLiteral( "Single Box" ) ); |
| 153 | + scalebar->setFillColor( QColor( 255, 0, 0, 100 ) ); |
| 154 | + scalebar->setFillColor2( QColor( 0, 255, 0, 50 ) ); |
| 155 | + scalebar->setLineColor( QColor( 0, 0, 255, 150 ) ); |
| 156 | + scalebar->setFontColor( QColor( 255, 0, 255, 100 ) ); |
| 157 | + scalebar->setLineWidth( 1.0 ); |
| 158 | + QgsLayoutChecker checker( QStringLiteral( "layoutscalebar_singlebox_alpha" ), &l ); |
| 159 | + checker.setControlPathPrefix( QStringLiteral( "layout_scalebar" ) ); |
| 160 | + QVERIFY( checker.testLayout( mReport, 0, 0 ) ); |
| 161 | +} |
| 162 | + |
| 163 | +void TestQgsLayoutScaleBar::doubleBox() |
| 164 | +{ |
| 165 | + QgsLayout l( QgsProject::instance() ); |
| 166 | + l.initializeDefaults(); |
| 167 | + QgsLayoutItemMap *map = new QgsLayoutItemMap( &l ); |
| 168 | + map->attemptSetSceneRect( QRectF( 20, 20, 150, 150 ) ); |
| 169 | + map->setFrameEnabled( true ); |
| 170 | + l.addLayoutItem( map ); |
| 171 | + map->setExtent( QgsRectangle( 17.923, 30.160, 18.023, 30.260 ) ); |
| 172 | + |
| 173 | + QgsLayoutItemScaleBar *scalebar = new QgsLayoutItemScaleBar( &l ); |
| 174 | + scalebar->attemptSetSceneRect( QRectF( 20, 180, 50, 20 ) ); |
| 175 | + l.addLayoutItem( scalebar ); |
| 176 | + scalebar->setMap( map ); |
| 177 | + scalebar->setFont( QgsFontUtils::getStandardTestFont() ); |
| 178 | + scalebar->setUnits( QgsUnitTypes::DistanceMeters ); |
| 179 | + scalebar->setUnitsPerSegment( 2000 ); |
| 180 | + scalebar->setNumberOfSegmentsLeft( 0 ); |
| 181 | + scalebar->setNumberOfSegments( 2 ); |
| 182 | + scalebar->setHeight( 5 ); |
| 183 | + scalebar->setLineWidth( 1.0 ); |
| 184 | + |
| 185 | + scalebar->setFillColor( Qt::black ); |
| 186 | + scalebar->setFillColor2( Qt::white ); |
| 187 | + scalebar->setLineColor( Qt::black ); |
| 188 | + scalebar->setLineWidth( 1.0 ); |
| 189 | + scalebar->setFontColor( Qt::black ); |
| 190 | + scalebar->setStyle( QStringLiteral( "Double Box" ) ); |
| 191 | + |
| 192 | + QgsLayoutChecker checker( QStringLiteral( "layoutscalebar_doublebox" ), &l ); |
| 193 | + checker.setControlPathPrefix( QStringLiteral( "layout_scalebar" ) ); |
| 194 | + QVERIFY( checker.testLayout( mReport, 0, 0 ) ); |
| 195 | +} |
| 196 | + |
| 197 | +void TestQgsLayoutScaleBar::numeric() |
| 198 | +{ |
| 199 | + QgsLayout l( QgsProject::instance() ); |
| 200 | + l.initializeDefaults(); |
| 201 | + QgsLayoutItemMap *map = new QgsLayoutItemMap( &l ); |
| 202 | + map->attemptSetSceneRect( QRectF( 20, 20, 150, 150 ) ); |
| 203 | + map->setFrameEnabled( true ); |
| 204 | + l.addLayoutItem( map ); |
| 205 | + map->setExtent( QgsRectangle( 17.923, 30.160, 18.023, 30.260 ) ); |
| 206 | + |
| 207 | + QgsLayoutItemScaleBar *scalebar = new QgsLayoutItemScaleBar( &l ); |
| 208 | + scalebar->attemptSetSceneRect( QRectF( 20, 180, 50, 20 ) ); |
| 209 | + l.addLayoutItem( scalebar ); |
| 210 | + scalebar->setMap( map ); |
| 211 | + scalebar->setFont( QgsFontUtils::getStandardTestFont() ); |
| 212 | + scalebar->setUnits( QgsUnitTypes::DistanceMeters ); |
| 213 | + scalebar->setUnitsPerSegment( 2000 ); |
| 214 | + scalebar->setNumberOfSegmentsLeft( 0 ); |
| 215 | + scalebar->setNumberOfSegments( 2 ); |
| 216 | + scalebar->setHeight( 5 ); |
| 217 | + scalebar->setLineWidth( 1.0 ); |
| 218 | + |
| 219 | + QFont newFont = QgsFontUtils::getStandardTestFont( QStringLiteral( "Bold" ) ); |
| 220 | + newFont.setPointSizeF( 12 ); |
| 221 | + scalebar->setFont( newFont ); |
| 222 | + |
| 223 | + scalebar->setStyle( QStringLiteral( "Numeric" ) ); |
| 224 | + QgsLayoutChecker checker( QStringLiteral( "layoutscalebar_numeric" ), &l ); |
| 225 | + checker.setControlPathPrefix( QStringLiteral( "layout_scalebar" ) ); |
| 226 | + bool result = checker.testLayout( mReport, 0, 0 ); |
| 227 | + QVERIFY( result ); |
| 228 | +} |
| 229 | + |
| 230 | +void TestQgsLayoutScaleBar::tick() |
| 231 | +{ |
| 232 | + QgsLayout l( QgsProject::instance() ); |
| 233 | + l.initializeDefaults(); |
| 234 | + QgsLayoutItemMap *map = new QgsLayoutItemMap( &l ); |
| 235 | + map->attemptSetSceneRect( QRectF( 20, 20, 150, 150 ) ); |
| 236 | + map->setFrameEnabled( true ); |
| 237 | + l.addLayoutItem( map ); |
| 238 | + map->setExtent( QgsRectangle( 17.923, 30.160, 18.023, 30.260 ) ); |
| 239 | + |
| 240 | + QgsLayoutItemScaleBar *scalebar = new QgsLayoutItemScaleBar( &l ); |
| 241 | + scalebar->attemptSetSceneRect( QRectF( 20, 180, 50, 20 ) ); |
| 242 | + l.addLayoutItem( scalebar ); |
| 243 | + scalebar->setMap( map ); |
| 244 | + scalebar->setFont( QgsFontUtils::getStandardTestFont() ); |
| 245 | + scalebar->setUnits( QgsUnitTypes::DistanceMeters ); |
| 246 | + scalebar->setUnitsPerSegment( 2000 ); |
| 247 | + scalebar->setNumberOfSegmentsLeft( 0 ); |
| 248 | + scalebar->setNumberOfSegments( 2 ); |
| 249 | + scalebar->setHeight( 5 ); |
| 250 | + scalebar->setLineWidth( 1.0 ); |
| 251 | + |
| 252 | + scalebar->setStyle( QStringLiteral( "Line Ticks Up" ) ); |
| 253 | + scalebar->setLineWidth( 1.0 ); |
| 254 | + QgsLayoutChecker checker( QStringLiteral( "layoutscalebar_tick" ), &l ); |
| 255 | + checker.setControlPathPrefix( QStringLiteral( "layout_scalebar" ) ); |
| 256 | + QVERIFY( checker.testLayout( mReport, 0, 0 ) ); |
| 257 | +} |
| 258 | + |
| 259 | +void TestQgsLayoutScaleBar::dataDefined() |
| 260 | +{ |
| 261 | + QgsLayout l( QgsProject::instance() ); |
| 262 | + QgsLayoutItemScaleBar *scalebar = new QgsLayoutItemScaleBar( &l ); |
| 263 | + l.addLayoutItem( scalebar ); |
| 264 | + |
| 265 | + scalebar->dataDefinedProperties().setProperty( QgsLayoutObject::ScalebarFillColor, QgsProperty::fromExpression( QStringLiteral( "'red'" ) ) ); |
| 266 | + scalebar->dataDefinedProperties().setProperty( QgsLayoutObject::ScalebarFillColor2, QgsProperty::fromExpression( QStringLiteral( "'blue'" ) ) ); |
| 267 | + scalebar->dataDefinedProperties().setProperty( QgsLayoutObject::ScalebarLineColor, QgsProperty::fromExpression( QStringLiteral( "'yellow'" ) ) ); |
| 268 | + scalebar->dataDefinedProperties().setProperty( QgsLayoutObject::ScalebarLineWidth, QgsProperty::fromExpression( QStringLiteral( "1.2" ) ) ); |
| 269 | + scalebar->refreshDataDefinedProperty(); |
| 270 | + QCOMPARE( scalebar->brush().color().name(), QColor( 255, 0, 0 ).name() ); |
| 271 | + QCOMPARE( scalebar->brush2().color().name(), QColor( 0, 0, 255 ).name() ); |
| 272 | + QCOMPARE( scalebar->pen().color().name(), QColor( 255, 255, 0 ).name() ); |
| 273 | + QCOMPARE( scalebar->pen().widthF(), 1.2 ); |
| 274 | +} |
| 275 | + |
| 276 | +QGSTEST_MAIN( TestQgsLayoutScaleBar ) |
| 277 | +#include "testqgslayoutscalebar.moc" |
0 commit comments