1
/***************************************************************************
2
qgsuniquevaluerenderer.cpp - description
5
copyright : (C) 2004 by Marco Hugentobler
6
email : marco.hugentobler@autoform.ch
7
***************************************************************************/
9
/***************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
16
***************************************************************************/
17
/* $Id: qgsuniquevaluerenderer.cpp 5468 2006-05-18 13:43:32Z rblazek $ */
18
#include "qgsuniquevaluerenderer.h"
19
#include "qgsfeatureattribute.h"
20
#include "qgsfeature.h"
21
#include "qgsvectorlayer.h"
22
#include "qgssymbol.h"
23
#include "qgssymbologyutils.h"
29
QgsUniqueValueRenderer::QgsUniqueValueRenderer(QGis::VectorType type): mClassificationField(0)
33
//call superclass method to set up selection colour
34
initialiseSelectionColor();
38
QgsUniqueValueRenderer::QgsUniqueValueRenderer(const QgsUniqueValueRenderer& other)
40
mVectorType = other.mVectorType;
41
mClassificationField = other.mClassificationField;
42
std::map<QString, QgsSymbol*> s = other.mSymbols;
43
for(std::map<QString, QgsSymbol*>::iterator it=s.begin(); it!=s.end(); ++it)
45
QgsSymbol* s = new QgsSymbol(*(it->second));
46
insertValue(it->first, s);
50
QgsUniqueValueRenderer& QgsUniqueValueRenderer::operator=(const QgsUniqueValueRenderer& other)
54
mVectorType = other.mVectorType;
55
mClassificationField = other.mClassificationField;
57
for(std::map<QString, QgsSymbol*>::iterator it=mSymbols.begin(); it!=mSymbols.end(); ++it)
59
QgsSymbol* s = new QgsSymbol(*(it->second));
60
insertValue(it->first, s);
66
QgsUniqueValueRenderer::~QgsUniqueValueRenderer()
68
for(std::map<QString,QgsSymbol*>::iterator it=mSymbols.begin();it!=mSymbols.end();++it)
74
const std::list<QgsSymbol*> QgsUniqueValueRenderer::symbols() const
76
std::list <QgsSymbol*> symbollist;
77
for(std::map<QString, QgsSymbol*>::const_iterator it = mSymbols.begin(); it!=mSymbols.end(); ++it)
79
symbollist.push_back(it->second);
84
void QgsUniqueValueRenderer::insertValue(QString name, QgsSymbol* symbol)
86
mSymbols.insert(std::make_pair(name, symbol));
89
void QgsUniqueValueRenderer::setClassificationField(int field)
91
mClassificationField=field;
94
int QgsUniqueValueRenderer::classificationField()
96
return mClassificationField;
99
void QgsUniqueValueRenderer::renderFeature(QPainter* p, QgsFeature* f,QPixmap* pic,
100
double* scalefactor, bool selected, double widthScale)
102
std::vector < QgsFeatureAttribute > vec = f->attributeMap();
103
QString value = vec[0].fieldValue();
104
std::map<QString,QgsSymbol*>::iterator it=mSymbols.find(value);
105
if(it!=mSymbols.end())
107
QgsSymbol* symbol = it->second;
110
if ( pic && mVectorType == QGis::Point ) {
111
*pic = symbol->getPointSymbolAsPixmap( widthScale,
112
selected, mSelectionColor );
114
if ( scalefactor ) *scalefactor = 1;
118
if ( mVectorType != QGis::Point )
122
QPen pen=symbol->pen();
123
pen.setWidthF ( widthScale * pen.width() );
125
p->setBrush(symbol->brush());
129
QPen pen=symbol->pen();
130
pen.setWidthF ( widthScale * pen.width() );
131
pen.setColor(mSelectionColor);
132
QBrush brush=symbol->brush();
133
brush.setColor(mSelectionColor);
142
qWarning("Warning, no render item found in QgsUniqueValueRenderer::renderFeature");
148
void QgsUniqueValueRenderer::readXML(const QDomNode& rnode, QgsVectorLayer& vl)
150
mVectorType = vl.vectorType();
151
QDomNode classnode = rnode.namedItem("classificationfield");
152
int classificationfield = classnode.toElement().text().toInt();
153
this->setClassificationField(classificationfield);
155
QDomNode symbolnode = rnode.namedItem("symbol");
156
while (!symbolnode.isNull())
158
QgsSymbol* msy = new QgsSymbol(mVectorType);
159
msy->readXML ( symbolnode );
160
this->insertValue(msy->lowerValue(),msy);
161
symbolnode = symbolnode.nextSibling();
162
vl.setRenderer(this);
166
void QgsUniqueValueRenderer::clearValues()
168
for(std::map<QString,QgsSymbol*>::iterator it=mSymbols.begin();it!=mSymbols.end();++it)
175
QString QgsUniqueValueRenderer::name() const
177
return "Unique Value";
180
std::list<int> QgsUniqueValueRenderer::classificationAttributes() const
183
list.push_back(mClassificationField);
187
bool QgsUniqueValueRenderer::writeXML( QDomNode & layer_node, QDomDocument & document ) const
190
QDomElement uniquevalue=document.createElement("uniquevalue");
191
layer_node.appendChild(uniquevalue);
192
QDomElement classificationfield=document.createElement("classificationfield");
193
QDomText classificationfieldtxt=document.createTextNode(QString::number(mClassificationField));
194
classificationfield.appendChild(classificationfieldtxt);
195
uniquevalue.appendChild(classificationfield);
196
for(std::map<QString,QgsSymbol*>::const_iterator it=mSymbols.begin();it!=mSymbols.end();++it)
198
if(!(it->second)->writeXML(uniquevalue,document))
206
QgsRenderer* QgsUniqueValueRenderer::clone() const
208
QgsUniqueValueRenderer* r = new QgsUniqueValueRenderer(*this);