qwt_text_label.cpp
Go to the documentation of this file.
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 "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 
22 {
23 public:
25  indent( 4 ),
26  margin( 0 )
27  {
28  }
29 
30  int indent;
31  int margin;
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  d_data->text = text;
55 }
56 
59 {
60  delete d_data;
61 }
62 
64 {
65  d_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 
84 QString QwtTextLabel::plainText() const
85 {
86  return d_data->text.text();
87 }
88 
96 void QwtTextLabel::setText( const QString &text,
97  QwtText::TextFormat textFormat )
98 {
99  d_data->text.setText( text, textFormat );
100 
101  update();
102  updateGeometry();
103 }
104 
110 {
111  d_data->text = text;
112 
113  update();
114  updateGeometry();
115 }
116 
119 {
120  return d_data->text;
121 }
122 
125 {
126  d_data->text = QwtText();
127 
128  update();
129  updateGeometry();
130 }
131 
133 int QwtTextLabel::indent() const
134 {
135  return d_data->indent;
136 }
137 
143 {
144  if ( indent < 0 )
145  indent = 0;
146 
147  d_data->indent = indent;
148 
149  update();
150  updateGeometry();
151 }
152 
154 int QwtTextLabel::margin() const
155 {
156  return d_data->margin;
157 }
158 
164 {
165  d_data->margin = margin;
166 
167  update();
168  updateGeometry();
169 }
170 
173 {
174  return minimumSizeHint();
175 }
176 
179 {
180  QSizeF sz = d_data->text.textSize( font() );
181 
182  const QMargins m = contentsMargins();
183 
184  int mw = m.left() + m.right() + 2 * d_data->margin;
185  int mh = m.top() + m.bottom() + 2 * d_data->margin;
186 
187  int indent = d_data->indent;
188  if ( indent <= 0 )
189  indent = defaultIndent();
190 
191  if ( indent > 0 )
192  {
193  const int align = d_data->text.renderFlags();
194  if ( align & Qt::AlignLeft || align & Qt::AlignRight )
195  mw += d_data->indent;
196  else if ( align & Qt::AlignTop || align & Qt::AlignBottom )
197  mh += d_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 = d_data->text.renderFlags();
212 
213  int indent = d_data->indent;
214  if ( indent <= 0 )
215  indent = defaultIndent();
216 
217  const QMargins m = contentsMargins();
218 
219  width -= m.left() + m.right() - 2 * d_data->margin;
220  if ( renderFlags & Qt::AlignLeft || renderFlags & Qt::AlignRight )
221  width -= indent;
222 
223  int height = qwtCeil( d_data->text.heightForWidth( width, font() ) );
224  if ( ( renderFlags & Qt::AlignTop ) || ( renderFlags & Qt::AlignBottom ) )
225  height += indent;
226 
227  height += m.top() + m.bottom() + 2 * d_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  d_data->text.draw( painter, textRect );
282 }
283 
289 {
290  QRect r = contentsRect();
291 
292  if ( !r.isEmpty() && d_data->margin > 0 )
293  {
294  const int m = d_data->margin;
295  r.adjust( m, m, -m, -m );
296  }
297 
298  if ( !r.isEmpty() )
299  {
300  int indent = d_data->indent;
301  if ( indent <= 0 )
302  indent = defaultIndent();
303 
304  if ( indent > 0 )
305  {
306  const int renderFlags = d_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 
331 {
332  if ( frameWidth() <= 0 )
333  return 0;
334 
335  QFont fnt;
337  fnt = d_data->text.font();
338  else
339  fnt = font();
340 
341  return QwtPainter::horizontalAdvance( QFontMetrics( fnt ), 'x' ) / 2;
342 }
343 
344 #if QWT_MOC_INCLUDE
345 #include "moc_qwt_text_label.cpp"
346 #endif
Definition: format.h:1173
static void drawFocusRect(QPainter *, const QWidget *)
Draw a focus rectangle on a widget using its style.
QSizeF textSize() const
Definition: qwt_text.cpp:547
int renderFlags() const
Definition: qwt_text.cpp:294
void setText(const QString &, QwtText::TextFormat textFormat=AutoText)
Definition: qwt_text.cpp:254
TextFormat
Text format.
Definition: qwt_text.h:64
void clear()
Clear the text and all QwtText attributes.
void setMargin(int)
virtual ~QwtTextLabel()
Destructor.
void draw(QPainter *painter, const QRectF &rect) const
Definition: qwt_text.cpp:592
const QwtText & text() const
Return the text.
virtual int heightForWidth(int) const QWT_OVERRIDE
int defaultIndent() const
QwtTextLabel(QWidget *parent=NULL)
virtual QSize minimumSizeHint() const QWT_OVERRIDE
Return a minimum size hint.
bool testPaintAttribute(PaintAttribute) const
Definition: qwt_text.cpp:459
double heightForWidth(double width) const
Definition: qwt_text.cpp:499
void setIndent(int)
static int horizontalAdvance(const QFontMetrics &, const QString &)
A class representing a text.
Definition: qwt_text.h:51
QString text() const
Definition: qwt_text.cpp:266
virtual void drawContents(QPainter *)
Redraw the text and focus indicator.
PrivateData * d_data
int indent() const
virtual void drawText(QPainter *, const QRectF &)
Redraw the text.
QRect textRect() const
QFont font() const
Return the font.
Definition: qwt_text.cpp:313
The text has an individual font.
Definition: qwt_text.h:117
int margin() const
QString plainText() const
virtual QSize sizeHint() const QWT_OVERRIDE
Return a size hint.
int qwtCeil(qreal value)
Definition: qwt_math.h:262
virtual void paintEvent(QPaintEvent *) QWT_OVERRIDE
void setPlainText(const QString &)
void setText(const QString &, QwtText::TextFormat textFormat=QwtText::AutoText)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10