From 106f094a88be10be0cfc6eb8258517684ba50dfc Mon Sep 17 00:00:00 2001 From: mhugent Date: Fri, 5 Sep 2008 11:31:25 +0000 Subject: [PATCH] Font workaround enabled for composer scale bar git-svn-id: http://svn.osgeo.org/qgis/trunk@9261 c8812cc2-4d05-0410-92ff-de0c093fc19c --- src/core/composer/qgscomposeritem.cpp | 4 +- src/core/composer/qgscomposeritem.h | 41 +++++++++-------- src/core/composer/qgscomposerscalebar.cpp | 45 +++---------------- src/core/composer/qgscomposerscalebar.h | 6 --- .../composer/qgsdoubleboxscalebarstyle.cpp | 2 +- src/core/composer/qgsnumericscalebarstyle.cpp | 13 +++--- src/core/composer/qgsscalebarstyle.cpp | 18 +++----- .../composer/qgssingleboxscalebarstyle.cpp | 2 +- src/core/composer/qgsticksscalebarstyle.cpp | 2 +- 9 files changed, 46 insertions(+), 87 deletions(-) diff --git a/src/core/composer/qgscomposeritem.cpp b/src/core/composer/qgscomposeritem.cpp index cb86109244d..d32847a06b4 100644 --- a/src/core/composer/qgscomposeritem.cpp +++ b/src/core/composer/qgscomposeritem.cpp @@ -523,7 +523,7 @@ void QgsComposerItem::hoverMoveEvent( QGraphicsSceneHoverEvent * event ) } } -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 @@ void QgsComposerItem::drawText(QPainter* p, int x, int y, const QString& text, c 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); diff --git a/src/core/composer/qgscomposeritem.h b/src/core/composer/qgscomposeritem.h index fe093b88394..8fd216f8643 100644 --- a/src/core/composer/qgscomposeritem.h +++ b/src/core/composer/qgscomposeritem.h @@ -105,6 +105,28 @@ class CORE_EXPORT QgsComposerItem: public QGraphicsRectItem 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 @@ class CORE_EXPORT QgsComposerItem: public QGraphicsRectItem /**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 diff --git a/src/core/composer/qgscomposerscalebar.cpp b/src/core/composer/qgscomposerscalebar.cpp index 2842a337f7d..36356045221 100644 --- a/src/core/composer/qgscomposerscalebar.cpp +++ b/src/core/composer/qgscomposerscalebar.cpp @@ -48,11 +48,11 @@ void QgsComposerScaleBar::paint( QPainter* painter, const QStyleOptionGraphicsIt 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 @@ void QgsComposerScaleBar::applyDefaultSettings() 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 @@ void QgsComposerScaleBar::update() QgsComposerItem::update(); } -double QgsComposerScaleBar::fontHeight() const -{ - QFontMetricsF labelFontMetrics( mFont ); - return labelFontMetrics.ascent(); -} - void QgsComposerScaleBar::updateSegmentSize() { refreshSegmentMM(); @@ -267,34 +253,13 @@ QString QgsComposerScaleBar::firstLabelString() const } 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(); } diff --git a/src/core/composer/qgscomposerscalebar.h b/src/core/composer/qgscomposerscalebar.h index 8b88081be81..aa2ad1cb44d 100644 --- a/src/core/composer/qgscomposerscalebar.h +++ b/src/core/composer/qgscomposerscalebar.h @@ -56,9 +56,6 @@ class CORE_EXPORT QgsComposerScaleBar: public QObject, public QgsComposerItem 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 @@ class CORE_EXPORT QgsComposerScaleBar: public QObject, public QgsComposerItem of the segment*/ void segmentPositions( QList >& posWidthList ) const; - /**Returns height of mFont in points*/ - double fontHeight() const; - /**Sets box size suitable to content*/ void adjustBoxSize(); diff --git a/src/core/composer/qgsdoubleboxscalebarstyle.cpp b/src/core/composer/qgsdoubleboxscalebarstyle.cpp index 3e2c523f2db..ec90133e57d 100644 --- a/src/core/composer/qgsdoubleboxscalebarstyle.cpp +++ b/src/core/composer/qgsdoubleboxscalebarstyle.cpp @@ -45,7 +45,7 @@ void QgsDoubleBoxScaleBarStyle::draw( QPainter* p, double xOffset ) const { 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(); diff --git a/src/core/composer/qgsnumericscalebarstyle.cpp b/src/core/composer/qgsnumericscalebarstyle.cpp index cf95ee494d5..dd33da961aa 100644 --- a/src/core/composer/qgsnumericscalebarstyle.cpp +++ b/src/core/composer/qgsnumericscalebarstyle.cpp @@ -48,10 +48,9 @@ void QgsNumericScaleBarStyle::draw( QPainter* p, double xOffset ) const } 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 @@ QRectF QgsNumericScaleBarStyle::calculateBoxSize() const 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 diff --git a/src/core/composer/qgsscalebarstyle.cpp b/src/core/composer/qgsscalebarstyle.cpp index 30012691002..9181dbe1ae2 100644 --- a/src/core/composer/qgsscalebarstyle.cpp +++ b/src/core/composer/qgsscalebarstyle.cpp @@ -43,11 +43,10 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const 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 > segmentInfo; @@ -79,7 +78,7 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const 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 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const 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 @@ QRectF QgsScaleBarStyle::calculateBoxSize() const 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 @@ QRectF QgsScaleBarStyle::calculateBoxSize() const } 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 ); } diff --git a/src/core/composer/qgssingleboxscalebarstyle.cpp b/src/core/composer/qgssingleboxscalebarstyle.cpp index fa2ab2b2faf..d4e3e84ea19 100644 --- a/src/core/composer/qgssingleboxscalebarstyle.cpp +++ b/src/core/composer/qgssingleboxscalebarstyle.cpp @@ -40,7 +40,7 @@ void QgsSingleBoxScaleBarStyle::draw( QPainter* p, double xOffset ) const { 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() ); diff --git a/src/core/composer/qgsticksscalebarstyle.cpp b/src/core/composer/qgsticksscalebarstyle.cpp index a647a6fcf3f..a55c376e799 100644 --- a/src/core/composer/qgsticksscalebarstyle.cpp +++ b/src/core/composer/qgsticksscalebarstyle.cpp @@ -53,7 +53,7 @@ void QgsTicksScaleBarStyle::draw( QPainter* p, double xOffset ) const { 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();