Qwt User's Guide 6.3.0

Loading...
Searching...
No Matches
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_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
37 class QwtLegendLabel::PrivateData
38{
39 public:
40 PrivateData()
41 : itemMode( QwtLegendData::ReadOnly )
42 , isDown( false )
43 , spacing( Margin )
44 {
45 }
46
47 QwtLegendData::Mode itemMode;
48 QwtLegendData legendData;
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
84 const QwtLegendData& QwtLegendLabel::data() const
85{
86 return m_data->legendData;
87}
88
92 QwtLegendLabel::QwtLegendLabel( QWidget* parent )
93 : QwtTextLabel( parent )
94{
95 m_data = new PrivateData;
96 setMargin( Margin );
97 setIndent( Margin );
98}
99
101 QwtLegendLabel::~QwtLegendLabel()
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
131 void QwtLegendLabel::setItemMode( QwtLegendData::Mode mode )
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 );
140 setMargin( ButtonFrame + Margin );
141
142 updateGeometry();
143 }
144}
145
150 QwtLegendData::Mode QwtLegendLabel::itemMode() const
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
180QPixmap 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 {
196 m_data->spacing = spacing;
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
210 int QwtLegendLabel::spacing() const
211{
212 return m_data->spacing;
213}
214
221 void QwtLegendLabel::setChecked( bool on )
222{
223 if ( m_data->itemMode == QwtLegendData::Checkable )
224 {
225 const bool isBlocked = signalsBlocked();
226 blockSignals( true );
227
228 setDown( on );
229
230 blockSignals( isBlocked );
231 }
232}
233
235 bool QwtLegendLabel::isChecked() const
236{
237 return m_data->itemMode == QwtLegendData::Checkable && isDown();
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
249 if ( m_data->itemMode == QwtLegendData::Clickable )
250 {
251 if ( m_data->isDown )
252 Q_EMIT pressed();
253 else
254 {
255 Q_EMIT released();
256 Q_EMIT clicked();
257 }
258 }
259
260 if ( m_data->itemMode == QwtLegendData::Checkable )
261 Q_EMIT checked( m_data->isDown );
262}
263
265 bool QwtLegendLabel::isDown() const
266{
267 return m_data->isDown;
268}
269
271QSize QwtLegendLabel::sizeHint() const
272{
273 QSize sz = QwtTextLabel::sizeHint();
274 sz.setHeight( qMax( sz.height(), m_data->icon.height() + 4 ) );
275
276 if ( m_data->itemMode != QwtLegendData::ReadOnly )
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() );
315 if ( m_data->itemMode != QwtLegendData::ReadOnly )
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 {
335 case QwtLegendData::Clickable:
336 {
337 setDown( true );
338 return;
339 }
340 case QwtLegendData::Checkable:
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 {
358 case QwtLegendData::Clickable:
359 {
360 setDown( false );
361 return;
362 }
363 case QwtLegendData::Checkable:
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 {
380 case QwtLegendData::Clickable:
381 {
382 if ( !e->isAutoRepeat() )
383 setDown( true );
384 return;
385 }
386 case QwtLegendData::Checkable:
387 {
388 if ( !e->isAutoRepeat() )
389 setDown( !isDown() );
390 return;
391 }
392 default:;
393 }
394 }
395
396 QwtTextLabel::keyPressEvent( e );
397}
398
400 void QwtLegendLabel::keyReleaseEvent( QKeyEvent* e )
401{
402 if ( e->key() == Qt::Key_Space )
403 {
404 switch ( m_data->itemMode )
405 {
406 case QwtLegendData::Clickable:
407 {
408 if ( !e->isAutoRepeat() )
409 setDown( false );
410 return;
411 }
412 case QwtLegendData::Checkable:
413 {
414 return; // do nothing, but accept
415 }
416 default:;
417 }
418 }
419
420 QwtTextLabel::keyReleaseEvent( e );
421}
422
423#include "moc_qwt_legend_label.cpp"
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:271
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:374
QwtLegendLabel::setItemMode
void setItemMode(QwtLegendData::Mode)
Definition qwt_legend_label.cpp:131
QwtLegendLabel::spacing
int spacing() const
Definition qwt_legend_label.cpp:210
QwtLegendLabel::QwtLegendLabel
QwtLegendLabel(QWidget *parent=0)
Definition qwt_legend_label.cpp:92
QwtLegendLabel::isDown
bool isDown() const
Return true, if the item is down.
Definition qwt_legend_label.cpp:265
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:113
QwtLegendLabel::itemMode
QwtLegendData::Mode itemMode() const
Definition qwt_legend_label.cpp:150
QwtLegendLabel::data
const QwtLegendData & data() const
Definition qwt_legend_label.cpp:84
QwtLegendLabel::icon
QPixmap icon() const
Definition qwt_legend_label.cpp:180
QwtLegendLabel::paintEvent
virtual void paintEvent(QPaintEvent *) override
Paint event.
Definition qwt_legend_label.cpp:286
QwtLegendLabel::setData
void setData(const QwtLegendData &)
Definition qwt_legend_label.cpp:62
QwtLegendLabel::setSpacing
void setSpacing(int spacing)
Change the spacing between icon and text.
Definition qwt_legend_label.cpp:191
QwtLegendLabel::setDown
void setDown(bool)
Set the item being down.
Definition qwt_legend_label.cpp:241
QwtLegendLabel::~QwtLegendLabel
virtual ~QwtLegendLabel()
Destructor.
Definition qwt_legend_label.cpp:101
QwtLegendLabel::mousePressEvent
virtual void mousePressEvent(QMouseEvent *) override
Handle mouse press events.
Definition qwt_legend_label.cpp:329
QwtLegendLabel::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *) override
Handle mouse release events.
Definition qwt_legend_label.cpp:352
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:162
QwtLegendLabel::setChecked
void setChecked(bool on)
Definition qwt_legend_label.cpp:221
QwtLegendLabel::keyReleaseEvent
virtual void keyReleaseEvent(QKeyEvent *) override
Handle key release events.
Definition qwt_legend_label.cpp:400
QwtLegendLabel::isChecked
bool isChecked() const
Return true, if the item is checked.
Definition qwt_legend_label.cpp:235
QwtPainter::devicePixelRatio
static qreal devicePixelRatio(const QPaintDevice *)
Definition qwt_painter.cpp:1481
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