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 <qstyle.h>
14 #include <qstyleoption.h>
15 #include <qpainter.h>
16 #include <qevent.h>
17 #include <qmath.h>
18 
20 {
21 public:
23  indent( 4 ),
24  margin( 0 )
25  {
26  }
27 
28  int indent;
29  int margin;
31 };
32 
37 QwtTextLabel::QwtTextLabel( QWidget *parent ):
38  QFrame( parent )
39 {
40  init();
41 }
42 
48 QwtTextLabel::QwtTextLabel( const QwtText &text, QWidget *parent ):
49  QFrame( parent )
50 {
51  init();
52  d_data->text = text;
53 }
54 
57 {
58  delete d_data;
59 }
60 
62 {
63  d_data = new PrivateData();
64  setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
65 }
66 
71 void QwtTextLabel::setPlainText( const QString &text )
72 {
73  setText( QwtText( text ) );
74 }
75 
82 QString QwtTextLabel::plainText() const
83 {
84  return d_data->text.text();
85 }
86 
94 void QwtTextLabel::setText( const QString &text,
95  QwtText::TextFormat textFormat )
96 {
97  d_data->text.setText( text, textFormat );
98 
99  update();
100  updateGeometry();
101 }
102 
108 {
109  d_data->text = text;
110 
111  update();
112  updateGeometry();
113 }
114 
117 {
118  return d_data->text;
119 }
120 
123 {
124  d_data->text = QwtText();
125 
126  update();
127  updateGeometry();
128 }
129 
131 int QwtTextLabel::indent() const
132 {
133  return d_data->indent;
134 }
135 
141 {
142  if ( indent < 0 )
143  indent = 0;
144 
145  d_data->indent = indent;
146 
147  update();
148  updateGeometry();
149 }
150 
152 int QwtTextLabel::margin() const
153 {
154  return d_data->margin;
155 }
156 
162 {
163  d_data->margin = margin;
164 
165  update();
166  updateGeometry();
167 }
168 
171 {
172  return minimumSizeHint();
173 }
174 
177 {
178  QSizeF sz = d_data->text.textSize( font() );
179 
180  int left, right, top, bottom;
181  getContentsMargins( &left, &top, &right, &bottom );
182 
183  int mw = left + right + 2 * d_data->margin;
184  int mh = top + bottom + 2 * d_data->margin;
185 
186  int indent = d_data->indent;
187  if ( indent <= 0 )
188  indent = defaultIndent();
189 
190  if ( indent > 0 )
191  {
192  const int align = d_data->text.renderFlags();
193  if ( align & Qt::AlignLeft || align & Qt::AlignRight )
194  mw += d_data->indent;
195  else if ( align & Qt::AlignTop || align & Qt::AlignBottom )
196  mh += d_data->indent;
197  }
198 
199  sz += QSizeF( mw, mh );
200 
201  return QSize( qCeil( sz.width() ), qCeil( sz.height() ) );
202 }
203 
208 int QwtTextLabel::heightForWidth( int width ) const
209 {
210  const int renderFlags = d_data->text.renderFlags();
211 
212  int indent = d_data->indent;
213  if ( indent <= 0 )
214  indent = defaultIndent();
215 
216  int left, right, top, bottom;
217  getContentsMargins( &left, &top, &right, &bottom );
218 
219  width -= left + right - 2 * d_data->margin;
220  if ( renderFlags & Qt::AlignLeft || renderFlags & Qt::AlignRight )
221  width -= indent;
222 
223  int height = qCeil( d_data->text.heightForWidth( width, font() ) );
224  if ( ( renderFlags & Qt::AlignTop ) || ( renderFlags & Qt::AlignBottom ) )
225  height += indent;
226 
227  height += top + 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.init(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 QFontMetrics( fnt ).width( 'x' ) / 2;
342 }
343 
virtual QSize sizeHint() const
Return a size hint.
virtual int heightForWidth(int) const
static void drawFocusRect(QPainter *, const QWidget *)
Draw a focus rectangle on a widget using its style.
int renderFlags() const
Definition: qwt_text.cpp:284
void setText(const QString &, QwtText::TextFormat textFormat=AutoText)
Definition: qwt_text.cpp:244
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:560
const QwtText & text() const
Return the text.
int defaultIndent() const
QwtTextLabel(QWidget *parent=NULL)
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
bool testPaintAttribute(PaintAttribute) const
Definition: qwt_text.cpp:449
CONSTEXPR_F fields align(second_tag, fields f) noexcept
QSizeF textSize(const QFont &=QFont()) const
Definition: qwt_text.cpp:526
void setIndent(int)
virtual QSize minimumSizeHint() const
Return a minimum size hint.
A class representing a text.
Definition: qwt_text.h:51
QString text() const
Definition: qwt_text.cpp:256
virtual void drawContents(QPainter *)
Redraw the text and focus indicator.
PrivateData * d_data
int indent() const
virtual void paintEvent(QPaintEvent *e)
virtual void drawText(QPainter *, const QRectF &)
Redraw the text.
QRect textRect() const
QFont font() const
Return the font.
Definition: qwt_text.cpp:303
The text has an individual font.
Definition: qwt_text.h:112
int margin() const
QString plainText() const
double heightForWidth(double width, const QFont &=QFont()) const
Definition: qwt_text.cpp:490
void setPlainText(const QString &)
void setText(const QString &, QwtText::TextFormat textFormat=QwtText::AutoText)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:18