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

svn_qgis at osgeo.org svn_qgis at osgeo.org
Mon Jul 14 16:21:58 EDT 2008


Author: jef
Date: 2008-07-14 16:21:57 -0400 (Mon, 14 Jul 2008)
New Revision: 8775

Modified:
   trunk/qgis/python/core/qgslabel.sip
   trunk/qgis/python/core/qgslabelattributes.sip
   trunk/qgis/src/app/qgslabeldialog.cpp
   trunk/qgis/src/core/qgslabel.cpp
   trunk/qgis/src/core/qgslabel.h
   trunk/qgis/src/core/qgslabelattributes.cpp
   trunk/qgis/src/core/qgslabelattributes.h
   trunk/qgis/src/ui/qgslabeldialogbase.ui
Log:
add support for multiline labels (applies #1138)

Modified: trunk/qgis/python/core/qgslabel.sip
===================================================================
--- trunk/qgis/python/core/qgslabel.sip	2008-07-14 20:19:52 UTC (rev 8774)
+++ trunk/qgis/python/core/qgslabel.sip	2008-07-14 20:21:57 UTC (rev 8775)
@@ -30,13 +30,14 @@
   YOffset,
   Angle,
   Alignment,
-        BufferEnabled,
+  BufferEnabled,
   BufferSize,
   BufferColor,
   BufferBrush,
   BorderWidth,
   BorderColor,
   BorderStyle,
+  MultilineEnabled,
   LabelFieldCount
 };
 
@@ -46,7 +47,7 @@
     void renderLabel ( QPainter* painter, QgsRect& viewExtent, 
                        QgsCoordinateTransform* coordTransform,
                        QgsMapToPixel *transform,
-           QgsFeature &feature, bool selected, QgsLabelAttributes *classAttributes=0, double sizeScale = 1);
+                       QgsFeature &feature, bool selected, QgsLabelAttributes *classAttributes=0, double sizeScale = 1);
 
     /** Reads the renderer configuration from an XML file
      @param rnode the DOM node to read 

Modified: trunk/qgis/python/core/qgslabelattributes.sip
===================================================================
--- trunk/qgis/python/core/qgslabelattributes.sip	2008-07-14 20:19:52 UTC (rev 8774)
+++ trunk/qgis/python/core/qgslabelattributes.sip	2008-07-14 20:21:57 UTC (rev 8775)
@@ -101,6 +101,8 @@
     void  setBorderStyle ( Qt::PenStyle style );
     bool  borderStyleIsSet ( ) const;
     Qt::PenStyle   borderStyle ( ) const;
-    
+
+    bool  multilineEnabled() const;
+    void  setMultilineEnabled( bool useMultiline );
 };
 

Modified: trunk/qgis/src/app/qgslabeldialog.cpp
===================================================================
--- trunk/qgis/src/app/qgslabeldialog.cpp	2008-07-14 20:19:52 UTC (rev 8774)
+++ trunk/qgis/src/app/qgslabeldialog.cpp	2008-07-14 20:21:57 UTC (rev 8775)
@@ -239,6 +239,8 @@
   {
     spinBufferSize->setValue(1);
   }
+  //set the state of the multiline enabled checkbox
+  chkUseMultiline->setChecked(myLabelAttributes->multilineEnabled());   
   //set the state of the buffer enabled checkbox
   chkUseBuffer->setChecked(myLabelAttributes->bufferEnabled());   
   
@@ -360,6 +362,7 @@
     if (radioBelow->isChecked())       myLabelAttributes->setAlignment(Qt::AlignTop   | Qt::AlignHCenter); 
     if (radioOver->isChecked())        myLabelAttributes->setAlignment(Qt::AlignCenter);
       
+    myLabelAttributes->setMultilineEnabled(chkUseMultiline->isChecked());
     myLabelAttributes->setBufferEnabled(chkUseBuffer->isChecked());
     myLabelAttributes->setBufferColor(mBufferColor); 
     myTypeInt = 0;

Modified: trunk/qgis/src/core/qgslabel.cpp
===================================================================
--- trunk/qgis/src/core/qgslabel.cpp	2008-07-14 20:19:52 UTC (rev 8774)
+++ trunk/qgis/src/core/qgslabel.cpp	2008-07-14 20:21:57 UTC (rev 8775)
@@ -71,7 +71,7 @@
 
     const QgsAttributeMap& attrs = feature.attributeMap();
     QgsAttributeMap::const_iterator it = attrs.find(mLabelFieldIdx[attr]);
-    
+
     if (it != attrs.end())
     {
       return it->toString();
@@ -86,7 +86,7 @@
                             const QgsCoordinateTransform* coordTransform,
                             const QgsMapToPixel *transform,
                             QgsFeature &feature, bool selected, QgsLabelAttributes *classAttributes,
-       			    double sizeScale )
+                            double sizeScale )
 {
 
     QPen pen;
@@ -137,20 +137,20 @@
     int sizeType;
     value = fieldValue ( SizeType, feature );
     if( value.isEmpty() )
-    	sizeType = mLabelAttributes->sizeType();
+      sizeType = mLabelAttributes->sizeType();
     else
     {
-    	value = value.toLower();
-	if( value.compare("mapunits") == 0 )
-		sizeType = QgsLabelAttributes::MapUnits;
-	else
-		sizeType = QgsLabelAttributes::PointUnits;
+      value = value.toLower();
+      if( value.compare("mapunits") == 0 )
+        sizeType = QgsLabelAttributes::MapUnits;
+      else
+        sizeType = QgsLabelAttributes::PointUnits;
     }
     if ( sizeType == QgsLabelAttributes::MapUnits )
     {
         size *= scale;
     } else {
-	size *= sizeScale;
+        size *= sizeScale;
     }
     if(size>0.0)
     	font.setPointSizeF ( size );
@@ -214,8 +214,26 @@
     /* Alignment */
     int alignment;
     QFontMetrics fm ( font );
