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
22static const int ButtonFrame = 2;
23static const int Margin = 2;
24
25static 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
37class 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
62void 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
114{
115 const int flags = Qt::AlignLeft | Qt::AlignVCenter
116 | Qt::TextExpandTabs | Qt::TextWordWrap;
117
118 QwtText txt = text;
119 txt.setRenderFlags( flags );
120
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 );
140 setMargin( ButtonFrame + Margin );
141
142 updateGeometry();
143 }
144}
145
151{
152 return m_data->itemMode;
153}
154
162void 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
191void 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
211{
212 return m_data->spacing;
213}
214
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
236{
237 return m_data->itemMode == QwtLegendData::Checkable && isDown();
238}
239
241void 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
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
276 if ( m_data->itemMode != QwtLegendData::ReadOnly )
277 {
278 sz += buttonShift( this );
279 sz = qwtExpandedToGlobalStrut( sz );
280 }
281
282 return sz;
283}
284
286void 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
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
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
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#include "moc_qwt_legend_label.cpp"
QPixmap toPixmap(qreal devicePixelRatio=0.0) const
Convert the graphic to a QPixmap.
Attributes of an entry on a legend.
Mode mode() const
Mode
Mode defining how a legend entry interacts.
@ Checkable
The legend item is checkable, like a checkable button.
@ Clickable
The legend item is clickable, like a push button.
@ ReadOnly
The legend item is not interactive, like a label.
bool hasRole(int role) const
QwtGraphic icon() const
QwtText title() const
A widget representing something on a QwtLegend.
virtual QSize sizeHint() const override
Return a size hint.
void checked(bool)
Signal, when the legend item has been toggled.
virtual void keyPressEvent(QKeyEvent *) override
Handle key press events.
void setItemMode(QwtLegendData::Mode)
QwtLegendLabel(QWidget *parent=0)
bool isDown() const
Return true, if the item is down.
void released()
Signal, when the legend item has been released.
virtual void setText(const QwtText &) override
QwtLegendData::Mode itemMode() const
const QwtLegendData & data() const
QPixmap icon() const
virtual void paintEvent(QPaintEvent *) override
Paint event.
void setData(const QwtLegendData &)
void setSpacing(int spacing)
Change the spacing between icon and text.
void setDown(bool)
Set the item being down.
virtual ~QwtLegendLabel()
Destructor.
virtual void mousePressEvent(QMouseEvent *) override
Handle mouse press events.
virtual void mouseReleaseEvent(QMouseEvent *) override
Handle mouse release events.
void clicked()
Signal, when the legend item has been clicked.
void pressed()
Signal, when the legend item has been pressed.
void setIcon(const QPixmap &)
void setChecked(bool on)
virtual void keyReleaseEvent(QKeyEvent *) override
Handle key release events.
bool isChecked() const
Return true, if the item is checked.
static qreal devicePixelRatio(const QPaintDevice *)
A class representing a text.
Definition qwt_text.h:52
void setRenderFlags(int)
Change the render flags.
Definition qwt_text.cpp:304
A Widget which displays a QwtText.
int indent() const
Return label's text indent in pixels.
int margin() const
Return label's text margin in pixels.
const QwtText & text() const
Return the text.
virtual QSize sizeHint() const override
Return a size hint.
void setMargin(int)
void setIndent(int)
virtual void drawContents(QPainter *)
Redraw the text and focus indicator.
void setText(const QString &, QwtText::TextFormat textFormat=QwtText::AutoText)