[QGIS Commit] r13945 - in trunk/qgis/src: app core ui

svn_qgis at osgeo.org svn_qgis at osgeo.org
Tue Jul 20 09:50:10 EDT 2010


Author: mhugent
Date: 2010-07-20 13:50:09 +0000 (Tue, 20 Jul 2010)
New Revision: 13945

Modified:
   trunk/qgis/src/app/qgslabelinggui.cpp
   trunk/qgis/src/core/qgspallabeling.cpp
   trunk/qgis/src/core/qgspallabeling.h
   trunk/qgis/src/ui/qgslabelingguibase.ui
Log:
[FEATURE]: multiline labels for labeling-ng

Modified: trunk/qgis/src/app/qgslabelinggui.cpp
===================================================================
--- trunk/qgis/src/app/qgslabelinggui.cpp	2010-07-20 13:21:04 UTC (rev 13944)
+++ trunk/qgis/src/app/qgslabelinggui.cpp	2010-07-20 13:50:09 UTC (rev 13945)
@@ -119,6 +119,7 @@
   chkNoObstacle->setChecked( !lyr.obstacle );
   chkLabelPerFeaturePart->setChecked( lyr.labelPerPart );
   chkMergeLines->setChecked( lyr.mergeLines );
+  chkMultiLine->setChecked( lyr.multiLineLabels );
   mMinSizeSpinBox->setValue( lyr.minFeatureSize );
 
   bool scaleBased = ( lyr.scaleMin != 0 && lyr.scaleMax != 0 );
@@ -216,6 +217,7 @@
   lyr.obstacle = !chkNoObstacle->isChecked();
   lyr.labelPerPart = chkLabelPerFeaturePart->isChecked();
   lyr.mergeLines = chkMergeLines->isChecked();
+  lyr.multiLineLabels = chkMultiLine->isChecked();
   if ( chkScaleBasedVisibility->isChecked() )
   {
     lyr.scaleMin = spinScaleMin->value();

Modified: trunk/qgis/src/core/qgspallabeling.cpp
===================================================================
--- trunk/qgis/src/core/qgspallabeling.cpp	2010-07-20 13:21:04 UTC (rev 13944)
+++ trunk/qgis/src/core/qgspallabeling.cpp	2010-07-20 13:50:09 UTC (rev 13945)
@@ -122,6 +122,7 @@
   bufferColor = Qt::white;
   labelPerPart = false;
   mergeLines = false;
+  multiLineLabels = false;
   minFeatureSize = 0.0;
   vectorScaleFactor = 1.0;
   rasterCompressFactor = 1.0;
@@ -145,6 +146,7 @@
   bufferColor = s.bufferColor;
   labelPerPart = s.labelPerPart;
   mergeLines = s.mergeLines;
+  multiLineLabels = s.multiLineLabels;
   minFeatureSize = s.minFeatureSize;
   vectorScaleFactor = s.vectorScaleFactor;
   rasterCompressFactor = s.rasterCompressFactor;
@@ -201,6 +203,7 @@
   bufferColor = _readColor( layer, "labeling/bufferColor" );
   labelPerPart = layer->customProperty( "labeling/labelPerPart" ).toBool();
   mergeLines = layer->customProperty( "labeling/mergeLines" ).toBool();
+  multiLineLabels = layer->customProperty( "labeling/multiLineLabels" ).toBool();
   minFeatureSize = layer->customProperty( "labeling/minFeatureSize" ).toDouble();
 }
 
@@ -229,6 +232,7 @@
   _writeColor( layer, "labeling/bufferColor", bufferColor );
   layer->setCustomProperty( "labeling/labelPerPart", labelPerPart );
   layer->setCustomProperty( "labeling/mergeLines", mergeLines );
+  layer->setCustomProperty( "labeling/multiLineLabels", multiLineLabels );
   layer->setCustomProperty( "labeling/minFeatureSize", minFeatureSize );
 }
 
