Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / libs / qwt / src / qwt_legend_label.cpp
blob19a7eb957833fb8e1621ef81a78d3a8d5053e627
1 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2 * Qwt Widget Library
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"
12 #include "qwt_math.h"
13 #include "qwt_painter.h"
14 #include "qwt_symbol.h"
15 #include "qwt_graphic.h"
16 #include <qpainter.h>
17 #include <qdrawutil.h>
18 #include <qstyle.h>
19 #include <qpen.h>
20 #include <qevent.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 )
29 QStyleOption option;
30 option.init( 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
41 public:
42 PrivateData():
43 itemMode( QwtLegendData::ReadOnly ),
44 isDown( false ),
45 spacing( Margin )
49 QwtLegendData::Mode itemMode;
50 QwtLegendData legendData;
51 bool isDown;
53 QPixmap icon;
55 int spacing;
58 /*!
59 Set the attributes of the legend label
61 \param legendData Attributes of the label
62 \sa data()
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() );
77 if ( doUpdate )
79 setUpdatesEnabled( true );
80 update();
84 /*!
85 \return Attributes of the label
86 \sa setData(), QwtPlotItem::legendData()
88 const QwtLegendData &QwtLegendLabel::data() const
90 return d_data->legendData;
93 /*!
94 \param parent Parent widget
96 QwtLegendLabel::QwtLegendLabel( QWidget *parent ):
97 QwtTextLabel( parent )
99 d_data = new PrivateData;
100 setMargin( Margin );
101 setIndent( Margin );
104 //! Destructor
105 QwtLegendLabel::~QwtLegendLabel()
107 delete d_data;
108 d_data = NULL;
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;
122 QwtText txt = text;
123 txt.setRenderFlags( flags );
125 QwtTextLabel::setText( txt );
129 Set the item mode
130 The default is QwtLegendData::ReadOnly
132 \param mode Item mode
133 \sa itemMode()
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 );
146 updateGeometry();
151 \return Item mode
152 \sa setItemMode()
154 QwtLegendData::Mode QwtLegendLabel::itemMode() const
156 return d_data->itemMode;
160 Assign the icon
162 \param icon Pixmap representing a plot item
164 \sa icon(), QwtPlotItem::legendIcon()
166 void QwtLegendLabel::setIcon( const QPixmap &icon )
168 d_data->icon = icon;
170 int indent = margin() + d_data->spacing;
171 if ( icon.width() > 0 )
172 indent += icon.width() + d_data->spacing;
174 setIndent( indent );
178 \return Pixmap representing a plot item
179 \sa setIcon()
181 QPixmap QwtLegendLabel::icon() const
183 return d_data->icon;
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;
203 setIndent( indent );
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
220 \sa setItemMode()
222 void QwtLegendLabel::setChecked( bool on )
224 if ( d_data->itemMode == QwtLegendData::Checkable )
226 const bool isBlocked = signalsBlocked();
227 blockSignals( true );
229 setDown( on );
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 )
245 return;
247 d_data->isDown = down;
248 update();
250 if ( d_data->itemMode == QwtLegendData::Clickable )
252 if ( d_data->isDown )
253 Q_EMIT pressed();
254 else
256 Q_EMIT released();
257 Q_EMIT clicked();
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() );
283 return sz;
286 //! Paint event
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(),
297 palette(), true );
300 painter.save();
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() )
314 QRect iconRect = cr;
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 );
325 painter.restore();
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:
337 setDown( true );
338 return;
340 case QwtLegendData::Checkable:
342 setDown( !isDown() );
343 return;
345 default:;
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:
360 setDown( false );
361 return;
363 case QwtLegendData::Checkable:
365 return; // do nothing, but accept
367 default:;
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() )
383 setDown( true );
384 return;
386 case QwtLegendData::Checkable:
388 if ( !e->isAutoRepeat() )
389 setDown( !isDown() );
390 return;
392 default:;
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() )
409 setDown( false );
410 return;
412 case QwtLegendData::Checkable:
414 return; // do nothing, but accept
416 default:;
420 QwtTextLabel::keyReleaseEvent( e );