~ubuntu-branches/ubuntu/trusty/qgis/trusty

« back to all changes in this revision

Viewing changes to src/gui/qgsuniquevaluerenderer.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
 
                         qgsuniquevaluerenderer.cpp  -  description
3
 
                             -------------------
4
 
    begin                : July 2004
5
 
    copyright            : (C) 2004 by Marco Hugentobler
6
 
    email                : marco.hugentobler@autoform.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: 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"
24
 
#include <QDomNode>
25
 
#include <QPainter>
26
 
#include <QPixmap>
27
 
#include <vector>
28
 
 
29
 
QgsUniqueValueRenderer::QgsUniqueValueRenderer(QGis::VectorType type): mClassificationField(0)
30
 
{
31
 
    mVectorType = type;
32
 
 
33
 
//call superclass method to set up selection colour
34
 
    initialiseSelectionColor();
35
 
 
36
 
}
37
 
 
38
 
QgsUniqueValueRenderer::QgsUniqueValueRenderer(const QgsUniqueValueRenderer& other)
39
 
{
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)
44
 
    {
45
 
        QgsSymbol* s = new QgsSymbol(*(it->second));
46
 
        insertValue(it->first, s);
47
 
    }
48
 
}
49
 
 
50
 
QgsUniqueValueRenderer& QgsUniqueValueRenderer::operator=(const QgsUniqueValueRenderer& other)
51
 
{
52
 
    if(this != &other)
53
 
    {
54
 
        mVectorType = other.mVectorType;
55
 
        mClassificationField = other.mClassificationField;
56
 
        clearValues();
57
 
        for(std::map<QString, QgsSymbol*>::iterator it=mSymbols.begin(); it!=mSymbols.end(); ++it)
58
 
        {
59
 
            QgsSymbol* s = new QgsSymbol(*(it->second));
60
 
            insertValue(it->first, s);
61
 
        }
62
 
    }
63
 
    return *this;
64
 
}
65
 
 
66
 
QgsUniqueValueRenderer::~QgsUniqueValueRenderer()
67
 
{
68
 
    for(std::map<QString,QgsSymbol*>::iterator it=mSymbols.begin();it!=mSymbols.end();++it)
69
 
    {
70
 
        delete it->second;
71
 
    }
72
 
}
73
 
 
74
 
const std::list<QgsSymbol*> QgsUniqueValueRenderer::symbols() const
75
 
{
76
 
    std::list <QgsSymbol*> symbollist;
77
 
    for(std::map<QString, QgsSymbol*>::const_iterator it = mSymbols.begin(); it!=mSymbols.end(); ++it)
78
 
    {
79
 
        symbollist.push_back(it->second);
80
 
    }
81
 
    return symbollist;
82
 
}
83
 
 
84
 
void QgsUniqueValueRenderer::insertValue(QString name, QgsSymbol* symbol)
85
 
{
86
 
    mSymbols.insert(std::make_pair(name, symbol));
87
 
}
88
 
 
89
 
void QgsUniqueValueRenderer::setClassificationField(int field)
90
 
{
91
 
    mClassificationField=field;
92
 
}
93
 
 
94
 
int QgsUniqueValueRenderer::classificationField()
95
 
{
96
 
    return mClassificationField;
97
 
}
98
 
    
99
 
void QgsUniqueValueRenderer::renderFeature(QPainter* p, QgsFeature* f,QPixmap* pic, 
100
 
        double* scalefactor, bool selected, double widthScale)
101
 
{
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())
106
 
    {
107
 
        QgsSymbol* symbol = it->second;
108
 
 
109
 
        // Point 
110
 
        if ( pic && mVectorType == QGis::Point ) {
111
 
            *pic = symbol->getPointSymbolAsPixmap(  widthScale,
112
 
                                                       selected, mSelectionColor );
113
 
            
114
 
            if ( scalefactor ) *scalefactor = 1;
115
 
        } 
116
 
 
117
 
        // Line, polygon
118
 
        if ( mVectorType != QGis::Point )
119
 
        {
120
 
            if( !selected ) 
121
 
            {
122
 
                QPen pen=symbol->pen();
123
 
                pen.setWidthF ( widthScale * pen.width() );
124
 
                p->setPen(pen);
125
 
                p->setBrush(symbol->brush());
126
 
            }
127
 
            else
128
 
            {
129
 
                QPen pen=symbol->pen();
130
 
                pen.setWidthF ( widthScale * pen.width() );
131
 
                pen.setColor(mSelectionColor);
132
 
                QBrush brush=symbol->brush();
133
 
                brush.setColor(mSelectionColor);
134
 
                p->setPen(pen);
135
 
                p->setBrush(brush);
136
 
            }
137
 
        }
138
 
    }
139
 
    else
140
 
    {
141
 
#ifdef QGISDEBUG
142
 
        qWarning("Warning, no render item found in QgsUniqueValueRenderer::renderFeature");
143
 
#endif
144
 
    }
145
 
    
146
 
}
147
 
 
148
 
void QgsUniqueValueRenderer::readXML(const QDomNode& rnode, QgsVectorLayer& vl)
149
 
{
150
 
    mVectorType = vl.vectorType();
151
 
    QDomNode classnode = rnode.namedItem("classificationfield");
152
 
    int classificationfield = classnode.toElement().text().toInt();
153
 
    this->setClassificationField(classificationfield);
154
 
 
155
 
    QDomNode symbolnode = rnode.namedItem("symbol");
156
 
    while (!symbolnode.isNull())
157
 
    {
158
 
        QgsSymbol* msy = new QgsSymbol(mVectorType);
159
 
        msy->readXML ( symbolnode );
160
 
        this->insertValue(msy->lowerValue(),msy);
161
 
        symbolnode = symbolnode.nextSibling();
162
 
    vl.setRenderer(this);
163
 
    }
164
 
}
165
 
 
166
 
void QgsUniqueValueRenderer::clearValues()
167
 
{
168
 
    for(std::map<QString,QgsSymbol*>::iterator it=mSymbols.begin();it!=mSymbols.end();++it)
169
 
    {
170
 
        delete it->second;
171
 
    }
172
 
    mSymbols.clear();
173
 
}
174
 
 
175
 
QString QgsUniqueValueRenderer::name() const
176
 
{
177
 
    return "Unique Value";
178
 
}
179
 
 
180
 
std::list<int> QgsUniqueValueRenderer::classificationAttributes() const
181
 
{
182
 
    std::list<int> list;
183
 
    list.push_back(mClassificationField);
184
 
    return list;
185
 
}
186
 
 
187
 
bool QgsUniqueValueRenderer::writeXML( QDomNode & layer_node, QDomDocument & document ) const
188
 
{
189
 
    bool returnval=true;
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)
197
 
    {
198
 
        if(!(it->second)->writeXML(uniquevalue,document))
199
 
        {
200
 
            returnval=false;  
201
 
        }
202
 
    }
203
 
    return returnval;
204
 
}
205
 
 
206
 
QgsRenderer* QgsUniqueValueRenderer::clone() const
207
 
{
208
 
    QgsUniqueValueRenderer* r = new QgsUniqueValueRenderer(*this);
209
 
    return r;
210
 
}