~ubuntu-branches/ubuntu/trusty/qgis/trusty

« back to all changes in this revision

Viewing changes to src/core/composer/qgscomposerlabel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
{
 
25
  //default font size is 10 point
 
26
  mFont.setPointSizeF( 10 );
 
27
}
 
28
 
 
29
QgsComposerLabel::~QgsComposerLabel()
 
30
{
 
31
}
 
32
 
 
33
void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
 
34
{
 
35
  if ( !painter )
 
36
  {
 
37
    return;
 
38
  }
 
39
 
 
40
  drawBackground( painter );
 
41
  painter->setPen( QPen( QColor( mFontColor ) ) ); //draw all text black
 
42
  painter->setFont( mFont );
 
43
 
 
44
  QFontMetricsF fontSize( mFont );
 
45
 
 
46
  //support multiline labels
 
47
  double penWidth = pen().widthF();
 
48
  QRectF painterRect( penWidth + mMargin, penWidth + mMargin, rect().width() - 2 * penWidth - 2 * mMargin,
 
49
                      rect().height() - 2 * penWidth - 2 * mMargin );
 
50
 
 
51
 
 
52
  drawText( painter, painterRect, displayText(), mFont );
 
53
 
 
54
  drawFrame( painter );
 
55
  if ( isSelected() )
 
56
  {
 
57
    drawSelectionBoxes( painter );
 
58
  }
 
59
}
 
60
 
 
61
void QgsComposerLabel::setText( const QString& text )
 
62
{
 
63
  mText = text;
 
64
}
 
65
 
 
66
QString QgsComposerLabel::displayText() const
 
67
{
 
68
  QString displayText = mText;
 
69
  replaceDateText( displayText );
 
70
  return displayText;
 
71
}
 
72
 
 
73
void QgsComposerLabel::replaceDateText( QString& text ) const
 
74
{
 
75
  int currentDatePos = text.indexOf( "$CURRENT_DATE" );
 
76
  if ( currentDatePos != -1 )
 
77
  {
 
78
    //check if there is a bracket just after $CURRENT_DATE
 
79
    QString formatText;
 
80
    int openingBracketPos = text.indexOf( "(", currentDatePos );
 
81
    int closingBracketPos = text.indexOf( ")", openingBracketPos + 1 );
 
82
    if ( openingBracketPos != -1 && closingBracketPos != -1 && ( closingBracketPos - openingBracketPos ) > 1 )
 
83
    {
 
84
      formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
 
85
      text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
 
86
    }
 
87
    else //no bracket
 
88
    {
 
89
      text.replace( "$CURRENT_DATE", QDate::currentDate().toString() );
 
90
    }
 
91
  }
 
92
}
 
93
 
 
94
void QgsComposerLabel::setFont( const QFont& f )
 
95
{
 
96
  mFont = f;
 
97
}
 
98
 
 
99
void QgsComposerLabel::adjustSizeToText()
 
100
{
 
101
  double textWidth = textWidthMillimeters( mFont, displayText() );
 
102
  double fontAscent = fontAscentMillimeters( mFont );
 
103
 
 
104
  setSceneRect( QRectF( transform().dx(), transform().dy(), textWidth + 2 * mMargin + 2 * pen().widthF() + 1, \
 
105
                        fontAscent + 2 * mMargin + 2 * pen().widthF() + 1 ) );
 
106
}
 
107
 
 
108
QFont QgsComposerLabel::font() const
 
109
{
 
110
  return mFont;
 
111
}
 
112
 
 
113
bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const
 
114
{
 
115
  if ( elem.isNull() )
 
116
  {
 
117
    return false;
 
118
  }
 
119
 
 
120
  QDomElement composerLabelElem = doc.createElement( "ComposerLabel" );
 
121
 
 
122
  composerLabelElem.setAttribute( "labelText", mText );
 
123
  composerLabelElem.setAttribute( "margin", QString::number( mMargin ) );
 
124
 
 
125
 
 
126
  //font
 
127
  QDomElement labelFontElem = doc.createElement( "LabelFont" );
 
128
  labelFontElem.setAttribute( "description", mFont.toString() );
 
129
  composerLabelElem.appendChild( labelFontElem );
 
130
 
 
131
  //font color
 
132
  QDomElement fontColorElem = doc.createElement( "FontColor" );
 
133
  fontColorElem.setAttribute( "red", mFontColor.red() );
 
134
  fontColorElem.setAttribute( "green", mFontColor.green() );
 
135
  fontColorElem.setAttribute( "blue", mFontColor.blue() );
 
136
  composerLabelElem.appendChild( fontColorElem );
 
137
 
 
138
  elem.appendChild( composerLabelElem );
 
139
  return _writeXML( composerLabelElem, doc );
 
140
}
 
141
 
 
142
bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument& doc )
 
143
{
 
144
  if ( itemElem.isNull() )
 
145
  {
 
146
    return false;
 
147
  }
 
148
 
 
149
  //restore label specific properties
 
150
 
 
151
  //text
 
152
  mText = itemElem.attribute( "labelText" );
 
153
 
 
154
  //margin
 
155
  mMargin = itemElem.attribute( "margin" ).toDouble();
 
156
 
 
157
  //font
 
158
  QDomNodeList labelFontList = itemElem.elementsByTagName( "LabelFont" );
 
159
  if ( labelFontList.size() > 0 )
 
160
  {
 
161
    QDomElement labelFontElem = labelFontList.at( 0 ).toElement();
 
162
    mFont.fromString( labelFontElem.attribute( "description" ) );
 
163
  }
 
164
 
 
165
  //font color
 
166
  QDomNodeList fontColorList = itemElem.elementsByTagName( "FontColor" );
 
167
  if ( fontColorList.size() > 0 )
 
168
  {
 
169
    QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
 
170
    int red = fontColorElem.attribute( "red", "0" ).toInt();
 
171
    int green = fontColorElem.attribute( "green", "0" ).toInt();
 
172
    int blue = fontColorElem.attribute( "blue", "0" ).toInt();
 
173
    mFontColor = QColor( red, green, blue );
 
174
  }
 
175
  else
 
176
  {
 
177
    mFontColor = QColor( 0, 0, 0 );
 
178
  }
 
179
 
 
180
  //restore general composer item properties
 
181
  QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
 
182
  if ( composerItemList.size() > 0 )
 
183
  {
 
184
    QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
 
185
    _readXML( composerItemElem, doc );
 
186
  }
 
187
  return true;
 
188
}