1
/***************************************************************************
2
qgscontinuouscolorrenderer.cpp - description
5
copyright : (C) 2003 by Marco Hugentobler
6
email : mhugent@geo.unizh.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: qgscontinuouscolorrenderer.cpp 5834 2006-09-18 09:31:12Z mhugent $ */
18
#include "qgscontinuouscolorrenderer.h"
20
#include "qgsvectorlayer.h"
22
#include "qgslegenditem.h"
23
#include "qgssymbologyutils.h"
24
#include "qgsmarkercatalogue.h"
25
#include "qgssymbol.h"
27
#include <QDomElement>
31
QgsContinuousColorRenderer::QgsContinuousColorRenderer(QGis::VectorType type): mMinimumSymbol(0), mMaximumSymbol(0)
34
//call superclass method to set up selection colour
35
initialiseSelectionColor();
38
QgsContinuousColorRenderer::QgsContinuousColorRenderer(const QgsContinuousColorRenderer& other)
40
mVectorType = other.mVectorType;
41
mClassificationField = other.mClassificationField;
42
mMinimumSymbol = new QgsSymbol(*other.mMinimumSymbol);
43
mMaximumSymbol = new QgsSymbol(*other.mMaximumSymbol);
46
QgsContinuousColorRenderer& QgsContinuousColorRenderer::operator=(const QgsContinuousColorRenderer& other)
50
mVectorType = other.mVectorType;
51
mClassificationField = other.mClassificationField;
52
delete mMinimumSymbol;
53
delete mMaximumSymbol;
54
mMinimumSymbol = new QgsSymbol(*other.mMinimumSymbol);
55
mMaximumSymbol = new QgsSymbol(*other.mMaximumSymbol);
60
QgsContinuousColorRenderer::~QgsContinuousColorRenderer()
62
delete mMinimumSymbol;
63
delete mMaximumSymbol;
66
void QgsContinuousColorRenderer::setMinimumSymbol(QgsSymbol* sy)
68
delete mMinimumSymbol;
72
void QgsContinuousColorRenderer::setMaximumSymbol(QgsSymbol* sy)
74
delete mMaximumSymbol;
78
void QgsContinuousColorRenderer::renderFeature(QPainter * p, QgsFeature * f, QPixmap* pic,
79
double* scalefactor, bool selected, double widthScale)
81
if ((mMinimumSymbol && mMaximumSymbol))
83
//first find out the value for the classification attribute
84
std::vector < QgsFeatureAttribute > vec = f->attributeMap();
85
double fvalue = vec[0].fieldValue().toDouble();
87
//double fvalue = vec[mClassificationField].fieldValue().toDouble();
88
double minvalue = mMinimumSymbol->lowerValue().toDouble();
89
double maxvalue = mMaximumSymbol->lowerValue().toDouble();
91
QColor mincolor, maxcolor;
93
if ( mVectorType == QGis::Line || mVectorType == QGis::Point )
95
mincolor = mMinimumSymbol->pen().color();
96
maxcolor = mMaximumSymbol->pen().color();
100
p->setPen(mMinimumSymbol->pen());
101
mincolor = mMinimumSymbol->fillColor();
102
maxcolor = mMaximumSymbol->fillColor();
107
if((maxvalue - minvalue)!=0)
109
red = int (maxcolor.red() * (fvalue - minvalue) / (maxvalue - minvalue) + mincolor.red() * (maxvalue - fvalue) / (maxvalue - minvalue));
110
green = int (maxcolor.green() * (fvalue - minvalue) / (maxvalue - minvalue) + mincolor.green() * (maxvalue - fvalue) / (maxvalue - minvalue));
111
blue = int (maxcolor.blue() * (fvalue - minvalue) / (maxvalue - minvalue) + mincolor.blue() * (maxvalue - fvalue) / (maxvalue - minvalue));
115
red = int (mincolor.red());
116
green = int (mincolor.green());
117
blue = int (mincolor.blue());
120
if ( mVectorType == QGis::Point && pic) {
121
// TODO we must get always new marker -> slow, but continuous color for points
122
// probably is not used frequently
125
// Use color for both pen and brush (user can add another layer with outpine)
126
// later add support for both pen and brush to dialog
127
QPen pen = mMinimumSymbol->pen();
128
pen.setColor ( QColor(red, green, blue) );
129
pen.setWidthF ( widthScale * pen.width() );
131
QBrush brush = mMinimumSymbol->brush();
134
pen.setColor ( mSelectionColor );
135
brush.setColor ( mSelectionColor );
137
brush.setColor ( QColor(red, green, blue) );
139
brush.setStyle ( Qt::SolidPattern );
141
*pic = QgsMarkerCatalogue::instance()->pixmapMarker ( mMinimumSymbol->pointSymbolName(), mMinimumSymbol->pointSize(),
144
if ( scalefactor ) *scalefactor = 1;
146
else if ( mVectorType == QGis::Line )
148
p->setPen( QPen(QColor(red, green, blue),
149
(int)(widthScale*mMinimumSymbol->pen().width()) ));//make sure the correct line width is used
153
p->setBrush(QColor(red, green, blue));
154
if (mDrawPolygonOutline)
157
pen.setColor(QColor(0,0,0));
158
pen.setWidthF(widthScale*mMinimumSymbol->pen().width());
162
p->setPen(Qt::NoPen);
166
QPen pen=mMinimumSymbol->pen();
167
pen.setColor(mSelectionColor);
168
QBrush brush=mMinimumSymbol->brush();
169
brush.setColor(mSelectionColor);
176
void QgsContinuousColorRenderer::readXML(const QDomNode& rnode, QgsVectorLayer& vl)
178
mVectorType = vl.vectorType();
179
QDomNode classnode = rnode.namedItem("classificationfield");
180
int classificationfield = classnode.toElement().text().toInt();
181
this->setClassificationField(classificationfield);
184
QDomNode polyoutlinenode = rnode.namedItem("polygonoutline");
185
QString polyoutline = polyoutlinenode.toElement().text();
186
if(polyoutline == "0")
188
mDrawPolygonOutline = false;
190
else if(polyoutline == "1")
192
mDrawPolygonOutline = true;
195
//read the settings for the renderitem of the minimum value
196
QDomNode lowernode = rnode.namedItem("lowestsymbol");
197
QDomNode lsymbolnode = lowernode.namedItem("symbol");
198
if( ! lsymbolnode.isNull() )
200
QgsSymbol* lsy = new QgsSymbol(mVectorType);
201
lsy->readXML ( lsymbolnode );
202
this->setMinimumSymbol(lsy);
204
QDomNode uppernode = rnode.namedItem("highestsymbol");
205
QDomNode usymbolnode = uppernode.namedItem("symbol");
206
if( ! usymbolnode.isNull() )
208
QgsSymbol* usy = new QgsSymbol(mVectorType);
209
usy->readXML ( usymbolnode );
210
this->setMaximumSymbol(usy);
212
vl.setRenderer(this);
215
std::list<int> QgsContinuousColorRenderer::classificationAttributes() const
218
list.push_back(mClassificationField);
222
QString QgsContinuousColorRenderer::name() const
224
return "Continuous Color";
227
bool QgsContinuousColorRenderer::writeXML( QDomNode & layer_node, QDomDocument & document ) const
231
QDomElement continuoussymbol=document.createElement("continuoussymbol");
232
layer_node.appendChild(continuoussymbol);
233
QDomElement classificationfield=document.createElement("classificationfield");
234
QDomText classificationfieldtxt=document.createTextNode(QString::number(mClassificationField));
235
classificationfield.appendChild(classificationfieldtxt);
236
continuoussymbol.appendChild(classificationfield);
239
QDomElement drawPolygonOutlines = document.createElement("polygonoutline");
240
int drawPolyInt = mDrawPolygonOutline ? 1 : 0;
241
QDomText drawPolygonText = document.createTextNode(QString::number(drawPolyInt));
242
drawPolygonOutlines.appendChild(drawPolygonText);
243
continuoussymbol.appendChild(drawPolygonOutlines);
245
QDomElement lowestsymbol=document.createElement("lowestsymbol");
246
continuoussymbol.appendChild(lowestsymbol);
249
mMinimumSymbol->writeXML(lowestsymbol,document);
251
QDomElement highestsymbol=document.createElement("highestsymbol");
252
continuoussymbol.appendChild(highestsymbol);
255
mMaximumSymbol->writeXML(highestsymbol,document);
261
const std::list<QgsSymbol*> QgsContinuousColorRenderer::symbols() const
263
std::list<QgsSymbol*> list;
264
list.push_back(mMinimumSymbol);
265
list.push_back(mMaximumSymbol);
269
QgsRenderer* QgsContinuousColorRenderer::clone() const
271
QgsContinuousColorRenderer* r = new QgsContinuousColorRenderer(*this);