GraphicsViewZoom.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "GraphicsViewZoom.h"
25 
26 #include <QMouseEvent>
27 #include <QApplication>
28 #include <QScrollBar>
29 #include <cmath>
30 #include <iostream>
31 #include <QGraphicsItem>
32 
35 
36 namespace armarx
37 {
38 
39 
40 
41 
43  : QObject(view), _view(view)
44  {
45  _view->viewport()->installEventFilter(this);
46  _view->setMouseTracking(true);
47  _modifiers = Qt::ControlModifier;
48  _zoom_factor_base = 1.0015;
49  }
50 
51  void Graphics_view_zoom::gentle_zoom(double factor)
52  {
53  _view->scale(factor, factor);
54  _view->centerOn(target_scene_pos);
55  QPointF delta_viewport_pos = target_viewport_pos - QPointF(_view->viewport()->width() / 2.0,
56  _view->viewport()->height() / 2.0);
57  QPointF viewport_center = _view->mapFromScene(target_scene_pos) - delta_viewport_pos;
58  _view->centerOn(_view->mapToScene(viewport_center.toPoint()));
59  emit zoomed();
60  }
61 
62  void Graphics_view_zoom::set_modifiers(Qt::KeyboardModifiers modifiers)
63  {
64  _modifiers = modifiers;
65 
66  }
67 
69  {
70  _zoom_factor_base = value;
71  }
72 
73  QGraphicsView* Graphics_view_zoom::getView() const
74  {
75  return _view;
76  }
77 
78  bool Graphics_view_zoom::eventFilter(QObject* object, QEvent* event)
79  {
80  if (event->type() == QEvent::MouseMove)
81  {
82  QMouseEvent* mouse_event = static_cast<QMouseEvent*>(event);
83  QPointF delta = target_viewport_pos - mouse_event->pos();
84 
85  if (fabs(delta.x()) > 5 || fabs(delta.y()) > 5)
86  {
87  target_viewport_pos = mouse_event->pos();
88  target_scene_pos = _view->mapToScene(mouse_event->pos());
89  }
90 
91  if (mouse_event->modifiers() == Qt::AltModifier)
92  {
93  auto center = _view->mapToScene(_view->viewport()->rect().center());
94  auto mat = _view->matrix();
95  QPointF scale(mat.m11(), mat.m22());
96 
97  // delta = _view->scale(delta.toPoint());
98  delta.setX(delta.x() / scale.x());
99  delta.setY(delta.y() / scale.y());
100  _view->centerOn(center.x() + delta.x(), center.y() + delta.y());
101  return true;
102 
103  }
104  }
105  else if (event->type() == QEvent::Wheel)
106  {
107  QWheelEvent* wheel_event = static_cast<QWheelEvent*>(event);
108 
109  if (QApplication::keyboardModifiers() == _modifiers)
110  {
111  if (wheel_event->orientation() == Qt::Vertical)
112  {
113  double angle = wheel_event->delta() * 0.5;
114  double factor = pow(_zoom_factor_base, angle);
115  gentle_zoom(factor);
116  return true;
117  }
118  }
119  }
120  else if (event->type() == QEvent::MouseButtonDblClick)
121  {
122  QMouseEvent* mouse_event = static_cast<QMouseEvent*>(event);
123  QGraphicsItem* item = _view->itemAt(mouse_event->pos());
124 
125  if (item)
126  {
127  ItemZoomer* zoomer = new ItemZoomer(_view, item, 500, item);
128  connect(zoomer, SIGNAL(zoomed()), this, SIGNAL(zoomed()));
129  // _view->fitInView(item->mapRectToScene(item->boundingRect().adjusted(-50,-50,50,50)), Qt::KeepAspectRatio);
130  }
131  }
132 
133  Q_UNUSED(object)
134  return false;
135  }
136 
137  ItemZoomer::ItemZoomer(QGraphicsView* view, const QGraphicsItem* item, int time, QGraphicsItem* parent)
138  : QGraphicsObject(parent)
139  , mView(view)
140  , mItem(item)
141  {
142  lt = new QTimeLine(time, this);
143  br = new QTimeLine(time, this);
144  brFinished = false;
145  ltFinished = false;
146 
147  start = mView->mapToScene(mView->rect()).boundingRect();
148  QRectF end = mItem->mapToScene(mItem->boundingRect().adjusted(-50, -50, 50, 50)).boundingRect();
149  curLT = start.topLeft();
150  curBR = start.bottomRight();
151 
152  ltStepSize = (end.topLeft() - start.topLeft()) / 100.0;
153  brStepSize = (end.bottomRight() - start.bottomRight()) / 100.0;
154 
155  lt->setFrameRange(0, 100);
156  br->setFrameRange(0, 100);
157 
158  connect(lt, SIGNAL(frameChanged(int)), SLOT(setHeight(int)));
159  connect(br, SIGNAL(frameChanged(int)), SLOT(setWidth(int)));
160 
161  connect(lt, SIGNAL(finished()), SLOT(setTimeLineLTFinished()));
162  connect(br, SIGNAL(finished()), SLOT(setTimeLineBRFinished()));
163  lt->start();
164  br->start();
165  }
166 
168  {
169 
170  }
171 
172  void ItemZoomer::setHeight(int height)
173  {
174  curLT = start.topLeft() + ltStepSize * height;
175  update();
176  }
177 
178  void ItemZoomer::setWidth(int width)
179  {
180  curBR = start.bottomRight() + brStepSize * width;
181  update();
182  }
183 
184  void ItemZoomer::setTimeLineBRFinished()
185  {
186  brFinished = true;
187  deleteSelf();
188  }
189 
190  void ItemZoomer::setTimeLineLTFinished()
191  {
192  ltFinished = true;
193  deleteSelf();
194  }
195 
196  void ItemZoomer::deleteSelf()
197  {
198  if (brFinished && ltFinished)
199  {
200  deleteLater();
201  }
202 
203  }
204  void ItemZoomer::update()
205  {
206  QRectF rect = mItem->mapToScene(mItem->boundingRect()).boundingRect();
207  rect.setTopLeft(curLT);
208  rect.setBottomRight(curBR);
209 
210  mView->fitInView(rect, Qt::KeepAspectRatio);
211  emit zoomed();
212  }
213 
215  {
216  return QRectF(0, 0, 0, 0);
217  }
218 
219  void ItemZoomer::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
220  {
221  }
222 
223 } // namespace armarx
224 
225 
226 
armarx::Graphics_view_zoom::set_modifiers
void set_modifiers(Qt::KeyboardModifiers modifiers)
Definition: GraphicsViewZoom.cpp:62
armarx::ItemZoomer::boundingRect
QRectF boundingRect() const override
Definition: GraphicsViewZoom.cpp:214
GraphicsViewZoom.h
armarx::Graphics_view_zoom::zoomed
void zoomed()
armarx::Graphics_view_zoom::gentle_zoom
void gentle_zoom(double factor)
Definition: GraphicsViewZoom.cpp:51
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::ItemZoomer::zoomed
void zoomed()
armarx::Graphics_view_zoom::Graphics_view_zoom
Graphics_view_zoom(QGraphicsView *view)
Definition: GraphicsViewZoom.cpp:42
option
#define option(type, fn)
armarx::Graphics_view_zoom::getView
QGraphicsView * getView() const
Definition: GraphicsViewZoom.cpp:73
ArmarXWidgetController.h
armarx::Graphics_view_zoom::set_zoom_factor_base
void set_zoom_factor_base(double value)
Definition: GraphicsViewZoom.cpp:68
angle
double angle(const Point &a, const Point &b, const Point &c)
Definition: point.hpp:100
Logging.h
armarx::ItemZoomer::ItemZoomer
ItemZoomer(QGraphicsView *view, const QGraphicsItem *item, int time, QGraphicsItem *parent=0)
Definition: GraphicsViewZoom.cpp:137
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::ItemZoomer::~ItemZoomer
~ItemZoomer() override
Definition: GraphicsViewZoom.cpp:167
armarx::ItemZoomer::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Definition: GraphicsViewZoom.cpp:219