+ QPainter qtPainter(this);
+ Painter painter(&qtPainter);
+
+ if (useAntialiasing)
+ qtPainter.setRenderHint(QPainter::Antialiasing);
+
+ Global::viewportHeight = size().height();
+
+ // Draw coordinate axes
+ painter.SetPen(QPen(Qt::blue, 1.0, Qt::DotLine));
+ painter.DrawLine(0, -16384, 0, 16384);
+ painter.DrawLine(-16384, 0, 16384, 0);
+
+ // Do object rendering...
+ RenderObjects(&painter, document.objects);
+
+ // Do tool rendering, if any...
+ if (Global::tool)
+ {
+ painter.SetPen(QPen(QColor(200, 100, 0, 255), 1.0, Qt::DashLine));
+ painter.DrawCrosshair(oldPoint);
+ ToolDraw(&painter);
+ }
+
+ // Do selection rectangle rendering, if any
+ if (Global::selectionInProgress)
+ {
+ painter.SetPen(QPen(QColor(255, 127, 0, 255)));
+ painter.SetBrush(QBrush(QColor(255, 127, 0, 100)));
+ painter.DrawRect(Global::selection);
+ }
+
+ if (!informativeText.isEmpty())
+ painter.DrawInformativeText(informativeText);
+}
+
+
+//
+// Renders objects in the passed in vector
+//
+void DrawingView::RenderObjects(Painter * painter, std::vector<void *> & v)
+{
+ std::vector<void *>::iterator i;
+
+ for(i=v.begin(); i!=v.end(); i++)
+ {
+ Object * obj = (Object *)(*i);
+ float scaledThickness = Global::scale * obj->thickness;
+
+ if ((Global::tool == TTRotate) && ctrlDown && obj->selected)
+ {
+ painter->SetPen(0x00FF00, 2.0, LSSolid);
+ }
+ else
+ {
+ painter->SetPen(obj->color, Global::zoom * scaledThickness, obj->style);
+ painter->SetBrush(obj->color);
+
+ if (obj->selected || obj->hitObject)
+ painter->SetPen(0xFF0000, Global::zoom * scaledThickness, LSDash);
+ }
+
+ switch (obj->type)
+ {
+ case OTLine:
+ painter->DrawLine(obj->p[0], obj->p[1]);
+
+ if (obj->hitPoint[0])
+ painter->DrawHandle(obj->p[0]);
+
+ if (obj->hitPoint[1])
+ painter->DrawHandle(obj->p[1]);
+
+ break;
+ case OTCircle:
+ painter->SetBrush(QBrush(Qt::NoBrush));
+ painter->DrawEllipse(obj->p[0], obj->radius[0], obj->radius[0]);
+
+ if (obj->hitPoint[0])
+ painter->DrawHandle(obj->p[0]);
+
+ break;
+ case OTArc:
+ painter->DrawArc(obj->p[0], obj->radius[0], obj->angle[0], obj->angle[1]);
+
+ if (obj->hitPoint[0])
+ painter->DrawHandle(obj->p[0]);
+
+ if (obj->hitPoint[1])
+ painter->DrawHandle(obj->p[0] + (Vector(cos(obj->angle[0]), sin(obj->angle[0])) * obj->radius[0]));
+
+ if (obj->hitPoint[2])
+ painter->DrawHandle(obj->p[0] + (Vector(cos(obj->angle[0] + obj->angle[1]), sin(obj->angle[0] + obj->angle[1])) * obj->radius[0]));
+
+ break;
+ case OTDimension:
+ {
+ Dimension * d = (Dimension *)obj;
+
+ Vector v(d->p[0], d->p[1]);
+ double angle = v.Angle();
+ Vector unit = v.Unit();
+ Vector linePt1 = d->p[0], linePt2 = d->p[1];
+ Vector ortho;
+ double x1, y1, length;
+
+ if (d->subtype == DTLinearVert)
+ {
+ if ((angle < 0) || (angle > PI))
+ {
+ x1 = (d->p[0].x > d->p[1].x ? d->p[0].x : d->p[1].x);
+ y1 = (d->p[0].y > d->p[1].y ? d->p[0].y : d->p[1].y);
+ ortho = Vector(1.0, 0);
+ angle = PI3_OVER_2;
+ }
+ else
+ {
+ x1 = (d->p[0].x > d->p[1].x ? d->p[1].x : d->p[0].x);
+ y1 = (d->p[0].y > d->p[1].y ? d->p[1].y : d->p[0].y);
+ ortho = Vector(-1.0, 0);
+ angle = PI_OVER_2;
+ }
+
+ linePt1.x = linePt2.x = x1;
+ length = fabs(d->p[0].y - d->p[1].y);
+ }
+ else if (d->subtype == DTLinearHorz)
+ {
+ if ((angle < PI_OVER_2) || (angle > PI3_OVER_2))
+ {
+ x1 = (d->p[0].x > d->p[1].x ? d->p[0].x : d->p[1].x);
+ y1 = (d->p[0].y > d->p[1].y ? d->p[0].y : d->p[1].y);
+ ortho = Vector(0, 1.0);
+ angle = 0;
+ }
+ else
+ {
+ x1 = (d->p[0].x > d->p[1].x ? d->p[1].x : d->p[0].x);
+ y1 = (d->p[0].y > d->p[1].y ? d->p[1].y : d->p[0].y);
+ ortho = Vector(0, -1.0);
+ angle = PI;
+ }
+
+ linePt1.y = linePt2.y = y1;
+ length = fabs(d->p[0].x - d->p[1].x);
+ }
+ else if (d->subtype == DTLinear)
+ {
+ angle = Vector(linePt1, linePt2).Angle();
+ ortho = Vector::Normal(linePt1, linePt2);
+ length = v.Magnitude();
+ }
+
+ unit = Vector(linePt1, linePt2).Unit();
+
+ Point p1 = linePt1 + (ortho * 10.0 * scaledThickness);
+ Point p2 = linePt2 + (ortho * 10.0 * scaledThickness);
+ Point p3 = linePt1 + (ortho * 16.0 * scaledThickness);
+ Point p4 = linePt2 + (ortho * 16.0 * scaledThickness);
+ Point p5 = d->p[0] + (ortho * 4.0 * scaledThickness);
+ Point p6 = d->p[1] + (ortho * 4.0 * scaledThickness);
+
+ /*
+ The numbers hardcoded into here, what are they?
+ I believe they are pixels.
+ */
+ // Draw extension lines (if certain type)
+ painter->DrawLine(p3, p5);
+ painter->DrawLine(p4, p6);
+
+ // Calculate whether or not the arrowheads are too crowded to put inside
+ // the extension lines. 9.0 is the length of the arrowhead.
+ double t = Geometry::ParameterOfLineAndPoint(linePt1, linePt2, linePt2 - (unit * 9.0 * scaledThickness));
+ //printf("Dimension::Draw(): t = %lf\n", t);
+
+ // On the screen, it's acting like this is actually 58%...
+ // This is correct, we want it to happen at > 50%
+ if (t > 0.58)
+ {
+ // Draw main dimension line + arrowheads
+ painter->DrawLine(p1, p2);
+ painter->DrawArrowhead(p1, p2, scaledThickness);
+ painter->DrawArrowhead(p2, p1, scaledThickness);
+ }
+ else
+ {
+ // Draw outside arrowheads
+ Point p7 = p1 - (unit * 9.0 * scaledThickness);
+ Point p8 = p2 + (unit * 9.0 * scaledThickness);
+ painter->DrawArrowhead(p1, p7, scaledThickness);
+ painter->DrawArrowhead(p2, p8, scaledThickness);
+ painter->DrawLine(p1, p1 - (unit * 14.0 * scaledThickness));
+ painter->DrawLine(p2, p2 + (unit * 14.0 * scaledThickness));
+ }
+
+ // Draw length of dimension line...
+ painter->SetFont(QFont("Arial", 8.0 * Global::zoom * scaledThickness));
+ Point ctr = p2 + (Vector(p2, p1) / 2.0);
+
+ #if 0
+ QString dimText = QString("%1\"").arg(Vector(endpoint - position).Magnitude());
+ #else
+ QString dimText;
+
+ if (length < 12.0)
+ dimText = QString("%1\"").arg(length);
+ else
+ {
+ double feet = (double)((int)length / 12);
+ double inches = length - (feet * 12.0);
+
+ if (inches == 0)
+ dimText = QString("%1'").arg(feet);
+ else
+ dimText = QString("%1' %2\"").arg(feet).arg(inches);
+ }
+ #endif
+
+ painter->DrawAngledText(ctr, angle, dimText, scaledThickness);
+
+ break;
+ }
+ case OTText:
+ {
+ Text * t = (Text *)obj;
+ painter->DrawTextObject(t->p[0], t->s.c_str(), scaledThickness);
+ break;
+ }
+ default:
+ break;
+ }
+ }
+}
+
+
+void DrawingView::AddHoveredToSelection(void)
+{
+ std::vector<void *>::iterator i;
+
+ for(i=document.objects.begin(); i!=document.objects.end(); i++)
+ {
+ if (((Object *)(*i))->hovered)
+ ((Object *)(*i))->selected = true;
+ }
+}
+
+
+void DrawingView::GetSelection(std::vector<void *> & v)
+{
+ v.clear();
+ std::vector<void *>::iterator i;
+
+ for(i=document.objects.begin(); i!=document.objects.end(); i++)
+ {
+ if (((Object *)(*i))->selected)
+ v.push_back(*i);
+ }
+}
+
+
+void DrawingView::GetHovered(std::vector<void *> & v)
+{
+ v.clear();
+ std::vector<void *>::iterator i;
+
+ for(i=document.objects.begin(); i!=document.objects.end(); i++)
+ {
+ if (((Object *)(*i))->hovered)
+// {
+//printf("GetHovered: adding object (%X) to hover... hp1=%s, hp2=%s, hl=%s\n", (*i), (((Line *)(*i))->hitPoint[0] ? "true" : "false"), (((Line *)(*i))->hitPoint[1] ? "true" : "false"), (((Line *)(*i))->hitObject ? "true" : "false"));
+ v.push_back(*i);
+// }
+ }
+}
+
+
+void DrawingView::resizeEvent(QResizeEvent * /*event*/)
+{
+ Global::screenSize = Vector(size().width(), size().height());
+ UpdateGridBackground();
+}
+
+
+void DrawingView::ToolHandler(int mode, Point p)
+{
+ if (Global::tool == TTLine)
+ LineHandler(mode, p);
+ else if (Global::tool == TTCircle)
+ CircleHandler(mode, p);
+ else if (Global::tool == TTArc)
+ ArcHandler(mode, p);
+ else if (Global::tool == TTRotate)
+ RotateHandler(mode, p);
+ else if (Global::tool == TTMirror)
+ MirrorHandler(mode, p);
+}