forked from planetfederal/QGIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestqgsgradients.cpp
293 lines (262 loc) · 10.5 KB
/
testqgsgradients.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/***************************************************************************
testqgsgradients.cpp
--------------------------------------
Date : 20 Jan 2008
Copyright : (C) 2008 by Tim Sutton
Email : tim @ linfiniti.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QtTest>
#include <QObject>
#include <QString>
#include <QStringList>
#include <QObject>
#include <QApplication>
#include <QFileInfo>
#include <QDir>
#include <QDesktopServices>
#include <iostream>
//qgis includes...
#include <qgsmaprenderer.h>
#include <qgsmaplayer.h>
#include <qgsvectorlayer.h>
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgsmaplayerregistry.h>
#include <qgssymbolv2.h>
#include <qgssinglesymbolrendererv2.h>
#include <qgsfillsymbollayerv2.h>
#include <qgsvectorcolorrampv2.h>
//qgis test includes
#include "qgsrenderchecker.h"
/** \ingroup UnitTests
* This is a unit test for gradient fill types.
*/
class TestQgsGradients: public QObject
{
Q_OBJECT;
private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init() {};// will be called before each testfunction is executed.
void cleanup() {};// will be called after every testfunction.
void gradientSymbol();
void gradientSymbolColors();
void gradientSymbolRamp();
void gradientSymbolRadial();
void gradientSymbolConical();
void gradientSymbolViewport();
void gradientSymbolReferencePoints();
void gradientSymbolCentroid();
void gradientSymbolReflectSpread();
void gradientSymbolRepeatSpread();
void gradientSymbolRotate();
void gradientSymbolFromQml();
private:
bool mTestHasError;
bool setQml( QString theType );
bool imageCheck( QString theType );
QgsMapRenderer * mpMapRenderer;
QgsVectorLayer * mpPolysLayer;
QgsGradientFillSymbolLayerV2* mGradientFill;
QgsFillSymbolV2* mFillSymbol;
QgsSingleSymbolRendererV2* mSymbolRenderer;
QString mTestDataDir;
QString mReport;
};
void TestQgsGradients::initTestCase()
{
mTestHasError = false;
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();
QgsApplication::showSettings();
//create some objects that will be used in all tests...
QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
mTestDataDir = myDataDir + QDir::separator();
//
//create a poly layer that will be used in all tests...
//
QString myPolysFileName = mTestDataDir + "polys.shp";
QFileInfo myPolyFileInfo( myPolysFileName );
mpPolysLayer = new QgsVectorLayer( myPolyFileInfo.filePath(),
myPolyFileInfo.completeBaseName(), "ogr" );
QgsVectorSimplifyMethod simplifyMethod;
simplifyMethod.setSimplifyHints( QgsVectorLayer::NoSimplification );
mpPolysLayer->setSimplifyMethod( simplifyMethod );
// Register the layer with the registry
QgsMapLayerRegistry::instance()->addMapLayers(
QList<QgsMapLayer *>() << mpPolysLayer );
//setup gradient fill
mGradientFill = new QgsGradientFillSymbolLayerV2();
mFillSymbol = new QgsFillSymbolV2();
mFillSymbol->changeSymbolLayer( 0, mGradientFill );
mSymbolRenderer = new QgsSingleSymbolRendererV2( mFillSymbol );
mpPolysLayer->setRendererV2( mSymbolRenderer );
// We only need maprender instead of mapcanvas
// since maprender does not require a qui
// and is more light weight
//
mpMapRenderer = new QgsMapRenderer();
QStringList myLayers;
myLayers << mpPolysLayer->id();
mpMapRenderer->setLayerSet( myLayers );
mReport += "<h1>Gradient Renderer Tests</h1>\n";
}
void TestQgsGradients::cleanupTestCase()
{
QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
}
}
void TestQgsGradients::gradientSymbol()
{
mReport += "<h2>Gradient symbol renderer test</h2>\n";
mGradientFill->setColor( QColor( "red" ) );
mGradientFill->setColor2( QColor( "blue" ) );
mGradientFill->setGradientType( QgsGradientFillSymbolLayerV2::Linear );
mGradientFill->setGradientColorType( QgsGradientFillSymbolLayerV2::SimpleTwoColor );
mGradientFill->setCoordinateMode( QgsGradientFillSymbolLayerV2::Feature );
mGradientFill->setGradientSpread( QgsGradientFillSymbolLayerV2::Pad );
mGradientFill->setReferencePoint1( QPointF( 0, 0 ) );
mGradientFill->setReferencePoint2( QPointF( 1, 1 ) );
QVERIFY( imageCheck( "gradient" ) );
}
void TestQgsGradients::gradientSymbolColors()
{
mReport += "<h2>Gradient symbol renderer color test</h2>\n";
mGradientFill->setColor( QColor( "green" ) );
mGradientFill->setColor2( QColor( "white" ) );
QVERIFY( imageCheck( "gradient_colors" ) );
mGradientFill->setColor( QColor( "red" ) );
mGradientFill->setColor2( QColor( "blue" ) );
}
void TestQgsGradients::gradientSymbolRamp()
{
QgsVectorGradientColorRampV2* gradientRamp = new QgsVectorGradientColorRampV2( QColor( Qt::red ), QColor( Qt::blue ) );
QgsGradientStopsList stops;
stops.append( QgsGradientStop( 0.5, QColor( Qt::white ) ) );
gradientRamp->setStops( stops );
mGradientFill->setColorRamp( gradientRamp );
mGradientFill->setGradientColorType( QgsGradientFillSymbolLayerV2::ColorRamp );
QVERIFY( imageCheck( "gradient_ramp" ) );
mGradientFill->setGradientColorType( QgsGradientFillSymbolLayerV2::SimpleTwoColor );
}
void TestQgsGradients::gradientSymbolRadial()
{
mReport += "<h2>Gradient symbol renderer radial test</h2>\n";
mGradientFill->setGradientType( QgsGradientFillSymbolLayerV2::Radial );
QVERIFY( imageCheck( "gradient_radial" ) );
mGradientFill->setGradientType( QgsGradientFillSymbolLayerV2::Linear );
}
void TestQgsGradients::gradientSymbolConical()
{
mReport += "<h2>Gradient symbol renderer conical test</h2>\n";
mGradientFill->setGradientType( QgsGradientFillSymbolLayerV2::Conical );
mGradientFill->setReferencePoint1( QPointF( 0.5, 0.5 ) );
QVERIFY( imageCheck( "gradient_conical" ) );
mGradientFill->setReferencePoint1( QPointF( 0, 0 ) );
mGradientFill->setGradientType( QgsGradientFillSymbolLayerV2::Linear );
}
void TestQgsGradients::gradientSymbolViewport()
{
mReport += "<h2>Gradient symbol renderer viewport test</h2>\n";
mGradientFill->setCoordinateMode( QgsGradientFillSymbolLayerV2::Viewport );
QVERIFY( imageCheck( "gradient_viewport" ) );
mGradientFill->setCoordinateMode( QgsGradientFillSymbolLayerV2::Feature );
}
void TestQgsGradients::gradientSymbolReferencePoints()
{
mReport += "<h2>Gradient symbol renderer reference points test</h2>\n";
mGradientFill->setReferencePoint1( QPointF( 0.5, 0.4 ) );
mGradientFill->setReferencePoint2( QPointF( 0, 0.2 ) );
QVERIFY( imageCheck( "gradient_refpoints" ) );
mGradientFill->setReferencePoint1( QPointF( 0, 0 ) );
mGradientFill->setReferencePoint2( QPointF( 1, 1 ) );
}
void TestQgsGradients::gradientSymbolCentroid()
{
mReport += "<h2>Gradient symbol renderer centroid reference point test</h2>\n";
mGradientFill->setReferencePoint1IsCentroid( true );
QVERIFY( imageCheck( "gradient_ref1centroid" ) );
mGradientFill->setReferencePoint1IsCentroid( false );
mGradientFill->setReferencePoint2IsCentroid( true );
QVERIFY( imageCheck( "gradient_ref2centroid" ) );
mGradientFill->setReferencePoint2IsCentroid( false );
}
void TestQgsGradients::gradientSymbolReflectSpread()
{
mReport += "<h2>Gradient symbol renderer reflect spread test</h2>\n";
mGradientFill->setReferencePoint2( QPointF( 0.5, 0.5 ) );
mGradientFill->setGradientSpread( QgsGradientFillSymbolLayerV2::Reflect );
QVERIFY( imageCheck( "gradient_reflect" ) );
mGradientFill->setGradientSpread( QgsGradientFillSymbolLayerV2::Pad );
mGradientFill->setReferencePoint2( QPointF( 1, 1 ) );
}
void TestQgsGradients::gradientSymbolRepeatSpread()
{
mReport += "<h2>Gradient symbol renderer repeat spread test</h2>\n";
mGradientFill->setReferencePoint2( QPointF( 0.5, 0.5 ) );
mGradientFill->setGradientSpread( QgsGradientFillSymbolLayerV2::Repeat );
QVERIFY( imageCheck( "gradient_repeat" ) );
mGradientFill->setGradientSpread( QgsGradientFillSymbolLayerV2::Pad );
mGradientFill->setReferencePoint2( QPointF( 1, 1 ) );
}
void TestQgsGradients::gradientSymbolRotate()
{
mReport += "<h2>Gradient symbol renderer rotate test</h2>\n";
mGradientFill->setAngle( 90 );
QVERIFY( imageCheck( "gradient_rotate" ) );
mGradientFill->setAngle( 0 );
}
void TestQgsGradients::gradientSymbolFromQml()
{
mReport += "<h2>Gradient symbol from QML test</h2>\n";
QVERIFY( setQml( "gradient" ) );
QgsVectorSimplifyMethod simplifyMethod;
simplifyMethod.setSimplifyHints( QgsVectorLayer::NoSimplification );
mpPolysLayer->setSimplifyMethod( simplifyMethod );
QVERIFY( imageCheck( "gradient_from_qml" ) );
}
//
// Private helper functions not called directly by CTest
//
bool TestQgsGradients::setQml( QString theType )
{
//load a qml style and apply to our layer
//the style will correspond to the renderer
//type we are testing
QString myFileName = mTestDataDir + "polys_" + theType + "_symbol.qml";
bool myStyleFlag = false;
QString error = mpPolysLayer->loadNamedStyle( myFileName, myStyleFlag );
if ( !myStyleFlag )
{
qDebug( "%s", error.toLocal8Bit().constData() );
}
return myStyleFlag;
}
bool TestQgsGradients::imageCheck( QString theTestType )
{
//use the QgsRenderChecker test utility class to
//ensure the rendered output matches our control image
mpMapRenderer->setExtent( mpPolysLayer->extent() );
QgsRenderChecker myChecker;
myChecker.setControlName( "expected_" + theTestType );
myChecker.setMapRenderer( mpMapRenderer );
bool myResultFlag = myChecker.runTest( theTestType );
mReport += myChecker.report();
return myResultFlag;
}
QTEST_MAIN( TestQgsGradients )
#include "moc_testqgsgradients.cxx"