Qwt User's Guide 6.3.0

Loading...
Searching...
No Matches
qwt_text_label.cpp
1/******************************************************************************
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 "qwt_math.h"
14
15#include <qstyle.h>
16#include <qstyleoption.h>
17#include <qpainter.h>
18#include <qevent.h>
19#include <qmargins.h>
20
21 class QwtTextLabel::PrivateData
22{
23 public:
24 PrivateData()
25 : indent( 4 )
26 , margin( 0 )
27 {
28 }
29
30 int indent;
31 int margin;
32 QwtText text;
33};
34
39 QwtTextLabel::QwtTextLabel( QWidget* parent )
40 : QFrame( parent )
41{
42 init();
43}
44
50 QwtTextLabel::QwtTextLabel( const QwtText& text, QWidget* parent )
51 : QFrame( parent )
52{
53 init();
54 m_data->text = text;
55}
56
58 QwtTextLabel::~QwtTextLabel()
59{
60 delete m_data;
61}
62
63 void QwtTextLabel::init()
64{
65 m_data = new PrivateData();
66 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
67}
68
73 void QwtTextLabel::setPlainText( const QString& text )
74{
75 setText( QwtText( text ) );
76}
77
84QString QwtTextLabel::plainText() const
85{
86 return m_data->text.text();
87}
88
96 void QwtTextLabel::setText( const QString& text,
97 QwtText::TextFormat textFormat )
98{
99 m_data->text.setText( text, textFormat );
100
101 update();
102 updateGeometry();
103}
104
109 void QwtTextLabel::setText( const QwtText& text )
110{
111 m_data->text = text;
112
113 update();
114 updateGeometry();
115}
116
118 const QwtText& QwtTextLabel::text() const
119{
120 return m_data->text;
121}
122
124 void QwtTextLabel::clear()
125{
126 m_data->text = QwtText();
127
128 update();
129 updateGeometry();
130}
131
133 int QwtTextLabel::indent() const
134{
135 return m_data->indent;
136}
137
142 void QwtTextLabel::setIndent( int indent )
143{
144 if ( indent < 0 )
145 indent = 0;
146
147 m_data->indent = indent;
148
149 update();
150 updateGeometry();
151}
152
154 int QwtTextLabel::margin() const
155{
156 return m_data->margin;
157}
158
163 void QwtTextLabel::setMargin( int margin )
164{
165 m_data->margin = margin;
166
167 update();
168 updateGeometry();
169}
170
172QSize QwtTextLabel::sizeHint() const
173{
174 return minimumSizeHint();
175}
176
178QSize QwtTextLabel::minimumSizeHint() const
179{
180 QSizeF sz = m_data->text.textSize( font() );
181
182 const QMargins m = contentsMargins();
183
184 int mw = m.left() + m.right() + 2 * m_data->margin;
185 int mh = m.top() + m.bottom() + 2 * m_data->margin;
186
187 int indent = m_data->indent;
188 if ( indent <= 0 )
189 indent = defaultIndent();
190
191 if ( indent > 0 )
192 {
193 const int align = m_data->text.renderFlags();
194 if ( align & Qt::AlignLeft || align & Qt::AlignRight )
195 mw += m_data->indent;
196 else if ( align & Qt::AlignTop || align & Qt::AlignBottom )
197 mh += m_data->indent;
198 }
199
200 sz += QSizeF( mw, mh );
201
202 return QSize( qwtCeil( sz.width() ), qwtCeil( sz.height() ) );
203}
204
209 int QwtTextLabel::heightForWidth( int width ) const
210{
211 const int renderFlags = m_data->text.renderFlags();
212
213 int indent = m_data->indent;
214 if ( indent <= 0 )
215 indent = defaultIndent();
216
217 const QMargins m = contentsMargins();
218
219 width -= m.left() + m.right() - 2 * m_data->margin;
220 if ( renderFlags & Qt::AlignLeft || renderFlags & Qt::AlignRight )
221 width -= indent;
222
223 int height = qwtCeil( m_data->text.heightForWidth( width, font() ) );
224 if ( ( renderFlags & Qt::AlignTop ) || ( renderFlags & Qt::AlignBottom ) )
225 height += indent;
226
227 height += m.top() + m.bottom() + 2 * m_data->margin;
228
229 return height;
230}
231
236 void QwtTextLabel::paintEvent( QPaintEvent* event )
237{
238 QPainter painter( this );
239 painter.setClipRegion( event->region() );
240
241 QStyleOption opt;
242 opt.initFrom(this);
243 style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
244
245 if ( !contentsRect().contains( event->rect() ) )
246 {
247 painter.setClipRegion( event->region() & frameRect() );
248 drawFrame( &painter );
249 }
250
251 painter.setClipRegion( event->region() & contentsRect() );
252
253 drawContents( &painter );
254}
255
257 void QwtTextLabel::drawContents( QPainter* painter )
258{
259 const QRect r = textRect();
260 if ( r.isEmpty() )
261 return;
262
263 painter->setFont( font() );
264 painter->setPen( palette().color( QPalette::Active, QPalette::Text ) );
265
266 drawText( painter, QRectF( r ) );
267
268 if ( hasFocus() )
269 {
270 const int m = 2;
271
272 QRect focusRect = contentsRect().adjusted( m, m, -m + 1, -m + 1);
273
274 QwtPainter::drawFocusRect( painter, this, focusRect );
275 }
276}
277
279 void QwtTextLabel::drawText( QPainter* painter, const QRectF& textRect )
280{
281 m_data->text.draw( painter, textRect );
282}
283
288QRect QwtTextLabel::textRect() const
289{
290 QRect r = contentsRect();
291
292 if ( !r.isEmpty() && m_data->margin > 0 )
293 {
294 const int m = m_data->margin;
295 r.adjust( m, m, -m, -m );
296 }
297
298 if ( !r.isEmpty() )
299 {
300 int indent = m_data->indent;
301 if ( indent <= 0 )
302 indent = defaultIndent();
303
304 if ( indent > 0 )
305 {
306 const int renderFlags = m_data->text.renderFlags();
307
308 if ( renderFlags & Qt::AlignLeft )
309 {
310 r.setX( r.x() + indent );
311 }
312 else if ( renderFlags & Qt::AlignRight )
313 {
314 r.setWidth( r.width() - indent );
315 }
316 else if ( renderFlags & Qt::AlignTop )
317 {
318 r.setY( r.y() + indent );
319 }
320 else if ( renderFlags & Qt::AlignBottom )
321 {
322 r.setHeight( r.height() - indent );
323 }
324 }
325 }
326
327 return r;
328}
329
330 int QwtTextLabel::defaultIndent() const
331{
332 if ( frameWidth() <= 0 )
333 return 0;
334
335 QFont fnt;
336 if ( m_data->text.testPaintAttribute( QwtText::PaintUsingTextFont ) )
337 fnt = m_data->text.font();
338 else
339 fnt = font();
340
341 return QwtPainter::horizontalAdvance( QFontMetrics( fnt ), 'x' ) / 2;
342}
343
344#include "moc_qwt_text_label.cpp"
QwtPainter::drawFocusRect
static void drawFocusRect(QPainter *, const QWidget *)
Draw a focus rectangle on a widget using its style.
Definition qwt_painter.cpp:805
QwtPainter::horizontalAdvance
static int horizontalAdvance(const QFontMetrics &, const QString &)
Definition qwt_painter.cpp:1369
QwtText
A class representing a text.
Definition qwt_text.h:52
QwtText::testPaintAttribute
bool testPaintAttribute(PaintAttribute) const
Definition qwt_text.cpp:482
QwtText::textSize
QSizeF textSize() const
Definition qwt_text.cpp:570
QwtText::renderFlags
int renderFlags() const
Definition qwt_text.cpp:317
QwtText::TextFormat
TextFormat
Text format.
Definition qwt_text.h:65
QwtText::font
QFont font() const
Return the font.
Definition qwt_text.cpp:336
QwtText::PaintUsingTextFont
@ PaintUsingTextFont
The text has an individual font.
Definition qwt_text.h:117
QwtText::setText
void setText(const QString &, QwtText::TextFormat textFormat=AutoText)
Definition qwt_text.cpp:277
QwtText::draw
void draw(QPainter *painter, const QRectF &rect) const
Definition qwt_text.cpp:615
QwtText::text
QString text() const
Definition qwt_text.cpp:289
QwtText::heightForWidth
double heightForWidth(double width) const
Definition qwt_text.cpp:522
QwtTextLabel::setPlainText
void setPlainText(const QString &)
Definition qwt_text_label.cpp:73
QwtTextLabel::heightForWidth
virtual int heightForWidth(int) const override
Definition qwt_text_label.cpp:209
QwtTextLabel::indent
int indent() const
Return label's text indent in pixels.
Definition qwt_text_label.cpp:133
QwtTextLabel::margin
int margin() const
Return label's text margin in pixels.
Definition qwt_text_label.cpp:154
QwtTextLabel::text
const QwtText & text() const
Return the text.
Definition qwt_text_label.cpp:118
QwtTextLabel::sizeHint
virtual QSize sizeHint() const override
Return a size hint.
Definition qwt_text_label.cpp:172
QwtTextLabel::clear
void clear()
Clear the text and all QwtText attributes.
Definition qwt_text_label.cpp:124
QwtTextLabel::plainText
QString plainText() const
Definition qwt_text_label.cpp:84
QwtTextLabel::minimumSizeHint
virtual QSize minimumSizeHint() const override
Return a minimum size hint.
Definition qwt_text_label.cpp:178
QwtTextLabel::setMargin
void setMargin(int)
Definition qwt_text_label.cpp:163
QwtTextLabel::QwtTextLabel
QwtTextLabel(QWidget *parent=NULL)
Definition qwt_text_label.cpp:39
QwtTextLabel::setIndent
void setIndent(int)
Definition qwt_text_label.cpp:142
QwtTextLabel::drawContents
virtual void drawContents(QPainter *)
Redraw the text and focus indicator.
Definition qwt_text_label.cpp:257
QwtTextLabel::setText
void setText(const QString &, QwtText::TextFormat textFormat=QwtText::AutoText)
Definition qwt_text_label.cpp:96
QwtTextLabel::textRect
QRect textRect() const
Definition qwt_text_label.cpp:288
QwtTextLabel::paintEvent
virtual void paintEvent(QPaintEvent *) override
Definition qwt_text_label.cpp:236
QwtTextLabel::~QwtTextLabel
virtual ~QwtTextLabel()
Destructor.
Definition qwt_text_label.cpp:58
QwtTextLabel::drawText
virtual void drawText(QPainter *, const QRectF &)
Redraw the text.
Definition qwt_text_label.cpp:279