@@ -273,9 +277,27 @@
 void QgsPalLayerSettings::calculateLabelSize( QString text, double& labelX, double& labelY )
 {
   QRectF labelRect = fontMetrics->boundingRect( text );
-  double w = labelRect.width() / rasterCompressFactor;
-  double h = labelRect.height() / rasterCompressFactor;
-
+  double w, h;
+  if ( !multiLineLabels )
+  {
+    w = labelRect.width() / rasterCompressFactor;
+    h = labelRect.height() / rasterCompressFactor;
+  }
+  else
+  {
+    QStringList multiLineSplit = text.split( "\n" );
+    h = fontMetrics->height() * multiLineSplit.size() / rasterCompressFactor;
+    w = 0;
+    for ( int i = 0; i < multiLineSplit.size(); ++i )
+    {
+      double width = fontMetrics->width( multiLineSplit.at( i ) );
+      if ( width > w )
+      {
+        w = width;
+      }
+      w /= rasterCompressFactor;
+    }
+  }
   QgsPoint ptSize = xform->toMapCoordinates( w, h );
 
   labelX = fabs( ptSize.x() - ptZero.x() );
@@ -650,38 +672,48 @@
 
   //QgsDebugMsg( "drawLabel " + QString::number( drawBuffer ) + " " + txt );
 
-  painter->save();
-  painter->translate( QPointF( outPt.x(), outPt.y() ) );
-  painter->rotate( -label->getAlpha() * 180 / M_PI );
-
-  // scale down painter: the font size has been multiplied by raster scale factor
-  // to workaround a Qt font scaling bug with small font sizes
-  painter->scale( 1.0 / lyr.rasterCompressFactor, 1.0 / lyr.rasterCompressFactor );
-
-  painter->translate( QPointF( 0, - lyr.fontMetrics->descent() ) );
-
-  if ( drawBuffer )
+  QStringList multiLineList;
+  if ( lyr.multiLineLabels )
   {
-    // we're drawing buffer
-    drawLabelBuffer( painter, txt, lyr.textFont, lyr.bufferSize * lyr.vectorScaleFactor * lyr.rasterCompressFactor , lyr.bufferColor );
+    multiLineList = txt.split( "\n" );
   }
   else
   {
-    // we're drawing real label
-    /*painter->setFont( lyr.textFont );
-    painter->setPen( lyr.textColor );
-    painter->drawText((0,0, txt);*/
+    multiLineList << txt;
+  }
 
-    QPainterPath path;
-    path.addText( 0, 0, lyr.textFont, txt );
-    painter->setPen( Qt::NoPen );
-    painter->setBrush( lyr.textColor );
-    painter->drawPath( path );
+  for ( int i = 0; i < multiLineList.size(); ++i )
+  {
+    painter->save();
+    painter->translate( QPointF( outPt.x(), outPt.y() ) );
+    painter->rotate( -label->getAlpha() * 180 / M_PI );
+
+    // scale down painter: the font size has been multiplied by raster scale factor
+    // to workaround a Qt font scaling bug with small font sizes
+    painter->scale( 1.0 / lyr.rasterCompressFactor, 1.0 / lyr.rasterCompressFactor );
+
+    double yMultiLineOffset = ( multiLineList.size() - 1 - i ) * lyr.fontMetrics->height();
+    painter->translate( QPointF( 0, - lyr.fontMetrics->descent() - yMultiLineOffset ) );
+
+    if ( drawBuffer )
+    {
+      // we're drawing buffer
+      drawLabelBuffer( painter, multiLineList.at( i ), lyr.textFont, lyr.bufferSize * lyr.vectorScaleFactor * lyr.rasterCompressFactor , lyr.bufferColor );
+    }
+    else
+    {
+      // we're drawing real label
+      QPainterPath path;
+      path.addText( 0, 0, lyr.textFont, multiLineList.at( i ) );
+      painter->setPen( Qt::NoPen );
+      painter->setBrush( lyr.textColor );
+      painter->drawPath( path );
+    }
+    painter->restore();
+
+    if ( label->getNextPart() )
+      drawLabel( label->getNextPart(), painter, xform, drawBuffer );
   }
-  painter->restore();
-
-  if ( label->getNextPart() )
-    drawLabel( label->getNextPart(), painter, xform, drawBuffer );
 }
 
 

Modified: trunk/qgis/src/core/qgspallabeling.h
===================================================================
--- trunk/qgis/src/core/qgspallabeling.h	2010-07-20 13:21:04 UTC (rev 13944)
+++ trunk/qgis/src/core/qgspallabeling.h	2010-07-20 13:50:09 UTC (rev 13945)
@@ -88,6 +88,7 @@
     QColor bufferColor;
     bool labelPerPart; // whether to label every feature's part or only the biggest one
     bool mergeLines;
+    bool multiLineLabels; //draw labels on multiple lines if they contain '\n'
     double minFeatureSize; // minimum feature size to be labelled (in mm)
 
     // called from register feature hook

Modified: trunk/qgis/src/ui/qgslabelingguibase.ui
===================================================================
--- trunk/qgis/src/ui/qgslabelingguibase.ui	2010-07-20 13:21:04 UTC (rev 13944)
+++ trunk/qgis/src/ui/qgslabelingguibase.ui	2010-07-20 13:50:09 UTC (rev 13945)
@@ -15,8 +15,7 @@
   </property>
   <property name="windowIcon">
    <iconset>
-    <normaloff/>
-   </iconset>
+    <normaloff>../../../../.designer/backup</normaloff>../../../../.designer/backup</iconset>
   </property>
   <layout class="QGridLayout" name="gridLayout_5">
    <item row="0" column="0">
@@ -662,7 +661,7 @@
      </property>
     </widget>
    </item>
-   <item row="6" column="0">
+   <item row="7" column="0">
     <layout class="QHBoxLayout" name="horizontalLayout_10">
      <item>
       <widget class="QLabel" name="label_19">
@@ -680,7 +679,7 @@
      </item>
     </layout>
    </item>
-   <item row="7" column="0">
+   <item row="8" column="0">
     <layout class="QHBoxLayout" name="horizontalLayout_2">
      <item>
       <widget class="QCheckBox" name="chkNoObstacle">
@@ -717,7 +716,7 @@
      </item>
     </layout>
    </item>
-   <item row="8" column="0">
+   <item row="9" column="0">
     <spacer name="verticalSpacer_2">
      <property name="orientation">
       <enum>Qt::Vertical</enum>
@@ -730,7 +729,7 @@
      </property>
     </spacer>
    </item>
-   <item row="9" column="0">
+   <item row="10" column="0">
     <widget class="QDialogButtonBox" name="buttonBox">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
@@ -740,6 +739,13 @@
      </property>
     </widget>
    </item>
+   <item row="6" column="0">
+    <widget class="QCheckBox" name="chkMultiLine">
+     <property name="text">
+      <string>multiline labels</string>
+     </property>
+    </widget>
+   </item>
   </layout>
  </widget>
  <layoutdefault spacing="6" margin="11"/>



More information about the QGIS-commit mailing list