OOPed the way playlist views are created.
This commit is contained in:
parent
88b846f3b6
commit
028c38b1b6
7 changed files with 61 additions and 67 deletions
|
@ -24,7 +24,7 @@ def main():
|
||||||
app.library.temporary_playlist.load_from_paths(arguments.track)
|
app.library.temporary_playlist.load_from_paths(arguments.track)
|
||||||
|
|
||||||
app.player.current_playlist = app.library.temporary_playlist
|
app.player.current_playlist = app.library.temporary_playlist
|
||||||
app.library.create_playlist_views()
|
app.library.load_playlist_views()
|
||||||
|
|
||||||
sys.exit(app.qt_app.exec())
|
sys.exit(app.qt_app.exec())
|
||||||
|
|
||||||
|
|
|
@ -40,5 +40,5 @@ class GUI:
|
||||||
|
|
||||||
def on_track_change(self, previous_track, track):
|
def on_track_change(self, previous_track, track):
|
||||||
self.track_control.on_track_change(previous_track, track)
|
self.track_control.on_track_change(previous_track, track)
|
||||||
self.app.library.on_track_change(previous_track, track)
|
self.app.player.current_playlist.view.on_track_change(previous_track, track)
|
||||||
|
|
||||||
|
|
|
@ -22,72 +22,13 @@ class Library:
|
||||||
self.temporary_playlist = Playlist(self.app, "Temporary Playlist")
|
self.temporary_playlist = Playlist(self.app, "Temporary Playlist")
|
||||||
self.playlists = [self.temporary_playlist]
|
self.playlists = [self.temporary_playlist]
|
||||||
|
|
||||||
self.play_icon = QIcon.fromTheme(QIcon.ThemeIcon.MediaPlaybackStart)
|
def load_playlist_views(self):
|
||||||
|
|
||||||
def create_playlist_views(self):
|
|
||||||
for library_dock in self.library_docks:
|
for library_dock in self.library_docks:
|
||||||
playlist_tabs: QTabWidget = library_dock.library.playlist_tabs
|
playlist_tabs: QTabWidget = library_dock.library.playlist_tabs
|
||||||
|
|
||||||
playlist_tabs.playlists = {}
|
playlist_tabs.playlists = {}
|
||||||
|
|
||||||
for playlist in self.playlists:
|
for playlist in self.playlists:
|
||||||
playlist_view = self.create_playlist_view(playlist)
|
playlist_view = PlaylistView(playlist)
|
||||||
playlist_tabs.playlists[playlist.title] = playlist_view
|
|
||||||
playlist_tabs.addTab(playlist_view, playlist.title)
|
playlist_tabs.addTab(playlist_view, playlist.title)
|
||||||
|
|
||||||
def create_playlist_view(self, playlist: Playlist):
|
|
||||||
view = PlaylistView(playlist)
|
|
||||||
|
|
||||||
view.itemActivated.connect(self.on_track_activation)
|
|
||||||
|
|
||||||
num_tracks = len(playlist.tracks)
|
|
||||||
|
|
||||||
i = 0
|
|
||||||
|
|
||||||
while i < num_tracks:
|
|
||||||
track = playlist.tracks[i]
|
|
||||||
|
|
||||||
track_item = QTreeWidgetItem(view)
|
|
||||||
|
|
||||||
track_item.setFlags(
|
|
||||||
Qt.ItemFlag.ItemIsEnabled |
|
|
||||||
Qt.ItemFlag.ItemIsSelectable |
|
|
||||||
Qt.ItemFlag.ItemIsDragEnabled
|
|
||||||
)
|
|
||||||
|
|
||||||
track_item.track = track
|
|
||||||
track_item.setText(2, track.tags.title)
|
|
||||||
track_item.setText(3, track.tags.artist)
|
|
||||||
track_item.setText(4, track.tags.album)
|
|
||||||
track_item.setText(5, str(i + 1))
|
|
||||||
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
return view
|
|
||||||
|
|
||||||
def on_track_activation(self, item, column):
|
|
||||||
index = item.treeWidget().indexOfTopLevelItem(item)
|
|
||||||
self.app.player.play_track_in_playlist(index)
|
|
||||||
|
|
||||||
def get_track_tree_item(self, track, playlist_view: PlaylistView):
|
|
||||||
index = self.app.player.current_playlist.tracks.index(track)
|
|
||||||
item = playlist_view.topLevelItem(index)
|
|
||||||
|
|
||||||
return item
|
|
||||||
|
|
||||||
def get_playing_playlist_view(self):
|
|
||||||
view = self.main_library_dock.library.playlist_tabs.playlists[self.app.player.current_playlist.title]
|
|
||||||
|
|
||||||
return view
|
|
||||||
|
|
||||||
def on_track_change(self, previous_track, track):
|
|
||||||
playing_playlist_view = self.get_playing_playlist_view()
|
|
||||||
|
|
||||||
if previous_track:
|
|
||||||
item = self.get_track_tree_item(previous_track, playing_playlist_view)
|
|
||||||
item.setIcon(0, QIcon(None))
|
|
||||||
|
|
||||||
if track:
|
|
||||||
item = self.get_track_tree_item(track, playing_playlist_view)
|
|
||||||
item.setIcon(0, self.play_icon)
|
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ class Playlist:
|
||||||
self.tracks: list[Track] = []
|
self.tracks: list[Track] = []
|
||||||
self.current_track_index = 0
|
self.current_track_index = 0
|
||||||
self.current_track: Track | None = None
|
self.current_track: Track | None = None
|
||||||
|
self.view = None
|
||||||
|
|
||||||
def load_from_paths(self, paths):
|
def load_from_paths(self, paths):
|
||||||
i = 0
|
i = 0
|
||||||
|
@ -21,7 +22,7 @@ class Playlist:
|
||||||
path = paths[i]
|
path = paths[i]
|
||||||
|
|
||||||
if os.path.isfile(path):
|
if os.path.isfile(path):
|
||||||
self.tracks.append(Track(self.app, path, cache=i==0)) # first track is cached
|
self.tracks.append(Track(self.app, i, path, cache=i==0)) # first track is cached
|
||||||
|
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
|
@ -47,7 +48,7 @@ class Playlist:
|
||||||
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
self.tracks.append(Track(self.app, line, cache=i==0)) # first track is cached
|
self.tracks.append(Track(self.app, i, line, cache=i==0)) # first track is cached
|
||||||
|
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,9 @@ class Track:
|
||||||
Class containing data for a track like file path, raw data...
|
Class containing data for a track like file path, raw data...
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, app, path: str, property_string: str=None, cache: bool=False):
|
def __init__(self, app, index: int, path: str, property_string: str=None, cache: bool=False):
|
||||||
self.app = app
|
self.app = app
|
||||||
|
self.index_custom_sort = index
|
||||||
self.path = path
|
self.path = path
|
||||||
self.property_string = property_string
|
self.property_string = property_string
|
||||||
|
|
||||||
|
@ -30,6 +31,8 @@ class Track:
|
||||||
self.sound = None
|
self.sound = None
|
||||||
self.duration = 0
|
self.duration = 0
|
||||||
|
|
||||||
|
self.item = None
|
||||||
|
|
||||||
if cache:
|
if cache:
|
||||||
self.cache()
|
self.cache()
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
from PyQt6.QtCore import pyqtSignal
|
from PyQt6.QtCore import pyqtSignal
|
||||||
from PyQt6.QtGui import QDropEvent
|
from PyQt6.QtGui import QDropEvent, QIcon
|
||||||
from PyQt6.QtWidgets import QTreeWidget, QAbstractItemView
|
from PyQt6.QtWidgets import QTreeWidget, QAbstractItemView
|
||||||
|
from .track import TrackItem
|
||||||
|
|
||||||
|
|
||||||
class PlaylistView(QTreeWidget):
|
class PlaylistView(QTreeWidget):
|
||||||
|
@ -12,12 +13,17 @@ class PlaylistView(QTreeWidget):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
|
|
||||||
self.playlist = playlist
|
self.playlist = playlist
|
||||||
|
self.app = playlist.app
|
||||||
|
|
||||||
|
playlist.view = self
|
||||||
|
|
||||||
self.setDragDropMode(QAbstractItemView.DragDropMode.InternalMove)
|
self.setDragDropMode(QAbstractItemView.DragDropMode.InternalMove)
|
||||||
self.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection)
|
self.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection)
|
||||||
|
|
||||||
self.setColumnCount(4)
|
self.setColumnCount(4)
|
||||||
|
|
||||||
|
self.play_icon = QIcon.fromTheme(QIcon.ThemeIcon.MediaPlaybackStart)
|
||||||
|
|
||||||
headers = [
|
headers = [
|
||||||
"",
|
"",
|
||||||
"#",
|
"#",
|
||||||
|
@ -29,6 +35,10 @@ class PlaylistView(QTreeWidget):
|
||||||
|
|
||||||
self.setHeaderLabels(headers)
|
self.setHeaderLabels(headers)
|
||||||
|
|
||||||
|
self.load_tracks()
|
||||||
|
|
||||||
|
self.itemActivated.connect(self.on_track_activation)
|
||||||
|
|
||||||
def update_track_numbers(self):
|
def update_track_numbers(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -41,3 +51,19 @@ class PlaylistView(QTreeWidget):
|
||||||
|
|
||||||
event.accept()
|
event.accept()
|
||||||
|
|
||||||
|
def load_tracks(self):
|
||||||
|
for track in self.playlist.tracks:
|
||||||
|
track_item = TrackItem(track, self)
|
||||||
|
|
||||||
|
def on_track_activation(self, item, column):
|
||||||
|
index = self.indexOfTopLevelItem(item)
|
||||||
|
self.app.player.play_track_in_playlist(index)
|
||||||
|
|
||||||
|
def on_track_change(self, previous_track, track):
|
||||||
|
# unmark the previous track and mark the current track as playing
|
||||||
|
if previous_track:
|
||||||
|
previous_track.item.setIcon(0, QIcon(None))
|
||||||
|
|
||||||
|
if track:
|
||||||
|
track.item.setIcon(0, self.play_icon)
|
||||||
|
|
||||||
|
|
23
wobuzz/ui/track.py
Normal file
23
wobuzz/ui/track.py
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
from PyQt6.QtCore import Qt
|
||||||
|
from PyQt6.QtWidgets import QTreeWidgetItem
|
||||||
|
|
||||||
|
|
||||||
|
class TrackItem(QTreeWidgetItem):
|
||||||
|
def __init__(self, track, parent=None):
|
||||||
|
super().__init__(parent)
|
||||||
|
|
||||||
|
self.track = track
|
||||||
|
track.item = self
|
||||||
|
|
||||||
|
self.setFlags(
|
||||||
|
Qt.ItemFlag.ItemIsEnabled |
|
||||||
|
Qt.ItemFlag.ItemIsSelectable |
|
||||||
|
Qt.ItemFlag.ItemIsDragEnabled
|
||||||
|
)
|
||||||
|
|
||||||
|
self.setText(2, track.tags.title)
|
||||||
|
self.setText(3, track.tags.artist)
|
||||||
|
self.setText(4, track.tags.album)
|
||||||
|
self.setText(5, str(track.index_custom_sort))
|
Loading…
Add table
Reference in a new issue