[QGIS Commit] r9261 - trunk/qgis/src/core/composer

svn_qgis at osgeo.org svn_qgis at osgeo.org
Fri Sep 5 07:31:25 EDT 2008


Author: mhugent
Date: 2008-09-05 07:31:25 -0400 (Fri, 05 Sep 2008)
New Revision: 9261

Modified:
   trunk/qgis/src/core/composer/qgscomposeritem.cpp
   trunk/qgis/src/core/composer/qgscomposeritem.h
   trunk/qgis/src/core/composer/qgscomposerscalebar.cpp
   trunk/qgis/src/core/composer/qgscomposerscalebar.h
   trunk/qgis/src/core/composer/qgsdoubleboxscalebarstyle.cpp
   trunk/qgis/src/core/composer/qgsnumericscalebarstyle.cpp
   trunk/qgis/src/core/composer/qgsscalebarstyle.cpp
   trunk/qgis/src/core/composer/qgssingleboxscalebarstyle.cpp
   trunk/qgis/src/core/composer/qgsticksscalebarstyle.cpp
Log:
Font workaround enabled for composer scale bar

Modified: trunk/qgis/src/core/composer/qgscomposeritem.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgscomposeritem.cpp	2008-09-05 09:18:42 UTC (rev 9260)
+++ trunk/qgis/src/core/composer/qgscomposeritem.cpp	2008-09-05 11:31:25 UTC (rev 9261)
@@ -523,7 +523,7 @@
   }
 }
 
-void QgsComposerItem::drawText(QPainter* p, int x, int y, const QString& text, const QFont& font)
+void QgsComposerItem::drawText(QPainter* p, int x, int y, const QString& text, const QFont& font) const
 {
   QFont textFont = scaledFontPixelSize(font);
   
@@ -535,7 +535,7 @@
   p->restore();
 }
 
-void QgsComposerItem::drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font)
+void QgsComposerItem::drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font) const
 {
   QFont textFont = scaledFontPixelSize(font);
 

Modified: trunk/qgis/src/core/composer/qgscomposeritem.h
===================================================================
--- trunk/qgis/src/core/composer/qgscomposeritem.h	2008-09-05 09:18:42 UTC (rev 9260)
+++ trunk/qgis/src/core/composer/qgscomposeritem.h	2008-09-05 11:31:25 UTC (rev 9261)
@@ -105,6 +105,28 @@
 
     const QgsComposition* composition() const {return mComposition;}
 
+    //functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the 
+    //painter down by the same factor for drawing
+
+    /**Draws Text. Takes care about all the composer specific issues (calculation to pixel, scaling of font and painter
+     to work arount the Qt font bug)*/
+    void drawText(QPainter* p, int x, int y, const QString& text, const QFont& font) const;
+
+    /**Like the above, but with a rectangle for multiline text*/
+    void drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font) const;
+
+    /**Returns the font width in MM (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
+    double textWidthMM(const QFont& font, const QString& text) const;
+
+    /**Returns the font ascent in MM (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
+    double fontAscentMM(const QFont& font) const;
+
+    /**Calculates font to from point size to pixel size*/
+    double pixelFontSize(double pointSize) const;
+
+    /**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE*/
+    QFont scaledFontPixelSize(const QFont& font) const;
+
   protected:
 
     QgsComposition* mComposition;
@@ -151,25 +173,6 @@
 
     /**Draw background*/
     virtual void drawBackground( QPainter* p );
-
-    /**Draws Text. Takes care about all the composer specific issues (calculation to pixel, scaling of font and painter
-     to work arount the Qt font bug)*/
-    void drawText(QPainter* p, int x, int y, const QString& text, const QFont& font);
-
-    /**Like the above, but with a rectangle for multiline text*/
-    void drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font);
-
-    /**Returns the font width in MM (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
-    double textWidthMM(const QFont& font, const QString& text) const;
-
-    /**Returns the font ascent in MM (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
-    double fontAscentMM(const QFont& font) const;
-
-    /**Calculates font to from point size to pixel size*/
-    double pixelFontSize(double pointSize) const;
-
-    /**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE*/
-    QFont scaledFontPixelSize(const QFont& font) const;
 };
 
 #endif

