]> Shamusworld >> Repos - architektonas/blobdiff - src/drawingview.cpp
Added new triangulation tool, ability to snap to endpoints/intersections.
[architektonas] / src / drawingview.cpp
index acfad409c2f22e685e49c646de30c355039684df..fc30ac8e259ce61a092b35210f2ab6280ce2b457 100644 (file)
@@ -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,96 @@ 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))
+       {
+               // 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();
 }