Skip to content

Commit

Permalink
add tests for cpt-city color ramps
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennesky committed Aug 3, 2012
1 parent bd160be commit e39c216
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 31 deletions.
46 changes: 37 additions & 9 deletions src/core/symbology-ng/qgsvectorcolorrampv2.cpp
Expand Up @@ -260,6 +260,7 @@ TODO
- fix ocal file parsing
- re-organize and rename colorramp classes and widgets
*/
QString QgsCptCityColorRampV2::mBaseDir;
QStringList QgsCptCityColorRampV2::mCollections;
QMap< QString, QStringList > QgsCptCityColorRampV2::mSchemeMap;
// QMap< QString, int > QgsCptCityColorRampV2::mSchemeNumColors;
Expand Down Expand Up @@ -348,7 +349,7 @@ QgsStringMap QgsCptCityColorRampV2::properties() const

QStringList QgsCptCityColorRampV2::listSchemeCollections( QString collectionName, bool recursive )
{
QDir dir = QDir( QgsCptCityColorRampV2::getSchemeBaseDir() + "/" + collectionName );
QDir dir = QDir( QgsCptCityColorRampV2::getBaseDir() + "/" + collectionName );
if ( ! dir.exists() )
return QStringList();

Expand Down Expand Up @@ -376,9 +377,12 @@ QStringList QgsCptCityColorRampV2::listSchemeCollections( QString collectionName

QStringList QgsCptCityColorRampV2::listSchemeNames( QString collectionName )
{
QDir dir = QDir( QgsCptCityColorRampV2::getSchemeBaseDir() + "/" + collectionName );
QDir dir = QDir( QgsCptCityColorRampV2::getBaseDir() + "/" + collectionName );
if ( ! dir.exists() )
{
QgsDebugMsg( "dir " + dir.dirName() + " does not exist" );
return QStringList();
}

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

QString QgsCptCityColorRampV2::getSchemeBaseDir()
QString QgsCptCityColorRampV2::getBaseDir()
{
// currently hard-coded, but could be also in QGis install path and/or configurable
return QgsApplication::qgisSettingsDirPath() + "/" + "cpt-city";
if ( mBaseDir.isNull() )
return QgsApplication::qgisSettingsDirPath() + "/" + "cpt-city";
else
return mBaseDir;
}

QString QgsCptCityColorRampV2::getFilename() const
{
if ( mSchemeName == "" )
return QString();
else
return QgsCptCityColorRampV2::getSchemeBaseDir() + "/" + mSchemeName + mVariantName + ".svg";
return QgsCptCityColorRampV2::getBaseDir() + "/" + mSchemeName + mVariantName + ".svg";
}

bool QgsCptCityColorRampV2::loadFile( QString filename )
Expand Down Expand Up @@ -532,11 +539,32 @@ bool QgsCptCityColorRampV2::loadFile( QString filename )
return true;
}

void QgsCptCityColorRampV2::loadSchemes( QString rootDir, bool reset )
bool QgsCptCityColorRampV2::hasBasicSchemes()
{
// Currently returns hasAllSchemes, because we don't have a minimal set yet
return hasAllSchemes();
}

bool QgsCptCityColorRampV2::hasAllSchemes()
{
//actually keep the name of the previously loaded, or look at the first element.
// if we have no collections loaded yet, just make sure we have at least one collection
// ideally we should test for a few schemes we know should be present
if ( mCollections.isEmpty() )
{
return ( ! QgsCptCityColorRampV2::listSchemeCollections( "", false ).isEmpty() );
}
return true;
}

// currently this methos takes some time, so it must be explicitly requested
bool QgsCptCityColorRampV2::loadSchemes( QString rootDir, bool reset )
{
// TODO should keep the name of the previously loaded, or see if the first element is inside rootDir
if ( ! reset && ! mCollections.isEmpty() )
return;
{
QgsDebugMsg( QString( "not loading schemes, rootDir=%1 reset=%2 empty=%3" ).arg( rootDir ).arg( reset ).arg( mCollections.isEmpty() ) );
return true;
}

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


foreach( QString path, mCollections )
{
// QgsDebugMsg("================================");
Expand Down Expand Up @@ -752,5 +779,6 @@ void QgsCptCityColorRampV2::loadSchemes( QString rootDir, bool reset )
mCollectionSelections[ viewName ] << curName;
}

return ( ! mCollections.isEmpty() );
}

11 changes: 8 additions & 3 deletions src/core/symbology-ng/qgsvectorcolorrampv2.h
Expand Up @@ -200,8 +200,13 @@ class CORE_EXPORT QgsCptCityColorRampV2 : public QgsVectorColorRampV2
/* static QList<QColor> listSchemeColors( QString schemeName, int colors ); */
static QList<int> listSchemeVariants( QString schemeName );

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

QString mSchemeName;
/* int mColors; //remove! */
QString mVariantName;
bool mContinuous;
QList< QColor > mPalette;
QList< double > mPaletteStops;
/* QMap< double, QColor > mPalette; */

static QString mBaseDir;
static QStringList mCollections;
static QMap< QString, QStringList > mSchemeMap; //key is collection, value is schemes
/* mSchemeNumColors removed, instead read on demand */
Expand Down
2 changes: 1 addition & 1 deletion src/gui/symbology-ng/qgscptcitycolorrampv2dialog.cpp
Expand Up @@ -43,7 +43,7 @@ QgsCptCityColorRampV2Dialog::QgsCptCityColorRampV2Dialog( QgsCptCityColorRampV2*
// QgsCptCityColorRampV2::loadSchemes( "cb" );

// show information on how to install cpt-city files if none are found
if ( QgsCptCityColorRampV2::schemeMap().isEmpty() )
if ( ! QgsCptCityColorRampV2::hasAllSchemes() )
{
QTextEdit *edit = new QTextEdit();
edit->setReadOnly( true );
Expand Down
13 changes: 11 additions & 2 deletions tests/src/core/testqgsrasterlayer.cpp
Expand Up @@ -57,6 +57,7 @@ class TestQgsRasterLayer: public QObject
void pseudoColor();
void colorRamp1();
void colorRamp2();
void colorRamp3();
void landsatBasic();
void landsatBasic875Qml();
void checkDimensions();
Expand Down Expand Up @@ -246,9 +247,17 @@ void TestQgsRasterLayer::colorRamp1()
void TestQgsRasterLayer::colorRamp2()
{
// ColorBrewer ramp
QgsVectorColorBrewerColorRampV2* cb2Ramp = new QgsVectorColorBrewerColorRampV2( "BrBG", 10 );
QVERIFY( testColorRamp( "raster_colorRamp2",
new QgsVectorColorBrewerColorRampV2( "BrBG", 10 ),
QgsColorRampShader::DISCRETE, 10 ) );
}

QVERIFY( testColorRamp( "raster_colorRamp2", cb2Ramp, QgsColorRampShader::DISCRETE, 10 ) );
void TestQgsRasterLayer::colorRamp3()
{
// cpt-city ramp
QVERIFY( testColorRamp( "raster_colorRamp3",
new QgsCptCityColorRampV2( "gmt/GMT_panoply", "" ),
QgsColorRampShader::DISCRETE, 10 ) );
}

void TestQgsRasterLayer::landsatBasic()
Expand Down
56 changes: 42 additions & 14 deletions tests/src/core/testqgsstylev2.cpp
Expand Up @@ -93,8 +93,8 @@ bool TestStyleV2::testValidColor( QgsVectorColorRampV2 *ramp, double value, QCol
QColor result = ramp->color( value );
if ( result != expected )
{
QWARN( QString( "value = %1 result = %2 expected = %3" ).arg( value ).arg(
result.name() ).arg( expected.name() ).toLocal8Bit().data() );
QWARN( QString( "value = %1 result = %2 expected = %3" ).arg( value ).arg(
result.name() ).arg( expected.name() ).toLocal8Bit().data() );
return false;
}
return true;
Expand All @@ -118,23 +118,51 @@ void TestStyleV2::testCreateColorRamps()
QVERIFY( mStyle->addColorRamp( "test_cb1", cb1Ramp ) == true );
QgsVectorColorBrewerColorRampV2* cb2Ramp = new QgsVectorColorBrewerColorRampV2( "RdYlGn", 6 );
QVERIFY( mStyle->addColorRamp( "test_cb2", cb2Ramp ) == true );

// cpt-city ramp - use gradients that are free to distribute
// set base dir because we are using a test home path - change this if we distribute a minimal set with qgis
QgsCptCityColorRampV2::setBaseDir( QDir::homePath() + QString( "/.qgis/cpt-city" ) );
QgsCptCityColorRampV2::loadSchemes( "" );
if ( QgsCptCityColorRampV2::hasBasicSchemes() )
{
QgsCptCityColorRampV2* cc1Ramp = new QgsCptCityColorRampV2( "jjg/misc/temperature", "" );
QVERIFY( mStyle->addColorRamp( "test_cc1", cc1Ramp ) == true );
QgsCptCityColorRampV2* cc2Ramp = new QgsCptCityColorRampV2( "cb/div/PiYG", "_10" );
QVERIFY( mStyle->addColorRamp( "test_cc2", cc2Ramp ) == true );
}
else
{
QWARN( "cpt-city support files not found - skipping cpt-city color ramp tests" );
}
}

void TestStyleV2::testLoadColorRamps()
{
QStringList colorRamps = mStyle->colorRampNames();
QStringList colorRampsTest = QStringList() << "BrBG" << "Spectral"
<< "test_gradient" << "test_random" << "test_cb1" << "test_cb2" ;
QStringList colorRampsTest = QStringList() << "BrBG" << "Spectral"
<< "test_gradient" << "test_random"
<< "test_cb1" << "test_cb2";

// values for color tests
QMultiMap< QString, QPair< double, QColor> > colorTests;
colorTests.insert( "test_gradient", qMakePair ( 0.25, QColor( "#ff7f7f" ) ) );
colorTests.insert( "test_gradient", qMakePair ( 0.66, QColor( "#adadff" ) ) );
colorTests.insert( "test_gradient", qMakePair( 0.25, QColor( "#ff7f7f" ) ) );
colorTests.insert( "test_gradient", qMakePair( 0.66, QColor( "#adadff" ) ) );
// cannot test random colors!
colorTests.insert( "test_cb1", qMakePair ( 0.25, QColor( "#fdae61" ) ) );
colorTests.insert( "test_cb1", qMakePair ( 0.66, QColor( "#abdda4" ) ) );
colorTests.insert( "test_cb2", qMakePair ( 0.25, QColor( "#fc8d59" ) ) );
colorTests.insert( "test_cb2", qMakePair ( 0.66, QColor( "#d9ef8b" ) ) );
colorTests.insert( "test_cb1", qMakePair( 0.25, QColor( "#fdae61" ) ) );
colorTests.insert( "test_cb1", qMakePair( 0.66, QColor( "#abdda4" ) ) );
colorTests.insert( "test_cb2", qMakePair( 0.25, QColor( "#fc8d59" ) ) );
colorTests.insert( "test_cb2", qMakePair( 0.66, QColor( "#d9ef8b" ) ) );

// cpt-city
if ( QgsCptCityColorRampV2::hasAllSchemes() )
{
colorRampsTest << "test_cc1";
colorTests.insert( "test_cc1", qMakePair( 0.25, QColor( "#466fcf" ) ) );
colorTests.insert( "test_cc1", qMakePair( 0.66, QColor( "#dbc85b" ) ) );
colorRampsTest << "test_cc2";
colorTests.insert( "test_cc2", qMakePair( 0.25, QColor( "#de77ae" ) ) );
colorTests.insert( "test_cc2", qMakePair( 0.66, QColor( "#b8e186" ) ) );
}

foreach( QString name, colorRampsTest )
{
Expand All @@ -148,19 +176,19 @@ void TestStyleV2::testLoadColorRamps()
QList< QPair< double, QColor> > values = colorTests.values( name );
for ( int i = 0; i < values.size(); ++i )
{
QVERIFY( testValidColor( ramp, values.at(i).first, values.at(i).second ) );
QVERIFY( testValidColor( ramp, values.at( i ).first, values.at( i ).second ) );
}
}
if ( ramp )
delete ramp;
delete ramp;
}
}

void TestStyleV2::testSaveLoad()
{
mStyle->save();
mStyle->clear();
mStyle->load( QgsApplication::userStyleV2Path() );
mStyle->load( QgsApplication::userStyleV2Path() );

QStringList colorRamps = mStyle->colorRampNames();
QStringList colorRampsTest = QStringList() << "test_gradient";
Expand All @@ -172,7 +200,7 @@ void TestStyleV2::testSaveLoad()
QgsVectorColorRampV2* ramp = mStyle->colorRamp( name );
QVERIFY( ramp != 0 );
if ( ramp )
delete ramp;
delete ramp;
}
}

Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions tests/testdata/zip/points3.geojson.gz.properties

This file was deleted.

0 comments on commit e39c216

Please sign in to comment.