class MainPanel(gui.CorePanel): def __init__(self, parent, server): gui.CorePanel.__init__(self, parent) self.playlist = Playlist() self.plpath = "" self.server = server self.statusthread = None self.clock = -1 self.clockthread = None # Flag to store whether playlist is modified self.plmodded = False # Configure the playlist view self.lstPlaylist.InsertColumn(1, "Duration", width=60) self.lstPlaylist.InsertColumn(2, "Status", width=60) self.lstPlaylist.InsertColumn(0, "Filename", width=250) # Placeholder for networking self.networkconn = None def StatusHighlightSet(self): '''Run through playlist and highlight loaded and playing rows''' try: if self.networkconn.connected == True: playing = self.networkconn.playing loaded = self.networkconn.loaded else: playing = "" loaded = "" except AttributeError: loaded = "" playing = "" # Clear all markers (complicated to prevent flicker from unneeded updates) for i in range(0, self.lstPlaylist.GetItemCount()): if self.lstPlaylist.GetItemText(i) == playing: self.lstPlaylist.SetItemBackgroundColour(i, 'red') self.lstPlaylist.SetStringItem(i, 2, "Playing") elif self.lstPlaylist.GetItemText(i) == loaded: self.lstPlaylist.SetItemBackgroundColour(i, 'green') self.lstPlaylist.SetStringItem(i, 2, "Loaded") elif self.lstPlaylist.GetItemBackgroundColour(i) != 'white': self.lstPlaylist.SetItemBackgroundColour(i, 'white') self.lstPlaylist.SetStringItem(i, 2, "") def PlaylistRefresh(self): # Clear existing playlist items self.lstPlaylist.DeleteAllItems() # Add all items from list for filename, duration in self.playlist.playlist: count = self.lstPlaylist.GetItemCount() self.lstPlaylist.InsertStringItem(count, filename) self.lstPlaylist.SetStringItem(count, 1, str(duration)) self.lstPlaylist.SetStringItem(count, 2, "") # Set selected item if self.playlist.index >= 0: self.lstPlaylist.Select(self.playlist.index, True) # Configure playing/loaded markers self.StatusHighlightSet() def PollStatus(self): time = 9.9 while self.networkconn != None and self.networkconn.run == True: if time >= 10: self.networkconn.get_info() time = 0 else: sleep(0.5) time = time + 0.5 def UpdateCountdown(self): self.lblCountdown.Show(True) while self.clock > 0: # Update the text countdown m, s = divmod(self.clock, 60) self.lblCountdown.SetLabel("{0:02d}:{1:02d}".format(int(round(m)), int(round(s)))) # Set some colours on the clock if self.clock <= 10: self.lblCountdown.SetForegroundColour((255,0,0)) elif self.clock <= 30: self.lblCountdown.SetForegroundColour((255,99,71)) else: self.lblCountdown.SetForegroundColour((0, 0, 0)) # This aims to count whole seconds until video end x = self.clock % 1 if x < 0.2: x = x + 1 self.clock = self.clock - x sleep(x) # Clean up when timer runs out self.lblCountdown.Show(False) self.lblPlaying.SetLabel("None") self.clock = None self.clockthread = None # Handle an auto play if set if self.chkAuto.Value == True: self.networkconn.playing = self.networkconn.loaded self.OnPlayUpdateFromList() def ConnectionCallback(self): self.lblConnected.SetLabel("Connected to {0}".format(self.server)) self.statusthread = threading.Thread(target=self.PollStatus) self.statusthread.start() self.btnAdd.Enable(True) # Set the loaded item self.OnPLSelect(None) def DataCallback(self, seconds): if self.networkconn.playing != "": self.lblPlaying.SetLabel(self.networkconn.playing) self.btnStop.Enable(True) self.chkAuto.Enable(True) if seconds != None: self.clock = seconds if self.clockthread == None: self.clockthread = threading.Thread(target=self.UpdateCountdown) self.clockthread.start() if (self.chkAuto.Value == True and self.networkconn.loaded == "" and self.lblLoaded.GetLabelText() != 'Please Wait...'): self.OnPlayUpdateFromList() else: self.lblPlaying.SetLabel("None") self.lblCountdown.Show(False) self.btnStop.Enable(False) if self.clock != -1: self.clock = -1 self.clockthread = None if self.networkconn.loaded != "": self.lblLoaded.SetLabel(self.networkconn.loaded) self.btnPlay.Enable(True) else: self.lblLoaded.SetLabel("None") self.btnPlay.Enable(False) # Fix red/green highlights self.StatusHighlightSet() def OnMoveUp(self, event): if self.lstPlaylist.SelectedItemCount > 0: self.playlist.moveUp(self.lstPlaylist.GetFirstSelected()) self.PlaylistRefresh() self.plmodded = True def OnMoveDown(self, event): if self.lstPlaylist.SelectedItemCount > 0: self.playlist.moveDown(self.lstPlaylist.GetFirstSelected()) self.PlaylistRefresh() self.plmodded = True def OnRemove(self, event): if self.lstPlaylist.SelectedItemCount > 0: self.playlist.removeItem(self.lstPlaylist.GetFirstSelected()) self.PlaylistRefresh() self.plmodded = True def OnPLSelect(self, event): if self.lstPlaylist.ItemCount > 0: if self.lstPlaylist.SelectedItemCount > 0: self.playlist.index = self.lstPlaylist.GetFirstSelected() # Load a new item if we're connected try: if self.networkconn.connected == True: if self.networkconn.loaded != self.playlist.getCurrentItemName(): self.networkconn.load(self.playlist.getCurrentItemName()) self.lblLoaded.SetLabel("Please Wait...") self.btnPlay.Enable(False) self.chkAuto.SetLabel("Auto-play selected item") else: self.GetParent().OnDisconnectInstruction() raise AttributeError except AttributeError: pass else: if self.chkAuto.Value == False: self.playlist.index = -1 else: self.playlist.index = -1 def OnPlayUpdateFromList(self): if self.playlist.index >= len(self.playlist.playlist): self.playlist.index = -1 self.OnStop(None) return plitem, plduration = self.playlist.playlist[self.playlist.index] if self.networkconn.playing != plitem: for item, duration in self.playlist.playlist: if item == self.networkconn.playing: plitem = item plduration = duration if self.networkconn.playing == plitem: # Fix the clock self.clock = float(plduration) if self.clockthread == None: self.clockthread = threading.Thread(target=self.UpdateCountdown) self.clockthread.start() # Deselect current item self.lstPlaylist.Select(self.lstPlaylist.GetFirstSelected(), False) # Change the checkbox label to play selected self.chkAuto.SetLabel('Auto-play next item') # Load the next playlist item if requested if self.chkAuto.Value == True: item, duration = self.playlist.getNextItem() self.networkconn.load(item) self.lblLoaded.SetLabel('Please Wait...') self.btnPlay.Enable(False) # Update the status markers self.StatusHighlightSet() else: self.lblLoaded.SetLabel("None") self.btnPlay.Enable(False) def OnPlay(self, event): # Play the loaded item self.networkconn.play() # Update the labels self.lblPlaying.SetLabel(self.networkconn.playing) self.btnStop.Enable(True) self.chkAuto.Enable(True) # Update playing statuses from playlist self.OnPlayUpdateFromList() def OnStop(self, event): self.networkconn.stop() self.btnStop.Enable(False) self.lblPlaying.SetLabel("None") self.lblCountdown.Show(False) self.clock = -1 self.chkAuto.SetLabel('Auto-play next item') self.chkAuto.Value = False self.chkAuto.Enable(False) self.networkconn.setauto(False) self.StatusHighlightSet() def OnchkAutoChange(self, event): try: if self.networkconn.connected == True: self.networkconn.setauto(self.chkAuto.Value) if (self.networkconn.loaded == ""): # Load the next playlist item if requested item, duration = self.playlist.getNextItem() self.networkconn.load(item) self.lblLoaded.SetLabel('Please Wait...') self.btnPlay.Enable(False) else: self.GetParent().OnDisconnectInstruction() raise AttributeError except AttributeError: self.chkAuto.Enable(False) self.chkAuto.Value = False def OnAdd(self, event): dlgAdd = dlgAddItems(self) filelist = self.networkconn.getfilelist() for item, duration in filelist: dlgAdd.ddVideoList.Append(item) if dlgAdd.ShowModal() == wx.ID_OK: if self.lstPlaylist.SelectedItemCount > 0: newindex = self.playlist.index else: newindex = -1 for item, duration in filelist: if item == dlgAdd.ddVideoList.GetStringSelection(): self.playlist.addItem(item, int(round(duration)), ) self.PlaylistRefresh()
class MainPanel(gui.CorePanel): def __init__(self, parent, server): gui.CorePanel.__init__(self, parent) self.playlist = Playlist() self.plpath = "" self.server = server self.statusthread = None # Time in milliseconds since epoch when VT finishes self.endtime = -1 self.clockthread = None # Flag to store whether playlist is modified self.plmodded = False # Configure the playlist view self.lstPlaylist.InsertColumn(1, "Duration", width=60) self.lstPlaylist.InsertColumn(2, "Status", width=60) self.lstPlaylist.InsertColumn(0, "Filename", width=250) # Placeholder for networking self.networkconn = None def StatusHighlightSet(self): '''Run through playlist and highlight loaded and playing rows''' try: if self.networkconn.connected == True: playing = self.networkconn.playing loaded = self.networkconn.loaded else: playing = "" loaded = "" for i in range(0, self.lstPlaylist.GetItemCount()): if self.lstPlaylist.GetItemBackgroundColour(i) != 'white': self.lstPlaylist.SetItemBackgroundColour(i, 'white') self.lstPlaylist.SetStringItem(i, 2, "") except AttributeError: loaded = "" playing = "" for i in range(0, self.lstPlaylist.GetItemCount()): if self.lstPlaylist.GetItemBackgroundColour(i) != 'white': self.lstPlaylist.SetItemBackgroundColour(i, 'white') self.lstPlaylist.SetStringItem(i, 2, "") # Clear all markers (complicated to prevent flicker from unneeded updates) for i in range(0, self.lstPlaylist.GetItemCount()): if self.lstPlaylist.GetItemText(i) == playing: self.lstPlaylist.SetItemBackgroundColour(i, 'red') self.lstPlaylist.SetStringItem(i, 2, "Playing") elif self.lstPlaylist.GetItemText(i) == loaded: self.lstPlaylist.SetItemBackgroundColour(i, 'green') self.lstPlaylist.SetStringItem(i, 2, "Loaded") elif self.lstPlaylist.GetItemBackgroundColour(i) != 'white': self.lstPlaylist.SetItemBackgroundColour(i, 'white') self.lstPlaylist.SetStringItem(i, 2, "") def PlaylistRefresh(self): # Clear existing playlist items self.lstPlaylist.DeleteAllItems() # Add all items from list for filename, duration in self.playlist.playlist: count = self.lstPlaylist.GetItemCount() self.lstPlaylist.InsertStringItem(count, filename) self.lstPlaylist.SetStringItem(count, 1, str(duration)) self.lstPlaylist.SetStringItem(count, 2, "") # Set selected item if self.playlist.index >= 0: self.lstPlaylist.Select(self.playlist.index, True) # Configure playing/loaded markers self.StatusHighlightSet() def PollStatus(self): time = 9.9 while self.networkconn != None and self.networkconn.run == True: if time >= 10: self.networkconn.get_info() time = 0 else: sleep(0.5) time = time + 0.5 def UpdateCountdown(self): self.lblCountdown.Show(True) dt = datetime.now() remtime = self.endtime - (mktime(dt.timetuple()) + dt.microsecond/1000000.0) while self.endtime != None: # Update the text countdown m, s = divmod(round(remtime), 60) self.lblCountdown.SetLabel("{0:02d}:{1:02d}".format(int(round(m)), int(round(s)))) # Set some colours on the clock if remtime <= 10: self.lblCountdown.SetForegroundColour((255,0,0)) elif remtime <= 30: self.lblCountdown.SetForegroundColour((255,99,71)) else: self.lblCountdown.SetForegroundColour((0, 0, 0)) # Calculate remaining time in seconds dt = datetime.now() try: remtime = self.endtime - (mktime(dt.timetuple()) + dt.microsecond/1000000.0) except TypeError: break if (remtime <= 0): break # Sleep a while, don't hammer the CPU sleep(0.1) # Clean up when timer runs out self.lblCountdown.Show(False) self.lblPlaying.SetLabel("None") self.endtime = None self.clockthread = None def ConnectionCallback(self): self.lblConnected.SetLabel("Connected to {0}".format(self.server)) self.statusthread = threading.Thread(target=self.PollStatus) self.statusthread.start() self.btnAdd.Enable(True) # Set the loaded item self.OnPLSelect(None) def DataCallback(self, seconds, changeover=False): """Handle updated data from server. Set changeover true to auto-advance""" if self.networkconn.playing != "": self.lblPlaying.SetLabel(self.networkconn.playing) self.btnStop.Enable(True) self.chkAuto.Enable(True) if seconds != None: self.endtime = calculate_end(seconds) if self.clockthread == None: self.clockthread = threading.Thread(target=self.UpdateCountdown) self.clockthread.start() if (self.chkAuto.Value == True and changeover == True): self.OnPlayUpdateFromList() else: self.lblPlaying.SetLabel("None") self.lblCountdown.Show(False) self.btnStop.Enable(False) if self.endtime != None: self.endtime = None self.clockthread = None if self.networkconn.loaded != "": self.lblLoaded.SetLabel(self.networkconn.loaded) self.btnPlay.Enable(True) if (self.networkconn.playing == ""): m, s = divmod(round(seconds), 60) self.lblCountdown.SetLabel("{0:02d}:{1:02d}".format(int(round(m)), int(round(s)))) self.lblCountdown.SetForegroundColour((0, 0, 0)) self.lblCountdown.Show(True) else: self.lblLoaded.SetLabel("None") self.btnPlay.Enable(False) # Fix red/green highlights self.StatusHighlightSet() def OnMoveUp(self, event): if self.lstPlaylist.SelectedItemCount > 0: self.playlist.moveUp(self.lstPlaylist.GetFirstSelected()) self.PlaylistRefresh() self.plmodded = True def OnMoveDown(self, event): if self.lstPlaylist.SelectedItemCount > 0: self.playlist.moveDown(self.lstPlaylist.GetFirstSelected()) self.PlaylistRefresh() self.plmodded = True def OnRemove(self, event): if self.lstPlaylist.SelectedItemCount > 0: self.playlist.removeItem(self.lstPlaylist.GetFirstSelected()) self.PlaylistRefresh() self.plmodded = True def OnPLSelect(self, event): if self.lstPlaylist.ItemCount > 0: if self.lstPlaylist.SelectedItemCount > 0: self.playlist.index = self.lstPlaylist.GetFirstSelected() # Load a new item if we're connected try: if self.networkconn.connected == True: if self.networkconn.loaded != self.playlist.getCurrentItemName(): self.networkconn.load(self.playlist.getCurrentItemName()) self.lblLoaded.SetLabel("Please Wait...") self.btnPlay.Enable(False) self.chkAuto.SetLabel("Auto-play selected item") else: self.GetParent().OnDisconnectInstruction() raise AttributeError except AttributeError: pass else: if self.chkAuto.Value == False: self.playlist.index = -1 else: self.playlist.index = -1 def OnPlayUpdateFromList(self): if self.playlist.index >= len(self.playlist.playlist): self.playlist.index = -1 self.OnStop(None) return plitem, plduration = self.playlist.playlist[self.playlist.index] if self.networkconn.playing != plitem: for item, duration in self.playlist.playlist: if item == self.networkconn.playing: plitem = item plduration = duration if self.networkconn.playing == plitem: # Deselect current item self.lstPlaylist.Select(self.lstPlaylist.GetFirstSelected(), False) # Change the checkbox label to play selected self.chkAuto.SetLabel('Auto-play next item') # Update the status markers self.StatusHighlightSet() # Load next if needed if (self.chkAuto.Value == True and self.networkconn.loaded == ""): item, duration = self.playlist.getNextItem(advance=True) self.networkconn.load(item) self.lblLoaded.SetLabel('Please Wait...') self.btnPlay.Enable(False) else: self.lblLoaded.SetLabel("None") self.btnPlay.Enable(False) def OnPlay(self, event): # Play the loaded item self.networkconn.play() # Update the labels self.lblPlaying.SetLabel(self.networkconn.playing) self.btnStop.Enable(True) self.chkAuto.Enable(True) # Make sure auto state cleared on server self.networkconn.setauto(False) # Update playing statuses from playlist self.OnPlayUpdateFromList() def OnStop(self, event): self.networkconn.stop() self.btnStop.Enable(False) self.lblPlaying.SetLabel("None") self.lblCountdown.Show(False) self.endtime = None self.chkAuto.SetLabel('Auto-play next item') self.chkAuto.SetValue(False) self.chkAuto.Enable(False) self.networkconn.setauto(False) self.StatusHighlightSet() def OnchkAutoChange(self, event): try: if self.networkconn.connected == True: self.networkconn.setauto(self.chkAuto.Value) if (self.chkAuto.Value == True): # Set current selection in playlist if not set already if (self.playlist.index == -1): if (self.networkconn.playing != ""): for i in range(0, len(self.playlist.playlist)): item, duration = self.playlist.playlist[i] if item == self.networkconn.playing: self.playlist.index = i break if (self.networkconn.loaded == ""): # Load the next playlist item if requested item, duration = self.playlist.getNextItem(advance=True) self.networkconn.load(item) self.lblLoaded.SetLabel('Please Wait...') self.btnPlay.Enable(False) else: self.GetParent().OnDisconnectInstruction() raise AttributeError except AttributeError: self.chkAuto.Enable(False) self.chkAuto.Value = False def OnAdd(self, event): dlgAdd = dlgAddItems(self) filelist = self.networkconn.getfilelist() if (filelist == None): wx.MessageBox("Error getting file list, try again later") return for item, duration in filelist: dlgAdd.ddVideoList.Append(item) if dlgAdd.ShowModal() == wx.ID_OK: if self.lstPlaylist.SelectedItemCount > 0: newindex = self.playlist.index else: newindex = -1 for item, duration in filelist: if item == dlgAdd.ddVideoList.GetStringSelection(): self.playlist.addItem(item, int(round(duration)), ) self.PlaylistRefresh()