Skip to content
Permalink
Browse files

Added renderer registry to allow custom renderers. GUI for renderers …

…is still hardcoded.

git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11932 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder
wonder committed Nov 6, 2009
1 parent d34fb39 commit 910d9e823c2c6f766c1fbf9b30ffebaccfc3429d
@@ -34,18 +34,10 @@ class QgsFeatureRendererV2

public:

enum RendererType
{
RendererSingleSymbol,
RendererCategorizedSymbol,
RendererGraduatedSymbol
// TODO: user type?
};

//! return a new renderer - used by default in vector layers
static QgsFeatureRendererV2* defaultRenderer(QGis::GeometryType geomType) /Factory/;

RendererType type() const;
QString type() const;

virtual QgsSymbolV2* symbolForFeature(QgsFeature& feature)=0;

@@ -69,7 +61,7 @@ public:
void setUsingSymbolLevels(bool usingSymbolLevels);

protected:
QgsFeatureRendererV2(RendererType type);
QgsFeatureRendererV2(QString type);

};

@@ -402,6 +402,7 @@ static QPixmap _symbolPreviewPixmap(QgsSymbolV2* sym, QSize iconSize)
QPixmap pix(iconSize);
pix.fill(Qt::white);
p.begin(&pix);
p.setRenderHint(QPainter::Antialiasing);
sym->drawPreviewIcon(&p, iconSize);
p.end();
return pix;
@@ -417,53 +418,49 @@ void QgsLegendLayer::vectorLayerSymbologyV2( QgsVectorLayer* layer )
bool showClassifiers = settings.value( "/qgis/showLegendClassifiers", false ).toBool();

QgsFeatureRendererV2* renderer = layer->rendererV2();
switch (renderer->type())
QString rendererType = renderer->type();
if (rendererType == "singleSymbol")
{
case QgsFeatureRendererV2::RendererSingleSymbol:
{
QgsSingleSymbolRendererV2* r = static_cast<QgsSingleSymbolRendererV2*>(renderer);
QPixmap pix = _symbolPreviewPixmap(r->symbol(), iconSize);
QgsSingleSymbolRendererV2* r = static_cast<QgsSingleSymbolRendererV2*>(renderer);
QPixmap pix = _symbolPreviewPixmap(r->symbol(), iconSize);

itemList.push_back( std::make_pair( "", pix ) );
}
break;
case QgsFeatureRendererV2::RendererCategorizedSymbol:
{
QgsCategorizedSymbolRendererV2* r = static_cast<QgsCategorizedSymbolRendererV2*>(renderer);
if (showClassifiers)
{
itemList.push_back( std::make_pair( r->classAttribute(), QPixmap() ) );
}
itemList.push_back( std::make_pair( "", pix ) );
}
else if (rendererType == "categorizedSymbol")
{
QgsCategorizedSymbolRendererV2* r = static_cast<QgsCategorizedSymbolRendererV2*>(renderer);
if (showClassifiers)
{
itemList.push_back( std::make_pair( r->classAttribute(), QPixmap() ) );
}

int count = r->categories().count();
for (int i = 0; i < count; i++)
{
const QgsRendererCategoryV2& cat = r->categories()[i];
QPixmap pix = _symbolPreviewPixmap( cat.symbol(), iconSize );
itemList.push_back( std::make_pair( cat.label(), pix ) );
}
}
break;
case QgsFeatureRendererV2::RendererGraduatedSymbol:
{
QgsGraduatedSymbolRendererV2* r = static_cast<QgsGraduatedSymbolRendererV2*>(renderer);
if (showClassifiers)
{
itemList.push_back( std::make_pair( r->classAttribute(), QPixmap() ) );
}
int count = r->categories().count();
for (int i = 0; i < count; i++)
{
const QgsRendererCategoryV2& cat = r->categories()[i];
QPixmap pix = _symbolPreviewPixmap( cat.symbol(), iconSize );
itemList.push_back( std::make_pair( cat.label(), pix ) );
}
}
else if (rendererType == "graduatedSymbol")
{
QgsGraduatedSymbolRendererV2* r = static_cast<QgsGraduatedSymbolRendererV2*>(renderer);
if (showClassifiers)
{
itemList.push_back( std::make_pair( r->classAttribute(), QPixmap() ) );
}

int count = r->ranges().count();
for (int i = 0; i < count; i++)
{
const QgsRendererRangeV2& range = r->ranges()[i];
QPixmap pix = _symbolPreviewPixmap( range.symbol(), iconSize );
itemList.push_back( std::make_pair( range.label(), pix ) );
}
}
break;
default:
// nothing for unknown renderers
break;
int count = r->ranges().count();
for (int i = 0; i < count; i++)
{
const QgsRendererRangeV2& range = r->ranges()[i];
QPixmap pix = _symbolPreviewPixmap( range.symbol(), iconSize );
itemList.push_back( std::make_pair( range.label(), pix ) );
}
}
else
{
// nothing for unknown renderers
}

