Qwt User's Guide 6.3.0

Loading...
Searching...
No Matches
qwt_plot.cpp
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.h"
11#include "qwt_plot_dict.h"
12#include "qwt_plot_layout.h"
13#include "qwt_scale_widget.h"
14#include "qwt_scale_engine.h"
15#include "qwt_scale_map.h"
16#include "qwt_text_label.h"
17#include "qwt_legend.h"
18#include "qwt_legend_data.h"
19#include "qwt_plot_canvas.h"
20#include "qwt_math.h"
21
22#include <qpainter.h>
23#include <qpointer.h>
24#include <qapplication.h>
25#include <qcoreevent.h>
26
27 static inline void qwtEnableLegendItems( QwtPlot* plot, bool on )
28{
29 // gcc seems to have problems with const char sig[] in combination with certain options
30 const char* sig = SIGNAL(legendDataChanged(QVariant,QList<QwtLegendData>));
31 const char* slot = SLOT(updateLegendItems(QVariant,QList<QwtLegendData>));
32
33 if ( on )
34 QObject::connect( plot, sig, plot, slot );
35 else
36 QObject::disconnect( plot, sig, plot, slot );
37}
38
39 static void qwtSetTabOrder(
40 QWidget* first, QWidget* second, bool withChildren )
41{
42 QList< QWidget* > tabChain;
43 tabChain += first;
44 tabChain += second;
45
46 if ( withChildren )
47 {
48 QList< QWidget* > children = second->findChildren< QWidget* >();
49
50 QWidget* w = second->nextInFocusChain();
51 while ( children.contains( w ) )
52 {
53 children.removeAll( w );
54
55 tabChain += w;
56 w = w->nextInFocusChain();
57 }
58 }
59
60 for ( int i = 0; i < tabChain.size() - 1; i++ )
61 {
62 QWidget* from = tabChain[i];
63 QWidget* to = tabChain[i + 1];
64
65 const Qt::FocusPolicy policy1 = from->focusPolicy();
66 const Qt::FocusPolicy policy2 = to->focusPolicy();
67
68 QWidget* proxy1 = from->focusProxy();
69 QWidget* proxy2 = to->focusProxy();
70
71 from->setFocusPolicy( Qt::TabFocus );
72 from->setFocusProxy( NULL);
73
74 to->setFocusPolicy( Qt::TabFocus );
75 to->setFocusProxy( NULL);
76
77 QWidget::setTabOrder( from, to );
78
79 from->setFocusPolicy( policy1 );
80 from->setFocusProxy( proxy1);
81
82 to->setFocusPolicy( policy2 );
83 to->setFocusProxy( proxy2 );
84 }
85}
86
87 class QwtPlot::PrivateData
88{
89 public:
90 QPointer< QwtTextLabel > titleLabel;
91 QPointer< QwtTextLabel > footerLabel;
92 QPointer< QWidget > canvas;
93 QPointer< QwtAbstractLegend > legend;
94 QwtPlotLayout* layout;
95
96 bool autoReplot;
97};
98
103 QwtPlot::QwtPlot( QWidget* parent )
104 : QFrame( parent )
105{
106 initPlot( QwtText() );
107}
108
114 QwtPlot::QwtPlot( const QwtText& title, QWidget* parent )
115 : QFrame( parent )
116{
117 initPlot( title );
118}
119
121 QwtPlot::~QwtPlot()
122{
123 setAutoReplot( false );
124 detachItems( QwtPlotItem::Rtti_PlotItem, autoDelete() );
125
126 delete m_data->layout;
127 deleteAxesData();
128 delete m_data;
129}
130
135 void QwtPlot::initPlot( const QwtText& title )
136{
137 m_data = new PrivateData;
138
139 m_data->layout = new QwtPlotLayout;
140 m_data->autoReplot = false;
141
142 // title
143 m_data->titleLabel = new QwtTextLabel( this );
144 m_data->titleLabel->setObjectName( "QwtPlotTitle" );
145 m_data->titleLabel->setFont( QFont( fontInfo().family(), 14, QFont::Bold ) );
146
147 QwtText text( title );
148 text.setRenderFlags( Qt::AlignCenter | Qt::TextWordWrap );
149 m_data->titleLabel->setText( text );
150
151 // footer
152 m_data->footerLabel = new QwtTextLabel( this );
153 m_data->footerLabel->setObjectName( "QwtPlotFooter" );
154
155 QwtText footer;
156 footer.setRenderFlags( Qt::AlignCenter | Qt::TextWordWrap );
157 m_data->footerLabel->setText( footer );
158
159 // legend
160 m_data->legend = NULL;
161
162 // axes
163 initAxesData();
164
165 // canvas
166 m_data->canvas = new QwtPlotCanvas( this );
167 m_data->canvas->setObjectName( "QwtPlotCanvas" );
168 m_data->canvas->installEventFilter( this );
169
170 setSizePolicy( QSizePolicy::MinimumExpanding,
171 QSizePolicy::MinimumExpanding );
172
173 resize( 200, 200 );
174
175 using namespace QwtAxis;
176
177 QList< QWidget* > focusChain;
178 focusChain << this << m_data->titleLabel << axisWidget( XTop )
179 << axisWidget( YLeft ) << m_data->canvas
180 << axisWidget( YRight ) << axisWidget( XBottom )
181 << m_data->footerLabel;
182
183 for ( int i = 0; i < focusChain.size() - 1; i++ )
184 qwtSetTabOrder( focusChain[i], focusChain[i + 1], false );
185
186 qwtEnableLegendItems( this, true );
187}
188
213 void QwtPlot::setCanvas( QWidget* canvas )
214{
215 if ( canvas == m_data->canvas )
216 return;
217
218 delete m_data->canvas;
219 m_data->canvas = canvas;
220
221 if ( canvas )
222 {
223 canvas->setParent( this );
224 canvas->installEventFilter( this );
225
226 if ( isVisible() )
227 canvas->show();
228 }
229}
230
237 bool QwtPlot::event( QEvent* event )
238{
239 bool ok = QFrame::event( event );
240 switch ( event->type() )
241 {
242 case QEvent::LayoutRequest:
243 updateLayout();
244 break;
245 case QEvent::PolishRequest:
246 replot();
247 break;
248 default:;
249 }
250 return ok;
251}
252
271 bool QwtPlot::eventFilter( QObject* object, QEvent* event )
272{
273 if ( object == m_data->canvas )
274 {
275 if ( event->type() == QEvent::Resize )
276 {
277 updateCanvasMargins();
278 }
279 else if ( event->type() == QEvent::ContentsRectChange )
280 {
281 updateLayout();
282 }
283 }
284
285 return QFrame::eventFilter( object, event );
286}
287
289 void QwtPlot::autoRefresh()
290{
291 if ( m_data->autoReplot )
292 replot();
293}
294
310 void QwtPlot::setAutoReplot( bool tf )
311{
312 m_data->autoReplot = tf;
313}
314
319 bool QwtPlot::autoReplot() const
320{
321 return m_data->autoReplot;
322}
323
328 void QwtPlot::setTitle( const QString& title )
329{
330 if ( title != m_data->titleLabel->text().text() )
331 {
332 m_data->titleLabel->setText( title );
333 updateLayout();
334 }
335}
336
341 void QwtPlot::setTitle( const QwtText& title )
342{
343 if ( title != m_data->titleLabel->text() )
344 {
345 m_data->titleLabel->setText( title );
346 updateLayout();
347 }
348}
349
351 QwtText QwtPlot::title() const
352{
353 return m_data->titleLabel->text();
354}
355
357 QwtTextLabel* QwtPlot::titleLabel()
358{
359 return m_data->titleLabel;
360}
361
363 const QwtTextLabel* QwtPlot::titleLabel() const
364{
365 return m_data->titleLabel;
366}
367
372 void QwtPlot::setFooter( const QString& text )
373{
374 if ( text != m_data->footerLabel->text().text() )
375 {
376 m_data->footerLabel->setText( text );
377 updateLayout();
378 }
379}
380
385 void QwtPlot::setFooter( const QwtText& text )
386{
387 if ( text != m_data->footerLabel->text() )
388 {
389 m_data->footerLabel->setText( text );
390 updateLayout();
391 }
392}
393
395 QwtText QwtPlot::footer() const
396{
397 return m_data->footerLabel->text();
398}
399
401 QwtTextLabel* QwtPlot::footerLabel()
402{
403 return m_data->footerLabel;
404}
405
407 const QwtTextLabel* QwtPlot::footerLabel() const
408{
409 return m_data->footerLabel;
410}
411
418 void QwtPlot::setPlotLayout( QwtPlotLayout* layout )
419{
420 if ( layout != m_data->layout )
421 {
422 delete m_data->layout;
423 m_data->layout = layout;
424
425 updateLayout();
426 }
427}
428
430 QwtPlotLayout* QwtPlot::plotLayout()
431{
432 return m_data->layout;
433}
434
436 const QwtPlotLayout* QwtPlot::plotLayout() const
437{
438 return m_data->layout;
439}
440
445 QwtAbstractLegend* QwtPlot::legend()
446{
447 return m_data->legend;
448}
449
454 const QwtAbstractLegend* QwtPlot::legend() const
455{
456 return m_data->legend;
457}
458
459
463QWidget* QwtPlot::canvas()
464{
465 return m_data->canvas;
466}
467
471 const QWidget* QwtPlot::canvas() const
472{
473 return m_data->canvas;
474}
475
480QSize QwtPlot::sizeHint() const
481{
482 int dw = 0;
483 int dh = 0;
484
485 for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ )
486 {
487 {
488 const QwtAxisId axisId( axisPos );
489
490 if ( isAxisVisible( axisId ) )
491 {
492 const int niceDist = 40;
493 const QwtScaleWidget* scaleWidget = axisWidget( axisId );
494 const QwtScaleDiv& scaleDiv = scaleWidget->scaleDraw()->scaleDiv();
495 const int majCnt = scaleDiv.ticks( QwtScaleDiv::MajorTick ).count();
496
497 const QSize hint = scaleWidget->minimumSizeHint();
498
499 if ( QwtAxis::isYAxis( axisPos ) )
500 {
501 const int hDiff = ( majCnt - 1 ) * niceDist - hint.height();
502 dh = qMax( dh, hDiff );
503 }
504 else
505 {
506 const int wDiff = ( majCnt - 1 ) * niceDist - hint.width();
507 dw = qMax( dw, wDiff );
508 }
509 }
510 }
511 }
512 return minimumSizeHint() + QSize( dw, dh );
513}
514
518QSize QwtPlot::minimumSizeHint() const
519{
520 QSize hint = m_data->layout->minimumSizeHint( this );
521 hint += QSize( 2 * frameWidth(), 2 * frameWidth() );
522
523 return hint;
524}
525
530 void QwtPlot::resizeEvent( QResizeEvent* e )
531{
532 QFrame::resizeEvent( e );
533 updateLayout();
534}
535
545 void QwtPlot::replot()
546{
547 bool doAutoReplot = autoReplot();
548 setAutoReplot( false );
549
550 updateAxes();
551
552 /*
553 Maybe the layout needs to be updated, because of changed
554 axes labels. We need to process them here before painting
555 to avoid that scales and canvas get out of sync.
556 */
557 QApplication::sendPostedEvents( this, QEvent::LayoutRequest );
558
559 if ( m_data->canvas )
560 {
561 const bool ok = QMetaObject::invokeMethod(
562 m_data->canvas, "replot", Qt::DirectConnection );
563 if ( !ok )
564 {
565 // fallback, when canvas has no a replot method
566 m_data->canvas->update( m_data->canvas->contentsRect() );
567 }
568 }
569
570 setAutoReplot( doAutoReplot );
571}
572
577 void QwtPlot::updateLayout()
578{
579 QwtPlotLayout* layout = m_data->layout;
580 layout->activate( this, contentsRect() );
581
582 const QRect titleRect = layout->titleRect().toRect();
583 const QRect footerRect = layout->footerRect().toRect();
584 const QRect legendRect = layout->legendRect().toRect();
585 const QRect canvasRect = layout->canvasRect().toRect();
586
587 // resize and show the visible widgets
588
589 if ( !m_data->titleLabel->text().isEmpty() )
590 {
591 m_data->titleLabel->setGeometry( titleRect );
592 if ( !m_data->titleLabel->isVisibleTo( this ) )
593 m_data->titleLabel->show();
594 }
595 else
596 m_data->titleLabel->hide();
597
598 if ( !m_data->footerLabel->text().isEmpty() )
599 {
600 m_data->footerLabel->setGeometry( footerRect );
601 if ( !m_data->footerLabel->isVisibleTo( this ) )
602 m_data->footerLabel->show();
603 }
604 else
605 {
606 m_data->footerLabel->hide();
607 }
608
609 for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ )
610 {
611 {
612 const QwtAxisId axisId( axisPos );
613
614 QwtScaleWidget* scaleWidget = axisWidget( axisId );
615
616 if ( isAxisVisible( axisId ) )
617 {
618 const QRect scaleRect = layout->scaleRect( axisId ).toRect();
619
620 if ( scaleRect != scaleWidget->geometry() )
621 {
622 scaleWidget->setGeometry( scaleRect );
623
624 int startDist, endDist;
625 scaleWidget->getBorderDistHint( startDist, endDist );
626 scaleWidget->setBorderDist( startDist, endDist );
627 }
628
629 if ( !scaleWidget->isVisibleTo( this ) )
630 scaleWidget->show();
631 }
632 else
633 {
634 scaleWidget->hide();
635 }
636 }
637 }
638
639 if ( m_data->legend )
640 {
641 if ( m_data->legend->isEmpty() )
642 {
643 m_data->legend->hide();
644 }
645 else
646 {
647 m_data->legend->setGeometry( legendRect );
648 m_data->legend->show();
649 }
650 }
651
652 m_data->canvas->setGeometry( canvasRect );
653}
654
670 void QwtPlot::getCanvasMarginsHint(
671 const QwtScaleMap maps[], const QRectF& canvasRect,
672 double& left, double& top, double& right, double& bottom) const
673{
674 left = top = right = bottom = -1.0;
675
676 const QwtPlotItemList& itmList = itemList();
677 for ( QwtPlotItemIterator it = itmList.begin();
678 it != itmList.end(); ++it )
679 {
680 const QwtPlotItem* item = *it;
681 if ( item->testItemAttribute( QwtPlotItem::Margins ) )
682 {
683 using namespace QwtAxis;
684
685 double m[ AxisPositions ];
686 item->getCanvasMarginHint(
687 maps[ item->xAxis() ], maps[ item->yAxis() ],
688 canvasRect, m[YLeft], m[XTop], m[YRight], m[XBottom] );
689
690 left = qwtMaxF( left, m[YLeft] );
691 top = qwtMaxF( top, m[XTop] );
692 right = qwtMaxF( right, m[YRight] );
693 bottom = qwtMaxF( bottom, m[XBottom] );
694 }
695 }
696}
697
706 void QwtPlot::updateCanvasMargins()
707{
708 using namespace QwtAxis;
709
710 QwtScaleMap maps[ AxisPositions ];
711 for ( int axisId = 0; axisId < AxisPositions; axisId++ )
712 maps[axisId] = canvasMap( axisId );
713
714 double margins[AxisPositions];
715 getCanvasMarginsHint( maps, canvas()->contentsRect(),
716 margins[YLeft], margins[XTop], margins[YRight], margins[XBottom] );
717
718 bool doUpdate = false;
719 for ( int axisPos = 0; axisPos < AxisPositions; axisPos++ )
720 {
721 if ( margins[axisPos] >= 0.0 )
722 {
723 const int m = qwtCeil( margins[axisPos] );
724 plotLayout()->setCanvasMargin( m, axisPos);
725 doUpdate = true;
726 }
727 }
728
729 if ( doUpdate )
730 updateLayout();
731}
732
742 void QwtPlot::drawCanvas( QPainter* painter )
743{
744 QwtScaleMap maps[ QwtAxis::AxisPositions ];
745 for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ )
746 maps[axisPos] = canvasMap( axisPos );
747
748 drawItems( painter, m_data->canvas->contentsRect(), maps );
749}
750
764 void QwtPlot::drawItems( QPainter* painter, const QRectF& canvasRect,
765 const QwtScaleMap maps[ QwtAxis::AxisPositions ] ) const
766{
767 const QwtPlotItemList& itmList = itemList();
768 for ( QwtPlotItemIterator it = itmList.begin();
769 it != itmList.end(); ++it )
770 {
771 QwtPlotItem* item = *it;
772 if ( item && item->isVisible() )
773 {
774 const QwtAxisId xAxis = item->xAxis();
775 const QwtAxisId yAxis = item->yAxis();
776
777 painter->save();
778
779 painter->setRenderHint( QPainter::Antialiasing,
780 item->testRenderHint( QwtPlotItem::RenderAntialiased ) );
781
782#if QT_VERSION < 0x050100
783 painter->setRenderHint( QPainter::HighQualityAntialiasing,
784 item->testRenderHint( QwtPlotItem::RenderAntialiased ) );
785#endif
786
787 item->draw( painter, maps[xAxis], maps[yAxis], canvasRect );
788
789 painter->restore();
790 }
791 }
792}
793
800 QwtScaleMap QwtPlot::canvasMap( QwtAxisId axisId ) const
801{
802 QwtScaleMap map;
803 if ( !m_data->canvas )
804 return map;
805
806 map.setTransformation( axisScaleEngine( axisId )->transformation() );
807
808 const QwtScaleDiv& sd = axisScaleDiv( axisId );
809 map.setScaleInterval( sd.lowerBound(), sd.upperBound() );
810
811 if ( isAxisVisible( axisId ) )
812 {
813 const QwtScaleWidget* s = axisWidget( axisId );
814 if ( QwtAxis::isYAxis( axisId ) )
815 {
816 double y = s->y() + s->startBorderDist() - m_data->canvas->y();
817 double h = s->height() - s->startBorderDist() - s->endBorderDist();
818 map.setPaintInterval( y + h, y );
819 }
820 else
821 {
822 double x = s->x() + s->startBorderDist() - m_data->canvas->x();
823 double w = s->width() - s->startBorderDist() - s->endBorderDist();
824 map.setPaintInterval( x, x + w );
825 }
826 }
827 else
828 {
829 using namespace QwtAxis;
830
831 const QRect& canvasRect = m_data->canvas->contentsRect();
832 if ( isYAxis( axisId ) )
833 {
834 int top = 0;
835 if ( !plotLayout()->alignCanvasToScale( XTop ) )
836 top = plotLayout()->canvasMargin( XTop );
837
838 int bottom = 0;
839 if ( !plotLayout()->alignCanvasToScale( XBottom ) )
840 bottom = plotLayout()->canvasMargin( XBottom );
841
842 map.setPaintInterval( canvasRect.bottom() - bottom,
843 canvasRect.top() + top );
844 }
845 else
846 {
847 int left = 0;
848 if ( !plotLayout()->alignCanvasToScale( YLeft ) )
849 left = plotLayout()->canvasMargin( YLeft );
850
851 int right = 0;
852 if ( !plotLayout()->alignCanvasToScale( YRight ) )
853 right = plotLayout()->canvasMargin( YRight );
854
855 map.setPaintInterval( canvasRect.left() + left,
856 canvasRect.right() - right );
857 }
858 }
859
860 return map;
861}
862
873 void QwtPlot::setCanvasBackground( const QBrush& brush )
874{
875 QPalette pal = m_data->canvas->palette();
876 pal.setBrush( QPalette::Window, brush );
877
878 canvas()->setPalette( pal );
879}
880
888QBrush QwtPlot::canvasBackground() const
889{
890 return canvas()->palette().brush(
891 QPalette::Normal, QPalette::Window );
892}
893
927 void QwtPlot::insertLegend( QwtAbstractLegend* legend,
928 QwtPlot::LegendPosition pos, double ratio )
929{
930 m_data->layout->setLegendPosition( pos, ratio );
931
932 if ( legend != m_data->legend )
933 {
934 if ( m_data->legend && m_data->legend->parent() == this )
935 delete m_data->legend;
936
937 m_data->legend = legend;
938
939 if ( m_data->legend )
940 {
941 connect(
942 this, SIGNAL(legendDataChanged(QVariant,QList<QwtLegendData>)),
943 m_data->legend, SLOT(updateLegend(QVariant,QList<QwtLegendData>))
944 );
945
946 if ( m_data->legend->parent() != this )
947 m_data->legend->setParent( this );
948
949 qwtEnableLegendItems( this, false );
950 updateLegend();
951 qwtEnableLegendItems( this, true );
952
953 QwtLegend* lgd = qobject_cast< QwtLegend* >( legend );
954 if ( lgd )
955 {
956 switch ( m_data->layout->legendPosition() )
957 {
958 case LeftLegend:
959 case RightLegend:
960 {
961 if ( lgd->maxColumns() == 0 )
962 lgd->setMaxColumns( 1 ); // 1 column: align vertical
963 break;
964 }
965 case TopLegend:
966 case BottomLegend:
967 {
968 lgd->setMaxColumns( 0 ); // unlimited
969 break;
970 }
971 default:
972 break;
973 }
974 }
975
976 QWidget* previousInChain = NULL;
977 switch ( m_data->layout->legendPosition() )
978 {
979 case LeftLegend:
980 {
981 const QwtAxisId axisId( QwtAxis::XTop );
982 previousInChain = axisWidget( axisId );
983 break;
984 }
985 case TopLegend:
986 {
987 previousInChain = this;
988 break;
989 }
990 case RightLegend:
991 {
992 const QwtAxisId axisId( QwtAxis::YRight );
993 previousInChain = axisWidget( axisId );
994 break;
995 }
996 case BottomLegend:
997 {
998 previousInChain = footerLabel();
999 break;
1000 }
1001 }
1002
1003 if ( previousInChain )
1004 qwtSetTabOrder( previousInChain, legend, true );
1005 }
1006 }
1007
1008 updateLayout();
1009}
1010
1016 void QwtPlot::updateLegend()
1017{
1018 const QwtPlotItemList& itmList = itemList();
1019 for ( QwtPlotItemIterator it = itmList.begin();
1020 it != itmList.end(); ++it )
1021 {
1022 updateLegend( *it );
1023 }
1024}
1025
1032 void QwtPlot::updateLegend( const QwtPlotItem* plotItem )
1033{
1034 if ( plotItem == NULL )
1035 return;
1036
1037 QList< QwtLegendData > legendData;
1038
1039 if ( plotItem->testItemAttribute( QwtPlotItem::Legend ) )
1040 legendData = plotItem->legendData();
1041
1042 const QVariant itemInfo = itemToInfo( const_cast< QwtPlotItem* >( plotItem ) );
1043 Q_EMIT legendDataChanged( itemInfo, legendData );
1044}
1045
1058 void QwtPlot::updateLegendItems( const QVariant& itemInfo,
1059 const QList< QwtLegendData >& legendData )
1060{
1061 QwtPlotItem* plotItem = infoToItem( itemInfo );
1062 if ( plotItem )
1063 {
1064 const QwtPlotItemList& itmList = itemList();
1065 for ( QwtPlotItemIterator it = itmList.begin();
1066 it != itmList.end(); ++it )
1067 {
1068 QwtPlotItem* item = *it;
1069 if ( item->testItemInterest( QwtPlotItem::LegendInterest ) )
1070 item->updateLegend( plotItem, legendData );
1071 }
1072 }
1073}
1074
1081 void QwtPlot::attachItem( QwtPlotItem* plotItem, bool on )
1082{
1083 if ( plotItem->testItemInterest( QwtPlotItem::LegendInterest ) )
1084 {
1085 // plotItem is some sort of legend
1086
1087 const QwtPlotItemList& itmList = itemList();
1088 for ( QwtPlotItemIterator it = itmList.begin();
1089 it != itmList.end(); ++it )
1090 {
1091 QwtPlotItem* item = *it;
1092
1093 QList< QwtLegendData > legendData;
1094 if ( on && item->testItemAttribute( QwtPlotItem::Legend ) )
1095 {
1096 legendData = item->legendData();
1097 plotItem->updateLegend( item, legendData );
1098 }
1099 }
1100 }
1101
1102 if ( on )
1103 insertItem( plotItem );
1104 else
1105 removeItem( plotItem );
1106
1107 Q_EMIT itemAttached( plotItem, on );
1108
1109 if ( plotItem->testItemAttribute( QwtPlotItem::Legend ) )
1110 {
1111 // the item wants to be represented on the legend
1112
1113 if ( on )
1114 {
1115 updateLegend( plotItem );
1116 }
1117 else
1118 {
1119 const QVariant itemInfo = itemToInfo( plotItem );
1120 Q_EMIT legendDataChanged( itemInfo, QList< QwtLegendData >() );
1121 }
1122 }
1123
1124 autoRefresh();
1125}
1126
1139QVariant QwtPlot::itemToInfo( QwtPlotItem* plotItem ) const
1140{
1141 return QVariant::fromValue( plotItem );
1142}
1143
1159 QwtPlotItem* QwtPlot::infoToItem( const QVariant& itemInfo ) const
1160{
1161 if ( itemInfo.canConvert< QwtPlotItem* >() )
1162 return qvariant_cast< QwtPlotItem* >( itemInfo );
1163
1164 return NULL;
1165}
1166
1167#include "moc_qwt_plot.cpp"
QList
Definition qwt_abstract_legend.h:17
QwtAbstractLegend
Abstract base class for legend widgets.
Definition qwt_abstract_legend.h:35
QwtAbstractScaleDraw::scaleDiv
const QwtScaleDiv & scaleDiv() const
Definition qwt_abstract_scale_draw.cpp:133
QwtLegend
The legend widget.
Definition qwt_legend.h:32
QwtLegend::maxColumns
uint maxColumns() const
Definition qwt_legend.cpp:310
QwtLegend::setMaxColumns
void setMaxColumns(uint numColums)
Set the maximum number of entries in a row.
Definition qwt_legend.cpp:296
QwtPlotCanvas
Canvas of a QwtPlot.
Definition qwt_plot_canvas.h:30
QwtPlotDict::insertItem
void insertItem(QwtPlotItem *)
Definition qwt_plot_dict.cpp:115
QwtPlotDict::itemList
const QwtPlotItemList & itemList() const
A QwtPlotItemList of all attached plot items.
Definition qwt_plot_dict.cpp:166
QwtPlotDict::autoDelete
bool autoDelete() const
Definition qwt_plot_dict.cpp:104
QwtPlotDict::removeItem
void removeItem(QwtPlotItem *)
Definition qwt_plot_dict.cpp:126
QwtPlotDict::detachItems
void detachItems(int rtti=QwtPlotItem::Rtti_PlotItem, bool autoDelete=true)
Definition qwt_plot_dict.cpp:138
QwtPlot
A 2-D plotting widget.
Definition qwt_plot.h:79
QwtPlot::insertLegend
void insertLegend(QwtAbstractLegend *, LegendPosition=QwtPlot::RightLegend, double ratio=-1.0)
Insert a legend.
Definition qwt_plot.cpp:927
QwtPlot::eventFilter
virtual bool eventFilter(QObject *, QEvent *) override
Event filter.
Definition qwt_plot.cpp:271
QwtPlot::setPlotLayout
void setPlotLayout(QwtPlotLayout *)
Assign a new plot layout.
Definition qwt_plot.cpp:418
QwtPlot::itemAttached
void itemAttached(QwtPlotItem *plotItem, bool on)
QwtPlot::updateAxes
void updateAxes()
Rebuild the axes scales.
Definition qwt_plot_axis.cpp:670
QwtPlot::~QwtPlot
virtual ~QwtPlot()
Destructor.
Definition qwt_plot.cpp:121
QwtPlot::setFooter
void setFooter(const QString &)
Definition qwt_plot.cpp:372
QwtPlot::LegendPosition
LegendPosition
Definition qwt_plot.h:94
QwtPlot::TopLegend
@ TopLegend
The legend will be above the title.
Definition qwt_plot.h:105
QwtPlot::LeftLegend
@ LeftLegend
The legend will be left from the QwtAxis::YLeft axis.
Definition qwt_plot.h:96
QwtPlot::RightLegend
@ RightLegend
The legend will be right from the QwtAxis::YRight axis.
Definition qwt_plot.h:99
QwtPlot::BottomLegend
@ BottomLegend
The legend will be below the footer.
Definition qwt_plot.h:102
QwtPlot::getCanvasMarginsHint
virtual void getCanvasMarginsHint(const QwtScaleMap maps[], const QRectF &canvasRect, double &left, double &top, double &right, double &bottom) const
Calculate the canvas margins.
Definition qwt_plot.cpp:670
QwtPlot::axisScaleEngine
QwtScaleEngine * axisScaleEngine(QwtAxisId)
Definition qwt_plot_axis.cpp:190
QwtPlot::autoReplot
bool autoReplot() const
Definition qwt_plot.cpp:319
QwtPlot::itemToInfo
virtual QVariant itemToInfo(QwtPlotItem *) const
Build an information, that can be used to identify a plot item on the legend.
Definition qwt_plot.cpp:1139
QwtPlot::drawItems
virtual void drawItems(QPainter *, const QRectF &, const QwtScaleMap maps[QwtAxis::AxisPositions]) const
Definition qwt_plot.cpp:764
QwtPlot::event
virtual bool event(QEvent *) override
Adds handling of layout requests.
Definition qwt_plot.cpp:237
QwtPlot::canvas
QWidget * canvas()
Definition qwt_plot.cpp:463
QwtPlot::footerLabel
QwtTextLabel * footerLabel()
Definition qwt_plot.cpp:401
QwtPlot::isAxisVisible
bool isAxisVisible(QwtAxisId) const
Definition qwt_plot_axis.cpp:225
QwtPlot::replot
virtual void replot()
Redraw the plot.
Definition qwt_plot.cpp:545
QwtPlot::plotLayout
QwtPlotLayout * plotLayout()
Definition qwt_plot.cpp:430
QwtPlot::setTitle
void setTitle(const QString &)
Definition qwt_plot.cpp:328
QwtPlot::updateLegend
void updateLegend()
Definition qwt_plot.cpp:1016
QwtPlot::minimumSizeHint
virtual QSize minimumSizeHint() const override
Return a minimum size hint.
Definition qwt_plot.cpp:518
QwtPlot::infoToItem
virtual QwtPlotItem * infoToItem(const QVariant &) const
Identify the plot item according to an item info object, that has bee generated from itemToInfo().
Definition qwt_plot.cpp:1159
QwtPlot::canvasBackground
QBrush canvasBackground() const
Definition qwt_plot.cpp:888
QwtPlot::setAutoReplot
void setAutoReplot(bool=true)
Set or reset the autoReplot option.
Definition qwt_plot.cpp:310
QwtPlot::legend
QwtAbstractLegend * legend()
Definition qwt_plot.cpp:445
QwtPlot::axisWidget
const QwtScaleWidget * axisWidget(QwtAxisId) const
Definition qwt_plot_axis.cpp:141
QwtPlot::footer
QwtText footer() const
Definition qwt_plot.cpp:395
QwtPlot::titleLabel
QwtTextLabel * titleLabel()
Definition qwt_plot.cpp:357
QwtPlot::canvasMap
virtual QwtScaleMap canvasMap(QwtAxisId) const
Definition qwt_plot.cpp:800
QwtPlot::QwtPlot
QwtPlot(QWidget *=NULL)
Constructor.
Definition qwt_plot.cpp:103
QwtPlot::resizeEvent
virtual void resizeEvent(QResizeEvent *) override
Definition qwt_plot.cpp:530
QwtPlot::legendDataChanged
void legendDataChanged(const QVariant &itemInfo, const QList< QwtLegendData > &data)
QwtPlot::updateLayout
virtual void updateLayout()
Adjust plot content to its current size.
Definition qwt_plot.cpp:577
QwtPlot::setCanvasBackground
void setCanvasBackground(const QBrush &)
Change the background of the plotting area.
Definition qwt_plot.cpp:873
QwtPlot::drawCanvas
virtual void drawCanvas(QPainter *)
Definition qwt_plot.cpp:742
QwtPlot::setCanvas
void setCanvas(QWidget *)
Set the drawing canvas of the plot widget.
Definition qwt_plot.cpp:213
QwtPlot::autoRefresh
void autoRefresh()
Replots the plot if autoReplot() is true.
Definition qwt_plot.cpp:289
QwtPlot::axisScaleDiv
const QwtScaleDiv & axisScaleDiv(QwtAxisId) const
Return the scale division of a specified axis.
Definition qwt_plot_axis.cpp:283
QwtPlot::updateCanvasMargins
void updateCanvasMargins()
Update the canvas margins.
Definition qwt_plot.cpp:706
QwtPlot::sizeHint
virtual QSize sizeHint() const override
Definition qwt_plot.cpp:480
QwtPlot::title
QwtText title() const
Definition qwt_plot.cpp:351
QwtPlotItem
Base class for items on the plot canvas.
Definition qwt_plot_item.h:67
QwtPlotItem::yAxis
QwtAxisId yAxis() const
Return yAxis.
Definition qwt_plot_item.cpp:559
QwtPlotItem::draw
virtual void draw(QPainter *painter, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect) const =0
Draw the item.
QwtPlotItem::isVisible
bool isVisible() const
Definition qwt_plot_item.cpp:470
QwtPlotItem::legendData
virtual QList< QwtLegendData > legendData() const
Return all information, that is needed to represent the item on the legend.
Definition qwt_plot_item.cpp:626
QwtPlotItem::xAxis
QwtAxisId xAxis() const
Return xAxis.
Definition qwt_plot_item.cpp:553
QwtPlotItem::testItemInterest
bool testItemInterest(ItemInterest) const
Definition qwt_plot_item.cpp:299
QwtPlotItem::Rtti_PlotItem
@ Rtti_PlotItem
Unspecific value, that can be used, when it doesn't matter.
Definition qwt_plot_item.h:78
QwtPlotItem::RenderAntialiased
@ RenderAntialiased
Enable antialiasing.
Definition qwt_plot_item.h:206
QwtPlotItem::testItemAttribute
bool testItemAttribute(ItemAttribute) const
Definition qwt_plot_item.cpp:266
QwtPlotItem::testRenderHint
bool testRenderHint(RenderHint) const
Definition qwt_plot_item.cpp:332
QwtPlotItem::Legend
@ Legend
The item is represented on the legend.
Definition qwt_plot_item.h:150
QwtPlotItem::Margins
@ Margins
Definition qwt_plot_item.h:164
QwtPlotItem::getCanvasMarginHint
virtual void getCanvasMarginHint(const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect, double &left, double &top, double &right, double &bottom) const
Calculate a hint for the canvas margin.
Definition qwt_plot_item.cpp:595
QwtPlotItem::updateLegend
virtual void updateLegend(const QwtPlotItem *, const QList< QwtLegendData > &)
Update the item to changes of the legend info.
Definition qwt_plot_item.cpp:690
QwtPlotItem::LegendInterest
@ LegendInterest
Definition qwt_plot_item.h:197
QwtPlotLayout
Layout engine for QwtPlot.
Definition qwt_plot_layout.h:28
QwtPlotLayout::titleRect
QRectF titleRect() const
Definition qwt_plot_layout.cpp:1282
QwtPlotLayout::setCanvasMargin
void setCanvasMargin(int margin, int axis=-1)
Definition qwt_plot_layout.cpp:1066
QwtPlotLayout::setLegendPosition
void setLegendPosition(QwtPlot::LegendPosition pos, double ratio)
Specify the position of the legend.
Definition qwt_plot_layout.cpp:1183
QwtPlotLayout::footerRect
QRectF footerRect() const
Definition qwt_plot_layout.cpp:1304
QwtPlotLayout::canvasMargin
int canvasMargin(int axisId) const
Definition qwt_plot_layout.cpp:1089
QwtPlotLayout::legendRect
QRectF legendRect() const
Definition qwt_plot_layout.cpp:1328
QwtPlotLayout::legendPosition
QwtPlot::LegendPosition legendPosition() const
Definition qwt_plot_layout.cpp:1237
QwtPlotLayout::minimumSizeHint
virtual QSize minimumSizeHint(const QwtPlot *) const
Definition qwt_plot_layout.cpp:1404
QwtPlotLayout::scaleRect
QRectF scaleRect(QwtAxisId) const
Definition qwt_plot_layout.cpp:1355
QwtPlotLayout::canvasRect
QRectF canvasRect() const
Definition qwt_plot_layout.cpp:1380
QwtPlotLayout::activate
virtual void activate(const QwtPlot *, const QRectF &plotRect, Options options=Options())
Recalculate the geometry of all components.
Definition qwt_plot_layout.cpp:1508
QwtScaleDiv
A class representing a scale division.
Definition qwt_scale_div.h:34
QwtScaleDiv::lowerBound
double lowerBound() const
Definition qwt_scale_div.cpp:131
QwtScaleDiv::upperBound
double upperBound() const
Definition qwt_scale_div.cpp:151
QwtScaleDiv::ticks
QList< double > ticks(int tickType) const
Definition qwt_scale_div.cpp:309
QwtScaleDiv::MajorTick
@ MajorTick
Major ticks.
Definition qwt_scale_div.h:49
QwtScaleMap
A scale map.
Definition qwt_scale_map.h:27
QwtScaleMap::setPaintInterval
void setPaintInterval(double p1, double p2)
Specify the borders of the paint device interval.
Definition qwt_scale_map.cpp:119
QwtScaleMap::setScaleInterval
void setScaleInterval(double s1, double s2)
Specify the borders of the scale interval.
Definition qwt_scale_map.cpp:100
QwtScaleMap::setTransformation
void setTransformation(QwtTransform *)
Definition qwt_scale_map.cpp:76
QwtScaleWidget
A Widget which contains a scale.
Definition qwt_scale_widget.h:35
QwtScaleWidget::startBorderDist
int startBorderDist() const
Definition qwt_scale_widget.cpp:381
QwtScaleWidget::scaleDraw
const QwtScaleDraw * scaleDraw() const
Definition qwt_scale_widget.cpp:354
QwtScaleWidget::endBorderDist
int endBorderDist() const
Definition qwt_scale_widget.cpp:390
QwtScaleWidget::getBorderDistHint
void getBorderDistHint(int &start, int &end) const
Calculate a hint for the border distances.
Definition qwt_scale_widget.cpp:815
QwtScaleWidget::setBorderDist
void setBorderDist(int dist1, int dist2)
Definition qwt_scale_widget.cpp:251
QwtScaleWidget::minimumSizeHint
virtual QSize minimumSizeHint() const override
Definition qwt_scale_widget.cpp:733
QwtText
A class representing a text.
Definition qwt_text.h:52
QwtText::setRenderFlags
void setRenderFlags(int)
Change the render flags.
Definition qwt_text.cpp:304
QwtTextLabel
A Widget which displays a QwtText.
Definition qwt_text_label.h:27
QwtAxis
Definition qwt_axis.h:19
QwtAxis::isYAxis
bool isYAxis(int axisPos)
Definition qwt_axis.h:57
QwtAxis::YRight
@ YRight
Y axis right of the canvas.
Definition qwt_axis.h:27
QwtAxis::XTop
@ XTop
X axis above the canvas.
Definition qwt_axis.h:33