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


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45