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 <cmath>
27#include <iostream>
28
29#include <QApplication>
30#include <QGraphicsItem>
31#include <QMouseEvent>
32#include <QScrollBar>
33
35
37
38namespace armarx
39{
40
41
42 Graphics_view_zoom::Graphics_view_zoom(QGraphicsView* view) : QObject(view), _view(view)
43 {
44 _view->viewport()->installEventFilter(this);
45 _view->setMouseTracking(true);
46 _modifiers = Qt::ControlModifier;
47 _zoom_factor_base = 1.0015;
48 }
49
50 void
52 {
53 _view->scale(factor, factor);
54 _view->centerOn(target_scene_pos);
55 QPointF delta_viewport_pos =
56 target_viewport_pos -
57 QPointF(_view->viewport()->width() / 2.0, _view->viewport()->height() / 2.0);
58 QPointF viewport_center = _view->mapFromScene(target_scene_pos) - delta_viewport_pos;
59 _view->centerOn(_view->mapToScene(viewport_center.toPoint()));
60 emit zoomed();
61 }
62
63 void
64 Graphics_view_zoom::set_modifiers(Qt::KeyboardModifiers modifiers)
65 {
66 _modifiers = modifiers;
67 }
68
69 void
71 {
72 _zoom_factor_base = value;
73 }
74
75 QGraphicsView*
77 {
78 return _view;
79 }
80
81 bool
82 Graphics_view_zoom::eventFilter(QObject* object, QEvent* event)
83 {
84 if (event->type() == QEvent::MouseMove)
85 {
86 QMouseEvent* mouse_event = static_cast<QMouseEvent*>(event);
87 QPointF delta = target_viewport_pos - mouse_event->pos();
88
89 if (fabs(delta.x()) > 5 || fabs(delta.y()) > 5)
90 {
91 target_viewport_pos = mouse_event->pos();
92 target_scene_pos = _view->mapToScene(mouse_event->pos());
93 }
94
95 if (mouse_event->modifiers() == Qt::AltModifier)
96 {
97 auto center = _view->mapToScene(_view->viewport()->rect().center());
98 auto mat = _view->matrix();
99 QPointF scale(mat.m11(), mat.m22());
100
101 // delta = _view->scale(delta.toPoint());
102 delta.setX(delta.x() / scale.x());
103 delta.setY(delta.y() / scale.y());
104 _view->centerOn(center.x() + delta.x(), center.y() + delta.y());
105 return true;
106 }
107 }
108 else if (event->type() == QEvent::Wheel)
109 {
110 QWheelEvent* wheel_event = static_cast<QWheelEvent*>(event);
111
112 if (QApplication::keyboardModifiers() == _modifiers)
113 {
114 if (wheel_event->orientation() == Qt::Vertical)
115 {
116 double angle = wheel_event->delta() * 0.5;
117 double factor = pow(_zoom_factor_base, angle);
118 gentle_zoom(factor);
119 return true;
120 }
121 }
122 }
123 else if (event->type() == QEvent::MouseButtonDblClick)
124 {
125 QMouseEvent* mouse_event = static_cast<QMouseEvent*>(event);
126 QGraphicsItem* item = _view->itemAt(mouse_event->pos());
127
128 if (item)
129 {
130 ItemZoomer* zoomer = new ItemZoomer(_view, item, 500, item);
131 connect(zoomer, SIGNAL(zoomed()), this, SIGNAL(zoomed()));
132 // _view->fitInView(item->mapRectToScene(item->boundingRect().adjusted(-50,-50,50,50)), Qt::KeepAspectRatio);
133 }
134 }
135
136 Q_UNUSED(object)
137 return false;
138 }
139
140 ItemZoomer::ItemZoomer(QGraphicsView* view,
141 const QGraphicsItem* item,
142 int time,
143 QGraphicsItem* parent) :
144 QGraphicsObject(parent), mView(view), mItem(item)
145 {
146 lt = new QTimeLine(time, this);
147 br = new QTimeLine(time, this);
148 brFinished = false;
149 ltFinished = false;
150
151 start = mView->mapToScene(mView->rect()).boundingRect();
152 QRectF end =
153 mItem->mapToScene(mItem->boundingRect().adjusted(-50, -50, 50, 50)).boundingRect();
154 curLT = start.topLeft();
155 curBR = start.bottomRight();
156
157 ltStepSize = (end.topLeft() - start.topLeft()) / 100.0;
158 brStepSize = (end.bottomRight() - start.bottomRight()) / 100.0;
159
160 lt->setFrameRange(0, 100);
161 br->setFrameRange(0, 100);
162
163 connect(lt, SIGNAL(frameChanged(int)), SLOT(setHeight(int)));
164 connect(br, SIGNAL(frameChanged(int)), SLOT(setWidth(int)));
165
166 connect(lt, SIGNAL(finished()), SLOT(setTimeLineLTFinished()));
167 connect(br, SIGNAL(finished()), SLOT(setTimeLineBRFinished()));
168 lt->start();
169 br->start();
170 }
171
175
176 void
177 ItemZoomer::setHeight(int height)
178 {
179 curLT = start.topLeft() + ltStepSize * height;
180 update();
181 }
182
183 void
184 ItemZoomer::setWidth(int width)
185 {
186 curBR = start.bottomRight() + brStepSize * width;
187 update();
188 }
189
190 void
191 ItemZoomer::setTimeLineBRFinished()
192 {
193 brFinished = true;
194 deleteSelf();
195 }
196
197 void
198 ItemZoomer::setTimeLineLTFinished()
199 {
200 ltFinished = true;
201 deleteSelf();
202 }
203
204 void
205 ItemZoomer::deleteSelf()
206 {
207 if (brFinished && ltFinished)
208 {
209 deleteLater();
210 }
211 }
212
213 void
214 ItemZoomer::update()
215 {
216 QRectF rect = mItem->mapToScene(mItem->boundingRect()).boundingRect();
217 rect.setTopLeft(curLT);
218 rect.setBottomRight(curBR);
219
220 mView->fitInView(rect, Qt::KeepAspectRatio);
221 emit zoomed();
222 }
223
224 QRectF
226 {
227 return QRectF(0, 0, 0, 0);
228 }
229
230 void
231 ItemZoomer::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
232 {
233 }
234
235} // namespace armarx
#define option(type, fn)
void set_zoom_factor_base(double value)
Graphics_view_zoom(QGraphicsView *view)
void set_modifiers(Qt::KeyboardModifiers modifiers)
void gentle_zoom(double factor)
QGraphicsView * getView() const
ItemZoomer(QGraphicsView *view, const QGraphicsItem *item, int time, QGraphicsItem *parent=0)
QRectF boundingRect() const override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
This file offers overloads of toIce() and fromIce() functions for STL container types.
double angle(const Point &a, const Point &b, const Point &c)
Definition point.hpp:109