+ 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;
+ }