QDebug operator<<(QDebug dbg, const QGLCamera &cam) { dbg << "QGLCamera"; if (!cam.objectName().isEmpty()) dbg << cam.objectName(); dbg << "\n"; dbg << " projection:" << ( cam.projectionType() == QGLCamera::Perspective ? "Perspective" : "Orthographic" ); dbg << "-- viewsize:" << cam.viewSize().width() << "x" << cam.viewSize().height() << "\n"; dbg << " near-plane:" << cam.nearPlane() << "-- far-plane:" << cam.farPlane(); dbg << "-- field-of-view:" << cam.fieldOfView() << "\n"; dbg << " rotation:" << cam.screenRotation() << " -- motion adjust:" << cam.motionAdjustment() << " -- aspect adjust:" << cam.adjustForAspectRatio() << "\n"; dbg << " eye:" << cam.eye() << "-- center:" << cam.center(); dbg << "-- up:" << cam.upVector() << "\n"; return dbg; }
// Convert deltas in the X and Y directions into percentages of // the view width and height. QPointF QGLGraphicsNavigationItemPrivate::viewDelta(qreal deltax, qreal deltay) { QRectF rect = viewportItem->rect(); qreal w = rect.width(); qreal h = rect.height(); QGLCamera *camera = viewportItem->camera(); bool scaleToWidth; qreal scaleFactor, scaleX, scaleY; QSizeF viewSize = camera->viewSize(); if (w >= h) { if (viewSize.width() >= viewSize.height()) scaleToWidth = true; else scaleToWidth = false; } else { if (viewSize.width() >= viewSize.height()) scaleToWidth = false; else scaleToWidth = true; } int rotation = camera->screenRotation(); if (rotation == 90 || rotation == 270) { scaleToWidth = !scaleToWidth; qSwap(deltax, deltay); } if (rotation == 90 || rotation == 180) { deltax = -deltax; } if (rotation == 180 || rotation == 270) { deltay = -deltay; } if (scaleToWidth) { scaleFactor = 2.0f / viewSize.width(); scaleX = scaleFactor * h / w; scaleY = scaleFactor; } else { scaleFactor = 2.0f / viewSize.height(); scaleX = scaleFactor; scaleY = scaleFactor * w / h; } return QPointF(deltax * scaleX / w, deltay * scaleY / h); }
// Rotate about the object being viewed. void QGLGraphicsNavigationItemPrivate::rotate(qreal deltax, qreal deltay) { QGLCamera *camera = viewportItem->camera(); QRectF rect = viewportItem->rect(); int rotation = camera->screenRotation(); if (rotation == 90 || rotation == 270) { qSwap(deltax, deltay); } if (rotation == 90 || rotation == 180) { deltax = -deltax; } if (rotation == 180 || rotation == 270) { deltay = -deltay; } qreal anglex = deltax * 90.0f / rect.width(); qreal angley = deltay * 90.0f / rect.height(); QQuaternion q = camera->pan(-anglex); q *= camera->tilt(-angley); camera->rotateCenter(q); }
void CubeItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { if (navigating) { QPoint delta = event->pos().toPoint() - pressedPos; int deltax = delta.x(); int deltay = delta.y(); QGLCamera *camera = this->camera(); int rotation = camera->screenRotation(); if (rotation == 90 || rotation == 270) { qSwap(deltax, deltay); } if (rotation == 90 || rotation == 180) { deltax = -deltax; } if (rotation == 180 || rotation == 270) { deltay = -deltay; } qreal anglex = deltax * 90.0f / rect().width(); qreal angley = deltay * 90.0f / rect().height(); QQuaternion q = startNavCamera->pan(-anglex); q *= startNavCamera->tilt(-angley); camera->setEye(startNavCamera->eye()); camera->setCenter(startNavCamera->center()); camera->setUpVector(startNavCamera->upVector()); camera->rotateCenter(q); } else if (pressedFace != -1) { int face; QPoint pos = cubeIntersection (event->widget(), event->pos().toPoint(), &face); if (face != pressedFace) pos = QPoint(-1, -1); deliverSceneEvent(pos, event); return; } QGraphicsItem::mouseMoveEvent(event); }