Skip to content
Permalink
Browse files

categorized and graduated renderers remember their source symbol and …

…color ramp

git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11918 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder
wonder committed Nov 4, 2009
1 parent 5e9a058 commit d34fb39eeac27b975fddf5dde0665fb1fc5fd51c
@@ -3,6 +3,7 @@

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

#include "qgsfeature.h"
#include "qgslogger.h"
@@ -63,7 +64,11 @@ QString QgsRendererCategoryV2::dump()
///////////////////

QgsCategorizedSymbolRendererV2::QgsCategorizedSymbolRendererV2(QString attrName, QgsCategoryList categories)
: QgsFeatureRendererV2(RendererCategorizedSymbol), mAttrName(attrName), mCategories(categories)
: QgsFeatureRendererV2(RendererCategorizedSymbol),
mAttrName(attrName),
mCategories(categories),
mSourceSymbol(NULL),
mSourceColorRamp(NULL)
{
for (int i = 0; i < mCategories.count(); ++i)
{
@@ -80,6 +85,8 @@ QgsCategorizedSymbolRendererV2::QgsCategorizedSymbolRendererV2(QString attrName,
QgsCategorizedSymbolRendererV2::~QgsCategorizedSymbolRendererV2()
{
mCategories.clear(); // this should also call destructors of symbols
delete mSourceSymbol;
delete mSourceColorRamp;
}

void QgsCategorizedSymbolRendererV2::rebuildHash()
@@ -202,6 +209,10 @@ QString QgsCategorizedSymbolRendererV2::dump()
QgsFeatureRendererV2* QgsCategorizedSymbolRendererV2::clone()
{
QgsCategorizedSymbolRendererV2* r = new QgsCategorizedSymbolRendererV2( mAttrName, mCategories );
if (mSourceSymbol)
r->setSourceSymbol(mSourceSymbol->clone());
if (mSourceColorRamp)
r->setSourceColorRamp(mSourceColorRamp->clone());
r->setUsingSymbolLevels( usingSymbolLevels() );
return r;
}
@@ -251,6 +262,25 @@ QgsFeatureRendererV2* QgsCategorizedSymbolRendererV2::create(QDomElement& elemen
// delete symbols if there are any more
QgsSymbolLayerV2Utils::clearSymbolMap(symbolMap);

// try to load source symbol (optional)
QDomElement sourceSymbolElem = element.firstChildElement("source-symbol");
if (!sourceSymbolElem.isNull())
{
QgsSymbolV2Map sourceSymbolMap = QgsSymbolLayerV2Utils::loadSymbols(sourceSymbolElem);
if (sourceSymbolMap.contains("0"))
{
r->setSourceSymbol( sourceSymbolMap.take("0") );
}
QgsSymbolLayerV2Utils::clearSymbolMap(sourceSymbolMap);
}

// try to load color ramp (optional)
QDomElement sourceColorRampElem = element.firstChildElement("colorramp");
if (!sourceColorRampElem.isNull() && sourceColorRampElem.attribute("name") == "[source]")
{
r->setSourceColorRamp( QgsSymbolLayerV2Utils::loadColorRamp(sourceColorRampElem) );
}

// TODO: symbol levels
return r;
}
@@ -283,8 +313,44 @@ QDomElement QgsCategorizedSymbolRendererV2::save(QDomDocument& doc)
rendererElem.appendChild(catsElem);

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

// save source symbol
if (mSourceSymbol)
{
QgsSymbolV2Map sourceSymbols;
sourceSymbols.insert("0", mSourceSymbol);
QDomElement sourceSymbolElem = QgsSymbolLayerV2Utils::saveSymbols(sourceSymbols, "source-symbol", doc);
rendererElem.appendChild(sourceSymbolElem);
}

// save source color ramp
if (mSourceColorRamp)
{
QDomElement colorRampElem = QgsSymbolLayerV2Utils::saveColorRamp("[source]", mSourceColorRamp, doc);
rendererElem.appendChild(colorRampElem);
}

return rendererElem;
}

QgsSymbolV2* QgsCategorizedSymbolRendererV2::sourceSymbol()
{
return mSourceSymbol;
}
void QgsCategorizedSymbolRendererV2::setSourceSymbol(QgsSymbolV2* sym)
{
delete mSourceSymbol;
mSourceSymbol = sym;
}

QgsVectorColorRampV2* QgsCategorizedSymbolRendererV2::sourceColorRamp()
{
return mSourceColorRamp;
}
void QgsCategorizedSymbolRendererV2::setSourceColorRamp(QgsVectorColorRampV2* ramp)
{
delete mSourceColorRamp;
mSourceColorRamp = ramp;
}
@@ -5,6 +5,7 @@

#include <QHash>

class QgsVectorColorRampV2;

class QgsRendererCategoryV2
{
@@ -78,9 +79,17 @@ class QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2
//! store renderer info to XML element
virtual QDomElement save(QDomDocument& doc);

QgsSymbolV2* sourceSymbol();
void setSourceSymbol(QgsSymbolV2* sym);

QgsVectorColorRampV2* sourceColorRamp();
void setSourceColorRamp(QgsVectorColorRampV2* ramp);

protected:
QgsCategoryList mCategories;
QString mAttrName;
QgsSymbolV2* mSourceSymbol;
QgsVectorColorRampV2* mSourceColorRamp;

//! attribute index (derived from attribute name in startRender)
int mAttrNum;
@@ -3,6 +3,7 @@

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

#include "qgsfeature.h"
#include "qgslogger.h"
@@ -68,14 +69,21 @@ QString QgsRendererRangeV2::dump()


QgsGraduatedSymbolRendererV2::QgsGraduatedSymbolRendererV2(QString attrName, QgsRangeList ranges)
: QgsFeatureRendererV2(RendererGraduatedSymbol), mAttrName(attrName), mRanges(ranges), mMode(Custom)
: QgsFeatureRendererV2(RendererGraduatedSymbol),
mAttrName(attrName),
mRanges(ranges),
mMode(Custom),
mSourceSymbol(NULL),
mSourceColorRamp(NULL)
{
// TODO: check ranges for sanity (NULL symbols, invalid ranges)
}

QgsGraduatedSymbolRendererV2::~QgsGraduatedSymbolRendererV2()
{
mRanges.clear(); // should delete all the symbols
delete mSourceSymbol;
delete mSourceColorRamp;
}

QgsSymbolV2* QgsGraduatedSymbolRendererV2::symbolForValue(double value)
@@ -155,6 +163,10 @@ QString QgsGraduatedSymbolRendererV2::dump()
QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::clone()
{
QgsGraduatedSymbolRendererV2* r = new QgsGraduatedSymbolRendererV2( mAttrName, mRanges );
if (mSourceSymbol)
r->setSourceSymbol(mSourceSymbol->clone());
if (mSourceColorRamp)
r->setSourceColorRamp(mSourceColorRamp->clone());
r->setUsingSymbolLevels( usingSymbolLevels() );
return r;
}
@@ -275,7 +287,11 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(
ranges.append( QgsRendererRangeV2(lower, upper, newSymbol, label) );
}

return new QgsGraduatedSymbolRendererV2( attrName, ranges );
QgsGraduatedSymbolRendererV2* r = new QgsGraduatedSymbolRendererV2( attrName, ranges );
r->setSourceSymbol( symbol->clone() );
r->setSourceColorRamp( ramp->clone() );
r->setMode(mode);
return r;
}


@@ -318,6 +334,36 @@ QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::create(QDomElement& element)
// delete symbols if there are any more
QgsSymbolLayerV2Utils::clearSymbolMap(symbolMap);

// try to load source symbol (optional)
QDomElement sourceSymbolElem = element.firstChildElement("source-symbol");
if (!sourceSymbolElem.isNull())
{
QgsSymbolV2Map sourceSymbolMap = QgsSymbolLayerV2Utils::loadSymbols(sourceSymbolElem);
if (sourceSymbolMap.contains("0"))
{
r->setSourceSymbol( sourceSymbolMap.take("0") );
}
QgsSymbolLayerV2Utils::clearSymbolMap(sourceSymbolMap);
}

// try to load color ramp (optional)
QDomElement sourceColorRampElem = element.firstChildElement("colorramp");
if (!sourceColorRampElem.isNull() && sourceColorRampElem.attribute("name") == "[source]")
{
r->setSourceColorRamp( QgsSymbolLayerV2Utils::loadColorRamp(sourceColorRampElem) );
}

// try to load mode
QDomElement modeElem = element.firstChildElement("mode");
if (!modeElem.isNull())
{
QString modeString = modeElem.attribute("name");
if (modeString == "equal")
r->setMode(EqualInterval);
else if (modeString == "quantile")
r->setMode(Quantile);
}

// TODO: symbol levels
return r;
}
@@ -351,8 +397,57 @@ QDomElement QgsGraduatedSymbolRendererV2::save(QDomDocument& doc)
rendererElem.appendChild(rangesElem);

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

// save source symbol
if (mSourceSymbol)
{
QgsSymbolV2Map sourceSymbols;
sourceSymbols.insert("0", mSourceSymbol);
QDomElement sourceSymbolElem = QgsSymbolLayerV2Utils::saveSymbols(sourceSymbols, "source-symbol", doc);
rendererElem.appendChild(sourceSymbolElem);
}

// save source color ramp
if (mSourceColorRamp)
{
QDomElement colorRampElem = QgsSymbolLayerV2Utils::saveColorRamp("[source]", mSourceColorRamp, doc);
rendererElem.appendChild(colorRampElem);
}

// save mode
QString modeString;
if (mMode == EqualInterval)
modeString = "equal";
else if (mMode == Quantile)
modeString = "quantile";
if (!modeString.isEmpty())
{
QDomElement modeElem = doc.createElement("mode");
modeElem.setAttribute("name", modeString);
rendererElem.appendChild(modeElem);
}

return rendererElem;
}

