3 // Part of the Architektonas Project
4 // Extensively rewritten and refactored by James L. Hammons
5 // (C) 2010 Underground Software
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- -----------------------------------------------------------
11 // JLH 05/21/2010 Created this file. :-)
14 // BIG NOTE: THIS CLASS ASSUMES THAT THE PAINTER * IS VALID!!! BAD THINGS WILL
15 // HAPPEN IF IT IS NOT!!!
17 #include "paintinterface.h"
22 PaintInterface::PaintInterface(QPainter * p): painter(p), drawingMode(RS2::ModeFull),
23 offset(Vector(0.0, 0.0))
28 * Sets the drawing mode.
30 void PaintInterface::setDrawingMode(RS2::DrawingMode m)
36 * @return Current drawing mode.
38 RS2::DrawingMode PaintInterface::getDrawingMode()
43 void PaintInterface::createArc(QPolygon & pa, const Vector& cp, double radius,
44 double a1, double a2, bool reversed)
48 RS_DEBUG->print(RS_Debug::D_WARNING, "PaintInterface::createArc: invalid radius: %f", radius);
52 int cix; // Next point on circle
54 double aStep; // Angle Step (rad)
55 double a; // Current Angle (rad)
57 if (fabs(2.0 / radius) <= 1.0)
58 aStep = asin(2.0 / radius);
66 // less than a pixel long lines:
67 //if (radius*aStep<1.0) {
74 pa.setPoint(i++, toScreenX(cp.x + cos(a1) * radius), toScreenY(cp.y - sin(a1) * radius));
75 //moveTo(toScreenX(cp.x+cos(a1)*radius),
76 // toScreenY(cp.y-sin(a1)*radius));
80 // Arc Counterclockwise:
81 if (a1 > a2 - 1.0e-10)
84 for(a=a1+aStep; a<=a2; a+=aStep)
86 cix = toScreenX(cp.x + cos(a) * radius);
87 ciy = toScreenY(cp.y - sin(a) * radius);
89 pa.setPoint(i++, cix, ciy);
95 if (a1 < a2 + 1.0e-10)
98 for(a=a1-aStep; a>=a2; a-=aStep)
100 cix = toScreenX(cp.x + cos(a) * radius);
101 ciy = toScreenY(cp.y - sin(a) * radius);
104 pa.setPoint(i++, cix, ciy);
109 pa.setPoint(i++, toScreenX(cp.x + cos(a2) * radius), toScreenY(cp.y - sin(a2) * radius));
113 void PaintInterface::drawRect(const Vector & p1, const Vector & p2)
115 drawLine(Vector(p1.x, p1.y), Vector(p2.x, p1.y));
116 drawLine(Vector(p2.x, p1.y), Vector(p2.x, p2.y));
117 drawLine(Vector(p2.x, p2.y), Vector(p1.x, p2.y));
118 drawLine(Vector(p1.x, p2.y), Vector(p1.x, p1.y));
121 void PaintInterface::drawHandle(const Vector & p, const RS_Color & c, int size)
126 fillRect((int)(p.x - size), (int)(p.y - size), 2 * size, 2 * size, c);
129 void PaintInterface::setPreviewMode()
131 drawingMode = RS2::ModeXOR;
136 bool PaintInterface::isPreviewMode()
138 return (drawingMode == RS2::ModeXOR);
141 void PaintInterface::setOffset(const Vector & o)
146 int PaintInterface::toScreenX(double x)
148 return RS_Math::round(offset.x + x);
151 int PaintInterface::toScreenY(double y)
153 return RS_Math::round(offset.y + y);
157 * Draws a grid point at (x1, y1).
159 void PaintInterface::drawGridPoint(const Vector & p)
161 painter->drawPoint(toScreenX(p.x), toScreenY(p.y));
165 * Draws a point at (x1, y1).
167 void PaintInterface::drawPoint(const Vector & p)
169 painter->drawLine(toScreenX(p.x - 1), toScreenY(p.y), toScreenX(p.x + 1), toScreenY(p.y));
170 painter->drawLine(toScreenX(p.x), toScreenY(p.y - 1), toScreenX(p.x), toScreenY(p.y + 1));
174 * Draws a line from (x1, y1) to (x2, y2).
176 void PaintInterface::drawLine(const Vector & p1, const Vector & p2)
179 int w2 = (int)getPen().getScreenWidth() / 2;
180 QPainter::drawLine(toScreenX(p1.x - w2), toScreenY(p1.y - w2),
181 toScreenX(p2.x - w2), toScreenY(p2.y - w2));
183 painter->drawLine(toScreenX(p1.x), toScreenY(p1.y), toScreenX(p2.x), toScreenY(p2.y));
188 * Draws a rectangle with corners p1, p2.
190 /*void PaintInterface::drawRect(const Vector& p1, const Vector& p2) {
191 / *QPainter::drawRect(toScreenX(p1.x), toScreenY(p1.y),
192 abs(toScreenX(p2.x) - toScreenX(p1.x)),
193 abs(toScreenY(p2.y) - toScreenY(p1.y)));* /
194 QPainter::drawLine(toScreenX(p1.x), toScreenY(p1.y),
195 toScreenX(p2.x), toScreenY(p1.y));
196 QPainter::drawLine(toScreenX(p2.x), toScreenY(p1.y),
197 toScreenX(p2.x), toScreenY(p2.y));
198 QPainter::drawLine(toScreenX(p2.x), toScreenY(p2.y),
199 toScreenX(p1.x), toScreenY(p2.y));
200 QPainter::drawLine(toScreenX(p1.x), toScreenY(p2.y),
201 toScreenX(p1.x), toScreenY(p1.y));
205 * Draws an arc which starts / ends exactly at the given coordinates.
207 * @param cx center in x
208 * @param cy center in y
209 * @param radius Radius
210 * @param a1 Angle 1 in rad
211 * @param a2 Angle 2 in rad
212 * @param x1 startpoint x
213 * @param y1 startpoint y
214 * @param x2 endpoint x
215 * @param y2 endpoint y
216 * @param reversed true: clockwise, false: counterclockwise
218 void PaintInterface::drawArc(const Vector & cp, double radius,
219 double a1, double a2, const Vector & p1, const Vector & p2,
223 QPainter::drawArc(cx - radius, cy - radius, 2 * radius, 2 * radius, a1 * 16, (a2 - a1) * 16);
231 drawArcMac(cp, radius, a1, a2, reversed);
233 int cix; // Next point on circle
235 double aStep; // Angle Step (rad)
236 double a; // Current Angle (rad)
237 double linStep; // linear step (pixels)
239 if (drawingMode == RS2::ModePreview)
244 if (fabs(linStep / radius) <= 1.0)
245 aStep = asin(linStep / radius);
254 // Arc Counterclockwise:
255 if (a1 > a2 - 1.0e-10)
261 poly.setPoint(i++, toScreenX(p1.x), toScreenY(p1.y));
263 for(a=a1+aStep; a<=a2; a+=aStep)
265 cix = toScreenX(cp.x + cos(a) * radius);
266 ciy = toScreenY(cp.y - sin(a) * radius);
269 poly.setPoint(i++, cix, ciy);
272 //lineTo(toScreenX(p2.x), toScreenY(p2.y));
274 poly.setPoint(i++, toScreenX(p2.x), toScreenY(p2.y));
280 if (a1 < a2 + 1.0e-10)
285 poly.setPoint(i++, toScreenX(p1.x), toScreenY(p1.y));
287 for(a=a1-aStep; a>=a2; a-=aStep)
289 cix = toScreenX(cp.x + cos(a) * radius);
290 ciy = toScreenY(cp.y - sin(a) * radius);
291 poly.setPoint(i++, cix, ciy);
294 poly.setPoint(i++, toScreenX(p2.x), toScreenY(p2.y));
304 * @param cx center in x
305 * @param cy center in y
306 * @param radius Radius
307 * @param a1 Angle 1 in rad
308 * @param a2 Angle 2 in rad
309 * @param reversed true: clockwise, false: counterclockwise
311 void PaintInterface::drawArc(const Vector & cp, double radius, double a1, double a2, bool reversed)
318 drawArcMac(cp, radius, a1, a2, reversed);
321 createArc(p, cp, radius, a1, a2, reversed);
323 painter->drawPolyline(p);
329 * Draws an arc on apple.
331 * @param cx center in x
332 * @param cy center in y
333 * @param radius Radius
334 * @param a1 Angle 1 in rad
335 * @param a2 Angle 2 in rad
336 * @param reversed true: clockwise, false: counterclockwise
338 void PaintInterface::drawArcMac(const Vector& cp, double radius, double a1, double a2, bool reversed)
340 RS_DEBUG->print("PaintInterface::drawArcMac");
345 double cix; // Next point on circle
347 double aStep; // Angle Step (rad)
348 double a; // Current Angle (rad)
352 if (2.0 / radius <= 1.0)
353 aStep = asin(2.0 / radius);
360 ox = cp.x + cos(a1) * radius;
361 oy = cp.y - sin(a1) * radius;
365 // Arc Counterclockwise:
366 if (a1 > a2 - 1.0e-10)
369 for(a=a1+aStep; a<=a2; a+=aStep)
371 cix = cp.x + cos(a) * radius;
372 ciy = cp.y - sin(a) * radius;
373 drawLine(Vector(ox, oy), Vector(cix, ciy));
381 if (a1 < a2 + 1.0e-10)
384 for(a=a1-aStep; a>=a2; a-=aStep)
386 cix = cp.x + cos(a) * radius;
387 ciy = cp.y - sin(a) * radius;
388 drawLine(Vector(ox, oy), Vector(cix, ciy));
394 drawLine(Vector(ox, oy), Vector(cp.x + cos(a2) * radius, cp.y - sin(a2) * radius));
400 * @param cx center in x
401 * @param cy center in y
402 * @param radius Radius
404 void PaintInterface::drawCircle(const Vector & cp, double radius)
406 //This old code was causing the Qt painter code to segfault...
407 //If we have problems with arcs, then drawArc() is probably the culprit.
409 if (drawingMode == RS2::ModeXOR && radius < 500)
411 // This is _very_ slow for large arcs:
412 painter->drawEllipse(toScreenX(cp.x - radius), toScreenY(cp.y - radius), RS_Math::round(2.0 * radius), RS_Math::round(2.0 * radius));
417 drawArcMac(cp, radius, 0.0, 2 * M_PI, false);
419 drawArc(cp, radius, 0.0, 2 * M_PI, cp + Vector(radius, 0.0), cp + Vector(radius, 0.0), false);
423 // painter->drawEllipse(toScreenX(cp.x - radius), toScreenY(cp.y - radius), RS_Math::round(2.0 * radius), RS_Math::round(2.0 * radius));
424 painter->drawEllipse(QPointF(cp.x, cp.y), radius, radius);
429 * Draws a rotated ellipse arc.
431 void PaintInterface::drawEllipse(const Vector & cp, double radius1, double radius2,
432 double angle, double a1, double a2, bool reversed)
434 double aStep; // Angle Step (rad)
435 double a; // Current Angle (rad)
441 Vector vc(cp.x, cp.y);
442 vp.set(cp.x + cos(a1) * radius1, cp.y - sin(a1) * radius2);
443 vp.rotate(vc, -angle);
444 // moveTo(toScreenX(vp.x), toScreenY(vp.y));
448 // Arc Counterclockwise:
449 if (a1 > a2 - RS_TOLERANCE)
452 for(a=a1+aStep; a<=a2; a+=aStep)
455 vp.set(cp.x + cos(a) * radius1, cp.y - sin(a) * radius2);
456 vp.rotate(vc, -angle);
457 // lineTo(toScreenX(vp.x), toScreenY(vp.y));
458 drawLine(Vector(toScreenX(last.x), toScreenY(last.y)),
459 Vector(toScreenX(vp.x), toScreenY(vp.y)));
465 if (a1 < a2 + RS_TOLERANCE)
468 for(a=a1-aStep; a>=a2; a-=aStep)
471 vp.set(cp.x + cos(a) * radius1, cp.y - sin(a) * radius2);
472 vp.rotate(vc, -angle);
473 // lineTo(toScreenX(vp.x), toScreenY(vp.y));
474 drawLine(Vector(toScreenX(last.x), toScreenY(last.y)),
475 Vector(toScreenX(vp.x), toScreenY(vp.y)));
480 vp.set(cp.x + cos(a2) * radius1, cp.y - sin(a2) * radius2);
481 vp.rotate(vc, -angle);
482 // lineTo(toScreenX(vp.x), toScreenY(vp.y));
483 drawLine(Vector(toScreenX(last.x), toScreenY(last.y)),
484 Vector(toScreenX(vp.x), toScreenY(vp.y)));
491 void PaintInterface::drawImg(QImage & img, const Vector & pos,
492 double angle, const Vector & factor, int sx, int sy, int sw, int sh)
497 wm.translate(pos.x, pos.y);
498 wm.scale(factor.x, factor.y);
499 wm.rotate(RS_Math::rad2deg(-angle));
500 painter->setWorldMatrix(wm);
502 if (fabs(angle) < 1.0e-4)
503 painter->drawImage(0 + sx, -img.height() + sy, img, sx, sy, sw, sh);
505 painter->drawImage(0, -img.height(), img);
510 void PaintInterface::fillRect(int x1, int y1, int w, int h, const RS_Color & col)
512 painter->fillRect(x1, y1, w, h, col);
515 void PaintInterface::fillTriangle(const Vector & p1, const Vector & p2, const Vector & p3)
518 poly.putPoints(0, 3, toScreenX(p1.x),toScreenY(p1.y), toScreenX(p2.x),toScreenY(p2.y), toScreenX(p3.x),toScreenY(p3.y));
519 setBrush(painter->pen().color());
523 void PaintInterface::erase()
525 painter->eraseRect(0, 0, getWidth(), getHeight());
528 int PaintInterface::getWidth()
530 return painter->device()->width();
533 int PaintInterface::getHeight()
535 return painter->device()->height();
538 void PaintInterface::setPreviewPen()
540 setPen(RS_Color(0, 255, 255));
543 RS_Pen PaintInterface::getPen()
548 void PaintInterface::setPen(const RS_Pen & pen)
552 if (drawingMode == RS2::ModeBW)
553 lpen.setColor(RS_Color(0, 0, 0));
555 QPen p(lpen.getColor(), RS_Math::round(lpen.getScreenWidth()), RS2::rsToQtLineType(lpen.getLineType()));
556 p.setJoinStyle(Qt::RoundJoin);
557 p.setCapStyle(Qt::RoundCap);
561 void PaintInterface::setPen(const RS_Color & color)
563 if (drawingMode == RS2::ModeBW)
565 lpen.setColor(RS_Color(0, 0, 0));
566 painter->setPen(RS_Color(0, 0, 0));
570 lpen.setColor(color);
571 painter->setPen(color);
575 void PaintInterface::setPen(int r, int g, int b)
577 if (drawingMode == RS2::ModeBW)
578 setPen(QColor(0, 0, 0));
580 setPen(QColor(r, g, b));
583 void PaintInterface::disablePen()
585 lpen = RS_Pen(RS2::FlagInvalid);
586 painter->setPen(Qt::NoPen);
589 void PaintInterface::setBrush(const RS_Color & color)
591 if (drawingMode == RS2::ModeBW)
592 painter->setBrush(QColor(0, 0, 0));
594 painter->setBrush(color);
597 void PaintInterface::drawPolygon(const QPolygon & a)
599 painter->drawPolygon(a);
602 void PaintInterface::setXORMode()
604 painter->setCompositionMode(QPainter::CompositionMode_Xor);
607 void PaintInterface::setNormalMode()
609 painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
612 void PaintInterface::setClipRect(int x, int y, int w, int h)
614 painter->setClipRect(x, y, w, h);
615 painter->setClipping(true);
618 void PaintInterface::resetClipping()
620 painter->setClipping(false);