qwt_legend_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_legend_label.h"
11 #include "qwt_legend_data.h"
12 #include "qwt_graphic.h"
13 #include "qwt.h"
14 
15 #include <qpainter.h>
16 #include <qdrawutil.h>
17 #include <qstyle.h>
18 #include <qevent.h>
19 #include <qstyleoption.h>
20 
21 static const int ButtonFrame = 2;
22 static const int Margin = 2;
23 
24 static QSize buttonShift( const QwtLegendLabel *w )
25 {
26  QStyleOption option;
27  option.initFrom( w );
28 
29  const int ph = w->style()->pixelMetric(
30  QStyle::PM_ButtonShiftHorizontal, &option, w );
31  const int pv = w->style()->pixelMetric(
32  QStyle::PM_ButtonShiftVertical, &option, w );
33  return QSize( ph, pv );
34 }
35 
37 {
38 public:
40  itemMode( QwtLegendData::ReadOnly ),
41  isDown( false ),
42  spacing( Margin )
43  {
44  }
45 
48  bool isDown;
49 
50  QPixmap icon;
51 
52  int spacing;
53 };
54 
62 {
64 
65  const bool doUpdate = updatesEnabled();
66  if ( doUpdate )
67  setUpdatesEnabled( false );
68 
69  setText( legendData.title() );
70  setIcon( legendData.icon().toPixmap() );
71 
72  if ( legendData.hasRole( QwtLegendData::ModeRole ) )
73  setItemMode( legendData.mode() );
74 
75  if ( doUpdate )
76  setUpdatesEnabled( true );
77 }
78 
84 {
85  return d_data->legendData;
86 }
87 
91 QwtLegendLabel::QwtLegendLabel( QWidget *parent ):
92  QwtTextLabel( parent )
93 {
94  d_data = new PrivateData;
95  setMargin( Margin );
96  setIndent( Margin );
97 }
98 
101 {
102  delete d_data;
103  d_data = NULL;
104 }
105 
113 {
114  const int flags = Qt::AlignLeft | Qt::AlignVCenter
115  | Qt::TextExpandTabs | Qt::TextWordWrap;
116 
117  QwtText txt = text;
118  txt.setRenderFlags( flags );
119 
120  QwtTextLabel::setText( txt );
121 }
122 
131 {
132  if ( mode != d_data->itemMode )
133  {
134  d_data->itemMode = mode;
135  d_data->isDown = false;
136 
137  setFocusPolicy( ( mode != QwtLegendData::ReadOnly )
138  ? Qt::TabFocus : Qt::NoFocus );
140 
141  updateGeometry();
142  }
143 }
144 
150 {
151  return d_data->itemMode;
152 }
153 
161 void QwtLegendLabel::setIcon( const QPixmap &icon )
162 {
163  d_data->icon = icon;
164 
165  int indent = margin() + d_data->spacing;
166  if ( icon.width() > 0 )
167  indent += icon.width() + d_data->spacing;
168 
169  setIndent( indent );
170 }
171 
176 QPixmap QwtLegendLabel::icon() const
177 {
178  return d_data->icon;
179 }
180 
188 {
189  spacing = qMax( spacing, 0 );
190  if ( spacing != d_data->spacing )
191  {
193 
194  int indent = margin() + d_data->spacing;
195  if ( d_data->icon.width() > 0 )
196  indent += d_data->icon.width() + d_data->spacing;
197 
198  setIndent( indent );
199  }
200 }
201 
207 {
208  return d_data->spacing;
209 }
210 
218 {
220  {
221  const bool isBlocked = signalsBlocked();
222  blockSignals( true );
223 
224  setDown( on );
225 
226  blockSignals( isBlocked );
227  }
228 }
229 
232 {
234 }
235 
237 void QwtLegendLabel::setDown( bool down )
238 {
239  if ( down == d_data->isDown )
240  return;
241 
242  d_data->isDown = down;
243  update();
244 
246  {
247  if ( d_data->isDown )
248  Q_EMIT pressed();
249  else
250  {
251  Q_EMIT released();
252  Q_EMIT clicked();
253  }
254  }
255 
257  Q_EMIT checked( d_data->isDown );
258 }
259 
262 {
263  return d_data->isDown;
264 }
265 
268 {
269  QSize sz = QwtTextLabel::sizeHint();
270  sz.setHeight( qMax( sz.height(), d_data->icon.height() + 4 ) );
271 
273  {
274  sz += buttonShift( this );
275  sz = qwtExpandedToGlobalStrut( sz );
276  }
277 
278  return sz;
279 }
280 
282 void QwtLegendLabel::paintEvent( QPaintEvent *e )
283 {
284  const QRect cr = contentsRect();
285 
286  QPainter painter( this );
287  painter.setClipRegion( e->region() );
288 
289  if ( d_data->isDown )
290  {
291  qDrawWinButton( &painter, 0, 0, width(), height(),
292  palette(), true );
293  }
294 
295  painter.save();
296 
297  if ( d_data->isDown )
298  {
299  const QSize shiftSize = buttonShift( this );
300  painter.translate( shiftSize.width(), shiftSize.height() );
301  }
302 
303  painter.setClipRect( cr );
304 
305  drawContents( &painter );
306 
307  if ( !d_data->icon.isNull() )
308  {
309  QRect iconRect = cr;
310  iconRect.setX( iconRect.x() + margin() );
312  iconRect.setX( iconRect.x() + ButtonFrame );
313 
314  iconRect.setSize( d_data->icon.size() );
315  iconRect.moveCenter( QPoint( iconRect.center().x(), cr.center().y() ) );
316 
317  painter.drawPixmap( iconRect, d_data->icon );
318  }
319 
320  painter.restore();
321 }
322 
324 void QwtLegendLabel::mousePressEvent( QMouseEvent *e )
325 {
326  if ( e->button() == Qt::LeftButton )
327  {
328  switch ( d_data->itemMode )
329  {
331  {
332  setDown( true );
333  return;
334  }
336  {
337  setDown( !isDown() );
338  return;
339  }
340  default:;
341  }
342  }
343  QwtTextLabel::mousePressEvent( e );
344 }
345 
347 void QwtLegendLabel::mouseReleaseEvent( QMouseEvent *e )
348 {
349  if ( e->button() == Qt::LeftButton )
350  {
351  switch ( d_data->itemMode )
352  {
354  {
355  setDown( false );
356  return;
357  }
359  {
360  return; // do nothing, but accept
361  }
362  default:;
363  }
364  }
365  QwtTextLabel::mouseReleaseEvent( e );
366 }
367 
369 void QwtLegendLabel::keyPressEvent( QKeyEvent *e )
370 {
371  if ( e->key() == Qt::Key_Space )
372  {
373  switch ( d_data->itemMode )
374  {
376  {
377  if ( !e->isAutoRepeat() )
378  setDown( true );
379  return;
380  }
382  {
383  if ( !e->isAutoRepeat() )
384  setDown( !isDown() );
385  return;
386  }
387  default:;
388  }
389  }
390 
391  QwtTextLabel::keyPressEvent( e );
392 }
393 
396 {
397  if ( e->key() == Qt::Key_Space )
398  {
399  switch ( d_data->itemMode )
400  {
402  {
403  if ( !e->isAutoRepeat() )
404  setDown( false );
405  return;
406  }
408  {
409  return; // do nothing, but accept
410  }
411  default:;
412  }
413  }
414 
415  QwtTextLabel::keyReleaseEvent( e );
416 }
417 
418 #if QWT_MOC_INCLUDE
419 #include "moc_qwt_legend_label.cpp"
420 #endif
void pressed()
Signal, when the legend item has been pressed.
A widget representing something on a QwtLegend.
The legend item is checkable, like a checkable button.
virtual void paintEvent(QPaintEvent *) QWT_OVERRIDE
Paint event.
bool isChecked() const
Return true, if the item is checked.
A Widget which displays a QwtText.
void clicked()
Signal, when the legend item has been clicked.
void setRenderFlags(int)
Change the render flags.
Definition: qwt_text.cpp:281
QSize qwtExpandedToGlobalStrut(const QSize &size)
Definition: qwt.cpp:21
void setMargin(int)
const QwtLegendData & data() const
The legend item is not interactive, like a label.
The legend item is clickable, like a push button.
const QwtText & text() const
Return the text.
void setDown(bool)
Set the item being down.
void setSpacing(int spacing)
Change the spacing between icon and text.
void setChecked(bool on)
bool hasRole(int role) const
QwtLegendData::Mode itemMode
void setIndent(int)
virtual ~QwtLegendLabel()
Destructor.
virtual QSize sizeHint() const QWT_OVERRIDE
Return a size hint.
virtual void keyReleaseEvent(QKeyEvent *) QWT_OVERRIDE
Handle key release events.
virtual void keyPressEvent(QKeyEvent *) QWT_OVERRIDE
Handle key press events.
A class representing a text.
Definition: qwt_text.h:51
virtual void drawContents(QPainter *)
Redraw the text and focus indicator.
QwtGraphic icon() const
virtual void setText(const QwtText &) QWT_OVERRIDE
virtual void mousePressEvent(QMouseEvent *) QWT_OVERRIDE
Handle mouse press events.
int indent() const
static QSize buttonShift(const QwtLegendLabel *w)
QwtLegendLabel(QWidget *parent=0)
int spacing() const
static const int Margin
Mode mode() const
void checked(bool)
Signal, when the legend item has been toggled.
QPixmap toPixmap(qreal devicePixelRatio=0.0) const
Convert the graphic to a QPixmap.
QwtText title() const
QPixmap icon() const
void setIcon(const QPixmap &)
void released()
Signal, when the legend item has been released.
QwtLegendData::Mode itemMode() const
void setData(const QwtLegendData &)
int margin() const
PrivateData * d_data
Mode
Mode defining how a legend entry interacts.
bool isDown() const
Return true, if the item is down.
virtual QSize sizeHint() const QWT_OVERRIDE
Return a size hint.
static const int ButtonFrame
Attributes of an entry on a legend.
void setItemMode(QwtLegendData::Mode)
virtual void mouseReleaseEvent(QMouseEvent *) QWT_OVERRIDE
Handle mouse release events.
void setText(const QString &, QwtText::TextFormat textFormat=QwtText::AutoText)


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