source: ntrip/trunk/GnssCenter/qwt/qwt_legend_item.cpp@ 10455

Last change on this file since 10455 was 4839, checked in by mervart, 12 years ago
File size: 8.7 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_item.h"
11#include "qwt_math.h"
12#include "qwt_painter.h"
13#include "qwt_symbol.h"
14#include <qpainter.h>
15#include <qdrawutil.h>
16#include <qstyle.h>
17#include <qpen.h>
18#include <qevent.h>
19#include <qstyleoption.h>
20#include <qapplication.h>
21
22static const int ButtonFrame = 2;
23static const int Margin = 2;
24
25static QSize buttonShift( const QwtLegendItem *w )
26{
27 QStyleOption option;
28 option.init( 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 QwtLegendItem::PrivateData
38{
39public:
40 PrivateData():
41 itemMode( QwtLegend::ReadOnlyItem ),
42 isDown( false ),
43 identifierSize( 8, 8 ),
44 spacing( Margin )
45 {
46 }
47
48 QwtLegend::LegendItemMode itemMode;
49 bool isDown;
50
51 QSize identifierSize;
52 QPixmap identifier;
53
54 int spacing;
55};
56
57/*!
58 \param parent Parent widget
59*/
60QwtLegendItem::QwtLegendItem( QWidget *parent ):
61 QwtTextLabel( parent )
62{
63 d_data = new PrivateData;
64 setMargin( Margin );
65 setIndent( Margin + d_data->identifierSize.width() + 2 * d_data->spacing );
66}
67
68//! Destructor
69QwtLegendItem::~QwtLegendItem()
70{
71 delete d_data;
72 d_data = NULL;
73}
74
75/*!
76 Set the text to the legend item
77
78 \param text Text label
79 \sa QwtTextLabel::text()
80*/
81void QwtLegendItem::setText( const QwtText &text )
82{
83 const int flags = Qt::AlignLeft | Qt::AlignVCenter
84 | Qt::TextExpandTabs | Qt::TextWordWrap;
85
86 QwtText txt = text;
87 txt.setRenderFlags( flags );
88
89 QwtTextLabel::setText( txt );
90}
91
92/*!
93 Set the item mode
94 The default is QwtLegend::ReadOnlyItem
95
96 \param mode Item mode
97 \sa itemMode()
98*/
99void QwtLegendItem::setItemMode( QwtLegend::LegendItemMode mode )
100{
101 if ( mode != d_data->itemMode )
102 {
103 d_data->itemMode = mode;
104 d_data->isDown = false;
105
106 setFocusPolicy( mode != QwtLegend::ReadOnlyItem ? Qt::TabFocus : Qt::NoFocus );
107 setMargin( ButtonFrame + Margin );
108
109 updateGeometry();
110 }
111}
112
113/*!
114 Return the item mode
115
116 \sa setItemMode()
117*/
118QwtLegend::LegendItemMode QwtLegendItem::itemMode() const
119{
120 return d_data->itemMode;
121}
122
123/*!
124 Assign the identifier
125 The identifier needs to be created according to the identifierWidth()
126
127 \param identifier Pixmap representing a plot item
128
129 \sa identifier(), identifierWidth()
130*/
131void QwtLegendItem::setIdentifier( const QPixmap &identifier )
132{
133 d_data->identifier = identifier;
134 update();
135}
136
137/*!
138 \return pixmap representing a plot item
139 \sa setIdentifier()
140*/
141QPixmap QwtLegendItem::identifier() const
142{
143 return d_data->identifier;
144}
145
146/*!
147 Set the size for the identifier
148 Default is 8x8 pixels
149
150 \param size New size
151
152 \sa identifierSize()
153*/
154void QwtLegendItem::setIdentifierSize( const QSize &size )
155{
156 QSize sz = size.expandedTo( QSize( 0, 0 ) );
157 if ( sz != d_data->identifierSize )
158 {
159 d_data->identifierSize = sz;
160 setIndent( margin() + d_data->identifierSize.width()
161 + 2 * d_data->spacing );
162 updateGeometry();
163 }
164}
165/*!
166 Return the width of the identifier
167
168 \sa setIdentifierSize()
169*/
170QSize QwtLegendItem::identifierSize() const
171{
172 return d_data->identifierSize;
173}
174
175/*!
176 Change the spacing
177 \param spacing Spacing
178 \sa spacing(), identifierWidth(), QwtTextLabel::margin()
179*/
180void QwtLegendItem::setSpacing( int spacing )
181{
182 spacing = qMax( spacing, 0 );
183 if ( spacing != d_data->spacing )
184 {
185 d_data->spacing = spacing;
186 setIndent( margin() + d_data->identifierSize.width()
187 + 2 * d_data->spacing );
188 }
189}
190
191/*!
192 Return the spacing
193 \sa setSpacing(), identifierWidth(), QwtTextLabel::margin()
194*/
195int QwtLegendItem::spacing() const
196{
197 return d_data->spacing;
198}
199
200/*!
201 Check/Uncheck a the item
202
203 \param on check/uncheck
204 \sa setItemMode()
205*/
206void QwtLegendItem::setChecked( bool on )
207{
208 if ( d_data->itemMode == QwtLegend::CheckableItem )
209 {
210 const bool isBlocked = signalsBlocked();
211 blockSignals( true );
212
213 setDown( on );
214
215 blockSignals( isBlocked );
216 }
217}
218
219//! Return true, if the item is checked
220bool QwtLegendItem::isChecked() const
221{
222 return d_data->itemMode == QwtLegend::CheckableItem && isDown();
223}
224
225//! Set the item being down
226void QwtLegendItem::setDown( bool down )
227{
228 if ( down == d_data->isDown )
229 return;
230
231 d_data->isDown = down;
232 update();
233
234 if ( d_data->itemMode == QwtLegend::ClickableItem )
235 {
236 if ( d_data->isDown )
237 Q_EMIT pressed();
238 else
239 {
240 Q_EMIT released();
241 Q_EMIT clicked();
242 }
243 }
244
245 if ( d_data->itemMode == QwtLegend::CheckableItem )
246 Q_EMIT checked( d_data->isDown );
247}
248
249//! Return true, if the item is down
250bool QwtLegendItem::isDown() const
251{
252 return d_data->isDown;
253}
254
255//! Return a size hint
256QSize QwtLegendItem::sizeHint() const
257{
258 QSize sz = QwtTextLabel::sizeHint();
259 sz.setHeight( qMax( sz.height(), d_data->identifier.height() + 4 ) );
260
261 if ( d_data->itemMode != QwtLegend::ReadOnlyItem )
262 {
263 sz += buttonShift( this );
264 sz = sz.expandedTo( QApplication::globalStrut() );
265 }
266
267 return sz;
268}
269
270//! Paint event
271void QwtLegendItem::paintEvent( QPaintEvent *e )
272{
273 const QRect cr = contentsRect();
274
275 QPainter painter( this );
276 painter.setClipRegion( e->region() );
277
278 if ( d_data->isDown )
279 {
280 qDrawWinButton( &painter, 0, 0, width(), height(),
281 palette(), true );
282 }
283
284 painter.save();
285
286 if ( d_data->isDown )
287 {
288 const QSize shiftSize = buttonShift( this );
289 painter.translate( shiftSize.width(), shiftSize.height() );
290 }
291
292 painter.setClipRect( cr );
293
294 drawContents( &painter );
295
296 if ( !d_data->identifier.isNull() )
297 {
298 QRect identRect = cr;
299 identRect.setX( identRect.x() + margin() );
300 if ( d_data->itemMode != QwtLegend::ReadOnlyItem )
301 identRect.setX( identRect.x() + ButtonFrame );
302
303 identRect.setSize( d_data->identifier.size() );
304 identRect.moveCenter( QPoint( identRect.center().x(), cr.center().y() ) );
305
306 painter.drawPixmap( identRect, d_data->identifier );
307 }
308
309 painter.restore();
310}
311
312//! Handle mouse press events
313void QwtLegendItem::mousePressEvent( QMouseEvent *e )
314{
315 if ( e->button() == Qt::LeftButton )
316 {
317 switch ( d_data->itemMode )
318 {
319 case QwtLegend::ClickableItem:
320 {
321 setDown( true );
322 return;
323 }
324 case QwtLegend::CheckableItem:
325 {
326 setDown( !isDown() );
327 return;
328 }
329 default:;
330 }
331 }
332 QwtTextLabel::mousePressEvent( e );
333}
334
335//! Handle mouse release events
336void QwtLegendItem::mouseReleaseEvent( QMouseEvent *e )
337{
338 if ( e->button() == Qt::LeftButton )
339 {
340 switch ( d_data->itemMode )
341 {
342 case QwtLegend::ClickableItem:
343 {
344 setDown( false );
345 return;
346 }
347 case QwtLegend::CheckableItem:
348 {
349 return; // do nothing, but accept
350 }
351 default:;
352 }
353 }
354 QwtTextLabel::mouseReleaseEvent( e );
355}
356
357//! Handle key press events
358void QwtLegendItem::keyPressEvent( QKeyEvent *e )
359{
360 if ( e->key() == Qt::Key_Space )
361 {
362 switch ( d_data->itemMode )
363 {
364 case QwtLegend::ClickableItem:
365 {
366 if ( !e->isAutoRepeat() )
367 setDown( true );
368 return;
369 }
370 case QwtLegend::CheckableItem:
371 {
372 if ( !e->isAutoRepeat() )
373 setDown( !isDown() );
374 return;
375 }
376 default:;
377 }
378 }
379
380 QwtTextLabel::keyPressEvent( e );
381}
382
383//! Handle key release events
384void QwtLegendItem::keyReleaseEvent( QKeyEvent *e )
385{
386 if ( e->key() == Qt::Key_Space )
387 {
388 switch ( d_data->itemMode )
389 {
390 case QwtLegend::ClickableItem:
391 {
392 if ( !e->isAutoRepeat() )
393 setDown( false );
394 return;
395 }
396 case QwtLegend::CheckableItem:
397 {
398 return; // do nothing, but accept
399 }
400 default:;
401 }
402 }
403
404 QwtTextLabel::keyReleaseEvent( e );
405}
Note: See TracBrowser for help on using the repository browser.