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


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:43