Modified: trunk/qgis/src/core/composer/qgscomposerscalebar.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgscomposerscalebar.cpp	2008-09-05 09:18:42 UTC (rev 9260)
+++ trunk/qgis/src/core/composer/qgscomposerscalebar.cpp	2008-09-05 11:31:25 UTC (rev 9261)
@@ -48,11 +48,11 @@
   drawBackground( painter );
   painter->setPen( QPen( QColor( 0, 0, 0 ) ) ); //draw all text black
 
-  //calculate half of first label width as labels are drawn centered
-  QFontMetricsF fontMetrics( mFont );
+  //x-offset is half of first label width because labels are drawn centered
   QString firstLabel = firstLabelString();
+  double firstLabelWidth = textWidthMM(mFont, firstLabel);
 
-  mStyle->draw( painter, fontMetrics.width( firstLabel ) / 2 );
+  mStyle->draw( painter, firstLabelWidth / 2 );
 
   //draw frame and selection boxes if necessary
   drawFrame( painter );
@@ -126,15 +126,7 @@
   mBrush.setColor( QColor( 0, 0, 0 ) );
   mBrush.setStyle( Qt::SolidPattern );
 
-  //default size 12 point
-  if ( mComposition )
-  {
-    mFont.setPixelSize( mComposition->pixelFontSize( 12 ) );
-  }
-  else
-  {
-    mFont.setPixelSize( 5 );
-  }
+  mFont.setPointSizeF(12.0);
 
   mLabelBarSpace = 3.0;
   mBoxContentSpace = 1.0;
@@ -173,12 +165,6 @@
   QgsComposerItem::update();
 }
 
-double QgsComposerScaleBar::fontHeight() const
-{
-  QFontMetricsF labelFontMetrics( mFont );
-  return labelFontMetrics.ascent();
-}
-
 void QgsComposerScaleBar::updateSegmentSize()
 {
   refreshSegmentMM();
@@ -268,33 +254,12 @@
 
 QFont QgsComposerScaleBar::font() const
 {
-  if ( mComposition ) //make pixel to point conversion to show correct point value in dialogs
-  {
-    double pointSize = mComposition->pointFontSize( mFont.pixelSize() );
-    QFont returnFont = mFont;
-    returnFont.setPointSize( pointSize );
-    return returnFont;
-  }
-}
-
-QFont QgsComposerScaleBar::fontPixelSize() const
-{
   return mFont;
 }
 
 void QgsComposerScaleBar::setFont( const QFont& font )
 {
-  //set font size in pixels for proper preview and printout
-  if ( mComposition )
-  {
-    int pixelSize = mComposition->pixelFontSize( font.pointSizeF() );
-    mFont = font;
-    mFont.setPixelSize( pixelSize );
-  }
-  else
-  {
-    mFont = font;
-  }
+  mFont = font;
   adjustBoxSize();
   update();
 }

Modified: trunk/qgis/src/core/composer/qgscomposerscalebar.h
===================================================================
--- trunk/qgis/src/core/composer/qgscomposerscalebar.h	2008-09-05 09:18:42 UTC (rev 9260)
+++ trunk/qgis/src/core/composer/qgscomposerscalebar.h	2008-09-05 11:31:25 UTC (rev 9261)
@@ -56,9 +56,6 @@
     void setUnitLabeling( const QString& label ) {mUnitLabeling = label;}
 
     QFont font() const;
-
-    /**Returns font that has size set in pixels. Used from QgsComposerScaleBarStyle*/
-    QFont fontPixelSize() const;
     
     void setFont( const QFont& font );
 
@@ -96,9 +93,6 @@
      of the segment*/
     void segmentPositions( QList<QPair<double, double> >& posWidthList ) const;
 
-    /**Returns height of mFont in points*/
-    double fontHeight() const;
-
     /**Sets box size suitable to content*/
     void adjustBoxSize();
 

Modified: trunk/qgis/src/core/composer/qgsdoubleboxscalebarstyle.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgsdoubleboxscalebarstyle.cpp	2008-09-05 09:18:42 UTC (rev 9260)
+++ trunk/qgis/src/core/composer/qgsdoubleboxscalebarstyle.cpp	2008-09-05 11:31:25 UTC (rev 9261)
@@ -45,7 +45,7 @@
   {
     return;
   }
-  double barTopPosition = mScaleBar->fontHeight() + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
+  double barTopPosition = mScaleBar->fontAscentMM(mScaleBar->font()) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
   double segmentHeight = mScaleBar->height() / 2;
 
   p->save();

Modified: trunk/qgis/src/core/composer/qgsnumericscalebarstyle.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgsnumericscalebarstyle.cpp	2008-09-05 09:18:42 UTC (rev 9260)
+++ trunk/qgis/src/core/composer/qgsnumericscalebarstyle.cpp	2008-09-05 11:31:25 UTC (rev 9261)
@@ -48,11 +48,10 @@
   }
 
   p->save();
