Move to top when shuffling + avoid random index switch bug

This commit is contained in:
Et0h 2016-01-29 15:30:21 +00:00
parent 85ce40bdbd
commit 3b6f8fd414
3 changed files with 52 additions and 18 deletions

View File

@ -1255,8 +1255,8 @@ class UiManager(object):
self.lastSecondaryOSDEndTime = None self.lastSecondaryOSDEndTime = None
self.lastError = "" self.lastError = ""
def setPlaylist(self, newPlaylist): def setPlaylist(self, newPlaylist, newIndexFilename=None):
self.__ui.setPlaylist(newPlaylist) self.__ui.setPlaylist(newPlaylist, newIndexFilename)
def setPlaylistIndexFilename(self, filename): def setPlaylistIndexFilename(self, filename):
self.__ui.setPlaylistIndexFilename(filename) self.__ui.setPlaylistIndexFilename(filename)
@ -1349,11 +1349,11 @@ class SyncplayPlaylist():
def changeToPlaylistIndex(self, index, username = None): def changeToPlaylistIndex(self, index, username = None):
if not self._playlist or len(self._playlist) == 0: if not self._playlist or len(self._playlist) == 0:
return return
path = None
if index is None: if index is None:
return return
if username is None and not self._client.sharedPlaylistIsEnabled(): if username is None and not self._client.sharedPlaylistIsEnabled():
return return
self._playlistIndex = index
try: try:
filename = self._playlist[index] filename = self._playlist[index]
self._ui.setPlaylistIndexFilename(filename) self._ui.setPlaylistIndexFilename(filename)
@ -1401,22 +1401,53 @@ class SyncplayPlaylist():
except IndexError: except IndexError:
self._ui.showDebugMessage("Could not change playlist index due to IndexError") self._ui.showDebugMessage("Could not change playlist index due to IndexError")
def changePlaylist(self, files, username = None): def _getValidIndexFromNewPlaylist(self, newPlaylist=None):
try: if self._playlistIndex is None or not newPlaylist or len(newPlaylist) <= 1:
filename = self._playlist[self._playlistIndex] return 0
newIndex = files.index(filename)
except: i = self._playlistIndex
while i <= len(self._playlist):
try:
filename = self._playlist[i]
validIndex = newPlaylist.index(filename)
return validIndex
except:
i += 1
i = self._playlistIndex
while i > 0:
try:
filename = self._playlist[i]
validIndex = newPlaylist.index(filename)
return validIndex+1 if validIndex < len(newPlaylist)-1 else validIndex
except:
i -= 1
return 0
def _getFilenameFromIndexInGivenPlaylist(self, _playlist, _index):
if not _index or not _playlist:
return None
filename = _playlist[_index] if len(_playlist) > _index else None
return filename
def changePlaylist(self, files, username = None, resetIndex=False):
if resetIndex:
newIndex = 0 newIndex = 0
filename = files[0] if files and len(files) > 0 else None
else:
newIndex = self._getValidIndexFromNewPlaylist(files)
filename = self._getFilenameFromIndexInGivenPlaylist(files, newIndex)
self._updateUndoPlaylistBuffer(newPlaylist=files, newRoom=self._client.userlist.currentUser.room) self._updateUndoPlaylistBuffer(newPlaylist=files, newRoom=self._client.userlist.currentUser.room)
self._playlist = files self._playlist = files
if username is None and self._client.isConnectedAndInARoom(): if username is None:
if self._client.sharedPlaylistIsEnabled(): if self._client.isConnectedAndInARoom() and self._client.sharedPlaylistIsEnabled():
self._client._protocol.setPlaylist(files) self._client._protocol.setPlaylist(files)
self.changeToPlaylistIndex(newIndex) self.changeToPlaylistIndex(newIndex)
self._ui.setPlaylist(self._playlist, filename)
else: else:
self._ui.setPlaylist(self._playlist) self._ui.setPlaylist(self._playlist, filename)
self.changeToPlaylistIndex(newIndex, username) self.changeToPlaylistIndex(newIndex, username)
self._ui.showMessage(getMessage("playlist-contents-changed-notification").format(username)) self._ui.showMessage(getMessage("playlist-contents-changed-notification").format(username))
@ -1424,16 +1455,14 @@ class SyncplayPlaylist():
def undoPlaylistChange(self): def undoPlaylistChange(self):
if self.canUndoPlaylist(self._playlist): if self.canUndoPlaylist(self._playlist):
newPlaylist = self._getPreviousPlaylist() newPlaylist = self._getPreviousPlaylist()
self._ui.setPlaylist(newPlaylist) self.changePlaylist(newPlaylist, username=None)
self.changePlaylist(newPlaylist)
@needsSharedPlaylistsEnabled @needsSharedPlaylistsEnabled
def shufflePlaylist(self): def shufflePlaylist(self):
if self._playlist and len(self._playlist) > 0: if self._playlist and len(self._playlist) > 0:
shuffledPlaylist = deepcopy(self._playlist) shuffledPlaylist = deepcopy(self._playlist)
random.shuffle(shuffledPlaylist) random.shuffle(shuffledPlaylist)
self._ui.setPlaylist(shuffledPlaylist) self.changePlaylist(shuffledPlaylist, username=None, resetIndex=True)
self.changePlaylist(shuffledPlaylist)
def canUndoPlaylist(self, currentPlaylist): def canUndoPlaylist(self, currentPlaylist):
return self._previousPlaylist is not None and currentPlaylist <> self._previousPlaylist return self._previousPlaylist is not None and currentPlaylist <> self._previousPlaylist

