qmatrix.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/painting/qmatrix.cpp
Switch to Source codePreprocessed file
LineSourceCount
1-
2-
3-
4-
5QMatrix::QMatrix()-
6 : _m11(1.)-
7 , _m12(0.)-
8 , _m21(0.)-
9 , _m22(1.)-
10 , _dx(0.)-
11 , _dy(0.)-
12{-
13}-
14QMatrix::QMatrix(qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy)-
15 : _m11(m11)-
16 , _m12(m12)-
17 , _m21(m21)-
18 , _m22(m22)-
19 , _dx(dx)-
20 , _dy(dy)-
21{-
22}-
23-
24-
25-
26-
27-
28QMatrix::QMatrix(const QMatrix &matrix) noexcept-
29 : _m11(matrix._m11)-
30 , _m12(matrix._m12)-
31 , _m21(matrix._m21)-
32 , _m22(matrix._m22)-
33 , _dx(matrix._dx)-
34 , _dy(matrix._dy)-
35{-
36}
never executed: end of block
0
37void QMatrix::setMatrix(qreal m11, qreal m12, qreal m21, qreal m22, qreal dx, qreal dy)-
38{-
39 _m11 = m11;-
40 _m12 = m12;-
41 _m21 = m21;-
42 _m22 = m22;-
43 _dx = dx;-
44 _dy = dy;-
45}-
46void QMatrix::map(qreal x, qreal y, qreal *tx, qreal *ty) const-
47{-
48 { qreal fx = x; qreal fy = y; *tx = _m11*fx + _m21*fy + _dx; *ty = _m12*fx + _m22*fy + _dy; };-
49}-
50void QMatrix::map(int x, int y, int *tx, int *ty) const-
51{-
52 { qreal fx = x; qreal fy = y; *tx = qRound(_m11*fx + _m21*fy + _dx); *ty = qRound(_m12*fx + _m22*fy + _dy); };-
53}-
54-
55QRect QMatrix::mapRect(const QRect &rect) const-
56{-
57 QRect result;-
58 if (_m12 == 0.0F && _m21 == 0.0F) {-
59 int x = qRound(_m11*rect.x() + _dx);-
60 int y = qRound(_m22*rect.y() + _dy);-
61 int w = qRound(_m11*rect.width());-
62 int h = qRound(_m22*rect.height());-
63 if (w < 0) {-
64 w = -w;-
65 x -= w;-
66 }-
67 if (h < 0) {-
68 h = -h;-
69 y -= h;-
70 }-
71 result = QRect(x, y, w, h);-
72 } else {-
73-
74 qreal x0, y0;-
75 qreal x, y;-
76 { qreal fx = rect.left(); qreal fy = rect.top(); x0 = _m11*fx + _m21*fy + _dx; y0 = _m12*fx + _m22*fy + _dy; };-
77 qreal xmin = x0;-
78 qreal ymin = y0;-
79 qreal xmax = x0;-
80 qreal ymax = y0;-
81 { qreal fx = rect.right() + 1; qreal fy = rect.top(); x = _m11*fx + _m21*fy + _dx; y = _m12*fx + _m22*fy + _dy; };-
82 xmin = qMin(xmin, x);-
83 ymin = qMin(ymin, y);-
84 xmax = qMax(xmax, x);-
85 ymax = qMax(ymax, y);-
86 { qreal fx = rect.right() + 1; qreal fy = rect.bottom() + 1; x = _m11*fx + _m21*fy + _dx; y = _m12*fx + _m22*fy + _dy; };-
87 xmin = qMin(xmin, x);-
88 ymin = qMin(ymin, y);-
89 xmax = qMax(xmax, x);-
90 ymax = qMax(ymax, y);-
91 { qreal fx = rect.left(); qreal fy = rect.bottom() + 1; x = _m11*fx + _m21*fy + _dx; y = _m12*fx + _m22*fy + _dy; };-
92 xmin = qMin(xmin, x);-
93 ymin = qMin(ymin, y);-
94 xmax = qMax(xmax, x);-
95 ymax = qMax(ymax, y);-
96 result = QRect(qRound(xmin), qRound(ymin), qRound(xmax)-qRound(xmin), qRound(ymax)-qRound(ymin));-
97 }-
98 return result;-
99}-
100QRectF QMatrix::mapRect(const QRectF &rect) const-
101{-
102 QRectF result;-
103 if (_m12 == 0.0F && _m21 == 0.0F) {-
104 qreal x = _m11*rect.x() + _dx;-
105 qreal y = _m22*rect.y() + _dy;-
106 qreal w = _m11*rect.width();-
107 qreal h = _m22*rect.height();-
108 if (w < 0) {-
109 w = -w;-
110 x -= w;-
111 }-
112 if (h < 0) {-
113 h = -h;-
114 y -= h;-
115 }-
116 result = QRectF(x, y, w, h);-
117 } else {-
118 qreal x0, y0;-
119 qreal x, y;-
120 { qreal fx = rect.x(); qreal fy = rect.y(); x0 = _m11*fx + _m21*fy + _dx; y0 = _m12*fx + _m22*fy + _dy; };-
121 qreal xmin = x0;-
122 qreal ymin = y0;-
123 qreal xmax = x0;-
124 qreal ymax = y0;-
125 { qreal fx = rect.x() + rect.width(); qreal fy = rect.y(); x = _m11*fx + _m21*fy + _dx; y = _m12*fx + _m22*fy + _dy; };-
126 xmin = qMin(xmin, x);-
127 ymin = qMin(ymin, y);-
128 xmax = qMax(xmax, x);-
129 ymax = qMax(ymax, y);-
130 { qreal fx = rect.x() + rect.width(); qreal fy = rect.y() + rect.height(); x = _m11*fx + _m21*fy + _dx; y = _m12*fx + _m22*fy + _dy; };-
131 xmin = qMin(xmin, x);-
132 ymin = qMin(ymin, y);-
133 xmax = qMax(xmax, x);-
134 ymax = qMax(ymax, y);-
135 { qreal fx = rect.x(); qreal fy = rect.y() + rect.height(); x = _m11*fx + _m21*fy + _dx; y = _m12*fx + _m22*fy + _dy; };-
136 xmin = qMin(xmin, x);-
137 ymin = qMin(ymin, y);-
138 xmax = qMax(xmax, x);-
139 ymax = qMax(ymax, y);-
140 result = QRectF(xmin, ymin, xmax-xmin, ymax - ymin);-
141 }-
142 return result;-
143}-
144QPoint QMatrix::map(const QPoint &p) const-
145{-
146 qreal fx = p.x();-
147 qreal fy = p.y();-
148 return QPoint(qRound(_m11*fx + _m21*fy + _dx),-
149 qRound(_m12*fx + _m22*fy + _dy));-
150}-
151QPointF QMatrix::map(const QPointF &point) const-
152{-
153 qreal fx = point.x();-
154 qreal fy = point.y();-
155 return QPointF(_m11*fx + _m21*fy + _dx, _m12*fx + _m22*fy + _dy);-
156}-
157QLineF QMatrix::map(const QLineF &line) const-
158{-
159 return QLineF(map(line.p1()), map(line.p2()));-
160}-
161QLine QMatrix::map(const QLine &line) const-
162{-
163 return QLine(map(line.p1()), map(line.p2()));-
164}-
165QPolygon QMatrix::map(const QPolygon &a) const-
166{-
167 int size = a.size();-
168 int i;-
169 QPolygon p(size);-
170 const QPoint *da = a.constData();-
171 QPoint *dp = p.data();-
172 for(i = 0; i < size; i++) {-
173 { qreal fx = da[i].x(); qreal fy = da[i].y(); dp[i].rx() = qRound(_m11*fx + _m21*fy + _dx); dp[i].ry() = qRound(_m12*fx + _m22*fy + _dy); };-
174 }-
175 return p;-
176}-
177QPolygonF QMatrix::map(const QPolygonF &a) const-
178{-
179 int size = a.size();-
180 int i;-
181 QPolygonF p(size);-
182 const QPointF *da = a.constData();-
183 QPointF *dp = p.data();-
184 for(i = 0; i < size; i++) {-
185 { qreal fx = da[i].xp; qreal fy = da[i].yp; dp[i].xp = _m11*fx + _m21*fy + _dx; dp[i].yp = _m12*fx + _m22*fy + _dy; };-
186 }-
187 return p;-
188}-
189extern QPainterPath qt_regionToPath(const QRegion &region);-
190QRegion QMatrix::map(const QRegion &r) const-
191{-
192 if (_m11 == 1.0 && _m22 == 1.0 && _m12 == 0.0 && _m21 == 0.0) {-
193 if (_dx == 0.0 && _dy == 0.0)-
194 return r;-
195 QRegion copy(r);-
196 copy.translate(qRound(_dx), qRound(_dy));-
197 return copy;-
198 }-
199-
200 QPainterPath p = map(qt_regionToPath(r));-
201 return p.toFillPolygon().toPolygon();-
202}-
203QPainterPath QMatrix::map(const QPainterPath &path) const-
204{-
205 if (path.isEmpty())-
206 return QPainterPath();-
207-
208 QPainterPath copy = path;-
209-
210-
211 if (_m11 == 1.0 && _m22 == 1.0 && _m12 == 0.0 && _m21 == 0.0) {-
212-
213-
214 if (_dx != 0.0 || _dy != 0.0) {-
215 copy.detach();-
216 for (int i=0; i<path.elementCount(); ++i) {-
217 QPainterPath::Element &e = copy.d_ptr->elements[i];-
218 e.x += _dx;-
219 e.y += _dy;-
220 }-
221 }-
222-
223-
224 } else {-
225 copy.detach();-
226 for (int i=0; i<path.elementCount(); ++i) {-
227 QPainterPath::Element &e = copy.d_ptr->elements[i];-
228 qreal fx = e.x, fy = e.y;-
229 e.x = _m11*fx + _m21*fy + _dx;-
230 e.y = _m12*fx + _m22*fy + _dy;-
231 }-
232 }-
233-
234 return copy;-
235}-
236QPolygon QMatrix::mapToPolygon(const QRect &rect) const-
237{-
238 QPolygon a(4);-
239 qreal x[4], y[4];-
240 if (_m12 == 0.0F && _m21 == 0.0F) {-
241 x[0] = _m11*rect.x() + _dx;-
242 y[0] = _m22*rect.y() + _dy;-
243 qreal w = _m11*rect.width();-
244 qreal h = _m22*rect.height();-
245 if (w < 0) {-
246 w = -w;-
247 x[0] -= w;-
248 }-
249 if (h < 0) {-
250 h = -h;-
251 y[0] -= h;-
252 }-
253 x[1] = x[0]+w;-
254 x[2] = x[1];-
255 x[3] = x[0];-
256 y[1] = y[0];-
257 y[2] = y[0]+h;-
258 y[3] = y[2];-
259 } else {-
260 qreal right = rect.x() + rect.width();-
261 qreal bottom = rect.y() + rect.height();-
262 { qreal fx = rect.x(); qreal fy = rect.y(); x[0] = _m11*fx + _m21*fy + _dx; y[0] = _m12*fx + _m22*fy + _dy; };-
263 { qreal fx = right; qreal fy = rect.y(); x[1] = _m11*fx + _m21*fy + _dx; y[1] = _m12*fx + _m22*fy + _dy; };-
264 { qreal fx = right; qreal fy = bottom; x[2] = _m11*fx + _m21*fy + _dx; y[2] = _m12*fx + _m22*fy + _dy; };-
265 { qreal fx = rect.x(); qreal fy = bottom; x[3] = _m11*fx + _m21*fy + _dx; y[3] = _m12*fx + _m22*fy + _dy; };-
266 }-
267 a.setPoints(4, qRound(x[0]), qRound(y[0]),-
268 qRound(x[1]), qRound(y[1]),-
269 qRound(x[2]), qRound(y[2]),-
270 qRound(x[3]), qRound(y[3]));-
271 return a;-
272}-
273void QMatrix::reset()-
274{-
275 _m11 = _m22 = 1.0;-
276 _m12 = _m21 = _dx = _dy = 0.0;-
277}-
278QMatrix &QMatrix::translate(qreal dx, qreal dy)-
279{-
280 _dx += dx*_m11 + dy*_m21;-
281 _dy += dy*_m22 + dx*_m12;-
282 return *this;-
283}-
284QMatrix &QMatrix::scale(qreal sx, qreal sy)-
285{-
286 _m11 *= sx;-
287 _m12 *= sx;-
288 _m21 *= sy;-
289 _m22 *= sy;-
290 return *this;-
291}-
292QMatrix &QMatrix::shear(qreal sh, qreal sv)-
293{-
294 qreal tm11 = sv*_m21;-
295 qreal tm12 = sv*_m22;-
296 qreal tm21 = sh*_m11;-
297 qreal tm22 = sh*_m12;-
298 _m11 += tm11;-
299 _m12 += tm12;-
300 _m21 += tm21;-
301 _m22 += tm22;-
302 return *this;-
303}-
304-
305const qreal deg2rad = qreal(0.017453292519943295769);-
306QMatrix &QMatrix::rotate(qreal a)-
307{-
308 qreal sina = 0;-
309 qreal cosa = 0;-
310 if (a == 90. || a == -270.)-
311 sina = 1.;-
312 else if (a == 270. || a == -90.)-
313 sina = -1.;-
314 else if (a == 180.)-
315 cosa = -1.;-
316 else{-
317 qreal b = deg2rad*a;-
318 sina = qSin(b);-
319 cosa = qCos(b);-
320 }-
321 qreal tm11 = cosa*_m11 + sina*_m21;-
322 qreal tm12 = cosa*_m12 + sina*_m22;-
323 qreal tm21 = -sina*_m11 + cosa*_m21;-
324 qreal tm22 = -sina*_m12 + cosa*_m22;-
325 _m11 = tm11; _m12 = tm12;-
326 _m21 = tm21; _m22 = tm22;-
327 return *this;-
328}-
329QMatrix QMatrix::inverted(bool *invertible) const-
330{-
331 qreal dtr = determinant();-
332 if (dtr == 0.0) {-
333 if (invertible)-
334 *invertible = false;-
335 return QMatrix(true);-
336 }-
337 else {-
338 if (invertible)-
339 *invertible = true;-
340 qreal dinv = 1.0/dtr;-
341 return QMatrix((_m22*dinv), (-_m12*dinv),-
342 (-_m21*dinv), (_m11*dinv),-
343 ((_m21*_dy - _m22*_dx)*dinv),-
344 ((_m12*_dx - _m11*_dy)*dinv),-
345 true);-
346 }-
347}-
348bool QMatrix::operator==(const QMatrix &m) const-
349{-
350 return _m11 == m._m11 &&-
351 _m12 == m._m12 &&-
352 _m21 == m._m21 &&-
353 _m22 == m._m22 &&-
354 _dx == m._dx &&-
355 _dy == m._dy;-
356}-
357uint qHash(const QMatrix &key, uint seed) noexcept-
358{-
359 QtPrivate::QHashCombine hash;-
360 seed = hash(key.m11(), seed);-
361 seed = hash(key.m12(), seed);-
362 seed = hash(key.m21(), seed);-
363 seed = hash(key.m22(), seed);-
364 seed = hash(key.dx(), seed);-
365 seed = hash(key.dy(), seed);-
366 return
never executed: return seed;
seed;
never executed: return seed;
0
367}-
368bool QMatrix::operator!=(const QMatrix &m) const-
369{-
370 return _m11 != m._m11 ||-
371 _m12 != m._m12 ||-
372 _m21 != m._m21 ||-
373 _m22 != m._m22 ||-
374 _dx != m._dx ||-
375 _dy != m._dy;-
376}-
377QMatrix &QMatrix::operator *=(const QMatrix &m)-
378{-
379 qreal tm11 = _m11*m._m11 + _m12*m._m21;-
380 qreal tm12 = _m11*m._m12 + _m12*m._m22;-
381 qreal tm21 = _m21*m._m11 + _m22*m._m21;-
382 qreal tm22 = _m21*m._m12 + _m22*m._m22;-
383-
384 qreal tdx = _dx*m._m11 + _dy*m._m21 + m._dx;-
385 qreal tdy = _dx*m._m12 + _dy*m._m22 + m._dy;-
386-
387 _m11 = tm11; _m12 = tm12;-
388 _m21 = tm21; _m22 = tm22;-
389 _dx = tdx; _dy = tdy;-
390 return *this;-
391}-
392QMatrix QMatrix::operator *(const QMatrix &m) const-
393{-
394 qreal tm11 = _m11*m._m11 + _m12*m._m21;-
395 qreal tm12 = _m11*m._m12 + _m12*m._m22;-
396 qreal tm21 = _m21*m._m11 + _m22*m._m21;-
397 qreal tm22 = _m21*m._m12 + _m22*m._m22;-
398-
399 qreal tdx = _dx*m._m11 + _dy*m._m21 + m._dx;-
400 qreal tdy = _dx*m._m12 + _dy*m._m22 + m._dy;-
401 return QMatrix(tm11, tm12, tm21, tm22, tdx, tdy, true);-
402}-
403-
404-
405-
406-
407-
408QMatrix &QMatrix::operator=(const QMatrix &matrix) noexcept-
409{-
410 _m11 = matrix._m11;-
411 _m12 = matrix._m12;-
412 _m21 = matrix._m21;-
413 _m22 = matrix._m22;-
414 _dx = matrix._dx;-
415 _dy = matrix._dy;-
416 return
never executed: return *this;
*this;
never executed: return *this;
0
417}-
418-
419-
420-
421-
422-
423-
424-
425QMatrix::operator QVariant() const-
426{-
427 return QVariant(QVariant::Matrix, this);-
428}-
429-
430__attribute__((visibility("default"))) QPainterPath operator *(const QPainterPath &p, const QMatrix &m)-
431{-
432 return m.map(p);-
433}-
434QDataStream &operator<<(QDataStream &s, const QMatrix &m)-
435{-
436 if (s.version() == 1) {-
437 s << (float)m.m11() << (float)m.m12() << (float)m.m21()-
438 << (float)m.m22() << (float)m.dx() << (float)m.dy();-
439 } else {-
440 s << double(m.m11())-
441 << double(m.m12())-
442 << double(m.m21())-
443 << double(m.m22())-
444 << double(m.dx())-
445 << double(m.dy());-
446 }-
447 return s;-
448}-
449QDataStream &operator>>(QDataStream &s, QMatrix &m)-
450{-
451 if (s.version() == 1) {-
452 float m11, m12, m21, m22, dx, dy;-
453 s >> m11; s >> m12; s >> m21; s >> m22;-
454 s >> dx; s >> dy;-
455 m.setMatrix(m11, m12, m21, m22, dx, dy);-
456 }-
457 else {-
458 double m11, m12, m21, m22, dx, dy;-
459 s >> m11;-
460 s >> m12;-
461 s >> m21;-
462 s >> m22;-
463 s >> dx;-
464 s >> dy;-
465 m.setMatrix(m11, m12, m21, m22, dx, dy);-
466 }-
467 return s;-
468}-
469-
470-
471-
472QDebug operator<<(QDebug dbg, const QMatrix &m)-
473{-
474 QDebugStateSaver saver(dbg);-
475 dbg.nospace() << "QMatrix("-
476 << "11=" << m.m11()-
477 << " 12=" << m.m12()-
478 << " 21=" << m.m21()-
479 << " 22=" << m.m22()-
480 << " dx=" << m.dx()-
481 << " dy=" << m.dy()-
482 << ')';-
483 return dbg;-
484}-
485-
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9