26 #include <QMouseEvent>
27 #include <QApplication>
31 #include <QGraphicsItem>
43 : QObject(view), _view(view)
45 _view->viewport()->installEventFilter(
this);
46 _view->setMouseTracking(
true);
47 _modifiers = Qt::ControlModifier;
48 _zoom_factor_base = 1.0015;
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()));
64 _modifiers = modifiers;
70 _zoom_factor_base =
value;
78 bool Graphics_view_zoom::eventFilter(QObject*
object, QEvent* event)
80 if (event->type() == QEvent::MouseMove)
82 QMouseEvent* mouse_event =
static_cast<QMouseEvent*
>(event);
83 QPointF delta = target_viewport_pos - mouse_event->pos();
85 if (fabs(delta.x()) > 5 || fabs(delta.y()) > 5)
87 target_viewport_pos = mouse_event->pos();
88 target_scene_pos = _view->mapToScene(mouse_event->pos());
91 if (mouse_event->modifiers() == Qt::AltModifier)
93 auto center = _view->mapToScene(_view->viewport()->rect().center());
94 auto mat = _view->matrix();
95 QPointF scale(mat.m11(), mat.m22());
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());
105 else if (event->type() == QEvent::Wheel)
107 QWheelEvent* wheel_event =
static_cast<QWheelEvent*
>(event);
109 if (QApplication::keyboardModifiers() == _modifiers)
111 if (wheel_event->orientation() == Qt::Vertical)
113 double angle = wheel_event->delta() * 0.5;
114 double factor = pow(_zoom_factor_base,
angle);
120 else if (event->type() == QEvent::MouseButtonDblClick)
122 QMouseEvent* mouse_event =
static_cast<QMouseEvent*
>(event);
123 QGraphicsItem* item = _view->itemAt(mouse_event->pos());
127 ItemZoomer* zoomer =
new ItemZoomer(_view, item, 500, item);
128 connect(zoomer, SIGNAL(
zoomed()),
this, SIGNAL(
zoomed()));
138 : QGraphicsObject(parent)
142 lt =
new QTimeLine(time,
this);
143 br =
new QTimeLine(time,
this);
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();
152 ltStepSize = (end.topLeft() - start.topLeft()) / 100.0;
153 brStepSize = (end.bottomRight() - start.bottomRight()) / 100.0;
155 lt->setFrameRange(0, 100);
156 br->setFrameRange(0, 100);
158 connect(lt, SIGNAL(frameChanged(
int)), SLOT(setHeight(
int)));
159 connect(br, SIGNAL(frameChanged(
int)), SLOT(setWidth(
int)));
161 connect(lt, SIGNAL(finished()), SLOT(setTimeLineLTFinished()));
162 connect(br, SIGNAL(finished()), SLOT(setTimeLineBRFinished()));
172 void ItemZoomer::setHeight(
int height)
174 curLT = start.topLeft() + ltStepSize * height;
178 void ItemZoomer::setWidth(
int width)
180 curBR = start.bottomRight() + brStepSize * width;
184 void ItemZoomer::setTimeLineBRFinished()
190 void ItemZoomer::setTimeLineLTFinished()
196 void ItemZoomer::deleteSelf()
198 if (brFinished && ltFinished)
204 void ItemZoomer::update()
206 QRectF rect = mItem->mapToScene(mItem->boundingRect()).boundingRect();
207 rect.setTopLeft(curLT);
208 rect.setBottomRight(curBR);
210 mView->fitInView(rect, Qt::KeepAspectRatio);
216 return QRectF(0, 0, 0, 0);