Debian Codegraph @

PathPaintEngine.cpp

raw symbols SCIP
1 /***************************************************************************
2 * This file is part of Robocut. *
3 * Copyright (C) 2010 Tim Hutt <[email protected]> *
4 * Copyright (C) 2010 Markus Schulz <[email protected]> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
20 ***************************************************************************/
21
22 #include "PathPaintEngine.h"
23
24 #include <cmath>
25 #include <QDebug>
26
27 PathPaintEngine::PathPaintEngine(QPaintDevice* pdev)
28 : QPaintEngine(AllFeatures)
29 {
30 dev = dynamic_cast<PathPaintDevice*>(pdev);
31 if (!dev)
32 qWarning("PathPaintEngine: unsupported target device.");
33 }
34
35 bool PathPaintEngine::begin(QPaintDevice* pdev)
36 {
37 dev = dynamic_cast<PathPaintDevice*>(pdev);
38 if (!dev)
39 qWarning("PathPaintEngine: unsupported target device.");
40
41 // TODO: setActive?
42 return true;
43 }
44
45 bool approximatelyEqual(qreal a, qreal b, qreal epsilon)
46 {
47 return fabs(a - b) <= ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
48 }
49
50 bool essentiallyEqual(qreal a, qreal b, qreal epsilon)
51 {
52 return fabs(a - b) <= ( (fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * epsilon);
53 }
54
55 bool definitelyGreaterThan(qreal a, qreal b, qreal epsilon)
56 {
57 return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
58 }
59
60 bool definitelyLessThan(qreal a, qreal b, qreal epsilon)
61 {
62 return (b - a) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
63 }
64
65
66 void PathPaintEngine::drawPath(const QPainterPath& path)
67 {
68 if (!dev)
69 return;
70
71 if(!isCosmetic)
72 {
73 QList<QPolygonF> polys = path.toSubpathPolygons();
74 for (int i = 0; i < polys.size(); ++i)
75 {
76 if(dashPattern.empty()) dev->addPath(transform.map(polys[i]));
77 else
78 {
79 QPolygonF polytemp = transform.map(polys[i]), newpoly;
80 int dashtoggle = 1, dashi=0, j = 0;
81 qreal actualdashsize = dashPattern[dashi];
82 QPointF origin = QPointF(polytemp[j]), testp;
83 j++;
84 do
85 {
86 newpoly = QPolygonF();
87 newpoly.append(origin);
88 do
89 {
90 testp = polytemp[j];
91 origin = QPointF(getPointAtLenght(QPointF(origin), polytemp[j], actualdashsize));
92 if (essentiallyEqual(origin.x(), polytemp[j].x(), 0.01 ) && approximatelyEqual(origin.y(), polytemp[j].y(),0.01) && j+1 < polytemp.size())
93 {
94 origin = polytemp[j];
95 int tempz = polytemp.size();
96 j++;
97 testp = polytemp[j];
98 }
99 newpoly.append(origin);
100
101 }while(definitelyGreaterThan(actualdashsize,0.0,0.1) && testp!=origin);
102 if(dashtoggle == 1)
103 {
104 dev->addPath(newpoly);
105 }
106 dashtoggle = dashtoggle * -1;
107 dashi++;
108 if(dashi >= dashPattern.size()) dashi=0;
109 actualdashsize = dashPattern[dashi];
110 }while(!essentiallyEqual(origin.x(), polytemp[j].x(), 0.001 ) || !essentiallyEqual(origin.y(), polytemp[j].y(),0.001));
111 }
112 }
113 }
114 }
115
116 void PathPaintEngine::drawPixmap(const QRectF& r, const QPixmap& pm, const QRectF& sr)
117 {
118 // Nop.
119 }
120
121 void PathPaintEngine::drawPolygon(const QPointF* points, int pointCount, PolygonDrawMode mode)
122 {
123 if (!dev)
124 return;
125
126 QPolygonF p;
127 for (int i = 0; i < pointCount; ++i)
128 p.append(points[i]);
129 dev->addPath(transform.map(p));
130 }
131
132 bool PathPaintEngine::end()
133 {
134 if (!dev)
135 return false;
136 return true;
137 }
138
139 QPaintEngine::Type PathPaintEngine::type() const
140 {
141 return QPaintEngine::User;
142 }
143 void PathPaintEngine::updateState(const QPaintEngineState& state)
144 {
145 if (state.state() & DirtyTransform)
146 transform = state.transform();
147 dashPattern = state.pen().dashPattern();
148 isCosmetic = state.pen().isCosmetic();
149 }
150
151 qreal PathPaintEngine::getDistance(const QPointF &p1, const QPointF &p2)
152 {
153 qreal testx1 = p1.x();
154 qreal testy1 = p1.y();
155 qreal testx2 = p2.x();
156 qreal testy2 = p2.y();
157 qreal a = 0.0;
158 qreal b = 0.0;
159 double c = 0.0;
160
161 if(testx1 >= testx2) a = testx1 - testx2;
162 else a = testx2 - testx1;
163 if(testy1 >= testy2) b = testy1 - testy2;
164 else b = testy2 - testy1;
165 c = sqrt((double)(a*a+b*b));
166 return (qreal) c;
167 }
168
169 QPointF PathPaintEngine::getPointAtLenght(const QPointF &p1, const QPointF &p2, qreal &l1)
170 {
171 qreal testx1 = p1.x();
172 qreal testy1 = p1.y();
173 qreal testx2 = p2.x();
174 qreal testy2 = p2.y();
175 qreal lenghtp1p2 = getDistance(p1,p2);
176 qreal lenghtdash = l1;
177 l1 = lenghtp1p2 - lenghtdash;
178 if (definitelyLessThan(lenghtp1p2, lenghtdash, 0.01))
179 {
180 l1 = lenghtdash - lenghtp1p2;
181 return p2;
182 }
183 if (definitelyGreaterThan(lenghtdash, lenghtp1p2, 0.01))
184 {
185 l1 = lenghtdash - lenghtp1p2;
186 return p1;
187 }
188 l1 = 0;
189 qreal factor = lenghtp1p2/lenghtdash;
190 qreal a = testx1-testx2;
191 qreal b = testy1-testy2;
192 qreal aa = a/factor;
193 qreal bb = b/factor;
194 QPointF ret = QPointF(testx1-aa,testy1-bb);
195 return ret;
196
197 }
198