Skip to content

Commit

Permalink
Add method for finding matching schemes from a color scheme registry
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 16, 2014
1 parent f7f4c74 commit 6312ff5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/core/qgscolorschemeregistry.h
Expand Up @@ -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;
Expand All @@ -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
58 changes: 58 additions & 0 deletions tests/src/core/testqgscolorschemeregistry.cpp 100755 → 100644
Expand Up @@ -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;
Expand All @@ -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:

Expand Down Expand Up @@ -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.