X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdrawingview.cpp;h=629999f148b4a83a9bb604bb0360bc502b54d2a1;hb=6c1279871f6bb86bc59e2561b6a7f74ab081f71e;hp=acfad409c2f22e685e49c646de30c355039684df;hpb=4d6ba8a6eb781dbee818b6a55d21df7b52468936;p=architektonas diff --git a/src/drawingview.cpp b/src/drawingview.cpp index acfad40..629999f 100644 --- a/src/drawingview.cpp +++ b/src/drawingview.cpp @@ -35,6 +35,7 @@ #include "arc.h" #include "circle.h" #include "dimension.h" +#include "geometry.h" #include "line.h" #include "painter.h" @@ -343,6 +344,11 @@ void DrawingView::mousePressEvent(QMouseEvent * event) if (Object::snapToGrid) point = Object::SnapPointToGrid(point); + // We always snap to object points, and they take precendence over + // grid points... + if (Object::snapPointIsValid) + point = Object::snapPoint; + toolAction->MouseDown(point); } @@ -368,13 +374,15 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event) { Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y())); Object::selection.setBottomRight(QPointF(point.x, point.y)); + // Only needs to be done here, as mouse down is always preceded by movement + Object::snapPointIsValid = false; + // Scrolling... if (event->buttons() & Qt::MiddleButton) { point = Vector(event->x(), event->y()); // Since we're using Qt coords for scrolling, we have to adjust them here to // conform to Cartesian coords, since the origin is using Cartesian. :-) -// Vector delta(point, oldPoint); Vector delta(oldPoint, point); delta /= Painter::zoom; delta.y = -delta.y; @@ -386,31 +394,106 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event) return; } - // Grid processing... +#if 0 + // Grid processing... (only snap here is left button is down) if ((event->buttons() & Qt::LeftButton) && Object::snapToGrid) { point = Object::SnapPointToGrid(point); } - oldPoint = point; + // Snap points on objects always take precedence over the grid, whether + // dragging an object or not... +//thisnowok + if (Object::snapPointIsValid) + { +// Uncommenting this causes the cursor to become unresponsive after the first +// object is added. +// point = Object::snapPoint; + } +#endif + +// oldPoint = point; //we should keep track of the last point here and only pass this down *if* the point //changed... - document.PointerMoved(point); - if (document.NeedsUpdate() || Object::selectionInProgress) - update(); + // This returns true if we've moved over an object... + if (document.PointerMoved(point)) + { +/* +Now objects handle mouse move snapping as well. The code below mainly works only +for tools; we need to fix it so that objects work as well... + +There's a problem with the object point snapping in that it's dependent on the +order of the objects in the document. Most likely this is because it counts the +selected object last and thus fucks up the algorithm. Need to fix this... + + +*/ + // Do object snapping here. Grid snapping on mouse down is done in the + // objects themselves, only because we have to hit test the raw point, + // not the snapped point. There has to be a better way...! + if (document.penultimateObjectHovered) + { + // Two objects are hovered, see if we have an intersection point + if ((document.lastObjectHovered->type == OTLine) && (document.penultimateObjectHovered->type == OTLine)) + { + double t; + int n = Geometry::Intersects((Line *)document.lastObjectHovered, (Line *)document.penultimateObjectHovered, &t); + + if (n == 1) + { + Object::snapPoint = document.lastObjectHovered->GetPointAtParameter(t); + Object::snapPointIsValid = true; + } + } + else if ((document.lastObjectHovered->type == OTCircle) && (document.penultimateObjectHovered->type == OTCircle)) + { + Point p1, p2; + int n = Geometry::Intersects((Circle *)document.lastObjectHovered, (Circle *)document.penultimateObjectHovered, 0, 0, 0, 0, &p1, &p2); + + if (n == 1) + { + Object::snapPoint = p1; + Object::snapPointIsValid = true; + } + else if (n == 2) + { + double d1 = Vector(point, p1).Magnitude(); + double d2 = Vector(point, p2).Magnitude(); + + if (d1 < d2) + Object::snapPoint = p1; + else + Object::snapPoint = p2; + + Object::snapPointIsValid = true; + } + } + } +// else +// { + // Otherwise, it was a single object hovered... +// } + } if (toolAction) { if (Object::snapToGrid) - { point = Object::SnapPointToGrid(point); - oldPoint = point; - } + + // We always snap to object points, and they take precendence over + // grid points... + if (Object::snapPointIsValid) + point = Object::snapPoint; toolAction->MouseMoved(point); - update(); } + + // This is used to draw the tool crosshair... + oldPoint = point; + + if (document.NeedsUpdate() || Object::selectionInProgress || toolAction) + update(); }