Fixed some bugs that happened when tracks got rearranged.

This commit is contained in:
The Wobbler 2025-01-26 16:49:09 +01:00
parent b2bd8ef784
commit 519b2d0adb
10 changed files with 72 additions and 17 deletions

View file

@ -38,8 +38,6 @@ def main():
app.library.temporary_playlist.load_from_paths(tracks) app.library.temporary_playlist.load_from_paths(tracks)
app.library.temporary_playlist.view.load_tracks() app.library.temporary_playlist.view.load_tracks()
app.player.current_playlist = app.library.temporary_playlist
sys.exit(app.qt_app.exec()) sys.exit(app.qt_app.exec())

View file

@ -11,6 +11,8 @@ class GUI:
self.dropped = [] self.dropped = []
self.clicked_playlist = self.app.library.temporary_playlist
self.window = MainWindow(app) self.window = MainWindow(app)
self.settings = self.window.settings self.settings = self.window.settings
self.track_control = self.window.track_control self.track_control = self.window.track_control

View file

@ -17,8 +17,8 @@ class Player:
self.track_progress = TrackProgress(self.app) self.track_progress = TrackProgress(self.app)
self.current_playlist = Playlist(self.app, "None")
self.history = Playlist(self.app, "History") self.history = Playlist(self.app, "History")
self.current_playlist = Playlist(self.app, "None")
self.playing = False self.playing = False
self.paused = False self.paused = False
@ -65,7 +65,7 @@ class Player:
self.history.append_track(self.current_playlist.current_track) self.history.append_track(self.current_playlist.current_track)
self.app.gui.on_track_change(None, self.current_playlist.current_track) self.app.gui.on_track_change(self.history.h_last_track(), self.current_playlist.current_track)
self.app.gui.track_control.on_playstate_update() self.app.gui.track_control.on_playstate_update()
@ -133,7 +133,9 @@ class Player:
def stop(self): def stop(self):
self.music_channel.stop() self.music_channel.stop()
self.track_progress.stop() self.track_progress.stop()
self.current_sound_duration = self.current_playlist.current_track.duration
if not self.current_playlist.current_track is None:
self.current_sound_duration = self.current_playlist.current_track.duration
self.playing = False self.playing = False
self.paused = False self.paused = False

View file

@ -63,6 +63,8 @@ class Playlist:
if self.current_track is None and self.has_tracks(): if self.current_track is None and self.has_tracks():
self.current_track = self.tracks[0] self.current_track = self.tracks[0]
#self.app.player.history.append_track(self.current_track)
def load_from_wbz(self, path): def load_from_wbz(self, path):
pass pass

View file

@ -30,11 +30,40 @@ class Track:
self.sound = None self.sound = None
self.duration = 0 self.duration = 0
self.item = None self.items = []
self.occurrences = {} # all occurrences in playlists categorized by playlist and track widget
if cache: if cache:
self.cache() self.cache()
def set_occurrences(self):
# set track item for every occurrence of track in a playlist
new_occurrences = {}
for item in self.items:
playlist_occurrences = new_occurrences.get(item.playlist, {})
playlist_occurrences[id(item)] = item.index
new_occurrences[item.playlist] = playlist_occurrences
self.correct_occurrences(new_occurrences)
self.occurrences = new_occurrences
def correct_occurrences(self, new_occurrences):
"""
If this track is the currently playing track, and it gets moved, this corrects the current playlist index.
"""
if self.app.player.current_playlist.current_track is self:
for item in self.items:
if (
item.playlist in self.occurrences and
self.occurrences[item.playlist][id(item)] == self.app.player.current_playlist.current_track_index
):
self.app.player.current_playlist.set_track(new_occurrences[item.playlist][id(item)])
def cache(self): def cache(self):
self.load_audio() self.load_audio()
# audio = normalize(audio) # audio = normalize(audio)

View file

@ -29,4 +29,6 @@ class TrackProgress:
def stop(self): def stop(self):
self.timer.stop() self.timer.stop()
self.remaining_time = self.app.player.current_playlist.current_track.duration
if not self.app.player.current_playlist.current_track is None:
self.remaining_time = self.app.player.current_playlist.current_track.duration

View file

