kchart

KDChartTextPiece.cpp
1/*
2 KDChart - a multi-platform charting engine
3 */
4
5/****************************************************************************
6 ** Copyright (C) 2001-2003 Klarälvdalens Datakonsult AB. All rights reserved.
7 **
8 ** This file is part of the KDChart library.
9 **
10 ** This file may be distributed and/or modified under the terms of the
11 ** GNU General Public License version 2 as published by the Free Software
12 ** Foundation and appearing in the file LICENSE.GPL included in the
13 ** packaging of this file.
14 **
15 ** Licensees holding valid commercial KDChart licenses may use this file in
16 ** accordance with the KDChart Commercial License Agreement provided with
17 ** the Software.
18 **
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 **
22 ** See http://www.klaralvdalens-datakonsult.se/?page=products for
23 ** information about KDChart Commercial License Agreements.
24 **
25 ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
26 ** licensing are not clear to you.
27 **
28 **********************************************************************/
29#include "KDChartTextPiece.h"
30
31#include <tqstylesheet.h>
32#include <tqsimplerichtext.h>
33#include <tqfontmetrics.h>
34#include <tqpainter.h>
35#include <tqapplication.h>
36#include <tqrect.h>
37
38KDChartTextPiece::KDChartTextPiece()
39 :TQObject(0)
40{
41 _isRichText = false;
42 _richText = 0;
43
44 _font = TQApplication::font();
45 // These three are needed for both
46 _metrics = new TQFontMetrics( _font );
47 _dirtyMetrics = true;
48 _text = TQString("");
49}
50
51
52KDChartTextPiece::KDChartTextPiece( const TQString& text, const TQFont& font )
53 :TQObject(0)
54{
55 if( TQStyleSheet::mightBeRichText( text ) ) {
56 _isRichText = true;
57 _richText = new TQSimpleRichText( text, font );
58 _richText->adjustSize();
59 } else {
60 _isRichText = false;
61 _richText = 0;
62 }
63
64 // These three are needed for both
65 _metrics = new TQFontMetrics( font );
66 _dirtyMetrics = true;
67 _text = text;
68 _font = font;
69}
70
71
72KDChartTextPiece::KDChartTextPiece( TQPainter *p, const TQString& text, const TQFont& font )
73 :TQObject(0)
74{
75
76 if( TQStyleSheet::mightBeRichText( text ) ) {
77 _isRichText = true;
78 _richText = new TQSimpleRichText( text, font );
79 //tqDebug( "richtext width %s", TQString::number(_richText->width()).latin1());
80 //tqDebug( "richtext height %s", TQString::number(_richText->height()).latin1());
81 _richText->adjustSize();
82 //tqDebug( "richtext width %s", TQString::number(_richText->width()).latin1());
83 //tqDebug( "richtext height %s", TQString::number(_richText->height()).latin1());
84
85 } else {
86 _isRichText = false;
87 _richText = 0;
88 }
89
90 // These three are needed for both
91 _dirtyMetrics = (p == 0);
92 if( _dirtyMetrics ) {
93 _metrics = new TQFontMetrics( font );
94 //tqDebug("dirty metrics text: %s", text.latin1());
95 }
96 else{
97 p->save();
98 p->setFont( font );
99 _metrics = new TQFontMetrics( p->fontMetrics() );
100 //tqDebug ( "drawing metrics text: %s", text.latin1() );
101 //p->drawRect( _metrics->boundingRect( text) );
102 //p->drawText( _metrics->boundingRect(text).bottomRight(), text);
103 p->restore();
104 }
105 _text = text;
106 _font = font;
107}
108
109
110void KDChartTextPiece::deepCopy( const KDChartTextPiece* source )
111{
112 if( !source || this == source )
113 return;
114 if( _richText )
115 delete _richText;
116 _isRichText = source->_isRichText;
117 if( source->_richText ) {
118 _richText = new TQSimpleRichText( source->_text, source->_font );
119 _richText->adjustSize();
120 }
121 else
122 _richText = 0;
123
124 // used for both
125 if( _metrics )
126 delete _metrics;
127 _metrics = new TQFontMetrics( *source->_metrics );
128 _dirtyMetrics = source->_dirtyMetrics;
129 _text = source->_text;
130 _font = source->_font;
131}
132
133const KDChartTextPiece* KDChartTextPiece::clone() const
134{
135 KDChartTextPiece* newPiece = new KDChartTextPiece();
136 newPiece->deepCopy( this );
137 return newPiece;
138}
139
140
141KDChartTextPiece::~KDChartTextPiece()
142{
143 if( _richText )
144 delete _richText;
145 if( _metrics )
146 delete _metrics;
147}
148
149
150int KDChartTextPiece::width() const
151{
152 if( _isRichText )
153 return _richText->widthUsed();
154 else
155 return _metrics->width( _text );
156}
157
158
159int KDChartTextPiece::height() const
160{
161
162 if( _isRichText ) {
163 //tqDebug ("_richText height %s", TQString::number(_richText->height()).latin1());
164 return _richText->height();
165 }
166 else {
167
168 //tqDebug ("_metrics height %s", TQString::number(_metrics->height()).latin1());
169 return _metrics->height();
170 }
171}
172
173
174int KDChartTextPiece::fontLeading() const
175{
176 return _metrics->leading();
177}
178
179TQRect KDChartTextPiece::rect( TQPainter *p, const TQRect& clipRect) const
180{
181 TQRect rect( clipRect );
182 TQFont font( _font );
183
184 if( _isRichText ) {
185
186 // Pending Michel make sure the fonts are not too large
187 if ( _richText->height() > clipRect.height() || _richText->width() > clipRect.width() )
188 font.setPixelSize( TQMIN( (int)clipRect.width(),(int)clipRect.height() ) );
189
190 _richText->setDefaultFont( font );
191 _richText->setWidth( p, clipRect.width() );
192 rect.setWidth( _richText->width() );
193 rect.setHeight( _richText->height() );
194 } else
195 rect = clipRect;
196
197 return rect;
198}
199
200void KDChartTextPiece::draw( TQPainter *p, int x, int y,
201 const TQRect& clipRect,
202 const TQColor& color,
203 const TQBrush* paper ) const
204{
205
206 if( _isRichText ) {
207
208 TQColorGroup cg;
209 //calculate the text area before drawing
210 TQRect txtArea = rect( p,clipRect);
211 cg.setColor( TQColorGroup::Text, color );
212 _richText->draw( p, txtArea.x(), txtArea.y(),txtArea , cg, paper );
213 } else {
214 p->save();
215 p->setFont( _font );
216 if( paper )
217 p->setBrush( *paper );
218 p->setPen( color );
219 //dont clip to avoid truncated text
220 //p->setClipRect( txtArea );
221 if( _dirtyMetrics ){
222 if( _metrics )
223 delete _metrics;
224 KDChartTextPiece* meNotConst = const_cast<KDChartTextPiece*>(this);
225 //KDChartTextPiece* meNotConst(const_cast<KDChartTextPiece*>(this));
226 meNotConst->_metrics = new TQFontMetrics( p->fontMetrics() );
227 meNotConst->_dirtyMetrics = false;
228 }
229
230 p->drawText( x, y + _metrics->ascent(), _text );
231 p->restore();
232 }
233}
234
235
236void KDChartTextPiece::draw( TQPainter *p, int x, int y,
237 const TQRegion& clipRegion,
238 const TQColor& color,
239 const TQBrush* paper ) const
240{
241 if( _isRichText ) {
242 TQColorGroup cg;
243 cg.setColor( TQColorGroup::Text, color );
244 _richText->setDefaultFont( _font );
245 _richText->setWidth( p, clipRegion.boundingRect().width() );
246 _richText->draw( p, x, y, clipRegion, cg, paper );
247 } else {
248 p->save();
249 p->setFont( _font );
250 if( paper )
251 p->setBrush( *paper );
252 p->setPen( color );
253 p->setClipRegion( clipRegion );
254
255 if( _dirtyMetrics ){
256 if( _metrics )
257 delete _metrics;
258
259 // this line does not compile with MSVC++:
260 // KDChartTextPiece* meNotConst( const_cast<KDChartTextPiece*>(this) );
261 KDChartTextPiece* meNotConst = const_cast<KDChartTextPiece*>(this);
262
263 meNotConst->_metrics = new TQFontMetrics( p->fontMetrics() );
264 meNotConst->_dirtyMetrics = false;
265 }
266
267 p->drawText( x, y + _metrics->ascent(), _text );
268 p->restore();
269 }
270}
271
272
273TQString KDChartTextPiece::text() const
274{
275 return _text;
276}
277
278
279TQFont KDChartTextPiece::font() const
280{
281 return _font;
282}
283
284
285bool KDChartTextPiece::isRichText() const
286{
287 return _isRichText;
288}
289
290
291
292#include "KDChartTextPiece.moc"