void MapperGLCanvas::mouseMoveEvent(QMouseEvent* event) { static QPoint lastMousePos; QPointF pos = mapToScene(event->pos()); // Prepare to store commands undoStack = getMainWindow()->getUndoStack(); // Vertex grab. if (_mousePressedOnVertex) { // std::cout << "Move event " << std::endl; MShape* shape = getCurrentShape(); if (shape && _activeVertex != NO_VERTEX) { // QPointF p = shape->getVertex(_activeVertex); // // Set point to mouse coordinates. // p.setX(pos.x()); // p.setY(pos.y()); QPointF p = pos; // Stick to vertices. if (_mainWindow->stickyVertices()) _glueVertex(&p); shape->setVertex(_activeVertex, p); } } // Shape grab. else if (_shapeGrabbed) { // std::cout << "Move event " << std::endl; MShape* shape = getCurrentShape(); if (shape) { if (_shapeFirstGrab) { lastMousePos = _mousePressedPosition; _shapeFirstGrab = false; } } QPointF diff = pos - mapToScene(lastMousePos); shape->translate(diff.x(), diff.y()); } // Window translation action else if (event->buttons() & Qt::MiddleButton) { QPointF diff = event->pos() - lastMousePos; QGraphicsView* view = scene()->views().first(); view->translate(diff.x(), diff.y()); // view->update(); } lastMousePos = event->pos(); }
void MapperGLCanvas::_glueVertex(QPointF* p) { MappingManager manager = MainWindow::instance()->getMappingManager(); for (int i = 0; i < manager.nMappings(); i++) { MShape *shape = manager.getMapping(i)->getShape().get(); if (shape && shape != getCurrentShape()) { for (int vertex = 0; vertex < shape->nVertices(); vertex++) { const QPointF& v = shape->getVertex(vertex); if (distIsInside(v, *p, MM::VERTEX_STICK_RADIUS)) { p->setX(v.x()); p->setY(v.y()); } } } } }
void MapperGLCanvas::mousePressEvent(QMouseEvent* event) { bool mousePressedOnSomething = false; _mousePressedPosition = event->pos(); QPointF pos = mapToScene(event->pos()); // Drag the scene with middle button. if (event->buttons() & Qt::MiddleButton) { // NOTE: This is a trick code to implement scroll hand drag using the middle button. QMouseEvent releaseEvent(QEvent::MouseButtonRelease, event->pos(), event->globalPos(), Qt::LeftButton, 0, event->modifiers()); QGraphicsView::mouseReleaseEvent(&releaseEvent); setDragMode(QGraphicsView::ScrollHandDrag); // We need to pretend it is actually the left button that was pressed! QMouseEvent fakeEvent(event->type(), event->pos(), event->globalPos(), Qt::LeftButton, event->buttons() | Qt::LeftButton, event->modifiers()); QGraphicsView::mousePressEvent(&fakeEvent); } // Check for vertex selection first. else if (event->buttons() & Qt::LeftButton) { MShape* shape = getCurrentShape(); if (shape) { // Note: we compare with the square value for fastest computation of the distance int minDistance = sq(MM::VERTEX_SELECT_RADIUS); // Find the ID of the nearest vertex (from currently selected shape) for (int i = 0; i < shape->nVertices(); i++) { int dist = distSq(_mousePressedPosition, mapFromScene(shape->getVertex(i))); // squared distance if (dist < minDistance) { _activeVertex = i; minDistance = dist; _mousePressedOnVertex = true; mousePressedOnSomething = true; _grabbedObjectStartPosition = shape->getVertex(i); } } } } if (mousePressedOnSomething) return; // Check for shape selection. if (event->buttons() & (Qt::LeftButton | Qt::RightButton)) // Add Right click for context menu { MShape* selectedShape = getCurrentShape(); // Possibility of changing shape in output by clicking on it. MappingManager manager = getMainWindow()->getMappingManager(); QVector<Mapping::ptr> mappings = manager.getVisibleMappings(); for (QVector<Mapping::ptr>::const_iterator it = mappings.end() - 1; it >= mappings.begin(); --it) { MShape *shape = getShapeFromMappingId((*it)->getId()); // Check if mouse was pressed on that shape. if (shape && shape->includesPoint(pos)) { mousePressedOnSomething = true; // Deselect vertices. deselectVertices(); // Change mapping (only available in destination). if (isOutput() && shape != selectedShape) { // Change current mapping. getMainWindow()->setCurrentMapping((*it)->getId()); // Reset selected shape to new one. selectedShape = getCurrentShape(); } break; } } // Grab the shape. if (event->buttons() & Qt::LeftButton) // This preserve me from duplicate code above { if (selectedShape && selectedShape->includesPoint(pos)) { _shapeGrabbed = true; _shapeFirstGrab = true; _grabbedObjectStartPosition = pos; } } } if (mousePressedOnSomething) return; // Deactivate. deselectAll(); }
void FootprintPadShapeSelectorWidget::btnOctagonToggled(bool checked) noexcept { mBtnRound->setChecked(!checked); mBtnRect->setChecked(!checked); emit currentShapeChanged(getCurrentShape()); }