plotzoomer.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 "plotzoomer.h"
8 #include <QMouseEvent>
9 #include <QApplication>
10 #include <QSettings>
11 #include <QPen>
12 
13 #include "qwt_plot_zoomer.h"
14 #include "qwt_scale_map.h"
15 #include "qwt_plot.h"
16 #include "PlotJuggler/svg_util.h"
17 
18 PlotZoomer::PlotZoomer(QWidget* canvas)
19  : QwtPlotZoomer(canvas, false)
20  , _mouse_pressed(false)
21  , _zoom_enabled(false)
22  , _keep_aspect_ratio(false)
23 {
25 }
26 
27 void PlotZoomer::widgetMousePressEvent(QMouseEvent* me)
28 {
29  _mouse_pressed = false;
30  auto patterns = this->mousePattern();
31  for (QwtEventPattern::MousePattern& pattern : patterns)
32  {
33  if (this->mouseMatch(pattern, me))
34  {
35  _mouse_pressed = true;
36  // this->setTrackerMode(AlwaysOn);
37  _initial_pos = me->pos();
38  }
39  break;
40  }
42 }
43 
44 void PlotZoomer::widgetMouseMoveEvent(QMouseEvent* me)
45 {
46  if (_mouse_pressed)
47  {
48  auto patterns = this->mousePattern();
49  for (QwtEventPattern::MousePattern& pattern : patterns)
50  {
51  QRect rect(me->pos(), _initial_pos);
52  QRectF zoomRect = invTransform(rect.normalized());
53 
54  if (zoomRect.width() > minZoomSize().width() &&
55  zoomRect.height() > minZoomSize().height())
56  {
57  if (!_zoom_enabled)
58  {
59  QSettings settings;
60  QString theme = settings.value("Preferences::theme", "light").toString();
61  const QPixmap& pixmap = LoadSvg(":/resources/svg/zoom_in.svg", theme);
62  QCursor zoom_cursor(pixmap.scaled(24, 24));
63 
64  _zoom_enabled = true;
67  QPen pen(parentWidget()->palette().foreground().color(), 1, Qt::DashLine);
68  this->setRubberBandPen(pen);
69  QApplication::setOverrideCursor(zoom_cursor);
70  }
71  }
72  else if (_zoom_enabled)
73  {
74  _zoom_enabled = false;
76  QApplication::restoreOverrideCursor();
77  }
78  break;
79  }
80  }
82 }
83 
85 {
86  _mouse_pressed = false;
87  _zoom_enabled = false;
90 }
91 
92 bool PlotZoomer::accept(QPolygon& pa) const
93 {
94  QApplication::restoreOverrideCursor();
95 
96  if (pa.count() < 2)
97  return false;
98 
99  QRect rect = QRect(pa[0], pa[int(pa.count()) - 1]);
100  QRectF zoomRect = invTransform(rect.normalized());
101 
102  if (zoomRect.width() < minZoomSize().width() &&
103  zoomRect.height() < minZoomSize().height())
104  {
105  return false;
106  }
107  return QwtPlotZoomer::accept(pa);
108 }
109 
110 void PlotZoomer::zoom(const QRectF& zoomRect)
111 {
112  QRectF rect = zoomRect;
113 
114  if (_keep_aspect_ratio)
115  {
116  const QRectF cr = canvas()->contentsRect();
117  const double canvas_ratio = cr.width() / cr.height();
118  const double zoom_ratio = zoomRect.width() / zoomRect.height();
119 
120  if (zoom_ratio < canvas_ratio)
121  {
122  double new_width = zoomRect.height() * canvas_ratio;
123  double increment = new_width - zoomRect.width();
124  rect.setWidth(new_width);
125  rect.moveLeft(rect.left() - 0.5 * increment);
126  }
127  else
128  {
129  double new_height = zoomRect.width() / canvas_ratio;
130  double increment = new_height - zoomRect.height();
131  rect.setHeight(new_height);
132  rect.moveTop(rect.top() - 0.5 * increment);
133  }
134  }
135  QwtPlotZoomer::zoom(rect);
136 }
137 
139 {
140  return QSizeF(scaleRect().width() * 0.02, scaleRect().height() * 0.02);
141 }
PlotZoomer::_mouse_pressed
bool _mouse_pressed
Definition: plotzoomer.h:40
QwtPicker::widgetMousePressEvent
virtual void widgetMousePressEvent(QMouseEvent *)
Definition: qwt_picker.cpp:1001
color
color
Definition: color.h:16
QwtPlotZoomer
QwtPlotZoomer provides stacked zooming for a plot widget.
Definition: qwt_plot_zoomer.h:79
QwtPlotZoomer::zoomRect
QRectF zoomRect() const
Definition: qwt_plot_zoomer.cpp:287
qwt_plot_zoomer.h
QwtEventPattern::mousePattern
const QVector< MousePattern > & mousePattern() const
Definition: qwt_event_pattern.cpp:151
LoadSvg
const QPixmap & LoadSvg(QString filename, QString style_name="light")
Definition: svg_util.h:26
PlotZoomer::_zoom_enabled
bool _zoom_enabled
Definition: plotzoomer.h:41
PlotZoomer::_initial_pos
QPoint _initial_pos
Definition: plotzoomer.h:43
PlotZoomer::accept
virtual bool accept(QPolygon &) const override
Check and correct a selected rectangle.
Definition: plotzoomer.cpp:92
QwtPlotPicker::invTransform
QRectF invTransform(const QRect &) const
Definition: qwt_plot_picker.cpp:341
QwtPicker::RectRubberBand
@ RectRubberBand
A rectangle ( only for QwtPickerMachine::RectSelection )
Definition: qwt_picker.h:142
QwtPicker::AlwaysOff
@ AlwaysOff
Display never.
Definition: qwt_picker.h:164
PlotZoomer::widgetMouseMoveEvent
virtual void widgetMouseMoveEvent(QMouseEvent *event) override
Definition: plotzoomer.cpp:44
PlotZoomer::zoom
virtual void zoom(const QRectF &rect) override
Zoom in.
Definition: plotzoomer.cpp:110
QwtPicker::widgetMouseMoveEvent
virtual void widgetMouseMoveEvent(QMouseEvent *)
Definition: qwt_picker.cpp:1015
QwtEventPattern::mouseMatch
bool mouseMatch(MousePatternCode, const QMouseEvent *) const
Compare a mouse event with an event pattern.
Definition: qwt_event_pattern.cpp:188
PlotZoomer::widgetMousePressEvent
virtual void widgetMousePressEvent(QMouseEvent *event) override
Definition: plotzoomer.cpp:27
PlotZoomer::_keep_aspect_ratio
bool _keep_aspect_ratio
Definition: plotzoomer.h:42
PlotZoomer::widgetMouseReleaseEvent
virtual void widgetMouseReleaseEvent(QMouseEvent *event) override
Definition: plotzoomer.cpp:84
qwt_scale_map.h
QwtPlotPicker::canvas
QWidget * canvas()
Definition: qwt_plot_picker.cpp:114
PlotZoomer::minZoomSize
virtual QSizeF minZoomSize() const override
Limit zooming by a minimum rectangle.
Definition: plotzoomer.cpp:138
QwtPicker::setRubberBandPen
void setRubberBandPen(const QPen &)
Definition: qwt_picker.cpp:460
plotzoomer.h
QwtPlotZoomer::zoom
virtual void zoom(const QRectF &)
Zoom in.
Definition: qwt_plot_zoomer.cpp:310
QwtPicker::widgetMouseReleaseEvent
virtual void widgetMouseReleaseEvent(QMouseEvent *)
Definition: qwt_picker.cpp:1069
QwtPicker::parentWidget
QWidget * parentWidget()
Return the parent widget, where the selection happens.
Definition: qwt_picker.cpp:263
QwtPicker::setRubberBand
void setRubberBand(RubberBand)
Definition: qwt_picker.cpp:290
QwtPlotPicker::scaleRect
QRectF scaleRect() const
Definition: qwt_plot_picker.cpp:149
PlotZoomer::PlotZoomer
PlotZoomer()
QwtPicker::NoRubberBand
@ NoRubberBand
No rubberband.
Definition: qwt_picker.h:130
QwtPicker::setTrackerMode
void setTrackerMode(DisplayMode)
Set the display mode of the tracker.
Definition: qwt_picker.cpp:320
qwt_plot.h
QwtPlotZoomer::accept
virtual bool accept(QPolygon &) const QWT_OVERRIDE
Check and correct a selected rectangle.
Definition: qwt_plot_zoomer.cpp:567
svg_util.h
QwtEventPattern::MousePattern
A pattern for mouse events.
Definition: qwt_event_pattern.h:149


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45