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


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:47:33