[QGIS Commit] r9260 - trunk/qgis/src/core/composer
svn_qgis at osgeo.org
svn_qgis at osgeo.org
Fri Sep 5 05:18:42 EDT 2008
Author: mhugent
Date: 2008-09-05 05:18:42 -0400 (Fri, 05 Sep 2008)
New Revision: 9260
Modified:
trunk/qgis/src/core/composer/qgscomposeritem.cpp
trunk/qgis/src/core/composer/qgscomposeritem.h
trunk/qgis/src/core/composer/qgscomposerlabel.cpp
Log:
Use qt font bug workaround for composer label. Other items will follow soon. All the up-and downscaling is done in a central place in composer item base class.
Modified: trunk/qgis/src/core/composer/qgscomposeritem.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgscomposeritem.cpp 2008-09-05 08:46:02 UTC (rev 9259)
+++ trunk/qgis/src/core/composer/qgscomposeritem.cpp 2008-09-05 09:18:42 UTC (rev 9260)
@@ -27,6 +27,8 @@
#include "qgsrect.h" //just for debugging
#include "qgslogger.h"
+#define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter
+
QgsComposerItem::QgsComposerItem( QgsComposition* composition ): QGraphicsRectItem( 0 ), mComposition( composition ), mBoundingResizeRectangle( 0 ), mFrame( true )
{
setFlag( QGraphicsItem::ItemIsSelectable, true );
@@ -520,3 +522,57 @@
setCursor( cursorForPosition( event->pos() ) );
}
}
+
+void QgsComposerItem::drawText(QPainter* p, int x, int y, const QString& text, const QFont& font)
+{
+ QFont textFont = scaledFontPixelSize(font);
+
+ p->save();
+ p->setFont(textFont);
+ double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
+ p->scale(scaleFactor, scaleFactor);
+ p->drawText(x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE, text);
+ p->restore();
+}
+
+void QgsComposerItem::drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font)
+{
+ QFont textFont = scaledFontPixelSize(font);
+
+ QRectF scaledRect(rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
+ rect.width() * FONT_WORKAROUND_SCALE, rect.height() * FONT_WORKAROUND_SCALE);
+
+ p->save();
+ p->setFont(textFont);
+ double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
+ p->scale(scaleFactor, scaleFactor);
+ p->drawText(scaledRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, text);
+ p->restore();
+}
+
+double QgsComposerItem::textWidthMM(const QFont& font, const QString& text) const
+{
+ QFont metricsFont = scaledFontPixelSize(font);
+ QFontMetrics fontMetrics(metricsFont);
+ return (fontMetrics.width(text) / FONT_WORKAROUND_SCALE);
+}
+
+double QgsComposerItem::fontAscentMM(const QFont& font) const
+{
+ QFont metricsFont = scaledFontPixelSize(font);
+ QFontMetrics fontMetrics(metricsFont);
+ return (fontMetrics.ascent() / FONT_WORKAROUND_SCALE);
+}
+
+double QgsComposerItem::pixelFontSize(double pointSize) const
+{
+ return (pointSize * 0.3527);
+}
+
+QFont QgsComposerItem::scaledFontPixelSize(const QFont& font) const
+{
+ QFont scaledFont = font;
+ double pixelSize = pixelFontSize(font.pointSizeF()) * FONT_WORKAROUND_SCALE + 0.5;
+ scaledFont.setPixelSize(pixelSize);
+ return scaledFont;
+}
Modified: trunk/qgis/src/core/composer/qgscomposeritem.h
===================================================================
--- trunk/qgis/src/core/composer/qgscomposeritem.h 2008-09-05 08:46:02 UTC (rev 9259)
+++ trunk/qgis/src/core/composer/qgscomposeritem.h 2008-09-05 09:18:42 UTC (rev 9260)
@@ -151,6 +151,25 @@
/**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/qgscomposerlabel.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgscomposerlabel.cpp 2008-09-05 08:46:02 UTC (rev 9259)
+++ trunk/qgis/src/core/composer/qgscomposerlabel.cpp 2008-09-05 09:18:42 UTC (rev 9260)
@@ -19,17 +19,10 @@
#include <QDomElement>
#include <QPainter>
-QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ): QgsComposerItem( composition ), mMargin( 0.0 )
+QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ): QgsComposerItem( composition ), mMargin( 1.0 )
{
//default font size is 10 point
- if ( mComposition )
- {
- mFont.setPixelSize( mComposition->pixelFontSize( 10 ) );
- }
- else
- {
- mFont.setPixelSize( 4 );
- }
+ mFont.setPointSizeF(10);
}
QgsComposerLabel::~QgsComposerLabel()
@@ -53,9 +46,9 @@
double penWidth = pen().widthF();
QRectF painterRect( penWidth + mMargin, penWidth + mMargin, rect().width() - 2 * penWidth - 2 * mMargin,
rect().height() - 2 * penWidth - 2 * mMargin);
- painter->drawText( painterRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, mText );
+ //painter->drawText( painterRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, mText );
+ drawText(painter, painterRect, mText, mFont);
-
drawFrame( painter );
if ( isSelected() )
{
@@ -70,34 +63,20 @@
void QgsComposerLabel::setFont( const QFont& f )
{
- //set font size in pixels for proper preview and printout
- if ( mComposition )
- {
- int pixelSize = mComposition->pixelFontSize( f.pointSizeF() );
- mFont = f;
- mFont.setPixelSize( pixelSize );
- }
- else
- {
- mFont = f;
- }
+ mFont = f;
}
void QgsComposerLabel::adjustSizeToText()
{
- QFontMetricsF fontInfo( mFont );
- setSceneRect( QRectF( transform().dx(), transform().dy(), fontInfo.width( mText ) + 2 * mMargin + 2 * pen().widthF(), fontInfo.ascent() + 2 * mMargin + 2 * pen().widthF() ) );
+ double textWidth = textWidthMM(mFont, mText);
+ double fontAscent = fontAscentMM(mFont);
+
+ setSceneRect( QRectF( transform().dx(), transform().dy(), textWidth + 2 * mMargin + 2 * pen().widthF() + 1, \
+ fontAscent + 2 * mMargin + 2 * pen().widthF() + 1) );
}
QFont QgsComposerLabel::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;
- }
return mFont;
}
More information about the QGIS-commit
mailing list