+
+void DrawingView::wheelEvent(QWheelEvent * event)
+{
+ double zoomFactor = 1.25;
+ 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 - Global::origin) / zoomFactor);
+ Global::origin = newOrigin;
+ Global::zoom *= zoomFactor;
+ }
+ else
+ {
+ Vector newOrigin = center + ((-center + Global::origin) * zoomFactor);
+ Global::origin = newOrigin;
+ Global::zoom /= zoomFactor;
+ }
+
+#if 1
+// Global::gridSpacing = gridPixels / Painter::zoom;
+// UpdateGridBackground();
+ SetGridSize(Global::gridSpacing * Global::zoom);
+ update();
+// zoomIndicator->setText(QString("Grid: %1\", BU: Inch").arg(Global::gridSpacing));
+#endif
+}
+
+
+void DrawingView::keyPressEvent(QKeyEvent * event)
+{
+#if 0
+ if (toolAction)
+ toolAction->KeyDown(event->key());
+#endif
+ bool oldShift = shiftDown;
+ bool oldCtrl = ctrlDown;
+
+ if (event->key() == Qt::Key_Shift)
+ shiftDown = true;
+ else if (event->key() == Qt::Key_Control)
+ ctrlDown = true;
+
+ if ((oldShift != shiftDown) || (oldCtrl != ctrlDown))
+ update();
+}
+
+
+void DrawingView::keyReleaseEvent(QKeyEvent * event)
+{
+#if 0
+ if (toolAction)
+ toolAction->KeyReleased(event->key());
+#endif
+ bool oldShift = shiftDown;
+ bool oldCtrl = ctrlDown;
+
+ if (event->key() == Qt::Key_Shift)
+ shiftDown = false;
+ else if (event->key() == Qt::Key_Control)
+ ctrlDown = false;
+
+ if ((oldShift != shiftDown) || (oldCtrl != ctrlDown))
+ update();
+}
+
+//
+// This looks strange, but it's really quite simple: We want a point that's
+// more than half-way to the next grid point to snap there while conversely we
+// want a point that's less than half-way to to the next grid point then snap
+// to the one before it. So we add half of the grid spacing to the point, then
+// divide by it so that we can remove the fractional part, then multiply it
+// back to get back to the correct answer.
+//
+Point DrawingView::SnapPointToGrid(Point point)
+{
+ point += Global::gridSpacing / 2.0; // *This* adds to Z!!!
+ point /= Global::gridSpacing;
+ point.x = floor(point.x);//need to fix this for negative numbers...
+ point.y = floor(point.y);
+ point.z = 0; // Make *sure* Z doesn't go anywhere!!!
+ point *= Global::gridSpacing;
+ return point;
+}
+
+
+void DrawingView::CheckObjectBounds(void)
+{
+ std::vector<void *>::iterator i;
+ numSelected = 0;
+
+ for(i=document.objects.begin(); i!=document.objects.end(); i++)
+ {
+ Object * obj = (Object *)(*i);
+ obj->selected = false;
+
+ switch (obj->type)
+ {
+ case OTLine:
+ {
+ Line * l = (Line *)obj;
+
+ if (Global::selection.contains(l->p1.x, l->p1.y) && Global::selection.contains(l->p2.x, l->p2.y))
+ l->selected = true;
+
+ break;
+ }
+ case OTCircle:
+ {
+ Circle * c = (Circle *)obj;
+
+ if (Global::selection.contains(c->p1.x - c->radius, c->p1.y - c->radius) && Global::selection.contains(c->p1.x + c->radius, c->p1.y + c->radius))
+ c->selected = true;
+
+ break;
+ }
+ case OTArc:
+ {
+ Arc * a = (Arc *)obj;
+
+ double start = a->angle1;
+ double end = start + a->angle2;
+ QPointF p1(cos(start), sin(start));
+ QPointF p2(cos(end), sin(end));
+ QRectF bounds(p1, p2);
+
+#if 1
+ // Swap X/Y coordinates if they're backwards...
+ if (bounds.left() > bounds.right())
+ {
+ double temp = bounds.left();
+ bounds.setLeft(bounds.right());
+ bounds.setRight(temp);
+ }
+
+ if (bounds.bottom() > bounds.top())
+ {
+ double temp = bounds.bottom();
+ bounds.setBottom(bounds.top());
+ bounds.setTop(temp);
+ }
+#else
+ // Doesn't work as advertised! For shame!
+ bounds = bounds.normalized();
+#endif
+
+ // If the end of the arc is before the beginning, add 360 degrees to it
+ if (end < start)
+ end += 2.0 * PI;
+
+ // Adjust the bounds depending on which axes are crossed
+ if ((start < PI_OVER_2) && (end > PI_OVER_2))
+ bounds.setTop(1.0);
+
+ if ((start < PI) && (end > PI))
+ bounds.setLeft(-1.0);
+
+ if ((start < (PI + PI_OVER_2)) && (end > (PI + PI_OVER_2)))
+ bounds.setBottom(-1.0);
+
+ if ((start < (2.0 * PI)) && (end > (2.0 * PI)))
+ bounds.setRight(1.0);
+
+ if ((start < ((2.0 * PI) + PI_OVER_2)) && (end > ((2.0 * PI) + PI_OVER_2)))
+ bounds.setTop(1.0);
+
+ if ((start < (3.0 * PI)) && (end > (3.0 * PI)))
+ bounds.setLeft(-1.0);
+
+ if ((start < ((3.0 * PI) + PI_OVER_2)) && (end > ((3.0 * PI) + PI_OVER_2)))
+ bounds.setBottom(-1.0);
+
+ bounds.setTopLeft(QPointF(bounds.left() * a->radius, bounds.top() * a->radius));
+ bounds.setBottomRight(QPointF(bounds.right() * a->radius, bounds.bottom() * a->radius));
+ bounds.translate(a->p1.x, a->p1.y);
+
+ if (Global::selection.contains(bounds))
+ a->selected = true;
+
+ break;
+ }
+ default:
+ break;
+ }
+
+ if (obj->selected)
+ numSelected++;
+ }
+}
+
+
+bool DrawingView::HitTestObjects(Point point)
+{
+ std::vector<void *>::iterator i;
+ numHovered = 0;
+ bool needUpdate = false;
+
+ for(i=document.objects.begin(); i!=document.objects.end(); i++)
+ {
+ Object * obj = (Object *)(*i);
+
+ switch (obj->type)
+ {
+ case OTLine:
+ {
+ Line * l = (Line *)obj;
+ bool oldHP0 = l->hitPoint[0], oldHP1 = l->hitPoint[1], oldHO = l->hitObject;
+ l->hitPoint[0] = l->hitPoint[1] = l->hitObject = false;
+ Vector lineSegment = l->p2 - l->p1;
+ Vector v1 = point - l->p1;
+ Vector v2 = point - l->p2;
+ double t = Geometry::ParameterOfLineAndPoint(l->p1, l->p2, point);
+ double distance;
+
+ if (t < 0.0)
+ distance = v1.Magnitude();
+ else if (t > 1.0)
+ distance = v2.Magnitude();
+ else
+ // distance = ?Det?(ls, v1) / |ls|
+ distance = fabs((lineSegment.x * v1.y - v1.x * lineSegment.y)
+ / lineSegment.Magnitude());
+
+ if ((v1.Magnitude() * Global::zoom) < 8.0)
+ {
+ l->hitPoint[0] = true;
+// snapPoint = l->p1;
+// snapPointIsValid = true;
+ }
+ else if ((v2.Magnitude() * Global::zoom) < 8.0)
+ {
+ l->hitPoint[1] = true;
+// snapPoint = l->p2;
+// snapPointIsValid = true;
+ }
+ else if ((distance * Global::zoom) < 5.0)
+ l->hitObject = true;
+
+// bool oldHovered = l->hovered;
+ l->hovered = (l->hitPoint[0] || l->hitPoint[1] || l->hitObject ? true : false);
+// l->hovered = l->hitObject;
+
+// if (oldHovered != l->hovered)
+ if ((oldHP0 != l->hitPoint[0]) || (oldHP1 != l->hitPoint[1]) || (oldHO != l->hitObject))
+ needUpdate = true;
+
+ break;
+ }
+ case OTCircle:
+ {
+ Circle * c = (Circle *)obj;
+
+
+ break;
+ }
+ default:
+ break;
+ }
+
+ if (obj->hovered)
+// {
+ numHovered++;
+//printf("MouseMove: OBJECT HOVERED (numHovered = %i)\n", numHovered);
+// }
+ }
+
+ return needUpdate;
+}
+
+
+#if 0
+ // This returns true if we've moved over an object...
+ if (document.PointerMoved(point)) // <-- This
+ // This is where the object would do automagic dragging & shit. Since we don't
+ // do that anymore, we need a strategy to handle it.
+ {
+
+/*
+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)
+ {
+ Global::snapPoint = document.lastObjectHovered->GetPointAtParameter(t);
+ Global::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)
+ {
+ Global::snapPoint = p1;
+ Global::snapPointIsValid = true;
+ }
+ else if (n == 2)
+ {
+ double d1 = Vector(point, p1).Magnitude();
+ double d2 = Vector(point, p2).Magnitude();
+
+ if (d1 < d2)
+ Global::snapPoint = p1;
+ else
+ Global::snapPoint = p2;
+
+ Global::snapPointIsValid = true;
+ }
+ }
+ }
+// else
+// {
+ // Otherwise, it was a single object hovered...
+// }
+ }
+
+ if (toolAction)
+ {
+ if (Global::snapToGrid)
+ point = Global::SnapPointToGrid(point);
+
+ // We always snap to object points, and they take precendence over
+ // grid points...
+ if (Global::snapPointIsValid)
+ point = Global::snapPoint;
+
+ toolAction->MouseMoved(point);
+ }
+#else
+#endif
+