bool MapView::event(QEvent *e) { // Ignore space bar events since they're handled by the MainWindow if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease) { if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Space) { e->ignore(); return false; } } else if (e->type() == QEvent::Gesture) { QGestureEvent *gestureEvent = static_cast<QGestureEvent *>(e); if (QGesture *gesture = gestureEvent->gesture(Qt::PinchGesture)) { QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture); if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged) handlePinchGesture(pinch); } } else if (e->type() == QEvent::ShortcutOverride) { auto keyEvent = static_cast<QKeyEvent*>(e); if (Utils::isZoomInShortcut(keyEvent) || Utils::isZoomOutShortcut(keyEvent) || Utils::isResetZoomShortcut(keyEvent)) { e->accept(); return true; } } return QGraphicsView::event(e); }
bool TouchSupportManager::handleGesture(QGestureEvent *gestureEvent) { if (gestureEvent->gesture(Qt::TapGesture)) { mScroller.onTap(); } else if (QGesture *tapAndHold = gestureEvent->gesture(Qt::TapAndHoldGesture)) { // Filters out tap & hold with mouse if (mFingersInGesture > 0) { processGestureState(tapAndHold); simulateRightClick(static_cast<QTapAndHoldGesture *>(tapAndHold)); } } else if (QGesture *pan = gestureEvent->gesture(Qt::PanGesture)) { processGestureState(pan); if (mFingersInGesture > 2) { mScroller.onPan(pan); } } else if (QGesture *pinch = gestureEvent->gesture(Qt::PinchGesture)) { processGestureState(pinch); QPinchGesture *pinchGesture = static_cast<QPinchGesture *>(pinch); mEditorView->setTransformationAnchor(QGraphicsView::AnchorViewCenter); mEditorView->zoom(pinchGesture->scaleFactor()); mEditorView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse); } return true; }
void QPinchGestureRecognizer::reset(QGesture *state) { QPinchGesture *pinch = static_cast<QPinchGesture *>(state); QPinchGesturePrivate *d = pinch->d_func(); d->totalChangeFlags = d->changeFlags = 0; d->startCenterPoint = d->lastCenterPoint = d->centerPoint = QPointF(); d->totalScaleFactor = d->lastScaleFactor = d->scaleFactor = 1; d->totalRotationAngle = d->lastRotationAngle = d->rotationAngle = 0; d->isNewSequence = true; d->startPosition[0] = d->startPosition[1] = QPointF(); QGestureRecognizer::reset(state); }
bool TouchSupportManager::handleGesture(QGestureEvent *gestureEvent) { if (gestureEvent->gesture(Qt::TapGesture)) { mScroller.onTap(); } else if (QGesture *tapAndHold = gestureEvent->gesture(Qt::TapAndHoldGesture)) { if (mFingersInGesture > 0) { processGestureState(tapAndHold); simulateRightClick(static_cast<QTapAndHoldGesture *>(tapAndHold)); } } else if (QGesture *pan = gestureEvent->gesture(Qt::PanGesture)) { processGestureState(pan); mScroller.onPan(pan); } else if (QGesture *pinch = gestureEvent->gesture(Qt::PinchGesture)) { processGestureState(pinch); QPinchGesture *pinchGesture = static_cast<QPinchGesture *>(pinch); mEditorView->zoom(pinchGesture->scaleFactor()); } return true; }
bool View::gestureEvent(QGestureEvent* event) { // // pinch has precedence if (QGesture *pinchEvent = event->gesture(Qt::PinchGesture)) { QPinchGesture* pinch = static_cast<QPinchGesture*>(pinchEvent); // // only pinch if the fingers have already moved a significant amount qreal totalScaleFactor = pinch->totalScaleFactor(); if((totalScaleFactor < 0.66) || (totalScaleFactor > 1.5)){ qreal zoomDelta = pinch->scaleFactor(); qreal resultZoom = m_zoomFactor * zoomDelta; if(resultZoom > s_maxZoomFactor){ zoomDelta = s_maxZoomFactor / m_zoomFactor; }else if(resultZoom < s_minZoomFactor){ zoomDelta = s_minZoomFactor / m_zoomFactor; } // scale the view scale(zoomDelta,zoomDelta); m_zoomFactor *= zoomDelta; return true; } } // // pan if (QGesture *panEvent = event->gesture(Qt::PanGesture)) { QPanGesture* pan = static_cast<QPanGesture*>(panEvent); QPointF delta = pan->delta(); qreal factor = (1.0 / m_zoomFactor) * 0.9; QScrollBar* vScrollBar = verticalScrollBar(); vScrollBar->setValue(vScrollBar->value() - int(delta.y()/factor)); QScrollBar* hScrollBar = horizontalScrollBar(); hScrollBar->setValue(hScrollBar->value() - int(delta.x()/factor)); } return true; }
bool MapView::event(QEvent *e) { // Ignore space bar events since they're handled by the MainWindow if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease) { if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Space) { e->ignore(); return false; } } else if (e->type() == QEvent::Gesture) { QGestureEvent *gestureEvent = static_cast<QGestureEvent *>(e); if (QGesture *gesture = gestureEvent->gesture(Qt::PinchGesture)) { QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture); if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged) handlePinchGesture(pinch); } } return QGraphicsView::event(e); }
bool HPiano::gestureEvent(QGestureEvent *event) { if (QGesture *gesture = event->gesture(Qt::PinchGesture)) { // Zoom in/out when receiving a pinch gesture QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture); static qreal magStart = 1.0; if (pinch->state() == Qt::GestureStarted) { magStart = scaleVal; } if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged) { // On Windows, totalScaleFactor() contains the net magnification. // On OS X, totalScaleFactor() is 1, and scaleFactor() contains the net magnification. qreal value = pinch->totalScaleFactor(); if (value == 1) { value = pinch->scaleFactor(); } // Qt 5.4 doesn't report pinch->centerPoint() correctly setScale(magStart*value); } } return true; }
bool CanvasQt::gestureEvent(QGestureEvent* ge) { QGesture* gesture = nullptr; QPanGesture* panGesture = nullptr; QPinchGesture* pinchGesture = nullptr; if((gesture = ge->gesture(Qt::PanGesture))){ panGesture = static_cast<QPanGesture *>(gesture); } if ((gesture = ge->gesture(Qt::PinchGesture))){ pinchGesture = static_cast<QPinchGesture *>(gesture); } if(panGesture && pinchGesture){ double absDeltaDist = glm::abs(static_cast<double>(pinchGesture->scaleFactor())-1.0); if(absDeltaDist > 0.05 || (lastType_ == Qt::PinchGesture || lastType_ != Qt::PanGesture)){ lastType_ = Qt::PinchGesture; pinchTriggered(pinchGesture); } else{ lastType_ = Qt::PanGesture; panTriggered(panGesture); } } else if(panGesture){ lastType_ = Qt::PanGesture; panTriggered(panGesture); } else if(pinchGesture){ double absDeltaDist = glm::abs(static_cast<double>(pinchGesture->scaleFactor())-1.0); if(absDeltaDist > 0.05) lastType_ = Qt::PinchGesture; pinchTriggered(pinchGesture); } ge->accept(); return true; }
bool SimObjectWidget::event(QEvent* event) { if(event->type() == QEvent::Gesture) { QPinchGesture* pinch = static_cast<QPinchGesture*>(static_cast<QGestureEvent*>(event)->gesture(Qt::PinchGesture)); if(pinch && (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged)) { float change = static_cast<float>(pinch->scaleFactor() > pinch->lastScaleFactor() ? -pinch->scaleFactor() / pinch->lastScaleFactor() : pinch->lastScaleFactor() / pinch->scaleFactor()); objectRenderer.zoom(change * 100.f); update(); return true; } } return QGLWidget::event(event); }
void CanvasView::gestureEvent(QGestureEvent *event) { QPinchGesture *pinch = static_cast<QPinchGesture*>(event->gesture(Qt::PinchGesture)); if(pinch) { if(pinch->state() == Qt::GestureStarted) { _gestureStartZoom = _zoom; _gestureStartAngle = _rotate; } if((pinch->changeFlags() & QPinchGesture::ScaleFactorChanged)) setZoom(_gestureStartZoom * pinch->scaleFactor()); if((pinch->changeFlags() & QPinchGesture::RotationAngleChanged)) setRotation(_gestureStartAngle + pinch->rotationAngle()); } }
QGestureRecognizer::Result QPinchGestureRecognizer::recognize(QGesture *state, QObject *, QEvent *event) { QPinchGesture *q = static_cast<QPinchGesture *>(state); QPinchGesturePrivate *d = q->d_func(); const QTouchEvent *ev = static_cast<const QTouchEvent *>(event); QGestureRecognizer::Result result; switch (event->type()) { case QEvent::TouchBegin: { result = QGestureRecognizer::MayBeGesture; break; } case QEvent::TouchEnd: { if (q->state() != Qt::NoGesture) { result = QGestureRecognizer::FinishGesture; } else { result = QGestureRecognizer::CancelGesture; } break; } case QEvent::TouchUpdate: { d->changeFlags = 0; if (ev->touchPoints().size() == 2) { QTouchEvent::TouchPoint p1 = ev->touchPoints().at(0); QTouchEvent::TouchPoint p2 = ev->touchPoints().at(1); d->hotSpot = p1.screenPos(); d->isHotSpotSet = true; QPointF centerPoint = (p1.screenPos() + p2.screenPos()) / 2.0; if (d->isNewSequence) { d->startPosition[0] = p1.screenPos(); d->startPosition[1] = p2.screenPos(); d->lastCenterPoint = centerPoint; } else { d->lastCenterPoint = d->centerPoint; } d->centerPoint = centerPoint; d->changeFlags |= QPinchGesture::CenterPointChanged; if (d->isNewSequence) { d->scaleFactor = 1.0; d->lastScaleFactor = 1.0; } else { d->lastScaleFactor = d->scaleFactor; QLineF line(p1.screenPos(), p2.screenPos()); QLineF lastLine(p1.lastScreenPos(), p2.lastScreenPos()); d->scaleFactor = line.length() / lastLine.length(); } d->totalScaleFactor = d->totalScaleFactor * d->scaleFactor; d->changeFlags |= QPinchGesture::ScaleFactorChanged; qreal angle = QLineF(p1.screenPos(), p2.screenPos()).angle(); if (angle > 180) angle -= 360; qreal startAngle = QLineF(p1.startScreenPos(), p2.startScreenPos()).angle(); if (startAngle > 180) startAngle -= 360; const qreal rotationAngle = startAngle - angle; if (d->isNewSequence) d->lastRotationAngle = 0.0; else d->lastRotationAngle = d->rotationAngle; d->rotationAngle = rotationAngle; d->totalRotationAngle += d->rotationAngle - d->lastRotationAngle; d->changeFlags |= QPinchGesture::RotationAngleChanged; d->totalChangeFlags |= d->changeFlags; d->isNewSequence = false; result = QGestureRecognizer::TriggerGesture; } else { d->isNewSequence = true; if (q->state() == Qt::NoGesture) result = QGestureRecognizer::Ignore; else result = QGestureRecognizer::FinishGesture; } break; } case QEvent::MouseButtonPress: case QEvent::MouseMove: case QEvent::MouseButtonRelease: result = QGestureRecognizer::Ignore; break; default: result = QGestureRecognizer::Ignore; break; } return result; }
//=============================================================== bool MousePanNZoomNavigator::eventFilter(QObject *widget, QEvent *e) { // according to Qt's doc, this constant has been defined by wheel mouse vendors // we need it to interpret the value of QWheelEvent->delta() #define WHEEL_DELTA 120 if (e->type() == QEvent::Wheel && (((QWheelEvent *) e)->orientation() == Qt::Vertical)) { GlMainWidget *g = (GlMainWidget *) widget; if(((QWheelEvent *) e)->delta() < 0 && g->getScene()->getCamera().getZoomFactor() < 0.5f) { return true; } g->getScene()->zoomXY(((QWheelEvent *) e)->delta() / WHEEL_DELTA, ((QWheelEvent *) e)->x(), ((QWheelEvent *) e)->y()); g->draw(false); return true; } if(e->type() == QEvent::Gesture) { GlMainWidget *g = (GlMainWidget *) widget; QGestureEvent* gesture = (QGestureEvent*)e; QPointF center; //swipe events and pan events are never fired, known Qt bug /*if(gesture->gesture(Qt::SwipeGesture)) { QSwipeGesture* swipe = (QSwipeGesture*)gesture->gesture(Qt::SwipeGesture); int x = cos(swipe->swipeAngle()) * swipe->property("velocity").toFloat(); int y = sin(swipe->swipeAngle()) * swipe->property("velocity").toFloat(); g->getScene()->translateCamera(x, y, 0); }*/ if(gesture->gesture(Qt::PinchGesture)) { QPinchGesture* pinch = (QPinchGesture*)gesture->gesture(Qt::PinchGesture); Camera& camera = g->getScene()->getCamera(); //store the camera scale factor when starting the gesture if(pinch->state() == Qt::GestureStarted) { cameraScaleFactor = camera.getZoomFactor(); isGesturing = true; } if(pinch->changeFlags() & QPinchGesture::ScaleFactorChanged) { //only set the zoom factor if two events in a row were in the same direction (zoom in or out) to smooth a bit the effect. if((pinch->lastScaleFactor() > 1 && pinch->scaleFactor() > 1) || (pinch->lastScaleFactor() <= 1 && pinch->scaleFactor() <= 1)) { camera.setZoomFactor(cameraScaleFactor * pinch->totalScaleFactor()); } } if(pinch->changeFlags() & QPinchGesture::RotationAngleChanged) { /*//backup the current camera center Coord oldCenter = camera.getCenter(); Coord oldEye = camera.getEyes(); //sets the camera center to the center of the pich gesture Coord rotationCenter(g->mapFromGlobal(pinch->centerPoint().toPoint()).x(), g->mapFromGlobal(pinch->centerPoint().toPoint()).y(), oldCenter.getZ()); Coord rotationEye=camera.getEyes()+(rotationCenter-oldCenter); camera.setCenter(rotationCenter); camera.setEyes(rotationEye);*/ //rotates the camera camera.rotate((pinch->rotationAngle() - pinch->lastRotationAngle())/180*M_PI, 0, 0, 1); /* //restore old camera center and eyes camera.setCenter(oldCenter); camera.setEyes(oldEye); */ } if(pinch->state() == Qt::GestureFinished) { isGesturing = false; } if(gesture->gesture(Qt::PanGesture)) { QPanGesture* pan = (QPanGesture*)gesture->gesture(Qt::PanGesture); if(pan->state() == Qt::GestureStarted) { isGesturing = true; } if(pan->state() == Qt::GestureFinished) { isGesturing = false; } center = pan->delta(); g->getScene()->translateCamera(pan->delta().x(), -pan->delta().y(), 0); } } g->draw(false); return true; } return false; }