Wobuzz/wobuzz/ui/playlist_tabs/tab_bar.py

75 lines
2.2 KiB
Python

#!/usr/bin/python3
from PyQt6.QtGui import QDragEnterEvent, QContextMenuEvent
from PyQt6.QtWidgets import QTabBar
from .tab_context_menu import PlaylistContextMenu
class PlaylistTabBar(QTabBar):
def __init__(self, parent=None):
super().__init__(parent)
self.tab_widget = parent
self.app = parent.library.app
self.context_menu = PlaylistContextMenu(self)
self.setAcceptDrops(True)
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())
self.tab_widget.setCurrentIndex(index)
def on_click(self, index):
if index == -1: # do nothing if no tab was clicked
return
playlist_view = self.tab_widget.widget(index)
playlist = playlist_view.playlist
if not playlist.loaded:
playlist.load()
self.app.gui.clicked_playlist = playlist
def on_doubleclick(self, index):
playlist_view = self.tab_widget.widget(index)
if playlist_view is None: # dont crash if no playlist was double-clicked
return
playlist = playlist_view.playlist
self.app.player.start_playlist(playlist)
def contextMenuEvent(self, event: QContextMenuEvent, title=None):
# get title by self.tabAt() if the event is called from PyQt, else its executed from the tab title and getting
# the index by tabAt() wouldn't be possible
if title is None:
pos = event.pos()
index = self.tabAt(pos)
if index == -1: # when no tab was clicked, do nothing
return
title = self.tabButton(index, QTabBar.ButtonPosition.RightSide)
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