Skip to content

Commit 106f094

Browse files
author
mhugent
committedSep 5, 2008
Font workaround enabled for composer scale bar
git-svn-id: http://svn.osgeo.org/qgis/trunk@9261 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 14310d2 commit 106f094

9 files changed

+46
-87
lines changed
 

‎src/core/composer/qgscomposeritem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ void QgsComposerItem::hoverMoveEvent( QGraphicsSceneHoverEvent * event )
523523
}
524524
}
525525

526-
void QgsComposerItem::drawText(QPainter* p, int x, int y, const QString& text, const QFont& font)
526+
void QgsComposerItem::drawText(QPainter* p, int x, int y, const QString& text, const QFont& font) const
527527
{
528528
QFont textFont = scaledFontPixelSize(font);
529529

@@ -535,7 +535,7 @@ void QgsComposerItem::drawText(QPainter* p, int x, int y, const QString& text, c
535535
p->restore();
536536
}
537537

538-
void QgsComposerItem::drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font)
538+
void QgsComposerItem::drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font) const
539539
{
540540
QFont textFont = scaledFontPixelSize(font);
541541

‎src/core/composer/qgscomposeritem.h

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,28 @@ class CORE_EXPORT QgsComposerItem: public QGraphicsRectItem
105105

106106
const QgsComposition* composition() const {return mComposition;}
107107

108+
//functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
109+
//painter down by the same factor for drawing
110+
111+
/**Draws Text. Takes care about all the composer specific issues (calculation to pixel, scaling of font and painter
112+
to work arount the Qt font bug)*/
113+
void drawText(QPainter* p, int x, int y, const QString& text, const QFont& font) const;
114+
115+
/**Like the above, but with a rectangle for multiline text*/
116+
void drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font) const;
117+
118+
/**Returns the font width in MM (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
119+
double textWidthMM(const QFont& font, const QString& text) const;
120+
121+
/**Returns the font ascent in MM (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
122+
double fontAscentMM(const QFont& font) const;
123+
124+
/**Calculates font to from point size to pixel size*/
125+
double pixelFontSize(double pointSize) const;
126+
127+
/**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE*/
128+
QFont scaledFontPixelSize(const QFont& font) const;
129+
108130
protected:
109131

110132
QgsComposition* mComposition;
@@ -151,25 +173,6 @@ class CORE_EXPORT QgsComposerItem: public QGraphicsRectItem
151173

152174
/**Draw background*/
153175
virtual void drawBackground( QPainter* p );
154-
155-
/**Draws Text. Takes care about all the composer specific issues (calculation to pixel, scaling of font and painter
156-
to work arount the Qt font bug)*/
157-
void drawText(QPainter* p, int x, int y, const QString& text, const QFont& font);
158-
159-
/**Like the above, but with a rectangle for multiline text*/
160-
void drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font);
161-
162-
/**Returns the font width in MM (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
163-
double textWidthMM(const QFont& font, const QString& text) const;
164-
165-
/**Returns the font ascent in MM (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
166-
double fontAscentMM(const QFont& font) const;
167-
168-
/**Calculates font to from point size to pixel size*/
169-
double pixelFontSize(double pointSize) const;
170-
171-
/**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE*/
172-
QFont scaledFontPixelSize(const QFont& font) const;
173176
};
174177

175178
#endif

‎src/core/composer/qgscomposerscalebar.cpp

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ void QgsComposerScaleBar::paint( QPainter* painter, const QStyleOptionGraphicsIt
4848
drawBackground( painter );
4949
painter->setPen( QPen( QColor( 0, 0, 0 ) ) ); //draw all text black
5050

51-
//calculate half of first label width as labels are drawn centered
52-
QFontMetricsF fontMetrics( mFont );
51+
//x-offset is half of first label width because labels are drawn centered
5352
QString firstLabel = firstLabelString();
53+
double firstLabelWidth = textWidthMM(mFont, firstLabel);
5454

55-
mStyle->draw( painter, fontMetrics.width( firstLabel ) / 2 );
55+
mStyle->draw( painter, firstLabelWidth / 2 );
5656

5757
//draw frame and selection boxes if necessary
5858
drawFrame( painter );
@@ -126,15 +126,7 @@ void QgsComposerScaleBar::applyDefaultSettings()
126126
mBrush.setColor( QColor( 0, 0, 0 ) );
127127
mBrush.setStyle( Qt::SolidPattern );
128128

129-
//default size 12 point
130-
if ( mComposition )
131-
{
132-
mFont.setPixelSize( mComposition->pixelFontSize( 12 ) );
133-
}
134-
else
135-
{
136-
mFont.setPixelSize( 5 );
137-
}
129+
mFont.setPointSizeF(12.0);
138130

139131
mLabelBarSpace = 3.0;
140132
mBoxContentSpace = 1.0;
@@ -173,12 +165,6 @@ void QgsComposerScaleBar::update()
173165
QgsComposerItem::update();
174166
}
175167

