Skip to content
Permalink
Browse files

Add method for finding matching schemes from a color scheme registry

  • Loading branch information
nyalldawson committed Aug 16, 2014
1 parent f7f4c74 commit 6312ff58532199fa7347225d17a92b8dcbc92883
Showing with 79 additions and 0 deletions.
  1. +21 −0 src/core/qgscolorschemeregistry.h
  2. +58 −0 tests/src/core/testqgscolorschemeregistry.cpp
@@ -77,6 +77,12 @@ class CORE_EXPORT QgsColorSchemeRegistry
*/
QList<QgsColorScheme *> schemes() const;

/**Return color schemes of a specific type
* @param schemeList destination list for matching schemes
* @note not available in python bindings
*/
template<class T> void schemes( QList<T*>& schemeList );

private:

static QgsColorSchemeRegistry *mInstance;
@@ -85,4 +91,19 @@ class CORE_EXPORT QgsColorSchemeRegistry

};

template<class T> void QgsColorSchemeRegistry::schemes( QList<T*>& schemeList )
{
schemeList.clear();
QList<QgsColorScheme *> schemeInstanceList = schemes();
QList<QgsColorScheme *>::iterator schemeIt = schemeInstanceList.begin();
for ( ; schemeIt != schemeInstanceList.end(); ++schemeIt )
{
T* scheme = dynamic_cast<T*>( *schemeIt );
if ( scheme )
{
schemeList.push_back( scheme );
}
}
}

#endif
@@ -20,6 +20,43 @@
#include <QObject>
#include <QtTest>

//dummy color scheme for testing
class DummyColorScheme : public QgsColorScheme
{
public:

DummyColorScheme() {}

virtual ~DummyColorScheme() {}

virtual QString schemeName() const { return QString( "Dummy scheme" ); }

virtual QgsNamedColorList fetchColors( const QString context = QString(),
const QColor baseColor = QColor() )
{
QList< QPair< QColor, QString> > colors;
if ( context == QString( "testscheme" ) )
{
colors << qMakePair( QColor( 255, 255, 0 ), QString( "schemetest" ) );
}
else if ( baseColor.isValid() )
{
colors << qMakePair( baseColor, QString( "base" ) );
}
else
{
colors << qMakePair( QColor( 255, 0, 0 ), QString( "red" ) ) << qMakePair( QColor( 0, 255, 0 ), QString() );
}
return colors;
}

virtual QgsColorScheme* clone() const
{
return new DummyColorScheme();
}

};

class TestQgsColorSchemeRegistry : public QObject
{
Q_OBJECT;
@@ -36,6 +73,7 @@ class TestQgsColorSchemeRegistry : public QObject
void addDefaultSchemes(); // check adding a scheme to an empty registry
void populateFromInstance(); // check populating an empty scheme from the registry
void removeScheme(); // check removing a scheme from a registry
void matchingSchemes(); //check fetching schemes of specific type

private:

@@ -136,5 +174,25 @@ void TestQgsColorSchemeRegistry::removeScheme()
delete registry;
}

void TestQgsColorSchemeRegistry::matchingSchemes()
{
QgsColorSchemeRegistry* registry = new QgsColorSchemeRegistry();
//add some schemes
QgsColorScheme* recentScheme = new QgsRecentColorScheme();
registry->addColorScheme( recentScheme );
DummyColorScheme* dummyScheme = new DummyColorScheme();
registry->addColorScheme( dummyScheme );
QVERIFY( registry->schemes().length() == 2 );
QList< QgsRecentColorScheme* > recentSchemes;
QList< DummyColorScheme* > dummySchemes;
registry->schemes( recentSchemes );
QVERIFY( recentSchemes.length() == 1 );
QCOMPARE( recentSchemes.at( 0 ), recentScheme );
registry->schemes( dummySchemes );
QVERIFY( dummySchemes.length() == 1 );
QCOMPARE( dummySchemes.at( 0 ), dummyScheme );
delete registry;
}

QTEST_MAIN( TestQgsColorSchemeRegistry )
#include "moc_testqgscolorschemeregistry.cxx"

0 comments on commit 6312ff5

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