Skip to content
Permalink
Browse files

Added loading and saving of renderers v2 in project files.

git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11881 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder
wonder committed Nov 1, 2009
1 parent 1e7c9cf commit d5630cc84edb1872ca4e263954ecb2c165fd852e
@@ -1,5 +1,6 @@
<!DOCTYPE qgis_style>
<qgis_style version="0" >
<symbols>
<symbol type="marker" name="blue square" >
<layer class="SimpleMarker" locked="0" >
<prop k="angle" v="0" />
@@ -97,6 +98,8 @@
<prop k="size" v="12" />
</layer>
</symbol>
</symbols>
<colorramps>
<colorramp type="gradient" name="blue" >
<prop k="color1" v="0,0,255" />
<prop k="color2" v="207,205,255" />
@@ -109,4 +112,5 @@
<prop k="color1" v="255,0,0" />
<prop k="color2" v="255,255,0" />
</colorramp>
</colorramps>
</qgis_style>
@@ -2315,12 +2315,28 @@ bool QgsVectorLayer::readXml( QDomNode & layer_node )
}
}

QString errorMsg;
if ( !readSymbology( layer_node, errorMsg ) )
QDomElement rendererElement = layer_node.firstChildElement(RENDERER_TAG_NAME);
if (!rendererElement.isNull())
{
return false;
// using renderer v2
setUsingRendererV2(true);

QgsFeatureRendererV2* r = QgsFeatureRendererV2::load(rendererElement);
if (r == NULL)
return false;
setRendererV2(r);
}
else
{
// using renderer v1
setUsingRendererV2(false);

QString errorMsg;
if ( !readSymbology( layer_node, errorMsg ) )
{
return false;
}
}

return mValid; // should be true if read successfully

@@ -2455,13 +2471,19 @@ bool QgsVectorLayer::writeXml( QDomNode & layer_node,
}

// renderer specific settings

QString errorMsg;
if ( !writeSymbology( layer_node, document, errorMsg ) )
if (mUsingRendererV2)
{
return false;
QDomElement rendererElement = mRendererV2->save(document);
layer_node.appendChild(rendererElement);
}
else
{
QString errorMsg;
if ( !writeSymbology( layer_node, document, errorMsg ) )
{
return false;
}
}


return true;
} // bool QgsVectorLayer::writeXml
@@ -1,14 +1,18 @@
#include "qgsrendererv2.h"
#include "qgssymbolv2.h"
#include "qgssymbollayerv2utils.h"

#include "qgsrendercontext.h"
#include "qgsgeometry.h"
#include "qgsfeature.h"
#include "qgslogger.h"

#include <QDomElement>
#include <QDomDocument>
#include <QPolygonF>



static unsigned char* _getPoint(QPointF& pt, const QgsMapToPixel& mapToPixel, unsigned char* wkb)
{
wkb++; // jump over endian info
@@ -247,6 +251,40 @@ QString QgsFeatureRendererV2::dump()
}


QgsFeatureRendererV2* QgsFeatureRendererV2::load(QDomElement& element)
{
// <renderer-v2 type=""> ... </renderer-v2>

if (element.isNull())
return NULL;

// 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);
}

// unknown renderer type
return NULL;
}

QDomElement QgsFeatureRendererV2::save(QDomDocument& doc)
{
// create empty renderer element
return doc.createElement(RENDERER_TAG_NAME);
}

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

QgsSingleSymbolRendererV2::QgsSingleSymbolRendererV2(QgsSymbolV2* symbol)
@@ -310,6 +348,39 @@ QgsSymbolV2List QgsSingleSymbolRendererV2::symbols()
return lst;
}

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

QgsSymbolV2Map symbolMap = QgsSymbolLayerV2Utils::loadSymbols(symbolsElem);

if (!symbolMap.contains("0"))
return NULL;

QgsSingleSymbolRendererV2* r = new QgsSingleSymbolRendererV2( symbolMap.take("0") );

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

// TODO: symbol levels
return r;
}

QDomElement QgsSingleSymbolRendererV2::save(QDomDocument& doc)
{
QDomElement rendererElem = doc.createElement(RENDERER_TAG_NAME);
rendererElem.setAttribute("type", "singleSymbol");

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

return rendererElem;
}

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

QgsRendererCategoryV2::QgsRendererCategoryV2(QVariant value, QgsSymbolV2* symbol, QString label)
@@ -513,6 +584,81 @@ QgsSymbolV2List QgsCategorizedSymbolRendererV2::symbols()
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();
}

int attrNum = element.attribute("attr").toInt();

QgsCategorizedSymbolRendererV2* r = new QgsCategorizedSymbolRendererV2(attrNum, 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", mAttrNum);

// 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;
}

/////////////////////////
// graduated

@@ -778,3 +924,82 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer(

return new QgsGraduatedSymbolRendererV2( attrNum, ranges );
}



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

QDomElement rangesElem = element.firstChildElement("ranges");
if (rangesElem.isNull())
return NULL;

QgsSymbolV2Map symbolMap = QgsSymbolLayerV2Utils::loadSymbols(symbolsElem);
QgsRangeList ranges;

QDomElement rangeElem = rangesElem.firstChildElement();
while (!rangeElem.isNull())
{
if (rangeElem.tagName() == "range")
{
double lowerValue = rangeElem.attribute("lower").toDouble();
double upperValue = rangeElem.attribute("upper").toDouble();
QString symbolName = rangeElem.attribute("symbol");
QString label = rangeElem.attribute("label");
if (symbolMap.contains(symbolName))
{
QgsSymbolV2* symbol = symbolMap.take(symbolName);
ranges.append( QgsRendererRangeV2(lowerValue, upperValue, symbol, label) );
}
}
rangeElem = rangeElem.nextSiblingElement();
}

int attrNum = element.attribute("attr").toInt();

QgsGraduatedSymbolRendererV2* r = new QgsGraduatedSymbolRendererV2(attrNum, ranges);

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

// TODO: symbol levels
return r;
}

QDomElement QgsGraduatedSymbolRendererV2::save(QDomDocument& doc)
{
QDomElement rendererElem = doc.createElement(RENDERER_TAG_NAME);
rendererElem.setAttribute("type", "graduatedSymbol");
rendererElem.setAttribute("attr", mAttrNum);

// ranges
int i = 0;
QgsSymbolV2Map symbols;
QDomElement rangesElem = doc.createElement("ranges");
QgsRangeList::const_iterator it = mRanges.constBegin();
for ( ; it != mRanges.end(); it++)
{
const QgsRendererRangeV2& range = *it;
QString symbolName = QString::number(i);
symbols.insert(symbolName, range.symbol());

QDomElement rangeElem = doc.createElement("range");
rangeElem.setAttribute("lower", range.lowerValue());
rangeElem.setAttribute("upper", range.upperValue());
rangeElem.setAttribute("symbol", symbolName);
rangeElem.setAttribute("label", range.label());
rangesElem.appendChild(rangeElem);
i++;
}

rendererElem.appendChild(rangesElem);

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

return rendererElem;
}

0 comments on commit d5630cc

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