Qwt User's Guide: qwt_legend_label.cpp Source File

Qwt User's Guide 6.2.0

qwt_legend_label.cpp
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.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
36 class QwtLegendLabel::PrivateData
37 {
38 public:
39 PrivateData()
40 : itemMode( QwtLegendData::ReadOnly )
41 , isDown( false )
42 , spacing( Margin )
43 {
44 }
45
46 QwtLegendData::Mode itemMode;
47 QwtLegendData legendData;
48 bool isDown;
49
50 QPixmap icon;
51
52 int spacing;
53 };
54
61 void QwtLegendLabel::setData( const QwtLegendData& legendData )
62 {
63 m_data->legendData = legendData;
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
83 const QwtLegendData& QwtLegendLabel::data() const
84 {
85 return m_data->legendData;
86 }
87
91 QwtLegendLabel::QwtLegendLabel( QWidget* parent )
92 : QwtTextLabel( parent )
93 {
94 m_data = new PrivateData;
95 setMargin( Margin );
96 setIndent( Margin );
97 }
98
100 QwtLegendLabel::~QwtLegendLabel()
101 {
102 delete m_data;
103 m_data = NULL;
104 }
105
112 void QwtLegendLabel::setText( const QwtText& text )
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
130 void QwtLegendLabel::setItemMode( QwtLegendData::Mode mode )
131 {
132 if ( mode != m_data->itemMode )
133 {
134 m_data->itemMode = mode;
135 m_data->isDown = false;
136
137 setFocusPolicy( ( mode != QwtLegendData::ReadOnly )
138 ? Qt::TabFocus : Qt::NoFocus );
139 setMargin( ButtonFrame + Margin );
140
141 updateGeometry();
142 }
143 }
144
149 QwtLegendData::Mode QwtLegendLabel::itemMode() const
150 {
151 return m_data->itemMode;
152 }
153
161 void QwtLegendLabel::setIcon( const QPixmap& icon )
162 {
163 m_data->icon = icon;
164
165 int indent = margin() + m_data->spacing;
166 if ( icon.width() > 0 )
167 indent += icon.width() + m_data->spacing;
168
169 setIndent( indent );
170 }
171
176 QPixmap QwtLegendLabel::icon() const
177 {
178 return m_data->icon;
179 }
180
187 void QwtLegendLabel::setSpacing( int spacing )
188 {
189 spacing = qMax( spacing, 0 );
190 if ( spacing != m_data->spacing )
191 {
192 m_data->spacing = spacing;
193
194 int indent = margin() + m_data->spacing;
195 if ( m_data->icon.width() > 0 )
196 indent += m_data->icon.width() + m_data->spacing;
197
198 setIndent( indent );
199 }
200 }
201
206 int QwtLegendLabel::spacing() const
207 {
208 return m_data->spacing;
209 }
210
217 void QwtLegendLabel::setChecked( bool on )
218 {
219 if ( m_data->itemMode == QwtLegendData::Checkable )
220 {
221 const bool isBlocked = signalsBlocked();
222 blockSignals( true );
223
224 setDown( on );
225
226 blockSignals( isBlocked );
227 }
228 }
229
231 bool QwtLegendLabel::isChecked() const
232 {
233 return m_data->itemMode == QwtLegendData::Checkable && isDown();
234 }
235
237 void QwtLegendLabel::setDown( bool down )
238 {
239 if ( down == m_data->isDown )
240 return;
241
242 m_data->isDown = down;
243 update();
244
245 if ( m_data->itemMode == QwtLegendData::Clickable )
246 {
247 if ( m_data->isDown )
248 Q_EMIT pressed();
249 else
250 {
251 Q_EMIT released();
252 Q_EMIT clicked();
253 }
254 }
255
256 if ( m_data->itemMode == QwtLegendData::Checkable )
257 Q_EMIT checked( m_data->isDown );
258 }
259
261 bool QwtLegendLabel::isDown() const
262 {
263 return m_data->isDown;
264 }
265
267 QSize QwtLegendLabel::sizeHint() const
268 {
269 QSize sz = QwtTextLabel::sizeHint();
270 sz.setHeight( qMax( sz.height(), m_data->icon.height() + 4 ) );
271
272 if ( m_data->itemMode != QwtLegendData::ReadOnly )
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 ( m_data->isDown )
290 {
291 qDrawWinButton( &painter, 0, 0, width(), height(),
292 palette(), true );
293 }
294
295 painter.save();
296
297 if ( m_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 ( !m_data->icon.isNull() )
308 {
309 QRect iconRect = cr;
310 iconRect.setX( iconRect.x() + margin() );
311 if ( m_data->itemMode != QwtLegendData::ReadOnly )
312 iconRect.setX( iconRect.x() + ButtonFrame );
313
314 iconRect.setSize( m_data->icon.size() );
315 iconRect.moveCenter( QPoint( iconRect.center().x(), cr.center().y() ) );
316
317 painter.drawPixmap( iconRect, m_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 ( m_data->itemMode )
329 {
330 case QwtLegendData::Clickable:
331 {
332 setDown( true );
333 return;
334 }
335 case QwtLegendData::Checkable:
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 ( m_data->itemMode )
352 {
353 case QwtLegendData::Clickable:
354 {
355 setDown( false );
356 return;
357 }
358 case QwtLegendData::Checkable:
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 ( m_data->itemMode )
374 {
375 case QwtLegendData::Clickable:
376 {
377 if ( !e->isAutoRepeat() )
378 setDown( true );
379 return;
380 }
381 case QwtLegendData::Checkable:
382 {
383 if ( !e->isAutoRepeat() )
384 setDown( !isDown() );
385 return;
386 }
387 default:;
388 }
389 }
390
391 QwtTextLabel::keyPressEvent( e );
392 }
393
395 void QwtLegendLabel::keyReleaseEvent( QKeyEvent* e )
396 {
397 if ( e->key() == Qt::Key_Space )
398 {
399 switch ( m_data->itemMode )
400 {
401 case QwtLegendData::Clickable:
402 {
403 if ( !e->isAutoRepeat() )
404 setDown( false );
405 return;
406 }
407 case QwtLegendData::Checkable:
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
QwtGraphic::toPixmap
QPixmap toPixmap(qreal devicePixelRatio=0.0) const
Convert the graphic to a QPixmap.
Definition: qwt_graphic.cpp:812
QwtLegendData
Attributes of an entry on a legend.
Definition: qwt_legend_data.h:37
QwtLegendData::mode
Mode mode() const
Definition: qwt_legend_data.cpp:120
QwtLegendData::Mode
Mode
Mode defining how a legend entry interacts.
Definition: qwt_legend_data.h:41
QwtLegendData::Checkable
@ Checkable
The legend item is checkable, like a checkable button.
Definition: qwt_legend_data.h:49
QwtLegendData::Clickable
@ Clickable
The legend item is clickable, like a push button.
Definition: qwt_legend_data.h:46
QwtLegendData::ReadOnly
@ ReadOnly
The legend item is not interactive, like a label.
Definition: qwt_legend_data.h:43
QwtLegendData::hasRole
bool hasRole(int role) const
Definition: qwt_legend_data.cpp:51
QwtLegendData::icon
QwtGraphic icon() const
Definition: qwt_legend_data.cpp:106
QwtLegendData::title
QwtText title() const
Definition: qwt_legend_data.cpp:88
QwtLegendLabel
A widget representing something on a QwtLegend.
Definition: qwt_legend_label.h:23
QwtLegendLabel::sizeHint
virtual QSize sizeHint() const override
Return a size hint.
Definition: qwt_legend_label.cpp:267
QwtLegendLabel::checked
void checked(bool)
Signal, when the legend item has been toggled.
QwtLegendLabel::keyPressEvent
virtual void keyPressEvent(QKeyEvent *) override
Handle key press events.
Definition: qwt_legend_label.cpp:369
QwtLegendLabel::setItemMode
void setItemMode(QwtLegendData::Mode)
Definition: qwt_legend_label.cpp:130
QwtLegendLabel::spacing
int spacing() const
Definition: qwt_legend_label.cpp:206
QwtLegendLabel::QwtLegendLabel
QwtLegendLabel(QWidget *parent=0)
Definition: qwt_legend_label.cpp:91
QwtLegendLabel::isDown
bool isDown() const
Return true, if the item is down.
Definition: qwt_legend_label.cpp:261
QwtLegendLabel::released
void released()
Signal, when the legend item has been released.
QwtLegendLabel::setText
virtual void setText(const QwtText &) override
Definition: qwt_legend_label.cpp:112
QwtLegendLabel::itemMode
QwtLegendData::Mode itemMode() const
Definition: qwt_legend_label.cpp:149
QwtLegendLabel::data
const QwtLegendData & data() const
Definition: qwt_legend_label.cpp:83
QwtLegendLabel::icon
QPixmap icon() const
Definition: qwt_legend_label.cpp:176
QwtLegendLabel::paintEvent
virtual void paintEvent(QPaintEvent *) override
Paint event.
Definition: qwt_legend_label.cpp:282
QwtLegendLabel::setData
void setData(const QwtLegendData &)
Definition: qwt_legend_label.cpp:61
QwtLegendLabel::setSpacing
void setSpacing(int spacing)
Change the spacing between icon and text.
Definition: qwt_legend_label.cpp:187
QwtLegendLabel::setDown
void setDown(bool)
Set the item being down.
Definition: qwt_legend_label.cpp:237
QwtLegendLabel::~QwtLegendLabel
virtual ~QwtLegendLabel()
Destructor.
Definition: qwt_legend_label.cpp:100
QwtLegendLabel::mousePressEvent
virtual void mousePressEvent(QMouseEvent *) override
Handle mouse press events.
Definition: qwt_legend_label.cpp:324
QwtLegendLabel::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *) override
Handle mouse release events.
Definition: qwt_legend_label.cpp:347
QwtLegendLabel::clicked
void clicked()
Signal, when the legend item has been clicked.
QwtLegendLabel::pressed
void pressed()
Signal, when the legend item has been pressed.
QwtLegendLabel::setIcon
void setIcon(const QPixmap &)
Definition: qwt_legend_label.cpp:161
QwtLegendLabel::setChecked
void setChecked(bool on)
Definition: qwt_legend_label.cpp:217
QwtLegendLabel::keyReleaseEvent
virtual void keyReleaseEvent(QKeyEvent *) override
Handle key release events.
Definition: qwt_legend_label.cpp:395
QwtLegendLabel::isChecked
bool isChecked() const
Return true, if the item is checked.
Definition: qwt_legend_label.cpp:231
QwtText
A class representing a text.
Definition: qwt_text.h:52
QwtText::setRenderFlags
void setRenderFlags(int)
Change the render flags.
Definition: qwt_text.cpp:304
QwtTextLabel
A Widget which displays a QwtText.
Definition: qwt_text_label.h:27
QwtTextLabel::indent
int indent() const
Return label's text indent in pixels.
Definition: qwt_text_label.cpp:133
QwtTextLabel::margin
int margin() const
Return label's text margin in pixels.
Definition: qwt_text_label.cpp:154
QwtTextLabel::text
const QwtText & text() const
Return the text.
Definition: qwt_text_label.cpp:118
QwtTextLabel::sizeHint
virtual QSize sizeHint() const override
Return a size hint.
Definition: qwt_text_label.cpp:172
QwtTextLabel::setMargin
void setMargin(int)
Definition: qwt_text_label.cpp:163
QwtTextLabel::setIndent
void setIndent(int)
Definition: qwt_text_label.cpp:142
QwtTextLabel::drawContents
virtual void drawContents(QPainter *)
Redraw the text and focus indicator.
Definition: qwt_text_label.cpp:257
QwtTextLabel::setText
void setText(const QString &, QwtText::TextFormat textFormat=QwtText::AutoText)
Definition: qwt_text_label.cpp:96