176-
double QgsComposerScaleBar::fontHeight() const
177-
{
178-
QFontMetricsF labelFontMetrics( mFont );
179-
return labelFontMetrics.ascent();
180-
}
181-
182168
void QgsComposerScaleBar::updateSegmentSize()
183169
{
184170
refreshSegmentMM();
@@ -267,34 +253,13 @@ QString QgsComposerScaleBar::firstLabelString() const
267253
}
268254

269255
QFont QgsComposerScaleBar::font() const
270-
{
271-
if ( mComposition ) //make pixel to point conversion to show correct point value in dialogs
272-
{
273-
double pointSize = mComposition->pointFontSize( mFont.pixelSize() );
274-
QFont returnFont = mFont;
275-
returnFont.setPointSize( pointSize );
276-
return returnFont;
277-
}
278-
}
279-
280-
QFont QgsComposerScaleBar::fontPixelSize() const
281256
{
282257
return mFont;
283258
}
284259

285260
void QgsComposerScaleBar::setFont( const QFont& font )
286261
{
287-
//set font size in pixels for proper preview and printout
288-
if ( mComposition )
289-
{
290-
int pixelSize = mComposition->pixelFontSize( font.pointSizeF() );
291-
mFont = font;
292-
mFont.setPixelSize( pixelSize );
293-
}
294-
else
295-
{
296-
mFont = font;
297-
}
262+
mFont = font;
298263
adjustBoxSize();
299264
update();
300265
}

‎src/core/composer/qgscomposerscalebar.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ class CORE_EXPORT QgsComposerScaleBar: public QObject, public QgsComposerItem
5656
void setUnitLabeling( const QString& label ) {mUnitLabeling = label;}
5757

5858
QFont font() const;
59-
60-
/**Returns font that has size set in pixels. Used from QgsComposerScaleBarStyle*/
61-
QFont fontPixelSize() const;
6259

6360
void setFont( const QFont& font );
6461

@@ -96,9 +93,6 @@ class CORE_EXPORT QgsComposerScaleBar: public QObject, public QgsComposerItem
9693
of the segment*/
9794
void segmentPositions( QList<QPair<double, double> >& posWidthList ) const;
9895

99-
/**Returns height of mFont in points*/
100-
double fontHeight() const;
101-
10296
/**Sets box size suitable to content*/
10397
void adjustBoxSize();
10498

‎src/core/composer/qgsdoubleboxscalebarstyle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void QgsDoubleBoxScaleBarStyle::draw( QPainter* p, double xOffset ) const
4545
{
4646
return;
4747
}
48-
double barTopPosition = mScaleBar->fontHeight() + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
48+
double barTopPosition = mScaleBar->fontAscentMM(mScaleBar->font()) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
4949
double segmentHeight = mScaleBar->height() / 2;
5050

5151
p->save();

‎src/core/composer/qgsnumericscalebarstyle.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ void QgsNumericScaleBarStyle::draw( QPainter* p, double xOffset ) const
4848
}
4949

5050
p->save();
51-
5251
p->setFont( mScaleBar->font() );
53-
p->drawText( QPointF( mScaleBar->pen().widthF() + mScaleBar->boxContentSpace(), mScaleBar->boxContentSpace()
54-
+ mScaleBar->fontHeight() ), scaleText() );
52+
53+
mScaleBar->drawText(p, mScaleBar->pen().widthF() + mScaleBar->boxContentSpace(), mScaleBar->boxContentSpace() + mScaleBar->fontAscentMM(mScaleBar->font()), scaleText(), mScaleBar->font());
5554

5655
p->restore();
5756
}
@@ -64,10 +63,12 @@ QRectF QgsNumericScaleBarStyle::calculateBoxSize() const
6463
return rect;
6564
}
6665

67-
QFontMetricsF fontMetrics( mScaleBar->font() );
66+
double textWidth = mScaleBar->textWidthMM(mScaleBar->font(), scaleText());
67+
double textHeight = mScaleBar->fontAscentMM(mScaleBar->font());
68+
6869
return QRectF( mScaleBar->transform().dx(), mScaleBar->transform().dy(), 2 * mScaleBar->boxContentSpace()
69-
+ 2 * mScaleBar->pen().width() + fontMetrics.width( scaleText() ),
70-
mScaleBar->fontHeight() + 2 * mScaleBar->boxContentSpace() );
70+
+ 2 * mScaleBar->pen().width() + textWidth,
71+
textHeight + 2 * mScaleBar->boxContentSpace() );
7172
}
7273

7374
QString QgsNumericScaleBarStyle::scaleText() const

‎src/core/composer/qgsscalebarstyle.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const
4343

4444
p->save();
4545

46-
p->setFont( mScaleBar->fontPixelSize() );
46+
p->setFont(mScaleBar->font());
4747

