Skip to content

Commit 68cd1a8

Browse files
author
mhugent
committed
Fix current_date field in composer label
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11350 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent cce37c0 commit 68cd1a8

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

python/core/qgscomposerlabel.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class QgsComposerLabel: QgsComposerItem
1818

1919
QString text();
2020
void setText( const QString& text );
21+
22+
/**Returns the text as it appears on screen (with replaced data field)
23+
@note this function was added in version 1.2*/
24+
QString displayText() const;
25+
2126
QFont font() const;
2227
void setFont( const QFont& f );
2328
double margin();

src/core/composer/qgscomposerlabel.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
4747
double penWidth = pen().widthF();
4848
QRectF painterRect( penWidth + mMargin, penWidth + mMargin, rect().width() - 2 * penWidth - 2 * mMargin,
4949
rect().height() - 2 * penWidth - 2 * mMargin );
50-
//painter->drawText( painterRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, mText );
51-
drawText( painter, painterRect, mText, mFont );
50+
51+
52+
drawText( painter, painterRect, displayText(), mFont );
5253

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

6061
void QgsComposerLabel::setText( const QString& text )
6162
{
63+
mText = text;
64+
65+
#if 0
6266
//replace '$CURRENT_DATE<(FORMAT)>' with the current date
6367
//e.g. $CURRENT_DATE(d 'June' yyyy)
6468
mText = text;
@@ -79,6 +83,35 @@ void QgsComposerLabel::setText( const QString& text )
7983
mText.replace( "$CURRENT_DATE", QDate::currentDate().toString() );
8084
}
8185
}
86+
#endif //0
87+
}
88+
89+
QString QgsComposerLabel::displayText() const
90+
{
91+
QString displayText = mText;
92+
replaceDateText(displayText);
93+
return displayText;
94+
}
95+
96+
void QgsComposerLabel::replaceDateText(QString& text) const
97+
{
98+
int currentDatePos = text.indexOf( "$CURRENT_DATE" );
99+
if ( currentDatePos != -1 )
100+
{
101+
//check if there is a bracket just after $CURRENT_DATE
102+
QString formatText;
103+
int openingBracketPos = text.indexOf( "(", currentDatePos );
104+
int closingBracketPos = text.indexOf( ")", openingBracketPos + 1 );
105+
if ( openingBracketPos != -1 && closingBracketPos != -1 && ( closingBracketPos - openingBracketPos ) > 1 )
106+
{
107+
formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
108+
text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
109+
}
110+
else //no bracket
111+
{
112+
text.replace( "$CURRENT_DATE", QDate::currentDate().toString() );
113+
}
114+
}
82115
}
83116

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

89122
void QgsComposerLabel::adjustSizeToText()
90123
{
91-
double textWidth = textWidthMillimeters( mFont, mText );
124+
double textWidth = textWidthMillimeters( mFont, displayText() );
92125
double fontAscent = fontAscentMillimeters( mFont );
93126

94127
setSceneRect( QRectF( transform().dx(), transform().dy(), textWidth + 2 * mMargin + 2 * pen().widthF() + 1, \

src/core/composer/qgscomposerlabel.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
3636

3737
QString text() {return mText;}
3838
void setText( const QString& text );
39+
40+
/**Returns the text as it appears on screen (with replaced data field)
41+
@note this function was added in version 1.2*/
42+
QString displayText() const;
43+
3944
QFont font() const;
4045
void setFont( const QFont& f );
4146
double margin() {return mMargin;}
@@ -61,6 +66,9 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
6166

6267
// Border between text and fram (in mm)
6368
double mMargin;
69+
70+
/**Replaces replace '$CURRENT_DATE<(FORMAT)>' with the current date (e.g. $CURRENT_DATE(d 'June' yyyy)*/
71+
void replaceDateText(QString& text) const;
6472
};
6573

6674
#endif

0 commit comments

Comments
 (0)