1 /* This file is part of the KDE project
2    Copyright (C) 2008 Marijn Kruisselbrink <mkruisselbrink@kde.org>
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 "KPrSpiralWipeStrategy.h"
21 #include "KPrSpiralWipeEffectFactory.h"
22 
getSubType(int firstLeg,bool clockwise,bool reverse)23 static int getSubType(int firstLeg, bool clockwise, bool reverse)
24 {
25     if (!reverse) {
26         switch (firstLeg) {
27             case 0: return clockwise ? KPrSpiralWipeEffectFactory::ClockwiseTopLeftIn : KPrSpiralWipeEffectFactory::CounterClockwiseTopLeftIn;
28             case 1: return clockwise ? KPrSpiralWipeEffectFactory::ClockwiseTopRightIn : KPrSpiralWipeEffectFactory::CounterClockwiseBottomLeftIn;
29             case 2: return clockwise ? KPrSpiralWipeEffectFactory::ClockwiseBottomRightIn : KPrSpiralWipeEffectFactory::CounterClockwiseBottomRightIn;
30             case 3: return clockwise ? KPrSpiralWipeEffectFactory::ClockwiseBottomLeftIn : KPrSpiralWipeEffectFactory::CounterClockwiseTopRightIn;
31         }
32     } else {
33         switch (firstLeg) {
34             case 0: return !clockwise ? KPrSpiralWipeEffectFactory::ClockwiseTopLeftOut : KPrSpiralWipeEffectFactory::CounterClockwiseTopLeftOut;
35             case 1: return !clockwise ? KPrSpiralWipeEffectFactory::ClockwiseTopRightOut : KPrSpiralWipeEffectFactory::CounterClockwiseBottomLeftOut;
36             case 2: return !clockwise ? KPrSpiralWipeEffectFactory::ClockwiseBottomRightOut : KPrSpiralWipeEffectFactory::CounterClockwiseBottomRightOut;
37             case 3: return !clockwise ? KPrSpiralWipeEffectFactory::ClockwiseBottomLeftOut : KPrSpiralWipeEffectFactory::CounterClockwiseTopRightOut;
38         }
39     }
40     // not reached
41     return KPrSpiralWipeEffectFactory::ClockwiseTopLeftIn;
42 }
43 
getSmilSubType(int firstLeg,bool clockwise)44 static const char * getSmilSubType(int firstLeg, bool clockwise)
45 {
46     switch (firstLeg) {
47         case 0: return clockwise ? "topLeftClockwise" : "topLeftCounterClockwise";
48         case 1: return clockwise ? "topRightClockwise" : "bottomLeftCounterClockwise";
49         case 2: return clockwise ? "bottomRightClockwise" : "bottomRightCounterClockwise";
50         default:
51         case 3: return clockwise ? "bottomLeftClockwise" : "topRightCounterClockwise";
52     }
53 }
54 
KPrSpiralWipeStrategy(int firstLeg,bool clockwise,bool reverse)55 KPrSpiralWipeStrategy::KPrSpiralWipeStrategy(int firstLeg, bool clockwise, bool reverse)
56     : KPrMatrixWipeStrategy( getSubType(firstLeg, clockwise, reverse), "spiralWipe", getSmilSubType(firstLeg, clockwise), reverse ),
57     m_firstLeg(firstLeg), m_clockwise(clockwise)
58 {
59 }
60 
~KPrSpiralWipeStrategy()61 KPrSpiralWipeStrategy::~KPrSpiralWipeStrategy()
62 {
63 }
64 
squareIndex(int x,int y,int columns,int rows)65 int KPrSpiralWipeStrategy::squareIndex(int x, int y, int columns, int rows)
66 {
67     int curRing = qMin(qMin(x, y), qMin(columns - x - 1, rows - y - 1));
68     int maxRingSize = (columns + rows - 2) * 2;
69     int passed = 0;
70     if (curRing > 0) passed = curRing * (maxRingSize + maxRingSize - (curRing-1) * 8) / 2;
71     int leg = 0;
72     if (m_clockwise) {
73         if (y == curRing) {
74             leg = 0;
75         }
76         if (x == columns - curRing - 1) {
77             leg = 1;
78         }
79         if (y == rows - curRing - 1) {
80             leg = 2;
81         }
82         if (x == curRing && y != curRing) {
83             leg = 3;
84         }
85         if (curRing * 2 + 1 == rows) {
86             if (m_firstLeg == 0 || m_firstLeg == 3) {
87                 leg = 0;
88             }
89         }
90         if (leg < m_firstLeg) leg += 4;
91         if (leg > m_firstLeg && leg < m_firstLeg+4) passed += (m_firstLeg&1 ? rows : columns) - 2*curRing - 1;
92         if (leg > m_firstLeg+1 && leg < m_firstLeg+4) passed += (m_firstLeg&1 ? columns : rows) - 2*curRing - 1;
93         if (leg > m_firstLeg+2 && leg < m_firstLeg+4) passed += (m_firstLeg&1 ? rows : columns) - 2*curRing - 1;
94         if (leg > 3) leg -= 4;
95 
96         if (leg == 0) {
97             passed += x - curRing;
98         } else if (leg == 1) {
99             passed += y - curRing;
100         } else if (leg == 2) {
101             passed += columns - x - curRing - 1;
102         } else if (leg == 3) {
103             passed += rows - y - curRing - 1;
104         }
105     } else {
106         if (x == curRing) {
107             leg = 0;
108         }
109         if (y == rows - curRing - 1) {
110             leg = 1;
111         }
112         if (x == columns - curRing - 1) {
113             leg = 2;
114         }
115         if (y == curRing && x != curRing) {
116             leg = 3;
117         }
118         if (curRing * 2 + 1 == rows) {
119             if (m_firstLeg == 0 || m_firstLeg == 1) {
120                 leg = 1;
121             }
122         }
123         if (leg < m_firstLeg) leg += 4;
124         if (leg > m_firstLeg && leg < m_firstLeg+4) passed += (m_firstLeg&1 ? columns : rows) - 2*curRing - 1;
125         if (leg > m_firstLeg+1 && leg < m_firstLeg+4) passed += (m_firstLeg&1 ? rows : columns) - 2*curRing - 1;
126         if (leg > m_firstLeg+2 && leg < m_firstLeg+4) passed += (m_firstLeg&1 ? columns : rows) - 2*curRing - 1;
127         if (leg > 3) leg -= 4;
128 
129         if (leg == 0) {
130             passed += y - curRing;
131         } else if (leg == 1) {
132             passed += x - curRing;
133         } else if (leg == 2) {
134             passed += rows - y - curRing - 1;
135         } else if (leg == 3) {
136             passed += columns - x - curRing - 1;
137         }
138     }
139     return reverse() ? columns * rows - passed - 1 : passed;
140 }
141 
maxIndex(int columns,int rows)142 int KPrSpiralWipeStrategy::maxIndex(int columns, int rows)
143 {
144     return columns * rows;
145 }
146 
147