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_math.h"
13 #include "qwt_painter.h"
14 #include "qwt_symbol.h"
15 #include "qwt_graphic.h"
16 #include <qpainter.h>
17 #include <qdrawutil.h>
18 #include <qstyle.h>
19 #include <qpen.h>
20 #include <qevent.h>
21 #include <qstyleoption.h>
22 #include <qapplication.h>
23 
24 static const int ButtonFrame = 2;
25 static const int Margin = 2;
26 
27 static QSize buttonShift( const QwtLegendLabel *w )
28 {
29  QStyleOption option;
30  option.init( w );
31 
32  const int ph = w->style()->pixelMetric(
33  QStyle::PM_ButtonShiftHorizontal, &option, w );
34  const int pv = w->style()->pixelMetric(
35  QStyle::PM_ButtonShiftVertical, &option, w );
36  return QSize( ph, pv );
37 }
38 
40 {
41 public:
43  itemMode( QwtLegendData::ReadOnly ),
44  isDown( false ),
45  spacing( Margin )
46  {
47  }
48 
51  bool isDown;
52 
53  QPixmap icon;
54 
55  int spacing;
56 };
57 
65 {
67 
68  const bool doUpdate = updatesEnabled();
69  if ( doUpdate )
70  setUpdatesEnabled( false );
71 
72  setText( legendData.title() );
73  setIcon( legendData.icon().toPixmap() );
74 
75  if ( legendData.hasRole( QwtLegendData::ModeRole ) )
76  setItemMode( legendData.mode() );
77 
78  if ( doUpdate )
79  setUpdatesEnabled( true );
80 }
81 
87 {
88  return d_data->legendData;
89 }
90 
94 QwtLegendLabel::QwtLegendLabel( QWidget *parent ):
95  QwtTextLabel( parent )
96 {
97  d_data = new PrivateData;
98  setMargin( Margin );
99  setIndent( Margin );
100 }
101 
104 {
105  delete d_data;
106  d_data = NULL;
107 }
108 
116 {
117  const int flags = Qt::AlignLeft | Qt::AlignVCenter
118  | Qt::TextExpandTabs | Qt::TextWordWrap;
119 
120  QwtText txt = text;
121  txt.setRenderFlags( flags );
122 
123  QwtTextLabel::setText( txt );
124 }
125 
134 {
135  if ( mode != d_data->itemMode )
136  {
137  d_data->itemMode = mode;
138  d_data->isDown = false;
139 
140  setFocusPolicy( ( mode != QwtLegendData::ReadOnly )
141  ? Qt::TabFocus : Qt::NoFocus );
143 
144  updateGeometry();
145  }
146 }
147 
153 {
154  return d_data->itemMode;
155 }
156 
164 void QwtLegendLabel::setIcon( const QPixmap &icon )
165 {
166  d_data->icon = icon;
167 
168  int indent = margin() + d_data->spacing;
169  if ( icon.width() > 0 )
170  indent += icon.width() + d_data->spacing;
171 
172  setIndent( indent );
173 }
174 
179 QPixmap QwtLegendLabel::icon() const
180 {
181  return d_data->icon;
182 }
183 
191 {
192  spacing = qMax( spacing, 0 );
193  if ( spacing != d_data->spacing )
194  {
196 
197  int indent = margin() + d_data->spacing;
198  if ( d_data->icon.width() > 0 )
199  indent += d_data->icon.width() + d_data->spacing;
200 
201  setIndent( indent );
202  }
203 }
204 
210 {
211  return d_data->spacing;
212 }
213 
221 {
223  {
224  const bool isBlocked = signalsBlocked();
225  blockSignals( true );
226 
227  setDown( on );
228 
229  blockSignals( isBlocked );
230  }
231 }
232 
235 {
237 }
238 
240 void QwtLegendLabel::setDown( bool down )
241 {
242  if ( down == d_data->isDown )
243  return;
244 
245  d_data->isDown = down;
246  update();
247 
249  {
250  if ( d_data->isDown )
251  Q_EMIT pressed();
252  else
253  {
254  Q_EMIT released();
255  Q_EMIT clicked();
256  }
257  }
258 
260  Q_EMIT checked( d_data->isDown );
261 }
262 
265 {
266  return d_data->isDown;
267 }
268 
271 {
272  QSize sz = QwtTextLabel::sizeHint();
273  sz.setHeight( qMax( sz.height(), d_data->icon.height() + 4 ) );
274 
276  {
277  sz += buttonShift( this );
278  sz = sz.expandedTo( QApplication::globalStrut() );
279  }
280 
281  return sz;
282 }
283 
285 void QwtLegendLabel::paintEvent( QPaintEvent *e )
286 {
287  const QRect cr = contentsRect();
288 
289  QPainter painter( this );
290  painter.setClipRegion( e->region() );
291 
292  if ( d_data->isDown )
293  {
294  qDrawWinButton( &painter, 0, 0, width(), height(),
295  palette(), true );
296  }
297 
298  painter.save();
299 
300  if ( d_data->isDown )
301  {
302  const QSize shiftSize = buttonShift( this );
303  painter.translate( shiftSize.width(), shiftSize.height() );
304  }
305 
306  painter.setClipRect( cr );
307 
308  drawContents( &painter );
309 
310  if ( !d_data->icon.isNull() )
311  {
312  QRect iconRect = cr;
313  iconRect.setX( iconRect.x() + margin() );
315  iconRect.setX( iconRect.x() + ButtonFrame );
316 
317  iconRect.setSize( d_data->icon.size() );
318  iconRect.moveCenter( QPoint( iconRect.center().x(), cr.center().y() ) );
319 
320  painter.drawPixmap( iconRect, d_data->icon );
321  }
322 
323  painter.restore();
324 }
325 
327 void QwtLegendLabel::mousePressEvent( QMouseEvent *e )
328 {
329  if ( e->button() == Qt::LeftButton )
330  {
331  switch ( d_data->itemMode )
332  {
334  {
335  setDown( true );
336  return;
337  }
339  {
340  setDown( !isDown() );
341  return;
342  }
343  default:;
344  }
345  }
346  QwtTextLabel::mousePressEvent( e );
347 }
348 
350 void QwtLegendLabel::mouseReleaseEvent( QMouseEvent *e )
351 {
352  if ( e->button() == Qt::LeftButton )
353  {
354  switch ( d_data->itemMode )
355  {
357  {
358  setDown( false );
359  return;
360  }
362  {
363  return; // do nothing, but accept
364  }
365  default:;
366  }
367  }
368  QwtTextLabel::mouseReleaseEvent( e );
369 }
370 
372 void QwtLegendLabel::keyPressEvent( QKeyEvent *e )
373 {
374  if ( e->key() == Qt::Key_Space )
375  {
376  switch ( d_data->itemMode )
377  {
379  {
380  if ( !e->isAutoRepeat() )
381  setDown( true );
382  return;
383  }
385  {
386  if ( !e->isAutoRepeat() )
387  setDown( !isDown() );
388  return;
389  }
390  default:;
391  }
392  }
393 
394  QwtTextLabel::keyPressEvent( e );
395 }
396 
399 {
400  if ( e->key() == Qt::Key_Space )
401  {
402  switch ( d_data->itemMode )
403  {
405  {
406  if ( !e->isAutoRepeat() )
407  setDown( false );
408  return;
409  }
411  {
412  return; // do nothing, but accept
413  }
414  default:;
415  }
416  }
417 
418  QwtTextLabel::keyReleaseEvent( e );
419 }
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 QSize sizeHint() const
Return a size hint.
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.
QPixmap toPixmap() const
Convert the graphic to a QPixmap.
virtual void keyReleaseEvent(QKeyEvent *)
Handle key release events.
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 update(const std::string &key, const XmlRpc::XmlRpcValue &v)
void setChecked(bool on)
virtual void mouseReleaseEvent(QMouseEvent *)
Handle mouse release events.
bool hasRole(int role) const
virtual QSize sizeHint() const
Return a size hint.
QwtLegendData::Mode itemMode
void setIndent(int)
void setRenderFlags(int flags)
Change the render flags.
Definition: qwt_text.cpp:271
virtual ~QwtLegendLabel()
Destructor.
A class representing a text.
Definition: qwt_text.h:51
virtual void drawContents(QPainter *)
Redraw the text and focus indicator.
QwtGraphic icon() const
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.
QwtText title() const
virtual void keyPressEvent(QKeyEvent *)
Handle key press events.
QPixmap icon() const
void setIcon(const QPixmap &)
void released()
Signal, when the legend item has been released.
QwtLegendData::Mode itemMode() const
virtual void mousePressEvent(QMouseEvent *)
Handle mouse press events.
void setData(const QwtLegendData &)
int margin() const
virtual void setText(const QwtText &)
PrivateData * d_data
Mode
Mode defining how a legend entry interacts.
int flags
virtual void paintEvent(QPaintEvent *)
Paint event.
bool isDown() const
Return true, if the item is down.
static const int ButtonFrame
Attributes of an entry on a legend.
void setItemMode(QwtLegendData::Mode)
void setText(const QString &, QwtText::TextFormat textFormat=QwtText::AutoText)


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