Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
qgscomposerlabel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposerlabel.cpp
3  -------------------
4  begin : January 2005
5  copyright : (C) 2005 by Radim Blazek
6  email : blazek@itc.it
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgscomposerlabel.h"
19 #include <QDate>
20 #include <QDomElement>
21 #include <QPainter>
22 
23 QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ): QgsComposerItem( composition ), mMargin( 1.0 ), mFontColor( QColor( 0, 0, 0 ) ), \
24  mHAlignment( Qt::AlignLeft ), mVAlignment( Qt::AlignTop )
25 {
26  //default font size is 10 point
27  mFont.setPointSizeF( 10 );
28 }
29 
31 {
32 }
33 
34 void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
35 {
36  if ( !painter )
37  {
38  return;
39  }
40 
41  drawBackground( painter );
42  painter->setPen( QPen( QColor( mFontColor ) ) ); //draw all text black
43  painter->setFont( mFont );
44 
45  QFontMetricsF fontSize( mFont );
46 
47  //support multiline labels
48  double penWidth = pen().widthF();
49  QRectF painterRect( penWidth + mMargin, penWidth + mMargin, rect().width() - 2 * penWidth - 2 * mMargin,
50  rect().height() - 2 * penWidth - 2 * mMargin );
51 
52 
53  drawText( painter, painterRect, displayText(), mFont, mHAlignment, mVAlignment );
54 
55  drawFrame( painter );
56  if ( isSelected() )
57  {
58  drawSelectionBoxes( painter );
59  }
60 }
61 
62 void QgsComposerLabel::setText( const QString& text )
63 {
64  mText = text;
65 }
66 
68 {
69  QString displayText = mText;
70  replaceDateText( displayText );
71  return displayText;
72 }
73 
74 void QgsComposerLabel::replaceDateText( QString& text ) const
75 {
76  int currentDatePos = text.indexOf( "$CURRENT_DATE" );
77  if ( currentDatePos != -1 )
78  {
79  //check if there is a bracket just after $CURRENT_DATE
80  QString formatText;
81  int openingBracketPos = text.indexOf( "(", currentDatePos );
82  int closingBracketPos = text.indexOf( ")", openingBracketPos + 1 );
83  if ( openingBracketPos != -1 && closingBracketPos != -1 && ( closingBracketPos - openingBracketPos ) > 1 )
84  {
85  formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
86  text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
87  }
88  else //no bracket
89  {
90  text.replace( "$CURRENT_DATE", QDate::currentDate().toString() );
91  }
92  }
93 }
94 
95 void QgsComposerLabel::setFont( const QFont& f )
96 {
97  mFont = f;
98 }
99 
101 {
102  double textWidth = textWidthMillimeters( mFont, displayText() );
103  double fontAscent = fontAscentMillimeters( mFont );
104 
105  setSceneRect( QRectF( transform().dx(), transform().dy(), textWidth + 2 * mMargin + 2 * pen().widthF() + 1, \
106  fontAscent + 2 * mMargin + 2 * pen().widthF() + 1 ) );
107 }
108 
110 {
111  return mFont;
112 }
113 
114 bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const
115 {
116  QString alignment;
117 
118  if ( elem.isNull() )
119  {
120  return false;
121  }
122 
123  QDomElement composerLabelElem = doc.createElement( "ComposerLabel" );
124 
125  composerLabelElem.setAttribute( "labelText", mText );
126  composerLabelElem.setAttribute( "margin", QString::number( mMargin ) );
127 
128  composerLabelElem.setAttribute( "halign", mHAlignment );
129  composerLabelElem.setAttribute( "valign", mVAlignment );
130  composerLabelElem.setAttribute( "id", mId );
131 
132 
133  //font
134  QDomElement labelFontElem = doc.createElement( "LabelFont" );
135  labelFontElem.setAttribute( "description", mFont.toString() );
136  composerLabelElem.appendChild( labelFontElem );
137 
138  //font color
139  QDomElement fontColorElem = doc.createElement( "FontColor" );
140  fontColorElem.setAttribute( "red", mFontColor.red() );
141  fontColorElem.setAttribute( "green", mFontColor.green() );
142  fontColorElem.setAttribute( "blue", mFontColor.blue() );
143  composerLabelElem.appendChild( fontColorElem );
144 
145  elem.appendChild( composerLabelElem );
146  return _writeXML( composerLabelElem, doc );
147 }
148 
149 bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument& doc )
150 {
151  QString alignment;
152 
153  if ( itemElem.isNull() )
154  {
155  return false;
156  }
157 
158  //restore label specific properties
159 
160  //text
161  mText = itemElem.attribute( "labelText" );
162 
163  //margin
164  mMargin = itemElem.attribute( "margin" ).toDouble();
165 
166  //Horizontal alignment
167  mHAlignment = ( Qt::AlignmentFlag )( itemElem.attribute( "halign" ).toInt() );
168 
169  //Vertical alignment
170  mVAlignment = ( Qt::AlignmentFlag )( itemElem.attribute( "valign" ).toInt() );
171 
172  //id
173  mId = itemElem.attribute( "id", "" );
174 
175  //font
176  QDomNodeList labelFontList = itemElem.elementsByTagName( "LabelFont" );
177  if ( labelFontList.size() > 0 )
178  {
179  QDomElement labelFontElem = labelFontList.at( 0 ).toElement();
180  mFont.fromString( labelFontElem.attribute( "description" ) );
181  }
182 
183  //font color
184  QDomNodeList fontColorList = itemElem.elementsByTagName( "FontColor" );
185  if ( fontColorList.size() > 0 )
186  {
187  QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
188  int red = fontColorElem.attribute( "red", "0" ).toInt();
189  int green = fontColorElem.attribute( "green", "0" ).toInt();
190  int blue = fontColorElem.attribute( "blue", "0" ).toInt();
191  mFontColor = QColor( red, green, blue );
192  }
193  else
194  {
195  mFontColor = QColor( 0, 0, 0 );
196  }
197 
198  //restore general composer item properties
199  QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
200  if ( composerItemList.size() > 0 )
201  {
202  QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
203  _readXML( composerItemElem, doc );
204  }
205  emit itemChanged();
206  return true;
207 }