-    int width = fm.width ( text );
-    int height = fm.height();
+    int width, height;
+
+    if( mLabelAttributes->multilineEnabled() )
+    {
+      QStringList texts = text.split("\n");
+      
+      width=0;
+      for(int i=0; i<texts.size(); i++) {
+        int w = fm.width(texts[i]);
+        if(w>width)
+          width=w;
+      }
+
+      height = fm.height()*texts.size();
+    }
+    else
+    { 
+      width = fm.width ( text );
+      height = fm.height();
+    }
     int dx = 0;
     int dy = 0;
 
@@ -226,23 +244,23 @@
     }
     else
     {
-        value = value.toLower();
+      value = value.toLower();
 
-	alignment=0;
+      alignment=0;
 
-        if ( value.contains("left") )
-            alignment |= Qt::AlignLeft;
-        else if( value.contains("right") )
-	    alignment |= Qt::AlignRight;
-	else
-	    alignment |= Qt::AlignHCenter;
+      if ( value.contains("left") )
+        alignment |= Qt::AlignLeft;
+      else if( value.contains("right") )
+        alignment |= Qt::AlignRight;
+      else
+        alignment |= Qt::AlignHCenter;
 
-	if( value.contains("bottom") )
-	    alignment |= Qt::AlignBottom;
-	else if( value.contains("top") )
-	    alignment |= Qt::AlignTop;
-	else
-	    alignment |= Qt::AlignVCenter;
+      if( value.contains("bottom") )
+        alignment |= Qt::AlignBottom;
+      else if( value.contains("top") )
+        alignment |= Qt::AlignTop;
+      else
+        alignment |= Qt::AlignVCenter;
     }
 
     if ( alignment & Qt::AlignLeft )
@@ -319,7 +337,7 @@
     {
       renderLabel(painter, overridePoint, coordTransform, 
                   transform, text, font, pen, dx, dy,
-                  xoffset, yoffset, ang);
+                  xoffset, yoffset, ang, width, height, alignment);
     }
     else
     {
@@ -329,7 +347,7 @@
       {
         renderLabel(painter, points[i], coordTransform, 
                     transform, text, font, pen, dx, dy,
-                    xoffset, yoffset, ang);
+                    xoffset, yoffset, ang, width, height, alignment);
       }
     }
 }
@@ -340,7 +358,8 @@
                            QString text, QFont font, QPen pen,
                            int dx, int dy, 
                            double xoffset, double yoffset, 
-                           double ang)
+                           double ang,
+                           int width, int height, int alignment)
 {
     // Convert point to projected units
     if (coordTransform)
@@ -372,31 +391,38 @@
     painter->setFont ( font );
     painter->translate ( x, y );
     painter->rotate ( -ang );
+
     //
     // Draw a buffer behind the text if one is desired
     //
     if (mLabelAttributes->bufferSizeIsSet() && mLabelAttributes->bufferEnabled())
     {
-        int myBufferSize = static_cast<int>(mLabelAttributes->bufferSize());
-        if (mLabelAttributes->bufferColorIsSet())
+      int myBufferSize = static_cast<int>(mLabelAttributes->bufferSize());
+      if (mLabelAttributes->bufferColorIsSet())
+      {
+          painter->setPen( mLabelAttributes->bufferColor());
+      }
+      else //default to a white buffer
+      {
+          painter->setPen( Qt::white);
+      }
+      for (int i = dx-myBufferSize; i <= dx+myBufferSize; i++)
+      {
+        for (int j = dy-myBufferSize; j <= dy+myBufferSize; j++)
         {
-            painter->setPen( mLabelAttributes->bufferColor());
+          if( mLabelAttributes->multilineEnabled() )
+            painter->drawText( i , j-height, width, height, alignment, text);
+          else
+            painter->drawText( i , j, text);
         }
-        else //default to a white buffer
-        {
-            painter->setPen( Qt::white);
-        }
-        for (int i = dx-myBufferSize; i <= dx+myBufferSize; i++)
-        {
-            for (int j = dy-myBufferSize; j <= dy+myBufferSize; j++)
-            {
-                painter->drawText( i ,j, text);
-            }
-        }
+      }
     }
 
     painter->setPen ( pen );
-    painter->drawText ( dx, dy, text );
+    if( mLabelAttributes->multilineEnabled() )
+      painter->drawText ( dx, dy-height, width, height, alignment, text );
+    else
+      painter->drawText ( dx, dy, text );
     painter->restore();
 }
 
@@ -882,6 +908,20 @@
         setLabelField ( BufferEnabled, _elementFieldIndex(el) );
     }
 
+    scratchNode = node.namedItem("multilineenabled");
+
+    if ( scratchNode.isNull() )
+    {
+        QgsDebugMsg("couldn't find QgsLabel ``multilineenabled'' attribute");
+    }
+    else
+    {
+        el = scratchNode.toElement();
+
+        mLabelAttributes->setMultilineEnabled ( (bool)el.attribute("on","0").toInt() );
+        setLabelField ( MultilineEnabled, _elementFieldIndex(el) );
+    }
+
 } // QgsLabel::readXML()
 
 
@@ -1158,6 +1198,26 @@
     {
         xml << "\t\t\t<bufferenabled on=\"" << "\" field=\"" << "\" />\n";
     }
+
+    // multiline enabled
+    if (mLabelAttributes->multilineEnabled())
+    {
+      if (mLabelFieldIdx[MultilineEnabled] != -1)
+      {
+          xml << "\t\t\t<multilineenabled on=\"" << mLabelAttributes->multilineEnabled()
+              << "\" field=\"" << mLabelFieldIdx[MultilineEnabled] << "\" />\n";
+      }
+      else
+      {
+          xml << "\t\t\t<multilineenabled on=\"" << mLabelAttributes->multilineEnabled()
+              << "\" field=\"\" />\n";
+      }
+    }
+    else
+    {
+        xml << "\t\t\t<multilineenabled on=\"" << "\" field=\"" << "\" />\n";
+    }
+
     xml << "\t\t</labelattributes>\n";
 }
 

Modified: trunk/qgis/src/core/qgslabel.h
===================================================================
--- trunk/qgis/src/core/qgslabel.h	2008-07-14 20:19:52 UTC (rev 8774)
+++ trunk/qgis/src/core/qgslabel.h	2008-07-14 20:21:57 UTC (rev 8775)
@@ -73,6 +73,7 @@
 	BorderWidth,
 	BorderColor,
 	BorderStyle,
+	MultilineEnabled,
 	LabelFieldCount
     };
 
@@ -127,7 +128,8 @@
                      QString text, QFont font, QPen pen,
                      int dx, int dy,
                      double xoffset, double yoffset,
-                     double ang);
+                     double ang,
+                     int width, int height, int alignment);
 
     /** Get label point for simple feature in map units */
     void labelPoint ( std::vector<QgsPoint>&, QgsFeature & feature );

Modified: trunk/qgis/src/core/qgslabelattributes.cpp
===================================================================
--- trunk/qgis/src/core/qgslabelattributes.cpp	2008-07-14 20:19:52 UTC (rev 8774)
+++ trunk/qgis/src/core/qgslabelattributes.cpp	2008-07-14 20:21:57 UTC (rev 8775)
@@ -51,7 +51,8 @@
       mBufferStyleIsSet( false ),
       mBorderColorIsSet( false ),
       mBorderWidthIsSet( false ),
-      mBorderStyleIsSet( false )
+      mBorderStyleIsSet( false ),
+      mMultilineEnabledFlag( false )
 {
 
     if ( def ) { // set defaults
@@ -388,13 +389,25 @@
     return mBorderPen.style();
 }
 
+  /* Multiline */
+bool QgsLabelAttributes::multilineEnabled() const
+{
+  return mMultilineEnabledFlag;
+}
+void QgsLabelAttributes::setMultilineEnabled(bool useMultilineFlag)
+{
+  mMultilineEnabledFlag=useMultilineFlag;
+}
+
+  /* units */
 QString QgsLabelAttributes::unitsName ( int units ) 
 {
-    if ( units == MapUnits ){ 
-	return QString("mu");
-    }
-	
-    return QString("pt");
+  if ( units == MapUnits )
+  {
+    return QString("mu");
+  }
+
+  return QString("pt");
 }
 
 int QgsLabelAttributes::unitsCode ( const QString &name )
@@ -406,6 +419,7 @@
     return PointUnits;
 }
 
+ /* alignment */
 QString QgsLabelAttributes::alignmentName ( int alignment ) 
 {
   std::cout << "QString QgsLabelAttributes::alignmentName (" << alignment << ")" << std::endl;

Modified: trunk/qgis/src/core/qgslabelattributes.h
===================================================================
--- trunk/qgis/src/core/qgslabelattributes.h	2008-07-14 20:19:52 UTC (rev 8774)
+++ trunk/qgis/src/core/qgslabelattributes.h	2008-07-14 20:21:57 UTC (rev 8775)
@@ -171,6 +171,9 @@
     void  setBorderStyle ( Qt::PenStyle style );
     bool  borderStyleIsSet ( void ) const;
     Qt::PenStyle   borderStyle ( void ) const;
+
+    bool  multilineEnabled() const;
+    void  setMultilineEnabled( bool useMultiline );
     
  protected:
     /* Text */
@@ -224,6 +227,9 @@
     bool mBorderColorIsSet;
     bool mBorderWidthIsSet;
     bool mBorderStyleIsSet;
+
+    /** Multiline enablement */
+    bool mMultilineEnabledFlag;
 };
 
 #endif

Modified: trunk/qgis/src/ui/qgslabeldialogbase.ui
===================================================================
--- trunk/qgis/src/ui/qgslabeldialogbase.ui	2008-07-14 20:19:52 UTC (rev 8774)
+++ trunk/qgis/src/ui/qgslabeldialogbase.ui	2008-07-14 20:21:57 UTC (rev 8775)
@@ -449,6 +449,16 @@
         </widget>
        </item>
        <item row="5" column="0" >
+        <widget class="QCheckBox" name="chkUseMultiline" >
+         <property name="text" >
+          <string>Multiline labels?</string>
+         </property>
+         <property name="checked" >
+          <bool>true</bool>
+         </property>
+        </widget>
+       </item>
+       <item row="6" column="0" >
         <spacer>
          <property name="orientation" >
           <enum>Qt::Vertical</enum>



More information about the QGIS-commit mailing list