+++ /dev/null
-// paintinterface.cpp
-//
-// Part of the Architektonas Project
-// Extensively rewritten and refactored by James L. Hammons
-// Portions copyright (C) 2001-2003 RibbonSoft
-// Copyright (C) 2010 Underground Software
-// See the README and GPLv2 files for licensing and warranty information
-//
-// JLH = James L. Hammons <jlhamm@acm.org>
-//
-// Who When What
-// --- ---------- -----------------------------------------------------------
-// JLH 05/21/2010 Created this file. :-)
-//
-
-// BIG NOTE: THIS CLASS ASSUMES THAT THE PAINTER * IS VALID!!! BAD THINGS WILL
-// HAPPEN IF IT IS NOT!!!
-
-#include "paintinterface.h"
-
-#include "debug.h"
-#include "mathextra.h"
-
-PaintInterface::PaintInterface(QPainter * p): painter(p), drawingMode(RS2::ModeFull),
- offset(Vector(0.0, 0.0))
-{
-}
-
-/**
-* Sets the drawing mode.
-*/
-void PaintInterface::setDrawingMode(RS2::DrawingMode m)
-{
- drawingMode = m;
-}
-
-/**
-* @return Current drawing mode.
-*/
-RS2::DrawingMode PaintInterface::getDrawingMode()
-{
- return drawingMode;
-}
-
-void PaintInterface::createArc(QPolygon & pa, const Vector& cp, double radius,
- double a1, double a2, bool reversed)
-{
- if (radius < 1.0e-6)
- {
- DEBUG->print(Debug::D_WARNING, "PaintInterface::createArc: invalid radius: %f", radius);
- return;
- }
-
- int cix; // Next point on circle
- int ciy; //
- double aStep; // Angle Step (rad)
- double a; // Current Angle (rad)
-
- if (fabs(2.0 / radius) <= 1.0)
- aStep = asin(2.0 / radius);
- else
- aStep = 1.0;
-
- //if (aStep<0.05) {
- // aStep = 0.05;
- //}
-
- // less than a pixel long lines:
- //if (radius*aStep<1.0) {
- // aStep =
- //}
-
- //QPointArray pa;
- int i = 0;
- pa.resize(i + 1);
- pa.setPoint(i++, toScreenX(cp.x + cos(a1) * radius), toScreenY(cp.y - sin(a1) * radius));
- //moveTo(toScreenX(cp.x+cos(a1)*radius),
- // toScreenY(cp.y-sin(a1)*radius));
-
- if (!reversed)
- {
- // Arc Counterclockwise:
- if (a1 > a2 - 1.0e-10)
- a2 += 2 * M_PI;
-
- for(a=a1+aStep; a<=a2; a+=aStep)
- {
- cix = toScreenX(cp.x + cos(a) * radius);
- ciy = toScreenY(cp.y - sin(a) * radius);
- pa.resize(i + 1);
- pa.setPoint(i++, cix, ciy);
- }
- }
- else
- {
- // Arc Clockwise:
- if (a1 < a2 + 1.0e-10)
- a2 -= 2 * M_PI;
-
- for(a=a1-aStep; a>=a2; a-=aStep)
- {
- cix = toScreenX(cp.x + cos(a) * radius);
- ciy = toScreenY(cp.y - sin(a) * radius);
- //lineTo(cix, ciy);
- pa.resize(i + 1);
- pa.setPoint(i++, cix, ciy);
- }
- }
-
- pa.resize(i + 1);
- pa.setPoint(i++, toScreenX(cp.x + cos(a2) * radius), toScreenY(cp.y - sin(a2) * radius));
- //drawPolyline(pa);
-}
-
-void PaintInterface::drawRect(const Vector & p1, const Vector & p2)
-{
- drawLine(Vector(p1.x, p1.y), Vector(p2.x, p1.y));
- drawLine(Vector(p2.x, p1.y), Vector(p2.x, p2.y));
- drawLine(Vector(p2.x, p2.y), Vector(p1.x, p2.y));
- drawLine(Vector(p1.x, p2.y), Vector(p1.x, p1.y));
-}
-
-void PaintInterface::drawHandle(const Vector & p, const Color & c, int size)
-{
- if (size < 0)
- size = 2;
-
- fillRect((int)(p.x - size), (int)(p.y - size), 2 * size, 2 * size, c);
-}
-
-void PaintInterface::setPreviewMode()
-{
- drawingMode = RS2::ModeXOR;
- setXORMode();
- setPreviewPen();
-}
-
-bool PaintInterface::isPreviewMode()
-{
- return (drawingMode == RS2::ModeXOR);
-}
-
-void PaintInterface::setOffset(const Vector & o)
-{
- offset = o;
-}
-
-int PaintInterface::toScreenX(double x)
-{
- return Math::round(offset.x + x);
-}
-
-int PaintInterface::toScreenY(double y)
-{
- return Math::round(offset.y + y);
-}
-
-/**
- * Draws a grid point at (x1, y1).
- */
-void PaintInterface::drawGridPoint(const Vector & p)
-{
- painter->drawPoint(toScreenX(p.x), toScreenY(p.y));
-}
-
-/**
- * Draws a point at (x1, y1).
- */
-void PaintInterface::drawPoint(const Vector & p)
-{
- painter->drawLine(toScreenX(p.x - 1), toScreenY(p.y), toScreenX(p.x + 1), toScreenY(p.y));
- painter->drawLine(toScreenX(p.x), toScreenY(p.y - 1), toScreenX(p.x), toScreenY(p.y + 1));
-}
-
-/**
- * Draws a line from (x1, y1) to (x2, y2).
- */
-void PaintInterface::drawLine(const Vector & p1, const Vector & p2)
-{
-#ifdef __APPLE__
- int w2 = (int)getPen().getScreenWidth() / 2;
- QPainter::drawLine(toScreenX(p1.x - w2), toScreenY(p1.y - w2),
- toScreenX(p2.x - w2), toScreenY(p2.y - w2));
-#else
- painter->drawLine(toScreenX(p1.x), toScreenY(p1.y), toScreenX(p2.x), toScreenY(p2.y));
-#endif
-}
-
-/**
- * Draws a rectangle with corners p1, p2.
- */
-/*void PaintInterface::drawRect(const Vector& p1, const Vector& p2) {
- / *QPainter::drawRect(toScreenX(p1.x), toScreenY(p1.y),
- abs(toScreenX(p2.x) - toScreenX(p1.x)),
- abs(toScreenY(p2.y) - toScreenY(p1.y)));* /
- QPainter::drawLine(toScreenX(p1.x), toScreenY(p1.y),
- toScreenX(p2.x), toScreenY(p1.y));
- QPainter::drawLine(toScreenX(p2.x), toScreenY(p1.y),
- toScreenX(p2.x), toScreenY(p2.y));
- QPainter::drawLine(toScreenX(p2.x), toScreenY(p2.y),
- toScreenX(p1.x), toScreenY(p2.y));
- QPainter::drawLine(toScreenX(p1.x), toScreenY(p2.y),
- toScreenX(p1.x), toScreenY(p1.y));
-}*/
-
-/**
- * Draws an arc which starts / ends exactly at the given coordinates.
- *
- * @param cx center in x
- * @param cy center in y
- * @param radius Radius
- * @param a1 Angle 1 in rad
- * @param a2 Angle 2 in rad
- * @param x1 startpoint x
- * @param y1 startpoint y
- * @param x2 endpoint x
- * @param y2 endpoint y
- * @param reversed true: clockwise, false: counterclockwise
- */
-void PaintInterface::drawArc(const Vector & cp, double radius,
- double a1, double a2, const Vector & p1, const Vector & p2,
- bool reversed)
-{
- /*
- QPainter::drawArc(cx - radius, cy - radius, 2 * radius, 2 * radius, a1 * 16, (a2 - a1) * 16);
- */
-
- if (radius <= 0.5)
- drawGridPoint(cp);
- else
- {
-#ifdef __APPLE__
- drawArcMac(cp, radius, a1, a2, reversed);
-#else
- int cix; // Next point on circle
- int ciy; //
- double aStep; // Angle Step (rad)
- double a; // Current Angle (rad)
- double linStep; // linear step (pixels)
-
- if (drawingMode == RS2::ModePreview)
- linStep = 20.0;
- else
- linStep = 6.0;
-
- if (fabs(linStep / radius) <= 1.0)
- aStep = asin(linStep / radius);
- else
- aStep = 1.0;
-
- if (aStep < 0.05)
- aStep = 0.05;
-
- if (!reversed)
- {
- // Arc Counterclockwise:
- if (a1 > a2 - 1.0e-10)
- a2 += 2 * M_PI;
-
- QPolygon poly;
- int i = 0;
-// pa.resize(i + 1);
- poly.setPoint(i++, toScreenX(p1.x), toScreenY(p1.y));
-
- for(a=a1+aStep; a<=a2; a+=aStep)
- {
- cix = toScreenX(cp.x + cos(a) * radius);
- ciy = toScreenY(cp.y - sin(a) * radius);
- //lineTo(cix, ciy);
-// pa.resize(i + 1);
- poly.setPoint(i++, cix, ciy);
- }
-
- //lineTo(toScreenX(p2.x), toScreenY(p2.y));
-// pa.resize(i + 1);
- poly.setPoint(i++, toScreenX(p2.x), toScreenY(p2.y));
- drawPolygon(poly);
- }
- else
- {
- // Arc Clockwise:
- if (a1 < a2 + 1.0e-10)
- a2 -= 2 * M_PI;
-
- QPolygon poly;
- int i = 0;
- poly.setPoint(i++, toScreenX(p1.x), toScreenY(p1.y));
-
- for(a=a1-aStep; a>=a2; a-=aStep)
- {
- cix = toScreenX(cp.x + cos(a) * radius);
- ciy = toScreenY(cp.y - sin(a) * radius);
- poly.setPoint(i++, cix, ciy);
- }
-
- poly.setPoint(i++, toScreenX(p2.x), toScreenY(p2.y));
- drawPolygon(poly);
- }
-#endif
- }
-}
-
-/**
- * Draws an arc.
- *
- * @param cx center in x
- * @param cy center in y
- * @param radius Radius
- * @param a1 Angle 1 in rad
- * @param a2 Angle 2 in rad
- * @param reversed true: clockwise, false: counterclockwise
- */
-void PaintInterface::drawArc(const Vector & cp, double radius, double a1, double a2, bool reversed)
-{
- if (radius <= 0.5)
- drawGridPoint(cp);
- else
- {
-#ifdef __APPLE__
- drawArcMac(cp, radius, a1, a2, reversed);
-#else
- QPolygon p;
- createArc(p, cp, radius, a1, a2, reversed);
-// drawPolygon(p);
- painter->drawPolyline(p);
-#endif
- }
-}
-
-/**
- * Draws an arc on apple.
- *
- * @param cx center in x
- * @param cy center in y
- * @param radius Radius
- * @param a1 Angle 1 in rad
- * @param a2 Angle 2 in rad
- * @param reversed true: clockwise, false: counterclockwise
- */
-void PaintInterface::drawArcMac(const Vector& cp, double radius, double a1, double a2, bool reversed)
-{
- DEBUG->print("PaintInterface::drawArcMac");
- if (radius <= 0.5)
- drawGridPoint(cp);
- else
- {
- double cix; // Next point on circle
- double ciy; //
- double aStep; // Angle Step (rad)
- double a; // Current Angle (rad)
- double ox;
- double oy;
-
- if (2.0 / radius <= 1.0)
- aStep = asin(2.0 / radius);
- else
- aStep = 1.0;
-
- if (aStep < 0.05)
- aStep = 0.05;
-
- ox = cp.x + cos(a1) * radius;
- oy = cp.y - sin(a1) * radius;
-
- if (!reversed)
- {
- // Arc Counterclockwise:
- if (a1 > a2 - 1.0e-10)
- a2 += 2 * M_PI;
-
- for(a=a1+aStep; a<=a2; a+=aStep)
- {
- cix = cp.x + cos(a) * radius;
- ciy = cp.y - sin(a) * radius;
- drawLine(Vector(ox, oy), Vector(cix, ciy));
- ox = cix;
- oy = ciy;
- }
- }
- else
- {
- // Arc Clockwise:
- if (a1 < a2 + 1.0e-10)
- a2 -= 2 * M_PI;
-
- for(a=a1-aStep; a>=a2; a-=aStep)
- {
- cix = cp.x + cos(a) * radius;
- ciy = cp.y - sin(a) * radius;
- drawLine(Vector(ox, oy), Vector(cix, ciy));
- ox = cix;
- oy = ciy;
- }
- }
-
- drawLine(Vector(ox, oy), Vector(cp.x + cos(a2) * radius, cp.y - sin(a2) * radius));
- }
-}
-
-/**
- * Draws a circle.
- * @param cx center in x
- * @param cy center in y
- * @param radius Radius
- */
-void PaintInterface::drawCircle(const Vector & cp, double radius)
-{
-//This old code was causing the Qt painter code to segfault...
-//If we have problems with arcs, then drawArc() is probably the culprit.
-#if 0
- if (drawingMode == RS2::ModeXOR && radius < 500)
- {
- // This is _very_ slow for large arcs:
- painter->drawEllipse(toScreenX(cp.x - radius), toScreenY(cp.y - radius), Math::round(2.0 * radius), Math::round(2.0 * radius));
- }
- else
- {
-#ifdef __APPLE__
- drawArcMac(cp, radius, 0.0, 2 * M_PI, false);
-#else
- drawArc(cp, radius, 0.0, 2 * M_PI, cp + Vector(radius, 0.0), cp + Vector(radius, 0.0), false);
-#endif
- }
-#else
-// painter->drawEllipse(toScreenX(cp.x - radius), toScreenY(cp.y - radius), Math::round(2.0 * radius), Math::round(2.0 * radius));
- painter->drawEllipse(QPointF(cp.x, cp.y), radius, radius);
-#endif
-}
-
-/**
- * Draws a rotated ellipse arc.
- */
-void PaintInterface::drawEllipse(const Vector & cp, double radius1, double radius2,
- double angle, double a1, double a2, bool reversed)
-{
- double aStep; // Angle Step (rad)
- double a; // Current Angle (rad)
-
- // Angle step in rad
- aStep = 0.01;
-
- Vector vp;
- Vector vc(cp.x, cp.y);
- vp.set(cp.x + cos(a1) * radius1, cp.y - sin(a1) * radius2);
- vp.rotate(vc, -angle);
-// moveTo(toScreenX(vp.x), toScreenY(vp.y));
-
- if (!reversed)
- {
- // Arc Counterclockwise:
- if (a1 > a2 - RS_TOLERANCE)
- a2 += 2 * M_PI;
-
- for(a=a1+aStep; a<=a2; a+=aStep)
- {
- Vector last = vp;
- vp.set(cp.x + cos(a) * radius1, cp.y - sin(a) * radius2);
- vp.rotate(vc, -angle);
-// lineTo(toScreenX(vp.x), toScreenY(vp.y));
- drawLine(Vector(toScreenX(last.x), toScreenY(last.y)),
- Vector(toScreenX(vp.x), toScreenY(vp.y)));
- }
- }
- else
- {
- // Arc Clockwise:
- if (a1 < a2 + RS_TOLERANCE)
- a2 -= 2 * M_PI;
-
- for(a=a1-aStep; a>=a2; a-=aStep)
- {
- Vector last = vp;
- vp.set(cp.x + cos(a) * radius1, cp.y - sin(a) * radius2);
- vp.rotate(vc, -angle);
-// lineTo(toScreenX(vp.x), toScreenY(vp.y));
- drawLine(Vector(toScreenX(last.x), toScreenY(last.y)),
- Vector(toScreenX(vp.x), toScreenY(vp.y)));
- }
- }
-
- Vector last = vp;
- vp.set(cp.x + cos(a2) * radius1, cp.y - sin(a2) * radius2);
- vp.rotate(vc, -angle);
-// lineTo(toScreenX(vp.x), toScreenY(vp.y));
- drawLine(Vector(toScreenX(last.x), toScreenY(last.y)),
- Vector(toScreenX(vp.x), toScreenY(vp.y)));
- //}
-}
-
-/**
- * Draws image.
- */
-void PaintInterface::drawImg(QImage & img, const Vector & pos,
- double angle, const Vector & factor, int sx, int sy, int sw, int sh)
-{
- painter->save();
-
- QMatrix wm;
- wm.translate(pos.x, pos.y);
- wm.scale(factor.x, factor.y);
- wm.rotate(Math::rad2deg(-angle));
- painter->setWorldMatrix(wm);
-
- if (fabs(angle) < 1.0e-4)
- painter->drawImage(0 + sx, -img.height() + sy, img, sx, sy, sw, sh);
- else
- painter->drawImage(0, -img.height(), img);
-
- painter->restore();
-}
-
-void PaintInterface::fillRect(int x1, int y1, int w, int h, const Color & col)
-{
- painter->fillRect(x1, y1, w, h, col);
-}
-
-void PaintInterface::fillTriangle(const Vector & p1, const Vector & p2, const Vector & p3)
-{
- QPolygon poly(3);
- poly.putPoints(0, 3, toScreenX(p1.x),toScreenY(p1.y), toScreenX(p2.x),toScreenY(p2.y), toScreenX(p3.x),toScreenY(p3.y));
- setBrush(painter->pen().color());
- drawPolygon(poly);
-}
-
-void PaintInterface::erase()
-{
- painter->eraseRect(0, 0, getWidth(), getHeight());
-}
-
-int PaintInterface::getWidth()
-{
- return painter->device()->width();
-}
-
-int PaintInterface::getHeight()
-{
- return painter->device()->height();
-}
-
-void PaintInterface::setPreviewPen()
-{
- setPen(Color(0, 255, 255));
-}
-
-Pen PaintInterface::getPen()
-{
- return lpen;
-}
-
-void PaintInterface::setPen(const Pen & pen)
-{
- lpen = pen;
-
- if (drawingMode == RS2::ModeBW)
- lpen.setColor(Color(0, 0, 0));
-
- QPen p(lpen.getColor(), Math::round(lpen.getScreenWidth()), RS2::rsToQtLineType(lpen.getLineType()));
- p.setJoinStyle(Qt::RoundJoin);
- p.setCapStyle(Qt::RoundCap);
- painter->setPen(p);
-}
-
-void PaintInterface::setPen(const Color & color)
-{
- if (drawingMode == RS2::ModeBW)
- {
- lpen.setColor(Color(0, 0, 0));
- painter->setPen(Color(0, 0, 0));
- }
- else
- {
- lpen.setColor(color);
- painter->setPen(color);
- }
-}
-
-void PaintInterface::setPen(int r, int g, int b)
-{
- if (drawingMode == RS2::ModeBW)
- setPen(QColor(0, 0, 0));
- else
- setPen(QColor(r, g, b));
-}
-
-void PaintInterface::disablePen(void)
-{
- lpen = Pen(RS2::FlagInvalid);
- painter->setPen(Qt::NoPen);
-}
-
-void PaintInterface::setBrush(const Color & color)
-{
- if (drawingMode == RS2::ModeBW)
- painter->setBrush(QColor(0, 0, 0));
- else
- painter->setBrush(color);
-}
-
-void PaintInterface::disableBrush(void)
-{
-// lpen = Pen(RS2::FlagInvalid);
- painter->setBrush(Qt::NoBrush);
-}
-
-void PaintInterface::drawPolygon(const QPolygon & a)
-{
- painter->drawPolygon(a);
-}
-
-void PaintInterface::setXORMode(void)
-{
- painter->setCompositionMode(QPainter::CompositionMode_Xor);
-}
-
-void PaintInterface::setNormalMode(void)
-{
- painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
-}
-
-void PaintInterface::setClipRect(int x, int y, int w, int h)
-{
- painter->setClipRect(x, y, w, h);
- painter->setClipping(true);
-}
-
-void PaintInterface::resetClipping()
-{
- painter->setClipping(false);
-}