diff --git a/Mapper.cpp b/Mapper.cpp index 014dfd7..7103a38 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -52,6 +52,10 @@ bool ShapeGraphicsItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event // QPointF pos = moveEvent->newPos();// + this->pos(); QPointF pos = mouseEvent->scenePos(); + + // Sticky vertex. + _glueVertex(&pos); + // qDebug() << moveEvent->oldPos() << " " << pos << " " << childItems().at(idx)->pos() << endl; _shape->setVertex(idx, pos); @@ -98,6 +102,8 @@ void ShapeGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) return; QGraphicsItem::mouseMoveEvent(event); + + // Sync shape. _syncShape(); } @@ -168,8 +174,7 @@ void ShapeGraphicsItem::_syncShape() QList children = childItems(); for (int i=0; i<_shape->nVertices(); i++) { - QGraphicsItem* child = children.at(i); - _shape->setVertex(i, child->scenePos()); + _shape->setVertex(i, children.at(i)->scenePos()); } // The shape object is the model: it contains the logic to make sure the vertices are ok. @@ -187,6 +192,27 @@ void ShapeGraphicsItem::_syncVertices() } } +void ShapeGraphicsItem::_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 != _shape.get()) + { + 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 VertexGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent * event) { ShapeGraphicsItem* shapeParent = static_cast(parentItem()); diff --git a/Mapper.h b/Mapper.h index a45783a..f027668 100644 --- a/Mapper.h +++ b/Mapper.h @@ -99,6 +99,9 @@ protected: virtual void _doPaint(QPainter *painter, const QStyleOptionGraphicsItem *option) = 0; + // TODO: Perhaps the sticky-sensitivity should be configurable through GUI + void _glueVertex(QPointF* p); + Mapping::ptr _mapping; MShape::ptr _shape; bool _output; diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index 7791e9a..274327c 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -334,29 +334,29 @@ void MapperGLCanvas::enableStickyVertices(bool value) _stickyVertices = value; } -/* Stick vertex p of Shape orig to another Shape's vertex, if the 2 vertices are - * close enough. The distance per coordinate is currently set in dist_stick - * variable. Perhaps the sticky-sensitivity should be configurable through GUI */ -void MapperGLCanvas::glueVertex(MShape *orig, QPointF *p) -{ - MappingManager manager = getMainWindow()->getMappingManager(); - for (int i = 0; i < manager.nMappings(); i++) - { - MShape *shape = getShapeFromMappingId(manager.getMapping(i)->getId()); - if (shape && shape != orig) - { - 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()); - } - } - } - } -} +///* Stick vertex p of Shape orig to another Shape's vertex, if the 2 vertices are +// * close enough. The distance per coordinate is currently set in dist_stick +// * variable. Perhaps the sticky-sensitivity should be configurable through GUI */ +//void MapperGLCanvas::glueVertex(MShape *orig, QPointF *p) +//{ +// MappingManager manager = getMainWindow()->getMappingManager(); +// for (int i = 0; i < manager.nMappings(); i++) +// { +// MShape *shape = getShapeFromMappingId(manager.getMapping(i)->getId()); +// if (shape && shape != orig) +// { +// 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::deselectVertices() diff --git a/MapperGLCanvas.h b/MapperGLCanvas.h index 7ce323f..3b71225 100644 --- a/MapperGLCanvas.h +++ b/MapperGLCanvas.h @@ -60,9 +60,6 @@ public: * close enough. The distance per coordinate is currently set in dist_stick * variable. */ - // TODO: Perhaps the sticky-sensitivity should be configurable through GUI - void glueVertex(MShape *, QPointF *); - /// Returns pointer to main window. MainWindow* getMainWindow() const { return _mainWindow; }