curvetracker.cpp
3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "curvetracker.h"
#include <qwt_picker_machine.h>
#include <qwt_series_data.h>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
struct compareX
{
inline bool operator()( const double x, const QPointF &pos ) const
{
return ( x < pos.x() );
}
};
CurveTracker::CurveTracker( QWidget *canvas ):
QwtPlotPicker( canvas )
{
setTrackerMode( QwtPlotPicker::ActiveOnly );
setRubberBand( VLineRubberBand );
setStateMachine( new QwtPickerDragPointMachine() );
}
QRect CurveTracker::trackerRect( const QFont &font ) const
{
QRect r = QwtPlotPicker::trackerRect( font );
// align r to the first curve
const QwtPlotItemList curves = plot()->itemList( QwtPlotItem::Rtti_PlotCurve );
if ( curves.size() > 0 )
{
QPointF pos = invTransform( trackerPosition() );
const QLineF line = curveLineAt(
static_cast<const QwtPlotCurve *>( curves[0] ), pos.x() );
if ( !line.isNull() )
{
const double curveY = line.pointAt(
( pos.x() - line.p1().x() ) / line.dx() ).y();
pos.setY( curveY );
pos = transform( pos );
r.moveBottom( pos.y() );
}
}
return r;
}
QwtText CurveTracker::trackerTextF( const QPointF &pos ) const
{
QwtText trackerText;
trackerText.setColor( Qt::black );
QColor c( "#333333" );
trackerText.setBorderPen( QPen( c, 2 ) );
c.setAlpha( 200 );
trackerText.setBackgroundBrush( c );
QString info;
const QwtPlotItemList curves =
plot()->itemList( QwtPlotItem::Rtti_PlotCurve );
for ( int i = 0; i < curves.size(); i++ )
{
const QString curveInfo = curveInfoAt(
static_cast<const QwtPlotCurve *>( curves[i] ), pos );
if ( !curveInfo.isEmpty() )
{
if ( !info.isEmpty() )
info += "<br>";
info += curveInfo;
}
}
trackerText.setText( info );
return trackerText;
}
QString CurveTracker::curveInfoAt(
const QwtPlotCurve *curve, const QPointF &pos ) const
{
const QLineF line = curveLineAt( curve, pos.x() );
if ( line.isNull() )
return QString::null;
const double y = line.pointAt(
( pos.x() - line.p1().x() ) / line.dx() ).y();
QString info( "<font color=""%1"">%2</font>" );
return info.arg( curve->pen().color().name() ).arg( y );
}
QLineF CurveTracker::curveLineAt(
const QwtPlotCurve *curve, double x ) const
{
QLineF line;
if ( curve->dataSize() >= 2 )
{
const QRectF br = curve->boundingRect();
if ( br.isValid() && x >= br.left() && x <= br.right() )
{
int index = qwtUpperSampleIndex<QPointF>(
*curve->data(), x, compareX() );
if ( index == -1 &&
x == curve->sample( curve->dataSize() - 1 ).x() )
{
// the last sample is excluded from qwtUpperSampleIndex
index = curve->dataSize() - 1;
}
if ( index > 0 )
{
line.setP1( curve->sample( index - 1 ) );
line.setP2( curve->sample( index ) );
}
}
}
return line;
}