This commit is contained in:
tsi-billypom 2024-09-06 16:04:56 -04:00
parent 9cefd00501
commit 31f23efcc1
3 changed files with 43 additions and 37 deletions

View File

@ -50,14 +50,12 @@ class MusicTable(QTableView):
deleteKey = pyqtSignal() deleteKey = pyqtSignal()
refreshMusicTable = pyqtSignal() refreshMusicTable = pyqtSignal()
def __init__(self: QTableView, parent=None): def __init__(self, parent):
# QTableView.__init__(self, parent)
super().__init__(parent) super().__init__(parent)
# Necessary for actions related to cell values
# FIXME: why does this give me pyright errors # FIXME: why does this give me pyright errors
self.model = QStandardItemModel(self) self.model = QStandardItemModel()
# self.model = QAbstractItemModel(self)
self.setModel(self.model) self.setModel(self.model)
# self.model: QAbstractItemModel | None = QAbstractItemModel(self)
# Config # Config
self.config = configparser.ConfigParser() self.config = configparser.ConfigParser()
@ -153,7 +151,6 @@ class MusicTable(QTableView):
QMessageBox.Yes, QMessageBox.Yes,
) )
if reply: if reply:
model = self.model
selected_filepaths = self.get_selected_songs_filepaths() selected_filepaths = self.get_selected_songs_filepaths()
selected_indices = self.get_selected_rows() selected_indices = self.get_selected_rows()
for file in selected_filepaths: for file in selected_filepaths:
@ -165,7 +162,7 @@ class MusicTable(QTableView):
self.model.dataChanged.disconnect(self.on_cell_data_changed) self.model.dataChanged.disconnect(self.on_cell_data_changed)
for index in selected_indices: for index in selected_indices:
try: try:
model.removeRow(index) self.model.removeRow(index)
except Exception as e: except Exception as e:
logging.info(f" delete_songs() failed | {e}") logging.info(f" delete_songs() failed | {e}")
self.load_music_table() self.load_music_table()
@ -237,7 +234,6 @@ class MusicTable(QTableView):
e.ignore() e.ignore()
def dropEvent(self, e: QDropEvent | None): def dropEvent(self, e: QDropEvent | None):
self.model.dataChanged.disconnect(self.on_cell_data_changed)
if e is None: if e is None:
return return
data = e.mimeData() data = e.mimeData()
@ -253,7 +249,6 @@ class MusicTable(QTableView):
self.threadpool.start(worker) self.threadpool.start(worker)
else: else:
e.ignore() e.ignore()
self.model.dataChanged.connect(self.on_cell_data_changed)
def keyPressEvent(self, e): def keyPressEvent(self, e):
"""Press a key. Do a thing""" """Press a key. Do a thing"""
@ -287,7 +282,7 @@ class MusicTable(QTableView):
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)
shortcut.activated.connect(self.reorganize_selected_files) shortcut.activated.connect(self.handle_reorganize_selected_files)
def on_cell_data_changed(self, topLeft: QModelIndex, bottomRight: QModelIndex): def on_cell_data_changed(self, topLeft: QModelIndex, bottomRight: QModelIndex):
"""Handles updating ID3 tags when data changes in a cell""" """Handles updating ID3 tags when data changes in a cell"""
@ -308,7 +303,13 @@ class MusicTable(QTableView):
# Update the library with new metadata # Update the library with new metadata
update_song_in_database(song_id, edited_column_name, user_input_data) update_song_in_database(song_id, edited_column_name, user_input_data)
def reorganize_selected_files(self): def handle_reorganize_selected_files(self):
""""""
worker = Worker(self.reorganize_selected_files)
worker.signals.signal_progress.connect(self.qapp.handle_progress)
self.threadpool.start(worker)
def reorganize_selected_files(self, progress_callback):
"""Ctrl+Shift+R = Reorganize""" """Ctrl+Shift+R = Reorganize"""
filepaths = self.get_selected_songs_filepaths() filepaths = self.get_selected_songs_filepaths()
# Confirmation screen (yes, no) # Confirmation screen (yes, no)
@ -326,6 +327,7 @@ class MusicTable(QTableView):
if str(filepath).startswith((target_dir)): if str(filepath).startswith((target_dir)):
continue continue
try: try:
progress_callback.emit(filepath)
# Read file metadata # Read file metadata
artist, album = get_reorganize_vars(filepath) artist, album = get_reorganize_vars(filepath)
# Determine the new path that needs to be made # Determine the new path that needs to be made
@ -382,6 +384,7 @@ class MusicTable(QTableView):
# Loading the table also causes cell data to change, technically # Loading the table also causes cell data to change, technically
# so we must disconnect the dataChanged trigger before loading # so we must disconnect the dataChanged trigger before loading
# then re-enable after we are done loading # then re-enable after we are done loading
pass
self.model.dataChanged.disconnect(self.on_cell_data_changed) self.model.dataChanged.disconnect(self.on_cell_data_changed)
except Exception as e: except Exception as e:
logging.info( logging.info(
@ -431,7 +434,7 @@ class MusicTable(QTableView):
item.setData(id, Qt.UserRole) item.setData(id, Qt.UserRole)
self.model.layoutChanged.emit() # emits a signal that the view should be updated self.model.layoutChanged.emit() # emits a signal that the view should be updated
try: try:
self.model.dataChanged.connect(self.on_cell_data_changed) # self.model.dataChanged.connect(self.on_cell_data_changed)
self.restore_scroll_position() self.restore_scroll_position()
except Exception: except Exception:
pass pass
@ -476,11 +479,6 @@ class MusicTable(QTableView):
self.model.data(self.model.index(row, 0), Qt.UserRole) self.model.data(self.model.index(row, 0), Qt.UserRole)
for row in selected_rows for row in selected_rows
] ]
# selected_ids = []
# for index in indexes:
# model_item = self.model.item(index.row())
# id_data = model_item.data(Qt.UserRole)
# selected_ids.append(id_data)
return id_list return id_list
def get_current_song_filepath(self) -> str: def get_current_song_filepath(self) -> str:

