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 colors import Colors
44
45
46class Score(object):
47    LOCK_ITEMS, UNLOCK_ITEMS, SKIP_LOCK = range(3)
48
49    FROM_CURRENT, FROM_START, NEW_ANIMATION_ONLY, ONLY_IF_VISIBLE = range(4)
50
51    def __init__(self):
52        self._index = {}
53        self._playlist = []
54
55    def hasQueuedMovies(self):
56        return len(self._playlist) > 0
57
58    def prepare(self, movie, runMode, lockMode):
59        if lockMode == Score.LOCK_ITEMS:
60            for item in movie:
61                if runMode != Score.ONLY_IF_VISIBLE or item.isVisible():
62                    item.setEnabled(False)
63                    item.prepare()
64        elif lockMode == Score.UNLOCK_ITEMS:
65            for item in movie:
66                if runMode != Score.ONLY_IF_VISIBLE or item.isVisible():
67                    item.setEnabled(True)
68                    item.prepare()
69        else:
70            for item in movie:
71                if runMode != Score.ONLY_IF_VISIBLE or item.isVisible():
72                    item.prepare()
73
74    def _play(self, movie, runMode):
75        if runMode == Score.NEW_ANIMATION_ONLY:
76            for item in movie:
77                if item.notOwnerOfItem():
78                    item.play(True)
79        elif runMode == Score.ONLY_IF_VISIBLE:
80            for item in movie:
81                if item.isVisible():
82                    item.play(runMode == Score.FROM_START)
83        else:
84            for item in movie:
85                item.play(runMode == Score.FROM_START)
86
87    def queueMovie(self, indexName, runMode=FROM_START, lockMode=SKIP_LOCK):
88        try:
89            movie = self._index[indexName]
90        except KeyError:
91            Colors.debug("Queuing movie:", indexName, "(does not exist)")
92            return
93
94        self.prepare(movie, runMode, lockMode)
95        self._playlist.append((movie, runMode))
96        Colors.debug("Queuing movie:", indexName)
97
98    def playQue(self):
99        for movie, runMode in self._playlist:
100            self._play(movie, runMode)
101
102        self._playlist = []
103        Colors.debug("********* Playing que *********")
104
105    def insertMovie(self, indexName):
106        movie = []
107        self._index[indexName] = movie
108
109        return movie
110