~ubuntu-branches/ubuntu/quantal/qgis/quantal

« back to all changes in this revision

Viewing changes to src/gui/qgssinglesymbolrenderer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): William Grant
  • Date: 2007-05-06 13:42:32 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070506134232-pyli6t388w5asd8x
Tags: 0.8.0-3ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - debian/rules, debian/qgis.install, debian/qgis.dirs debian/qgis.desktop:
    Add and install .desktop.
* debian/qgis.desktop: Remove Applications category; it's not real.
* Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                         qgssinglesymbolrenderer.cpp  -  description
 
3
                             -------------------
 
4
    begin                : Oct 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: qgssinglesymbolrenderer.cpp 5705 2006-08-17 10:40:00Z g_j_m $ */
 
18
#include "qgis.h"
 
19
#include "qgssinglesymbolrenderer.h"
 
20
#include "qgsfeature.h"
 
21
#include "qgis.h"
 
22
#include "qgsvectorlayer.h"
 
23
#include "qgslegenditem.h"
 
24
#include "qgssymbologyutils.h"
 
25
 
 
26
#include <QString>
 
27
#include <QDomNode>
 
28
#include <QPainter>
 
29
#include <QPixmap>
 
30
 
 
31
QgsSingleSymbolRenderer::QgsSingleSymbolRenderer(QGis::VectorType type)
 
32
{
 
33
    mVectorType=type;
 
34
    //call superclass method to set up selection colour
 
35
    initialiseSelectionColor();
 
36
  
 
37
    //initial setting based on random color
 
38
    QgsSymbol* sy = new QgsSymbol(mVectorType);
 
39
  
 
40
    //random fill colors for points and polygons and pen colors for lines
 
41
    int red = 1 + (int) (255.0 * rand() / (RAND_MAX + 1.0));
 
42
    int green = 1 + (int) (255.0 * rand() / (RAND_MAX + 1.0));
 
43
    int blue = 1 + (int) (255.0 * rand() / (RAND_MAX + 1.0));
 
44
 
 
45
    if (type == QGis::Line)
 
46
    {
 
47
        sy->setColor(QColor(red, green, blue));
 
48
    } 
 
49
    else
 
50
    {
 
51
        sy->setFillColor(QColor(red, green, blue));
 
52
        sy->setFillStyle(Qt::SolidPattern);
 
53
        sy->setColor(QColor(0, 0, 0));
 
54
    }
 
55
    sy->setLineWidth(1);
 
56
    mSymbol=sy;
 
57
}
 
58
 
 
59
QgsSingleSymbolRenderer::QgsSingleSymbolRenderer(const QgsSingleSymbolRenderer& other)
 
60
{
 
61
    mVectorType = other.mVectorType;
 
62
    mSymbol = new QgsSymbol(*other.mSymbol);
 
63
}
 
64
 
 
65
QgsSingleSymbolRenderer& QgsSingleSymbolRenderer::operator=(const QgsSingleSymbolRenderer& other)
 
66
{
 
67
    if(this!=&other)
 
68
    {
 
69
        mVectorType = other.mVectorType;
 
70
        delete mSymbol;
 
71
        mSymbol = new QgsSymbol(*other.mSymbol);
 
72
    }
 
73
    return *this;
 
74
}
 
75
 
 
76
QgsSingleSymbolRenderer::~QgsSingleSymbolRenderer()
 
77
{
 
78
    delete mSymbol;
 
79
}
 
80
 
 
81
void QgsSingleSymbolRenderer::addSymbol(QgsSymbol* sy)
 
82
{
 
83
    delete mSymbol;
 
84
    mSymbol=sy;
 
85
}
 
86
 
 
87
void QgsSingleSymbolRenderer::renderFeature(QPainter * p, QgsFeature * f, QPixmap* pic, 
 
88
                 double* scalefactor, bool selected, double widthScale)
 
89
{
 
90
        // Point 
 
91
        if ( pic && mVectorType == QGis::Point) {
 
92
            *pic = mSymbol->getPointSymbolAsPixmap(  widthScale, 
 
93
                                         selected, mSelectionColor );
 
94
            
 
95
            if ( scalefactor ) *scalefactor = 1;
 
96
        } 
 
97
 
 
98
        // Line, polygon
 
99
        if ( mVectorType != QGis::Point )
 
100
        {
 
101
            if( !selected ) 
 
102
            {
 
103
                QPen pen=mSymbol->pen();
 
104
                pen.setWidthF ( widthScale * pen.width() );
 
105
                p->setPen(pen);
 
106
                p->setBrush(mSymbol->brush());
 
107
            }
 
108
            else
 
109
            {
 
110
                QPen pen=mSymbol->pen();
 
111
                pen.setWidthF ( widthScale * pen.width() );
 
112
                if (mVectorType == QGis::Line)
 
113
                  pen.setColor(mSelectionColor);
 
114
                QBrush brush=mSymbol->brush();
 
115
                brush.setColor(mSelectionColor);
 
116
                p->setPen(pen);
 
117
                p->setBrush(brush);
 
118
            }
 
119
        }
 
120
}
 
121
 
 
122
void QgsSingleSymbolRenderer::readXML(const QDomNode& rnode, QgsVectorLayer& vl)
 
123
{
 
124
    mVectorType = vl.vectorType();
 
125
    QgsSymbol* sy = new QgsSymbol(mVectorType);
 
126
 
 
127
    QDomNode synode = rnode.namedItem("symbol");
 
128
    
 
129
    if ( synode.isNull() )
 
130
    {
 
131
        qDebug( "%s:%d in project file no symbol node in renderitem DOM" );
 
132
        // XXX abort?
 
133
    }
 
134
    else
 
135
    {
 
136
        sy->readXML ( synode );
 
137
    }
 
138
 
 
139
    //create a renderer and add it to the vector layer
 
140
    this->addSymbol(sy);
 
141
    vl.setRenderer(this);
 
142
}
 
143
 
 
144
bool QgsSingleSymbolRenderer::writeXML( QDomNode & layer_node, QDomDocument & document ) const
 
145
{
 
146
  bool returnval=false;
 
147
  QDomElement singlesymbol=document.createElement("singlesymbol");
 
148
  layer_node.appendChild(singlesymbol);
 
149
  if(mSymbol)
 
150
  {
 
151
    returnval=mSymbol->writeXML(singlesymbol,document);
 
152
  }
 
153
  return returnval;
 
154
}
 
155
 
 
156
 
 
157
std::list<int> QgsSingleSymbolRenderer::classificationAttributes() const
 
158
{
 
159
  std::list<int> list;
 
160
  return list;//return an empty list
 
161
}
 
162
 
 
163
QString QgsSingleSymbolRenderer::name() const
 
164
{
 
165
  return "Single Symbol";
 
166
}
 
167
 
 
168
const std::list<QgsSymbol*> QgsSingleSymbolRenderer::symbols() const
 
169
{
 
170
    std::list<QgsSymbol*> list;
 
171
    list.push_back(mSymbol);
 
172
    return list;
 
173
}
 
174
 
 
175
QgsRenderer* QgsSingleSymbolRenderer::clone() const
 
176
{
 
177
    QgsSingleSymbolRenderer* r = new QgsSingleSymbolRenderer(*this);
 
178
    return r;
 
179
}