Quantum GIS API Documentation  1.7.4
src/core/composer/qgscomposerlabel.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                          qgscomposerlabel.cpp
00003                              -------------------
00004     begin                : January 2005
00005     copyright            : (C) 2005 by Radim Blazek
00006     email                : [email protected]
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "qgscomposerlabel.h"
00019 #include <QDate>
00020 #include <QDomElement>
00021 #include <QPainter>
00022 
00023 QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ): QgsComposerItem( composition ), mMargin( 1.0 ), mFontColor( QColor( 0, 0, 0 ) ), \
00024     mHAlignment( Qt::AlignLeft ), mVAlignment( Qt::AlignTop )
00025 {
00026   //default font size is 10 point
00027   mFont.setPointSizeF( 10 );
00028 }
00029 
00030 QgsComposerLabel::~QgsComposerLabel()
00031 {
00032 }
00033 
00034 void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
00035 {
00036   if ( !painter )
00037   {
00038     return;
00039   }
00040 
00041   drawBackground( painter );
00042   painter->setPen( QPen( QColor( mFontColor ) ) ); //draw all text black
00043   painter->setFont( mFont );
00044 
00045   QFontMetricsF fontSize( mFont );
00046 
00047   //support multiline labels
00048   double penWidth = pen().widthF();
00049   QRectF painterRect( penWidth + mMargin, penWidth + mMargin, rect().width() - 2 * penWidth - 2 * mMargin,
00050                       rect().height() - 2 * penWidth - 2 * mMargin );
00051 
00052 
00053   drawText( painter, painterRect, displayText(), mFont, mHAlignment, mVAlignment );
00054 
00055   drawFrame( painter );
00056   if ( isSelected() )
00057   {
00058     drawSelectionBoxes( painter );
00059   }
00060 }
00061 
00062 void QgsComposerLabel::setText( const QString& text )
00063 {
00064   mText = text;
00065 }
00066 
00067 QString QgsComposerLabel::displayText() const
00068 {
00069   QString displayText = mText;
00070   replaceDateText( displayText );
00071   return displayText;
00072 }
00073 
00074 void QgsComposerLabel::replaceDateText( QString& text ) const
00075 {
00076   int currentDatePos = text.indexOf( "$CURRENT_DATE" );
00077   if ( currentDatePos != -1 )
00078   {
00079     //check if there is a bracket just after $CURRENT_DATE
00080     QString formatText;
00081     int openingBracketPos = text.indexOf( "(", currentDatePos );
00082     int closingBracketPos = text.indexOf( ")", openingBracketPos + 1 );
00083     if ( openingBracketPos != -1 && closingBracketPos != -1 && ( closingBracketPos - openingBracketPos ) > 1 )
00084     {
00085       formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
00086       text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
00087     }
00088     else //no bracket
00089     {
00090       text.replace( "$CURRENT_DATE", QDate::currentDate().toString() );
00091     }
00092   }
00093 }
00094 
00095 void QgsComposerLabel::setFont( const QFont& f )
00096 {
00097   mFont = f;
00098 }
00099 
00100 void QgsComposerLabel::adjustSizeToText()
00101 {
00102   double textWidth = textWidthMillimeters( mFont, displayText() );
00103   double fontAscent = fontAscentMillimeters( mFont );
00104 
00105   setSceneRect( QRectF( transform().dx(), transform().dy(), textWidth + 2 * mMargin + 2 * pen().widthF() + 1, \
00106                         fontAscent + 2 * mMargin + 2 * pen().widthF() + 1 ) );
00107 }
00108 
00109 QFont QgsComposerLabel::font() const
00110 {
00111   return mFont;
00112 }
00113 
00114 bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const
00115 {
00116   QString alignment;
00117 
00118   if ( elem.isNull() )
00119   {
00120     return false;
00121   }
00122 
00123   QDomElement composerLabelElem = doc.createElement( "ComposerLabel" );
00124 
00125   composerLabelElem.setAttribute( "labelText", mText );
00126   composerLabelElem.setAttribute( "margin", QString::number( mMargin ) );
00127 
00128   composerLabelElem.setAttribute( "halign", mHAlignment );
00129   composerLabelElem.setAttribute( "valign", mVAlignment );
00130   composerLabelElem.setAttribute( "id", mId );
00131 
00132 
00133   //font
00134   QDomElement labelFontElem = doc.createElement( "LabelFont" );
00135   labelFontElem.setAttribute( "description", mFont.toString() );
00136   composerLabelElem.appendChild( labelFontElem );
00137 
00138   //font color
00139   QDomElement fontColorElem = doc.createElement( "FontColor" );
00140   fontColorElem.setAttribute( "red", mFontColor.red() );
00141   fontColorElem.setAttribute( "green", mFontColor.green() );
00142   fontColorElem.setAttribute( "blue", mFontColor.blue() );
00143   composerLabelElem.appendChild( fontColorElem );
00144 
00145   elem.appendChild( composerLabelElem );
00146   return _writeXML( composerLabelElem, doc );
00147 }
00148 
00149 bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument& doc )
00150 {
00151   QString alignment;
00152 
00153   if ( itemElem.isNull() )
00154   {
00155     return false;
00156   }
00157 
00158   //restore label specific properties
00159 
00160   //text
00161   mText = itemElem.attribute( "labelText" );
00162 
00163   //margin
00164   mMargin = itemElem.attribute( "margin" ).toDouble();
00165 
00166   //Horizontal alignment
00167   mHAlignment = ( Qt::AlignmentFlag )( itemElem.attribute( "halign" ).toInt() );
00168 
00169   //Vertical alignment
00170   mVAlignment = ( Qt::AlignmentFlag )( itemElem.attribute( "valign" ).toInt() );
00171 
00172   //id
00173   mId = itemElem.attribute( "id", "" );
00174 
00175   //font
00176   QDomNodeList labelFontList = itemElem.elementsByTagName( "LabelFont" );
00177   if ( labelFontList.size() > 0 )
00178   {
00179     QDomElement labelFontElem = labelFontList.at( 0 ).toElement();
00180     mFont.fromString( labelFontElem.attribute( "description" ) );
00181   }
00182 
00183   //font color
00184   QDomNodeList fontColorList = itemElem.elementsByTagName( "FontColor" );
00185   if ( fontColorList.size() > 0 )
00186   {
00187     QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
00188     int red = fontColorElem.attribute( "red", "0" ).toInt();
00189     int green = fontColorElem.attribute( "green", "0" ).toInt();
00190     int blue = fontColorElem.attribute( "blue", "0" ).toInt();
00191     mFontColor = QColor( red, green, blue );
00192   }
00193   else
00194   {
00195     mFontColor = QColor( 0, 0, 0 );
00196   }
00197 
00198   //restore general composer item properties
00199   QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
00200   if ( composerItemList.size() > 0 )
00201   {
00202     QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
00203     _readXML( composerItemElem, doc );
00204   }
00205   emit itemChanged();
00206   return true;
00207 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines