Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
qgssinglesymbolrendererv2.cpp
Go to the documentation of this file.
1 
3 
4 #include "qgssymbolv2.h"
6 
7 #include "qgslogger.h"
8 #include "qgsfeature.h"
9 #include "qgsvectorlayer.h"
10 
11 #include <QDomDocument>
12 #include <QDomElement>
13 
15  : QgsFeatureRendererV2( "singleSymbol" ), mRotationFieldIdx( -1 ), mSizeScaleFieldIdx( -1 ), mTempSymbol( NULL )
16 {
17  Q_ASSERT( symbol );
18  mSymbol = symbol;
19 }
20 
22 {
23  delete mSymbol;
24 }
25 
27 {
28  if ( mRotationFieldIdx == -1 && mSizeScaleFieldIdx == -1 )
29  return mSymbol;
30 
31  double rotation = 0;
32  double sizeScale = 1;
33  if ( mRotationFieldIdx != -1 )
34  {
35  rotation = feature.attributeMap()[mRotationFieldIdx].toDouble();
36  }
37  if ( mSizeScaleFieldIdx != -1 )
38  {
39  sizeScale = feature.attributeMap()[mSizeScaleFieldIdx].toDouble();
40  }
41 
43  {
44  QgsMarkerSymbolV2* markerSymbol = static_cast<QgsMarkerSymbolV2*>( mTempSymbol );
45  if ( mRotationFieldIdx != -1 )
46  markerSymbol->setAngle( rotation );
47  if ( mSizeScaleFieldIdx != -1 )
48  markerSymbol->setSize( sizeScale * mOrigSize );
49  }
50  else if ( mTempSymbol->type() == QgsSymbolV2::Line )
51  {
52  QgsLineSymbolV2* lineSymbol = static_cast<QgsLineSymbolV2*>( mTempSymbol );
53  if ( mSizeScaleFieldIdx != -1 )
54  lineSymbol->setWidth( sizeScale * mOrigSize );
55  }
56  else if ( mTempSymbol->type() == QgsSymbolV2::Fill )
57  {
58  QgsFillSymbolV2* fillSymbol = static_cast<QgsFillSymbolV2*>( mTempSymbol );
59  if ( mRotationFieldIdx != -1 )
60  fillSymbol->setAngle( rotation );
61  }
62 
63  return mTempSymbol;
64 }
65 
67 {
68  if ( !mSymbol )
69  {
70  return;
71  }
72  mRotationFieldIdx = mRotationField.isEmpty() ? -1 : vlayer->fieldNameIndex( mRotationField );
74 
75  mSymbol->startRender( context );
76 
77  if ( mRotationFieldIdx != -1 || mSizeScaleFieldIdx != -1 )
78  {
79  // we are going to need a temporary symbol
81 
82  int hints = 0;
85  mTempSymbol->setRenderHints( hints );
86 
87  mTempSymbol->startRender( context );
88 
89  if ( mSymbol->type() == QgsSymbolV2::Marker )
90  {
91  mOrigSize = static_cast<QgsMarkerSymbolV2*>( mSymbol )->size();
92  }
93  else if ( mSymbol->type() == QgsSymbolV2::Line )
94  {
95  mOrigSize = static_cast<QgsLineSymbolV2*>( mSymbol )->width();
96  }
97  else
98  {
99  mOrigSize = 0;
100  }
101  }
102 }
103 
105 {
106  if ( !mSymbol )
107  {
108  return;
109  }
110  mSymbol->stopRender( context );
111 
112  if ( mRotationFieldIdx != -1 || mSizeScaleFieldIdx != -1 )
113  {
114  // we are going to need a temporary symbol
115  mTempSymbol->stopRender( context );
116  delete mTempSymbol;
117  mTempSymbol = NULL;
118  }
119 }
120 
122 {
123  QList<QString> lst;
124  if ( !mRotationField.isEmpty() )
125  lst.append( mRotationField );
126  if ( !mSizeScaleField.isEmpty() )
127  lst.append( mSizeScaleField );
128  return lst;
129 }
130 
132 {
133  return mSymbol;
134 }
135 
137 {
138  Q_ASSERT( s );
139  delete mSymbol;
140  mSymbol = s;
141 }
142 
144 {
145  if ( mSymbol )
146  {
147  return QString( "SINGLE: %1" ).arg( mSymbol->dump() );
148  }
149  else
150  {
151  return "";
152  }
153 }
154 
156 {
161  return r;
162 }
163 
165 {
166  QgsSymbolV2List lst;
167  lst.append( mSymbol );
168  return lst;
169 }
170 
172 {
173  QDomElement symbolsElem = element.firstChildElement( "symbols" );
174  if ( symbolsElem.isNull() )
175  return NULL;
176 
177  QgsSymbolV2Map symbolMap = QgsSymbolLayerV2Utils::loadSymbols( symbolsElem );
178 
179  if ( !symbolMap.contains( "0" ) )
180  return NULL;
181 
182  QgsSingleSymbolRendererV2* r = new QgsSingleSymbolRendererV2( symbolMap.take( "0" ) );
183 
184  // delete symbols if there are any more
186 
187  QDomElement rotationElem = element.firstChildElement( "rotation" );
188  if ( !rotationElem.isNull() )
189  r->setRotationField( rotationElem.attribute( "field" ) );
190 
191  QDomElement sizeScaleElem = element.firstChildElement( "sizescale" );
192  if ( !sizeScaleElem.isNull() )
193  r->setSizeScaleField( sizeScaleElem.attribute( "field" ) );
194 
195  // TODO: symbol levels
196  return r;
197 }
198 
199 QDomElement QgsSingleSymbolRendererV2::save( QDomDocument& doc )
200 {
201  QDomElement rendererElem = doc.createElement( RENDERER_TAG_NAME );
202  rendererElem.setAttribute( "type", "singleSymbol" );
203  rendererElem.setAttribute( "symbollevels", ( mUsingSymbolLevels ? "1" : "0" ) );
204 
206  symbols["0"] = mSymbol;
207  QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols( symbols, "symbols", doc );
208  rendererElem.appendChild( symbolsElem );
209 
210  QDomElement rotationElem = doc.createElement( "rotation" );
211  rotationElem.setAttribute( "field", mRotationField );
212  rendererElem.appendChild( rotationElem );
213 
214  QDomElement sizeScaleElem = doc.createElement( "sizescale" );
215  sizeScaleElem.setAttribute( "field", mSizeScaleField );
216  rendererElem.appendChild( sizeScaleElem );
217 
218  return rendererElem;
219 }
220 
222 {
224  if ( mSymbol )
225  {
226  QPixmap pix = QgsSymbolLayerV2Utils::symbolPreviewPixmap( mSymbol, iconSize );
227  lst << qMakePair( QString(), pix );
228  }
229  return lst;
230 }
231 
233 {
235  lst << qMakePair( QString(), mSymbol );
236  return lst;
237 }