-
   p->setFont( mScaleBar->font() );
-  p->drawText( QPointF( mScaleBar->pen().widthF() + mScaleBar->boxContentSpace(), mScaleBar->boxContentSpace()
-                        + mScaleBar->fontHeight() ), scaleText() );
 
+  mScaleBar->drawText(p, mScaleBar->pen().widthF() + mScaleBar->boxContentSpace(), mScaleBar->boxContentSpace() + mScaleBar->fontAscentMM(mScaleBar->font()), scaleText(), mScaleBar->font());
+
   p->restore();
 }
 
@@ -64,10 +63,12 @@
     return rect;
   }
 
-  QFontMetricsF fontMetrics( mScaleBar->font() );
+  double textWidth = mScaleBar->textWidthMM(mScaleBar->font(), scaleText());
+  double textHeight = mScaleBar->fontAscentMM(mScaleBar->font());
+
   return QRectF( mScaleBar->transform().dx(), mScaleBar->transform().dy(), 2 * mScaleBar->boxContentSpace()
-                 + 2 * mScaleBar->pen().width() + fontMetrics.width( scaleText() ),
-                 mScaleBar->fontHeight() + 2 * mScaleBar->boxContentSpace() );
+                 + 2 * mScaleBar->pen().width() + textWidth,
+                 textHeight + 2 * mScaleBar->boxContentSpace() );
 }
 
 QString QgsNumericScaleBarStyle::scaleText() const

Modified: trunk/qgis/src/core/composer/qgsscalebarstyle.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgsscalebarstyle.cpp	2008-09-05 09:18:42 UTC (rev 9260)
+++ trunk/qgis/src/core/composer/qgsscalebarstyle.cpp	2008-09-05 11:31:25 UTC (rev 9261)
@@ -43,11 +43,10 @@
 
   p->save();
 
-  p->setFont( mScaleBar->fontPixelSize() );
+  p->setFont(mScaleBar->font());
 
-  QFontMetricsF fontMetrics( mScaleBar->fontPixelSize() );
   QString firstLabel = mScaleBar->firstLabelString();
-  double xOffset = fontMetrics.width( firstLabel ) / 2;
+  double xOffset =  mScaleBar->textWidthMM(mScaleBar->font(), firstLabel) / 2;
 
   //double mCurrentXCoord = mScaleBar->pen().widthF() + mScaleBar->boxContentSpace();
   QList<QPair<double, double> > segmentInfo;
@@ -79,7 +78,7 @@
 
     if ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) //don't draw label for intermediate left segments
     {
-      p->drawText( QPointF( segmentIt->first - fontMetrics.width( currentNumericLabel ) / 2 + xOffset, mScaleBar->fontHeight() + mScaleBar->boxContentSpace() ), currentNumericLabel );
+      mScaleBar->drawText(p, segmentIt->first - mScaleBar->textWidthMM(mScaleBar->font(), currentNumericLabel) / 2 + xOffset, mScaleBar->fontAscentMM(mScaleBar->font()) + mScaleBar->boxContentSpace(), currentNumericLabel, mScaleBar->font());
     }
 
     if ( segmentCounter >= nSegmentsLeft )
