• 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
  • qt
qt/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 using namespace WebCore;
43 
44 namespace khtml {
45 
46 Path::Path()
47  : m_path(new QPainterPath())
48 {
49 }
50 
51 Path::~Path()
52 {
53  delete m_path;
54 }
55 
56 Path::Path(const Path& other)
57  : m_path(new QPainterPath(*other.platformPath()))
58 {
59 }
60 
61 Path& Path::operator=(const Path& other)
62 {
63  if (&other != this) {
64  delete m_path;
65  m_path = new QPainterPath(*other.platformPath());
66  }
67 
68  return *this;
69 }
70 
71 bool Path::contains(const FloatPoint& point, WindRule rule) const
72 {
73  Qt::FillRule savedRule = m_path->fillRule();
74  m_path->setFillRule(rule == RULE_EVENODD ? Qt::OddEvenFill : Qt::WindingFill);
75 
76  bool contains = m_path->contains(point);
77 
78  m_path->setFillRule(savedRule);
79  return contains;
80 }
81 
82 void Path::translate(const FloatSize& size)
83 {
84  QMatrix matrix;
85  matrix.translate(size.width(), size.height());
86  *m_path = (*m_path) * matrix;
87 }
88 
89 FloatRect Path::boundingRect() const
90 {
91  return m_path->boundingRect();
92 }
93 
94 void Path::moveTo(const FloatPoint& point)
95 {
96  m_path->moveTo(point);
97 }
98 
99 void Path::addLineTo(const FloatPoint& p)
100 {
101  m_path->lineTo(p);
102 }
103 
104 void Path::addQuadCurveTo(const FloatPoint& cp, const FloatPoint& p)
105 {
106  m_path->quadTo(cp, p);
107 }
108 
109 void Path::addBezierCurveTo(const FloatPoint& cp1, const FloatPoint& cp2, const FloatPoint& p)
110 {
111  m_path->cubicTo(cp1, cp2, p);
112 }
113 
114 void Path::addArcTo(const FloatPoint& p1, const FloatPoint& p2, float radius)
115 {
116  //FIXME: busted
117  qWarning("arcTo is busted");
118  m_path->arcTo(p1.x(), p1.y(), p2.x(), p2.y(), radius, 90);
119 }
120 
121 void Path::closeSubpath()
122 {
123  m_path->closeSubpath();
124 }
125 
126 #define DEGREES(t) ((t) * 180.0 / M_PI)
127 void Path::addArc(const FloatPoint& p, float r, float sar, float ear, bool anticlockwise)
128 {
129  qreal xc = p.x();
130  qreal yc = p.y();
131  qreal radius = r;
132 
133 
134  //### HACK
135  // In Qt we don't switch the coordinate system for degrees
136  // and still use the 0,0 as bottom left for degrees so we need
137  // to switch
138  sar = -sar;
139  ear = -ear;
140  anticlockwise = !anticlockwise;
141  //end hack
142 
143  float sa = DEGREES(sar);
144  float ea = DEGREES(ear);
145 
146  double span = 0;
147 
148  double xs = xc - radius;
149  double ys = yc - radius;
150  double width = radius*2;
151  double height = radius*2;
152 
153  if (!anticlockwise && (ea < sa))
154  span += 360;
155  else if (anticlockwise && (sa < ea))
156  span -= 360;
157 
158  // this is also due to switched coordinate system
159  // we would end up with a 0 span instead of 360
160  if (!(qFuzzyCompare(span + (ea - sa), 0.0) &&
161  qFuzzyCompare(qAbs(span), 360.0))) {
162  span += ea - sa;
163  }
164 
165  m_path->moveTo(QPointF(xc + radius * cos(sar),
166  yc - radius * sin(sar)));
167 
168  m_path->arcTo(xs, ys, width, height, sa, span);
169 }
170 
171 void Path::addRect(const FloatRect& r)
172 {
173  m_path->addRect(r.x(), r.y(), r.width(), r.height());
174 }
175 
176 void Path::addEllipse(const FloatRect& r)
177 {
178  m_path->addEllipse(r.x(), r.y(), r.width(), r.height());
179 }
180 
181 void Path::clear()
182 {
183  *m_path = QPainterPath();
184 }
185 
186 bool Path::isEmpty() const
187 {
188  return m_path->isEmpty();
189 }
190 
191 DOM::DOMString Path::debugString() const
192 {
193  QString ret;
194  for (int i = 0; i < m_path->elementCount(); ++i) {
195  const QPainterPath::Element &cur = m_path->elementAt(i);
196 
197  switch (cur.type) {
198  case QPainterPath::MoveToElement:
199  ret += QString("M %1 %2").arg(cur.x).arg(cur.y);
200  break;
201  case QPainterPath::LineToElement:
202  ret += QString("L %1 %2").arg(cur.x).arg(cur.y);
203  break;
204  case QPainterPath::CurveToElement:
205  {
206  const QPainterPath::Element &c1 = m_path->elementAt(i + 1);
207  const QPainterPath::Element &c2 = m_path->elementAt(i + 2);
208 
209  Q_ASSERT(c1.type == QPainterPath::CurveToDataElement);
210  Q_ASSERT(c2.type == QPainterPath::CurveToDataElement);
211 
212  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);
213 
214  i += 2;
215  break;
216  }
217  case QPainterPath::CurveToDataElement:
218  Q_ASSERT(false);
219  break;
220  }
221  }
222 
223  return ret;
224 }
225 
226 void Path::apply(void* info, PathApplierFunction function) const
227 {
228  PathElement pelement;
229  FloatPoint points[3];
230  pelement.points = points;
231  for (int i = 0; i < m_path->elementCount(); ++i) {
232  const QPainterPath::Element& cur = m_path->elementAt(i);
233 
234  switch (cur.type) {
235  case QPainterPath::MoveToElement:
236  pelement.type = PathElementMoveToPoint;
237  pelement.points[0] = QPointF(cur);
238  function(info, &pelement);
239  break;
240  case QPainterPath::LineToElement:
241  pelement.type = PathElementAddLineToPoint;
242  pelement.points[0] = QPointF(cur);
243  function(info, &pelement);
244  break;
245  case QPainterPath::CurveToElement:
246  {
247  const QPainterPath::Element& c1 = m_path->elementAt(i + 1);
248  const QPainterPath::Element& c2 = m_path->elementAt(i + 2);
249 
250  Q_ASSERT(c1.type == QPainterPath::CurveToDataElement);
251  Q_ASSERT(c2.type == QPainterPath::CurveToDataElement);
252 
253  pelement.type = PathElementAddCurveToPoint;
254  pelement.points[0] = QPointF(cur);
255  pelement.points[1] = QPointF(c1);
256  pelement.points[2] = QPointF(c2);
257  function(info, &pelement);
258 
259  i += 2;
260  break;
261  }
262  case QPainterPath::CurveToDataElement:
263  Q_ASSERT(false);
264  }
265  }
266 }
267 
268 void Path::transform(const AffineTransform& transform)
269 {
270  if (m_path) {
271  QMatrix mat = transform;
272  QPainterPath temp = mat.map(*m_path);
273  delete m_path;
274  m_path = new QPainterPath(temp);
275  }
276 }
277 
278 }
279 
280 // 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
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
DOM::DOMString
This class implements the basic string we use in the DOM.
Definition: dom_string.h:43
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
DEGREES
#define DEGREES(t)
Definition: qt/PathQt.cpp:126
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
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