void ViewportView::wheelEvent(QWheelEvent* event) { QVector3D a = sceneToWorld(mapToScene(event->pos())); scale *= pow(1.001, -event->delta()); QVector3D b = sceneToWorld(mapToScene(event->pos())); center += a - b; update(); }
void Canvas::wheelEvent(QWheelEvent *event) { QVector3D a = sceneToWorld(mapToScene(event->pos())); scale *= pow(1.001, -event->delta()); QVector3D b = sceneToWorld(mapToScene(event->pos())); pan(a - b); emit(viewChanged()); }
void ViewportView::mouseMoveEvent(QMouseEvent* event) { QGraphicsView::mouseMoveEvent(event); current_pos = event->pos(); if (scene()->mouseGrabberItem() == NULL) { if (event->buttons() == Qt::LeftButton) { center += click_pos_world - sceneToWorld(mapToScene(event->pos())); update(); } else if (event->buttons() == Qt::RightButton && !angle_locked) { QPointF d = click_pos - event->pos(); pitch = fmin(0, fmax(-M_PI, pitch - 0.01 * d.y())); yaw = fmod(yaw + M_PI - 0.01 * d.x(), M_PI*2) - M_PI; click_pos = event->pos(); update(); } dragged = true; } // Redraw to update cursor position scene()->invalidate(QRect(), QGraphicsScene::ForegroundLayer); }
void Viewport::mouseMoveEvent(QMouseEvent *event) { _dragging = true; _current_pos = event->pos(); QGraphicsView::mouseMoveEvent(event); if (scene->mouseGrabberItem() == NULL) { if (event->buttons() == Qt::LeftButton) { pan(_click_pos_world - sceneToWorld(mapToScene(event->pos()))); } else if (event->buttons() == Qt::RightButton && !angle_locked) { QPointF d = _click_pos - event->pos(); pitch = fmin(0, fmax(-M_PI, pitch - 0.01 * d.y())); yaw = fmod(yaw + M_PI - 0.01 * d.x(), M_PI*2) - M_PI; _click_pos = event->pos(); update(); scene->invalidate(QRect(),QGraphicsScene::ForegroundLayer); emit(viewChanged()); } } updateInfo(); }
void Canvas::mousePressEvent(QMouseEvent *event) { // On right-click, show a menu of items to raise. if (event->button() == Qt::RightButton) { auto menu = new QMenu(this); QSet<Control*> used; for (auto i : items(event->pos())) { while (i->parentItem()) { i = i->parentItem(); } auto c = dynamic_cast<Control*>(i); if (c && !used.contains(c) && !dynamic_cast<AxesControl*>(i)) { auto n = c->getNode(); auto a = new QAction( n->getName() + " (" + n->getType() + ")", menu); a->setData(QVariant::fromValue(i)); menu->addAction(a); used << c; } } if (!menu->isEmpty()) { QAction* chosen = menu->exec(QCursor::pos()); if (chosen) { if (raised) { raised->setZValue(1); } raised = static_cast<Control*>( chosen->data().value<QGraphicsItem*>()); raised->setZValue(1.1); } } menu->deleteLater(); } QGraphicsView::mousePressEvent(event); if (!event->isAccepted()) { if (event->button() == Qt::LeftButton) { _click_pos = mapToScene(event->pos()); _click_pos_world = sceneToWorld(_click_pos); } else { _click_pos = event->pos(); } } }
void ViewportView::drawCoords(QPainter* painter) const { QPointF mouse_pos = mapToScene(mapFromGlobal(QCursor::pos())); if (!sceneRect().contains(mouse_pos)) { return; } // Get rotate-only transform matrix QMatrix4x4 M = getMatrix(ROT); const float threshold = 0.98; const auto a = M.inverted() * QVector3D(0, 0, 1); QList<QPair<char, QVector3D>> axes = { {'x', QVector3D(1, 0, 0)}, {'y', QVector3D(0, 1, 0)}, {'z', QVector3D(0, 0, 1)}}; char axis = 0; float opacity = 0; for (const auto v : axes) { float dot = fabs(QVector3D::dotProduct(a, v.second)); if (dot > threshold) { axis = v.first; opacity = (dot - threshold) / (1 - threshold); } } auto p = sceneToWorld(mouse_pos); int value = opacity * 200; painter->setPen(QPen(QColor(255, 255, 255, value))); QString txt; if (axis == 'z') { txt = QString("X: %1\nY: %2").arg(p.x()).arg(p.y()); } else if (axis == 'y') { txt = QString("X: %1\nZ: %2").arg(p.x()).arg(p.z()); } else if (axis == 'x') { txt = QString("Y: %1\nZ: %2").arg(p.y()).arg(p.z()); } painter->drawText({-width()/2.0 + 10, -height()/2.0 + 10, 300, 200}, txt); }
void ViewportView::mousePressEvent(QMouseEvent* event) { QGraphicsView::mousePressEvent(event); // If the event hasn't been accepted, record click position for // panning / rotation on mouse drag. if (!event->isAccepted()) { if (event->button() == Qt::LeftButton) { click_pos = mapToScene(event->pos()); click_pos_world = sceneToWorld(click_pos); } else { click_pos = event->pos(); } dragged = false; } }
void Canvas::mouseMoveEvent(QMouseEvent *event) { QGraphicsView::mouseMoveEvent(event); if (scene->mouseGrabberItem() == NULL) { if (event->buttons() == Qt::LeftButton) { pan(_click_pos_world - sceneToWorld(mapToScene(event->pos()))); } else if (event->buttons() == Qt::RightButton) { QPointF d = _click_pos - event->pos(); pitch = fmin(0, fmax(-M_PI, pitch - 0.01 * d.y())); yaw = fmod(yaw + M_PI - 0.01 * d.x(), M_PI*2) - M_PI; _click_pos = event->pos(); update(); emit(viewChanged()); } } }
void Viewport::updateInfo() { // Then add a text label in the lower-left corner // giving mouse coordinates (if we're near an axis) if (getAxis().first && hover) { QPair<char, float> axis = getAxis(); QPointF mouse_pos = mapToScene(mapFromGlobal(QCursor::pos())); if (!sceneRect().contains(mouse_pos)) axis.first = '\0'; auto p = sceneToWorld(mouse_pos); int value = axis.second * 200; mouse_info->setDefaultTextColor(QColor(value, value, value)); if (axis.first == 'z') mouse_info->setPlainText(QString("X: %1\nY: %2") .arg(p.x()).arg(p.y())); else if (axis.first == 'y') mouse_info->setPlainText(QString("X: %1\nZ: %2") .arg(p.x()).arg(p.z())); else if (axis.first == 'x') mouse_info->setPlainText(QString("Y: %1\nZ: %2") .arg(p.y()).arg(p.z())); } else { mouse_info->setPlainText(" \n "); } if (hover) { QString info; auto proxies = getProxiesAtPosition(_current_pos); if (proxies.size() > 0) { auto n = proxies.first()->getNode(); QString desc = QString::fromStdString(n->getName()); // + " (" + n->getTitle() + ")"; if (proxies.size() > 1) info = QString("Current: %1, (%2 more below)") .arg(desc).arg(proxies.size()-1); else info = QString("Current: %1").arg(desc); } else { info = QString("Current: <none>"); } /* display scale, pitch, and yaw */ info += QString("\nScale: %1").arg(scale/100); info += QString("\nPitch: %1").arg(getPitch()); info += QString("\nYaw: %1").arg(getYaw()); scene_info->setPlainText(info); } else { scene_info->setPlainText(""); } }