LVRGraphicsView.cpp
Go to the documentation of this file.
1 #include "LVRGraphicsView.hpp"
2 
3 #include <QMouseEvent>
4 #include <QApplication>
5 #include <QScrollBar>
6 #include <qmath.h>
7 
8 namespace lvr2 {
9 
11 {
12  viewport()->installEventFilter(this);
13  setMouseTracking(true);
14  m_modifiers = Qt::ControlModifier;
15  m_zoom_factor_base = 1.0015;
16 }
17 
18 void LVRGraphicsView::gentle_zoom(double factor)
19 {
20  scale(factor, factor);
21  centerOn(m_target_scene_pos);
22  QPointF delta_viewport_pos = m_target_viewport_pos - QPointF(viewport()->width() / 2.0, viewport()->height() / 2.0);
23  QPointF viewport_center = mapFromScene(m_target_scene_pos) - delta_viewport_pos;
24  centerOn(mapToScene(viewport_center.toPoint()));
25  Q_EMIT(zoomed());
26 }
27 
28 void LVRGraphicsView::set_modifiers(Qt::KeyboardModifiers modifiers)
29 {
30  m_modifiers = modifiers;
31 }
32 
34 {
35  m_zoom_factor_base = value;
36 }
37 
38 bool LVRGraphicsView::eventFilter(QObject* object, QEvent* event)
39 {
40 
41  if (event->type() == QEvent::MouseMove) {
42  QMouseEvent* mouse_event = static_cast<QMouseEvent*>(event);
43  QPointF delta = m_target_viewport_pos - mouse_event->pos();
44  if (qAbs(delta.x()) > 5 || qAbs(delta.y()) > 5)
45  {
46  m_target_viewport_pos = mouse_event->pos();
47  m_target_scene_pos = mapToScene(mouse_event->pos());
48  }
49  } else if (event->type() == QEvent::Wheel) {
50  QWheelEvent* wheel_event = static_cast<QWheelEvent*>(event);
51  if (QApplication::keyboardModifiers() == m_modifiers) {
52  if (wheel_event->orientation() == Qt::Vertical) {
53  double angle = wheel_event->angleDelta().y();
54  double factor = qPow(m_zoom_factor_base, angle);
55  gentle_zoom(factor);
56  return true;
57  }
58  }
59  }
60 
61  Q_UNUSED(object)
62  return false;
63 }
64 
65 void LVRGraphicsView::closeEvent(QCloseEvent *event)
66 {
67  Q_EMIT(closed());
68 }
69 
70 } // namespace lvr2
void set_zoom_factor_base(double value)
bool eventFilter(QObject *object, QEvent *event)
void closeEvent(QCloseEvent *event)
void gentle_zoom(double factor)
void set_modifiers(Qt::KeyboardModifiers modifiers)
Qt::KeyboardModifiers m_modifiers


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:08