+/**
+ * Catches an entity which is close to the given position 'pos'.
+ *
+ * @param pos A graphic coordinate.
+ * @param level The level of resolving for iterating through the entity
+ * container
+ * @return Pointer to the entity or NULL.
+ */
+Entity * GraphicView::CatchEntity(const Vector & pos, RS2::ResolveLevel level)
+{
+ DEBUG->print("GraphicView::CatchEntity");
+
+ // Set default distance for points inside solids
+ double dist = toGraphDX(snapRange) * 0.9;
+ Entity * entity = container->getNearestEntity(pos, &dist, level);
+
+ // DEBUGGING INFO
+ int idx = -1;
+
+ if (entity && entity->getParent())
+ idx = entity->getParent()->findEntity(entity);
+ // END DEBUGGING INFO
+
+ if (entity && dist <= toGraphDX(snapRange))
+ {
+ // Highlight:
+ DEBUG->print("GraphicView::CatchEntity: found: idx=%d", idx);
+ return entity;
+ }
+
+ DEBUG->print("GraphicView::CatchEntity: not found");
+ return NULL;
+}
+
+/**
+ * Catches an entity which is close to the mouse cursor.
+ *
+ * @param e A mouse event.
+ * @param level The level of resolving for iterating through the entity
+ * container
+ * @return Pointer to the entity or NULL.
+ */
+Entity * GraphicView::CatchEntity(QMouseEvent * e, RS2::ResolveLevel level)
+{
+ return CatchEntity(Vector(toGraphX(e->x()), toGraphY(e->y())), level);
+}
+
+/**
+ * Snap to a coordinate in the drawing using the current snap mode.
+ *
+ * @param e A mouse event.
+ * @return The coordinates of the point or an invalid vector.
+ */
+Vector GraphicView::SnapPoint(QMouseEvent * e)
+{
+ DEBUG->print("GraphicView::SnapPoint");
+ /*Vector*/ snapSpot = Vector(false);
+
+ if (!e)
+ {
+ DEBUG->print(Debug::D_WARNING, "GraphicView::SnapPoint: QMouseEvent is NULL");
+ return snapSpot;
+ }
+
+ Vector mouseCoord = toGraph(e->x(), e->y());
+
+// switch (snapMode)
+ switch (defaultSnapMode)
+ {
+ case RS2::SnapFree:
+// snapSpot = snapFree(mouseCoord);
+ snapEntity = NULL;
+ snapSpot = mouseCoord;
+ break;
+
+ case RS2::SnapEndpoint:
+// snapSpot = snapEndpoint(mouseCoord);
+ snapSpot = container->getNearestEndpoint(mouseCoord, NULL);
+ break;
+
+ case RS2::SnapGrid:
+ snapSpot = SnapGrid(mouseCoord);
+ break;
+
+ case RS2::SnapOnEntity:
+// snapSpot = snapOnEntity(mouseCoord);
+ snapSpot = container->getNearestPointOnEntity(mouseCoord, true, NULL, &snapEntity);
+ break;
+
+ case RS2::SnapCenter:
+// snapSpot = snapCenter(mouseCoord);
+ snapSpot = container->getNearestCenter(mouseCoord, NULL);
+ break;
+
+ case RS2::SnapMiddle:
+// snapSpot = snapMiddle(mouseCoord);
+ snapSpot = container->getNearestMiddle(mouseCoord, NULL);
+ break;
+
+ case RS2::SnapDist:
+// snapSpot = snapDist(mouseCoord);
+ snapSpot = container->getNearestDist(snapDistance, mouseCoord, NULL);
+ break;
+
+ case RS2::SnapIntersection:
+// snapSpot = snapIntersection(mouseCoord);
+ snapSpot = container->getNearestIntersection(mouseCoord, NULL);
+ break;
+
+ default:
+ break;
+ }
+
+ // This is declared here because I really, really hate extraneous braces in
+ // my switch statements. :-P
+ Vector relZero = getRelativeZero();
+ Vector restrictX = Vector(relZero.x, snapSpot.y);
+ Vector restrictY = Vector(snapSpot.x, relZero.y);
+// Vector snapCoord;
+ // Handle snap restrictions that can be activated in addition to the ones above:
+// switch (snapRes)
+ switch (defaultSnapRes)
+ {
+ case RS2::RestrictOrthogonal:
+//// snapCoord = restrictOrthogonal(snapSpot);
+// rz = graphicView->getRelativeZero();
+// retx = Vector(rz.x, snapSpot.y);
+// rety = Vector(snapSpot.x, rz.y);
+ snapCoord = (restrictX.distanceTo(snapSpot) < restrictY.distanceTo(snapSpot) ?
+ restrictX : restrictY);
+ break;
+ case RS2::RestrictHorizontal:
+//// snapCoord = restrictHorizontal(snapSpot);
+// rz = graphicView->getRelativeZero();
+// snapCoord = Vector(snapSpot.x, rz.y);
+ snapCoord = restrictY;
+ break;
+ case RS2::RestrictVertical:
+//// snapCoord = restrictVertical(snapSpot);
+// rz = graphicView->getRelativeZero();
+// snapCoord = Vector(rz.x, snapSpot.y);
+ snapCoord = restrictX;
+ break;
+ default:
+ case RS2::RestrictNothing:
+ snapCoord = snapSpot;
+ break;
+ }
+
+ if (DIALOGFACTORY)
+ DIALOGFACTORY->updateCoordinateWidget(snapCoord, snapCoord - relZero);
+
+ DEBUG->print("GraphicView::SnapPoint: OK");
+
+ return snapCoord;
+}
+
+/**
+ * Snaps to a grid point.
+ *
+ * @param coord The mouse coordinate.
+ * @return The coordinates of the point or an invalid vector.
+ */
+Vector GraphicView::SnapGrid(Vector coord)