reorganize files thread fix + header widths strange behavior

This commit is contained in:
billypom on debian 2024-09-18 22:08:55 -04:00
parent 1081d6b715
commit 59664e6c48

View File

@ -103,10 +103,6 @@ class MusicTable(QTableView):
self.songChanged = None self.songChanged = None
self.selected_song_filepath = "" self.selected_song_filepath = ""
self.current_song_filepath = "" self.current_song_filepath = ""
table_view_column_widths = str(self.config["table"]["column_widths"]).split(",")
for i in range(self.model.columnCount() - 1):
self.setColumnWidth(i, int(table_view_column_widths[i]))
self.horizontalHeader().setStretchLastSection(True) self.horizontalHeader().setStretchLastSection(True)
self.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive) self.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive)
# self.horizontalHeader().setCascadingSectionResizes(True) # self.horizontalHeader().setCascadingSectionResizes(True)
@ -121,6 +117,20 @@ class MusicTable(QTableView):
# Final actions # Final actions
self.load_music_table() self.load_music_table()
self.setup_keyboard_shortcuts() self.setup_keyboard_shortcuts()
# Load the column widths from last save
# This doesn't work inside its own function for some reason
# self.load_header_widths()
table_view_column_widths = str(self.config["table"]["column_widths"]).split(",")
for i in range(self.model.columnCount() - 1):
self.setColumnWidth(i, int(table_view_column_widths[i]))
def load_header_widths(self):
"""
Loads the header widths from the last application close.
"""
table_view_column_widths = str(self.config["table"]["column_widths"]).split(",")
for i in range(self.model.columnCount() - 1):
self.setColumnWidth(i, int(table_view_column_widths[i]))
def resizeEvent(self, e: typing.Optional[QResizeEvent]) -> None: def resizeEvent(self, e: typing.Optional[QResizeEvent]) -> None:
"""Do something when the QTableView is resized""" """Do something when the QTableView is resized"""
@ -137,7 +147,7 @@ class MusicTable(QTableView):
if sum != qtableview_width: if sum != qtableview_width:
# if not the last header # if not the last header
if logicalIndex < (col_count - 1): if logicalIndex < (col_count):
next_header_size = self.horizontalHeader().sectionSize(logicalIndex + 1) next_header_size = self.horizontalHeader().sectionSize(logicalIndex + 1)
# If it should shrink # If it should shrink
if next_header_size > (sum_of_cols - qtableview_width): if next_header_size > (sum_of_cols - qtableview_width):
@ -313,8 +323,8 @@ class MusicTable(QTableView):
print(f"files: {files}") print(f"files: {files}")
if directories: if directories:
worker = Worker(self.get_audio_files_recursively, directories) worker = Worker(self.get_audio_files_recursively, directories)
# worker.signals.signal_progress.connect(self.handle_progress) worker.signals.signal_progress.connect(self.handle_progress)
worker.signals.signal_progress.connect(self.qapp.handle_progress) # worker.signals.signal_progress.connect(self.qapp.handle_progress)
worker.signals.signal_result.connect(self.on_recursive_search_finished) worker.signals.signal_result.connect(self.on_recursive_search_finished)
worker.signals.signal_finished.connect(self.load_music_table) worker.signals.signal_finished.connect(self.load_music_table)
if self.qapp: if self.qapp:
@ -330,10 +340,6 @@ class MusicTable(QTableView):
if result: if result:
self.add_files(result) self.add_files(result)
# def handle_progress(self, data):
# """Emits data to main"""
# self.handleProgressSignal.emit(data)
def keyPressEvent(self, e): def keyPressEvent(self, e):
"""Press a key. Do a thing""" """Press a key. Do a thing"""
if not e: if not e:
@ -366,7 +372,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.handle_reorganize_selected_files) shortcut.activated.connect(self.confirm_reorganize_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"""
@ -388,14 +394,16 @@ 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 handle_reorganize_selected_files(self): def handle_progress(self, data):
"""""" """Emits data to main"""
worker = Worker(self.reorganize_selected_files) self.handleProgressSignal.emit(data)
worker.signals.signal_progress.connect(self.qapp.handle_progress)
self.qapp.threadpool.start(worker)
def reorganize_selected_files(self, progress_callback=None): def confirm_reorganize_files(self) -> None:
"""Ctrl+Shift+R = Reorganize""" """
Ctrl+Shift+R = Reorganize
Asks user to confirm reorganizing files,
then starts a new thread to do the work
"""
filepaths = self.get_selected_songs_filepaths() filepaths = self.get_selected_songs_filepaths()
# Confirmation screen (yes, no) # Confirmation screen (yes, no)
reply = QMessageBox.question( reply = QMessageBox.question(
@ -405,7 +413,17 @@ class MusicTable(QTableView):
QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes | QMessageBox.No,
QMessageBox.Yes, QMessageBox.Yes,
) )
if reply: if reply == QMessageBox.Yes:
worker = Worker(self.reorganize_files, filepaths)
worker.signals.signal_progress.connect(self.handle_progress)
worker.signals.signal_finished.connect(self.load_music_table)
self.qapp.threadpool.start(worker)
def reorganize_files(self, filepaths, progress_callback=None):
"""
Reorganizes files into Artist/Album/Song,
based on the directories->reorganize_destination config
"""
# Get target directory # Get target directory
target_dir = str(self.config["directories"]["reorganize_destination"]) target_dir = str(self.config["directories"]["reorganize_destination"])
for filepath in filepaths: for filepath in filepaths:
@ -438,7 +456,6 @@ class MusicTable(QTableView):
f"reorganize_selected_files() | Error moving file: {filepath} | {e}" f"reorganize_selected_files() | Error moving file: {filepath} | {e}"
) )
# Draw the rest of the owl # Draw the rest of the owl
self.load_music_table()
QMessageBox.information( QMessageBox.information(
self, "Reorganization complete", "Files successfully reorganized" self, "Reorganization complete", "Files successfully reorganized"
) )