From af2b7b6c8d5fc3f5d796914ee9a73387521376b8 Mon Sep 17 00:00:00 2001 From: wobbl Date: Sat, 28 Dec 2024 20:41:18 +0100 Subject: [PATCH] Implemented displaying of tracks in a QTreeWidget. --- wobuzz/command_line.py | 10 ++++++---- wobuzz/gui.py | 9 +++++++++ wobuzz/library/library.py | 42 +++++++++++++++++++++++++++++++++++++++ wobuzz/main.py | 2 ++ wobuzz/player/player.py | 2 +- wobuzz/player/playlist.py | 9 ++++++--- wobuzz/player/track.py | 2 ++ wobuzz/ui/library.py | 5 +++-- wobuzz/ui/library_dock.py | 5 ----- wobuzz/ui/main_window.py | 4 ---- wobuzz/ui/playlist.py | 19 ++++++++++++++++++ 11 files changed, 90 insertions(+), 19 deletions(-) create mode 100644 wobuzz/library/library.py create mode 100644 wobuzz/ui/playlist.py diff --git a/wobuzz/command_line.py b/wobuzz/command_line.py index d234407..48e7ab2 100644 --- a/wobuzz/command_line.py +++ b/wobuzz/command_line.py @@ -3,6 +3,7 @@ import sys import argparse from main import Wobuzz +from player.playlist import Playlist description = "A music player made by The Wobbler." @@ -17,12 +18,13 @@ arguments = parser.parse_args() app = Wobuzz() if arguments.playlist: - print(arguments.playlist) - app.player.current_playlist.load_from_m3u(arguments.playlist) + app.library.temporary_playlist.load_from_m3u(arguments.playlist) if arguments.track: - print(arguments.track) - app.player.current_playlist.load_from_paths(arguments.track) + app.library.temporary_playlist.load_from_paths(arguments.track) + +app.player.current_playlist = app.library.temporary_playlist +app.library.create_playlist_views() sys.exit(app.qt_app.exec()) diff --git a/wobuzz/gui.py b/wobuzz/gui.py index f5068e7..b36d5a4 100644 --- a/wobuzz/gui.py +++ b/wobuzz/gui.py @@ -1,5 +1,7 @@ #!/usr/bin/python3 +from PyQt6.QtCore import Qt +from PyQt6.QtWidgets import QDockWidget from ui.main_window import MainWindow @@ -9,6 +11,13 @@ class GUI: self.window = MainWindow() + self.window.addDockWidget(Qt.DockWidgetArea.RightDockWidgetArea, self.app.library.main_library_dock) + + self.app.library.main_library_dock.setFeatures( + QDockWidget.DockWidgetFeature.DockWidgetMovable | + QDockWidget.DockWidgetFeature.DockWidgetFloatable + ) + if self.app.settings.window_maximized: self.window.showMaximized() diff --git a/wobuzz/library/library.py b/wobuzz/library/library.py new file mode 100644 index 0000000..f0a5ef7 --- /dev/null +++ b/wobuzz/library/library.py @@ -0,0 +1,42 @@ +#!/usr/bin/python3 + +from PyQt6.QtWidgets import QTabWidget, QTreeWidgetItem +from player.playlist import Playlist +from ui.library_dock import LibraryDock +from ui.playlist import PlaylistView + + +class Library: + """ + Class that manages and syncs the library and library gui dock widgets. + """ + + def __init__(self, app): + self.app = app + + self.main_library_dock = LibraryDock() + self.library_docks = [self.main_library_dock] + + self.temporary_playlist = Playlist(self.app, "Temporary Playlist") + self.playlists = [self.temporary_playlist] + + def create_playlist_views(self): + for library_dock in self.library_docks: + playlist_tabs: QTabWidget = library_dock.library.playlist_tabs + + playlist_tabs.playlists = {} + + for playlist in self.playlists: + playlist_view = self.create_playlist_view(playlist) + playlist_tabs.playlists[playlist.title] = playlist_view + playlist_tabs.addTab(playlist_view, playlist.title) + + def create_playlist_view(self, playlist: Playlist): + view = PlaylistView() + + for track in playlist.tracks: + track_item = QTreeWidgetItem(view) + track_item.setText(1, track.title) + + return view + diff --git a/wobuzz/main.py b/wobuzz/main.py index ba226d9..1e1de44 100644 --- a/wobuzz/main.py +++ b/wobuzz/main.py @@ -6,6 +6,7 @@ from wobbl_tools.data_file import load_dataclass_json from settings import Settings from utils import Utils from player.player import Player +from library.library import Library from gui import GUI from gui_communication.gui_communication import GUICommunication @@ -20,6 +21,7 @@ class Wobuzz: self.settings.set_attribute_change_event(self.on_settings_change) self.player = Player(self) + self.library = Library(self) self.gui = GUI(self) self.gui_communication = GUICommunication(self) diff --git a/wobuzz/player/player.py b/wobuzz/player/player.py index 39cdda2..68b7bb5 100644 --- a/wobuzz/player/player.py +++ b/wobuzz/player/player.py @@ -19,7 +19,7 @@ class Player: self.track_progress = TrackProgress(self.app) - self.current_playlist = Playlist(self.app) + self.current_playlist = Playlist(self.app, "None") self.playing = False self.paused = False diff --git a/wobuzz/player/playlist.py b/wobuzz/player/playlist.py index 477e7b0..4101b0a 100644 --- a/wobuzz/player/playlist.py +++ b/wobuzz/player/playlist.py @@ -5,8 +5,9 @@ from .track import Track class Playlist: - def __init__(self, app): + def __init__(self, app, title: str): self.app = app + self.title = title # playlist title self.tracks: list[Track] = [] self.current_track_index = 0 self.current_track: Track | None = None @@ -22,7 +23,8 @@ class Playlist: i += 1 - self.current_track = self.tracks[0] # current track is the first track in the playlist + if self.current_track is None: # set current track to the first track if there is no currently playing track + self.current_track = self.tracks[0] def load_from_m3u(self, path): file = open(path, "r") @@ -43,7 +45,8 @@ class Playlist: i += 1 - self.current_track = self.tracks[0] + if self.current_track is None: # set current track to the first track if there is no currently playing track + self.current_track = self.tracks[0] def load_from_wbz(self, path): pass diff --git a/wobuzz/player/track.py b/wobuzz/player/track.py index 310fe38..5485e72 100644 --- a/wobuzz/player/track.py +++ b/wobuzz/player/track.py @@ -16,6 +16,8 @@ class Track: self.property_string = property_string self.cached = cache + # get filename (will be replaced by proper name getter in future) + self.title = path.split("/")[-1].split(".")[0] self.audio = None self.sound = None self.duration = 0 diff --git a/wobuzz/ui/library.py b/wobuzz/ui/library.py index b25a48d..79ee8ae 100644 --- a/wobuzz/ui/library.py +++ b/wobuzz/ui/library.py @@ -7,8 +7,9 @@ class Library(QToolBox): def __init__(self, parent=None): super().__init__(parent) - playlist_tabs = QTabWidget() - self.addItem(playlist_tabs, "Playlists") + self.playlist_tabs = QTabWidget() + self.addItem(self.playlist_tabs, "Playlists") + label = QLabel() self.addItem(label, "Genres") label = QLabel() diff --git a/wobuzz/ui/library_dock.py b/wobuzz/ui/library_dock.py index ca69db1..2bb768d 100644 --- a/wobuzz/ui/library_dock.py +++ b/wobuzz/ui/library_dock.py @@ -15,11 +15,6 @@ class LibraryDock(QDockWidget): Qt.DockWidgetArea.BottomDockWidgetArea ) - self.setFeatures( - QDockWidget.DockWidgetFeature.DockWidgetMovable | - QDockWidget.DockWidgetFeature.DockWidgetFloatable - ) - self.library = Library(self) self.setWidget(self.library) diff --git a/wobuzz/ui/main_window.py b/wobuzz/ui/main_window.py index df38598..a6e6cf9 100644 --- a/wobuzz/ui/main_window.py +++ b/wobuzz/ui/main_window.py @@ -3,7 +3,6 @@ from PyQt6.QtCore import Qt from PyQt6.QtWidgets import QMainWindow, QMenu from .track_control import TrackControl -from .library_dock import LibraryDock from .settings.settings import Settings @@ -30,6 +29,3 @@ class MainWindow(QMainWindow): self.settings.hide() self.addDockWidget(Qt.DockWidgetArea.LeftDockWidgetArea, self.settings) - self.library_dock = LibraryDock() - self.addDockWidget(Qt.DockWidgetArea.RightDockWidgetArea, self.library_dock) - diff --git a/wobuzz/ui/playlist.py b/wobuzz/ui/playlist.py new file mode 100644 index 0000000..066a3b7 --- /dev/null +++ b/wobuzz/ui/playlist.py @@ -0,0 +1,19 @@ +#!/usr/bin/python3 + +from PyQt6.QtWidgets import QTreeWidget + + +class PlaylistView(QTreeWidget): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.setColumnCount(4) + + headers = [ + "#", + "Title", + "Artist", + "Album" + ] + + self.setHeaderLabels(headers) \ No newline at end of file