Connected PlaylistView.itemDoubleClicked to a function that then tells the player to play that track.

This commit is contained in:
The Wobbler 2024-12-29 14:31:21 +01:00
parent 8fbb40d2f9
commit da27963884
5 changed files with 34 additions and 6 deletions

View file

@ -30,15 +30,21 @@ class Library:
playlist_view = self.create_playlist_view(playlist) playlist_view = self.create_playlist_view(playlist)
playlist_tabs.playlists[playlist.title] = playlist_view playlist_tabs.playlists[playlist.title] = playlist_view
playlist_tabs.addTab(playlist_view, playlist.title) playlist_tabs.addTab(playlist_view, playlist.title)
playlist_view.itemDoubleClicked.connect(self.on_track_doubleclick)
def create_playlist_view(self, playlist: Playlist): def create_playlist_view(self, playlist: Playlist):
view = PlaylistView() view = PlaylistView()
for track in playlist.tracks: for track in playlist.tracks:
track_item = QTreeWidgetItem(view) track_item = QTreeWidgetItem(view)
track_item.track = track
track_item.setText(1, track.tags.title) track_item.setText(1, track.tags.title)
track_item.setText(2, track.tags.artist) track_item.setText(2, track.tags.artist)
track_item.setText(3, track.tags.album) track_item.setText(3, track.tags.album)
return view return view
def on_track_doubleclick(self, item, column):
index = item.treeWidget().indexOfTopLevelItem(item)
self.app.player.play_track_in_playlist(index)

View file

@ -52,6 +52,15 @@ class Player:
self.track_progress.start() self.track_progress.start()
self.app.gui_communication.on_track_start() self.app.gui_communication.on_track_start()
def play_track_in_playlist(self, track_index):
self.stop()
self.current_sound, self.current_sound_duration = self.current_playlist.set_track(track_index)
self.play()
self.track_progress.start()
self.app.gui_communication.on_track_start()
def pause(self): def pause(self):
self.music_channel.pause() self.music_channel.pause()
self.track_progress.pause() self.track_progress.pause()

View file

@ -36,11 +36,14 @@ class Playlist:
print(lines) print(lines)
i = 0 i = 0
num_lines = len(lines)
while i < len(lines): while i < num_lines:
line = lines[i] line = lines[i]
if line.startswith("#") or line.startswith("http"): # filter out comments, extended m3u and urls if line.startswith("#") or line.startswith("http"): # filter out comments, extended m3u and urls
i += 1
continue continue
self.tracks.append(Track(self.app, line, cache=i==0)) # first track is cached self.tracks.append(Track(self.app, line, cache=i==0)) # first track is cached
@ -68,7 +71,6 @@ class Playlist:
if not self.current_track.cached: # make sure the track is cached because else the player can't play it if not self.current_track.cached: # make sure the track is cached because else the player can't play it
self.current_track.cache() self.current_track.cache()
self.current_track.cached = True
return self.current_track.sound, self.current_track.duration return self.current_track.sound, self.current_track.duration
@ -81,7 +83,16 @@ class Playlist:
if not self.current_track.cached: # make sure the track is cached because else the player can't play it if not self.current_track.cached: # make sure the track is cached because else the player can't play it
self.current_track.cache() self.current_track.cache()
self.current_track.cached = True
return self.current_track.sound, self.current_track.duration
def set_track(self, track_index):
self.current_track_index = track_index
self.current_track = self.tracks[self.current_track_index]
if not self.current_track.cached:
self.current_track.cache()
return self.current_track.sound, self.current_track.duration return self.current_track.sound, self.current_track.duration

View file

@ -15,15 +15,15 @@ class Track:
self.app = app self.app = app
self.path = path self.path = path
self.property_string = property_string self.property_string = property_string
self.cached = cache
self.tags = TinyTag.get(self.path) self.tags = TinyTag.get(self.path)
self.cached = False
self.audio = None self.audio = None
self.sound = None self.sound = None
self.duration = 0 self.duration = 0
if self.cached: if cache:
self.cache() self.cache()
def cache(self): def cache(self):
@ -36,6 +36,8 @@ class Track:
self.duration = len(self.audio) # track duration in milliseconds self.duration = len(self.audio) # track duration in milliseconds
self.cached = True
def remaining(self, position: int): def remaining(self, position: int):
remaining_audio = self.audio[position:] remaining_audio = self.audio[position:]

View file

@ -10,7 +10,7 @@ class MainWindow(QMainWindow):
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent) super().__init__(parent)
self.setWindowTitle("WoBuzz") self.setWindowTitle("Wobuzz")
self.menu_bar = self.menuBar() self.menu_bar = self.menuBar()