Skip to content
Permalink
Browse files

add tests for cpt-city color ramps

  • Loading branch information
etiennesky committed Aug 3, 2012
1 parent bd160be commit e39c216a59c955e75a201e2683d12fef0cf3e37c
@@ -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;
@@ -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();

@@ -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++ )
@@ -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 )
@@ -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;
@@ -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("================================");
@@ -752,5 +779,6 @@ void QgsCptCityColorRampV2::loadSchemes( QString rootDir, bool reset )
mCollectionSelections[ viewName ] << curName;
}

return ( ! mCollections.isEmpty() );
}

@@ -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 );
@@ -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 */
@@ -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 );
@@ -57,6 +57,7 @@ class TestQgsRasterLayer: public QObject
void pseudoColor();
void colorRamp1();
void colorRamp2();
void colorRamp3();
void landsatBasic();
void landsatBasic875Qml();
void checkDimensions();
@@ -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()
@@ -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;
@@ -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 )
{
@@ -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";
@@ -172,7 +200,7 @@ void TestStyleV2::testSaveLoad()
QgsVectorColorRampV2* ramp = mStyle->colorRamp( name );
QVERIFY( ramp != 0 );
if ( ramp )
delete ramp;
delete ramp;
}
}

Binary file not shown.

This file was deleted.

0 comments on commit e39c216

Please sign in to comment.
You can’t perform that action at this time.