[QGIS Commit] r13599 - trunk/qgis/src/plugins/labeling

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sat May 29 18:56:40 EDT 2010


Author: wonder
Date: 2010-05-29 18:56:39 -0400 (Sat, 29 May 2010)
New Revision: 13599

Modified:
   trunk/qgis/src/plugins/labeling/labelinggui.cpp
   trunk/qgis/src/plugins/labeling/labelingguibase.ui
   trunk/qgis/src/plugins/labeling/labelpreview.cpp
   trunk/qgis/src/plugins/labeling/labelpreview.h
   trunk/qgis/src/plugins/labeling/pallabeling.cpp
   trunk/qgis/src/plugins/labeling/pallabeling.h
Log:
Applied patch #2739 from Marco - correct scaling of labeling from the labeling plugin in composer.
Includes few tweaks and improvements, additionally the buffer is not considered for the label frame.


Modified: trunk/qgis/src/plugins/labeling/labelinggui.cpp
===================================================================
--- trunk/qgis/src/plugins/labeling/labelinggui.cpp	2010-05-29 22:10:55 UTC (rev 13598)
+++ trunk/qgis/src/plugins/labeling/labelinggui.cpp	2010-05-29 22:56:39 UTC (rev 13599)
@@ -41,7 +41,7 @@
   connect( btnChangeFont, SIGNAL( clicked() ), this, SLOT( changeTextFont() ) );
   connect( chkBuffer, SIGNAL( toggled( bool ) ), this, SLOT( updatePreview() ) );
   connect( btnBufferColor, SIGNAL( clicked() ), this, SLOT( changeBufferColor() ) );
-  connect( spinBufferSize, SIGNAL( valueChanged( int ) ), this, SLOT( updatePreview() ) );
+  connect( spinBufferSize, SIGNAL( valueChanged( double ) ), this, SLOT( updatePreview() ) );
   connect( btnEngineSettings, SIGNAL( clicked() ), this, SLOT( showEngineConfigDialog() ) );
 
   // set placement methods page based on geometry type

Modified: trunk/qgis/src/plugins/labeling/labelingguibase.ui
===================================================================
--- trunk/qgis/src/plugins/labeling/labelingguibase.ui	2010-05-29 22:10:55 UTC (rev 13598)
+++ trunk/qgis/src/plugins/labeling/labelingguibase.ui	2010-05-29 22:56:39 UTC (rev 13599)
@@ -206,7 +206,7 @@
           <item row="0" column="2">
            <widget class="QLabel" name="label_9">
             <property name="text">
-             <string>pixels</string>
+             <string>mm</string>
             </property>
            </widget>
           </item>
@@ -312,7 +312,7 @@
             <item>
              <widget class="QLabel" name="label_15">
               <property name="text">
-               <string>pixels</string>
+               <string>mm</string>
               </property>
              </widget>
             </item>
@@ -472,9 +472,9 @@
            </widget>
           </item>
           <item>
-           <widget class="QSpinBox" name="spinBufferSize">
-            <property name="minimum">
-             <number>1</number>
+           <widget class="QDoubleSpinBox" name="spinBufferSize">
+            <property name="suffix">
+             <string> mm</string>
             </property>
            </widget>
           </item>
@@ -771,7 +771,6 @@
   <tabstop>btnChangeFont</tabstop>
   <tabstop>btnTextColor</tabstop>
   <tabstop>chkBuffer</tabstop>
-  <tabstop>spinBufferSize</tabstop>
   <tabstop>btnBufferColor</tabstop>
   <tabstop>sliderPriority</tabstop>
   <tabstop>chkScaleBasedVisibility</tabstop>

Modified: trunk/qgis/src/plugins/labeling/labelpreview.cpp
===================================================================
--- trunk/qgis/src/plugins/labeling/labelpreview.cpp	2010-05-29 22:10:55 UTC (rev 13598)
+++ trunk/qgis/src/plugins/labeling/labelpreview.cpp	2010-05-29 22:56:39 UTC (rev 13599)
@@ -15,9 +15,9 @@
   update();
 }
 
-void LabelPreview::setBuffer( int size, QColor color )
+void LabelPreview::setBuffer( double size, QColor color )
 {
-  mBufferSize = size;
+  mBufferSize = size * 88 / 25.4; //assume standard dpi for preview
   mBufferColor = color;
   update();
 }