@ -50,13 +50,18 @@ class PlaylistView(QTreeWidget):
track = track_item.track track = track_item.track
track_item.index_user_sort = i track_item.index_user_sort = i
track_item.index = i
track_item.setText(5, str(i + 1)) track_item.setText(5, str(i + 1))
self.playlist.tracks[i] = track self.playlist.tracks[i] = track
track.set_occurrences()
i += 1 i += 1
self.app.player.cache_next_track() if self.app.player.current_playlist.has_tracks():
self.app.player.cache_next_track()
def dropEvent(self, event: QDropEvent): def dropEvent(self, event: QDropEvent):
# receive items that were dropped and create new items from its tracks (new items bc. widgets can only have # receive items that were dropped and create new items from its tracks (new items bc. widgets can only have
@ -105,9 +110,6 @@ class PlaylistView(QTreeWidget):
i += 1 i += 1
if not self.topLevelItemCount() == 0:
self.topLevelItem(0).setIcon(0, self.playing_mark)
def on_track_activation(self, item, column): def on_track_activation(self, item, column):
if not self.app.player.current_playlist == self.playlist: if not self.app.player.current_playlist == self.playlist:
self.app.player.current_playlist = self.playlist self.app.player.current_playlist = self.playlist
@ -117,14 +119,16 @@ class PlaylistView(QTreeWidget):
def on_track_change(self, previous_track, track): def on_track_change(self, previous_track, track):
# remove playing mark from first track bc it may not be in the history # remove playing mark from first track bc it may not be in the history
self.topLevelItem(0).setIcon(0, QIcon(None)) #self.topLevelItem(0).setIcon(0, QIcon(None))
# unmark the previous track and mark the current track as playing # unmark the previous track and mark the current track as playing
if previous_track: if previous_track:
previous_track.item.setIcon(0, QIcon(None)) for item in previous_track.items:
item.setIcon(0, QIcon(None))
if track: if track:
track.item.setIcon(0, self.playing_mark) item = self.topLevelItem(self.app.player.current_playlist.current_track_index)
item.setIcon(0, self.playing_mark)
def append_track(self, track): def append_track(self, track):
TrackItem(track, self.topLevelItemCount() - 1, self) TrackItem(track, self.topLevelItemCount() - 1, self)

View file

@ -24,14 +24,21 @@ class PlaylistTabBar(QTabBar):
self.setAcceptDrops(True) self.setAcceptDrops(True)
self.tabBarClicked.connect(self.on_click)
self.tabBarDoubleClicked.connect(self.on_doubleclick)
def dragEnterEvent(self, event: QDragEnterEvent): def dragEnterEvent(self, event: QDragEnterEvent):
index = self.tabAt(event.position().toPoint()) index = self.tabAt(event.position().toPoint())
self.tab_widget.setCurrentIndex(index) self.tab_widget.setCurrentIndex(index)
def mouseDoubleClickEvent(self, event: QMouseEvent): def on_click(self, index):
index = self.tabAt(event.position().toPoint()) playlist_view = self.tab_widget.widget(index)
playlist = playlist_view.playlist
self.app.gui.clicked_playlist = playlist
def on_doubleclick(self, index):
playlist_view = self.tab_widget.widget(index) playlist_view = self.tab_widget.widget(index)
playlist = playlist_view.playlist playlist = playlist_view.playlist

View file

@ -11,7 +11,13 @@ class TrackItem(QTreeWidgetItem):
self.track = track self.track = track
self.index_user_sort = index self.index_user_sort = index
track.item = self self.index = index
self.playlist = parent.playlist
track.items.append(self)
track.set_occurrences()
self.setFlags( self.setFlags(
Qt.ItemFlag.ItemIsEnabled | Qt.ItemFlag.ItemIsEnabled |

View file

@ -83,6 +83,9 @@ class TrackControl(QToolBar):
elif self.app.player.current_playlist.has_tracks(): # stopped but tracks in the current playlist elif self.app.player.current_playlist.has_tracks(): # stopped but tracks in the current playlist
self.app.player.start_playing() self.app.player.start_playing()
elif self.app.player.current_playlist.title == "None":
self.app.player.start_playlist(self.app.gui.clicked_playlist)
def on_playstate_update(self): def on_playstate_update(self):
if self.app.player.playing: if self.app.player.playing:
if self.app.player.paused: if self.app.player.paused: