1 /* This file is part of the KDE project
2  * Copyright (C) 2009 Jos van den Oever <jos@vandenoever.info>
3  * Copyright (C) 2009 Thomas Zander <zander@kde.org>
4  * Copyright (C) 2008 Jan Hambrecht <jaham@gmx.net>
5  * Copyright (C) 2010 Thorsten Zachmann <zachmann@kde.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include "KoFlake.h"
24 #include "KoShape.h"
25 
26 #include <QGradient>
27 #include <math.h>
28 
cloneGradient(const QGradient * gradient)29 QGradient *KoFlake::cloneGradient(const QGradient *gradient)
30 {
31     if (! gradient)
32         return 0;
33 
34     QGradient *clone = 0;
35 
36     switch (gradient->type()) {
37     case QGradient::LinearGradient:
38     {
39         const QLinearGradient *lg = static_cast<const QLinearGradient*>(gradient);
40         clone = new QLinearGradient(lg->start(), lg->finalStop());
41         break;
42     }
43     case QGradient::RadialGradient:
44     {
45         const QRadialGradient *rg = static_cast<const QRadialGradient*>(gradient);
46         clone = new QRadialGradient(rg->center(), rg->radius(), rg->focalPoint());
47         break;
48     }
49     case QGradient::ConicalGradient:
50     {
51         const QConicalGradient *cg = static_cast<const QConicalGradient*>(gradient);
52         clone = new QConicalGradient(cg->center(), cg->angle());
53         break;
54     }
55     default:
56         return 0;
57     }
58 
59     clone->setCoordinateMode(gradient->coordinateMode());
60     clone->setSpread(gradient->spread());
61     clone->setStops(gradient->stops());
62 
63     return clone;
64 }
65 
toRelative(const QPointF & absolute,const QSizeF & size)66 QPointF KoFlake::toRelative(const QPointF &absolute, const QSizeF &size)
67 {
68     return QPointF(size.width() == 0 ? 0: absolute.x() / size.width(),
69                    size.height() == 0 ? 0: absolute.y() / size.height());
70 }
71 
toAbsolute(const QPointF & relative,const QSizeF & size)72 QPointF KoFlake::toAbsolute(const QPointF &relative, const QSizeF &size)
73 {
74     return QPointF(relative.x() * size.width(), relative.y() * size.height());
75 }
76