[QGIS Commit] r11350 - in trunk/qgis: python/core src/core/composer

svn_qgis at osgeo.org svn_qgis at osgeo.org
Wed Aug 12 03:35:40 EDT 2009


Author: mhugent
Date: 2009-08-12 03:35:38 -0400 (Wed, 12 Aug 2009)
New Revision: 11350

Modified:
   trunk/qgis/python/core/qgscomposerlabel.sip
   trunk/qgis/src/core/composer/qgscomposerlabel.cpp
   trunk/qgis/src/core/composer/qgscomposerlabel.h
Log:
Fix current_date field in composer label

Modified: trunk/qgis/python/core/qgscomposerlabel.sip
===================================================================
--- trunk/qgis/python/core/qgscomposerlabel.sip	2009-08-12 06:47:32 UTC (rev 11349)
+++ trunk/qgis/python/core/qgscomposerlabel.sip	2009-08-12 07:35:38 UTC (rev 11350)
@@ -18,6 +18,11 @@
 
     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();

Modified: trunk/qgis/src/core/composer/qgscomposerlabel.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgscomposerlabel.cpp	2009-08-12 06:47:32 UTC (rev 11349)
+++ trunk/qgis/src/core/composer/qgscomposerlabel.cpp	2009-08-12 07:35:38 UTC (rev 11350)
@@ -47,9 +47,10 @@
   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() )
   {
@@ -59,6 +60,9 @@
 
 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;
@@ -79,8 +83,37 @@
       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 )
 {
   mFont = f;
@@ -88,7 +121,7 @@
 
 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, \

Modified: trunk/qgis/src/core/composer/qgscomposerlabel.h
===================================================================
--- trunk/qgis/src/core/composer/qgscomposerlabel.h	2009-08-12 06:47:32 UTC (rev 11349)
+++ trunk/qgis/src/core/composer/qgscomposerlabel.h	2009-08-12 07:35:38 UTC (rev 11350)
@@ -36,6 +36,11 @@
 
     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;}
@@ -61,6 +66,9 @@
 
     // 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



More information about the QGIS-commit mailing list