source: ntrip/branches/BNC_2.11.0/qwtpolar/qwt_polar_item.cpp

Last change on this file was 4272, checked in by mervart, 12 years ago
File size: 10.0 KB
Line 
1/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2 * QwtPolar Widget Library
3 * Copyright (C) 2008 Uwe Rathmann
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the Qwt License, Version 1.0
7 *****************************************************************************/
8
9#include "qwt_polar_plot.h"
10#include "qwt_polar_item.h"
11#include <qwt_legend.h>
12#include <qwt_legend_item.h>
13#include <qwt_scale_div.h>
14#include <qpainter.h>
15
16class QwtPolarItem::PrivateData
17{
18public:
19 PrivateData():
20 plot( NULL ),
21 isVisible( true ),
22 attributes( 0 ),
23 renderHints( 0 ),
24 z( 0.0 )
25 {
26 }
27
28 mutable QwtPolarPlot *plot;
29
30 bool isVisible;
31 QwtPolarItem::ItemAttributes attributes;
32 QwtPolarItem::RenderHints renderHints;
33 double z;
34
35 QwtText title;
36};
37
38/*!
39 Constructor
40
41 \param title Item title, f.e used on a legend
42
43 \sa setTitle()
44*/
45QwtPolarItem::QwtPolarItem( const QwtText &title )
46{
47 d_data = new PrivateData;
48 d_data->title = title;
49}
50
51//! Destroy the QwtPolarItem
52QwtPolarItem::~QwtPolarItem()
53{
54 attach( NULL );
55 delete d_data;
56}
57
58/*!
59 \brief Attach the item to a plot.
60
61 This method will attach a QwtPolarItem to the QwtPolarPlot argument.
62 It will first detach the QwtPolarItem from any plot from a previous
63 call to attach (if necessary).
64 If a NULL argument is passed, it will detach from any QwtPolarPlot it
65 was attached to.
66
67 \param plot Plot widget
68
69 \sa QwtPolarItem::detach()
70*/
71void QwtPolarItem::attach( QwtPolarPlot *plot )
72{
73 if ( plot == d_data->plot )
74 return;
75
76 // remove the item from the previous plot
77
78 if ( d_data->plot )
79 {
80 if ( d_data->plot->legend() )
81 d_data->plot->legend()->remove( this );
82
83 d_data->plot->attachItem( this, false );
84
85 if ( d_data->plot->autoReplot() )
86 d_data->plot->update();
87 }
88
89 d_data->plot = plot;
90
91 if ( d_data->plot )
92 {
93 // insert the item into the current plot
94
95 d_data->plot->attachItem( this, true );
96 itemChanged();
97 }
98}
99
100/*!
101 Return rtti for the specific class represented. QwtPolarItem is simply
102 a virtual interface class, and base classes will implement this method
103 with specific rtti values so a user can differentiate them.
104
105 The rtti value is useful for environments, where the
106 runtime type information is disabled and it is not possible
107 to do a dynamic_cast<...>.
108
109 \return rtti value
110 \sa RttiValues
111*/
112int QwtPolarItem::rtti() const
113{
114 return Rtti_PolarItem;
115}
116
117//! \return Attached plot
118QwtPolarPlot *QwtPolarItem::plot() const
119{
120 return d_data->plot;
121}
122
123/*!
124 Plot items are painted in increasing z-order.
125
126 \return Z value
127 \sa setZ(), QwtPolarItemDict::itemList()
128*/
129double QwtPolarItem::z() const
130{
131 return d_data->z;
132}
133
134/*!
135 \brief Set the z value
136
137 Plot items are painted in increasing z-order.
138
139 \param z Z-value
140 \sa z(), QwtPolarItemDict::itemList()
141*/
142void QwtPolarItem::setZ( double z )
143{
144 if ( d_data->z != z )
145 {
146 if ( d_data->plot )
147 d_data->plot->attachItem( this, false );
148
149 d_data->z = z;
150
151 if ( d_data->plot )
152 d_data->plot->attachItem( this, true );
153
154 itemChanged();
155 }
156}
157
158/*!
159 Set a new title
160
161 \param title Title
162 \sa title()
163*/
164void QwtPolarItem::setTitle( const QString &title )
165{
166 setTitle( QwtText( title ) );
167}
168
169/*!
170 Set a new title
171
172 \param title Title
173 \sa title()
174*/
175void QwtPolarItem::setTitle( const QwtText &title )
176{
177 if ( d_data->title != title )
178 {
179 d_data->title = title;
180 itemChanged();
181 }
182}
183
184/*!
185 \return Title of the item
186 \sa setTitle()
187*/
188const QwtText &QwtPolarItem::title() const
189{
190 return d_data->title;
191}
192
193/*!
194 Toggle an item attribute
195
196 \param attribute Attribute type
197 \param on true/false
198
199 \sa testItemAttribute(), ItemAttribute
200*/
201void QwtPolarItem::setItemAttribute( ItemAttribute attribute, bool on )
202{
203 if ( bool( d_data->attributes & attribute ) != on )
204 {
205 if ( on )
206 d_data->attributes |= attribute;
207 else
208 d_data->attributes &= ~attribute;
209
210 itemChanged();
211 }
212}
213
214/*!
215 Test an item attribute
216
217 \param attribute Attribute type
218 \return true/false
219 \sa setItemAttribute(), ItemAttribute
220*/
221bool QwtPolarItem::testItemAttribute( ItemAttribute attribute ) const
222{
223 return d_data->attributes & attribute;
224}
225
226/*!
227 Toggle an render hint
228
229 \param hint Render hint
230 \param on true/false
231
232 \sa testRenderHint(), RenderHint
233*/
234void QwtPolarItem::setRenderHint( RenderHint hint, bool on )
235{
236 if ( ( ( d_data->renderHints & hint ) != 0 ) != on )
237 {
238 if ( on )
239 d_data->renderHints |= hint;
240 else
241 d_data->renderHints &= ~hint;
242
243 itemChanged();
244 }
245}
246
247/*!
248 Test a render hint
249
250 \param hint Render hint
251 \return true/false
252 \sa setRenderHint(), RenderHint
253*/
254bool QwtPolarItem::testRenderHint( RenderHint hint ) const
255{
256 return ( d_data->renderHints & hint );
257}
258
259//! Show the item
260void QwtPolarItem::show()
261{
262 setVisible( true );
263}
264
265//! Hide the item
266void QwtPolarItem::hide()
267{
268 setVisible( false );
269}
270
271/*!
272 Show/Hide the item
273
274 \param on Show if true, otherwise hide
275 \sa isVisible(), show(), hide()
276*/
277void QwtPolarItem::setVisible( bool on )
278{
279 if ( on != d_data->isVisible )
280 {
281 d_data->isVisible = on;
282 itemChanged();
283 }
284}
285
286/*!
287 \return true if visible
288 \sa setVisible(), show(), hide()
289*/
290bool QwtPolarItem::isVisible() const
291{
292 return d_data->isVisible;
293}
294
295/*!
296 Update the legend and call QwtPolarPlot::autoRefresh for the
297 parent plot.
298
299 \sa updateLegend()
300*/
301void QwtPolarItem::itemChanged()
302{
303 if ( d_data->plot )
304 {
305 if ( d_data->plot->legend() )
306 updateLegend( d_data->plot->legend() );
307
308 d_data->plot->autoRefresh();
309 }
310}
311
312/*!
313 Interval, that is necessary to display the item
314
315 This interval can be useful for operations like clipping or autoscaling
316 For items ( like the grid ), where a bounding interval makes no
317 sense an invalid interval is returned.
318
319 \param scaleId Scale id ( QwtPolar::Scale )
320 \return Bounding interval of the plot item for a specific scale
321*/
322QwtInterval QwtPolarItem::boundingInterval( int scaleId ) const
323{
324 Q_UNUSED( scaleId );
325
326 return QwtInterval(); // invalid
327}
328
329/*!
330 \brief Update the item to changes of the axes scale division
331
332 Update the item, when the axes of plot have changed.
333 The default implementation does nothing, but items that depend
334 on the scale division (like QwtPolarGrid()) have to reimplement
335 updateScaleDiv()
336
337 \param azimuthScaleDiv Scale division of the azimuth-scale
338 \param radialScaleDiv Scale division of the radius-axis
339 \param interval The interval of the radius-axis, that is
340 visible on the canvas
341
342 \sa QwtPolarPlot::updateAxes()
343*/
344void QwtPolarItem::updateScaleDiv( const QwtScaleDiv &azimuthScaleDiv,
345 const QwtScaleDiv &radialScaleDiv, const QwtInterval &interval )
346{
347 Q_UNUSED( azimuthScaleDiv );
348 Q_UNUSED( radialScaleDiv );
349 Q_UNUSED( interval );
350}
351
352/*!
353 \brief Update the widget that represents the item on the legend
354
355 updateLegend() is called from itemChanged() to adopt the widget
356 representing the item on the legend to its new configuration.
357
358 The default implementation is made for QwtPolarCurve and updates a
359 QwtLegendItem(), but an item could be represented by any type of widget,
360 by overloading legendItem() and updateLegend().
361
362 \sa legendItem(), itemChanged(), QwtLegend()
363*/
364void QwtPolarItem::updateLegend( QwtLegend *legend ) const
365{
366 if ( legend == NULL )
367 return;
368
369 QWidget *lgdItem = legend->find( this );
370 if ( testItemAttribute( QwtPolarItem::Legend ) )
371 {
372 if ( lgdItem == NULL )
373 {
374 lgdItem = legendItem();
375 if ( lgdItem )
376 legend->insert( this, lgdItem );
377 }
378
379 QwtLegendItem* label = qobject_cast<QwtLegendItem *>( lgdItem );
380 if ( label )
381 {
382 // paint the identifier
383 const QSize sz = label->identifierSize();
384
385 QPixmap identifier( sz.width(), sz.height() );
386 identifier.fill( Qt::transparent );
387
388 QPainter painter( &identifier );
389 painter.setRenderHint( QPainter::Antialiasing,
390 testRenderHint( QwtPolarItem::RenderAntialiased ) );
391
392 drawLegendIdentifier( &painter,
393 QRect( 0, 0, sz.width(), sz.height() ) );
394
395 painter.end();
396
397 const bool doUpdate = label->updatesEnabled();
398 if ( doUpdate )
399 label->setUpdatesEnabled( false );
400
401 label->setText( title() );
402 label->setIdentifier( identifier );
403 label->setItemMode( legend->itemMode() );
404
405 if ( doUpdate )
406 label->setUpdatesEnabled( true );
407
408 label->update();
409 }
410 }
411 else
412 {
413 if ( lgdItem )
414 {
415 lgdItem->hide();
416 lgdItem->deleteLater();
417 }
418 }
419}
420/*!
421 \brief Allocate the widget that represents the item on the legend
422
423 The default implementation is made for QwtPolarCurve and returns a
424 QwtLegendItem(), but an item could be represented by any type of widget,
425 by overloading legendItem() and updateLegend().
426
427 \return QwtLegendItem()
428 \sa updateLegend() QwtLegend()
429*/
430QWidget *QwtPolarItem::legendItem() const
431{
432 QwtLegendItem *item = new QwtLegendItem;
433 if ( d_data->plot )
434 {
435 QObject::connect( item, SIGNAL( clicked() ),
436 d_data->plot, SLOT( legendItemClicked() ) );
437 QObject::connect( item, SIGNAL( checked( bool ) ),
438 d_data->plot, SLOT( legendItemChecked( bool ) ) );
439 }
440 return item;
441}
442
443/*!
444 Some items like to display something (f.e. the azimuth axis) outside
445 of the area of the interval of the radial scale.
446 The default implementation returns 0 pixels
447
448 \return Hint for the margin
449*/
450int QwtPolarItem::marginHint() const
451{
452 return 0;
453}
Note: See TracBrowser for help on using the repository browser.