+ if (event->button() == Qt::LeftButton)
+ {
+ Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
+
+ // Handle tool processing, if any
+ if (Global::tool)
+ {
+ if (hoveringIntersection)
+ point = intersectionPoint;
+ else if (Global::snapToGrid)
+ point = SnapPointToGrid(point);
+
+ //Also, may want to figure out if hovering over a snap point on an
+ //object, snap to grid if not.
+ // Snap to object point if valid...
+// if (Global::snapPointIsValid)
+// point = Global::snapPoint;
+
+ ToolHandler(ToolMouseDown, point);
+ return;
+ }
+
+ // Clear the selection only if CTRL isn't being held on click
+ if (!ctrlDown)
+ ClearSelected(document.objects);
+// ClearSelection();
+
+ // If any objects are being hovered on click, add them to the selection
+ // & return
+ if (numHovered > 0)
+ {
+ AddHoveredToSelection();
+ update(); // needed??
+ GetHovered(hover); // prolly needed
+ dragged = (Object *)hover[0];
+ draggingObject = true;
+
+ // Alert the pen widget
+ emit(ObjectSelected(dragged));
+
+ // See if anything is using just a straight click on a handle
+ if (HandleObjectClicked())
+ {
+ draggingObject = false;
+ update();
+ return;
+ }
+
+ // Needed for grab & moving objects
+ // We do it *after*... why? (doesn't seem to confer any advantage...)
+ if (hoveringIntersection)
+ oldPoint = intersectionPoint;
+ else if (Global::snapToGrid)
+ oldPoint = SnapPointToGrid(point);
+
+ return;
+ }
+
+ // Didn't hit any object and not using a tool, so do a selection rectangle
+ Global::selectionInProgress = true;
+ Global::selection.setTopLeft(QPointF(point.x, point.y));
+ Global::selection.setBottomRight(QPointF(point.x, point.y));
+ }
+ else if (event->button() == Qt::MiddleButton)
+ {
+ scrollDrag = true;
+ oldPoint = Vector(event->x(), event->y());
+ // Should also change the mouse pointer as well...
+ setCursor(Qt::SizeAllCursor);
+ }
+}
+
+
+void DrawingView::mouseMoveEvent(QMouseEvent * event)
+{
+ Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
+ Global::selection.setBottomRight(QPointF(point.x, point.y));
+ // Only needs to be done here, as mouse down is always preceded by movement
+ Global::snapPointIsValid = false;
+ hoveringIntersection = 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(oldPoint, point);
+ delta /= Global::zoom;
+ delta.y = -delta.y;
+ Global::origin -= delta;
+
+ UpdateGridBackground();
+ update();
+ oldPoint = point;
+ return;
+ }
+
+ // If we're doing a selection rect, see if any objects are engulfed by it
+ // (implies left mouse button held down)
+ if (Global::selectionInProgress)
+ {
+ CheckObjectBounds();
+ update();
+ return;
+ }
+
+ // Do object hit testing...
+ bool needUpdate = HitTestObjects(point);
+ GetHovered(hover);
+
+ // Check for multi-hover...
+ if (numHovered > 1)
+ {
+//need to check for case where hover is over 2 circles and a 3rd's center...
+ Object * obj1 = (Object *)hover[0], * obj2 = (Object *)hover[1];
+
+ Geometry::Intersects(obj1, obj2);
+ int numIntersecting = Global::numIntersectParams;
+ double t = Global::intersectParam[0];
+ double u = Global::intersectParam[1];
+
+ if (numIntersecting > 0)
+ {
+ Vector v1 = Geometry::GetPointForParameter(obj1, t);
+ Vector v2 = Geometry::GetPointForParameter(obj2, u);
+ QString text = tr("Intersection t=%1 (%3, %4), u=%2 (%5, %6)");
+ informativeText = text.arg(t).arg(u).arg(v1.x).arg(v1.y).arg(v2.x).arg(v2.y);
+
+ hoveringIntersection = true;
+ intersectionPoint = v1;
+ }
+
+ numIntersecting = Global::numIntersectPoints;
+
+ if (numIntersecting > 0)
+ {
+ Vector v1 = Global::intersectPoint[0];
+
+ if (numIntersecting == 2)
+ {
+ Vector v2 = Global::intersectPoint[1];
+
+ if (Vector::Magnitude(v2, point) < Vector::Magnitude(v1, point))
+ v1 = v2;
+ }
+
+ QString text = tr("Intersection <%1, %2>");
+ informativeText = text.arg(v1.x).arg(v1.y);
+ hoveringIntersection = true;
+ intersectionPoint = v1;
+ }
+ }
+
+ // Handle object movement (left button down & over an object)
+ if ((event->buttons() & Qt::LeftButton) && draggingObject && !Global::tool)
+ {
+ if (hoveringIntersection)
+ point = intersectionPoint;
+ else if (hoverPointValid)
+ point = hoverPoint;
+ else if (Global::snapToGrid)
+ point = SnapPointToGrid(point);
+
+ HandleObjectMovement(point);
+ update();
+ oldPoint = point;
+ return;
+ }
+
+ // Do tool handling, if any are active...
+ if (Global::tool)
+ {
+ if (hoveringIntersection)
+ point = intersectionPoint;
+ else if (hoverPointValid)
+ point = hoverPoint;
+ else if (Global::snapToGrid)
+ {
+ if (angleSnap)
+ point = SnapPointToAngle(point);
+ else
+ point = SnapPointToGrid(point);
+ }
+
+ ToolHandler(ToolMouseMove, point);
+ }
+
+ // This is used to draw the tool crosshair...
+ oldPoint = point;
+
+ if (needUpdate || Global::tool)
+ update();
+}
+
+
+void DrawingView::mouseReleaseEvent(QMouseEvent * event)
+{
+ if (event->button() == Qt::LeftButton)
+ {
+//We need to update especially if nothing collided and the state needs to change. !!! FIX !!!
+//could set it up to use the document's update function (assumes that all object
+//updates are being reported correctly:
+// if (document.NeedsUpdate())
+ // Do an update if collided with at least *one* object in the document
+// if (collided)
+ update();
+
+ if (Global::tool)
+ {
+ Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
+ ToolHandler(ToolMouseUp, point);
+ return;
+ }
+
+ if (Global::selectionInProgress)
+ Global::selectionInProgress = false;
+
+ informativeText.clear();
+// Should we be doing this automagically? Hmm...
+ // Clear our vectors
+ select.clear();
+ hover.clear();
+
+ // Scoop 'em up
+ std::vector<void *>::iterator i;
+
+ for(i=document.objects.begin(); i!=document.objects.end(); i++)
+ {
+ if (((Object *)(*i))->selected)
+ select.push_back(*i);
+ }
+
+ draggingObject = false;
+ }
+ else if (event->button() == Qt::MiddleButton)
+ {
+ scrollDrag = false;
+ setCursor(Qt::ArrowCursor);
+ }
+}
+
+
+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;
+ }
+
+// Global::gridSpacing = gridPixels / Painter::zoom;
+// UpdateGridBackground();
+ SetGridSize(Global::gridSpacing * Global::zoom);
+ update();
+// zoomIndicator->setText(QString("Grid: %1\", BU: Inch").arg(Global::gridSpacing));
+}
+
+
+void DrawingView::keyPressEvent(QKeyEvent * event)
+{
+ 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))
+ {
+ if (Global::tool)
+ ToolHandler(ToolKeyDown, Point(0, 0));
+
+ update();
+ }
+
+ if (select.size() > 0)
+ {
+ if (event->key() == Qt::Key_Up)
+ {
+ TranslateObjects(select, Point(0, +1.0));
+ update();
+ }
+ else if (event->key() == Qt::Key_Down)
+ {
+ TranslateObjects(select, Point(0, -1.0));
+ update();
+ }
+ else if (event->key() == Qt::Key_Right)
+ {
+ TranslateObjects(select, Point(+1.0, 0));
+ update();
+ }
+ else if (event->key() == Qt::Key_Left)
+ {
+ TranslateObjects(select, Point(-1.0, 0));
+ update();
+ }
+ }
+}
+
+
+void DrawingView::keyReleaseEvent(QKeyEvent * event)
+{
+ 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))
+ {
+ if (Global::tool)
+ ToolHandler(ToolKeyUp, Point(0, 0));
+
+ 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;
+}
+
+
+Point DrawingView::SnapPointToAngle(Point point)
+{
+ // Snap to a single digit angle (using toolpoint #1 as the center)
+ double angle = Vector::Angle(toolPoint[0], point);
+ double length = Vector::Magnitude(toolPoint[0], point);
+
+ // Convert from radians to degrees
+ double degAngle = angle * RADIANS_TO_DEGREES;
+ double snapAngle = (double)((int)(degAngle + 0.5));
+
+ Vector v;
+ v.SetAngleAndLength(snapAngle * DEGREES_TO_RADIANS, length);
+ point = toolPoint[0] + v;
+
+ return point;
+}
+
+
+Rect DrawingView::GetObjectExtents(Object * obj)
+{
+ // Default to empty rect, if object checks below fail for some reason
+ Rect rect;
+
+ switch (obj->type)
+ {
+ case OTLine:
+ case OTDimension:
+ {
+ rect = Rect(obj->p[0], obj->p[1]);
+ break;
+ }
+
+ case OTCircle:
+ {
+ rect = Rect(obj->p[0], obj->p[0]);
+ rect.Expand(obj->radius[0]);
+ break;
+ }
+
+ case OTArc:
+ {
+ Arc * a = (Arc *)obj;
+
+ double start = a->angle[0];
+ double end = start + a->angle[1];
+ rect = Rect(Point(cos(start), sin(start)), Point(cos(end), sin(end)));
+
+ // If the end of the arc is before the beginning, add 360 degrees to it
+ if (end < start)
+ end += TAU;
+
+ // Adjust the bounds depending on which axes are crossed
+ if ((start < QTR_TAU) && (end > QTR_TAU))
+ rect.t = 1.0;
+
+ if ((start < HALF_TAU) && (end > HALF_TAU))
+ rect.l = -1.0;
+
+ if ((start < THREE_QTR_TAU) && (end > THREE_QTR_TAU))
+ rect.b = -1.0;
+
+ if ((start < TAU) && (end > TAU))
+ rect.r = 1.0;
+
+ if ((start < (TAU + QTR_TAU)) && (end > (TAU + QTR_TAU)))
+ rect.t = 1.0;
+
+ if ((start < (TAU + HALF_TAU)) && (end > (TAU + HALF_TAU)))
+ rect.l = -1.0;
+
+ if ((start < (TAU + THREE_QTR_TAU)) && (end > (TAU + THREE_QTR_TAU)))
+ rect.b = -1.0;
+
+ rect *= a->radius[0];
+ rect.Translate(a->p[0]);
+ break;
+ }
+
+ case OTText:
+ {
+ Text * t = (Text *)obj;
+ rect = Rect(t->p[0], Point(t->p[0].x + t->extents.Width(), t->p[0].y - t->extents.Height()));
+ break;
+ }
+
+ case OTContainer:
+ {
+ Container * c = (Container *)obj;
+ std::vector<void *>::iterator i = c->objects.begin();
+ rect = GetObjectExtents((Object *)*i);
+ i++;
+
+ for(; i!=c->objects.end(); i++)
+ rect |= GetObjectExtents((Object *)*i);
+ }
+
+ default:
+ break;
+ }
+
+ return rect;
+}
+
+
+void DrawingView::CheckObjectBounds(void)
+{
+ std::vector<void *>::iterator i;
+
+ for(i=document.objects.begin(); i!=document.objects.end(); i++)
+ {
+ Object * obj = (Object *)(*i);
+ obj->selected = false;
+
+ switch (obj->type)
+ {
+ case OTLine:
+ case OTDimension:
+ {
+ Line * l = (Line *)obj;
+
+ if (Global::selection.contains(l->p[0].x, l->p[0].y) && Global::selection.contains(l->p[1].x, l->p[1].y))
+ l->selected = true;
+
+ break;
+ }
+
+ case OTCircle:
+ {
+ Circle * c = (Circle *)obj;
+
+ if (Global::selection.contains(c->p[0].x - c->radius[0], c->p[0].y - c->radius[0]) && Global::selection.contains(c->p[0].x + c->radius[0], c->p[0].y + c->radius[0]))
+ c->selected = true;
+
+ break;
+ }
+
+ case OTArc:
+ {
+ Arc * a = (Arc *)obj;
+
+ double start = a->angle[0];
+ double end = start + a->angle[1];
+ 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 += TAU;
+
+ // Adjust the bounds depending on which axes are crossed
+ if ((start < QTR_TAU) && (end > QTR_TAU))
+ bounds.setTop(1.0);
+
+ if ((start < HALF_TAU) && (end > HALF_TAU))
+ bounds.setLeft(-1.0);
+
+ if ((start < THREE_QTR_TAU) && (end > THREE_QTR_TAU))
+ bounds.setBottom(-1.0);
+
+ if ((start < TAU) && (end > TAU))
+ bounds.setRight(1.0);
+
+ if ((start < (TAU + QTR_TAU)) && (end > (TAU + QTR_TAU)))
+ bounds.setTop(1.0);
+
+ if ((start < (TAU + HALF_TAU)) && (end > (TAU + HALF_TAU)))
+ bounds.setLeft(-1.0);
+
+ if ((start < (TAU + THREE_QTR_TAU)) && (end > (TAU + THREE_QTR_TAU)))
+ bounds.setBottom(-1.0);
+
+ bounds.setTopLeft(QPointF(bounds.left() * a->radius[0], bounds.top() * a->radius[0]));
+ bounds.setBottomRight(QPointF(bounds.right() * a->radius[0], bounds.bottom() * a->radius[0]));
+ bounds.translate(a->p[0].x, a->p[0].y);
+
+ if (Global::selection.contains(bounds))
+ a->selected = true;
+
+ break;
+ }
+
+ case OTText:
+ {
+ Text * t = (Text *)obj;
+ Rect r(obj->p[0], Point(t->p[0].x + t->extents.Width(), t->p[0].y - t->extents.Height()));
+
+ if (Global::selection.contains(r.l, r.t) && Global::selection.contains(r.r, r.b))
+ t->selected = true;
+
+ break;
+ }
+
+ default:
+ break;
+ }
+ }
+}
+
+
+bool DrawingView::HitTestObjects(Point point)
+{
+ std::vector<void *>::iterator i;
+ numHovered = 0;
+ bool needUpdate = false;
+ hoverPointValid = false;
+
+ for(i=document.objects.begin(); i!=document.objects.end(); i++)
+ {
+ Object * obj = (Object *)(*i);
+
+ // If we're seeing the object we're dragging, skip it
+ if (draggingObject && (obj == dragged))
+ continue;
+
+ if (HitTest(obj, point))
+ needUpdate = true;
+
+ if (obj->hovered)
+ {
+ numHovered++;
+//printf("MouseMove: OBJECT HOVERED (numHovered = %i)\n", numHovered);
+ emit(ObjectHovered(obj));
+ }
+ }
+
+ return needUpdate;
+}
+
+
+bool DrawingView::HitTest(Object * obj, Point point)
+{
+ bool needUpdate = false;
+
+ switch (obj->type)
+ {
+ case OTLine:
+ {
+ bool oldHP0 = obj->hitPoint[0], oldHP1 = obj->hitPoint[1], oldHO = obj->hitObject;
+ obj->hitPoint[0] = obj->hitPoint[1] = obj->hitObject = false;
+ Vector lineSegment = obj->p[1] - obj->p[0];
+ Vector v1 = point - obj->p[0];
+ Vector v2 = point - obj->p[1];
+ double t = Geometry::ParameterOfLineAndPoint(obj->p[0], obj->p[1], 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)
+ {
+ obj->hitPoint[0] = true;
+ hoverPoint = obj->p[0];
+ hoverPointValid = true;
+ }
+ else if ((v2.Magnitude() * Global::zoom) < 8.0)
+ {
+ obj->hitPoint[1] = true;
+ hoverPoint = obj->p[1];
+ hoverPointValid = true;
+ }
+ else if ((distance * Global::zoom) < 5.0)
+ obj->hitObject = true;
+
+ obj->hovered = (obj->hitPoint[0] || obj->hitPoint[1] || obj->hitObject ? true : false);
+
+ if ((oldHP0 != obj->hitPoint[0]) || (oldHP1 != obj->hitPoint[1]) || (oldHO != obj->hitObject))
+ needUpdate = true;
+
+ break;
+ }
+
+ case OTCircle:
+ {
+ bool oldHP = obj->hitPoint[0], oldHO = obj->hitObject;
+ obj->hitPoint[0] = obj->hitObject = false;
+ double length = Vector::Magnitude(obj->p[0], point);
+
+ if ((length * Global::zoom) < 8.0)
+ {
+ obj->hitPoint[0] = true;
+ hoverPoint = obj->p[0];
+ hoverPointValid = true;
+ }
+ else if ((fabs(length - obj->radius[0]) * Global::zoom) < 2.0)
+ obj->hitObject = true;
+
+ obj->hovered = (obj->hitPoint[0] || obj->hitObject ? true : false);
+
+ if ((oldHP != obj->hitPoint[0]) || (oldHO != obj->hitObject))
+ needUpdate = true;
+
+ break;
+ }
+
+ case OTArc:
+ {
+ bool oldHP0 = obj->hitPoint[0], oldHP1 = obj->hitPoint[1], oldHP2 = obj->hitPoint[2], oldHO = obj->hitObject;
+ obj->hitPoint[0] = obj->hitPoint[1] = obj->hitPoint[2] = obj->hitObject = false;
+ double length = Vector::Magnitude(obj->p[0], point);
+ double angle = Vector::Angle(obj->p[0], point);
+
+ // Make sure we get the angle in the correct spot
+ if (angle < obj->angle[0])
+ angle += TAU;
+
+ // Get the span that we're pointing at...
+ double span = angle - obj->angle[0];
+
+ // N.B.: Still need to hit test the arc start & arc span handles...
+ double spanAngle = obj->angle[0] + obj->angle[1];
+ Point handle1 = obj->p[0] + (Vector(cos(obj->angle[0]), sin(obj->angle[0])) * obj->radius[0]);
+ Point handle2 = obj->p[0] + (Vector(cos(spanAngle), sin(spanAngle)) * obj->radius[0]);
+ double length2 = Vector::Magnitude(point, handle1);
+ double length3 = Vector::Magnitude(point, handle2);
+
+ if ((length * Global::zoom) < 8.0)
+ {
+ obj->hitPoint[0] = true;
+ hoverPoint = obj->p[0];
+ hoverPointValid = true;
+ }
+ else if ((length2 * Global::zoom) < 8.0)
+ {
+ obj->hitPoint[1] = true;
+ hoverPoint = handle1;
+ hoverPointValid = true;
+ }
+ else if ((length3 * Global::zoom) < 8.0)
+ {
+ obj->hitPoint[2] = true;
+ hoverPoint = handle2;
+ hoverPointValid = true;
+ }
+ else if (((fabs(length - obj->radius[0]) * Global::zoom) < 2.0) && (span < obj->angle[1]))
+ obj->hitObject = true;
+
+ obj->hovered = (obj->hitPoint[0] || obj->hitPoint[1] || obj->hitPoint[2] || obj->hitObject ? true : false);
+
+ if ((oldHP0 != obj->hitPoint[0]) || (oldHP1 != obj->hitPoint[1]) || (oldHP2 != obj->hitPoint[2]) || (oldHO != obj->hitObject))
+ needUpdate = true;
+
+ break;
+ }
+
+ case OTDimension:
+ {
+ bool oldHP0 = obj->hitPoint[0], oldHP1 = obj->hitPoint[1], oldHP2 = obj->hitPoint[2], oldHP3 = obj->hitPoint[3], oldHP4 = obj->hitPoint[4], oldHO = obj->hitObject;
+ obj->hitPoint[0] = obj->hitPoint[1] = obj->hitPoint[2] = obj->hitPoint[3] = obj->hitPoint[4] = obj->hitObject = false;
+
+ Dimension * d = (Dimension *)obj;
+
+ Vector orthogonal = Vector::Normal(d->lp[0], d->lp[1]);
+ // Get our line parallel to our points
+ float scaledThickness = Global::scale * obj->thickness;
+ Point p1 = d->lp[0] + (orthogonal * 10.0 * scaledThickness);
+ Point p2 = d->lp[1] + (orthogonal * 10.0 * scaledThickness);
+ Point p3(p1, point);
+
+ Vector v1(d->p[0], point);
+ Vector v2(d->p[1], point);
+ Vector lineSegment(p1, p2);
+ double t = Geometry::ParameterOfLineAndPoint(p1, p2, point);
+ double distance;
+ Point midpoint = (p1 + p2) / 2.0;
+ Point hFSPoint = Point(midpoint, point);
+ Point hCS1Point = Point((p1 + midpoint) / 2.0, point);
+ Point hCS2Point = Point((midpoint + p2) / 2.0, point);
+
+ 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 * p3.y - p3.x * lineSegment.y)
+ / lineSegment.Magnitude());
+
+ if ((v1.Magnitude() * Global::zoom) < 8.0)
+ obj->hitPoint[0] = true;
+ else if ((v2.Magnitude() * Global::zoom) < 8.0)
+ obj->hitPoint[1] = true;
+ else if ((distance * Global::zoom) < 5.0)
+ obj->hitObject = true;
+
+ if ((hFSPoint.Magnitude() * Global::zoom) < 8.0)
+ obj->hitPoint[2] = true;
+ else if ((hCS1Point.Magnitude() * Global::zoom) < 8.0)
+ obj->hitPoint[3] = true;
+ else if ((hCS2Point.Magnitude() * Global::zoom) < 8.0)
+ obj->hitPoint[4] = true;
+
+// return (hitPoint1 || hitPoint2 || hitLine || hitFlipSwitch || hitChangeSwitch1 || hitChangeSwitch2 ? true : false);
+ obj->hovered = (obj->hitPoint[0] || obj->hitPoint[1] || obj->hitPoint[2] || obj->hitPoint[3] || obj->hitPoint[4] || obj->hitObject ? true : false);
+
+ if ((oldHP0 != obj->hitPoint[0]) || (oldHP1 != obj->hitPoint[1]) || (oldHP2 != obj->hitPoint[2]) || (oldHP3 != obj->hitPoint[3]) || (oldHP4 != obj->hitPoint[4]) || (oldHO != obj->hitObject))
+ needUpdate = true;
+
+ break;
+ }
+
+ case OTText:
+ {
+ Text * t = (Text *)obj;
+ bool oldHO = obj->hitObject;
+ obj->hitObject = false;
+
+ Rect r(obj->p[0], Point(obj->p[0].x + t->extents.Width(), obj->p[0].y - t->extents.Height()));
+//printf("Text: p=<%lf, %lf>, w/h=%lf, %lf [lrtb=%lf, %lf, %lf, %lf]\n", obj->p[0].x, obj->p[0].y, t->extents.Width(), t->extents.Height(), t->extents.l, t->extents.r, t->extents.t, t->extents.b);
+
+ if (r.Contains(point))
+ obj->hitObject = true;
+
+ obj->hovered = (obj->hitObject ? true : false);
+
+ if (oldHO != obj->hitObject)
+ needUpdate = true;
+
+ break;
+ }
+
+ case OTContainer:
+ {
+ // Containers must be recursively tested...
+ Container * c = (Container *)obj;
+ c->hitObject = false;
+ c->hovered = false;
+ std::vector<void *>::iterator i;
+
+ for(i=c->objects.begin(); i!=c->objects.end(); i++)
+ {
+ Object * cObj = (Object *)*i;
+
+ if (HitTest(cObj, point))
+ needUpdate = true;
+
+ if (cObj->hitObject == true)
+ c->hitObject = true;
+
+ if (cObj->hovered == true)
+ c->hovered = true;
+ }
+
+ break;
+ }
+
+ default:
+ break;
+ }
+
+ return needUpdate;
+}
+
+
+bool DrawingView::HandleObjectClicked(void)
+{
+ if (dragged->type == OTDimension)
+ {
+ Dimension * d = (Dimension *)dragged;
+
+ if (d->hitPoint[2])
+ {
+ // Hit the "flip sides" switch, so flip 'em
+ Point temp = d->p[0];
+ d->p[0] = d->p[1];
+ d->p[1] = temp;
+ return true;
+ }
+ else if (d->hitPoint[3])
+ {
+ // There are three cases here: aligned, horizontal, & vertical.
+ // Aligned and horizontal do the same thing, vertical goes back to
+ // linear.
+ if (d->subtype == DTLinearVert)
+ d->subtype = DTLinear;
+ else
+ d->subtype = DTLinearVert;
+
+ return true;
+ }
+ else if (d->hitPoint[4])
+ {
+ // There are three cases here: aligned, horizontal, & vertical.
+ // Aligned and vertical do the same thing, horizontal goes back to
+ // linear.
+ if (d->subtype == DTLinearHorz)
+ d->subtype = DTLinear;
+ else
+ d->subtype = DTLinearHorz;
+
+ return true;
+ }
+ }
+
+ return false;
+}
+
+
+void DrawingView::HandleObjectMovement(Point point)
+{
+ Point delta = point - oldPoint;
+//printf("HOM: old = (%f,%f), new = (%f, %f), delta = (%f, %f)\n", oldPoint.x, oldPoint.y, point.x, point.y, delta.x, delta.y);
+// Object * obj = (Object *)hover[0];
+ Object * obj = dragged;
+//printf("Object type = %i (size=%i), ", obj->type, hover.size());
+//printf("Object (%X) move: hp1=%s, hp2=%s, hl=%s\n", obj, (obj->hitPoint[0] ? "true" : "false"), (obj->hitPoint[1] ? "true" : "false"), (obj->hitObject ? "true" : "false"));
+
+ switch (obj->type)
+ {
+ case OTLine:
+ if (obj->hitPoint[0])
+ obj->p[0] = point;
+ else if (obj->hitPoint[1])
+ obj->p[1] = point;
+ else if (obj->hitObject)
+ {
+ obj->p[0] += delta;
+ obj->p[1] += delta;
+ }
+
+ break;
+
+ case OTCircle:
+ if (obj->hitPoint[0])
+ obj->p[0] = point;
+ else if (obj->hitObject)
+ {
+//this doesn't work. we need to save this on mouse down for this to work correctly!
+// double oldRadius = obj->radius[0];
+ obj->radius[0] = Vector::Magnitude(obj->p[0], point);
+
+ QString text = QObject::tr("Radius: %1");//\nScale: %2%");
+ informativeText = text.arg(obj->radius[0], 0, 'd', 4);//.arg(obj->radius[0] / oldRadius * 100.0, 0, 'd', 0);
+ }
+
+ break;
+
+ case OTArc:
+ if (obj->hitPoint[0])
+ obj->p[0] = point;
+ else if (obj->hitPoint[1])
+ {
+ // Change the Arc's span (handle #1)
+ if (shiftDown)
+ {
+ double angle = Vector::Angle(obj->p[0], point);
+ double delta = angle - obj->angle[0];
+
+ if (delta < 0)
+ delta += TAU;
+
+ obj->angle[1] -= delta;
+ obj->angle[0] = angle;
+
+ if (obj->angle[1] < 0)
+ obj->angle[1] += TAU;
+
+ QString text = QObject::tr("Span: %1") + QChar(0x00B0) + QObject::tr("\n%2") + QChar(0x00B0) + QObject::tr(" - %3") + QChar(0x00B0);
+ informativeText = text.arg(obj->angle[1] * RADIANS_TO_DEGREES, 0, 'd', 4).arg(obj->angle[0] * RADIANS_TO_DEGREES, 0, 'd', 2).arg((obj->angle[0] + obj->angle[1]) * RADIANS_TO_DEGREES, 0, 'd', 2);
+ return;
+ }
+
+ double angle = Vector::Angle(obj->p[0], point);
+ obj->angle[0] = angle;
+ QString text = QObject::tr("Start angle: %1") + QChar(0x00B0);
+ informativeText = text.arg(obj->angle[0] * RADIANS_TO_DEGREES, 0, 'd', 4);
+ }
+ else if (obj->hitPoint[2])
+ {
+ // Change the Arc's span (handle #2)
+ if (shiftDown)
+ {
+ double angle = Vector::Angle(obj->p[0], point);
+ obj->angle[1] = angle - obj->angle[0];
+
+ if (obj->angle[1] < 0)
+ obj->angle[1] += TAU;
+
+ QString text = QObject::tr("Span: %1") + QChar(0x00B0) + QObject::tr("\n%2") + QChar(0x00B0) + QObject::tr(" - %3") + QChar(0x00B0);
+ informativeText = text.arg(obj->angle[1] * RADIANS_TO_DEGREES, 0, 'd', 4).arg(obj->angle[0] * RADIANS_TO_DEGREES, 0, 'd', 2).arg((obj->angle[0] + obj->angle[1]) * RADIANS_TO_DEGREES, 0, 'd', 2);
+ return;
+ }
+
+ double angle = Vector::Angle(obj->p[0], point);
+ obj->angle[0] = angle - obj->angle[1];
+
+ if (obj->angle[0] < 0)
+ obj->angle[0] += TAU;
+
+ QString text = QObject::tr("End angle: %1") + QChar(0x00B0);
+ informativeText = text.arg((obj->angle[0] + obj->angle[1]) * RADIANS_TO_DEGREES, 0, 'd', 4);
+ }
+ else if (obj->hitObject)
+ {
+ if (shiftDown)
+ {
+ return;
+ }
+
+ obj->radius[0] = Vector::Magnitude(obj->p[0], point);
+ QString text = QObject::tr("Radius: %1");
+ informativeText = text.arg(obj->radius[0], 0, 'd', 4);
+ }
+
+ break;
+
+ case OTDimension:
+ if (obj->hitPoint[0])
+ obj->p[0] = point;
+ else if (obj->hitPoint[1])
+ obj->p[1] = point;
+ else if (obj->hitObject)
+ {
+ obj->p[0] += delta;
+ obj->p[1] += delta;
+ }
+
+ break;
+
+ case OTText:
+ if (obj->hitObject)
+ obj->p[0] += delta;
+
+ break;
+
+ case OTContainer:
+ // This is shitty, but works for now until I can code up something
+ // nicer :-)
+ TranslateObject(obj, delta);
+
+ break;
+ default:
+ break;
+ }