Fix current_date field in composer label

git-svn-id: http://svn.osgeo.org/qgis/trunk@11350 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
mhugent 2009-08-12 07:35:38 +00:00
parent 2811ad83c0
commit 702fc884ed
3 changed files with 49 additions and 3 deletions

View File

@ -18,6 +18,11 @@ class QgsComposerLabel: QgsComposerItem
QString text(); QString text();
void setText( const 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; QFont font() const;
void setFont( const QFont& f ); void setFont( const QFont& f );
double margin(); double margin();

View File

@ -47,8 +47,9 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
double penWidth = pen().widthF(); double penWidth = pen().widthF();
QRectF painterRect( penWidth + mMargin, penWidth + mMargin, rect().width() - 2 * penWidth - 2 * mMargin, QRectF painterRect( penWidth + mMargin, penWidth + mMargin, rect().width() - 2 * penWidth - 2 * mMargin,
rect().height() - 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 ); drawFrame( painter );
if ( isSelected() ) if ( isSelected() )
@ -59,6 +60,9 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
void QgsComposerLabel::setText( const QString& text ) void QgsComposerLabel::setText( const QString& text )
{ {
mText = text;
#if 0
//replace '$CURRENT_DATE<(FORMAT)>' with the current date //replace '$CURRENT_DATE<(FORMAT)>' with the current date
//e.g. $CURRENT_DATE(d 'June' yyyy) //e.g. $CURRENT_DATE(d 'June' yyyy)
mText = text; mText = text;
@ -79,6 +83,35 @@ void QgsComposerLabel::setText( const QString& text )
mText.replace( "$CURRENT_DATE", QDate::currentDate().toString() ); 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 ) void QgsComposerLabel::setFont( const QFont& f )
@ -88,7 +121,7 @@ void QgsComposerLabel::setFont( const QFont& f )
void QgsComposerLabel::adjustSizeToText() void QgsComposerLabel::adjustSizeToText()
{ {
double textWidth = textWidthMillimeters( mFont, mText ); double textWidth = textWidthMillimeters( mFont, displayText() );
double fontAscent = fontAscentMillimeters( mFont ); double fontAscent = fontAscentMillimeters( mFont );
setSceneRect( QRectF( transform().dx(), transform().dy(), textWidth + 2 * mMargin + 2 * pen().widthF() + 1, \ setSceneRect( QRectF( transform().dx(), transform().dy(), textWidth + 2 * mMargin + 2 * pen().widthF() + 1, \

View File

@ -36,6 +36,11 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
QString text() {return mText;} QString text() {return mText;}
void setText( const 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; QFont font() const;
void setFont( const QFont& f ); void setFont( const QFont& f );
double margin() {return mMargin;} double margin() {return mMargin;}
@ -61,6 +66,9 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
// Border between text and fram (in mm) // Border between text and fram (in mm)
double mMargin; double mMargin;
/**Replaces replace '$CURRENT_DATE<(FORMAT)>' with the current date (e.g. $CURRENT_DATE(d 'June' yyyy)*/
void replaceDateText(QString& text) const;
}; };
#endif #endif