2 // painter.cpp: Paint abstraction layer between Archtektonas and Qt
4 // Part of the Architektonas Project
5 // (C) 2011 Underground Software
6 // See the README and GPLv3 files for licensing and warranty information
8 // JLH = James Hammons <jlhamm@acm.org>
11 // --- ---------- ------------------------------------------------------------
12 // JLH 09/20/2011 Created this file
17 #include "mathconstants.h"
20 // Set class variable defaults
21 Vector Painter::origin(-10.0, -10.0);
22 double Painter::zoom = 1.0;
23 Vector Painter::screenSize(200.0, 200.0);
26 Painter::Painter(QPainter * p/*= NULL*/): painter(p)
34 Vector Painter::CartesianToQtCoords(Vector v)
36 // Convert regular Cartesian coordinates to the inverted Y-axis Qt coordinates
37 // at the current origin and zoom level.
38 return Vector((v.x - origin.x) * zoom, screenSize.y - ((v.y - origin.y) * zoom));
41 Vector Painter::QtToCartesianCoords(Vector v)
43 // Convert screen location, with inverted Y-axis coordinates, to regular
44 // Cartesian coordinates at the current zoom level.
45 return Vector((v.x / zoom) + origin.x, ((screenSize.y - v.y) / zoom) + origin.y);
49 e.g., we have a point on the screen at Qt coords of 10, 10, screenSize is 100, 100.
50 origin is -10, -10 and zoom level is 2 (200%)
52 1st, invert the Y: 10, 10 -> 10, 90
53 2nd, add origin: 10, 90 -> 0, 80 (no, not right--err, yes, it is)
54 3rd, aply zoom: 0, 80 -> 0, 40
58 1st, invert the Y: 10, 10 -> 10, 90
59 2nd, aply zoom: 10, 90 -> 5, 45
60 3rd, add origin: 5, 45 -> -5, 35
62 it depends on whether or not origin is in Qt coords or cartesian. If Qt, then the 1st
63 is correct, otherwise, the 2nd is correct.
65 The way we calculate the Cartesian to Qt shows the 2nd (origin is cartesian) to be correct.
69 void Painter::SetRenderHint(int hint)
74 painter->setRenderHint((QPainter::RenderHint)hint);
77 void Painter::SetBrush(QBrush brush)
82 painter->setBrush(brush);
85 void Painter::SetFont(QFont font)
90 painter->setFont(font);
93 void Painter::SetPen(QPen pen)
101 void Painter::DrawAngledText(Vector center, double angle, QString text)
106 // Strategy: Since Qt doesn't have any rotated text drawing functions,
107 // we instead translate the origin to the center of the text to be drawn and
108 // then rotate the frame to the desired angle.
109 center = CartesianToQtCoords(center);
111 // We may need this stuff... If dimension text is large enough.
112 // int textWidth = QFontMetrics(painter->font()).width(text);
113 // int textHeight = QFontMetrics(painter->font()).height();
114 QRectF textBox(-100, -100, 200, 200); // x, y, w, h; x/y = upper left corner
116 // This is in pixels. Might not render correctly at all zoom levels.
117 // Need to figure out if dimensions are always rendered at one size regardless of zoom,
118 // or if they have a definite size, and are thus zoomable.
119 // If zoomable, this is incorrect:
120 // (Added zoom, so this is correct now :-)
121 int yOffset = -12 * zoom;
123 // Fix text so it isn't upside down...
124 if ((angle > PI * 0.5) && (angle < PI * 1.5))
131 Vector offset = CartesianToQtCoords(Vector(0, yOffset));
132 textBox.translate(offset.x, offset.y);
134 textBox.translate(0, yOffset);
137 painter->translate(center.x, center.y);
138 // Angles are backwards in the Qt coord system, so we flip ours...
139 painter->rotate(-angle * RADIANS_TO_DEGREES);
140 //Need to fix this so the text scales as well...
141 painter->drawText(textBox, Qt::AlignCenter, text);
145 void Painter::DrawArc(Vector center, double radius, double startAngle, double span)
147 center = CartesianToQtCoords(center);
148 // Need to multiply scalar quantities by the zoom factor as well...
150 QRectF rectangle(QPointF(center.x - radius, center.y - radius),
151 QPointF(center.x + radius, center.y + radius));
152 int angle1 = (int)(startAngle * RADIANS_TO_DEGREES * 16.0);
153 int angle2 = (int)(span * RADIANS_TO_DEGREES * 16.0);
154 painter->drawArc(rectangle, angle1, angle2);
157 void Painter::DrawEllipse(Vector center, double axis1, double axis2)
159 // Need to multiply scalar quantities by the zoom factor as well...
160 center = CartesianToQtCoords(center);
161 painter->drawEllipse(QPointF(center.x, center.y), axis1 * zoom, axis2 * zoom);
164 // This function is for drawing object handles without regard for zoom level;
165 // we don't want our object handle size to depend on the zoom level!
166 void Painter::DrawHandle(Vector center)
168 center = CartesianToQtCoords(center);
169 painter->drawEllipse(QPointF(center.x, center.y), 4.0, 4.0);
172 void Painter::DrawLine(int x1, int y1, int x2, int y2)
177 Vector v1 = CartesianToQtCoords(Vector(x1, y1));
178 Vector v2 = CartesianToQtCoords(Vector(x2, y2));
179 painter->drawLine(v1.x, v1.y, v2.x, v2.y);
182 void Painter::DrawLine(Vector v1, Vector v2)
187 v1 = CartesianToQtCoords(v1);
188 v2 = CartesianToQtCoords(v2);
189 painter->drawLine(QPointF(v1.x, v1.y), QPointF(v2.x, v2.y));
192 void Painter::DrawPoint(int x, int y)
197 Vector v = CartesianToQtCoords(Vector(x, y));
198 painter->drawPoint(v.x, v.y);
201 void Painter::DrawRoundedRect(QRectF rect, double radiusX, double radiusY)
206 painter->drawRoundedRect(rect, radiusX, radiusY);
209 void Painter::DrawText(QRectF rect, int type, QString text)
214 painter->drawText(rect, (Qt::AlignmentFlag)type, text);