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 }
virtual void draw(QPainter *painter, const QwtScaleMap &, const QwtScaleMap &, const QRectF &canvasRect) const override
Draw the item.
const PJ::PlotData & _data
static void fillRect(QPainter *, const QRectF &, const QBrush &)
Wrapper for QPainter::fillRect()
std::map< QString, ColorMap::Ptr > & ColorMapLibrary()
Definition: color_map.cpp:48
double transform(double s) const
virtual QRectF boundingRect() const
virtual size_t size() const
Definition: plotdatabase.h:182
BackgroundColorItem(const PJ::PlotData &data, QString colormap_name)
A scale map.
Definition: qwt_scale_map.h:26
virtual RangeOpt rangeX() const
Definition: plotdatabase.h:275
color
Definition: color.h:23
virtual QRectF boundingRect() const override
Definition: format.h:895


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Jun 19 2023 03:01:38