source: ntrip/trunk/GnssCenter/qwt/qwt_text_label.cpp@ 9045

Last change on this file since 9045 was 4839, checked in by mervart, 12 years ago
File size: 6.5 KB
Line 
1/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2 * Qwt Widget Library
3 * Copyright (C) 1997 Josef Wilgen
4 * Copyright (C) 2002 Uwe Rathmann
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the Qwt License, Version 1.0
8 *****************************************************************************/
9
10#include "qwt_text_label.h"
11#include "qwt_text.h"
12#include "qwt_painter.h"
13#include <qpainter.h>
14#include <qevent.h>
15#include <qmath.h>
16
17class QwtTextLabel::PrivateData
18{
19public:
20 PrivateData():
21 indent( 4 ),
22 margin( 0 )
23 {
24 }
25
26 int indent;
27 int margin;
28 QwtText text;
29};
30
31/*!
32 Constructs an empty label.
33 \param parent Parent widget
34*/
35QwtTextLabel::QwtTextLabel( QWidget *parent ):
36 QFrame( parent )
37{
38 init();
39}
40
41/*!
42 Constructs a label that displays the text, text
43 \param parent Parent widget
44 \param text Text
45*/
46QwtTextLabel::QwtTextLabel( const QwtText &text, QWidget *parent ):
47 QFrame( parent )
48{
49 init();
50 d_data->text = text;
51}
52
53//! Destructor
54QwtTextLabel::~QwtTextLabel()
55{
56 delete d_data;
57}
58
59void QwtTextLabel::init()
60{
61 d_data = new PrivateData();
62 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
63}
64
65/*!
66 Change the label's text, keeping all other QwtText attributes
67 \param text New text
68 \param textFormat Format of text
69
70 \sa QwtText
71*/
72void QwtTextLabel::setText( const QString &text, QwtText::TextFormat textFormat )
73{
74 d_data->text.setText( text, textFormat );
75
76 update();
77 updateGeometry();
78}
79
80/*!
81 Change the label's text
82 \param text New text
83*/
84void QwtTextLabel::setText( const QwtText &text )
85{
86 d_data->text = text;
87
88 update();
89 updateGeometry();
90}
91
92//! Return the text
93const QwtText &QwtTextLabel::text() const
94{
95 return d_data->text;
96}
97
98//! Clear the text and all QwtText attributes
99void QwtTextLabel::clear()
100{
101 d_data->text = QwtText();
102
103 update();
104 updateGeometry();
105}
106
107//! Return label's text indent in pixels
108int QwtTextLabel::indent() const
109{
110 return d_data->indent;
111}
112
113/*!
114 Set label's text indent in pixels
115 \param indent Indentation in pixels
116*/
117void QwtTextLabel::setIndent( int indent )
118{
119 if ( indent < 0 )
120 indent = 0;
121
122 d_data->indent = indent;
123
124 update();
125 updateGeometry();
126}
127
128//! Return label's text indent in pixels
129int QwtTextLabel::margin() const
130{
131 return d_data->margin;
132}
133
134/*!
135 Set label's margin in pixels
136 \param margin Margin in pixels
137*/
138void QwtTextLabel::setMargin( int margin )
139{
140 d_data->margin = margin;
141
142 update();
143 updateGeometry();
144}
145
146//! Return label's margin in pixels
147QSize QwtTextLabel::sizeHint() const
148{
149 return minimumSizeHint();
150}
151
152//! Return a minimum size hint
153QSize QwtTextLabel::minimumSizeHint() const
154{
155 QSizeF sz = d_data->text.textSize( font() );
156
157 int mw = 2 * ( frameWidth() + d_data->margin );
158 int mh = mw;
159
160 int indent = d_data->indent;
161 if ( indent <= 0 )
162 indent = defaultIndent();
163
164 if ( indent > 0 )
165 {
166 const int align = d_data->text.renderFlags();
167 if ( align & Qt::AlignLeft || align & Qt::AlignRight )
168 mw += d_data->indent;
169 else if ( align & Qt::AlignTop || align & Qt::AlignBottom )
170 mh += d_data->indent;
171 }
172
173 sz += QSizeF( mw, mh );
174
175 return QSize( qCeil( sz.width() ), qCeil( sz.height() ) );
176}
177
178/*!
179 Returns the preferred height for this widget, given the width.
180 \param width Width
181*/
182int QwtTextLabel::heightForWidth( int width ) const
183{
184 const int renderFlags = d_data->text.renderFlags();
185
186 int indent = d_data->indent;
187 if ( indent <= 0 )
188 indent = defaultIndent();
189
190 width -= 2 * frameWidth();
191 if ( renderFlags & Qt::AlignLeft || renderFlags & Qt::AlignRight )
192 width -= indent;
193
194 int height = d_data->text.heightForWidth( width, font() );
195 if ( renderFlags & Qt::AlignTop || renderFlags & Qt::AlignBottom )
196 height += indent;
197
198 height += 2 * frameWidth();
199
200 return height;
201}
202
203/*!
204 Qt paint event
205 \param event Paint event
206*/
207void QwtTextLabel::paintEvent( QPaintEvent *event )
208{
209 QPainter painter( this );
210
211 if ( !contentsRect().contains( event->rect() ) )
212 {
213 painter.save();
214 painter.setClipRegion( event->region() & frameRect() );
215 drawFrame( &painter );
216 painter.restore();
217 }
218
219 painter.setClipRegion( event->region() & contentsRect() );
220
221 drawContents( &painter );
222}
223
224//! Redraw the text and focus indicator
225void QwtTextLabel::drawContents( QPainter *painter )
226{
227 const QRect r = textRect();
228 if ( r.isEmpty() )
229 return;
230
231 painter->setFont( font() );
232 painter->setPen( palette().color( QPalette::Active, QPalette::Text ) );
233
234 drawText( painter, r );
235
236 if ( hasFocus() )
237 {
238 const int margin = 2;
239
240 QRect focusRect = contentsRect();
241 focusRect.setRect( focusRect.x() + margin, focusRect.y() + margin,
242 focusRect.width() - 2 * margin - 2,
243 focusRect.height() - 2 * margin - 2 );
244
245 QwtPainter::drawFocusRect( painter, this, focusRect );
246 }
247}
248
249//! Redraw the text
250void QwtTextLabel::drawText( QPainter *painter, const QRect &textRect )
251{
252 d_data->text.draw( painter, textRect );
253}
254
255/*!
256 Calculate the rect for the text in widget coordinates
257 \return Text rect
258*/
259QRect QwtTextLabel::textRect() const
260{
261 QRect r = contentsRect();
262
263 if ( !r.isEmpty() && d_data->margin > 0 )
264 {
265 r.setRect( r.x() + d_data->margin, r.y() + d_data->margin,
266 r.width() - 2 * d_data->margin, r.height() - 2 * d_data->margin );
267 }
268
269 if ( !r.isEmpty() )
270 {
271 int indent = d_data->indent;
272 if ( indent <= 0 )
273 indent = defaultIndent();
274
275 if ( indent > 0 )
276 {
277 const int renderFlags = d_data->text.renderFlags();
278
279 if ( renderFlags & Qt::AlignLeft )
280 r.setX( r.x() + indent );
281 else if ( renderFlags & Qt::AlignRight )
282 r.setWidth( r.width() - indent );
283 else if ( renderFlags & Qt::AlignTop )
284 r.setY( r.y() + indent );
285 else if ( renderFlags & Qt::AlignBottom )
286 r.setHeight( r.height() - indent );
287 }
288 }
289
290 return r;
291}
292
293int QwtTextLabel::defaultIndent() const
294{
295 if ( frameWidth() <= 0 )
296 return 0;
297
298 QFont fnt;
299 if ( d_data->text.testPaintAttribute( QwtText::PaintUsingTextFont ) )
300 fnt = d_data->text.font();
301 else
302 fnt = font();
303
304 return QFontMetrics( fnt ).width( 'x' ) / 2;
305}
306
Note: See TracBrowser for help on using the repository browser.