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
21class 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
40 : QFrame( parent )
41{
42 init();
43}
44
50QwtTextLabel::QwtTextLabel( const QwtText& text, QWidget* parent )
51 : QFrame( parent )
52{
53 init();
54 m_data->text = text;
55}
56
59{
60 delete m_data;
61}
62
63void QwtTextLabel::init()
64{
65 m_data = new PrivateData();
66 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
67}
68
73void QwtTextLabel::setPlainText( const QString& text )
74{
75 setText( QwtText( text ) );
76}
77
85{
86 return m_data->text.text();
87}
88
96void QwtTextLabel::setText( const QString& text,
97 QwtText::TextFormat textFormat )
98{
99 m_data->text.setText( text, textFormat );
100
101 update();
102 updateGeometry();
103}
104
110{
111 m_data->text = text;
112
113 update();
114 updateGeometry();
115}
116
119{
120 return m_data->text;
121}
122
125{
126 m_data->text = QwtText();
127
128 update();
129 updateGeometry();
130}
131
134{
135 return m_data->indent;
136}
137
142void 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
155{
156 return m_data->margin;
157}
158
163void QwtTextLabel::setMargin( int margin )
164{
165 m_data->margin = margin;
166
167 update();
168 updateGeometry();
169}
170
173{
174 return minimumSizeHint();
175}
176
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
209int 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
236void 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
257void 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
279void QwtTextLabel::drawText( QPainter* painter, const QRectF& textRect )
280{
281 m_data->text.draw( painter, textRect );
282}
283
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
330int QwtTextLabel::defaultIndent() const
331{
332 if ( frameWidth() <= 0 )
333 return 0;
334
335 QFont fnt;
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"
static void drawFocusRect(QPainter *, const QWidget *)
Draw a focus rectangle on a widget using its style.
static int horizontalAdvance(const QFontMetrics &, const QString &)
A class representing a text.
Definition qwt_text.h:52
bool testPaintAttribute(PaintAttribute) const
Definition qwt_text.cpp:482
QSizeF textSize() const
Definition qwt_text.cpp:570
int renderFlags() const
Definition qwt_text.cpp:317
TextFormat
Text format.
Definition qwt_text.h:65
QFont font() const
Return the font.
Definition qwt_text.cpp:336
@ PaintUsingTextFont
The text has an individual font.
Definition qwt_text.h:117
void setText(const QString &, QwtText::TextFormat textFormat=AutoText)
Definition qwt_text.cpp:277
void draw(QPainter *painter, const QRectF &rect) const
Definition qwt_text.cpp:615
QString text() const
Definition qwt_text.cpp:289
double heightForWidth(double width) const
Definition qwt_text.cpp:522
void setPlainText(const QString &)
virtual int heightForWidth(int) const override
int indent() const
Return label's text indent in pixels.
int margin() const
Return label's text margin in pixels.
const QwtText & text() const
Return the text.
virtual QSize sizeHint() const override
Return a size hint.
void clear()
Clear the text and all QwtText attributes.
QString plainText() const
virtual QSize minimumSizeHint() const override
Return a minimum size hint.
void setMargin(int)
QwtTextLabel(QWidget *parent=NULL)
void setIndent(int)
virtual void drawContents(QPainter *)
Redraw the text and focus indicator.
void setText(const QString &, QwtText::TextFormat textFormat=QwtText::AutoText)
QRect textRect() const
virtual void paintEvent(QPaintEvent *) override
virtual ~QwtTextLabel()
Destructor.
virtual void drawText(QPainter *, const QRectF &)
Redraw the text.