Modified: trunk/qgis/src/plugins/labeling/labelpreview.h
===================================================================
--- trunk/qgis/src/plugins/labeling/labelpreview.h	2010-05-29 22:10:55 UTC (rev 13598)
+++ trunk/qgis/src/plugins/labeling/labelpreview.h	2010-05-29 22:56:39 UTC (rev 13599)
@@ -10,7 +10,7 @@
 
     void setTextColor( QColor color );
 
-    void setBuffer( int size, QColor color );
+    void setBuffer( double size, QColor color );
 
     void paintEvent( QPaintEvent* e );
 

Modified: trunk/qgis/src/plugins/labeling/pallabeling.cpp
===================================================================
--- trunk/qgis/src/plugins/labeling/pallabeling.cpp	2010-05-29 22:10:55 UTC (rev 13598)
+++ trunk/qgis/src/plugins/labeling/pallabeling.cpp	2010-05-29 22:56:39 UTC (rev 13599)
@@ -60,19 +60,19 @@
     const char* strId() { return mStrId.data(); }
     QString text() { return mText; }
 
-    pal::LabelInfo* info( QFontMetrics* fm, const QgsMapToPixel* xform )
+    pal::LabelInfo* info( QFontMetrics* fm, const QgsMapToPixel* xform, double fontScale )
     {
       if ( mInfo ) return mInfo;
 
       // create label info!
       QgsPoint ptZero = xform->toMapCoordinates( 0, 0 );
-      QgsPoint ptSize = xform->toMapCoordinates( 0, -fm->height() );
+      QgsPoint ptSize = xform->toMapCoordinates( 0, ( int )( -fm->height() / fontScale ) );
 
       mInfo = new pal::LabelInfo( mText.count(), ptSize.y() - ptZero.y() );
       for ( int i = 0; i < mText.count(); i++ )
       {
         mInfo->char_info[i].chr = mText[i].unicode();
-        ptSize = xform->toMapCoordinates( fm->width( mText[i] ), 0 );
+        ptSize = xform->toMapCoordinates(( int )( fm->width( mText[i] ) / fontScale ) , 0 );
         mInfo->char_info[i].width = ptSize.x() - ptZero.x();
       }
       return mInfo;
@@ -106,6 +106,8 @@
   labelPerPart = false;
   mergeLines = false;
   minFeatureSize = 0.0;
+  vectorScaleFactor = 1.0;
+  rasterCompressFactor = 1.0;
 }
 
 LayerSettings::LayerSettings( const LayerSettings& s )
@@ -127,6 +129,8 @@
   labelPerPart = s.labelPerPart;
   mergeLines = s.mergeLines;
   minFeatureSize = s.minFeatureSize;
+  vectorScaleFactor = s.vectorScaleFactor;
+  rasterCompressFactor = s.rasterCompressFactor;
 
   fontMetrics = NULL;
   ct = NULL;
@@ -251,11 +255,12 @@
 
 void LayerSettings::calculateLabelSize( QString text, double& labelX, double& labelY )
 {
-  //QFontMetrics fontMetrics(textFont);
-  QRect labelRect = /*QRect(0,0,20,20);*/ fontMetrics->boundingRect( text );
+  QRectF labelRect = fontMetrics->boundingRect( text );
+  double w = labelRect.width() / rasterCompressFactor;
+  double h = labelRect.height() / rasterCompressFactor;
 
-  // 2px border...
-  QgsPoint ptSize = xform->toMapCoordinates( labelRect.width() + 2, labelRect.height() + 2 );
+  QgsPoint ptSize = xform->toMapCoordinates( w, h );
+
   labelX = fabs( ptSize.x() - ptZero.x() );
   labelY = fabs( ptSize.y() - ptZero.y() );
 }
@@ -295,11 +300,11 @@
 
   // TODO: only for placement which needs character info
   pal::Feature* feat = palLayer->getFeature( lbl->strId() );
-  feat->setLabelInfo( lbl->info( fontMetrics, xform ) );
+  feat->setLabelInfo( lbl->info( fontMetrics, xform, rasterCompressFactor ) );
 
   // TODO: allow layer-wide feature dist in PAL...?
   if ( dist != 0 )
-    feat->setDistLabel( fabs( ptOne.x() - ptZero.x() )* dist );
+    feat->setDistLabel( fabs( ptOne.x() - ptZero.x() )* dist * vectorScaleFactor );
 }
 
 
@@ -402,14 +407,19 @@
   l->setMergeConnectedLines( lyr.mergeLines );
 
   // set font size from points to output size
