bool QG_GraphicView::event(QEvent *event) {
#if QT_VERSION >= 0x050200
    if (event->type() == QEvent::NativeGesture) {
        QNativeGestureEvent *nge = static_cast<QNativeGestureEvent *>(event);

        if (nge->gestureType() == Qt::ZoomNativeGesture) {
            double v = nge->value();
            RS2::ZoomDirection direction;
            double factor;

            if (v < 0) {
                direction = RS2::Out;
                factor = 1-v;
            } else {
                direction = RS2::In;
                factor = 1+v;
            }

            // It seems the NativeGestureEvent::pos() incorrectly reports global coordinates
            QPoint g = mapFromGlobal(nge->globalPos());
            RS_Vector mouse = toGraph(RS_Vector(g.x(), g.y()));
            setCurrentAction(new RS_ActionZoomIn(*container, *this, direction,
                                                 RS2::Both, mouse, factor));
        }

        return true;
    }
#endif
	return QWidget::event(event);
}
Example #2
0
bool MyGL::event(QEvent *e) {
    //Call parent method
    QWidget::event(e);
    //Do Not Delete This^

    //Zoom
    if (e->type() == QEvent::NativeGesture) {
       QNativeGestureEvent* native = static_cast<QNativeGestureEvent*>(e);
        if (native->gestureType() == Qt::ZoomNativeGesture) {
              double amount = native->value()*100;
              double total = camera.getZoom() - amount;
              camera.setZoom(total >= 0.5 && total <= 150 ? camera.getZoom()-amount : camera.getZoom());

              resizeGL();
              paintGL();
        }
    }
    //Rotate & Pan
    if (e->type() == QEvent::MouseMove) {
        QMouseEvent* mouse = static_cast<QMouseEvent*>(e);
        if(mouse->buttons() == Qt::LeftButton) {
            //If Shift is held Down
            if (mouse->modifiers() == Qt::ShiftModifier) {
                //If mouseInitPosition is unitialized (10000), set the current mouse position as it's initial
                if (mouseInitPosition.x() == 10000) {
                    mouseInitPosition = mouse->pos();
                }

                //Change in X or Y is the mouse's new - old positions
                float delX = (mouse->pos().x()-mouseInitPosition.x());
                delX /= 2.0; //<---- For aesthetics
                float delY = (mouse->pos().y()-mouseInitPosition.y());
                delY /= 2.0; //<---- For aesthetics

                //The new Position is a result of moving the camera along it's local up axis delY units.
                //To reach this we normalize the up vector, multiply it by delY, then add that to camera.
                glm::vec3 newPos = camera.getPosition() - (glm::normalize(camera.getUp()) * delY);
                newPos += (glm::normalize(camera.getLeft()) * delX);

                //Same movement for target
                glm::vec3 newTarget = camera.getTarget() - (glm::normalize(camera.getUp()) * delY);
                newTarget += (glm::normalize(camera.getLeft()) * delX);

                camera.setPosition(newPos);
                camera.setTarget(newTarget);

                //This is so the next time the mouse move event happens,
                //we calculate the change in mouse positions using this
                //as the initial position.
                mouseInitPosition = mouse->pos();//Reset Mouse Increment

            } else { //If no shift, then rotate
                if (mouseInitPosition.x() == 10000) {
                    mouseInitPosition = mouse->pos();
                }

                //To avoid weird flipping bug past Phi = 180, we limit Phi
                float newPhi = camera.getPhi() - (mouse->pos().y()-mouseInitPosition.y());
                if (newPhi < 180 && newPhi > 0) {
                    camera.setPhi(newPhi);
                }

                //Just in case user rotates in circles a lot of time
                //we make theta loop from 0-360.
                float newTheta = camera.getTheta() + (mouse->pos().x()-mouseInitPosition.x());
                if (newTheta > 360) {
                    camera.setTheta(newTheta - 360);
                } else {
                    camera.setTheta(newTheta);
                }
                mouseInitPosition = mouse->pos();
            }

            resizeGL();
            paintGL();
        }
    } else if (e->type() == QEvent::MouseButtonRelease) {
        //When user releases the mouse, 'uninitialze the mouseInitPosition'
        mouseInitPosition = QPoint(10000,10000);
    }

    return e->isAccepted();
}