Wobuzz/wobuzz/ui/playlist_tabs/tab_bar.py

59 lines
1.7 KiB
Python
Raw Normal View History

#!/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)
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
self.app.gui.clicked_playlist = playlist
def on_doubleclick(self, index):
playlist_view = self.tab_widget.widget(index)
playlist = playlist_view.playlist
if playlist.has_tracks(): # dont crash when playlist is empty
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)