plotlegend.cpp
Go to the documentation of this file.
1 #include "plotlegend.h"
2 #include <QEvent>
3 #include <QMouseEvent>
4 #include <QWheelEvent>
5 
6 
8 {
9  auto canvas_rect = _parent_plot->canvas()->rect();
10  if( alignment() & Qt::AlignRight)
11  {
12  return QRectF( geometry(canvas_rect).topRight(), QSize(8,-8) );
13  }
14  return QRectF( geometry(canvas_rect).topLeft(), QSize(-8,-8) );
15 }
16 
17 void PlotLegend::draw(QPainter *painter, const QwtScaleMap &xMap,
18  const QwtScaleMap &yMap,
19  const QRectF &rect) const
20 {
21  if( !_collapsed )
22  {
23  QwtPlotLegendItem::draw(painter, xMap, yMap, rect);
24  }
25 
26  QRectF iconRect = hideButtonRect();
27 
28  if( isVisible() && plotItems().size() > 0)
29  {
30  painter->save();
31 
32  auto col = _collapsed ? Qt::black : Qt::white;
33  painter->setPen( col );
34  painter->setBrush( QBrush(col, Qt::SolidPattern) );
35  painter->drawRect( iconRect );
36 
37  QPen black_pen (Qt::black);
38  black_pen.setWidth(2);
39  painter->setPen( black_pen );
40 // painter->drawLine( iconRect.topLeft(), iconRect.bottomRight() );
41 // painter->drawLine( iconRect.topRight(), iconRect.bottomLeft() );
42  painter->drawEllipse( iconRect );
43 
44  painter->restore();
45  }
46 }
47 
48 void PlotLegend::drawLegendData( QPainter *painter,
49  const QwtPlotItem *plotItem, const QwtLegendData &data,
50  const QRectF &rect ) const
51 {
52  Q_UNUSED( plotItem );
53 
54  const int m = margin();
55  const QRectF r = rect.toRect().adjusted( m, m, -m, -m );
56 
57  painter->setClipRect( r, Qt::IntersectClip );
58 
59  int titleOff = 0;
60 
61  const QwtGraphic graphic = data.icon();
62  if ( !graphic.isEmpty() )
63  {
64  QRectF iconRect( r.topLeft(), graphic.defaultSize() );
65 
66  iconRect.moveCenter(
67  QPoint( iconRect.center().x(), rect.center().y() ) );
68 
69  if( plotItem->isVisible() )
70  {
71  graphic.render( painter, iconRect, Qt::KeepAspectRatio );
72  }
73 
74  titleOff += iconRect.width() + spacing();
75  }
76 
77  const QwtText text = data.title();
78  if ( !text.isEmpty() )
79  {
80  auto pen = textPen();
81  if( !plotItem->isVisible() )
82  {
83  pen.setColor( Qt::gray );
84  }
85  painter->setPen( pen );
86  painter->setFont( font() );
87 
88  const QRectF textRect = r.adjusted( titleOff, 0, 0, 0 );
89  text.draw( painter, textRect );
90  }
91 }
92 
93 
94 bool PlotLegend::processWheelEvent(QWheelEvent* mouse_event)
95 {
96  if ( mouse_event->modifiers() == Qt::ControlModifier && isVisible() )
97  {
98  auto canvas_rect = _parent_plot->canvas()->rect();
99  auto legend_rect = geometry( canvas_rect );
100  if( legend_rect.contains( mouse_event->pos()) )
101  {
102  int point_size = font().pointSize();
103  if( mouse_event->delta() > 0 && point_size < 14)
104  {
105  emit legendSizeChanged(point_size+1);
106  }
107  if( mouse_event->delta() < 0 && point_size > 6)
108  {
109  emit legendSizeChanged(point_size-1);
110  }
111  return true;
112  }
113  }
114  return false;
115 }
116 
117 const QwtPlotItem* PlotLegend::processMousePressEvent(QMouseEvent* mouse_event)
118 {
119  auto canvas_rect = _parent_plot->canvas()->rect();
120  const QPoint press_point = mouse_event->pos();
121 
122  if( isVisible() && mouse_event->modifiers() == Qt::NoModifier)
123  {
124  if( !_collapsed && geometry( canvas_rect ).contains(press_point) )
125  {
126  for(auto item: plotItems())
127  {
128  auto item_rect = legendGeometries(item).first();
129  if( item_rect.contains( press_point ))
130  {
131  return item;
132  }
133  }
134  }
135  else if( hideButtonRect().contains(press_point) )
136  {
138  _parent_plot->replot();
139  return nullptr;
140  }
141  }
142  return nullptr;
143 }
bool _collapsed
Definition: plotlegend.h:36
const QwtPlotItem * processMousePressEvent(QMouseEvent *mouse_event)
Definition: plotlegend.cpp:117
void render(QPainter *) const
Replay all recorded painter commands.
QwtPlot * _parent_plot
Definition: plotlegend.h:35
bool isVisible() const
QSizeF defaultSize() const
Default size.
virtual QRect geometry(const QRectF &canvasRect) const
virtual void replot()
Redraw the plot.
Definition: qwt_plot.cpp:546
void draw(QPainter *painter, const QRectF &rect) const
Definition: qwt_text.cpp:560
virtual void draw(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &rect) const
virtual void drawLegendData(QPainter *painter, const QwtPlotItem *, const QwtLegendData &, const QRectF &) const override
Definition: plotlegend.cpp:48
QList< const QwtPlotItem * > plotItems() const
bool isEmpty() const
Definition: qwt_text.h:213
A class representing a text.
Definition: qwt_text.h:51
QwtGraphic icon() const
A paint device for scalable graphics.
Definition: qwt_graphic.h:74
A scale map.
Definition: qwt_scale_map.h:30
uintptr_t size
QRectF hideButtonRect() const
Definition: plotlegend.cpp:7
QWidget * canvas()
Definition: qwt_plot.cpp:467
QwtText title() const
virtual void draw(QPainter *p, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &rect) const override
Definition: plotlegend.cpp:17
bool isEmpty() const
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:64
QList< QRect > legendGeometries(const QwtPlotItem *) const
bool processWheelEvent(QWheelEvent *ev)
Definition: plotlegend.cpp:94
Qt::Alignment alignment() const
Attributes of an entry on a legend.
void legendSizeChanged(int point_size)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17