source: ntrip/trunk/BNC/qwt/qwt_legend_label.cpp@ 10518

Last change on this file since 10518 was 9383, checked in by stoecker, 4 years ago

update to qwt verion 6.1.1 to fix build with newer Qt5

File size: 8.9 KB
Line 
1/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
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_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>
23
24static const int ButtonFrame = 2;
25static const int Margin = 2;
26
27static QSize buttonShift( const QwtLegendLabel *w )
28{
29 QStyleOption option;
30 option.init( w );
31
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 );
37}
38
39class QwtLegendLabel::PrivateData
40{
41public:
42 PrivateData():
43 itemMode( QwtLegendData::ReadOnly ),
44 isDown( false ),
45 spacing( Margin )
46 {
47 }
48
49 QwtLegendData::Mode itemMode;
50 QwtLegendData legendData;
51 bool isDown;
52
53 QPixmap icon;
54
55 int spacing;
56};
57
58/*!
59 Set the attributes of the legend label
60
61 \param legendData Attributes of the label
62 \sa data()
63 */
64void QwtLegendLabel::setData( const QwtLegendData &legendData )
65{
66 d_data->legendData = legendData;
67
68 const bool doUpdate = updatesEnabled();
69 setUpdatesEnabled( false );
70
71 setText( legendData.title() );
72 setIcon( legendData.icon().toPixmap() );
73
74 if ( legendData.hasRole( QwtLegendData::ModeRole ) )
75 setItemMode( legendData.mode() );
76
77 if ( doUpdate )
78 {
79 setUpdatesEnabled( true );
80 update();
81 }
82}
83
84/*!
85 \return Attributes of the label
86 \sa setData(), QwtPlotItem::legendData()
87 */
88const QwtLegendData &QwtLegendLabel::data() const
89{
90 return d_data->legendData;
91}
92
93/*!
94 \param parent Parent widget
95*/
96QwtLegendLabel::QwtLegendLabel( QWidget *parent ):
97 QwtTextLabel( parent )
98{
99 d_data = new PrivateData;
100 setMargin( Margin );
101 setIndent( Margin );
102}
103
104//! Destructor
105QwtLegendLabel::~QwtLegendLabel()
106{
107 delete d_data;
108 d_data = NULL;
109}
110
111/*!
112 Set the text to the legend item
113
114 \param text Text label
115 \sa QwtTextLabel::text()
116*/
117void QwtLegendLabel::setText( const QwtText &text )
118{
119 const int flags = Qt::AlignLeft | Qt::AlignVCenter
120 | Qt::TextExpandTabs | Qt::TextWordWrap;
121
122 QwtText txt = text;
123 txt.setRenderFlags( flags );
124
125 QwtTextLabel::setText( txt );
126}
127
128/*!
129 Set the item mode
130 The default is QwtLegendData::ReadOnly
131
132 \param mode Item mode
133 \sa itemMode()
134*/
135void QwtLegendLabel::setItemMode( QwtLegendData::Mode mode )
136{
137 if ( mode != d_data->itemMode )
138 {
139 d_data->itemMode = mode;
140 d_data->isDown = false;
141
142 setFocusPolicy( ( mode != QwtLegendData::ReadOnly )
143 ? Qt::TabFocus : Qt::NoFocus );
144 setMargin( ButtonFrame + Margin );
145
146 updateGeometry();
147 }
148}
149
150/*!
151 \return Item mode
152 \sa setItemMode()
153*/
154QwtLegendData::Mode QwtLegendLabel::itemMode() const
155{
156 return d_data->itemMode;
157}
158
159/*!
160 Assign the icon
161
162 \param icon Pixmap representing a plot item
163
164 \sa icon(), QwtPlotItem::legendIcon()
165*/
166void QwtLegendLabel::setIcon( const QPixmap &icon )
167{
168 d_data->icon = icon;
169
170 int indent = margin() + d_data->spacing;
171 if ( icon.width() > 0 )
172 indent += icon.width() + d_data->spacing;
173
174 setIndent( indent );
175}
176
177/*!
178 \return Pixmap representing a plot item
179 \sa setIcon()
180*/
181QPixmap QwtLegendLabel::icon() const
182{
183 return d_data->icon;
184}
185
186/*!
187 \brief Change the spacing between icon and text
188
189 \param spacing Spacing
190 \sa spacing(), QwtTextLabel::margin()
191*/
192void QwtLegendLabel::setSpacing( int spacing )
193{
194 spacing = qMax( spacing, 0 );
195 if ( spacing != d_data->spacing )
196 {
197 d_data->spacing = spacing;
198
199 int indent = margin() + d_data->spacing;
200 if ( d_data->icon.width() > 0 )
201 indent += d_data->icon.width() + d_data->spacing;
202
203 setIndent( indent );
204 }
205}
206
207/*!
208 \return Spacing between icon and text
209 \sa setSpacing(), QwtTextLabel::margin()
210*/
211int QwtLegendLabel::spacing() const
212{
213 return d_data->spacing;
214}
215
216/*!
217 Check/Uncheck a the item
218
219 \param on check/uncheck
220 \sa setItemMode()
221*/
222void QwtLegendLabel::setChecked( bool on )
223{
224 if ( d_data->itemMode == QwtLegendData::Checkable )
225 {
226 const bool isBlocked = signalsBlocked();
227 blockSignals( true );
228
229 setDown( on );
230
231 blockSignals( isBlocked );
232 }
233}
234
235//! Return true, if the item is checked
236bool QwtLegendLabel::isChecked() const
237{
238 return d_data->itemMode == QwtLegendData::Checkable && isDown();
239}
240
241//! Set the item being down
242void QwtLegendLabel::setDown( bool down )
243{
244 if ( down == d_data->isDown )
245 return;
246
247 d_data->isDown = down;
248 update();
249
250 if ( d_data->itemMode == QwtLegendData::Clickable )
251 {
252 if ( d_data->isDown )
253 Q_EMIT pressed();
254 else
255 {
256 Q_EMIT released();
257 Q_EMIT clicked();
258 }
259 }
260
261 if ( d_data->itemMode == QwtLegendData::Checkable )
262 Q_EMIT checked( d_data->isDown );
263}
264
265//! Return true, if the item is down
266bool QwtLegendLabel::isDown() const
267{
268 return d_data->isDown;
269}
270
271//! Return a size hint
272QSize QwtLegendLabel::sizeHint() const
273{
274 QSize sz = QwtTextLabel::sizeHint();
275 sz.setHeight( qMax( sz.height(), d_data->icon.height() + 4 ) );
276
277 if ( d_data->itemMode != QwtLegendData::ReadOnly )
278 {
279 sz += buttonShift( this );
280 sz = sz.expandedTo( QApplication::globalStrut() );
281 }
282
283 return sz;
284}
285
286//! Paint event
287void QwtLegendLabel::paintEvent( QPaintEvent *e )
288{
289 const QRect cr = contentsRect();
290
291 QPainter painter( this );
292 painter.setClipRegion( e->region() );
293
294 if ( d_data->isDown )
295 {
296 qDrawWinButton( &painter, 0, 0, width(), height(),
297 palette(), true );
298 }
299
300 painter.save();
301
302 if ( d_data->isDown )
303 {
304 const QSize shiftSize = buttonShift( this );
305 painter.translate( shiftSize.width(), shiftSize.height() );
306 }
307
308 painter.setClipRect( cr );
309
310 drawContents( &painter );
311
312 if ( !d_data->icon.isNull() )
313 {
314 QRect iconRect = cr;
315 iconRect.setX( iconRect.x() + margin() );
316 if ( d_data->itemMode != QwtLegendData::ReadOnly )
317 iconRect.setX( iconRect.x() + ButtonFrame );
318
319 iconRect.setSize( d_data->icon.size() );
320 iconRect.moveCenter( QPoint( iconRect.center().x(), cr.center().y() ) );
321
322 painter.drawPixmap( iconRect, d_data->icon );
323 }
324
325 painter.restore();
326}
327
328//! Handle mouse press events
329void QwtLegendLabel::mousePressEvent( QMouseEvent *e )
330{
331 if ( e->button() == Qt::LeftButton )
332 {
333 switch ( d_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
351//! Handle mouse release events
352void QwtLegendLabel::mouseReleaseEvent( QMouseEvent *e )
353{
354 if ( e->button() == Qt::LeftButton )
355 {
356 switch ( d_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
373//! Handle key press events
374void QwtLegendLabel::keyPressEvent( QKeyEvent *e )
375{
376 if ( e->key() == Qt::Key_Space )
377 {
378 switch ( d_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
399//! Handle key release events
400void QwtLegendLabel::keyReleaseEvent( QKeyEvent *e )
401{
402 if ( e->key() == Qt::Key_Space )
403 {
404 switch ( d_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}
Note: See TracBrowser for help on using the repository browser.