spacebar to play/pause, enter key to play selected song, arrow key up and down for table navigation

This commit is contained in:
billypom on debian 2024-02-28 18:54:12 -05:00
parent dd558bd498
commit 8b77bd7fee
2 changed files with 45 additions and 6 deletions

View File

@ -1,7 +1,7 @@
import DBA import DBA
from PyQt5.QtGui import QStandardItem, QStandardItemModel from PyQt5.QtGui import QStandardItem, QStandardItemModel
from PyQt5.QtWidgets import QTableView from PyQt5.QtWidgets import QTableView
from PyQt5.QtCore import QTimer from PyQt5.QtCore import QTimer, Qt, pyqtSignal
from tinytag import TinyTag from tinytag import TinyTag
from utils import add_files_to_library from utils import add_files_to_library
from utils import get_id3_tags from utils import get_id3_tags
@ -10,6 +10,8 @@ import logging
class MusicTable(QTableView): class MusicTable(QTableView):
playPauseSignal = pyqtSignal()
enterKey = pyqtSignal()
def __init__(self, parent=None, qapp=None): def __init__(self, parent=None, qapp=None):
QTableView.__init__(self, parent) QTableView.__init__(self, parent)
self.headers = ['title', 'artist', 'album', 'genre', 'codec', 'year', 'path'] self.headers = ['title', 'artist', 'album', 'genre', 'codec', 'year', 'path']
@ -18,15 +20,38 @@ class MusicTable(QTableView):
self.current_song_filepath = None self.current_song_filepath = None
self.qapp = None self.qapp = None
# self.tableView.resizeColumnsToContents() # self.tableView.resizeColumnsToContents()
self.clicked.connect(self.set_selected_song_filepath) # These are faster than the click/double determination self.clicked.connect(self.set_selected_song_filepath)
self.doubleClicked.connect(self.set_current_song_filepath) # These are faster than the click/double determination self.doubleClicked.connect(self.set_current_song_filepath) # listens for emitted signal, runs set_current_song_filepath
self.enterKey.connect(self.set_current_song_filepath)
self.fetch_library() self.fetch_library()
def keyPressEvent(self, event):
"""Press a key. Do a thing"""
key = event.key()
if key == Qt.Key_Space: # Spacebar to play/pause
self.toggle_play_pause()
elif key == Qt.Key_Up: # Arrow key navigation
current_index = self.currentIndex()
new_index = self.model.index(current_index.row() - 1, current_index.column())
if new_index.isValid():
self.setCurrentIndex(new_index)
elif key == Qt.Key_Down: # Arrow key navigation
current_index = self.currentIndex()
new_index = self.model.index(current_index.row() + 1, current_index.column())
if new_index.isValid():
self.setCurrentIndex(new_index)
elif key in (Qt.Key_Return, Qt.Key_Enter):
self.enterKey.emit() # Enter key detected
else: # Default behavior
super().keyPressEvent(event)
def mousePressEvent(self, event): def mousePressEvent(self, event):
"""Press mouse button. Do thing"""
self.last = "Click" self.last = "Click"
QTableView.mousePressEvent(self, event) # Keep original functionality QTableView.mousePressEvent(self, event) # Keep original functionality
def mouseReleaseEvent(self, event): def mouseReleaseEvent(self, event):
"""Release mouse button. Do thing"""
if self.last == "Click": if self.last == "Click":
QTimer.singleShot(self.qapp.instance().doubleClickInterval(), QTimer.singleShot(self.qapp.instance().doubleClickInterval(),
self.performSingleClickAction) self.performSingleClickAction)
@ -39,7 +64,7 @@ class MusicTable(QTableView):
def mouseDoubleClickEvent(self, event): def mouseDoubleClickEvent(self, event):
self.last = "Double Click" self.last = "Double Click"
self.doubleClicked.emit(self.selectionModel().currentIndex()) self.doubleClicked.emit(self.selectionModel().currentIndex()) # emits the current index of the double clicked song
QTableView.mouseDoubleClickEvent(self, event) # Keep original functionality QTableView.mouseDoubleClickEvent(self, event) # Keep original functionality
def performSingleClickAction(self): def performSingleClickAction(self):
@ -47,6 +72,16 @@ class MusicTable(QTableView):
self.message = "Click" self.message = "Click"
self.update() self.update()
def toggle_play_pause(self):
"""Toggles the currently playing song by emitting a signal"""
if not self.current_song_filepath:
self.set_current_song_filepath()
self.playPauseSignal.emit()
def choose_new_song(self):
"""Starts the playback of a new song by emitting a signal"""
def get_selected_rows(self): def get_selected_rows(self):
"""Returns a list of indexes for every selected row""" """Returns a list of indexes for every selected row"""
rows = [] rows = []
@ -61,6 +96,8 @@ class MusicTable(QTableView):
def set_current_song_filepath(self): def set_current_song_filepath(self):
"""Sets the filepath of the currently playing/chosen song""" """Sets the filepath of the currently playing/chosen song"""
# Setting the current song filepath automatically plays that song
# self.tableView listens to this function and plays the audio file located at self.current_song_filepath
self.current_song_filepath = self.currentIndex().siblingAtColumn(self.headers.index('path')).data() self.current_song_filepath = self.currentIndex().siblingAtColumn(self.headers.index('path')).data()
print(f'Current song: {self.current_song_filepath}') print(f'Current song: {self.current_song_filepath}')

View File

@ -77,6 +77,8 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
## tableView ## tableView
# self.tableView.clicked.connect(self.set_clicked_cell_filepath) # self.tableView.clicked.connect(self.set_clicked_cell_filepath)
self.tableView.doubleClicked.connect(self.play_audio_file) # Double click to play song self.tableView.doubleClicked.connect(self.play_audio_file) # Double click to play song
self.tableView.enterKey.connect(self.play_audio_file) # Press Enter to play song
self.tableView.playPauseSignal.connect(self.on_play_clicked) # Spacebar toggle playpause signal
self.tableView.viewport().installEventFilter(self) # for drag & drop functionality self.tableView.viewport().installEventFilter(self) # for drag & drop functionality
# self.tableView.model.layoutChanged() # self.tableView.model.layoutChanged()
### set column widths ### set column widths