@@ -93,7 +92,7 @@
   if ( !segmentInfo.isEmpty() )
   {
     currentNumericLabel = QString::number( currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit() );
-    p->drawText( QPointF( segmentInfo.last().first + mScaleBar->segmentMM() - fontMetrics.width( currentNumericLabel ) / 2 + xOffset, mScaleBar->fontHeight() + mScaleBar->boxContentSpace() ), currentNumericLabel + " " + mScaleBar->unitLabeling() );
+    mScaleBar->drawText(p, segmentInfo.last().first + mScaleBar->segmentMM() - mScaleBar->textWidthMM(mScaleBar->font(), currentNumericLabel) / 2 + xOffset, mScaleBar->fontAscentMM(mScaleBar->font()) + mScaleBar->boxContentSpace(), currentNumericLabel + " " + mScaleBar->unitLabeling(), mScaleBar->font());
   }
 
   p->restore();
@@ -106,18 +105,15 @@
     return QRectF();
   }
 
-
-  QFontMetricsF fontMetrics( mScaleBar->fontPixelSize() );
-
   //consider centered first label
-  double firstLabelLeft = fontMetrics.width( mScaleBar->firstLabelString() ) / 2;
+  double firstLabelLeft = mScaleBar->textWidthMM(mScaleBar->font(), mScaleBar->firstLabelString()) / 2;
 
   //consider last number and label
 
   double largestLabelNumber = mScaleBar->numSegments() * mScaleBar->numUnitsPerSegment() / mScaleBar->numMapUnitsPerScaleBarUnit();
   QString largestNumberLabel = QString::number( largestLabelNumber );
   QString largestLabel = QString::number( largestLabelNumber ) + " " + mScaleBar->unitLabeling();
-  double largestLabelWidth = fontMetrics.width( largestLabel ) - fontMetrics.width( largestNumberLabel ) / 2;
+  double largestLabelWidth = mScaleBar->textWidthMM(mScaleBar->font(), largestLabel) - mScaleBar->textWidthMM(mScaleBar->font(), largestNumberLabel) / 2;
 
   double totalBarLength = 0.0;
 
@@ -131,7 +127,7 @@
   }
 
   double width =  firstLabelLeft + totalBarLength + 2 * mScaleBar->pen().widthF() + largestLabelWidth + 2 * mScaleBar->boxContentSpace();
-  double height = mScaleBar->height() + mScaleBar->labelBarSpace() + 2 * mScaleBar->boxContentSpace() + mScaleBar->fontHeight();
+  double height = mScaleBar->height() + mScaleBar->labelBarSpace() + 2 * mScaleBar->boxContentSpace() + mScaleBar->fontAscentMM(mScaleBar->font());
 
   return QRectF( mScaleBar->transform().dx(), mScaleBar->transform().dy(), width, height );
 }

Modified: trunk/qgis/src/core/composer/qgssingleboxscalebarstyle.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgssingleboxscalebarstyle.cpp	2008-09-05 09:18:42 UTC (rev 9260)
+++ trunk/qgis/src/core/composer/qgssingleboxscalebarstyle.cpp	2008-09-05 11:31:25 UTC (rev 9261)
@@ -40,7 +40,7 @@
   {
     return;
   }
-  double barTopPosition = mScaleBar->fontHeight() + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
+  double barTopPosition = mScaleBar->fontAscentMM(mScaleBar->font()) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
 
   p->save();
   p->setPen( p->pen() );

Modified: trunk/qgis/src/core/composer/qgsticksscalebarstyle.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgsticksscalebarstyle.cpp	2008-09-05 09:18:42 UTC (rev 9260)
+++ trunk/qgis/src/core/composer/qgsticksscalebarstyle.cpp	2008-09-05 11:31:25 UTC (rev 9261)
@@ -53,7 +53,7 @@
   {
     return;
   }
-  double barTopPosition = mScaleBar->fontHeight() + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
+  double barTopPosition = mScaleBar->fontAscentMM(mScaleBar->font()) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
   double middlePosition = barTopPosition + mScaleBar->height() / 2.0;
   double bottomPosition = barTopPosition + mScaleBar->height();
 



More information about the QGIS-commit mailing list