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


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