30
main.py
View File

@ -79,6 +79,7 @@ class WorkerSignals(QObject):
signal_started = pyqtSignal() signal_started = pyqtSignal()
signal_finished = pyqtSignal() signal_finished = pyqtSignal()
signal_result = pyqtSignal(object)
signal_progress = pyqtSignal(str) signal_progress = pyqtSignal(str)
@ -133,7 +134,6 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
global stopped global stopped
stopped = False stopped = False
# Multithreading stuff... # Multithreading stuff...
# self.workers = dict[UUID, WorkerThread] = {}
self.threadpool = QThreadPool() self.threadpool = QThreadPool()
# UI # UI
self.setupUi(self) self.setupUi(self)
@ -481,18 +481,24 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
filenames = open_files_window.selectedFiles() filenames = open_files_window.selectedFiles()
# Adds files to the library in a new thread # Adds files to the library in a new thread
worker = Worker(add_files_to_library, filenames) worker = Worker(add_files_to_library, filenames)
worker.signals.signal_progress.connect(self.handle_progress)
worker.signals.signal_started.connect(
lambda: self.tableView.model.dataChanged.disconnect(
self.tableView.on_cell_data_changed
)
)
worker.signals.signal_finished.connect(self.tableView.load_music_table) worker.signals.signal_finished.connect(self.tableView.load_music_table)
worker.signals.signal_finished.connect( worker.signals.signal_progress.connect(self.handle_progress)
lambda: self.tableView.model.dataChanged.connect( # try:
self.tableView.on_cell_data_changed # worker.signals.signal_started.connect(
) # lambda: self.tableView.model.dataChanged.disconnect(
) # self.tableView.on_cell_data_changed
# )
# )
# except:
# pass
# try:
# worker.signals.signal_finished.connect(
# lambda: self.tableView.model.dataChanged.connect(
# self.tableView.on_cell_data_changed
# )
# )
# except:
# pass
self.threadpool.start(worker) self.threadpool.start(worker)
def handle_progress(self, data): def handle_progress(self, data):

View File

@ -79,16 +79,18 @@ def add_files_to_library(files, progress_callback):
) )
insert_data = [] # Reset the insert_data list insert_data = [] # Reset the insert_data list
else: else:
logging.info("continuing...")
# continue adding files if we havent reached big length # continue adding files if we havent reached big length
continue continue
# Insert any remaining data # Insert any remaining data
if insert_data: logging.info("i check for insert data")
logging.info(f"inserting some songs: {len(insert_data)}") if insert_data:
with DBA.DBAccess() as db: logging.info(f"inserting some songs: {len(insert_data)}")
db.executemany( with DBA.DBAccess() as db:
"INSERT OR IGNORE INTO song (filepath, title, album, artist, track_number, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", db.executemany(
insert_data, "INSERT OR IGNORE INTO song (filepath, title, album, artist, track_number, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
) insert_data,
)
return True return True