• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KHTML

  • sources
  • kde-4.12
  • kdelibs
  • khtml
  • platform
  • graphics
PathQt.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Zack Rusin <zack@kde.org>
3  * 2006 Rob Buis <buis@kde.org>
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "config.h"
30 #include "Path.h"
31 
32 #include "FloatRect.h"
33 #include "PlatformString.h"
34 #include "AffineTransform.h"
35 #include <QPainterPath>
36 #include <QMatrix>
37 #include <QString>
38 
39 #define _USE_MATH_DEFINES
40 #include <math.h>
41 
42 namespace WebCore {
43 
44 Path::Path()
45  : m_path(new QPainterPath())
46 {
47 }
48 
49 Path::~Path()
50 {
51  delete m_path;
52 }
53 
54 Path::Path(const Path& other)
55  : m_path(new QPainterPath(*other.platformPath()))
56 {
57 }
58 
59 Path& Path::operator=(const Path& other)
60 {
61  if (&other != this) {
62  delete m_path;
63  m_path = new QPainterPath(*other.platformPath());
64  }
65 
66  return *this;
67 }
68 
69 bool Path::contains(const FloatPoint& point, WindRule rule) const
70 {
71  Qt::FillRule savedRule = m_path->fillRule();
72  m_path->setFillRule(rule == RULE_EVENODD ? Qt::OddEvenFill : Qt::WindingFill);
73 
74  bool contains = m_path->contains(point);
75 
76  m_path->setFillRule(savedRule);
77  return contains;
78 }
79 
80 void Path::translate(const FloatSize& size)
81 {
82  QMatrix matrix;
83  matrix.translate(size.width(), size.height());
84  *m_path = (*m_path) * matrix;
85 }
86 
87 FloatRect Path::boundingRect() const
88 {
89  return m_path->boundingRect();
90 }
91 
92 void Path::moveTo(const FloatPoint& point)
93 {
94  m_path->moveTo(point);
95 }
96 
97 void Path::addLineTo(const FloatPoint& p)
98 {
99  m_path->lineTo(p);
100 }
101 
102 void Path::addQuadCurveTo(const FloatPoint& cp, const FloatPoint& p)
103 {
104  m_path->quadTo(cp, p);
105 }
106 
107 void Path::addBezierCurveTo(const FloatPoint& cp1, const FloatPoint& cp2, const FloatPoint& p)
108 {
109  m_path->cubicTo(cp1, cp2, p);
110 }
111 
112 void Path::addArcTo(const FloatPoint& p1, const FloatPoint& p2, float radius)
113 {
114  //FIXME: busted
115  qWarning("arcTo is busted");
116  m_path->arcTo(p1.x(), p1.y(), p2.x(), p2.y(), radius, 90);
117 }
118 
119 void Path::closeSubpath()
120 {
121  m_path->closeSubpath();
122 }
123 
124 #define DEGREES(t) ((t) * 180.0 / M_PI)
125 void Path::addArc(const FloatPoint& p, float r, float sar, float ear, bool anticlockwise)
126 {
127  qreal xc = p.x();
128  qreal yc = p.y();
129  qreal radius = r;
130 
131 
132  //### HACK
133  // In Qt we don't switch the coordinate system for degrees
134  // and still use the 0,0 as bottom left for degrees so we need
135  // to switch
136  sar = -sar;
137  ear = -ear;
138  anticlockwise = !anticlockwise;
139  //end hack
140 
141  float sa = DEGREES(sar);
142  float ea = DEGREES(ear);
143 
144  double span = 0;
145 
146  double xs = xc - radius;
147  double ys = yc - radius;
148  double width = radius*2;
149  double height = radius*2;
150 
151  if (!anticlockwise && (ea < sa))
152  span += 360;
153  else if (anticlockwise && (sa < ea))
154  span -= 360;
155 
156  // this is also due to switched coordinate system
157  // we would end up with a 0 span instead of 360
158  if (!(qFuzzyCompare(span + (ea - sa), 0.0) &&
159  qFuzzyCompare(qAbs(span), 360.0))) {
160  span += ea - sa;
161  }
162 
163  m_path->moveTo(QPointF(xc + radius * cos(sar),
164  yc - radius * sin(sar)));
165 
166  m_path->arcTo(xs, ys, width, height, sa, span);
167 }
168 
169 void Path::addRect(const FloatRect& r)
170 {
171  m_path->addRect(r.x(), r.y(), r.width(), r.height());
172 }
173 
174 void Path::addEllipse(const FloatRect& r)
175 {
176  m_path->addEllipse(r.x(), r.y(), r.width(), r.height());
177 }
178 
179 void Path::clear()
180 {
181  *m_path = QPainterPath();
182 }
183 
184 bool Path::isEmpty() const
185 {
186  return m_path->isEmpty();
187 }
188 
189 String Path::debugString() const
190 {
191  QString ret;
192  for (int i = 0; i < m_path->elementCount(); ++i) {
193  const QPainterPath::Element &cur = m_path->elementAt(i);
194 
195  switch (cur.type) {
196  case QPainterPath::MoveToElement:
197  ret += QString("M %1 %2").arg(cur.x).arg(cur.y);
198  break;
199  case QPainterPath::LineToElement:
200  ret += QString("L %1 %2").arg(cur.x).arg(cur.y);
201  break;
202  case QPainterPath::CurveToElement:
203  {
204  const QPainterPath::Element &c1 = m_path->elementAt(i + 1);
205  const QPainterPath::Element &c2 = m_path->elementAt(i + 2);
206 
207  Q_ASSERT(c1.type == QPainterPath::CurveToDataElement);
208  Q_ASSERT(c2.type == QPainterPath::CurveToDataElement);
209 
210  ret += QString("C %1 %2 %3 %4 %5 %6").arg(cur.x).arg(cur.y).arg(c1.x).arg(c1.y).arg(c2.x).arg(c2.y);
211 
212  i += 2;
213  break;
214  }
215  case QPainterPath::CurveToDataElement:
216  Q_ASSERT(false);
217  break;
218  }
219  }
220 
221  return ret;
222 }
223 
224 void Path::apply(void* info, PathApplierFunction function) const
225 {
226  PathElement pelement;
227  FloatPoint points[3];
228  pelement.points = points;
229  for (int i = 0; i < m_path->elementCount(); ++i) {
230  const QPainterPath::Element& cur = m_path->elementAt(i);
231 
232  switch (cur.type) {
233  case QPainterPath::MoveToElement:
234  pelement.type = PathElementMoveToPoint;
235  pelement.points[0] = QPointF(cur);
236  function(info, &pelement);
237  break;
238  case QPainterPath::LineToElement:
239  pelement.type = PathElementAddLineToPoint;
240  pelement.points[0] = QPointF(cur);
241  function(info, &pelement);
242  break;
243  case QPainterPath::CurveToElement:
244  {
245  const QPainterPath::Element& c1 = m_path->elementAt(i + 1);
246  const QPainterPath::Element& c2 = m_path->elementAt(i + 2);
247 
248  Q_ASSERT(c1.type == QPainterPath::CurveToDataElement);
249  Q_ASSERT(c2.type == QPainterPath::CurveToDataElement);
250 
251  pelement.type = PathElementAddCurveToPoint;
252  pelement.points[0] = QPointF(cur);
253  pelement.points[1] = QPointF(c1);
254  pelement.points[2] = QPointF(c2);
255  function(info, &pelement);
256 
257  i += 2;
258  break;
259  }
260  case QPainterPath::CurveToDataElement:
261  Q_ASSERT(false);
262  }
263  }
264 }
265 
266 void Path::transform(const AffineTransform& transform)
267 {
268  if (m_path) {
269  QMatrix mat = transform;
270  QPainterPath temp = mat.map(*m_path);
271  delete m_path;
272  m_path = new QPainterPath(temp);
273  }
274 }
275 
276 }
277 
278 // vim: ts=4 sw=4 et
WebCore::FloatRect
Definition: FloatRect.h:59
WebCore::FloatSize::width
float width() const
Definition: FloatSize.h:56
WebCore::FloatRect::width
float width() const
Definition: FloatRect.h:78
WebCore::FloatRect::height
float height() const
Definition: FloatRect.h:79
khtml::Path::boundingRect
FloatRect boundingRect() const
Definition: PathQt.cpp:87
WebCore::FloatRect::x
float x() const
Definition: FloatRect.h:76
khtml::Path::addLineTo
void addLineTo(const FloatPoint &)
Definition: PathQt.cpp:97
DEGREES
#define DEGREES(t)
Definition: PathQt.cpp:124
WebCore::FloatSize::height
float height() const
Definition: FloatSize.h:57
WebCore::FloatSize
Definition: FloatSize.h:48
khtml::Path::transform
void transform(const AffineTransform &)
Definition: PathQt.cpp:266
QString
khtml::Path::addBezierCurveTo
void addBezierCurveTo(const FloatPoint &controlPoint1, const FloatPoint &controlPoint2, const FloatPoint &)
Definition: PathQt.cpp:107
khtml::PathElement
Definition: Path.h:61
khtml::Path::~Path
~Path()
Definition: PathQt.cpp:49
khtml::Path::contains
bool contains(const FloatPoint &, WindRule rule=RULE_NONZERO) const
Definition: PathQt.cpp:69
khtml::Path::Path
Path()
Definition: PathQt.cpp:44
khtml::Path::isEmpty
bool isEmpty() const
Definition: PathQt.cpp:184
khtml::Path::platformPath
PlatformPath * platformPath() const
Definition: Path.h:104
khtml::Path::addEllipse
void addEllipse(const FloatRect &)
Definition: PathQt.cpp:174
khtml::Path::addArc
void addArc(const FloatPoint &, float radius, float startAngle, float endAngle, bool anticlockwise)
Definition: PathQt.cpp:125
WebCore::FloatPoint
Definition: FloatPoint.h:61
WebCore::AffineTransform
Definition: AffineTransform.h:47
khtml::Path::debugString
DOM::DOMString debugString() const
Definition: PathQt.cpp:189
khtml::Path::addRect
void addRect(const FloatRect &)
Definition: PathQt.cpp:169
khtml::Path::clear
void clear()
Definition: PathQt.cpp:179
AffineTransform.h
khtml::PathElement::points
FloatPoint * points
Definition: Path.h:63
WebCore::FloatRect::y
float y() const
Definition: FloatRect.h:77
khtml::Path::moveTo
void moveTo(const FloatPoint &)
Definition: PathQt.cpp:92
khtml::Path::translate
void translate(const FloatSize &)
Definition: PathQt.cpp:80
WebCore::FloatPoint::y
float y() const
Definition: FloatPoint.h:70
khtml::Path::closeSubpath
void closeSubpath()
Definition: PathQt.cpp:119
khtml::Path::apply
void apply(void *info, PathApplierFunction) const
Definition: PathQt.cpp:224
WebCore::FloatPoint::x
float x() const
Definition: FloatPoint.h:69
FloatRect.h
khtml::Path::addArcTo
void addArcTo(const FloatPoint &, const FloatPoint &, float radius)
Definition: PathQt.cpp:112
khtml::Path::addQuadCurveTo
void addQuadCurveTo(const FloatPoint &controlPoint, const FloatPoint &point)
Definition: PathQt.cpp:102
Path.h
khtml::Path
Definition: Path.h:68
khtml::Path::operator=
Path & operator=(const Path &)
Definition: PathQt.cpp:59
WebCore::String
DOM::DOMString String
Definition: PlatformString.h:8
PlatformString.h
khtml::PathElement::type
PathElementType type
Definition: Path.h:62
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:51:22 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KHTML

Skip menu "KHTML"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal