|
3 | 3 |
|
4 | 4 | #include "qgssymbolv2.h" |
5 | 5 | #include "qgssymbollayerv2utils.h" |
| 6 | +#include "qgsvectorcolorrampv2.h" |
6 | 7 |
|
7 | 8 | #include "qgsfeature.h" |
8 | 9 | #include "qgslogger.h" |
@@ -68,14 +69,21 @@ QString QgsRendererRangeV2::dump() |
68 | 69 |
|
69 | 70 |
|
70 | 71 | QgsGraduatedSymbolRendererV2::QgsGraduatedSymbolRendererV2(QString attrName, QgsRangeList ranges) |
71 | | - : QgsFeatureRendererV2(RendererGraduatedSymbol), mAttrName(attrName), mRanges(ranges), mMode(Custom) |
| 72 | + : QgsFeatureRendererV2(RendererGraduatedSymbol), |
| 73 | + mAttrName(attrName), |
| 74 | + mRanges(ranges), |
| 75 | + mMode(Custom), |
| 76 | + mSourceSymbol(NULL), |
| 77 | + mSourceColorRamp(NULL) |
72 | 78 | { |
73 | 79 | // TODO: check ranges for sanity (NULL symbols, invalid ranges) |
74 | 80 | } |
75 | 81 |
|
76 | 82 | QgsGraduatedSymbolRendererV2::~QgsGraduatedSymbolRendererV2() |
77 | 83 | { |
78 | 84 | mRanges.clear(); // should delete all the symbols |
| 85 | + delete mSourceSymbol; |
| 86 | + delete mSourceColorRamp; |
79 | 87 | } |
80 | 88 |
|
81 | 89 | QgsSymbolV2* QgsGraduatedSymbolRendererV2::symbolForValue(double value) |
@@ -155,6 +163,10 @@ QString QgsGraduatedSymbolRendererV2::dump() |
155 | 163 | QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::clone() |
156 | 164 | { |
157 | 165 | QgsGraduatedSymbolRendererV2* r = new QgsGraduatedSymbolRendererV2( mAttrName, mRanges ); |
| 166 | + if (mSourceSymbol) |
| 167 | + r->setSourceSymbol(mSourceSymbol->clone()); |
| 168 | + if (mSourceColorRamp) |
| 169 | + r->setSourceColorRamp(mSourceColorRamp->clone()); |
158 | 170 | r->setUsingSymbolLevels( usingSymbolLevels() ); |
159 | 171 | return r; |
160 | 172 | } |
@@ -275,7 +287,11 @@ QgsGraduatedSymbolRendererV2* QgsGraduatedSymbolRendererV2::createRenderer( |
275 | 287 | ranges.append( QgsRendererRangeV2(lower, upper, newSymbol, label) ); |
276 | 288 | } |
277 | 289 |
|
278 | | - return new QgsGraduatedSymbolRendererV2( attrName, ranges ); |
| 290 | + QgsGraduatedSymbolRendererV2* r = new QgsGraduatedSymbolRendererV2( attrName, ranges ); |
| 291 | + r->setSourceSymbol( symbol->clone() ); |
| 292 | + r->setSourceColorRamp( ramp->clone() ); |
| 293 | + r->setMode(mode); |
| 294 | + return r; |
279 | 295 | } |
280 | 296 |
|
281 | 297 |
|
@@ -318,6 +334,36 @@ QgsFeatureRendererV2* QgsGraduatedSymbolRendererV2::create(QDomElement& element) |
318 | 334 | // delete symbols if there are any more |
319 | 335 | QgsSymbolLayerV2Utils::clearSymbolMap(symbolMap); |
320 | 336 |
|
| 337 | + // try to load source symbol (optional) |
| 338 | + QDomElement sourceSymbolElem = element.firstChildElement("source-symbol"); |
| 339 | + if (!sourceSymbolElem.isNull()) |
| 340 | + { |
| 341 | + QgsSymbolV2Map sourceSymbolMap = QgsSymbolLayerV2Utils::loadSymbols(sourceSymbolElem); |
| 342 | + if (sourceSymbolMap.contains("0")) |
| 343 | + { |
| 344 | + r->setSourceSymbol( sourceSymbolMap.take("0") ); |
| 345 | + } |
| 346 | + QgsSymbolLayerV2Utils::clearSymbolMap(sourceSymbolMap); |
| 347 | + } |
| 348 | + |
| 349 | + // try to load color ramp (optional) |
| 350 | + QDomElement sourceColorRampElem = element.firstChildElement("colorramp"); |
| 351 | + if (!sourceColorRampElem.isNull() && sourceColorRampElem.attribute("name") == "[source]") |
| 352 | + { |
| 353 | + r->setSourceColorRamp( QgsSymbolLayerV2Utils::loadColorRamp(sourceColorRampElem) ); |
| 354 | + } |
| 355 | + |
| 356 | + // try to load mode |
| 357 | + QDomElement modeElem = element.firstChildElement("mode"); |
| 358 | + if (!modeElem.isNull()) |
| 359 | + { |
| 360 | + QString modeString = modeElem.attribute("name"); |
| 361 | + if (modeString == "equal") |
| 362 | + r->setMode(EqualInterval); |
| 363 | + else if (modeString == "quantile") |
| 364 | + r->setMode(Quantile); |
| 365 | + } |
| 366 | + |
321 | 367 | // TODO: symbol levels |
322 | 368 | return r; |
323 | 369 | } |
@@ -351,8 +397,57 @@ QDomElement QgsGraduatedSymbolRendererV2::save(QDomDocument& doc) |
351 | 397 | rendererElem.appendChild(rangesElem); |
352 | 398 |
|
353 | 399 | // save symbols |
354 | | - QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols(symbols, doc); |
| 400 | + QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols(symbols, "symbols", doc); |
355 | 401 | rendererElem.appendChild(symbolsElem); |
356 | 402 |
|
| 403 | + // save source symbol |
| 404 | + if (mSourceSymbol) |
| 405 | + { |
| 406 | + QgsSymbolV2Map sourceSymbols; |
| 407 | + sourceSymbols.insert("0", mSourceSymbol); |
| 408 | + QDomElement sourceSymbolElem = QgsSymbolLayerV2Utils::saveSymbols(sourceSymbols, "source-symbol", doc); |
| 409 | + rendererElem.appendChild(sourceSymbolElem); |
| 410 | + } |
| 411 | + |
| 412 | + // save source color ramp |
| 413 | + if (mSourceColorRamp) |
| 414 | + { |
| 415 | + QDomElement colorRampElem = QgsSymbolLayerV2Utils::saveColorRamp("[source]", mSourceColorRamp, doc); |
| 416 | + rendererElem.appendChild(colorRampElem); |
| 417 | + } |
| 418 | + |
| 419 | + // save mode |
| 420 | + QString modeString; |
| 421 | + if (mMode == EqualInterval) |
| 422 | + modeString = "equal"; |
| 423 | + else if (mMode == Quantile) |
| 424 | + modeString = "quantile"; |
| 425 | + if (!modeString.isEmpty()) |
| 426 | + { |
| 427 | + QDomElement modeElem = doc.createElement("mode"); |
| 428 | + modeElem.setAttribute("name", modeString); |
| 429 | + rendererElem.appendChild(modeElem); |
| 430 | + } |
| 431 | + |
357 | 432 | return rendererElem; |
358 | 433 | } |
| 434 | + |
| 435 | +QgsSymbolV2* QgsGraduatedSymbolRendererV2::sourceSymbol() |
| 436 | +{ |
| 437 | + return mSourceSymbol; |
| 438 | +} |
| 439 | +void QgsGraduatedSymbolRendererV2::setSourceSymbol(QgsSymbolV2* sym) |
| 440 | +{ |
| 441 | + delete mSourceSymbol; |
| 442 | + mSourceSymbol = sym; |
| 443 | +} |
| 444 | + |
| 445 | +QgsVectorColorRampV2* QgsGraduatedSymbolRendererV2::sourceColorRamp() |
| 446 | +{ |
| 447 | + return mSourceColorRamp; |
| 448 | +} |
| 449 | +void QgsGraduatedSymbolRendererV2::setSourceColorRamp(QgsVectorColorRampV2* ramp) |
| 450 | +{ |
| 451 | + delete mSourceColorRamp; |
| 452 | + mSourceColorRamp = ramp; |
| 453 | +} |
0 commit comments