1#############################################################################
2##
3## Copyright (C) 2013 Riverbank Computing Limited.
4## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5## All rights reserved.
6##
7## This file is part of the examples of PyQt.
8##
9## $QT_BEGIN_LICENSE:LGPL$
10## Commercial Usage
11## Licensees holding valid Qt Commercial licenses may use this file in
12## accordance with the Qt Commercial License Agreement provided with the
13## Software or, alternatively, in accordance with the terms contained in
14## a written agreement between you and Nokia.
15##
16## GNU Lesser General Public License Usage
17## Alternatively, this file may be used under the terms of the GNU Lesser
18## General Public License version 2.1 as published by the Free Software
19## Foundation and appearing in the file LICENSE.LGPL included in the
20## packaging of this file.  Please review the following information to
21## ensure the GNU Lesser General Public License version 2.1 requirements
22## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23##
24## In addition, as a special exception, Nokia gives you certain additional
25## rights.  These rights are described in the Nokia Qt LGPL Exception
26## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27##
28## GNU General Public License Usage
29## Alternatively, this file may be used under the terms of the GNU
30## General Public License version 3.0 as published by the Free Software
31## Foundation and appearing in the file LICENSE.GPL included in the
32## packaging of this file.  Please review the following information to
33## ensure the GNU General Public License version 3.0 requirements will be
34## met: http://www.gnu.org/copyleft/gpl.html.
35##
36## If you have questions regarding the use of this file, please contact
37## Nokia at qt-info@nokia.com.
38## $QT_END_LICENSE$
39##
40#############################################################################
41
42
43from PyQt5.QtCore import QLineF, QPointF
44
45
46class Guide(object):
47    def __init__(self, follows=None):
48        self.scaleX = 1.0
49        self.scaleY = 1.0
50
51        if follows is not None:
52            while follows.nextGuide is not follows.firstGuide:
53                follows = follows.nextGuide
54
55            follows.nextGuide = self
56            self.prevGuide = follows
57            self.firstGuide = follows.firstGuide
58            self.nextGuide = follows.firstGuide
59            self.startLength = int(follows.startLength + follows.length()) + 1
60        else:
61            self.prevGuide = self
62            self.firstGuide = self
63            self.nextGuide = self
64            self.startLength = 0
65
66    def setScale(self, scaleX, scaleY, all=True):
67        self.scaleX = scaleX
68        self.scaleY = scaleY
69
70        if all:
71            next = self.nextGuide
72            while next is not self:
73                next.scaleX = scaleX
74                next.scaleY = scaleY
75                next = next.nextGuide
76
77    def setFence(self, fence, all=True):
78        self.fence = fence
79
80        if all:
81            next = self.nextGuide
82            while next is not self:
83                next.fence = fence
84                next = next.nextGuide
85
86    def lengthAll(self):
87        len = self.length()
88        next = self.nextGuide
89        while next is not self:
90            len += next.length()
91            next = next.nextGuide
92
93        return len
94
95    def move(self, item, dest, moveSpeed):
96        walkLine = QLineF(item.getGuidedPos(), dest)
97        if moveSpeed >= 0 and walkLine.length() > moveSpeed:
98            # The item is too far away from it's destination point so we move
99            # it towards it instead.
100            dx = walkLine.dx()
101            dy = walkLine.dy()
102
103            if abs(dx) > abs(dy):
104                # Walk along x-axis.
105                if dx != 0:
106                    d = moveSpeed * dy / abs(dx)
107
108                    if dx > 0:
109                        s = moveSpeed
110                    else:
111                        s = -moveSpeed
112
113                    dest.setX(item.getGuidedPos().x() + s)
114                    dest.setY(item.getGuidedPos().y() + d)
115            else:
116                # Walk along y-axis.
117                if dy != 0:
118                    d = moveSpeed * dx / abs(dy)
119
120                    if dy > 0:
121                        s = moveSpeed
122                    else:
123                        s = -moveSpeed
124
125                    dest.setX(item.getGuidedPos().x() + d)
126                    dest.setY(item.getGuidedPos().y() + s)
127
128        item.setGuidedPos(dest)
129
130    def startPos(self):
131        return QPointF(0, 0)
132
133    def endPos(self):
134        return QPointF(0, 0)
135
136    def length(self):
137        return 1.0
138
139    def guide(self, item, moveSpeed):
140        raise NotImplementedError
141