-  double size = 0.3527 * lyr.textFont.pointSizeF() * ctx.scaleFactor(); //* ctx.rasterScaleFactor();
-  lyr.textFont.setPixelSize(( int )size );
+  double size = 0.3527 * lyr.textFont.pointSizeF() * ctx.scaleFactor();
+  // request larger font and then scale down painter (to avoid Qt font scale bug)
+  lyr.textFont.setPixelSize(( int )( size*ctx.rasterScaleFactor() + 0.5 ) );
 
+  //raster and vector scale factors
+  lyr.vectorScaleFactor = ctx.scaleFactor();
+  lyr.rasterCompressFactor = ctx.rasterScaleFactor();
+
   // save the pal layer to our layer context (with some additional info)
   lyr.palLayer = l;
   lyr.fieldIndex = fldIndex;
   lyr.fontMetrics = new QFontMetrics( lyr.textFont );
-  lyr.fontBaseline = lyr.fontMetrics->boundingRect( "X" ).bottom(); // dummy text to find out how many pixels of the text are below the baseline
+
   lyr.xform = mMapRenderer->coordinateTransform();
   if ( mMapRenderer->hasCrsTransformEnabled() )
     lyr.ct = new QgsCoordinateTransform( layer->srs(), mMapRenderer->destinationSrs() );
@@ -623,16 +633,20 @@
 
   //QgsDebugMsg( "drawLabel " + QString::number( drawBuffer ) + " " + txt );
 
-  // shift by one as we have 2px border
   painter->save();
   painter->translate( QPointF( outPt.x(), outPt.y() ) );
   painter->rotate( -label->getAlpha() * 180 / M_PI );
-  painter->translate( QPointF( 1, -1 - lyr.fontBaseline ) );
 
+  // 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 )
   {
     // we're drawing buffer
-    drawLabelBuffer( painter, txt, lyr.textFont, lyr.bufferSize, lyr.bufferColor );
+    drawLabelBuffer( painter, txt, lyr.textFont, lyr.bufferSize * lyr.vectorScaleFactor * lyr.rasterCompressFactor , lyr.bufferColor );
   }
   else
   {
@@ -654,7 +668,7 @@
 }
 
 
-void PalLabeling::drawLabelBuffer( QPainter* p, QString text, const QFont& font, int size, QColor color )
+void PalLabeling::drawLabelBuffer( QPainter* p, QString text, const QFont& font, double size, QColor color )
 {
   /*
   p->setFont( font );
@@ -666,7 +680,9 @@
 
   QPainterPath path;
   path.addText( 0, 0, font, text );
-  p->setPen( QPen( color, size ) );
+  QPen pen( color );
+  pen.setWidthF( size );
+  p->setPen( pen );
   p->setBrush( color );
   p->drawPath( path );
 }

Modified: trunk/qgis/src/plugins/labeling/pallabeling.h
===================================================================
--- trunk/qgis/src/plugins/labeling/pallabeling.h	2010-05-29 22:10:55 UTC (rev 13598)
+++ trunk/qgis/src/plugins/labeling/pallabeling.h	2010-05-29 22:56:39 UTC (rev 13599)
@@ -60,9 +60,11 @@
     bool enabled;
     int priority; // 0 = low, 10 = high
     bool obstacle; // whether it's an obstacle
-    double dist; // distance from the feature (in pixels)
+    double dist; // distance from the feature (in mm)
+    double vectorScaleFactor; //scale factor painter units->pixels
+    double rasterCompressFactor; //pixel resolution scale factor
     int scaleMin, scaleMax; // disabled if both are zero
-    int bufferSize;
+    double bufferSize; //buffer size (in mm)
     QColor bufferColor;
     bool labelPerPart; // whether to label every feature's part or only the biggest one
     bool mergeLines;
@@ -81,7 +83,6 @@
     pal::Layer* palLayer;
     int fieldIndex;
     QFontMetrics* fontMetrics;
-    int fontBaseline;
     const QgsMapToPixel* xform;
     const QgsCoordinateTransform* ct;
     QgsPoint ptZero, ptOne;
@@ -145,7 +146,7 @@
 
     void drawLabelCandidateRect( pal::LabelPosition* lp, QPainter* painter, const QgsMapToPixel* xform );
     void drawLabel( pal::LabelPosition* label, QPainter* painter, const QgsMapToPixel* xform, bool drawBuffer = false );
-    static void drawLabelBuffer( QPainter* p, QString text, const QFont& font, int size, QColor color );
+    static void drawLabelBuffer( QPainter* p, QString text, const QFont& font, double size, QColor color );
 
   protected:
 



More information about the QGIS-commit mailing list