plotlegend.cpp
Go to the documentation of this file.
1 #include "plotlegend.h"
2 #include <QEvent>
3 #include <QMouseEvent>
4 #include <QWheelEvent>
5 #include <QPainter>
6 #include <QMarginsF>
7 #include "qwt_legend_data.h"
8 #include "qwt_graphic.h"
9 #include "qwt_text.h"
10 
11 PlotLegend::PlotLegend(QwtPlot* parent) : _parent_plot(parent), _collapsed(false)
12 {
14 
15  setMaxColumns(1);
16  setAlignmentInCanvas(Qt::Alignment(Qt::AlignTop | Qt::AlignRight));
17  setBackgroundMode(QwtPlotLegendItem::BackgroundMode::LegendBackground);;
18  setBorderRadius(0);
19 
20  setMargin(2);
21  setSpacing(1);
22  setItemMargin(2);
23 
24  QFont font = this->font();
25  font.setPointSize(9);
26  setFont(font);
27  setVisible(true);
28 
29  this->attach(parent);
30 }
31 
33 {
34  const int s = 5;
35  auto canvas_rect = _parent_plot->canvas()->rect();
36  if (alignmentInCanvas() & Qt::AlignRight)
37  {
38  return QRectF(geometry(canvas_rect).topRight() + QPoint(-s, -s) , QSize(s*2, s*2));
39  }
40  return QRectF(geometry(canvas_rect).topLeft() + QPoint(-s, -s), QSize(s*2, s*2));
41 }
42 
43 void PlotLegend::draw(QPainter* painter, const QwtScaleMap& xMap, const QwtScaleMap& yMap, const QRectF& rect) const
44 {
45  if (!_collapsed)
46  {
47  QwtPlotLegendItem::draw(painter, xMap, yMap, rect);
48  }
49 
50  QRectF iconRect = hideButtonRect();
51 
52  if (isVisible() && plotItems().size() > 0)
53  {
54  painter->save();
55 
56  QColor col = _parent_plot->canvas()->palette().foreground().color();
57  painter->setPen(col);
58  painter->setBrush(QBrush(Qt::white, Qt::SolidPattern));
59  painter->drawEllipse(iconRect);
60 
61  if( _collapsed ){
62  iconRect -= QMarginsF(3,3,3,3);
63  painter->setBrush(QBrush(col, Qt::SolidPattern));
64  painter->drawEllipse(iconRect);
65  }
66 
67  painter->restore();
68  }
69 }
70 
71 void PlotLegend::drawLegendData(QPainter* painter, const QwtPlotItem* plotItem, const QwtLegendData& data,
72  const QRectF& rect) const
73 {
74  Q_UNUSED(plotItem);
75 
76  const int m = margin();
77  const QRectF r = rect.toRect().adjusted(m, m, -m, -m);
78 
79  painter->setClipRect(r, Qt::IntersectClip);
80 
81  int titleOff = 0;
82 
83  const QwtGraphic graphic = data.icon();
84  if (!graphic.isEmpty())
85  {
86  QRectF iconRect(r.topLeft(), graphic.defaultSize());
87 
88  iconRect.moveCenter(QPoint(iconRect.center().x(), rect.center().y()));
89 
90  if (plotItem->isVisible())
91  {
92  graphic.render(painter, iconRect, Qt::KeepAspectRatio);
93  }
94 
95  titleOff += iconRect.width() + spacing();
96  }
97 
98  const QwtText text = data.title();
99  if (!text.isEmpty())
100  {
101  auto pen = textPen();
102  if (!plotItem->isVisible())
103  {
104  pen.setColor(QColor(122, 122, 122));
105  }
106  else
107  {
108  pen.setColor(_parent_plot->canvas()->palette().foreground().color());
109  }
110  painter->setPen(pen);
111  painter->setFont(font());
112 
113  const QRectF textRect = r.adjusted(titleOff, 0, 0, 0);
114  text.draw(painter, textRect);
115  }
116 }
117 
118 void PlotLegend::drawBackground(QPainter* painter, const QRectF& rect) const
119 {
120  painter->save();
121 
122  auto pen = textPen();
123  pen.setColor(_parent_plot->canvas()->palette().foreground().color());
124 
125  painter->setPen(pen);
126  painter->setBrush(backgroundBrush());
127  const double radius = borderRadius();
128  painter->drawRoundedRect(rect, radius, radius);
129 
130  painter->restore();
131 }
132 
133 const QwtPlotItem* PlotLegend::processMousePressEvent(QMouseEvent* mouse_event)
134 {
135  auto canvas_rect = _parent_plot->canvas()->rect();
136  const QPoint press_point = mouse_event->pos();
137 
138  if (isVisible() && mouse_event->modifiers() == Qt::NoModifier)
139  {
140  if ( (hideButtonRect() + QMargins(2,2,2,2) ).contains(press_point))
141  {
143  _parent_plot->replot();
144  return nullptr;
145  }
146 
147  if (!_collapsed && geometry(canvas_rect).contains(press_point))
148  {
149  for (auto item : plotItems())
150  {
151  auto item_rect = legendGeometries(item).first();
152  if (item_rect.contains(press_point))
153  {
154  return item;
155  }
156  }
157  }
158  }
159  return nullptr;
160 }
void setSpacing(int)
Set the spacing between the legend items.
bool _collapsed
Definition: plotlegend.h:27
const QwtPlotItem * processMousePressEvent(QMouseEvent *mouse_event)
Definition: plotlegend.cpp:133
void setBackgroundMode(BackgroundMode)
Set the background mode.
void render(QPainter *) const
Replay all recorded painter commands.
Enable antialiasing.
QwtPlot * _parent_plot
Definition: plotlegend.h:26
bool isVisible() const
void setFont(const QFont &)
QSizeF defaultSize() const
Default size.
virtual QRect geometry(const QRectF &canvasRect) const
virtual void replot()
Redraw the plot.
Definition: qwt_plot.cpp:539
void draw(QPainter *painter, const QRectF &rect) const
Definition: qwt_text.cpp:592
Qt::Alignment alignmentInCanvas() const
virtual void drawBackground(QPainter *painter, const QRectF &rect) const override
Definition: plotlegend.cpp:118
A 2-D plotting widget.
Definition: qwt_plot.h:75
void setMargin(int)
Set the margin around legend items.
void setAlignmentInCanvas(Qt::Alignment)
Set the alignmnet.
virtual void drawLegendData(QPainter *painter, const QwtPlotItem *, const QwtLegendData &, const QRectF &) const override
Definition: plotlegend.cpp:71
QList< const QwtPlotItem * > plotItems() const
QBrush backgroundBrush() const
bool isEmpty() const
Definition: qwt_text.cpp:716
A class representing a text.
Definition: qwt_text.h:51
QwtGraphic icon() const
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect) const QWT_OVERRIDE
A paint device for scalable graphics.
Definition: qwt_graphic.h:75
A scale map.
Definition: qwt_scale_map.h:26
double borderRadius() const
virtual void setVisible(bool)
QRectF hideButtonRect() const
Definition: plotlegend.cpp:32
QWidget * canvas()
Definition: qwt_plot.cpp:460
QwtText title() const
void attach(QwtPlot *plot)
Attach the item to a plot.
PlotLegend(QwtPlot *parent)
Definition: plotlegend.cpp:11
virtual void draw(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &rect) const override
Definition: plotlegend.cpp:43
bool isEmpty() const
void setRenderHint(RenderHint, bool on=true)
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:65
QList< QRect > legendGeometries(const QwtPlotItem *) const
dictionary data
Definition: mqtt_test.py:22
Attributes of an entry on a legend.
void setMaxColumns(uint)
Limit the number of columns.


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10