changeSymbologySettings( layer, itemList );
@@ -13,6 +13,7 @@ SET(QGIS_CORE_SRCS
symbology-ng/qgsmarkersymbollayerv2.cpp
symbology-ng/qgsfillsymbollayerv2.cpp
symbology-ng/qgsrendererv2.cpp
symbology-ng/qgsrendererv2registry.cpp
symbology-ng/qgssinglesymbolrendererv2.cpp
symbology-ng/qgscategorizedsymbolrendererv2.cpp
symbology-ng/qgsgraduatedsymbolrendererv2.cpp
@@ -64,7 +64,7 @@ QString QgsRendererCategoryV2::dump()
///////////////////

QgsCategorizedSymbolRendererV2::QgsCategorizedSymbolRendererV2(QString attrName, QgsCategoryList categories)
: QgsFeatureRendererV2(RendererCategorizedSymbol),
: QgsFeatureRendererV2("categorizedSymbol"),
mAttrName(attrName),
mCategories(categories),
mSourceSymbol(NULL),
@@ -69,7 +69,7 @@ QString QgsRendererRangeV2::dump()


QgsGraduatedSymbolRendererV2::QgsGraduatedSymbolRendererV2(QString attrName, QgsRangeList ranges)
: QgsFeatureRendererV2(RendererGraduatedSymbol),
: QgsFeatureRendererV2("graduatedSymbol"),
mAttrName(attrName),
mRanges(ranges),
mMode(Custom),
@@ -5,9 +5,7 @@

#include "qgssinglesymbolrendererv2.h" // for default renderer

// TODO: to be removed once renderer registry is in place
#include "qgscategorizedsymbolrendererv2.h"
#include "qgsgraduatedsymbolrendererv2.h"
#include "qgsrendererv2registry.h"

#include "qgsrendercontext.h"
#include "qgsgeometry.h"
@@ -121,7 +119,7 @@ static unsigned char* _getPolygon(QPolygonF& pts, QList<QPolygonF>& holes, const
}


QgsFeatureRendererV2::QgsFeatureRendererV2(RendererType type)
QgsFeatureRendererV2::QgsFeatureRendererV2(QString type)
: mType(type), mUsingSymbolLevels(false)
{
}
@@ -268,22 +266,13 @@ QgsFeatureRendererV2* QgsFeatureRendererV2::load(QDomElement& element)
// load renderer
QString rendererType = element.attribute("type");

// TODO: use renderer registry
if (rendererType == "singleSymbol")
{
return QgsSingleSymbolRendererV2::create(element);
}
else if (rendererType == "categorizedSymbol")
{
return QgsCategorizedSymbolRendererV2::create(element);
}
else if (rendererType == "graduatedSymbol")
{
return QgsGraduatedSymbolRendererV2::create(element);
}
QgsRendererV2CreateFunc pfCreate = QgsRendererV2Registry::instance()->rendererCreateFunction(rendererType);

// unknown renderer type?
if (pfCreate == NULL)
return NULL;

// unknown renderer type
return NULL;
return pfCreate(element);
}

QDomElement QgsFeatureRendererV2::save(QDomDocument& doc)
@@ -50,18 +50,10 @@ class QgsFeatureRendererV2
public:
// renderer takes ownership of its symbols!

enum RendererType
{
RendererSingleSymbol,
RendererCategorizedSymbol,
RendererGraduatedSymbol
// TODO: user type?
};

//! return a new renderer - used by default in vector layers
static QgsFeatureRendererV2* defaultRenderer(QGis::GeometryType geomType);

RendererType type() const { return mType; }
QString type() const { return mType; }

// to be overridden
virtual QgsSymbolV2* symbolForFeature(QgsFeature& feature)=0;
@@ -100,9 +92,9 @@ class QgsFeatureRendererV2


protected:
QgsFeatureRendererV2(RendererType type);
QgsFeatureRendererV2(QString type);

RendererType mType;
QString mType;

bool mUsingSymbolLevels;
};
@@ -0,0 +1,47 @@
#include "qgsrendererv2registry.h"

// default renderers
#include "qgssinglesymbolrendererv2.h"
#include "qgscategorizedsymbolrendererv2.h"
#include "qgsgraduatedsymbolrendererv2.h"


QgsRendererV2Registry* QgsRendererV2Registry::mInstance = NULL;

QgsRendererV2Registry::QgsRendererV2Registry()
{
// add default renderers
addRenderer("singleSymbol", QgsSingleSymbolRendererV2::create);
addRenderer("categorizedSymbol", QgsCategorizedSymbolRendererV2::create);
addRenderer("graduatedSymbol", QgsGraduatedSymbolRendererV2::create);
}

QgsRendererV2Registry* QgsRendererV2Registry::instance()
{
if (!mInstance)
mInstance = new QgsRendererV2Registry();

return mInstance;
}


bool QgsRendererV2Registry::addRenderer(QString rendererName, QgsRendererV2CreateFunc pfCreate)
{
if (mRenderers.contains(rendererName))
return false;
mRenderers.insert(rendererName, pfCreate);
return true;
}

bool QgsRendererV2Registry::removeRenderer(QString rendererName)
{
if (!mRenderers.contains(rendererName))
return false;
mRenderers.remove(rendererName);
return true;
}

QgsRendererV2CreateFunc QgsRendererV2Registry::rendererCreateFunction(QString rendererName)
{
return mRenderers.value(rendererName, NULL);
}
@@ -0,0 +1,37 @@
#ifndef QGSRENDERERV2REGISTRY_H
#define QGSRENDERERV2REGISTRY_H

#include <QMap>

class QgsFeatureRendererV2;
class QDomElement;

typedef QgsFeatureRendererV2* (*QgsRendererV2CreateFunc)(QDomElement&);

/**
Registry of renderers.
This is a singleton, renderers can be added / removed at any time
*/
class QgsRendererV2Registry
{
public:

static QgsRendererV2Registry* instance();

bool addRenderer(QString rendererName, QgsRendererV2CreateFunc pfCreate);

bool removeRenderer(QString rendererName);

QgsRendererV2CreateFunc rendererCreateFunction(QString rendererName);

protected:
//! protected constructor
QgsRendererV2Registry();

static QgsRendererV2Registry* mInstance;

QMap<QString, QgsRendererV2CreateFunc> mRenderers;
};

#endif // QGSRENDERERV2REGISTRY_H
@@ -8,7 +8,7 @@
#include <QDomElement>

QgsSingleSymbolRendererV2::QgsSingleSymbolRendererV2(QgsSymbolV2* symbol)
: QgsFeatureRendererV2(RendererSingleSymbol)
: QgsFeatureRendererV2("singleSymbol")
{
mSymbol = symbol;
}

0 comments on commit 910d9e8

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