source: ntrip/trunk/BNC/qwt/qwt_plot_item.cpp@ 11158

Last change on this file since 11158 was 11158, checked in by stuerze, 3 weeks ago

deprecated functions exchanged libs updated

File size: 15.9 KB
Line 
1/******************************************************************************
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_plot_item.h"
11#include "qwt_text.h"
12#include "qwt_plot.h"
13#include "qwt_legend_data.h"
14#include "qwt_scale_map.h"
15#include "qwt_graphic.h"
16
17#include <qpainter.h>
18
19class QwtPlotItem::PrivateData
20{
21 public:
22 PrivateData()
23 : plot( NULL )
24 , isVisible( true )
25 , renderThreadCount( 1 )
26 , z( 0.0 )
27 , xAxisId( QwtAxis::XBottom )
28 , yAxisId( QwtAxis::YLeft )
29 , legendIconSize( 8, 8 )
30 {
31 }
32
33 mutable QwtPlot* plot;
34
35 bool isVisible;
36
37 QwtPlotItem::ItemAttributes attributes;
38 QwtPlotItem::ItemInterests interests;
39
40 QwtPlotItem::RenderHints renderHints;
41 uint renderThreadCount;
42
43 double z;
44
45 QwtAxisId xAxisId;
46 QwtAxisId yAxisId;
47
48 QwtText title;
49 QSize legendIconSize;
50};
51
52/*!
53 Constructor
54 */
55QwtPlotItem::QwtPlotItem()
56{
57 m_data = new PrivateData;
58}
59
60/*!
61 Constructor
62 \param title Title of the item
63 */
64QwtPlotItem::QwtPlotItem( const QString& title )
65{
66 m_data = new PrivateData;
67 m_data->title = title;
68}
69
70/*!
71 Constructor
72 \param title Title of the item
73 */
74QwtPlotItem::QwtPlotItem( const QwtText& title )
75{
76 m_data = new PrivateData;
77 m_data->title = title;
78}
79
80//! Destroy the QwtPlotItem
81QwtPlotItem::~QwtPlotItem()
82{
83 attach( NULL );
84 delete m_data;
85}
86
87/*!
88 \brief Attach the item to a plot.
89
90 This method will attach a QwtPlotItem to the QwtPlot argument. It will first
91 detach the QwtPlotItem from any plot from a previous call to attach (if
92 necessary). If a NULL argument is passed, it will detach from any QwtPlot it
93 was attached to.
94
95 \param plot Plot widget
96 \sa detach()
97 */
98void QwtPlotItem::attach( QwtPlot* plot )
99{
100 if ( plot == m_data->plot )
101 return;
102
103 if ( m_data->plot )
104 m_data->plot->attachItem( this, false );
105
106 m_data->plot = plot;
107
108 if ( m_data->plot )
109 m_data->plot->attachItem( this, true );
110}
111
112/*!
113 \brief This method detaches a QwtPlotItem from any
114 QwtPlot it has been associated with.
115
116 detach() is equivalent to calling attach( NULL )
117 \sa attach()
118 */
119void QwtPlotItem::detach()
120{
121 attach( NULL );
122}
123
124/*!
125 Return rtti for the specific class represented. QwtPlotItem is simply
126 a virtual interface class, and base classes will implement this method
127 with specific rtti values so a user can differentiate them.
128
129 The rtti value is useful for environments, where the
130 runtime type information is disabled and it is not possible
131 to do a dynamic_cast<...>.
132
133 \return rtti value
134 \sa RttiValues
135 */
136int QwtPlotItem::rtti() const
137{
138 return Rtti_PlotItem;
139}
140
141//! Return attached plot
142QwtPlot* QwtPlotItem::plot() const
143{
144 return m_data->plot;
145}
146
147/*!
148 Plot items are painted in increasing z-order.
149
150 \return setZ(), QwtPlotDict::itemList()
151 */
152double QwtPlotItem::z() const
153{
154 return m_data->z;
155}
156
157/*!
158 \brief Set the z value
159
160 Plot items are painted in increasing z-order.
161
162 \param z Z-value
163 \sa z(), QwtPlotDict::itemList()
164 */
165void QwtPlotItem::setZ( double z )
166{
167 if ( m_data->z != z )
168 {
169 if ( m_data->plot ) // update the z order
170 m_data->plot->attachItem( this, false );
171
172 m_data->z = z;
173
174 if ( m_data->plot )
175 m_data->plot->attachItem( this, true );
176
177 itemChanged();
178 }
179}
180
181/*!
182 Set a new title
183
184 \param title Title
185 \sa title()
186 */
187void QwtPlotItem::setTitle( const QString& title )
188{
189 setTitle( QwtText( title ) );
190}
191
192/*!
193 Set a new title
194
195 \param title Title
196 \sa title()
197 */
198void QwtPlotItem::setTitle( const QwtText& title )
199{
200 if ( m_data->title != title )
201 {
202 m_data->title = title;
203
204 legendChanged();
205#if 0
206 itemChanged();
207#endif
208 }
209}
210
211/*!
212 \return Title of the item
213 \sa setTitle()
214 */
215const QwtText& QwtPlotItem::title() const
216{
217 return m_data->title;
218}
219
220/*!
221 Toggle an item attribute
222
223 \param attribute Attribute type
224 \param on true/false
225
226 \sa testItemAttribute(), ItemInterest
227 */
228void QwtPlotItem::setItemAttribute( ItemAttribute attribute, bool on )
229{
230 if ( m_data->attributes.testFlag( attribute ) != on )
231 {
232 if ( on )
233 m_data->attributes |= attribute;
234 else
235 m_data->attributes &= ~attribute;
236
237 if ( attribute == QwtPlotItem::Legend )
238 {
239 if ( on )
240 {
241 legendChanged();
242 }
243 else
244 {
245 /*
246 In the special case of taking an item from
247 the legend we can't use legendChanged() as
248 it depends on QwtPlotItem::Legend being enabled
249 */
250 if ( m_data->plot )
251 m_data->plot->updateLegend( this );
252 }
253 }
254
255 itemChanged();
256 }
257}
258
259/*!
260 Test an item attribute
261
262 \param attribute Attribute type
263 \return true/false
264 \sa setItemAttribute(), ItemInterest
265 */
266bool QwtPlotItem::testItemAttribute( ItemAttribute attribute ) const
267{
268 return m_data->attributes.testFlag( attribute );
269}
270
271/*!
272 Toggle an item interest
273
274 \param interest Interest type
275 \param on true/false
276
277 \sa testItemInterest(), ItemAttribute
278 */
279void QwtPlotItem::setItemInterest( ItemInterest interest, bool on )
280{
281 if ( m_data->interests.testFlag( interest ) != on )
282 {
283 if ( on )
284 m_data->interests |= interest;
285 else
286 m_data->interests &= ~interest;
287
288 itemChanged();
289 }
290}
291
292/*!
293 Test an item interest
294
295 \param interest Interest type
296 \return true/false
297 \sa setItemInterest(), ItemAttribute
298 */
299bool QwtPlotItem::testItemInterest( ItemInterest interest ) const
300{
301 return m_data->interests.testFlag( interest );
302}
303
304/*!
305 Toggle an render hint
306
307 \param hint Render hint
308 \param on true/false
309
310 \sa testRenderHint(), RenderHint
311 */
312void QwtPlotItem::setRenderHint( RenderHint hint, bool on )
313{
314 if ( m_data->renderHints.testFlag( hint ) != on )
315 {
316 if ( on )
317 m_data->renderHints |= hint;
318 else
319 m_data->renderHints &= ~hint;
320
321 itemChanged();
322 }
323}
324
325/*!
326 Test a render hint
327
328 \param hint Render hint
329 \return true/false
330 \sa setRenderHint(), RenderHint
331 */
332bool QwtPlotItem::testRenderHint( RenderHint hint ) const
333{
334 return m_data->renderHints.testFlag( hint );
335}
336
337/*!
338 On multi core systems rendering of certain plot item
339 ( f.e QwtPlotRasterItem ) can be done in parallel in
340 several threads.
341
342 The default setting is set to 1.
343
344 \param numThreads Number of threads to be used for rendering.
345 If numThreads is set to 0, the system specific
346 ideal thread count is used.
347
348 The default thread count is 1 ( = no additional threads )
349 */
350void QwtPlotItem::setRenderThreadCount( uint numThreads )
351{
352 m_data->renderThreadCount = numThreads;
353}
354
355/*!
356 \return Number of threads to be used for rendering.
357 If numThreads() is set to 0, the system specific
358 ideal thread count is used.
359 */
360uint QwtPlotItem::renderThreadCount() const
361{
362 return m_data->renderThreadCount;
363}
364
365/*!
366 Set the size of the legend icon
367
368 The default setting is 8x8 pixels
369
370 \param size Size
371 \sa legendIconSize(), legendIcon()
372 */
373void QwtPlotItem::setLegendIconSize( const QSize& size )
374{
375 if ( m_data->legendIconSize != size )
376 {
377 m_data->legendIconSize = size;
378 legendChanged();
379 }
380}
381
382/*!
383 \return Legend icon size
384 \sa setLegendIconSize(), legendIcon()
385 */
386QSize QwtPlotItem::legendIconSize() const
387{
388 return m_data->legendIconSize;
389}
390
391/*!
392 \return Icon representing the item on the legend
393
394 The default implementation returns an invalid icon
395
396 \param index Index of the legend entry
397 ( usually there is only one )
398 \param size Icon size
399
400 \sa setLegendIconSize(), legendData()
401 */
402QwtGraphic QwtPlotItem::legendIcon(
403 int index, const QSizeF& size ) const
404{
405 Q_UNUSED( index )
406 Q_UNUSED( size )
407
408 return QwtGraphic();
409}
410
411/*!
412 \brief Return a default icon from a brush
413
414 The default icon is a filled rectangle used
415 in several derived classes as legendIcon().
416
417 \param brush Fill brush
418 \param size Icon size
419
420 \return A filled rectangle
421 */
422QwtGraphic QwtPlotItem::defaultIcon(
423 const QBrush& brush, const QSizeF& size ) const
424{
425 QwtGraphic icon;
426 if ( !size.isEmpty() )
427 {
428 icon.setDefaultSize( size );
429
430 QRectF r( 0, 0, size.width(), size.height() );
431
432 QPainter painter( &icon );
433 painter.fillRect( r, brush );
434 }
435
436 return icon;
437}
438
439//! Show the item
440void QwtPlotItem::show()
441{
442 setVisible( true );
443}
444
445//! Hide the item
446void QwtPlotItem::hide()
447{
448 setVisible( false );
449}
450
451/*!
452 Show/Hide the item
453
454 \param on Show if true, otherwise hide
455 \sa isVisible(), show(), hide()
456 */
457void QwtPlotItem::setVisible( bool on )
458{
459 if ( on != m_data->isVisible )
460 {
461 m_data->isVisible = on;
462 itemChanged();
463 }
464}
465
466/*!
467 \return true if visible
468 \sa setVisible(), show(), hide()
469 */
470bool QwtPlotItem::isVisible() const
471{
472 return m_data->isVisible;
473}
474
475/*!
476 Update the legend and call QwtPlot::autoRefresh() for the
477 parent plot.
478
479 \sa QwtPlot::legendChanged(), QwtPlot::autoRefresh()
480 */
481void QwtPlotItem::itemChanged()
482{
483 if ( m_data->plot )
484 m_data->plot->autoRefresh();
485}
486
487/*!
488 Update the legend of the parent plot.
489 \sa QwtPlot::updateLegend(), itemChanged()
490 */
491void QwtPlotItem::legendChanged()
492{
493 if ( testItemAttribute( QwtPlotItem::Legend ) && m_data->plot )
494 m_data->plot->updateLegend( this );
495}
496
497/*!
498 Set X and Y axis
499
500 The item will painted according to the coordinates of its Axes.
501
502 \param xAxisId X Axis
503 \param yAxisId Y Axis
504
505 \sa setXAxis(), setYAxis(), xAxis(), yAxis()
506 */
507void QwtPlotItem::setAxes( QwtAxisId xAxisId, QwtAxisId yAxisId )
508{
509 if ( QwtAxis::isXAxis( xAxisId ) )
510 m_data->xAxisId = xAxisId;
511
512 if ( QwtAxis::isYAxis( yAxisId ) )
513 m_data->yAxisId = yAxisId;
514
515 itemChanged();
516}
517
518/*!
519 Set the X axis
520
521 The item will painted according to the coordinates its Axes.
522
523 \param axisId X Axis
524 \sa setAxes(), setYAxis(), xAxis()
525 */
526void QwtPlotItem::setXAxis( QwtAxisId axisId )
527{
528 if ( QwtAxis::isXAxis( axisId ) )
529 {
530 m_data->xAxisId = axisId;
531 itemChanged();
532 }
533}
534
535/*!
536 Set the Y axis
537
538 The item will painted according to the coordinates its Axes.
539
540 \param axisId Y Axis
541 \sa setAxes(), setXAxis(), yAxis()
542 */
543void QwtPlotItem::setYAxis( QwtAxisId axisId )
544{
545 if ( QwtAxis::isYAxis( axisId ) )
546 {
547 m_data->yAxisId = axisId;
548 itemChanged();
549 }
550}
551
552//! Return xAxis
553QwtAxisId QwtPlotItem::xAxis() const
554{
555 return m_data->xAxisId;
556}
557
558//! Return yAxis
559QwtAxisId QwtPlotItem::yAxis() const
560{
561 return m_data->yAxisId;
562}
563
564/*!
565 \return An invalid bounding rect: QRectF(1.0, 1.0, -2.0, -2.0)
566 \note A width or height < 0.0 is ignored by the autoscaler
567 */
568QRectF QwtPlotItem::boundingRect() const
569{
570 return QRectF( 1.0, 1.0, -2.0, -2.0 ); // invalid
571}
572
573/*!
574 \brief Calculate a hint for the canvas margin
575
576 When the QwtPlotItem::Margins flag is enabled the plot item
577 indicates, that it needs some margins at the borders of the canvas.
578 This is f.e. used by bar charts to reserve space for displaying
579 the bars.
580
581 The margins are in target device coordinates ( pixels on screen )
582
583 \param xMap Maps x-values into pixel coordinates.
584 \param yMap Maps y-values into pixel coordinates.
585 \param canvasRect Contents rectangle of the canvas in painter coordinates
586 \param left Returns the left margin
587 \param top Returns the top margin
588 \param right Returns the right margin
589 \param bottom Returns the bottom margin
590
591 The default implementation returns 0 for all margins
592
593 \sa QwtPlot::getCanvasMarginsHint(), QwtPlot::updateCanvasMargins()
594 */
595void QwtPlotItem::getCanvasMarginHint( const QwtScaleMap& xMap,
596 const QwtScaleMap& yMap, const QRectF& canvasRect,
597 double& left, double& top, double& right, double& bottom ) const
598{
599 Q_UNUSED( xMap );
600 Q_UNUSED( yMap );
601 Q_UNUSED( canvasRect );
602
603 // use QMargins, when we don't need to support Qt < 4.6 anymore
604 left = top = right = bottom = 0.0;
605}
606
607/*!
608 \brief Return all information, that is needed to represent
609 the item on the legend
610
611 Most items are represented by one entry on the legend
612 showing an icon and a text, but f.e. QwtPlotMultiBarChart
613 displays one entry for each bar.
614
615 QwtLegendData is basically a list of QVariants that makes it
616 possible to overload and reimplement legendData() to
617 return almost any type of information, that is understood
618 by the receiver that acts as the legend.
619
620 The default implementation returns one entry with
621 the title() of the item and the legendIcon().
622
623 \return Data, that is needed to represent the item on the legend
624 \sa title(), legendIcon(), QwtLegend, QwtPlotLegendItem
625 */
626QList< QwtLegendData > QwtPlotItem::legendData() const
627{
628 QwtLegendData data;
629
630 QwtText label = title();
631 label.setRenderFlags( label.renderFlags() & Qt::AlignLeft );
632
633 data.setValue( QwtLegendData::TitleRole,
634 QVariant::fromValue( label ) );
635
636 const QwtGraphic graphic = legendIcon( 0, legendIconSize() );
637 if ( !graphic.isNull() )
638 {
639 data.setValue( QwtLegendData::IconRole,
640 QVariant::fromValue( graphic ) );
641 }
642
643 QList< QwtLegendData > list;
644 list += data;
645
646 return list;
647}
648
649/*!
650 \brief Update the item to changes of the axes scale division
651
652 Update the item, when the axes of plot have changed.
653 The default implementation does nothing, but items that depend
654 on the scale division (like QwtPlotGrid()) have to reimplement
655 updateScaleDiv()
656
657 updateScaleDiv() is only called when the ScaleInterest interest
658 is enabled. The default implementation does nothing.
659
660 \param xScaleDiv Scale division of the x-axis
661 \param yScaleDiv Scale division of the y-axis
662
663 \sa QwtPlot::updateAxes(), ScaleInterest
664 */
665void QwtPlotItem::updateScaleDiv( const QwtScaleDiv& xScaleDiv,
666 const QwtScaleDiv& yScaleDiv )
667{
668 Q_UNUSED( xScaleDiv );
669 Q_UNUSED( yScaleDiv );
670}
671
672/*!
673 \brief Update the item to changes of the legend info
674
675 Plot items that want to display a legend ( not those, that want to
676 be displayed on a legend ! ) will have to implement updateLegend().
677
678 updateLegend() is only called when the LegendInterest interest
679 is enabled. The default implementation does nothing.
680
681 \param item Plot item to be displayed on a legend
682 \param data Attributes how to display item on the legend
683
684 \sa QwtPlotLegendItem
685
686 \note Plot items, that want to be displayed on a legend
687 need to enable the QwtPlotItem::Legend flag and to implement
688 legendData() and legendIcon()
689 */
690void QwtPlotItem::updateLegend( const QwtPlotItem* item,
691 const QList< QwtLegendData >& data )
692{
693 Q_UNUSED( item );
694 Q_UNUSED( data );
695}
696
697/*!
698 \brief Calculate the bounding scale rectangle of 2 maps
699
700 \param xMap Maps x-values into pixel coordinates.
701 \param yMap Maps y-values into pixel coordinates.
702
703 \return Bounding scale rect of the scale maps, not normalized
704 */
705QRectF QwtPlotItem::scaleRect( const QwtScaleMap& xMap,
706 const QwtScaleMap& yMap ) const
707{
708 return QRectF( xMap.s1(), yMap.s1(),
709 xMap.sDist(), yMap.sDist() );
710}
711
712/*!
713 \brief Calculate the bounding paint rectangle of 2 maps
714
715 \param xMap Maps x-values into pixel coordinates.
716 \param yMap Maps y-values into pixel coordinates.
717
718 \return Bounding paint rectangle of the scale maps, not normalized
719 */
720QRectF QwtPlotItem::paintRect( const QwtScaleMap& xMap,
721 const QwtScaleMap& yMap ) const
722{
723 const QRectF rect( xMap.p1(), yMap.p1(),
724 xMap.pDist(), yMap.pDist() );
725
726 return rect;
727}
Note: See TracBrowser for help on using the repository browser.