source: ntrip/trunk/BNC/qwt/qwt_text_label.cpp@ 10083

Last change on this file since 10083 was 9383, checked in by stoecker, 4 years ago

update to qwt verion 6.1.1 to fix build with newer Qt5

File size: 6.7 KB
RevLine 
[4271]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/*!
[8127]66 Interface for the designer plugin - does the same as setText()
67 \sa plainText()
68 */
69void QwtTextLabel::setPlainText( const QString &text )
70{
71 setText( QwtText( text ) );
72}
73
74/*!
75 Interface for the designer plugin
76
77 \return Text as plain text
78 \sa setPlainText(), text()
79 */
80QString QwtTextLabel::plainText() const
81{
82 return d_data->text.text();
83}
84
85/*!
[4271]86 Change the label's text, keeping all other QwtText attributes
87 \param text New text
88 \param textFormat Format of text
89
90 \sa QwtText
91*/
[9383]92void QwtTextLabel::setText( const QString &text,
[8127]93 QwtText::TextFormat textFormat )
[4271]94{
95 d_data->text.setText( text, textFormat );
96
97 update();
98 updateGeometry();
99}
100
101/*!
102 Change the label's text
103 \param text New text
104*/
105void QwtTextLabel::setText( const QwtText &text )
106{
107 d_data->text = text;
108
109 update();
110 updateGeometry();
111}
112
113//! Return the text
114const QwtText &QwtTextLabel::text() const
115{
116 return d_data->text;
117}
118
119//! Clear the text and all QwtText attributes
120void QwtTextLabel::clear()
121{
122 d_data->text = QwtText();
123
124 update();
125 updateGeometry();
126}
127
128//! Return label's text indent in pixels
129int QwtTextLabel::indent() const
130{
131 return d_data->indent;
132}
133
134/*!
135 Set label's text indent in pixels
136 \param indent Indentation in pixels
137*/
138void QwtTextLabel::setIndent( int indent )
139{
140 if ( indent < 0 )
141 indent = 0;
142
143 d_data->indent = indent;
144
145 update();
146 updateGeometry();
147}
148
[8127]149//! Return label's text margin in pixels
[4271]150int QwtTextLabel::margin() const
151{
152 return d_data->margin;
153}
154
155/*!
156 Set label's margin in pixels
157 \param margin Margin in pixels
158*/
159void QwtTextLabel::setMargin( int margin )
160{
161 d_data->margin = margin;
162
163 update();
164 updateGeometry();
165}
166
[8127]167//! Return a size hint
[4271]168QSize QwtTextLabel::sizeHint() const
169{
170 return minimumSizeHint();
171}
172
173//! Return a minimum size hint
174QSize QwtTextLabel::minimumSizeHint() const
175{
176 QSizeF sz = d_data->text.textSize( font() );
177
178 int mw = 2 * ( frameWidth() + d_data->margin );
179 int mh = mw;
180
181 int indent = d_data->indent;
182 if ( indent <= 0 )
183 indent = defaultIndent();
184
185 if ( indent > 0 )
186 {
187 const int align = d_data->text.renderFlags();
188 if ( align & Qt::AlignLeft || align & Qt::AlignRight )
189 mw += d_data->indent;
190 else if ( align & Qt::AlignTop || align & Qt::AlignBottom )
191 mh += d_data->indent;
192 }
193
194 sz += QSizeF( mw, mh );
195
196 return QSize( qCeil( sz.width() ), qCeil( sz.height() ) );
197}
198
199/*!
200 \param width Width
[8127]201 \return Preferred height for this widget, given the width.
[4271]202*/
203int QwtTextLabel::heightForWidth( int width ) const
204{
205 const int renderFlags = d_data->text.renderFlags();
206
207 int indent = d_data->indent;
208 if ( indent <= 0 )
209 indent = defaultIndent();
210
211 width -= 2 * frameWidth();
212 if ( renderFlags & Qt::AlignLeft || renderFlags & Qt::AlignRight )
213 width -= indent;
214
[8127]215 int height = qCeil( d_data->text.heightForWidth( width, font() ) );
216 if ( ( renderFlags & Qt::AlignTop ) || ( renderFlags & Qt::AlignBottom ) )
[4271]217 height += indent;
218
219 height += 2 * frameWidth();
220
221 return height;
222}
223
224/*!
225 Qt paint event
226 \param event Paint event
227*/
228void QwtTextLabel::paintEvent( QPaintEvent *event )
229{
230 QPainter painter( this );
231
232 if ( !contentsRect().contains( event->rect() ) )
233 {
234 painter.save();
235 painter.setClipRegion( event->region() & frameRect() );
236 drawFrame( &painter );
237 painter.restore();
238 }
239
240 painter.setClipRegion( event->region() & contentsRect() );
241
242 drawContents( &painter );
243}
244
245//! Redraw the text and focus indicator
246void QwtTextLabel::drawContents( QPainter *painter )
247{
248 const QRect r = textRect();
249 if ( r.isEmpty() )
250 return;
251
252 painter->setFont( font() );
253 painter->setPen( palette().color( QPalette::Active, QPalette::Text ) );
254
[8127]255 drawText( painter, QRectF( r ) );
[4271]256
257 if ( hasFocus() )
258 {
[8127]259 const int m = 2;
[4271]260
[8127]261 QRect focusRect = contentsRect().adjusted( m, m, -m + 1, -m + 1);
[4271]262
263 QwtPainter::drawFocusRect( painter, this, focusRect );
264 }
265}
266
267//! Redraw the text
[8127]268void QwtTextLabel::drawText( QPainter *painter, const QRectF &textRect )
[4271]269{
270 d_data->text.draw( painter, textRect );
271}
272
273/*!
[8127]274 Calculate geometry for the text in widget coordinates
275 \return Geometry for the text
[4271]276*/
277QRect QwtTextLabel::textRect() const
278{
279 QRect r = contentsRect();
280
281 if ( !r.isEmpty() && d_data->margin > 0 )
282 {
283 r.setRect( r.x() + d_data->margin, r.y() + d_data->margin,
284 r.width() - 2 * d_data->margin, r.height() - 2 * d_data->margin );
285 }
286
287 if ( !r.isEmpty() )
288 {
289 int indent = d_data->indent;
290 if ( indent <= 0 )
291 indent = defaultIndent();
292
293 if ( indent > 0 )
294 {
295 const int renderFlags = d_data->text.renderFlags();
296
297 if ( renderFlags & Qt::AlignLeft )
298 r.setX( r.x() + indent );
299 else if ( renderFlags & Qt::AlignRight )
300 r.setWidth( r.width() - indent );
301 else if ( renderFlags & Qt::AlignTop )
302 r.setY( r.y() + indent );
303 else if ( renderFlags & Qt::AlignBottom )
304 r.setHeight( r.height() - indent );
305 }
306 }
307
308 return r;
309}
310
311int QwtTextLabel::defaultIndent() const
312{
313 if ( frameWidth() <= 0 )
314 return 0;
315
316 QFont fnt;
317 if ( d_data->text.testPaintAttribute( QwtText::PaintUsingTextFont ) )
318 fnt = d_data->text.font();
319 else
320 fnt = font();
321
322 return QFontMetrics( fnt ).width( 'x' ) / 2;
323}
324
Note: See TracBrowser for help on using the repository browser.