1 /* This file is part of the KDE project
2    Copyright (C) 2008 Sven Langkamp <sven.langkamp@gmail.com>
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18 */
19 
20 #include "KPrClockWipeSubpathHelper.h"
21 
22 #include <math.h>
23 #include <float.h>
24 #include <QPainterPath>
25 
26 
KPrClockWipeSubpathHelper()27 KPrClockWipeSubpathHelper::KPrClockWipeSubpathHelper()
28 {
29 }
30 
~KPrClockWipeSubpathHelper()31 KPrClockWipeSubpathHelper::~KPrClockWipeSubpathHelper()
32 {
33 }
34 
addSubpathForCircularArc(QPainterPath * clipPath,QRect & boundingRect,double startAngle,double endAngle)35 void KPrClockWipeSubpathHelper::addSubpathForCircularArc(QPainterPath* clipPath, QRect& boundingRect, double startAngle, double endAngle)
36 {
37     if(fabs(startAngle - endAngle) < DBL_EPSILON)
38         return;
39 
40     int width = boundingRect.width();
41     int height = boundingRect.height();
42 
43     while(startAngle < 0)
44         startAngle += 2*M_PI;
45 
46     if(endAngle < startAngle)
47         endAngle += 2*M_PI;
48 
49     QPoint center = boundingRect.center();
50     double maxRadius = sqrt(double(width*width/4 + height*height/4));
51 
52     double startAngleInQuadrant = fmod(startAngle, 0.5*M_PI);
53     double quadrantAngle = static_cast<int>(startAngle / (0.5*M_PI))*(0.5*M_PI);
54 
55     int cornerX = cos(quadrantAngle + 0.5*M_PI) < 0 ? -width/2 : width/2;
56     int cornerY = sin(quadrantAngle + 0.5*M_PI) < 0 ? -height/2 : height/2;
57 
58     double cornerAngleInQuadrant;
59     if(cos(quadrantAngle + 0.5*M_PI)*sin(quadrantAngle + 0.5*M_PI) > 0)
60         cornerAngleInQuadrant = fabs(atan(static_cast<double>(cornerY)/cornerX));
61     else
62         cornerAngleInQuadrant = fabs(atan(static_cast<double>(cornerX)/cornerY));
63 
64     double cornerAngle;
65     if( startAngleInQuadrant < cornerAngleInQuadrant )
66         cornerAngle = quadrantAngle + cornerAngleInQuadrant;
67     else
68         cornerAngle = quadrantAngle + M_PI - cornerAngleInQuadrant;
69 
70     clipPath->moveTo(center);
71     clipPath->lineTo(QPoint(center.x() + maxRadius*cos(startAngle), center.y() - maxRadius*sin(startAngle)));
72 
73     while(cornerAngle < endAngle) {
74 
75         int cornerX = cos(cornerAngle) < 0 ?  0 : width;
76         int cornerY = sin(cornerAngle) < 0 ?  height : 0;
77         clipPath->lineTo(QPoint( boundingRect.x() + cornerX, boundingRect.y() + cornerY));
78 
79         quadrantAngle = static_cast<int>(cornerAngle / (0.5*M_PI))*(0.5*M_PI);
80         cornerAngleInQuadrant = cornerAngle - quadrantAngle;
81         cornerAngle = quadrantAngle + M_PI - cornerAngleInQuadrant;
82     }
83     clipPath->lineTo(QPoint(center.x() + maxRadius*cos(endAngle), center.y() - maxRadius*sin(endAngle)));
84 
85 
86     clipPath->closeSubpath();
87 }
88 
89