bar_delegate.cpp
Go to the documentation of this file.
1 // Renders a colored bar graph behind the data
2 // Author: Max Schwarz <max.schwarz@uni-bonn.de>
3 
4 #include "bar_delegate.h"
5 
6 #include <QPainter>
7 
8 namespace rqt_rosmon
9 {
10 
11 BarDelegate::BarDelegate(QObject* parent)
12  : QStyledItemDelegate(parent)
13  , m_min(0.0)
14  , m_max(100.0)
15 {
16 }
17 
18 void BarDelegate::setRange(double min, double max)
19 {
20  m_min = min;
21  m_max = max;
22 }
23 
24 void BarDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
25 {
26  double data;
27 
28  QVariant var = index.data(Qt::EditRole);
29  if(var.isValid())
30  data = var.toDouble();
31  else
32  data = index.data(Qt::DisplayRole).toDouble();
33 
34  double alpha = std::min(1.0, std::max(0.0, (data - m_min) / (m_max - m_min)));
35 
36  QRect rect = option.rect;
37  rect.setWidth(static_cast<int>(alpha * rect.width()));
38 
39  QColor color(
40  static_cast<int>(alpha * 255),
41  static_cast<int>((1.0f - alpha) * 255),
42  0
43 // (1.0f - alpha) * 255
44  );
45 
46  painter->fillRect(rect, color);
47 
48  QStyledItemDelegate::paint(painter, option, index);
49 }
50 
51 }
f
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void setRange(double min, double max)
BarDelegate(QObject *parent=0)


rqt_rosmon
Author(s): Max Schwarz
autogenerated on Sat Jan 9 2021 03:35:46