refactor musictable drag and drop into its own class

This commit is contained in:
billypom on debian 2024-03-29 20:55:29 -04:00
parent bb6725f3a3
commit b9c045574c
3 changed files with 29 additions and 20 deletions

View File

@ -1,6 +1,6 @@
from mutagen.easyid3 import EasyID3 from mutagen.easyid3 import EasyID3
import DBA import DBA
from PyQt5.QtGui import QStandardItem, QStandardItemModel, QKeySequence from PyQt5.QtGui import QStandardItem, QStandardItemModel, QKeySequence, QDragEnterEvent, QDropEvent
from PyQt5.QtWidgets import QTableView, QShortcut, QMessageBox, QAbstractItemView from PyQt5.QtWidgets import QTableView, QShortcut, QMessageBox, QAbstractItemView
from PyQt5.QtCore import QModelIndex, Qt, pyqtSignal, QTimer from PyQt5.QtCore import QModelIndex, Qt, pyqtSignal, QTimer
from utils import add_files_to_library from utils import add_files_to_library
@ -42,6 +42,32 @@ class MusicTable(QTableView):
self.model.layoutChanged.connect(self.restore_scroll_position) self.model.layoutChanged.connect(self.restore_scroll_position)
def dragEnterEvent(self, event: QDragEnterEvent):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event: QDragEnterEvent):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event: QDropEvent):
if event.mimeData().hasUrls():
files = []
for url in event.mimeData().urls():
if url.isLocalFile():
files.append(url.path())
self.add_files(files)
event.accept()
else:
event.ignore()
def setup_keyboard_shortcuts(self): def setup_keyboard_shortcuts(self):
"""Setup shortcuts here""" """Setup shortcuts here"""
shortcut = QShortcut(QKeySequence("Ctrl+Shift+R"), self) shortcut = QShortcut(QKeySequence("Ctrl+Shift+R"), self)

19
main.py
View File

@ -86,23 +86,6 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.tableView.horizontalHeader().setStretchLastSection(False) self.tableView.horizontalHeader().setStretchLastSection(False)
def eventFilter(self, source, event):
"""Handles events"""
# tableView (drag & drop)
if (source is self.tableView.viewport() and
(event.type() == QEvent.DragEnter or
event.type() == QEvent.DragMove or
event.type() == QEvent.Drop) and
event.mimeData().hasUrls()):
files = []
if event.type() == QEvent.Drop:
for url in event.mimeData().urls():
if url.isLocalFile():
files.append(url.path())
self.tableView.add_files(files)
event.accept()
return True
return super().eventFilter(source, event)
def closeEvent(self, event): def closeEvent(self, event):
"""Save settings when closing the application""" """Save settings when closing the application"""
@ -142,7 +125,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
self.load_album_art(self.current_song_album_art) self.load_album_art(self.current_song_album_art)
def load_album_art(self, album_art_data): def load_album_art(self, album_art_data):
"""Sets the album art for the currently playing track""" """Displays the album art for the currently playing track in the GraphicsView"""
if self.current_song_album_art: if self.current_song_album_art:
# Clear the scene # Clear the scene
self.album_art_scene.clear() self.album_art_scene.clear()

2
ui.ui
View File

@ -271,7 +271,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1152</width> <width>1152</width>
<height>24</height> <height>41</height>
</rect> </rect>
</property> </property>
<widget class="QMenu" name="menuFile"> <widget class="QMenu" name="menuFile">