~ubuntu-branches/ubuntu/maverick/qgis/maverick

« back to all changes in this revision

Viewing changes to src/gui/qgscontinuouscolorrenderer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
                         qgscontinuouscolorrenderer.cpp  -  description
3
 
                             -------------------
4
 
    begin                : Nov 2003
5
 
    copyright            : (C) 2003 by Marco Hugentobler
6
 
    email                : mhugent@geo.unizh.ch
7
 
 ***************************************************************************/
8
 
 
9
 
/***************************************************************************
10
 
 *                                                                         *
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.                                   *
15
 
 *                                                                         *
16
 
 ***************************************************************************/
17
 
/* $Id: qgscontinuouscolorrenderer.cpp 5834 2006-09-18 09:31:12Z mhugent $ */
18
 
#include "qgscontinuouscolorrenderer.h"
19
 
#include "qgis.h"
20
 
#include "qgsvectorlayer.h"
21
 
#include <cfloat>
22
 
#include "qgslegenditem.h"
23
 
#include "qgssymbologyutils.h"
24
 
#include "qgsmarkercatalogue.h"
25
 
#include "qgssymbol.h"
26
 
 
27
 
#include <QDomElement>
28
 
#include <QPainter>
29
 
#include <QPixmap>
30
 
 
31
 
QgsContinuousColorRenderer::QgsContinuousColorRenderer(QGis::VectorType type): mMinimumSymbol(0), mMaximumSymbol(0)
32
 
{
33
 
    mVectorType = type;
34
 
    //call superclass method to set up selection colour
35
 
    initialiseSelectionColor();
36
 
}
37
 
 
38
 
QgsContinuousColorRenderer::QgsContinuousColorRenderer(const QgsContinuousColorRenderer& other)
39
 
{
40
 
    mVectorType = other.mVectorType;
41
 
    mClassificationField = other.mClassificationField;
42
 
    mMinimumSymbol = new QgsSymbol(*other.mMinimumSymbol);
43
 
    mMaximumSymbol = new QgsSymbol(*other.mMaximumSymbol);
44
 
}
45
 
 
46
 
QgsContinuousColorRenderer& QgsContinuousColorRenderer::operator=(const QgsContinuousColorRenderer& other)
47
 
{
48
 
    if(this != &other)
49
 
    {
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);
56
 
    }
57
 
    return *this;
58
 
}
59
 
 
60
 
QgsContinuousColorRenderer::~QgsContinuousColorRenderer()
61
 
{
62
 
  delete mMinimumSymbol;
63
 
  delete mMaximumSymbol;
64
 
}
65
 
 
66
 
void QgsContinuousColorRenderer::setMinimumSymbol(QgsSymbol* sy)
67
 
{
68
 
  delete mMinimumSymbol;
69
 
  mMinimumSymbol = sy;
70
 
}
71
 
 
72
 
void QgsContinuousColorRenderer::setMaximumSymbol(QgsSymbol* sy)
73
 
{
74
 
  delete mMaximumSymbol;
75
 
  mMaximumSymbol = sy;
76
 
}
77
 
 
78
 
void QgsContinuousColorRenderer::renderFeature(QPainter * p, QgsFeature * f, QPixmap* pic, 
79
 
        double* scalefactor, bool selected, double widthScale)
80
 
{
81
 
  if ((mMinimumSymbol && mMaximumSymbol))
82
 
  {
83
 
    //first find out the value for the classification attribute
84
 
    std::vector < QgsFeatureAttribute > vec = f->attributeMap();
85
 
    double fvalue = vec[0].fieldValue().toDouble();
86
 
 
87
 
    //double fvalue = vec[mClassificationField].fieldValue().toDouble();
88
 
    double minvalue = mMinimumSymbol->lowerValue().toDouble();
89
 
    double maxvalue = mMaximumSymbol->lowerValue().toDouble();
90
 
 
91
 
    QColor mincolor, maxcolor;
92
 
 
93
 
    if ( mVectorType == QGis::Line || mVectorType == QGis::Point )
94
 
    {
95
 
      mincolor = mMinimumSymbol->pen().color();
96
 
      maxcolor = mMaximumSymbol->pen().color();
97
 
    } 
98
 
    else                    //if polygon
99
 
    {
100
 
      p->setPen(mMinimumSymbol->pen());
101
 
      mincolor = mMinimumSymbol->fillColor();
102
 
      maxcolor = mMaximumSymbol->fillColor();
103
 
    }
104
 
 
105
 
    int red,green,blue;
106
 
 
107
 
    if((maxvalue - minvalue)!=0)
108
 
    {
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));
112
 
    }
113
 
    else
114
 
    {
115
 
      red = int (mincolor.red());
116
 
      green = int (mincolor.green());
117
 
      blue = int (mincolor.blue());
118
 
    }
119
 
 
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
123
 
 
124
 
 
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() );
130
 
 
131
 
      QBrush brush = mMinimumSymbol->brush();
132
 
 
133
 
      if ( selected ) {
134
 
        pen.setColor ( mSelectionColor );
135
 
        brush.setColor ( mSelectionColor );
136
 
      } else {
137
 
        brush.setColor ( QColor(red, green, blue) );
138
 
      }
