PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/src/gui/testqgsquickprint.cpp

https://github.com/linz/Quantum-GIS
C++ | 175 lines | 97 code | 15 blank | 63 comment | 0 complexity | 34bf70d23fed37975425d83eb8e3ec27 MD5 | raw file
  1. /***************************************************************************
  2. testqgsquickprint.cpp
  3. --------------------------------------
  4. Date : 20 Jan 2008
  5. Copyright : (C) 2008 by Tim Sutton
  6. Email : tim @ linfiniti.com
  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 <QStringList>
  18. #include <QObject>
  19. #include <QApplication>
  20. #include <QFileInfo>
  21. #include <QDir>
  22. #include <QDesktopServices>
  23. #include <iostream>
  24. //qgis includes...
  25. #include <qgsmaprenderer.h>
  26. #include <qgsmaplayer.h>
  27. #include <qgsvectorlayer.h>
  28. #include <qgsapplication.h>
  29. #include <qgsproviderregistry.h>
  30. #include <qgsmaplayerregistry.h>
  31. #include <qgsquickprint.h>
  32. //qgis test includes
  33. #include <qgsrenderchecker.h>
  34. /** \ingroup UnitTests
  35. * This is a unit test for the different renderers for vector layers.
  36. */
  37. class TestQgsQuickPrint: public QObject
  38. {
  39. Q_OBJECT;
  40. private slots:
  41. void initTestCase();// will be called before the first testfunction is executed.
  42. void cleanupTestCase();// will be called after the last testfunction was executed.
  43. void init(){};// will be called before each testfunction is executed.
  44. void cleanup(){};// will be called after every testfunction.
  45. void basicMapTest();
  46. private:
  47. bool imageCheck(QString theType); //as above
  48. QgsMapRenderer * mpMapRenderer;
  49. QgsMapLayer * mpPointsLayer;
  50. QgsMapLayer * mpLinesLayer;
  51. QgsMapLayer * mpPolysLayer;
  52. QString mTestDataDir;
  53. QString mReport;
  54. };
  55. void TestQgsQuickPrint::initTestCase()
  56. {
  57. //
  58. // Runs once before any tests are run
  59. //
  60. // init QGIS's paths - true means that all path will be inited from prefix
  61. QString qgisPath = QCoreApplication::applicationDirPath ();
  62. QgsApplication::setPrefixPath(INSTALL_PREFIX, true);
  63. QgsApplication::showSettings();
  64. // Instantiate the plugin directory so that providers are loaded
  65. QgsProviderRegistry::instance(QgsApplication::pluginPath());
  66. //
  67. //create a point layer that will be used in all tests...
  68. //
  69. QString myDataDir (TEST_DATA_DIR); //defined in CmakeLists.txt
  70. mTestDataDir = myDataDir + QDir::separator();
  71. QString myPointsFileName = mTestDataDir + "points.shp";
  72. QFileInfo myPointFileInfo ( myPointsFileName );
  73. mpPointsLayer = new QgsVectorLayer ( myPointFileInfo.filePath(),
  74. myPointFileInfo.completeBaseName(), "ogr" );
  75. // Register the layer with the registry
  76. QgsMapLayerRegistry::instance()->addMapLayer(mpPointsLayer);
  77. //
  78. //create a poly layer that will be used in all tests...
  79. //
  80. QString myPolysFileName = mTestDataDir + "polys.shp";
  81. QFileInfo myPolyFileInfo ( myPolysFileName );
  82. mpPolysLayer = new QgsVectorLayer ( myPolyFileInfo.filePath(),
  83. myPolyFileInfo.completeBaseName(), "ogr" );
  84. // Register the layer with the registry
  85. QgsMapLayerRegistry::instance()->addMapLayer(mpPolysLayer);
  86. //
  87. // Create a line layer that will be used in all tests...
  88. //
  89. QString myLinesFileName = mTestDataDir + "lines.shp";
  90. QFileInfo myLineFileInfo ( myLinesFileName );
  91. mpLinesLayer = new QgsVectorLayer ( myLineFileInfo.filePath(),
  92. myLineFileInfo.completeBaseName(), "ogr" );
  93. // Register the layer with the registry
  94. QgsMapLayerRegistry::instance()->addMapLayer(mpLinesLayer);
  95. //
  96. // We only need maprender instead of mapcanvas
  97. // since maprender does not require a qui
  98. // and is more light weight
  99. //
  100. mpMapRenderer = new QgsMapRenderer();
  101. QStringList myLayers;
  102. myLayers << mpPointsLayer->getLayerID();
  103. myLayers << mpPolysLayer->getLayerID();
  104. myLayers << mpLinesLayer->getLayerID();
  105. mpMapRenderer->setLayerSet(myLayers);
  106. mpMapRenderer->setExtent(mpPointsLayer->extent());
  107. mReport+= "<h1>QuickPrint Tests</h1>\n";
  108. }
  109. void TestQgsQuickPrint::cleanupTestCase()
  110. {
  111. /*
  112. QString myReportFile = QDir::tempPath() + QDir::separator() + "quickprinttest.html";
  113. QFile myFile ( myReportFile);
  114. if ( myFile.open ( QIODevice::WriteOnly ) )
  115. {
  116. QTextStream myQTextStream ( &myFile );
  117. myQTextStream << mReport;
  118. myFile.close();
  119. QDesktopServices::openUrl("file://"+myReportFile);
  120. }
  121. */
  122. }
  123. void TestQgsQuickPrint::basicMapTest()
  124. {
  125. //make the legends really long so we can test
  126. //word wrapping
  127. mpPointsLayer->setLayerName("This is a very very very long name it should word wrap");
  128. mpPolysLayer->setLayerName("This is a very very very long name it should also word wrap");
  129. mpLinesLayer->setLayerName("This is a very very very very long name it should word wrap");
  130. //now print the map
  131. QgsQuickPrint myQuickPrint;
  132. myQuickPrint.setMapBackgroundColor ( Qt::cyan );
  133. myQuickPrint.setOutputPdf (QDir::tempPath() + QDir::separator() + "quickprinttest.pdf");
  134. myQuickPrint.setMapRenderer (mpMapRenderer);
  135. myQuickPrint.setTitle ("Map Title");
  136. myQuickPrint.setName ("Map Name");
  137. myQuickPrint.setCopyright ("Copyright Text");
  138. //void setNorthArrow(QString theFileName);
  139. //void setLogo1(QString theFileName);
  140. //void setLogo2(QString theFileName);
  141. myQuickPrint.printMap();
  142. }
  143. //
  144. // Helper functions below
  145. //
  146. bool TestQgsQuickPrint::imageCheck(QString theTestType)
  147. {
  148. //use the QgsRenderChecker test utility class to
  149. //ensure the rendered output matches our control image
  150. mpMapRenderer->setExtent(mpPointsLayer->extent());
  151. QString myExpectedImage = mTestDataDir + "expected_" + theTestType + ".png";
  152. QgsRenderChecker myChecker;
  153. myChecker.setExpectedImage ( myExpectedImage );
  154. myChecker.setMapRenderer ( mpMapRenderer );
  155. bool myResultFlag = myChecker.runTest( theTestType );
  156. mReport += myChecker.report();
  157. return myResultFlag;
  158. }
  159. QTEST_MAIN(TestQgsQuickPrint)
  160. #include "moc_testqgsquickprint.cxx"