Skip to content

Commit e39c216

Browse files
committed
add tests for cpt-city color ramps
1 parent bd160be commit e39c216

File tree

7 files changed

+99
-31
lines changed

7 files changed

+99
-31
lines changed

src/core/symbology-ng/qgsvectorcolorrampv2.cpp

+37-9
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ TODO
260260
- fix ocal file parsing
261261
- re-organize and rename colorramp classes and widgets
262262
*/
263+
QString QgsCptCityColorRampV2::mBaseDir;
263264
QStringList QgsCptCityColorRampV2::mCollections;
264265
QMap< QString, QStringList > QgsCptCityColorRampV2::mSchemeMap;
265266
// QMap< QString, int > QgsCptCityColorRampV2::mSchemeNumColors;
@@ -348,7 +349,7 @@ QgsStringMap QgsCptCityColorRampV2::properties() const
348349

349350
QStringList QgsCptCityColorRampV2::listSchemeCollections( QString collectionName, bool recursive )
350351
{
351-
QDir dir = QDir( QgsCptCityColorRampV2::getSchemeBaseDir() + "/" + collectionName );
352+
QDir dir = QDir( QgsCptCityColorRampV2::getBaseDir() + "/" + collectionName );
352353
if ( ! dir.exists() )
353354
return QStringList();
354355

@@ -376,9 +377,12 @@ QStringList QgsCptCityColorRampV2::listSchemeCollections( QString collectionName
376377

377378
QStringList QgsCptCityColorRampV2::listSchemeNames( QString collectionName )
378379
{
379-
QDir dir = QDir( QgsCptCityColorRampV2::getSchemeBaseDir() + "/" + collectionName );
380+
QDir dir = QDir( QgsCptCityColorRampV2::getBaseDir() + "/" + collectionName );
380381
if ( ! dir.exists() )
382+
{
383+
QgsDebugMsg( "dir " + dir.dirName() + " does not exist" );
381384
return QStringList();
385+
}
382386

383387
QStringList entries = dir.entryList( QStringList( "*.svg" ), QDir::Files, QDir::Name );
384388
for ( int i = 0; i < entries.count(); i++ )
@@ -403,18 +407,21 @@ QList<int> QgsCptCityColorRampV2::listSchemeVariants( QString schemeName )
403407
return variants;
404408
}
405409

406-
QString QgsCptCityColorRampV2::getSchemeBaseDir()
410+
QString QgsCptCityColorRampV2::getBaseDir()
407411
{
408412
// currently hard-coded, but could be also in QGis install path and/or configurable
409-
return QgsApplication::qgisSettingsDirPath() + "/" + "cpt-city";
413+
if ( mBaseDir.isNull() )
414+
return QgsApplication::qgisSettingsDirPath() + "/" + "cpt-city";
415+
else
416+
return mBaseDir;
410417
}
411418

412419
QString QgsCptCityColorRampV2::getFilename() const
413420
{
414421
if ( mSchemeName == "" )
415422
return QString();
416423
else
417-
return QgsCptCityColorRampV2::getSchemeBaseDir() + "/" + mSchemeName + mVariantName + ".svg";
424+
return QgsCptCityColorRampV2::getBaseDir() + "/" + mSchemeName + mVariantName + ".svg";
418425
}
419426

420427
bool QgsCptCityColorRampV2::loadFile( QString filename )
@@ -532,11 +539,32 @@ bool QgsCptCityColorRampV2::loadFile( QString filename )
532539
return true;
533540
}
534541

535-
void QgsCptCityColorRampV2::loadSchemes( QString rootDir, bool reset )
542+
bool QgsCptCityColorRampV2::hasBasicSchemes()
543+
{
544+
// Currently returns hasAllSchemes, because we don't have a minimal set yet
545+
return hasAllSchemes();
546+
}
547+
548+
bool QgsCptCityColorRampV2::hasAllSchemes()
536549
{
537-
//actually keep the name of the previously loaded, or look at the first element.
550+
// if we have no collections loaded yet, just make sure we have at least one collection
551+
// ideally we should test for a few schemes we know should be present
552+
if ( mCollections.isEmpty() )
553+
{
554+
return ( ! QgsCptCityColorRampV2::listSchemeCollections( "", false ).isEmpty() );
555+
}
556+
return true;
557+
}
558+
559+
// currently this methos takes some time, so it must be explicitly requested
560+
bool QgsCptCityColorRampV2::loadSchemes( QString rootDir, bool reset )
561+
{
562+
// TODO should keep the name of the previously loaded, or see if the first element is inside rootDir
538563
if ( ! reset && ! mCollections.isEmpty() )
539-
return;
564+
{
565+
QgsDebugMsg( QString( "not loading schemes, rootDir=%1 reset=%2 empty=%3" ).arg( rootDir ).arg( reset ).arg( mCollections.isEmpty() ) );
566+
return true;
567+
}
540568

541569
QString curName, prevName, prevPath, curVariant, curSep, schemeName;
542570
QStringList listVariant;
@@ -557,7 +585,6 @@ void QgsCptCityColorRampV2::loadSchemes( QString rootDir, bool reset )
557585
mCollections = listSchemeCollections( rootDir, true );
558586
qSort( mCollections.begin(), mCollections.end() );
559587

560-
561588
foreach( QString path, mCollections )
562589
{
563590
// QgsDebugMsg("================================");
@@ -752,5 +779,6 @@ void QgsCptCityColorRampV2::loadSchemes( QString rootDir, bool reset )
752779
mCollectionSelections[ viewName ] << curName;
753780
}
754781

782+
return ( ! mCollections.isEmpty() );
755783
}
756784

src/core/symbology-ng/qgsvectorcolorrampv2.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,13 @@ class CORE_EXPORT QgsCptCityColorRampV2 : public QgsVectorColorRampV2
200200
/* static QList<QColor> listSchemeColors( QString schemeName, int colors ); */
201201
static QList<int> listSchemeVariants( QString schemeName );
202202

203-
static QString getSchemeBaseDir();
204-
static void loadSchemes( QString rootDir = "", bool reset = false );
203+
static QString getBaseDir();
204+
static void setBaseDir( QString dirName ) { mBaseDir = dirName; }
205+
static bool loadSchemes( QString rootDir = "", bool reset = false );
206+
/** Is the minimal (free to distribute) set of schemes available? Currently returns hasAllSchemes, because we don't have a minimal set yet. */
207+
static bool hasBasicSchemes();
208+
/** Is the entire archive available? Currently tests that there is at least one scheme. */
209+
static bool hasAllSchemes();
205210
static QStringList listSchemeCollections( QString collectionName = "", bool recursive = false );
206211
static QStringList listSchemeNames( QString collectionName );
207212
static QgsCptCityColorRampV2* colorRampFromSVGFile( QString svgFile );
@@ -218,13 +223,13 @@ class CORE_EXPORT QgsCptCityColorRampV2 : public QgsVectorColorRampV2
218223
typedef QMap<double, QColor> StopsMap;
219224

220225
QString mSchemeName;
221-
/* int mColors; //remove! */
222226
QString mVariantName;
223227
bool mContinuous;
224228
QList< QColor > mPalette;
225229
QList< double > mPaletteStops;
226230
/* QMap< double, QColor > mPalette; */
227231

232+
static QString mBaseDir;
228233
static QStringList mCollections;
229234
static QMap< QString, QStringList > mSchemeMap; //key is collection, value is schemes
230235
/* mSchemeNumColors removed, instead read on demand */

src/gui/symbology-ng/qgscptcitycolorrampv2dialog.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ QgsCptCityColorRampV2Dialog::QgsCptCityColorRampV2Dialog( QgsCptCityColorRampV2*
4343
// QgsCptCityColorRampV2::loadSchemes( "cb" );
4444

4545
// show information on how to install cpt-city files if none are found
46-
if ( QgsCptCityColorRampV2::schemeMap().isEmpty() )
46+
if ( ! QgsCptCityColorRampV2::hasAllSchemes() )
4747
{
4848
QTextEdit *edit = new QTextEdit();
4949
edit->setReadOnly( true );

tests/src/core/testqgsrasterlayer.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class TestQgsRasterLayer: public QObject
5757
void pseudoColor();
5858
void colorRamp1();
5959
void colorRamp2();
60+
void colorRamp3();
6061
void landsatBasic();
6162
void landsatBasic875Qml();
6263
void checkDimensions();
@@ -246,9 +247,17 @@ void TestQgsRasterLayer::colorRamp1()
246247
void TestQgsRasterLayer::colorRamp2()
247248
{
248249
// ColorBrewer ramp
249-
QgsVectorColorBrewerColorRampV2* cb2Ramp = new QgsVectorColorBrewerColorRampV2( "BrBG", 10 );
250+
QVERIFY( testColorRamp( "raster_colorRamp2",
251+
new QgsVectorColorBrewerColorRampV2( "BrBG", 10 ),
252+
QgsColorRampShader::DISCRETE, 10 ) );
253+
}
250254

251-
QVERIFY( testColorRamp( "raster_colorRamp2", cb2Ramp, QgsColorRampShader::DISCRETE, 10 ) );
255+
void TestQgsRasterLayer::colorRamp3()
256+
{
257+
// cpt-city ramp
258+
QVERIFY( testColorRamp( "raster_colorRamp3",
259+
new QgsCptCityColorRampV2( "gmt/GMT_panoply", "" ),
260+
QgsColorRampShader::DISCRETE, 10 ) );
252261
}
253262

254263
void TestQgsRasterLayer::landsatBasic()

tests/src/core/testqgsstylev2.cpp

+42-14
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ bool TestStyleV2::testValidColor( QgsVectorColorRampV2 *ramp, double value, QCol
9393
QColor result = ramp->color( value );
9494
if ( result != expected )
9595
{
96-
QWARN( QString( "value = %1 result = %2 expected = %3" ).arg( value ).arg(
97-
result.name() ).arg( expected.name() ).toLocal8Bit().data() );
96+
QWARN( QString( "value = %1 result = %2 expected = %3" ).arg( value ).arg(
97+
result.name() ).arg( expected.name() ).toLocal8Bit().data() );
9898
return false;
9999
}
100100
return true;
@@ -118,23 +118,51 @@ void TestStyleV2::testCreateColorRamps()
118118
QVERIFY( mStyle->addColorRamp( "test_cb1", cb1Ramp ) == true );
119119
QgsVectorColorBrewerColorRampV2* cb2Ramp = new QgsVectorColorBrewerColorRampV2( "RdYlGn", 6 );
120120
QVERIFY( mStyle->addColorRamp( "test_cb2", cb2Ramp ) == true );
121+
122+
// cpt-city ramp - use gradients that are free to distribute
123+
// set base dir because we are using a test home path - change this if we distribute a minimal set with qgis
124+
QgsCptCityColorRampV2::setBaseDir( QDir::homePath() + QString( "/.qgis/cpt-city" ) );
125+
QgsCptCityColorRampV2::loadSchemes( "" );
126+
if ( QgsCptCityColorRampV2::hasBasicSchemes() )
127+
{
128+
QgsCptCityColorRampV2* cc1Ramp = new QgsCptCityColorRampV2( "jjg/misc/temperature", "" );
129+
QVERIFY( mStyle->addColorRamp( "test_cc1", cc1Ramp ) == true );
130+
QgsCptCityColorRampV2* cc2Ramp = new QgsCptCityColorRampV2( "cb/div/PiYG", "_10" );
131+
QVERIFY( mStyle->addColorRamp( "test_cc2", cc2Ramp ) == true );
132+
}
133+
else
134+
{
135+
QWARN( "cpt-city support files not found - skipping cpt-city color ramp tests" );
136+
}
121137
}
122138

123139
void TestStyleV2::testLoadColorRamps()
124140
{
125141
QStringList colorRamps = mStyle->colorRampNames();
126-
QStringList colorRampsTest = QStringList() << "BrBG" << "Spectral"
127-
<< "test_gradient" << "test_random" << "test_cb1" << "test_cb2" ;
142+
QStringList colorRampsTest = QStringList() << "BrBG" << "Spectral"
143+
<< "test_gradient" << "test_random"
144+
<< "test_cb1" << "test_cb2";
128145

129146
// values for color tests
130147
QMultiMap< QString, QPair< double, QColor> > colorTests;
131-
colorTests.insert( "test_gradient", qMakePair ( 0.25, QColor( "#ff7f7f" ) ) );
132-
colorTests.insert( "test_gradient", qMakePair ( 0.66, QColor( "#adadff" ) ) );
148+
colorTests.insert( "test_gradient", qMakePair( 0.25, QColor( "#ff7f7f" ) ) );
149+
colorTests.insert( "test_gradient", qMakePair( 0.66, QColor( "#adadff" ) ) );
133150
// cannot test random colors!
134-
colorTests.insert( "test_cb1", qMakePair ( 0.25, QColor( "#fdae61" ) ) );
135-
colorTests.insert( "test_cb1", qMakePair ( 0.66, QColor( "#abdda4" ) ) );
136-
colorTests.insert( "test_cb2", qMakePair ( 0.25, QColor( "#fc8d59" ) ) );
137-
colorTests.insert( "test_cb2", qMakePair ( 0.66, QColor( "#d9ef8b" ) ) );
151+
colorTests.insert( "test_cb1", qMakePair( 0.25, QColor( "#fdae61" ) ) );
152+
colorTests.insert( "test_cb1", qMakePair( 0.66, QColor( "#abdda4" ) ) );
153+
colorTests.insert( "test_cb2", qMakePair( 0.25, QColor( "#fc8d59" ) ) );
154+
colorTests.insert( "test_cb2", qMakePair( 0.66, QColor( "#d9ef8b" ) ) );
155+
156+
// cpt-city
157+
if ( QgsCptCityColorRampV2::hasAllSchemes() )
158+
{
159+
colorRampsTest << "test_cc1";
160+
colorTests.insert( "test_cc1", qMakePair( 0.25, QColor( "#466fcf" ) ) );
161+
colorTests.insert( "test_cc1", qMakePair( 0.66, QColor( "#dbc85b" ) ) );
162+
colorRampsTest << "test_cc2";
163+
colorTests.insert( "test_cc2", qMakePair( 0.25, QColor( "#de77ae" ) ) );
164+
colorTests.insert( "test_cc2", qMakePair( 0.66, QColor( "#b8e186" ) ) );
165+
}
138166

139167
foreach( QString name, colorRampsTest )
140168
{
@@ -148,19 +176,19 @@ void TestStyleV2::testLoadColorRamps()
148176
QList< QPair< double, QColor> > values = colorTests.values( name );
149177
for ( int i = 0; i < values.size(); ++i )
150178
{
151-
QVERIFY( testValidColor( ramp, values.at(i).first, values.at(i).second ) );
179+
QVERIFY( testValidColor( ramp, values.at( i ).first, values.at( i ).second ) );
152180
}
153181
}
154182
if ( ramp )
155-
delete ramp;
183+
delete ramp;
156184
}
157185
}
158186

159187
void TestStyleV2::testSaveLoad()
160188
{
161189
mStyle->save();
162190
mStyle->clear();
163-
mStyle->load( QgsApplication::userStyleV2Path() );
191+
mStyle->load( QgsApplication::userStyleV2Path() );
164192

165193
QStringList colorRamps = mStyle->colorRampNames();
166194
QStringList colorRampsTest = QStringList() << "test_gradient";
@@ -172,7 +200,7 @@ void TestStyleV2::testSaveLoad()
172200
QgsVectorColorRampV2* ramp = mStyle->colorRamp( name );
173201
QVERIFY( ramp != 0 );
174202
if ( ramp )
175-
delete ramp;
203+
delete ramp;
176204
}
177205
}
178206

Loading

tests/testdata/zip/points3.geojson.gz.properties

-2
This file was deleted.

0 commit comments

Comments
 (0)