|
| 1 | +/*************************************************************************** |
| 2 | + testqgsdiagram.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : Sep 7 2012 |
| 5 | + Copyright : (C) 2012 by Matthias Kuhn |
| 6 | + Email : matthias dot kuhn at gmx dot ch |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | +#include <QtTest> |
| 16 | +#include <QObject> |
| 17 | +#include <QString> |
| 18 | +#include <QStringList> |
| 19 | +#include <QObject> |
| 20 | +#include <QApplication> |
| 21 | +#include <QFileInfo> |
| 22 | +#include <QDir> |
| 23 | +#include <QDesktopServices> |
| 24 | +#include <QPainter> |
| 25 | + |
| 26 | +#include <iostream> |
| 27 | +//qgis includes... |
| 28 | +// #include <qgisapp.h> |
| 29 | +#include <diagram/qgspiediagram.h> |
| 30 | +#include <qgsdiagramrendererv2.h> |
| 31 | +#include <qgscomposition.h> |
| 32 | +#include <qgscompositionchecker.h> |
| 33 | +#include <qgscomposermap.h> |
| 34 | +#include <qgsmaprenderer.h> |
| 35 | +#include <qgsmaplayer.h> |
| 36 | +#include <qgsvectordataprovider.h> |
| 37 | +#include <qgsvectorlayer.h> |
| 38 | +#include <qgsapplication.h> |
| 39 | +#include <qgsproviderregistry.h> |
| 40 | +#include <qgsmaplayerregistry.h> |
| 41 | +#include <qgsrendererv2.h> |
| 42 | +//qgis test includes |
| 43 | +#include "qgsrenderchecker.h" |
| 44 | + |
| 45 | +/** \ingroup UnitTests |
| 46 | + * This is a unit test for the vector layer class. |
| 47 | + */ |
| 48 | +class TestQgsDiagram: public QObject |
| 49 | +{ |
| 50 | + Q_OBJECT; |
| 51 | + private: |
| 52 | + bool mTestHasError; |
| 53 | + QgsMapRenderer * mMapRenderer; |
| 54 | + QgsVectorLayer * mPointsLayer; |
| 55 | + QgsComposition * mComposition; |
| 56 | + QString mTestDataDir; |
| 57 | + QString mReport; |
| 58 | + QgsPieDiagram * mPieDiagram; |
| 59 | + QgsComposerMap * mComposerMap; |
| 60 | + |
| 61 | + private slots: |
| 62 | + |
| 63 | + |
| 64 | + // will be called before the first testfunction is executed. |
| 65 | + void initTestCase() |
| 66 | + { |
| 67 | + mTestHasError = false; |
| 68 | + QgsApplication::init(); |
| 69 | + QgsApplication::initQgis(); |
| 70 | + QgsApplication::showSettings(); |
| 71 | + |
| 72 | + //create some objects that will be used in all tests... |
| 73 | + |
| 74 | + // |
| 75 | + //create a non spatial layer that will be used in all tests... |
| 76 | + // |
| 77 | + QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt |
| 78 | + mTestDataDir = myDataDir + QDir::separator(); |
| 79 | + |
| 80 | + // |
| 81 | + //create a point layer that will be used in all tests... |
| 82 | + // |
| 83 | + QString myPointsFileName = mTestDataDir + "points.shp"; |
| 84 | + QFileInfo myPointFileInfo( myPointsFileName ); |
| 85 | + mPointsLayer = new QgsVectorLayer( myPointFileInfo.filePath(), |
| 86 | + myPointFileInfo.completeBaseName(), "ogr" ); |
| 87 | + |
| 88 | + // Register the layer with the registry |
| 89 | + QgsMapLayerRegistry::instance()->addMapLayers( |
| 90 | + QList<QgsMapLayer *>() << mPointsLayer ); |
| 91 | + |
| 92 | + // Create diagrams |
| 93 | + mPieDiagram = new QgsPieDiagram(); |
| 94 | + |
| 95 | + // Create map composition to draw on |
| 96 | + mMapRenderer = new QgsMapRenderer(); |
| 97 | + mMapRenderer->setLayerSet( QStringList() << mPointsLayer->id() ); |
| 98 | + mMapRenderer->setLabelingEngine( new QgsPalLabeling() ); |
| 99 | + mComposition = new QgsComposition( mMapRenderer ); |
| 100 | + mComposition->setPaperSize( 297, 210 ); // A4 landscape |
| 101 | + mComposerMap = new QgsComposerMap( mComposition, 20, 20, 200, 100 ); |
| 102 | + mComposerMap->setFrameEnabled( true ); |
| 103 | + mComposition->addComposerMap( mComposerMap ); |
| 104 | + |
| 105 | + mReport += "<h1>Diagram Tests</h1>\n"; |
| 106 | + } |
| 107 | + |
| 108 | + // will be called after the last testfunction was executed. |
| 109 | + void cleanupTestCase() |
| 110 | + { |
| 111 | + QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html"; |
| 112 | + QFile myFile( myReportFile ); |
| 113 | + if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) ) |
| 114 | + { |
| 115 | + QTextStream myQTextStream( &myFile ); |
| 116 | + myQTextStream << mReport; |
| 117 | + myFile.close(); |
| 118 | + //QDesktopServices::openUrl( "file:///" + myReportFile ); |
| 119 | + } |
| 120 | + |
| 121 | + delete mComposerMap; |
| 122 | + delete mComposition; |
| 123 | + delete mMapRenderer; |
| 124 | + delete mPointsLayer; |
| 125 | + } |
| 126 | + void init() {};// will be called before each testfunction is executed. |
| 127 | + void cleanup() {};// will be called after every testfunction. |
| 128 | + |
| 129 | + void testPieDiagram() |
| 130 | + { |
| 131 | + QgsDiagramSettings ds; |
| 132 | + QColor col1 = Qt::red; |
| 133 | + QColor col2 = Qt::yellow; |
| 134 | + col1.setAlphaF( 0.5 ); |
| 135 | + col2.setAlphaF( 0.5 ); |
| 136 | + ds.categoryColors = QList<QColor>() << col1 << col2; |
| 137 | + ds.categoryIndices = QList<int>() << 3 << 4; // Pilots / Cabin Crew |
| 138 | + ds.maxScaleDenominator = -1; |
| 139 | + ds.minScaleDenominator = -1; |
| 140 | + ds.minimumSize = 0; |
| 141 | + ds.penColor = Qt::green; |
| 142 | + ds.penWidth = .5; |
| 143 | + ds.scaleByArea = true; |
| 144 | + ds.sizeType = QgsDiagramSettings::MM; |
| 145 | + ds.size = QSizeF( 15, 15 ); |
| 146 | + |
| 147 | + |
| 148 | + QgsLinearlyInterpolatedDiagramRenderer *dr = new QgsLinearlyInterpolatedDiagramRenderer(); |
| 149 | + dr->setLowerValue( 0.0 ); |
| 150 | + dr->setLowerSize( QSizeF( 0.0, 0.0 ) ); |
| 151 | + dr->setUpperValue( 10 ); |
| 152 | + dr->setUpperSize( QSizeF( 100, 100 ) ); |
| 153 | + dr->setClassificationAttribute( 5 ); // Staff |
| 154 | + dr->setDiagram( mPieDiagram ); |
| 155 | + dr->setDiagramSettings( ds ); |
| 156 | + mPointsLayer->setDiagramRenderer( dr ); |
| 157 | + |
| 158 | + QgsDiagramLayerSettings dls = QgsDiagramLayerSettings(); |
| 159 | + dls.placement = QgsDiagramLayerSettings::OverPoint; |
| 160 | + dls.renderer = dr; |
| 161 | + |
| 162 | + dynamic_cast<QgsPalLabeling*> (mMapRenderer->labelingEngine())->setShowingAllLabels( true ); |
| 163 | + |
| 164 | + mPointsLayer->setDiagramLayerSettings( dls ); |
| 165 | + |
| 166 | + mComposerMap->setNewExtent( QgsRectangle( -122, -79, -70, 47 ) ); |
| 167 | + QgsCompositionChecker checker( "Composer map render", mComposition, QString( QString( TEST_DATA_DIR ) |
| 168 | + + QDir::separator() + "control_images" + QDir::separator() + "expected_diagram" + QDir::separator() |
| 169 | + + "expected_piediagram.png" ) ); |
| 170 | + |
| 171 | + QVERIFY( checker.testComposition() ); |
| 172 | + |
| 173 | + mPointsLayer->setDiagramRenderer( 0 ); |
| 174 | + } |
| 175 | +}; |
| 176 | + |
| 177 | +QTEST_MAIN( TestQgsDiagram ) |
| 178 | +#include "moc_testqgsdiagram.cxx" |
0 commit comments