139
 
      brush.setStyle ( Qt::SolidPattern );
140
 
 
141
 
      *pic = QgsMarkerCatalogue::instance()->pixmapMarker ( mMinimumSymbol->pointSymbolName(), mMinimumSymbol->pointSize(),
142
 
          pen, brush);
143
 
 
144
 
      if ( scalefactor ) *scalefactor = 1;
145
 
    } 
146
 
    else if ( mVectorType == QGis::Line )
147
 
    {
148
 
      p->setPen( QPen(QColor(red, green, blue),
149
 
            (int)(widthScale*mMinimumSymbol->pen().width()) ));//make sure the correct line width is used
150
 
    } 
151
 
    else
152
 
    {
153
 
      p->setBrush(QColor(red, green, blue));
154
 
      if (mDrawPolygonOutline)
155
 
      {
156
 
        QPen pen;
157
 
        pen.setColor(QColor(0,0,0));
158
 
        pen.setWidthF(widthScale*mMinimumSymbol->pen().width());
159
 
        p->setPen(pen);
160
 
      }
161
 
      else
162
 
        p->setPen(Qt::NoPen);
163
 
    }
164
 
    if(selected)
165
 
    {
166
 
      QPen pen=mMinimumSymbol->pen();
167
 
      pen.setColor(mSelectionColor);
168
 
      QBrush brush=mMinimumSymbol->brush();
169
 
      brush.setColor(mSelectionColor);
170
 
      p->setPen(pen);
171
 
      p->setBrush(brush);
172
 
    }
173
 
  }
174
 
}
175
 
 
176
 
void QgsContinuousColorRenderer::readXML(const QDomNode& rnode, QgsVectorLayer& vl)
177
 
{
178
 
    mVectorType = vl.vectorType();
179
 
    QDomNode classnode = rnode.namedItem("classificationfield");
180
 
    int classificationfield = classnode.toElement().text().toInt();
181
 
    this->setClassificationField(classificationfield);
182
 
    
183
 
    //polygon outline
184
 
    QDomNode polyoutlinenode = rnode.namedItem("polygonoutline");
185
 
    QString polyoutline = polyoutlinenode.toElement().text();
186
 
    if(polyoutline == "0")
187
 
    {
188
 
            mDrawPolygonOutline = false;
189
 
    }
190
 
    else if(polyoutline == "1")
191
 
    {
192
 
            mDrawPolygonOutline = true;
193
 
    }
194
 
 
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() )
199
 
    {
200
 
        QgsSymbol* lsy = new QgsSymbol(mVectorType);
201
 
        lsy->readXML ( lsymbolnode );
202
 
        this->setMinimumSymbol(lsy);
203
 
    }
204
 
    QDomNode uppernode = rnode.namedItem("highestsymbol");
205
 
    QDomNode usymbolnode = uppernode.namedItem("symbol");
206
 
    if( ! usymbolnode.isNull() )
207
 
    {
208
 
        QgsSymbol* usy = new QgsSymbol(mVectorType);
209
 
        usy->readXML ( usymbolnode );
210
 
        this->setMaximumSymbol(usy);
211
 
    }
212
 
    vl.setRenderer(this);
213
 
}
214
 
 
215
 
std::list<int> QgsContinuousColorRenderer::classificationAttributes() const
216
 
{
217
 
    std::list<int> list;
218
 
    list.push_back(mClassificationField);
219
 
    return list;
220
 
}
221
 
 
222
 
QString QgsContinuousColorRenderer::name() const
223
 
{
224
 
    return "Continuous Color";
225
 
}
226
 
 
227
 
bool QgsContinuousColorRenderer::writeXML( QDomNode & layer_node, QDomDocument & document ) const
228
 
{
229
 
    bool returnval=true;
230
 
#ifndef WIN32
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);
237
 
    
238
 
    //polygon outlines
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);
244
 
    
245
 
    QDomElement lowestsymbol=document.createElement("lowestsymbol");
246
 
    continuoussymbol.appendChild(lowestsymbol);
247
 
    if(mMinimumSymbol)
248
 
    {
249
 
        mMinimumSymbol->writeXML(lowestsymbol,document);
250
 
    }
251
 
    QDomElement highestsymbol=document.createElement("highestsymbol");
252
 
    continuoussymbol.appendChild(highestsymbol);
253
 
    if(mMaximumSymbol)
254
 
    {
255
 
        mMaximumSymbol->writeXML(highestsymbol,document);
256
 
    }
257
 
#endif
258
 
    return returnval;
259
 
}
260
 
 
261
 
const std::list<QgsSymbol*> QgsContinuousColorRenderer::symbols() const
262
 
{
263
 
    std::list<QgsSymbol*> list;
264
 
    list.push_back(mMinimumSymbol);
265
 
    list.push_back(mMaximumSymbol);
266
 
    return list;
267
 
}
268
 
 
269
 
QgsRenderer* QgsContinuousColorRenderer::clone() const
270
 
{
271
 
    QgsContinuousColorRenderer* r = new QgsContinuousColorRenderer(*this);
272
 
    return r;
273
 
}