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

Last change on this file since 8703 was 8127, checked in by stoecker, 8 years ago

update qwt and qwtpolar, many QT5 fixes (unfinished)

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