Quantum GIS API Documentation  1.8
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                : blazek@itc.it
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   Q_UNUSED( itemStyle );
00037   Q_UNUSED( pWidget );
00038   if ( !painter )
00039   {
00040     return;
00041   }
00042 
00043   drawBackground( painter );
00044   painter->setPen( QPen( QColor( mFontColor ) ) ); //draw all text black
00045   painter->setFont( mFont );
00046 
00047   QFontMetricsF fontSize( mFont );
00048 
00049   //support multiline labels
00050   double penWidth = pen().widthF();
00051   QRectF painterRect( penWidth + mMargin, penWidth + mMargin, rect().width() - 2 * penWidth - 2 * mMargin,
00052                       rect().height() - 2 * penWidth - 2 * mMargin );
00053 
00054 
00055   drawText( painter, painterRect, displayText(), mFont, mHAlignment, mVAlignment );
00056 
00057   drawFrame( painter );
00058   if ( isSelected() )
00059   {
00060     drawSelectionBoxes( painter );
00061   }
00062 }
00063 
00064 void QgsComposerLabel::setText( const QString& text )
00065 {
00066   mText = text;
00067 }
00068 
00069 QString QgsComposerLabel::displayText() const
00070 {
00071   QString displayText = mText;
00072   replaceDateText( displayText );
00073   return displayText;
00074 }
00075 
00076 void QgsComposerLabel::replaceDateText( QString& text ) const
00077 {
00078   int currentDatePos = text.indexOf( "$CURRENT_DATE" );
00079   if ( currentDatePos != -1 )
00080   {
00081     //check if there is a bracket just after $CURRENT_DATE
00082     QString formatText;
00083     int openingBracketPos = text.indexOf( "(", currentDatePos );
00084     int closingBracketPos = text.indexOf( ")", openingBracketPos + 1 );
00085     if ( openingBracketPos != -1 && closingBracketPos != -1 && ( closingBracketPos - openingBracketPos ) > 1 )
00086     {
00087       formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
00088       text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
00089     }
00090     else //no bracket
00091     {
00092       text.replace( "$CURRENT_DATE", QDate::currentDate().toString() );
00093     }
00094   }
00095 }
00096 
00097 void QgsComposerLabel::setFont( const QFont& f )
00098 {
00099   mFont = f;
00100 }
00101 
00102 void QgsComposerLabel::adjustSizeToText()
00103 {
00104   double textWidth = textWidthMillimeters( mFont, displayText() );
00105   double fontAscent = fontAscentMillimeters( mFont );
00106 
00107   setSceneRect( QRectF( transform().dx(), transform().dy(), textWidth + 2 * mMargin + 2 * pen().widthF() + 1,
00108                         fontAscent + 2 * mMargin + 2 * pen().widthF() + 1 ) );
00109 }
00110 
00111 QFont QgsComposerLabel::font() const
00112 {
00113   return mFont;
00114 }
00115 
00116 bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const
00117 {
00118   QString alignment;
00119 
00120   if ( elem.isNull() )
00121   {
00122     return false;
00123   }
00124 
00125   QDomElement composerLabelElem = doc.createElement( "ComposerLabel" );
00126 
00127   composerLabelElem.setAttribute( "labelText", mText );
00128   composerLabelElem.setAttribute( "margin", QString::number( mMargin ) );
00129 
00130   composerLabelElem.setAttribute( "halign", mHAlignment );
00131   composerLabelElem.setAttribute( "valign", mVAlignment );
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   //font
00173   QDomNodeList labelFontList = itemElem.elementsByTagName( "LabelFont" );
00174   if ( labelFontList.size() > 0 )
00175   {
00176     QDomElement labelFontElem = labelFontList.at( 0 ).toElement();
00177     mFont.fromString( labelFontElem.attribute( "description" ) );
00178   }
00179 
00180   //font color
00181   QDomNodeList fontColorList = itemElem.elementsByTagName( "FontColor" );
00182   if ( fontColorList.size() > 0 )
00183   {
00184     QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
00185     int red = fontColorElem.attribute( "red", "0" ).toInt();
00186     int green = fontColorElem.attribute( "green", "0" ).toInt();
00187     int blue = fontColorElem.attribute( "blue", "0" ).toInt();
00188     mFontColor = QColor( red, green, blue );
00189   }
00190   else
00191   {
00192     mFontColor = QColor( 0, 0, 0 );
00193   }
00194 
00195   //restore general composer item properties
00196   QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
00197   if ( composerItemList.size() > 0 )
00198   {
00199     QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
00200     _readXML( composerItemElem, doc );
00201   }
00202   emit itemChanged();
00203   return true;
00204 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines