]> Shamusworld >> Repos - architektonas/blobdiff - src/drawingview.cpp
Added new triangulation tool, ability to snap to endpoints/intersections.
[architektonas] / src / drawingview.cpp
index 9341a76279044ef78c64e3d21a39f95d85cda41c..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"
 
@@ -49,23 +50,14 @@ DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent),
        // The value in the settings file will override this.
        useAntialiasing(true),
        gridBackground(BACKGROUND_MAX_SIZE, BACKGROUND_MAX_SIZE),
-       scale(1.0), offsetX(-10), offsetY(-10),
-       document(Vector(0, 0)),
-       /*gridSpacing(12.0),*/ gridPixels(0), collided(false), //rotateTool(false),
-//     rx(150.0), ry(150.0),
-//     scrollDrag(false), addLineTool(false), addCircleTool(false),
-//     addDimensionTool(false),
-       toolAction(NULL)
+       scale(1.0), offsetX(-10), offsetY(-10), document(Vector(0, 0)),
+       gridPixels(0), collided(false), toolAction(NULL)
 {
        document.isTopLevelContainer = true;
        setBackgroundRole(QPalette::Base);
        setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 
-       Object::gridSpacing = 12.0;
-//     toolPalette = new ToolWindow();
-//     CreateCursors();
-//     setCursor(cur[TOOLSelect]);
-//     setMouseTracking(true);
+       Object::gridSpacing = 12.0;             // In base units (inch is default)
 
        Line * line = new Line(Vector(5, 5), Vector(50, 40), &document);
        document.Add(line);
@@ -130,7 +122,7 @@ need a thickness parameter similar to the "size" param for dimensions. (And now
 we do! :-)
 
 */
-       SetGridSize(12);
+       SetGridSize(12);        // This is in pixels
 }
 
 
@@ -341,32 +333,22 @@ void DrawingView::mousePressEvent(QMouseEvent * event)
        if (event->button() == Qt::LeftButton)
        {
                Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
-
-// Problem with this: Can't select stuff very well with the snap grid on.
-// Completely screws things up, as sometimes things don't fall on the grid.
-/*
-So, how to fix this? Have the Object check itself?
-Maybe we can fix this by having the initial point not be snapped, but when there's
-a drag, we substitute the snapped point 'oldPoint' which the Object keeps track of
-internally to know how far it was dragged...
-
-Now we do... :-/
-*/
-#if 0
-               if (Object::snapToGrid)
-                       point = Object::SnapPointToGrid(point);
-#endif
-
                collided = document.Collided(point);
 
+               // Do an update if collided with at least *one* object in the document
                if (collided)
-                       update();       // Do an update if collided with at least *one* object in the document
+                       update();
 
                if (toolAction)
                {
                        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);
                }
 
@@ -392,13 +374,16 @@ 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;
                Painter::origin -= delta;
@@ -409,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();
 }
 
 
@@ -470,10 +520,11 @@ void DrawingView::mouseReleaseEvent(QMouseEvent * event)
 void DrawingView::wheelEvent(QWheelEvent * event)
 {
        double zoomFactor = 1.25;
-       QSize sizeWin = /*drawing->*/size();
+       QSize sizeWin = size();
        Vector center(sizeWin.width() / 2.0, sizeWin.height() / 2.0);
        center = Painter::QtToCartesianCoords(center);
 
+       // This is not centering for some reason. Need to figure out why. :-/
        if (event->delta() > 0)
        {
                Vector newOrigin = center - ((center - Painter::origin) / zoomFactor);
@@ -487,10 +538,10 @@ void DrawingView::wheelEvent(QWheelEvent * event)
                Painter::zoom /= zoomFactor;
        }
 
-//     Object::gridSpacing = /*drawing->*/gridPixels / Painter::zoom;
+//     Object::gridSpacing = gridPixels / Painter::zoom;
+//     UpdateGridBackground();
        SetGridSize(Object::gridSpacing * Painter::zoom);
-//     /*drawing->*/UpdateGridBackground();
-       /*drawing->*/update();
+       update();
 //     zoomIndicator->setText(QString("Grid: %1\", BU: Inch").arg(Object::gridSpacing));
 }