QgsSymbolV2* QgsGraduatedSymbolRendererV2::sourceSymbol()
{
return mSourceSymbol;
}
void QgsGraduatedSymbolRendererV2::setSourceSymbol(QgsSymbolV2* sym)
{
delete mSourceSymbol;
mSourceSymbol = sym;
}

QgsVectorColorRampV2* QgsGraduatedSymbolRendererV2::sourceColorRamp()
{
return mSourceColorRamp;
}
void QgsGraduatedSymbolRendererV2::setSourceColorRamp(QgsVectorColorRampV2* ramp)
{
delete mSourceColorRamp;
mSourceColorRamp = ramp;
}
@@ -87,10 +87,18 @@ class QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
//! store renderer info to XML element
virtual QDomElement save(QDomDocument& doc);

QgsSymbolV2* sourceSymbol();
void setSourceSymbol(QgsSymbolV2* sym);

QgsVectorColorRampV2* sourceColorRamp();
void setSourceColorRamp(QgsVectorColorRampV2* ramp);

protected:
QgsRangeList mRanges;
QString mAttrName;
Mode mMode;
QgsSymbolV2* mSourceSymbol;
QgsVectorColorRampV2* mSourceColorRamp;

//! attribute index (derived from attribute name in startRender)
int mAttrNum;
@@ -95,7 +95,7 @@ QDomElement QgsSingleSymbolRendererV2::save(QDomDocument& doc)