View File

@ -23,7 +23,7 @@ class ConsoleUI(threading.Thread):
def drop(self): def drop(self):
pass pass
def setPlaylist(self, newPlaylist): def setPlaylist(self, newPlaylist, newIndexFilename=None):
pass pass
def setPlaylistIndexFilename(self, filename): def setPlaylistIndexFilename(self, filename):

View File

@ -175,7 +175,7 @@ class MainWindow(QtGui.QMainWindow):
def setPlaylistIndexFilename(self, filename): def setPlaylistIndexFilename(self, filename):
if filename <> self.playlistIndexFilename: if filename <> self.playlistIndexFilename:
self.playlistIndexFilename = filename self.playlistIndexFilename = filename
self.updatePlaylistIndexIcon() self.updatePlaylistIndexIcon()
def updatePlaylistIndexIcon(self): def updatePlaylistIndexIcon(self):
for item in xrange(self.count()): for item in xrange(self.count()):
@ -1413,14 +1413,19 @@ class MainWindow(QtGui.QMainWindow):
self._syncplayClient._player.openFile(dropfilepath, resetPosition=True) self._syncplayClient._player.openFile(dropfilepath, resetPosition=True)
self._syncplayClient.setPosition(0) self._syncplayClient.setPosition(0)
def setPlaylist(self, newPlaylist): def setPlaylist(self, newPlaylist, newIndexFilename=None):
if self.updatingPlaylist:
self.ui.showDebugMessage("Trying to set playlist while it is already being updated")
if newPlaylist == self.playlistState: if newPlaylist == self.playlistState:
self.playlist.setPlaylistIndexFilename(newIndexFilename)
return return
self.updatingPlaylist = True self.updatingPlaylist = True
if newPlaylist and len(newPlaylist) > 0: if newPlaylist and len(newPlaylist) > 0:
self.clearedPlaylistNote = True self.clearedPlaylistNote = True
self.playlistState = newPlaylist self.playlistState = newPlaylist
self.playlist.updatePlaylist(newPlaylist) self.playlist.updatePlaylist(newPlaylist)
if newIndexFilename:
self.playlist.setPlaylistIndexFilename(newIndexFilename)
self.updatingPlaylist = False self.updatingPlaylist = False
def setPlaylistIndexFilename(self, filename): def setPlaylistIndexFilename(self, filename):