1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
3 * Copyright (C) 1997 Josef Wilgen
4 * Copyright (C) 2002 Uwe Rathmann
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 *****************************************************************************/
10 #include "qwt_legend_label.h"
11 #include "qwt_legend_data.h"
13 #include "qwt_painter.h"
14 #include "qwt_symbol.h"
15 #include "qwt_graphic.h"
17 #include <qdrawutil.h>
21 #include <qstyleoption.h>
22 #include <qapplication.h>
24 static const int ButtonFrame
= 2;
25 static const int Margin
= 2;
27 static QSize
buttonShift( const QwtLegendLabel
*w
)
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
);
39 class QwtLegendLabel::PrivateData
43 itemMode( QwtLegendData::ReadOnly
),
49 QwtLegendData::Mode itemMode
;
50 QwtLegendData legendData
;
59 Set the attributes of the legend label
61 \param legendData Attributes of the label
64 void QwtLegendLabel::setData( const QwtLegendData
&legendData
)
66 d_data
->legendData
= legendData
;
68 const bool doUpdate
= updatesEnabled();
69 setUpdatesEnabled( false );
71 setText( legendData
.title() );
72 setIcon( legendData
.icon().toPixmap() );
74 if ( legendData
.hasRole( QwtLegendData::ModeRole
) )
75 setItemMode( legendData
.mode() );
79 setUpdatesEnabled( true );
85 \return Attributes of the label
86 \sa setData(), QwtPlotItem::legendData()
88 const QwtLegendData
&QwtLegendLabel::data() const
90 return d_data
->legendData
;
94 \param parent Parent widget
96 QwtLegendLabel::QwtLegendLabel( QWidget
*parent
):
97 QwtTextLabel( parent
)
99 d_data
= new PrivateData
;
105 QwtLegendLabel::~QwtLegendLabel()
112 Set the text to the legend item
114 \param text Text label
115 \sa QwtTextLabel::text()
117 void QwtLegendLabel::setText( const QwtText
&text
)
119 const int flags
= Qt::AlignLeft
| Qt::AlignVCenter
120 | Qt::TextExpandTabs
| Qt::TextWordWrap
;
123 txt
.setRenderFlags( flags
);
125 QwtTextLabel::setText( txt
);
130 The default is QwtLegendData::ReadOnly
132 \param mode Item mode
135 void QwtLegendLabel::setItemMode( QwtLegendData::Mode mode
)
137 if ( mode
!= d_data
->itemMode
)
139 d_data
->itemMode
= mode
;
140 d_data
->isDown
= false;
142 setFocusPolicy( ( mode
!= QwtLegendData::ReadOnly
)
143 ? Qt::TabFocus
: Qt::NoFocus
);
144 setMargin( ButtonFrame
+ Margin
);
154 QwtLegendData::Mode
QwtLegendLabel::itemMode() const
156 return d_data
->itemMode
;
162 \param icon Pixmap representing a plot item
164 \sa icon(), QwtPlotItem::legendIcon()
166 void QwtLegendLabel::setIcon( const QPixmap
&icon
)
170 int indent
= margin() + d_data
->spacing
;
171 if ( icon
.width() > 0 )
172 indent
+= icon
.width() + d_data
->spacing
;
178 \return Pixmap representing a plot item
181 QPixmap
QwtLegendLabel::icon() const
187 \brief Change the spacing between icon and text
189 \param spacing Spacing
190 \sa spacing(), QwtTextLabel::margin()
192 void QwtLegendLabel::setSpacing( int spacing
)
194 spacing
= qMax( spacing
, 0 );
195 if ( spacing
!= d_data
->spacing
)
197 d_data
->spacing
= spacing
;
199 int indent
= margin() + d_data
->spacing
;
200 if ( d_data
->icon
.width() > 0 )
201 indent
+= d_data
->icon
.width() + d_data
->spacing
;
208 \return Spacing between icon and text
209 \sa setSpacing(), QwtTextLabel::margin()
211 int QwtLegendLabel::spacing() const
213 return d_data
->spacing
;
217 Check/Uncheck a the item
219 \param on check/uncheck
222 void QwtLegendLabel::setChecked( bool on
)
224 if ( d_data
->itemMode
== QwtLegendData::Checkable
)
226 const bool isBlocked
= signalsBlocked();
227 blockSignals( true );
231 blockSignals( isBlocked
);
235 //! Return true, if the item is checked
236 bool QwtLegendLabel::isChecked() const
238 return d_data
->itemMode
== QwtLegendData::Checkable
&& isDown();
241 //! Set the item being down
242 void QwtLegendLabel::setDown( bool down
)
244 if ( down
== d_data
->isDown
)
247 d_data
->isDown
= down
;
250 if ( d_data
->itemMode
== QwtLegendData::Clickable
)
252 if ( d_data
->isDown
)
261 if ( d_data
->itemMode
== QwtLegendData::Checkable
)
262 Q_EMIT
checked( d_data
->isDown
);
265 //! Return true, if the item is down
266 bool QwtLegendLabel::isDown() const
268 return d_data
->isDown
;
271 //! Return a size hint
272 QSize
QwtLegendLabel::sizeHint() const
274 QSize sz
= QwtTextLabel::sizeHint();
275 sz
.setHeight( qMax( sz
.height(), d_data
->icon
.height() + 4 ) );
277 if ( d_data
->itemMode
!= QwtLegendData::ReadOnly
)
279 sz
+= buttonShift( this );
280 sz
= sz
.expandedTo( QApplication::globalStrut() );
287 void QwtLegendLabel::paintEvent( QPaintEvent
*e
)
289 const QRect cr
= contentsRect();
291 QPainter
painter( this );
292 painter
.setClipRegion( e
->region() );
294 if ( d_data
->isDown
)
296 qDrawWinButton( &painter
, 0, 0, width(), height(),
302 if ( d_data
->isDown
)
304 const QSize shiftSize
= buttonShift( this );
305 painter
.translate( shiftSize
.width(), shiftSize
.height() );
308 painter
.setClipRect( cr
);
310 drawContents( &painter
);
312 if ( !d_data
->icon
.isNull() )
315 iconRect
.setX( iconRect
.x() + margin() );
316 if ( d_data
->itemMode
!= QwtLegendData::ReadOnly
)
317 iconRect
.setX( iconRect
.x() + ButtonFrame
);
319 iconRect
.setSize( d_data
->icon
.size() );
320 iconRect
.moveCenter( QPoint( iconRect
.center().x(), cr
.center().y() ) );
322 painter
.drawPixmap( iconRect
, d_data
->icon
);
328 //! Handle mouse press events
329 void QwtLegendLabel::mousePressEvent( QMouseEvent
*e
)
331 if ( e
->button() == Qt::LeftButton
)
333 switch ( d_data
->itemMode
)
335 case QwtLegendData::Clickable
:
340 case QwtLegendData::Checkable
:
342 setDown( !isDown() );
348 QwtTextLabel::mousePressEvent( e
);
351 //! Handle mouse release events
352 void QwtLegendLabel::mouseReleaseEvent( QMouseEvent
*e
)
354 if ( e
->button() == Qt::LeftButton
)
356 switch ( d_data
->itemMode
)
358 case QwtLegendData::Clickable
:
363 case QwtLegendData::Checkable
:
365 return; // do nothing, but accept
370 QwtTextLabel::mouseReleaseEvent( e
);
373 //! Handle key press events
374 void QwtLegendLabel::keyPressEvent( QKeyEvent
*e
)
376 if ( e
->key() == Qt::Key_Space
)
378 switch ( d_data
->itemMode
)
380 case QwtLegendData::Clickable
:
382 if ( !e
->isAutoRepeat() )
386 case QwtLegendData::Checkable
:
388 if ( !e
->isAutoRepeat() )
389 setDown( !isDown() );
396 QwtTextLabel::keyPressEvent( e
);
399 //! Handle key release events
400 void QwtLegendLabel::keyReleaseEvent( QKeyEvent
*e
)
402 if ( e
->key() == Qt::Key_Space
)
404 switch ( d_data
->itemMode
)
406 case QwtLegendData::Clickable
:
408 if ( !e
->isAutoRepeat() )
412 case QwtLegendData::Checkable
:
414 return; // do nothing, but accept
420 QwtTextLabel::keyReleaseEvent( e
);