plot_background.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 "plot_background.h"
8 #include "qwt_scale_map.h"
9 #include "qwt_painter.h"
10 
12  : _data(data)
13  , _data_name(QString::fromStdString(data.plotName()))
14  , _colormap_name(colormap_name)
15 {
16 }
17 
18 void BackgroundColorItem::draw(QPainter* painter, const QwtScaleMap& xMap,
19  const QwtScaleMap& /*yMap*/,
20  const QRectF& canvasRect) const
21 {
22  if (_data.size() < 2)
23  {
24  return;
25  }
26  auto it = ColorMapLibrary().find(_colormap_name);
27  if (it == ColorMapLibrary().end())
28  {
29  return;
30  }
31  auto colormap = it->second;
32 
33  double prev_Y = _data[0].y;
34  double min_interval = _data[0].x;
35  QColor prev_color = colormap->mapColor(prev_Y);
36 
37  auto isEqual = [](double a, double b) {
38  return abs(a - b) < std::numeric_limits<float>::epsilon();
39  };
40 
41  const size_t N = _data.size();
42  double time_offset = _time_offset ? (*_time_offset) : 0;
43 
44  for (size_t i = 1; i < N; i++)
45  {
46  const auto& point = _data[i];
47  if (isEqual(prev_Y, point.y) && (i + 1) < N)
48  {
49  continue;
50  }
51  QColor color = colormap->mapColor(point.y);
52  if (prev_color != color || (i + 1) == N)
53  {
54  double max_interval = point.x;
55  double x1 = xMap.transform(min_interval - time_offset);
56  double x2 = xMap.transform(max_interval - time_offset);
57 
58  QRectF r(x1, canvasRect.top(), x2 - x1, canvasRect.height());
59  r = r.normalized();
60 
61  if (x1 < x2 && prev_color != Qt::transparent)
62  {
63  QwtPainter::fillRect(painter, r, prev_color);
64  }
65  prev_color = color;
66  min_interval = max_interval;
67  }
68  prev_Y = point.y;
69  }
70 }
71 
73 {
74  QRectF br = QwtPlotItem::boundingRect();
75  auto range_x = _data.rangeX();
76  if (range_x)
77  {
78  br.setLeft(_data.rangeX()->min);
79  br.setRight(_data.rangeX()->max);
80  }
81  return br;
82 }
color
color
Definition: color.h:16
PJ::TimeseriesBase
Definition: timeseries.h:16
BackgroundColorItem::draw
virtual void draw(QPainter *painter, const QwtScaleMap &, const QwtScaleMap &, const QRectF &canvasRect) const override
Draw the item.
Definition: plot_background.cpp:18
BackgroundColorItem::_data
const PJ::PlotData & _data
Definition: plot_background.h:49
PJ::PlotDataBase::rangeX
virtual RangeOpt rangeX() const
Definition: plotdatabase.h:276
QwtPlotItem::boundingRect
virtual QRectF boundingRect() const
Definition: qwt_plot_item.cpp:568
BackgroundColorItem::boundingRect
virtual QRectF boundingRect() const override
Definition: plot_background.cpp:72
BackgroundColorItem::_time_offset
double * _time_offset
Definition: plot_background.h:52
QwtPainter::fillRect
static void fillRect(QPainter *, const QRectF &, const QBrush &)
Wrapper for QPainter::fillRect()
Definition: qwt_painter.cpp:382
qwt_scale_map.h
BackgroundColorItem::BackgroundColorItem
BackgroundColorItem(const PJ::PlotData &data, QString colormap_name)
Definition: plot_background.cpp:11
plot_background.h
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:26
ColorMapLibrary
std::map< QString, ColorMap::Ptr > & ColorMapLibrary()
Definition: color_map.cpp:48
PJ::PlotDataBase::size
virtual size_t size() const
Definition: plotdatabase.h:183
qwt_painter.h
mqtt_test.data
dictionary data
Definition: mqtt_test.py:22
BackgroundColorItem::_colormap_name
QString _colormap_name
Definition: plot_background.h:51


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:23