diff --git a/wobuzz/gui.py b/wobuzz/gui.py index 8e2438c..c23dc49 100644 --- a/wobuzz/gui.py +++ b/wobuzz/gui.py @@ -40,6 +40,8 @@ class GUI: self.settings.update_all() def on_exit(self, event): + self.window.focusWidget().clearFocus() # clear focus on focused widget + self.app.player.stop() self.app.library.on_exit(event) diff --git a/wobuzz/player/playlist.py b/wobuzz/player/playlist.py index b1d4228..3cb4388 100644 --- a/wobuzz/player/playlist.py +++ b/wobuzz/player/playlist.py @@ -265,7 +265,6 @@ class Playlist: return self.current_track.sound, self.current_track.duration def save(self): - first_view = list(self.views.values())[0] first_view.sortItems(4, Qt.SortOrder.AscendingOrder) self.sync(first_view) @@ -283,8 +282,6 @@ class Playlist: wbz.close() def rename(self, title: str): - # remove from unique names so a new playlist can have the old name and delete old playlist. - if os.path.exists(self.path): os.remove(self.path) @@ -299,6 +296,7 @@ class Playlist: if self == self.app.library.temporary_playlist: self.app.library.temporary_playlist = None + # remove from unique names so a new playlist can have the old name and delete old playlist. if not old_title == self.title: # remove only when the playlist actually has a different name self.app.utils.unique_names.remove(old_title) diff --git a/wobuzz/ui/playlist_tabs/tab_bar.py b/wobuzz/ui/playlist_tabs/tab_bar.py index be7621e..ed50ba8 100644 --- a/wobuzz/ui/playlist_tabs/tab_bar.py +++ b/wobuzz/ui/playlist_tabs/tab_bar.py @@ -19,7 +19,6 @@ class PlaylistTabBar(QTabBar): self.tabBarClicked.connect(self.on_click) self.tabBarDoubleClicked.connect(self.on_doubleclick) - self.tabMoved.connect(self.on_tab_move) def dragEnterEvent(self, event: QDragEnterEvent): index = self.tabAt(event.position().toPoint()) @@ -59,17 +58,7 @@ class PlaylistTabBar(QTabBar): if index == -1: # when no tab was clicked, do nothing return - title = self.tabButton(index, QTabBar.ButtonPosition.RightSide) + playlist_view = self.tab_widget.widget(index) + playlist = playlist_view.playlist - self.context_menu.exec(event.globalPos(), title) - - def on_tab_move(self, i_from, i_to): - title = self.tabButton(i_to, QTabBar.ButtonPosition.RightSide) - - # update the index - title.index = i_to - - def update_title_indexes(self, after: int): - for i in range(after, self.count()): - title = self.tabButton(i, QTabBar.ButtonPosition.RightSide) - title.index = i + self.context_menu.exec(event.globalPos(), index, playlist) diff --git a/wobuzz/ui/playlist_tabs/tab_context_menu.py b/wobuzz/ui/playlist_tabs/tab_context_menu.py index 12ec0df..6d78b80 100644 --- a/wobuzz/ui/playlist_tabs/tab_context_menu.py +++ b/wobuzz/ui/playlist_tabs/tab_context_menu.py @@ -4,6 +4,8 @@ from PyQt6.QtCore import QPoint from PyQt6.QtGui import QAction from PyQt6.QtWidgets import QMenu, QTabBar +from .tab_title_editor import TabTitleEditor + class PlaylistContextMenu(QMenu): def __init__(self, parent=None): @@ -11,7 +13,8 @@ class PlaylistContextMenu(QMenu): self.tab_bar: QTabBar = parent - self.playlist_title = None + self.tab_index = -1 + self.playlist = None self.title = self.addSection("Playlist Actions") @@ -24,15 +27,19 @@ class PlaylistContextMenu(QMenu): self.rename_action.triggered.connect(self.rename) self.delete_action.triggered.connect(self.delete) - def exec(self, pos: QPoint, title): - self.playlist_title = title + def exec(self, pos: QPoint, index: int, playlist): + self.tab_index = index + self.playlist = playlist - self.title.setText(title.text()) # set section title + self.title.setText(playlist.title) super().exec(pos) def rename(self): - self.playlist_title.setFocus() + # create temporary QLineEdit for renaming the tab + title_editor = TabTitleEditor(self.playlist, self.tab_bar, self.tab_index) + + self.tab_bar.setTabButton(self.tab_index, QTabBar.ButtonPosition.RightSide, title_editor) def delete(self): - self.playlist_title.playlist_view.playlist.delete() + self.playlist.delete() diff --git a/wobuzz/ui/playlist_tabs/tab_title.py b/wobuzz/ui/playlist_tabs/tab_title.py deleted file mode 100644 index 5ae4181..0000000 --- a/wobuzz/ui/playlist_tabs/tab_title.py +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/python3 - -from PyQt6.QtCore import Qt -from PyQt6.QtGui import QMouseEvent, QCursor -from PyQt6.QtWidgets import QLineEdit - -from .tab_bar import PlaylistTabBar - - -class TabTitle(QLineEdit): - def __init__(self, app, label, parent, index: int, playlist_view): - super().__init__(label, parent) - - self.app = app - self.tab_bar: PlaylistTabBar = parent - self.index = index - self.playlist_view = playlist_view - - self.setStyleSheet("QLineEdit {background: transparent; border: none;}") - self.setFocusPolicy(Qt.FocusPolicy.TabFocus) - - self.setCursor(QCursor(Qt.CursorShape.ArrowCursor)) # normal cursor (would be a text cursor) - - self.editingFinished.connect(self.on_edit) - - def mouseDoubleClickEvent(self, event: QMouseEvent): - self.tab_bar.tabBarDoubleClicked.emit(self.index) - - def mousePressEvent(self, event: QMouseEvent): - self.tab_bar.tabBarClicked.emit(self.index) - self.tab_bar.setCurrentIndex(self.index) - - def contextMenuEvent(self, event): - self.tab_bar.contextMenuEvent(event, self) - - def on_edit(self): - self.clearFocus() - - self.playlist_view.playlist.rename(self.text()) - - self.setText(self.playlist_view.playlist.title) - diff --git a/wobuzz/ui/playlist_tabs/tab_title_editor.py b/wobuzz/ui/playlist_tabs/tab_title_editor.py new file mode 100644 index 0000000..3b020d7 --- /dev/null +++ b/wobuzz/ui/playlist_tabs/tab_title_editor.py @@ -0,0 +1,26 @@ +#!/usr/bin/python3 + +from PyQt6.QtWidgets import QLineEdit, QTabBar + + +class TabTitleEditor(QLineEdit): + def __init__(self, playlist, parent, index: int): + super().__init__(playlist.title, parent) + + self.playlist = playlist + self.tab_bar = parent + self.index = index + + self.tab_bar.setTabText(index, "") + + self.setFocus() + + self.editingFinished.connect(self.on_edit) + + def on_edit(self): + self.playlist.rename(self.text()) + + self.deleteLater() + self.tab_bar.setTabButton(self.index, QTabBar.ButtonPosition.RightSide, None) + self.tab_bar.setTabText(self.index, self.playlist.title) + diff --git a/wobuzz/ui/playlist_tabs/tab_widget.py b/wobuzz/ui/playlist_tabs/tab_widget.py index d2ffce6..4976458 100644 --- a/wobuzz/ui/playlist_tabs/tab_widget.py +++ b/wobuzz/ui/playlist_tabs/tab_widget.py @@ -1,9 +1,8 @@ #!/usr/bin/python3 -from PyQt6.QtWidgets import QTabWidget, QTabBar +from PyQt6.QtWidgets import QTabWidget from .tab_bar import PlaylistTabBar -from .tab_title import TabTitle class PlaylistTabs(QTabWidget): @@ -19,20 +18,3 @@ class PlaylistTabs(QTabWidget): self.setMovable(True) self.setAcceptDrops(True) - - def addTab(self, playlist_view, label) -> int: - index = super().addTab(playlist_view, None) - - title = TabTitle(self.app, label, self.tab_bar, index, playlist_view) - - self.tab_bar.setTabButton(index, QTabBar.ButtonPosition.RightSide, title) - - return index - - def tabRemoved(self, index): - # Update indexes because when a playlist is replaced, (and the old playlist widget is deleted by deleteLater()) - # the old playlist_widget is actually deleted later than the new one is created. - # Because of this, the new playlist tab gets immediately moved one to the left and we have to update the - # indexes. - self.tab_bar.update_title_indexes(index) -