QgsSymbolV2Map symbols;
symbols["0"] = mSymbol;
QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols(symbols, doc);
QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols(symbols, "symbols", doc);
rendererElem.appendChild(symbolsElem);

return rendererElem;
@@ -205,7 +205,7 @@ bool QgsStyleV2::load(QString filename)
{
if (e.tagName() == "colorramp")
{
QgsVectorColorRampV2* ramp = loadColorRamp(e);
QgsVectorColorRampV2* ramp = QgsSymbolLayerV2Utils::loadColorRamp(e);
if (ramp != NULL)
addColorRamp(e.attribute("name"), ramp);
}
@@ -220,25 +220,6 @@ bool QgsStyleV2::load(QString filename)
}


QgsVectorColorRampV2* QgsStyleV2::loadColorRamp(QDomElement& element)
{
QString rampType = element.attribute("type");

// parse properties
QgsStringMap props = QgsSymbolLayerV2Utils::parseProperties(element);

if (rampType == "gradient")
return QgsVectorGradientColorRampV2::create(props);
else if (rampType == "random")
return QgsVectorRandomColorRampV2::create(props);
else
{
QgsDebugMsg("unknown colorramp type " + rampType);
return NULL;
}
}



bool QgsStyleV2::save(QString filename)
{
@@ -251,14 +232,14 @@ bool QgsStyleV2::save(QString filename)
root.setAttribute("version", STYLE_CURRENT_VERSION);
doc.appendChild(root);

QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols(mSymbols, doc);
QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols(mSymbols, "symbols", doc);

QDomElement rampsElem = doc.createElement("colorramps");

// save color ramps
for (QMap<QString, QgsVectorColorRampV2*>::iterator itr = mColorRamps.begin(); itr != mColorRamps.end(); ++itr)
{
QDomElement rampEl = saveColorRamp(itr.key(), itr.value(), doc);
QDomElement rampEl = QgsSymbolLayerV2Utils::saveColorRamp(itr.key(), itr.value(), doc);
rampsElem.appendChild(rampEl);
}

@@ -278,13 +259,3 @@ bool QgsStyleV2::save(QString filename)

return true;
}

QDomElement QgsStyleV2::saveColorRamp(QString name, QgsVectorColorRampV2* ramp, QDomDocument& doc)
{
QDomElement rampEl = doc.createElement("colorramp");
rampEl.setAttribute("type", ramp->type());
rampEl.setAttribute("name", name);

QgsSymbolLayerV2Utils::saveProperties(ramp->properties(), doc, rampEl);
return rampEl;
}
@@ -78,10 +78,6 @@ class QgsStyleV2

protected:

QgsVectorColorRampV2* loadColorRamp(QDomElement& element);

QDomElement saveColorRamp(QString name, QgsVectorColorRampV2* ramp, QDomDocument& doc);

QgsSymbolV2Map mSymbols;
QgsVectorColorRampV2Map mColorRamps;

0 comments on commit d34fb39

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