|
41 | 41 | #include "qgsdiagram.h"
|
42 | 42 | #include "qgsdiagramrendererv2.h"
|
43 | 43 | #include "qgslabelsearchtree.h"
|
| 44 | +#include "qgssearchtreenode.h" |
| 45 | +#include "qgssearchstring.h" |
| 46 | + |
44 | 47 | #include <qgslogger.h>
|
45 | 48 | #include <qgsvectorlayer.h>
|
46 | 49 | #include <qgsmaplayerregistry.h>
|
47 | 50 | #include <qgsvectordataprovider.h>
|
48 | 51 | #include <qgsgeometry.h>
|
49 | 52 | #include <qgsmaprenderer.h>
|
50 |
| - |
| 53 | +#include <QMessageBox> |
51 | 54 |
|
52 | 55 | using namespace pal;
|
53 | 56 |
|
@@ -155,6 +158,7 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
|
155 | 158 | {
|
156 | 159 | // copy only permanent stuff
|
157 | 160 | fieldName = s.fieldName;
|
| 161 | + isExpression = s.isExpression; |
158 | 162 | placement = s.placement;
|
159 | 163 | placementFlags = s.placementFlags;
|
160 | 164 | textFont = s.textFont;
|
@@ -276,6 +280,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
|
276 | 280 | return; // there's no information available
|
277 | 281 |
|
278 | 282 | fieldName = layer->customProperty( "labeling/fieldName" ).toString();
|
| 283 | + isExpression = layer->customProperty( "labeling/isExpression").toBool(); |
279 | 284 | placement = ( Placement ) layer->customProperty( "labeling/placement" ).toInt();
|
280 | 285 | placementFlags = layer->customProperty( "labeling/placementFlags" ).toUInt();
|
281 | 286 | QString fontFamily = layer->customProperty( "labeling/fontFamily" ).toString();
|
@@ -311,6 +316,7 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
|
311 | 316 | layer->setCustomProperty( "labeling", "pal" );
|
312 | 317 |
|
313 | 318 | layer->setCustomProperty( "labeling/fieldName", fieldName );
|
| 319 | + layer->setCustomProperty( "labeling/isExpression", isExpression ); |
314 | 320 | layer->setCustomProperty( "labeling/placement", placement );
|
315 | 321 | layer->setCustomProperty( "labeling/placementFlags", ( unsigned int )placementFlags );
|
316 | 322 |
|
@@ -427,9 +433,36 @@ void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF* fm, QString t
|
427 | 433 | labelY = qAbs( ptSize.y() - ptZero.y() );
|
428 | 434 | }
|
429 | 435 |
|
430 |
| -void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext& context ) |
| 436 | +void QgsPalLayerSettings::registerFeature(QgsVectorLayer* layer, QgsFeature& f, const QgsRenderContext& context ) |
431 | 437 | {
|
432 |
| - QString labelText = f.attributeMap()[fieldIndex].toString(); |
| 438 | + QString labelText; |
| 439 | + if (isExpression) |
| 440 | + { |
| 441 | + QgsSearchString searchString; |
| 442 | + // We don't do any validating here as we should only have a vaild expression at this point. |
| 443 | + searchString.setString( fieldName ); |
| 444 | + |
| 445 | + QgsSearchTreeNode* searchTree = searchString.tree(); |
| 446 | + if ( !searchTree ) |
| 447 | + { |
| 448 | + return; |
| 449 | + } |
| 450 | + |
| 451 | + QgsSearchTreeValue outValue; |
| 452 | + searchTree->getValue( outValue, searchTree, layer->dataProvider()->fields() , f ); |
| 453 | + |
| 454 | + if (outValue.isError()) |
| 455 | + { |
| 456 | + QgsDebugMsg("EXPRESSION ERROR = " + outValue.string()); |
| 457 | + return; |
| 458 | + } |
| 459 | + QgsDebugMsg("EXPRESSION OUT VALUE = " + outValue.string()); |
| 460 | + labelText = outValue.string(); |
| 461 | + } |
| 462 | + else |
| 463 | + { |
| 464 | + labelText = f.attributeMap()[fieldIndex].toString(); |
| 465 | + } |
433 | 466 | double labelX, labelY; // will receive label size
|
434 | 467 | QFont labelFont = textFont;
|
435 | 468 |
|
@@ -569,7 +602,7 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
|
569 | 602 | // record the created geometry - it will be deleted at the end.
|
570 | 603 | geometries.append( lbl );
|
571 | 604 |
|
572 |
| - // register feature to the layer |
| 605 | + // feature to the layer |
573 | 606 | try
|
574 | 607 | {
|
575 | 608 | if ( !palLayer->registerFeature( lbl->strId(), lbl, labelX, labelY, labelText.toUtf8().constData(),
|
@@ -689,11 +722,15 @@ int QgsPalLabeling::prepareLayer( QgsVectorLayer* layer, QSet<int>& attrIndices,
|
689 | 722 | if ( !lyrTmp.enabled )
|
690 | 723 | return 0;
|
691 | 724 |
|
692 |
| - // find out which field will be needed |
693 |
| - int fldIndex = layer->fieldNameIndex( lyrTmp.fieldName ); |
694 |
| - if ( fldIndex == -1 ) |
695 |
| - return 0; |
696 |
| - attrIndices.insert( fldIndex ); |
| 725 | + // If we aren't an expression, we check to see if we can find the column. |
| 726 | + int fldIndex ; |
| 727 | + if (!lyrTmp.isExpression) |
| 728 | + { |
| 729 | + fldIndex = layer->fieldNameIndex( lyrTmp.fieldName ); |
| 730 | + if ( fldIndex == -1) |
| 731 | + return 0; |
| 732 | + attrIndices.insert( fldIndex ); |
| 733 | + } |
697 | 734 |
|
698 | 735 | //add indices of data defined fields
|
699 | 736 | QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator dIt = lyrTmp.dataDefinedProperties.constBegin();
|
@@ -782,7 +819,7 @@ int QgsPalLabeling::addDiagramLayer( QgsVectorLayer* layer, QgsDiagramLayerSetti
|
782 | 819 | void QgsPalLabeling::registerFeature( QgsVectorLayer* layer, QgsFeature& f, const QgsRenderContext& context )
|
783 | 820 | {
|
784 | 821 | QgsPalLayerSettings& lyr = mActiveLayers[layer];
|
785 |
| - lyr.registerFeature( f, context ); |
| 822 | + lyr.registerFeature(layer, f, context ); |
786 | 823 | }
|
787 | 824 |
|
788 | 825 | void QgsPalLabeling::registerDiagramFeature( QgsVectorLayer* layer, QgsFeature& feat, const QgsRenderContext& context )
|
@@ -836,7 +873,7 @@ void QgsPalLabeling::registerDiagramFeature( QgsVectorLayer* layer, QgsFeature&
|
836 | 873 | }
|
837 | 874 | }
|
838 | 875 |
|
839 |
| - // register feature to the layer |
| 876 | + // feature to the layer |
840 | 877 | int ddColX = layerIt.value().xPosColumn;
|
841 | 878 | int ddColY = layerIt.value().yPosColumn;
|
842 | 879 | double ddPosX = 0.0;
|
@@ -1212,7 +1249,6 @@ void QgsPalLabeling::drawLabel( pal::LabelPosition* label, QPainter* painter, co
|
1212 | 1249 |
|
1213 | 1250 | // TODO: optimize access :)
|
1214 | 1251 | const QgsPalLayerSettings& lyr = layer( label->getLayerName() );
|
1215 |
| - |
1216 | 1252 | QString text = (( QgsPalGeometry* )label->getFeaturePart()->getUserGeometry() )->text();
|
1217 | 1253 | QString txt = ( label->getPartId() == -1 ? text : QString( text[label->getPartId()] ) );
|
1218 | 1254 |
|
|
0 commit comments