customtracker.cpp
Go to the documentation of this file.
1 #include "customtracker.h"
2 #include "qwt_series_data.h"
3 #include "qwt_plot.h"
4 #include "qwt_plot_curve.h"
5 #include "qwt_event_pattern.h"
6 #include "qwt_symbol.h"
7 #include <qevent.h>
8 #include <QFontDatabase>
9 
10 struct compareX
11 {
12  inline bool operator()( const double x, const QPointF &pos ) const
13  {
14  return ( x < pos.x() );
15  }
16 };
17 
18 
19 
21  QObject( plot ),
22  _plot( plot ),
23  _param( VALUE )
24 {
25  _line_marker = ( new QwtPlotMarker );
26 
27  _line_marker->setLinePen(QPen(Qt::red));
29  _line_marker->setValue(0,0);
30  _line_marker->attach(plot);
31 
32  _text_marker = ( new QwtPlotMarker );
33  _text_marker->attach(plot);
34 
35  _visible = true;
36 }
37 
39 {
40  return _prev_trackerpoint;
41 }
42 
44 {
45  bool changed = _param != par;
46  _param = par;
47 
48  if( changed ){
50  }
51 }
52 
53 void CurveTracker::setEnabled(bool enable)
54 {
55  _visible = enable;
56  _line_marker->setVisible( enable );
57  _text_marker->setVisible( enable );
58 
59  for (int i = 0 ; i < _marker.size(); i++ )
60  {
61  _marker[i]->setVisible( enable );
62  }
63 }
64 
65 void CurveTracker::setPosition(const QPointF& position)
66 {
68 
69  _line_marker->setValue( position );
70 
71  QRectF rect;
72  rect.setBottom( _plot->canvasMap( QwtPlot::yLeft ).s1() );
73  rect.setTop( _plot->canvasMap( QwtPlot::yLeft ).s2() );
74  rect.setLeft( _plot->canvasMap( QwtPlot::xBottom ).s1() );
75  rect.setRight( _plot->canvasMap( QwtPlot::xBottom ).s2() );
76 
77  double tot_Y = 0;
78  int visible_points = 0;
79 
80  while( _marker.size() > curves.size())
81  {
82  _marker.back()->detach();
83  _marker.pop_back();
84  }
85 
86  for (int i = _marker.size() ; i < curves.size(); i++ )
87  {
88  _marker.push_back( new QwtPlotMarker );
89  _marker[i]->attach( _plot );
90  }
91 
92  double text_X_offset = 0;
93 
94  std::multimap<double,QString> text_lines;
95 
96  for ( int i = 0; i < curves.size(); i++ )
97  {
98  QwtPlotCurve *curve = static_cast<QwtPlotCurve *>(curves[i]);
99  _marker[i]->setVisible( curve->isVisible() );
100 
101  if( curve->isVisible() == false)
102  {
103  continue;
104  }
105  QColor color = curve->pen().color();
106 
107  text_X_offset = rect.width() * 0.02;
108 
109  if( !_marker[i]->symbol() )
110  {
111  QwtSymbol *sym = new QwtSymbol(
113  color,
114  color,
115  QSize(5,5));
116  _marker[i]->setSymbol(sym);
117  }
118 
119  const QLineF line = curveLineAt( curve, position.x() );
120 
121  if( line.isNull() )
122  {
123  continue;
124  }
125 
126  QPointF point;
127  double middle_X = (line.p1().x() + line.p2().x()) / 2.0;
128 
129  if( position.x() < middle_X )
130  point = line.p1();
131  else
132  point = line.p2();
133 
134  _marker[i]->setValue( point );
135 
136  if( rect.contains( point ) && _visible )
137  {
138  tot_Y += point.y();
139  visible_points++;
140  double val = point.y();
141 
142  QString line;
143 
144  if( _param == VALUE)
145  {
146  line = QString( "<font color=%1>%2</font>" ).arg( color.name() ).arg( val );
147  }
148  else if( _param == VALUE_NAME)
149  {
150  QString value = QString::number( val, 'f', 3);
151  int whitespaces = 8 - value.length();
152  while(whitespaces-- > 0) value.prepend("&nbsp;");
153 
154  line = QString( "<font color=%1>%2 : %3</font>" )
155  .arg( color.name() ).arg( value ).arg(curve->title().text() );
156  }
157 
158  text_lines.insert( std::make_pair(val, line) );
159  _marker[i]->setVisible( true );
160  }
161  else{
162  _marker[i]->setVisible( false );
163  }
164  _marker[i]->setValue( point );
165  }
166 
167  QwtText mark_text;
168  mark_text.setColor( Qt::black );
169 
170  QString text_marker_info;
171 
172  int count = 0;
173  for(auto it = text_lines.rbegin(); it != text_lines.rend(); it++)
174  {
175  text_marker_info += it->second;
176  if( count++ < text_lines.size() -1 )
177  {
178  text_marker_info += "<br>";
179  }
180  }
181 
182  QColor col( "#FFFFFF" );
183  mark_text.setBorderPen( QPen( col, 2 ) );
184  col.setAlpha( 220 );
185  mark_text.setBackgroundBrush( col );
186  mark_text.setText( text_marker_info );
187 
188  QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
189  font.setPointSize( 9 );
190 
191  mark_text.setFont( font );
192  mark_text.setRenderFlags( _param == VALUE ? Qt::AlignCenter : Qt::AlignLeft);
193 
194  _text_marker->setLabel(mark_text);
195  _text_marker->setLabelAlignment( Qt::AlignRight );
196 
197  _text_marker->setXValue( position.x() + text_X_offset );
198 
199  if(visible_points > 0){
200  _text_marker->setYValue( tot_Y/visible_points );
201  }
202 
203  double canvas_ratio = rect.width() / double(_plot->width());
204  double text_width = mark_text.textSize().width() * canvas_ratio;
205  bool exceed_right = (_text_marker->boundingRect().right() + text_width) > rect.right();
206 
207  if( exceed_right )
208  {
209  _text_marker->setXValue( position.x() - text_X_offset - text_width );
210  }
211 
212  _text_marker->setVisible( visible_points > 0 && _visible && _param != LINE_ONLY );
213 
214  _prev_trackerpoint = position;
215 
216 }
217 
218 
220  const QwtPlotCurve *curve, double x ) const
221 {
222  QLineF line;
223 
224  if ( curve->dataSize() >= 2 )
225  {
226  int index = qwtUpperSampleIndex<QPointF>(
227  *curve->data(), x, compareX() );
228 
229  if ( index > 0 )
230  {
231  line.setP1( curve->sample( index - 1 ) );
232  line.setP2( curve->sample( index ) );
233  }
234  }
235  return line;
236 }
237 
virtual size_t dataSize() const
CurveTracker(QwtPlot *)
A plot item, that represents a series of points.
void setFont(const QFont &)
Definition: qwt_text.cpp:296
double s1() const
Definition: qwt_scale_map.h:85
bool isVisible() const
void setValue(double, double)
Set Value.
void setText(const QString &, QwtText::TextFormat textFormat=AutoText)
Definition: qwt_text.cpp:244
Parameter _param
Definition: customtracker.h:51
void setBorderPen(const QPen &)
Definition: qwt_text.cpp:387
QList< QwtPlotItem * > QwtPlotItemList
See QT 4.x assistant documentation for QList.
Definition: qwt_plot_dict.h:20
const QwtPlotItemList & itemList() const
A QwtPlotItemList of all attached plot items.
QwtSeriesData< T > * data()
A 2-D plotting widget.
Definition: qwt_plot.h:74
A class for drawing symbols.
Definition: qwt_symbol.h:30
Y axis left of the canvas.
Definition: qwt_plot.h:96
void setColor(const QColor &)
Definition: qwt_text.cpp:332
double s2() const
Definition: qwt_scale_map.h:93
void setLinePen(const QColor &, qreal width=0.0, Qt::PenStyle=Qt::SolidLine)
QSizeF textSize(const QFont &=QFont()) const
Definition: qwt_text.cpp:526
void setRenderFlags(int flags)
Change the render flags.
Definition: qwt_text.cpp:271
QwtPlotMarker * _line_marker
Definition: customtracker.h:48
QLineF curveLineAt(const QwtPlotCurve *, double x) const
T value
void setXValue(double)
Set X Value.
A class representing a text.
Definition: qwt_text.h:51
virtual QwtScaleMap canvasMap(int axisId) const
Definition: qwt_plot.cpp:790
virtual QRectF boundingRect() const
QString text() const
Definition: qwt_text.cpp:256
void setBackgroundBrush(const QBrush &)
Definition: qwt_text.cpp:408
T sample(int index) const
void setEnabled(bool enable)
QwtPlotMarker * _text_marker
Definition: customtracker.h:49
void setLabel(const QwtText &)
Set the label.
QPointF _prev_trackerpoint
Definition: customtracker.h:46
std::vector< QwtPlotMarker * > _marker
Definition: customtracker.h:47
virtual void setVisible(bool)
void attach(QwtPlot *plot)
Attach the item to a plot.
QwtPlot * _plot
Definition: customtracker.h:50
void * arg
void setLabelAlignment(Qt::Alignment)
Set the alignment of the label.
void setYValue(double)
Set Y Value.
QPointF actualPosition() const
void setPosition(const QPointF &pos)
void setLineStyle(LineStyle st)
Set the line style.
int i
void setParameter(Parameter par)
bool operator()(const double x, const QPointF &pos) const
const QPen & pen() const
X axis below the canvas.
Definition: qwt_plot.h:102
A class for drawing markers.
const QwtText & title() const
A vertical line.
For QwtPlotCurve.
Definition: qwt_plot_item.h:91


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17