]> Shamusworld >> Repos - architektonas/blobdiff - src/painter.cpp
Added line-to-circle intersection code.
[architektonas] / src / painter.cpp
index 35528faf9887ce5930306cb6f6d2c26465392c9a..e8aaba1cf04cc8f7602a9d587884c0309f0e7222 100644 (file)
@@ -79,6 +79,26 @@ void Painter::SetRenderHint(int hint)
 }
 
 
+void Painter::SetPen(QPen pen)
+{
+       if (!painter)
+               return;
+
+       painter->setPen(pen);
+}
+
+
+void Painter::SetPen(uint32_t color, float size/*= 0*/, int style/*= 0*/)
+{
+       if (!painter)
+               return;
+
+       // We can cast style as Qt:PenStyle because they line up 1-to-1
+       painter->setPen(QPen(QColor(color >> 16, (color >> 8) & 0xFF, color & 0xFF, 255),
+               size, (Qt::PenStyle)style));
+}
+
+
 void Painter::SetBrush(QBrush brush)
 {
        if (!painter)
@@ -88,21 +108,21 @@ void Painter::SetBrush(QBrush brush)
 }
 
 
-void Painter::SetFont(QFont font)
+void Painter::SetBrush(uint32_t color)
 {
        if (!painter)
                return;
 
-       painter->setFont(font);
+       painter->setBrush(QBrush(QColor(color >> 16, (color >> 8) & 0xFF, color & 0xFF, 255)));
 }
 
 
-void Painter::SetPen(QPen pen)
+void Painter::SetFont(QFont font)
 {
        if (!painter)
                return;
 
-       painter->setPen(pen);
+       painter->setFont(font);
 }
 
 
@@ -128,11 +148,19 @@ void Painter::DrawAngledText(Vector center, double angle, QString text, double s
        float yOffset = -12.0 * Global::zoom * size;
 
        // Fix text so it isn't upside down...
+#if 0
        if ((angle > PI * 0.5) && (angle < PI * 1.5))
        {
                angle += PI;
                yOffset = 12.0 * Global::zoom * size;
        }
+#else
+       if ((angle > QTR_TAU) && (angle < THREE_QTR_TAU))
+       {
+               angle += HALF_TAU;
+               yOffset = 12.0 * Global::zoom * size;
+       }
+#endif
 
        textBox.translate(0, yOffset);
        painter->save();
@@ -145,6 +173,30 @@ void Painter::DrawAngledText(Vector center, double angle, QString text, double s
 }
 
 
+//
+// Draw angled text. Draws text using point p as the upper left corner.
+// Size is point size, angle is in radians (defaults to 0).
+//
+void Painter::DrawTextObject(Point p, QString text, double size, double angle/*= 0*/)
+{
+       if (!painter)
+               return;
+
+       p = CartesianToQtCoords(p);
+       painter->setFont(QFont("Arial", Global::zoom * size));
+       int textWidth = QFontMetrics(painter->font()).width(text);
+       int textHeight = QFontMetrics(painter->font()).height();
+
+       QRectF textBox(0, 0, textWidth, textHeight);
+       painter->save();
+       painter->translate(p.x, p.y);
+       // Angles are backwards in the Qt coord system, so we flip ours...
+       painter->rotate(-angle * RADIANS_TO_DEGREES);
+       painter->drawText(textBox, Qt::AlignLeft | Qt::AlignTop , text);
+       painter->restore();
+}
+
+
 void Painter::DrawArc(Vector center, double radius, double startAngle, double span)
 {
        center = CartesianToQtCoords(center);
@@ -171,6 +223,7 @@ void Painter::DrawEllipse(Vector center, double axis1, double axis2)
 void Painter::DrawHandle(Vector center)
 {
        center = CartesianToQtCoords(center);
+       painter->setPen(QPen(Qt::red, 2.0, Qt::DotLine));
        painter->setBrush(Qt::NoBrush);
        painter->drawEllipse(QPointF(center.x, center.y), 4.0, 4.0);
 }
@@ -185,7 +238,7 @@ void Painter::DrawArrowHandle(Vector center, double angle)
 
        // Since we're drawing directly on the screen, the Y is inverted. So we use
        // the mirror of the angle.
-       double orthoAngle = -angle + (PI / 2.0);
+       double orthoAngle = -angle + QTR_TAU;//(PI / 2.0);
        Vector orthogonal = Vector(cos(orthoAngle), sin(orthoAngle));
        Vector unit = Vector(cos(-angle), sin(-angle));
 
@@ -211,7 +264,7 @@ void Painter::DrawArrowToLineHandle(Vector center, double angle)
 
        // Since we're drawing directly on the screen, the Y is inverted. So we use
        // the mirror of the angle.
-       double orthoAngle = -angle + (PI / 2.0);
+       double orthoAngle = -angle + QTR_TAU;//(PI / 2.0);
        Vector orthogonal = Vector(cos(orthoAngle), sin(orthoAngle));
        Vector unit = Vector(cos(-angle), sin(-angle));
 
@@ -310,7 +363,7 @@ void Painter::DrawArrowhead(Vector head, Vector tail, double size)
 
        // We draw the arrowhead aligned along the line from tail to head
        double angle = Vector(head - tail).Angle();
-       double orthoAngle = angle + (PI / 2.0);
+       double orthoAngle = angle + QTR_TAU;//(PI / 2.0);
        Vector orthogonal = Vector(cos(orthoAngle), sin(orthoAngle));
        Vector unit = Vector(head - tail).Unit();