Skip to content

Commit

Permalink
Fix current_date field in composer label
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11350 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Aug 12, 2009
1 parent cce37c0 commit 68cd1a8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
5 changes: 5 additions & 0 deletions python/core/qgscomposerlabel.sip
Expand Up @@ -18,6 +18,11 @@ class QgsComposerLabel: QgsComposerItem

QString text();
void setText( const QString& text );

/**Returns the text as it appears on screen (with replaced data field)
@note this function was added in version 1.2*/
QString displayText() const;

QFont font() const;
void setFont( const QFont& f );
double margin();
Expand Down
39 changes: 36 additions & 3 deletions src/core/composer/qgscomposerlabel.cpp
Expand Up @@ -47,8 +47,9 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
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 );
drawText( painter, painterRect, mText, mFont );


drawText( painter, painterRect, displayText(), mFont );

drawFrame( painter );
if ( isSelected() )
Expand All @@ -59,6 +60,9 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*

void QgsComposerLabel::setText( const QString& text )
{
mText = text;

#if 0
//replace '$CURRENT_DATE<(FORMAT)>' with the current date
//e.g. $CURRENT_DATE(d 'June' yyyy)
mText = text;
Expand All @@ -79,6 +83,35 @@ void QgsComposerLabel::setText( const QString& text )
mText.replace( "$CURRENT_DATE", QDate::currentDate().toString() );
}
}
#endif //0
}

QString QgsComposerLabel::displayText() const
{
QString displayText = mText;
replaceDateText(displayText);
return displayText;
}

void QgsComposerLabel::replaceDateText(QString& text) const
{
int currentDatePos = text.indexOf( "$CURRENT_DATE" );
if ( currentDatePos != -1 )
{
//check if there is a bracket just after $CURRENT_DATE
QString formatText;
int openingBracketPos = text.indexOf( "(", currentDatePos );
int closingBracketPos = text.indexOf( ")", openingBracketPos + 1 );
if ( openingBracketPos != -1 && closingBracketPos != -1 && ( closingBracketPos - openingBracketPos ) > 1 )
{
formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
}
else //no bracket
{
text.replace( "$CURRENT_DATE", QDate::currentDate().toString() );
}
}
}

void QgsComposerLabel::setFont( const QFont& f )
Expand All @@ -88,7 +121,7 @@ void QgsComposerLabel::setFont( const QFont& f )

void QgsComposerLabel::adjustSizeToText()
{
double textWidth = textWidthMillimeters( mFont, mText );
double textWidth = textWidthMillimeters( mFont, displayText() );
double fontAscent = fontAscentMillimeters( mFont );

setSceneRect( QRectF( transform().dx(), transform().dy(), textWidth + 2 * mMargin + 2 * pen().widthF() + 1, \
Expand Down
8 changes: 8 additions & 0 deletions src/core/composer/qgscomposerlabel.h
Expand Up @@ -36,6 +36,11 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem

QString text() {return mText;}
void setText( const QString& text );

/**Returns the text as it appears on screen (with replaced data field)
@note this function was added in version 1.2*/
QString displayText() const;

QFont font() const;
void setFont( const QFont& f );
double margin() {return mMargin;}
Expand All @@ -61,6 +66,9 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem

// Border between text and fram (in mm)
double mMargin;

/**Replaces replace '$CURRENT_DATE<(FORMAT)>' with the current date (e.g. $CURRENT_DATE(d 'June' yyyy)*/
void replaceDateText(QString& text) const;
};

#endif
Expand Down

0 comments on commit 68cd1a8

Please sign in to comment.