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
lvr2::LVRGraphicsView::m_modifiers
Qt::KeyboardModifiers m_modifiers
Definition: LVRGraphicsView.hpp:27
lvr2::LVRGraphicsView::gentle_zoom
void gentle_zoom(double factor)
Definition: LVRGraphicsView.cpp:18
lvr2::LVRGraphicsView::m_zoom_factor_base
double m_zoom_factor_base
Definition: LVRGraphicsView.hpp:28
lvr2::LVRGraphicsView::closeEvent
void closeEvent(QCloseEvent *event)
Definition: LVRGraphicsView.cpp:65
lvr2::LVRGraphicsView::zoomed
void zoomed()
lvr2::LVRGraphicsView::set_modifiers
void set_modifiers(Qt::KeyboardModifiers modifiers)
Definition: LVRGraphicsView.cpp:28
lvr2::LVRGraphicsView::m_target_scene_pos
QPointF m_target_scene_pos
Definition: LVRGraphicsView.hpp:29
lvr2
Definition: BaseBufferManipulators.hpp:39
lvr2::LVRGraphicsView::set_zoom_factor_base
void set_zoom_factor_base(double value)
Definition: LVRGraphicsView.cpp:33
lvr2::LVRGraphicsView::eventFilter
bool eventFilter(QObject *object, QEvent *event)
Definition: LVRGraphicsView.cpp:38
LVRGraphicsView.hpp
lvr2::LVRGraphicsView::init
void init()
Definition: LVRGraphicsView.cpp:10
lvr2::LVRGraphicsView::m_target_viewport_pos
QPointF m_target_viewport_pos
Definition: LVRGraphicsView.hpp:30
lvr2::LVRGraphicsView::closed
void closed()


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 Wed Mar 2 2022 00:37:24