Skip to content

Commit

Permalink
[composer] Allow for finer control over text rendered with QgsCompose…
Browse files Browse the repository at this point in the history
…rItem::drawText, fixes clipped text when using italic fonts in legend titles
  • Loading branch information
nyalldawson committed May 15, 2014
1 parent 25d991c commit ecf2600
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
6 changes: 4 additions & 2 deletions python/core/composer/qgscomposeritem.sip
Expand Up @@ -353,8 +353,10 @@ class QgsComposerItem : QObject, QGraphicsRectItem
to work around the Qt font bug)*/
void drawText( QPainter* p, double x, double 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, Qt::AlignmentFlag halignment = Qt::AlignLeft, Qt::AlignmentFlag valignment = Qt::AlignTop ) const;
/**Like the above, but with a rectangle for multiline text
* @param flags allows for passing Qt::TextFlags to control appearance of rendered text
*/
void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment = Qt::AlignLeft, Qt::AlignmentFlag valignment = Qt::AlignTop, int flags = Qt::TextWordWrap ) const;

/**Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double textWidthMillimeters( const QFont& font, const QString& text ) const;
Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgscomposeritem.cpp
Expand Up @@ -619,7 +619,7 @@ void QgsComposerItem::drawText( QPainter* p, double x, double y, const QString&
p->restore();
}

void QgsComposerItem::drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment, Qt::AlignmentFlag valignment ) const
void QgsComposerItem::drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment, Qt::AlignmentFlag valignment, int flags ) const
{
QFont textFont = scaledFontPixelSize( font );

Expand All @@ -630,7 +630,7 @@ void QgsComposerItem::drawText( QPainter* p, const QRectF& rect, const QString&
p->setFont( textFont );
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
p->scale( scaleFactor, scaleFactor );
p->drawText( scaledRect, halignment | valignment | Qt::TextWordWrap, text );
p->drawText( scaledRect, halignment | valignment | flags, text );
p->restore();
}
void QgsComposerItem::drawArrowHead( QPainter* p, double x, double y, double angle, double arrowHeadWidth ) const
Expand Down
6 changes: 4 additions & 2 deletions src/core/composer/qgscomposeritem.h
Expand Up @@ -313,8 +313,10 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
to work around the Qt font bug)*/
void drawText( QPainter* p, double x, double 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, Qt::AlignmentFlag halignment = Qt::AlignLeft, Qt::AlignmentFlag valignment = Qt::AlignTop ) const;
/**Like the above, but with a rectangle for multiline text
* @param flags allows for passing Qt::TextFlags to control appearance of rendered text
*/
void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment = Qt::AlignLeft, Qt::AlignmentFlag valignment = Qt::AlignTop, int flags = Qt::TextWordWrap ) const;

/**Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double textWidthMillimeters( const QFont& font, const QString& text ) const;
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerlabel.cpp
Expand Up @@ -152,7 +152,7 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
//debug
//painter->setPen( QColor( Qt::red ) );
//painter->drawRect( painterRect );
drawText( painter, painterRect, textToDraw, mFont, mHAlignment, mVAlignment );
drawText( painter, painterRect, textToDraw, mFont, mHAlignment, mVAlignment, Qt::TextWordWrap );
}

painter->restore();
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerlegend.cpp
Expand Up @@ -249,7 +249,7 @@ QSizeF QgsComposerLegend::drawTitle( QPainter* painter, QPointF point, Qt::Align

if ( painter )
{
drawText( painter, r, *titlePart, styleFont( QgsComposerLegendStyle::Title ), halignment, Qt::AlignVCenter );
drawText( painter, r, *titlePart, styleFont( QgsComposerLegendStyle::Title ), halignment, Qt::AlignVCenter, Qt::TextDontClip );
}

//update max width of title
Expand Down

0 comments on commit ecf2600

Please sign in to comment.