48-
QFontMetricsF fontMetrics( mScaleBar->fontPixelSize() );
4948
QString firstLabel = mScaleBar->firstLabelString();
50-
double xOffset = fontMetrics.width( firstLabel ) / 2;
49+
double xOffset = mScaleBar->textWidthMM(mScaleBar->font(), firstLabel) / 2;
5150

5251
//double mCurrentXCoord = mScaleBar->pen().widthF() + mScaleBar->boxContentSpace();
5352
QList<QPair<double, double> > segmentInfo;
@@ -79,7 +78,7 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const
7978

8079
if ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) //don't draw label for intermediate left segments
8180
{
82-
p->drawText( QPointF( segmentIt->first - fontMetrics.width( currentNumericLabel ) / 2 + xOffset, mScaleBar->fontHeight() + mScaleBar->boxContentSpace() ), currentNumericLabel );
81+
mScaleBar->drawText(p, segmentIt->first - mScaleBar->textWidthMM(mScaleBar->font(), currentNumericLabel) / 2 + xOffset, mScaleBar->fontAscentMM(mScaleBar->font()) + mScaleBar->boxContentSpace(), currentNumericLabel, mScaleBar->font());
8382
}
8483

8584
if ( segmentCounter >= nSegmentsLeft )
@@ -93,7 +92,7 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const
9392
if ( !segmentInfo.isEmpty() )
9493
{
9594
currentNumericLabel = QString::number( currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit() );
96-
p->drawText( QPointF( segmentInfo.last().first + mScaleBar->segmentMM() - fontMetrics.width( currentNumericLabel ) / 2 + xOffset, mScaleBar->fontHeight() + mScaleBar->boxContentSpace() ), currentNumericLabel + " " + mScaleBar->unitLabeling() );
95+
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());
9796
}
9897

9998
p->restore();
@@ -106,18 +105,15 @@ QRectF QgsScaleBarStyle::calculateBoxSize() const
106105
return QRectF();
107106
}
108107

109-
110-
QFontMetricsF fontMetrics( mScaleBar->fontPixelSize() );
111-
112108
//consider centered first label
113-
double firstLabelLeft = fontMetrics.width( mScaleBar->firstLabelString() ) / 2;
109+
double firstLabelLeft = mScaleBar->textWidthMM(mScaleBar->font(), mScaleBar->firstLabelString()) / 2;
114110

115111
//consider last number and label
116112

117113
double largestLabelNumber = mScaleBar->numSegments() * mScaleBar->numUnitsPerSegment() / mScaleBar->numMapUnitsPerScaleBarUnit();
118114
QString largestNumberLabel = QString::number( largestLabelNumber );
119115
QString largestLabel = QString::number( largestLabelNumber ) + " " + mScaleBar->unitLabeling();
120-
double largestLabelWidth = fontMetrics.width( largestLabel ) - fontMetrics.width( largestNumberLabel ) / 2;
116+
double largestLabelWidth = mScaleBar->textWidthMM(mScaleBar->font(), largestLabel) - mScaleBar->textWidthMM(mScaleBar->font(), largestNumberLabel) / 2;
121117

122118
double totalBarLength = 0.0;
123119

@@ -131,7 +127,7 @@ QRectF QgsScaleBarStyle::calculateBoxSize() const
131127
}
132128

133129
double width = firstLabelLeft + totalBarLength + 2 * mScaleBar->pen().widthF() + largestLabelWidth + 2 * mScaleBar->boxContentSpace();
134-
double height = mScaleBar->height() + mScaleBar->labelBarSpace() + 2 * mScaleBar->boxContentSpace() + mScaleBar->fontHeight();
130+
double height = mScaleBar->height() + mScaleBar->labelBarSpace() + 2 * mScaleBar->boxContentSpace() + mScaleBar->fontAscentMM(mScaleBar->font());
135131

136132
return QRectF( mScaleBar->transform().dx(), mScaleBar->transform().dy(), width, height );
137133
}

‎src/core/composer/qgssingleboxscalebarstyle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void QgsSingleBoxScaleBarStyle::draw( QPainter* p, double xOffset ) const
4040
{
4141
return;
4242
}
43-
double barTopPosition = mScaleBar->fontHeight() + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
43+
double barTopPosition = mScaleBar->fontAscentMM(mScaleBar->font()) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
4444

4545
p->save();
4646
p->setPen( p->pen() );

‎src/core/composer/qgsticksscalebarstyle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void QgsTicksScaleBarStyle::draw( QPainter* p, double xOffset ) const
5353
{
5454
return;
5555
}
56-
double barTopPosition = mScaleBar->fontHeight() + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
56+
double barTopPosition = mScaleBar->fontAscentMM(mScaleBar->font()) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
5757
double middlePosition = barTopPosition + mScaleBar->height() / 2.0;
5858
double bottomPosition = barTopPosition + mScaleBar->height();
5959

0 commit comments

Comments
 (0)
Please sign in to comment.