]> Shamusworld >> Repos - ttedit/blobdiff - src/painter.cpp
Added rectangle point selection, canvas zooming.
[ttedit] / src / painter.cpp
diff --git a/src/painter.cpp b/src/painter.cpp
new file mode 100644 (file)
index 0000000..16268e7
--- /dev/null
@@ -0,0 +1,490 @@
+//
+// painter.cpp: Paint abstraction layer between Archtektonas and Qt
+//
+// Part of the Architektonas Project
+// (C) 2011 Underground Software
+// See the README and GPLv3 files for licensing and warranty information
+//
+// JLH = James Hammons <jlhamm@acm.org>
+//
+// WHO  WHEN        WHAT
+// ---  ----------  ------------------------------------------------------------
+// JLH  09/20/2011  Created this file
+//
+
+#include "painter.h"
+#include "global.h"
+#include "mathconstants.h"
+
+
+// Set class variable defaults
+//Vector Painter::origin(-10.0, -10.0);
+//double Painter::zoom = 1.0;
+//Vector Painter::screenSize(200.0, 200.0);
+
+
+Painter::Painter(QPainter * p/*= NULL*/): painter(p)
+{
+}
+
+
+Painter::~Painter()
+{
+}
+
+
+Vector Painter::CartesianToQtCoords(Vector v)
+{
+       // Convert regular Cartesian coordinates to the inverted Y-axis Qt
+       // coordinates at the current origin and zoom level.
+       return Vector((v.x - Global::origin.x) * Global::zoom, Global::screenSize.y - ((v.y - Global::origin.y) * Global::zoom));
+}
+
+
+Vector Painter::QtToCartesianCoords(Vector v)
+{
+       // Convert screen location, with inverted Y-axis coordinates, to regular
+       // Cartesian coordinates at the current zoom level.
+       return Vector((v.x / Global::zoom) + Global::origin.x, ((Global::screenSize.y - v.y) / Global::zoom) + Global::origin.y);
+/*
+How to do it:
+
+e.g., we have a point on the screen at Qt coords of 10, 10, screenSize is 100, 100.
+origin is -10, -10 and zoom level is 2 (200%)
+
+1st, invert the Y: 10, 10 -> 10, 90
+2nd, add origin:   10, 90 ->  0, 80 (no, not right--err, yes, it is)
+3rd, aply zoom:     0, 80 ->  0, 40
+
+or, is it:
+
+1st, invert the Y: 10, 10 -> 10, 90
+2nd, aply zoom:    10, 90 ->  5, 45
+3rd, add origin:    5, 45 -> -5, 35
+
+it depends on whether or not origin is in Qt coords or cartesian. If Qt, then the 1st
+is correct, otherwise, the 2nd is correct.
+
+The way we calculate the Cartesian to Qt shows the 2nd (origin is cartesian) to be correct.
+*/
+}
+
+
+void Painter::SetRenderHint(int hint)
+{
+       if (!painter)
+               return;
+
+       painter->setRenderHint((QPainter::RenderHint)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)
+               return;
+
+       painter->setBrush(brush);
+}
+
+
+void Painter::SetBrush(uint32_t color)
+{
+       if (!painter)
+               return;
+
+       painter->setBrush(QBrush(QColor(color >> 16, (color >> 8) & 0xFF, color & 0xFF, 255)));
+}
+
+
+void Painter::SetFont(QFont font)
+{
+       if (!painter)
+               return;
+
+       painter->setFont(font);
+}
+
+
+void Painter::DrawAngledText(Vector center, double angle, QString text, double size)
+{
+       if (!painter)
+               return;
+
+       // Strategy: Since Qt doesn't have any rotated text drawing functions,
+       // we instead translate the origin to the center of the text to be drawn and
+       // then rotate the frame to the desired angle.
+       center = CartesianToQtCoords(center);
+
+       // We may need this stuff... If dimension text is large enough.
+//     int textWidth = QFontMetrics(painter->font()).width(text);
+//     int textHeight = QFontMetrics(painter->font()).height();
+       QRectF textBox(-100.0 * Global::zoom * size, -100.0 * Global::zoom * size, 200.0 * Global::zoom * size, 200.0 * Global::zoom * size);   // x, y, w, h; x/y = upper left corner
+
+       // This is in pixels. Might not render correctly at all zoom levels.
+       // Need to figure out if dimensions are always rendered at one size
+       // regardless of zoom, or if they have a definite size, and are thus
+       // zoomable.
+       float yOffset = -12.0 * Global::zoom * size;
+
+       // Fix text so it isn't upside down...
+       if ((angle > PI * 0.5) && (angle < PI * 1.5))
+       {
+               angle += PI;
+               yOffset = 12.0 * Global::zoom * size;
+       }
+
+       textBox.translate(0, yOffset);
+       painter->save();
+       painter->translate(center.x, center.y);
+       // Angles are backwards in the Qt coord system, so we flip ours...
+       painter->rotate(-angle * RADIANS_TO_DEGREES);
+//Need to fix this so the text scales as well...
+       painter->drawText(textBox, Qt::AlignCenter, text);
+       painter->restore();
+}
+
+
+//
+// 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);
+       // Need to multiply scalar quantities by the zoom factor as well...
+       radius *= Global::zoom;
+       QRectF rectangle(QPointF(center.x - radius, center.y - radius),
+               QPointF(center.x + radius, center.y + radius));
+       int angle1 = (int)(startAngle * RADIANS_TO_DEGREES * 16.0);
+       int angle2 = (int)(span * RADIANS_TO_DEGREES * 16.0);
+       painter->drawArc(rectangle, angle1, angle2);
+}
+
+
+void Painter::DrawEllipse(Vector center, double axis1, double axis2)
+{
+       // Need to multiply scalar quantities by the zoom factor as well...
+       center = CartesianToQtCoords(center);
+       painter->drawEllipse(QPointF(center.x, center.y), axis1 * Global::zoom, axis2 * Global::zoom);
+}
+
+
+// This function is for drawing object handles without regard for zoom level;
+// we don't want our object handle size to depend on the zoom level!
+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);
+}
+
+
+// This function is for drawing object handles without regard for zoom level;
+// we don't want our object handle size to depend on the zoom level!
+void Painter::DrawArrowHandle(Vector center, double angle)
+{
+       center = CartesianToQtCoords(center);
+       QPolygonF arrow;
+
+       // 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);
+       Vector orthogonal = Vector(cos(orthoAngle), sin(orthoAngle));
+       Vector unit = Vector(cos(-angle), sin(-angle));
+
+       Point p0 = center + (unit * 6.0);
+       Point p1 = center + (unit * 21.0);
+       Point p1b = center + (unit * 11.0);
+       Point p2 = p1b + (orthogonal * 5.0);
+       Point p3 = p1b - (orthogonal * 5.0);
+
+       painter->drawLine(p0.x, p0.y, p1.x, p1.y);
+       arrow << QPointF(p1.x, p1.y) << QPointF(p2.x, p2.y) << QPointF(p3.x, p3.y);
+
+       painter->drawPolygon(arrow);
+}
+
+
+// This function is for drawing object handles without regard for zoom level;
+// we don't want our object handle size to depend on the zoom level!
+void Painter::DrawArrowToLineHandle(Vector center, double angle)
+{
+       DrawArrowHandle(center, angle);
+       center = CartesianToQtCoords(center);
+
+       // 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);
+       Vector orthogonal = Vector(cos(orthoAngle), sin(orthoAngle));
+       Vector unit = Vector(cos(-angle), sin(-angle));
+
+       Point p1 = center + (unit * 21.0);
+       Point p2 = p1 + (orthogonal * 7.0);
+       Point p3 = p1 - (orthogonal * 7.0);
+
+       painter->drawLine(p2.x, p2.y, p3.x, p3.y);
+}
+
+
+void Painter::DrawLine(int x1, int y1, int x2, int y2)
+{
+       if (!painter)
+               return;
+
+       Vector v1 = CartesianToQtCoords(Vector(x1, y1));
+       Vector v2 = CartesianToQtCoords(Vector(x2, y2));
+       painter->drawLine(v1.x, v1.y, v2.x, v2.y);
+}
+
+
+void Painter::DrawLine(Vector v1, Vector v2)
+{
+       if (!painter)
+               return;
+
+       v1 = CartesianToQtCoords(v1);
+       v2 = CartesianToQtCoords(v2);
+       painter->drawLine(QPointF(v1.x, v1.y), QPointF(v2.x, v2.y));
+}
+
+
+void Painter::DrawPoint(int x, int y)
+{
+       if (!painter)
+               return;
+
+       Vector v = CartesianToQtCoords(Vector(x, y));
+       painter->drawPoint(v.x, v.y);
+}
+
+
+// The rect passed in is in Qt coordinates...
+void Painter::DrawRoundedRect(QRectF rect, double radiusX, double radiusY)
+{
+       if (!painter)
+               return;
+
+       painter->drawRoundedRect(rect, radiusX, radiusY);
+}
+
+
+// The rect passed in is in Cartesian but we want to pad it by a set number of
+// pixels (currently set at 8), so the pad looks the same regardless of zoom.
+void Painter::DrawPaddedRect(QRectF rect)
+{
+       if (!painter)
+               return;
+
+       Vector v1 = CartesianToQtCoords(Vector(rect.x(), rect.y()));
+       Vector v2 = CartesianToQtCoords(Vector(rect.right(), rect.bottom()));
+       QRectF screenRect(QPointF(v1.x, v1.y), QPointF(v2.x, v2.y));
+       screenRect.adjust(-8, 8, 8, -8);        // Left/top, right/bottom
+       painter->drawRect(screenRect);
+}
+
+
+void Painter::DrawRect(QRectF rect)
+{
+       if (!painter)
+               return;
+
+       Vector v1 = CartesianToQtCoords(Vector(rect.x(), rect.y()));
+       Vector v2 = CartesianToQtCoords(Vector(rect.right(), rect.bottom()));
+       QRectF screenRect(QPointF(v1.x, v1.y), QPointF(v2.x, v2.y));
+       painter->drawRect(screenRect);
+}
+
+
+void Painter::DrawText(QRectF rect, int type, QString text)
+{
+       if (!painter)
+               return;
+
+       painter->drawText(rect, (Qt::AlignmentFlag)type, text);
+}
+
+
+void Painter::DrawArrowhead(Vector head, Vector tail, double size)
+{
+       if (!painter)
+               return;
+
+       QPolygonF arrow;
+
+       // We draw the arrowhead aligned along the line from tail to head
+       double angle = Vector(head - tail).Angle();
+       double orthoAngle = angle + (PI / 2.0);
+       Vector orthogonal = Vector(cos(orthoAngle), sin(orthoAngle));
+       Vector unit = Vector(head - tail).Unit();
+
+       Point p1 = head - (unit * 9.0 * size);
+       Point p2 = p1 + (orthogonal * 3.0 * size);
+       Point p3 = p1 - (orthogonal * 3.0 * size);
+
+       Point p4 = CartesianToQtCoords(head);
+       Point p5 = CartesianToQtCoords(p2);
+       Point p6 = CartesianToQtCoords(p3);
+
+       arrow << QPointF(p4.x, p4.y) << QPointF(p5.x, p5.y) << QPointF(p6.x, p6.y);
+
+       painter->drawPolygon(arrow);
+}
+
+
+// Point is given in Cartesian coordinates
+void Painter::DrawCrosshair(Vector point)
+{
+       if (!painter)
+               return;
+
+       Vector screenPoint = CartesianToQtCoords(point);
+       painter->drawLine(0, screenPoint.y, Global::screenSize.x, screenPoint.y);
+       painter->drawLine(screenPoint.x, 0, screenPoint.x, Global::screenSize.y);
+}
+
+
+void Painter::DrawInformativeText(QString text)
+{
+       painter->setFont(*Global::font);
+       QRectF bounds = painter->boundingRect(QRectF(), Qt::AlignVCenter, text);
+       bounds.moveTo(17.0, 17.0);
+       QRectF textRect = bounds;
+       textRect.adjust(-7.0, -7.0, 7.0, 7.0);
+
+       QPen pen = QPen(QColor(0x00, 0xFF, 0x00), 1.0, Qt::SolidLine);
+       painter->setPen(pen);
+       painter->setBrush(QBrush(QColor(0x40, 0xFF, 0x40, 0x9F)));
+       painter->drawRoundedRect(textRect, 7.0, 7.0);
+
+       pen = QPen(QColor(0x00, 0x5F, 0xDF));
+       painter->setPen(pen);
+       painter->drawText(bounds, Qt::AlignVCenter, text);
+}
+
+
+void Painter::DrawBezier(Point p1, Point p2, Point p3)
+{
+       p1 = CartesianToQtCoords(p1);
+       p2 = CartesianToQtCoords(p2);
+       p3 = CartesianToQtCoords(p3);
+
+       QPainterPath path;
+       path.moveTo(p1.x, p1.y);
+       path.quadTo(p2.x, p2.y, p3.x, p3.y);
+       painter->drawPath(path);
+}
+
+
+void Painter::DrawBezier(IPoint p1, IPoint p2, IPoint p3)
+{
+       DrawBezier(Point(p1.x, p1.y), Point(p2.x, p2.y), Point(p3.x, p3.y));
+}
+
+
+//
+// Draw a sqaure dot (5x5, centered on Vector; non-scaled)
+//
+void Painter::DrawSquareDot(Vector v)
+{
+       QPoint pt[4];
+       v = CartesianToQtCoords(v);
+
+       pt[0] = QPoint(v.x - 2, v.y - 2);
+       pt[1] = QPoint(v.x + 2, v.y - 2);
+       pt[2] = QPoint(v.x + 2, v.y + 2);
+       pt[3] = QPoint(v.x - 2, v.y + 2);
+
+       painter->drawPolygon(pt, 4);
+}
+
+
+//
+// Draw a round dot (5x5, centered on Vector; non-scaled)
+//
+void Painter::DrawRoundDot(Vector v)
+{
+       QPoint pt[8];
+       v = CartesianToQtCoords(v);
+
+       pt[0] = QPoint(v.x - 1, v.y - 2);
+       pt[1] = QPoint(v.x + 1, v.y - 2);
+       pt[2] = QPoint(v.x + 2, v.y - 1);
+       pt[3] = QPoint(v.x + 2, v.y + 1);
+       pt[4] = QPoint(v.x + 1, v.y + 2);
+       pt[5] = QPoint(v.x - 1, v.y + 2);
+       pt[6] = QPoint(v.x - 2, v.y + 1);
+       pt[7] = QPoint(v.x - 2, v.y - 1);
+
+       painter->drawPolygon(pt, 8);
+}
+
+
+//
+// Draw a sqaure dot (nxn, centered on Vector; non-scaled)
+//
+void Painter::DrawSquareDotN(Vector v, uint32_t n)
+{
+       QPoint pt[4];
+       uint32_t offset = (n - 1) / 2;
+       v = CartesianToQtCoords(v);
+
+       pt[0] = QPoint(v.x - offset, v.y - offset);
+       pt[1] = QPoint(v.x + offset, v.y - offset);
+       pt[2] = QPoint(v.x + offset, v.y + offset);
+       pt[3] = QPoint(v.x - offset, v.y + offset);
+
+       painter->drawPolygon(pt, 4);
+}
+
+
+//
+// Draw a round dot (nxn, centered on Vector; non-scaled)
+//
+void Painter::DrawRoundDotN(Vector v, uint32_t n)
+{
+       int radius = (n / 2) + 1;
+       v = CartesianToQtCoords(v);
+       painter->drawEllipse(v.x - radius, v.y - radius, n, n);
+}