00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
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 ) ) );
00043 painter->setFont( mFont );
00044
00045 QFontMetricsF fontSize( mFont );
00046
00047
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
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
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
00131
00132
00133 QDomElement labelFontElem = doc.createElement( "LabelFont" );
00134 labelFontElem.setAttribute( "description", mFont.toString() );
00135 composerLabelElem.appendChild( labelFontElem );
00136
00137
00138 QDomElement fontColorElem = doc.createElement( "FontColor" );
00139 fontColorElem.setAttribute( "red", mFontColor.red() );
00140 fontColorElem.setAttribute( "green", mFontColor.green() );
00141 fontColorElem.setAttribute( "blue", mFontColor.blue() );
00142 composerLabelElem.appendChild( fontColorElem );
00143
00144 elem.appendChild( composerLabelElem );
00145 return _writeXML( composerLabelElem, doc );
00146 }
00147
00148 bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument& doc )
00149 {
00150 QString alignment;
00151
00152 if ( itemElem.isNull() )
00153 {
00154 return false;
00155 }
00156
00157
00158
00159
00160 mText = itemElem.attribute( "labelText" );
00161
00162
00163 mMargin = itemElem.attribute( "margin" ).toDouble();
00164
00165
00166 mHAlignment = ( Qt::AlignmentFlag )( itemElem.attribute( "halign" ).toInt() );
00167
00168
00169 mVAlignment = ( Qt::AlignmentFlag )( itemElem.attribute( "valign" ).toInt() );
00170
00171
00172 QDomNodeList labelFontList = itemElem.elementsByTagName( "LabelFont" );
00173 if ( labelFontList.size() > 0 )
00174 {
00175 QDomElement labelFontElem = labelFontList.at( 0 ).toElement();
00176 mFont.fromString( labelFontElem.attribute( "description" ) );
00177 }
00178
00179
00180 QDomNodeList fontColorList = itemElem.elementsByTagName( "FontColor" );
00181 if ( fontColorList.size() > 0 )
00182 {
00183 QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
00184 int red = fontColorElem.attribute( "red", "0" ).toInt();
00185 int green = fontColorElem.attribute( "green", "0" ).toInt();
00186 int blue = fontColorElem.attribute( "blue", "0" ).toInt();
00187 mFontColor = QColor( red, green, blue );
00188 }
00189 else
00190 {
00191 mFontColor = QColor( 0, 0, 0 );
00192 }
00193
00194
00195 QDomNodeList composerItemList = itemElem.elementsByTagName( "ComposerItem" );
00196 if ( composerItemList.size() > 0 )
00197 {
00198 QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
00199 _readXML( composerItemElem, doc );
00200 }
00201 return true;
00202 }