Skip to content
Permalink
Browse files

split code of renderers into several files

git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11909 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder
wonder committed Nov 3, 2009
1 parent ef9190d commit 4654732b526d52ebc928bc76dea7eace306b3da4
@@ -78,7 +78,7 @@ protected:
class QgsSingleSymbolRendererV2 : QgsFeatureRendererV2
{
%TypeHeaderCode
#include <qgsrendererv2.h>
#include <qgssinglesymbolrendererv2.h>
%End

public:
@@ -108,7 +108,7 @@ public:
class QgsRendererCategoryV2
{
%TypeHeaderCode
#include <qgsrendererv2.h>
#include <qgscategorizedsymbolrendererv2.h>
%End

public:
@@ -135,7 +135,7 @@ typedef QList<QgsRendererCategoryV2> QgsCategoryList;
class QgsCategorizedSymbolRendererV2 : QgsFeatureRendererV2
{
%TypeHeaderCode
#include <qgsrendererv2.h>
#include <qgscategorizedsymbolrendererv2.h>
%End

public:
@@ -180,7 +180,7 @@ protected:
class QgsRendererRangeV2
{
%TypeHeaderCode
#include <qgsrendererv2.h>
#include <qgsgraduatedsymbolrendererv2.h>
%End

public:
@@ -206,7 +206,7 @@ typedef QList<QgsRendererRangeV2> QgsRangeList;
class QgsGraduatedSymbolRendererV2 : QgsFeatureRendererV2
{
%TypeHeaderCode
#include <qgsrendererv2.h>
#include <qgsgraduatedsymbolrendererv2.h>
%End

public:
@@ -39,6 +39,10 @@
#include "qgsrendererv2.h"
#include "qgssymbolv2.h"

#include "qgssinglesymbolrendererv2.h"
#include "qgscategorizedsymbolrendererv2.h"
#include "qgsgraduatedsymbolrendererv2.h"

#include <iostream>
#include <QAction>
#include <QCoreApplication>
@@ -13,6 +13,9 @@ SET(QGIS_CORE_SRCS
symbology-ng/qgsmarkersymbollayerv2.cpp
symbology-ng/qgsfillsymbollayerv2.cpp
symbology-ng/qgsrendererv2.cpp
symbology-ng/qgssinglesymbolrendererv2.cpp
symbology-ng/qgscategorizedsymbolrendererv2.cpp
symbology-ng/qgsgraduatedsymbolrendererv2.cpp
symbology-ng/qgsvectorcolorrampv2.cpp
symbology-ng/qgsstylev2.cpp

@@ -0,0 +1,290 @@

#include "qgscategorizedsymbolrendererv2.h"

#include "qgssymbolv2.h"
#include "qgssymbollayerv2utils.h"

#include "qgsfeature.h"
#include "qgslogger.h"

#include <QDomDocument>
#include <QDomElement>

QgsRendererCategoryV2::QgsRendererCategoryV2(QVariant value, QgsSymbolV2* symbol, QString label)
: mValue(value), mSymbol(symbol), mLabel(label)
{
}

QgsRendererCategoryV2::QgsRendererCategoryV2(const QgsRendererCategoryV2& cat)
: mValue(cat.mValue), mLabel(cat.mLabel)
{
mSymbol = cat.mSymbol->clone();
}


QgsRendererCategoryV2::~QgsRendererCategoryV2()
{
delete mSymbol;
}

QVariant QgsRendererCategoryV2::value() const
{
return mValue;
}

QgsSymbolV2* QgsRendererCategoryV2::symbol() const
{
return mSymbol;
}

QString QgsRendererCategoryV2::label() const
{
return mLabel;
}

void QgsRendererCategoryV2::setSymbol(QgsSymbolV2* s)
{
if (mSymbol == s)
return;
delete mSymbol;
mSymbol = s;
}

void QgsRendererCategoryV2::setLabel(QString label)
{
mLabel = label;
}

QString QgsRendererCategoryV2::dump()
{
return QString("%1::%2::%3\n").arg(mValue.toString()).arg(mLabel).arg(mSymbol->dump());
}

///////////////////

QgsCategorizedSymbolRendererV2::QgsCategorizedSymbolRendererV2(QString attrName, QgsCategoryList categories)
: QgsFeatureRendererV2(RendererCategorizedSymbol), mAttrName(attrName), mCategories(categories)
{
for (int i = 0; i < mCategories.count(); ++i)
{
QgsRendererCategoryV2& cat = mCategories[i];
if (cat.symbol() == NULL)
{
QgsDebugMsg("invalid symbol in a category! ignoring...");
mCategories.removeAt(i--);
}
//mCategories.insert(cat.value().toString(), cat);
}
}

QgsCategorizedSymbolRendererV2::~QgsCategorizedSymbolRendererV2()
{
mCategories.clear(); // this should also call destructors of symbols
}

void QgsCategorizedSymbolRendererV2::rebuildHash()
{
mSymbolHash.clear();

for (int i = 0; i < mCategories.count(); ++i)
{
QgsRendererCategoryV2& cat = mCategories[i];
mSymbolHash.insert(cat.value().toString(), cat.symbol());
}
}

QgsSymbolV2* QgsCategorizedSymbolRendererV2::symbolForValue(QVariant value)
{
// TODO: special case for int, double

QHash<QString, QgsSymbolV2*>::iterator it = mSymbolHash.find(value.toString());
if (it == mSymbolHash.end())
{
if (mSymbolHash.count() == 0)
QgsDebugMsg("there are no hashed symbols!!!");
else
QgsDebugMsg("attribute value not found: " + value.toString());
return NULL;
}
else
return *it;
}

QgsSymbolV2* QgsCategorizedSymbolRendererV2::symbolForFeature(QgsFeature& feature)
{
const QgsAttributeMap& attrMap = feature.attributeMap();
QgsAttributeMap::const_iterator ita = attrMap.find(mAttrNum);
if (ita == attrMap.end())
{
QgsDebugMsg("attribute '"+mAttrName+"' (index "+QString::number(mAttrNum)+") required by renderer not found");
return NULL;
}

// find the right category
return symbolForValue(*ita);
}

int QgsCategorizedSymbolRendererV2::categoryIndexForValue(QVariant val)
{
for (int i = 0; i < mCategories.count(); i++)
{
if (mCategories[i].value() == val)
return i;
}
return -1;
}

bool QgsCategorizedSymbolRendererV2::updateCategorySymbol(int catIndex, QgsSymbolV2* symbol)
{
if (catIndex < 0 || catIndex >= mCategories.size())
return false;
mCategories[catIndex].setSymbol(symbol);
return true;
}

bool QgsCategorizedSymbolRendererV2::updateCategoryLabel(int catIndex, QString label)
{
if (catIndex < 0 || catIndex >= mCategories.size())
return false;
mCategories[catIndex].setLabel(label);
return true;
}

bool QgsCategorizedSymbolRendererV2::deleteCategory(int catIndex)
{
if (catIndex < 0 || catIndex >= mCategories.size())
return false;

mCategories.removeAt(catIndex);
return true;
}

void QgsCategorizedSymbolRendererV2::deleteAllCategories()
{
mCategories.clear();
}

void QgsCategorizedSymbolRendererV2::startRender(QgsRenderContext& context, const QgsFieldMap& fields)
{
// make sure that the hash table is up to date
rebuildHash();

// find out classification attribute index from name
mAttrNum = fieldNameIndex(fields, mAttrName);

QgsCategoryList::iterator it = mCategories.begin();
for ( ; it != mCategories.end(); ++it)
it->symbol()->startRender(context);
}

void QgsCategorizedSymbolRendererV2::stopRender(QgsRenderContext& context)
{
QgsCategoryList::iterator it = mCategories.begin();
for ( ; it != mCategories.end(); ++it)
it->symbol()->stopRender(context);
}

QList<QString> QgsCategorizedSymbolRendererV2::usedAttributes()
{
QList<QString> lst;
lst.append(mAttrName);
return lst;
}

QString QgsCategorizedSymbolRendererV2::dump()
{
QString s = QString("CATEGORIZED: idx %1\n").arg(mAttrName);
for (int i=0; i<mCategories.count();i++)
s += mCategories[i].dump();
return s;
}

QgsFeatureRendererV2* QgsCategorizedSymbolRendererV2::clone()
{
QgsCategorizedSymbolRendererV2* r = new QgsCategorizedSymbolRendererV2( mAttrName, mCategories );
r->setUsingSymbolLevels( usingSymbolLevels() );
return r;
}

QgsSymbolV2List QgsCategorizedSymbolRendererV2::symbols()
{
QgsSymbolV2List lst;
for (int i = 0; i < mCategories.count(); i++)
lst.append(mCategories[i].symbol());
return lst;
}

QgsFeatureRendererV2* QgsCategorizedSymbolRendererV2::create(QDomElement& element)
{
QDomElement symbolsElem = element.firstChildElement("symbols");
if (symbolsElem.isNull())
return NULL;

QDomElement catsElem = element.firstChildElement("categories");
if (catsElem.isNull())
return NULL;

QgsSymbolV2Map symbolMap = QgsSymbolLayerV2Utils::loadSymbols(symbolsElem);
QgsCategoryList cats;

QDomElement catElem = catsElem.firstChildElement();
while (!catElem.isNull())
{
if (catElem.tagName() == "category")
{
QVariant value = QVariant(catElem.attribute("value"));
QString symbolName = catElem.attribute("symbol");
QString label = catElem.attribute("label");
if (symbolMap.contains(symbolName))
{
QgsSymbolV2* symbol = symbolMap.take(symbolName);
cats.append( QgsRendererCategoryV2(value, symbol, label) );
}
}
catElem = catElem.nextSiblingElement();
}

QString attrName = element.attribute("attr");

QgsCategorizedSymbolRendererV2* r = new QgsCategorizedSymbolRendererV2(attrName, cats);

// delete symbols if there are any more
QgsSymbolLayerV2Utils::clearSymbolMap(symbolMap);

// TODO: symbol levels
return r;
}

QDomElement QgsCategorizedSymbolRendererV2::save(QDomDocument& doc)
{
QDomElement rendererElem = doc.createElement(RENDERER_TAG_NAME);
rendererElem.setAttribute("type", "categorizedSymbol");
rendererElem.setAttribute("attr", mAttrName);

// categories
int i = 0;
QgsSymbolV2Map symbols;
QDomElement catsElem = doc.createElement("categories");
QgsCategoryList::const_iterator it = mCategories.constBegin();
for ( ; it != mCategories.end(); it++)
{
const QgsRendererCategoryV2& cat = *it;
QString symbolName = QString::number(i);
symbols.insert(symbolName, cat.symbol());

QDomElement catElem = doc.createElement("category");
catElem.setAttribute("value", cat.value().toString());
catElem.setAttribute("symbol", symbolName);
catElem.setAttribute("label", cat.label());
catsElem.appendChild(catElem);
i++;
}

rendererElem.appendChild(catsElem);

// save symbols
QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols(symbols, doc);
rendererElem.appendChild(symbolsElem);

return rendererElem;
}

0 comments on commit 4654732

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