Detach new entries creation from Render thread

Prevents the same hang-up from loading an existing library for when a
new library is being created or additional entries are being added. As
`add_new_files_runnable` is no longer blocking, an additional signal was
needed to be added for the program to know when it was safe to display
the library.
This commit is contained in:
Sean Krueger 2024-06-23 14:25:08 -05:00
parent d6613716c2
commit 0459d85cc6

View file

@ -160,6 +160,7 @@ class QtDriver(QObject):
"""A Qt GUI frontend driver for TagStudio.""" """A Qt GUI frontend driver for TagStudio."""
SIGTERM = Signal() SIGTERM = Signal()
new_files_added = Signal()
preview_panel: PreviewPanel preview_panel: PreviewPanel
@ -187,6 +188,7 @@ class QtDriver(QObject):
self.thumb_cutoff: float = time.time() self.thumb_cutoff: float = time.time()
# self.selected: list[tuple[int,int]] = [] # (Thumb Index, Page Index) # self.selected: list[tuple[int,int]] = [] # (Thumb Index, Page Index)
self.selected: list[tuple[ItemType, int]] = [] # (Item Type, Item ID) self.selected: list[tuple[ItemType, int]] = [] # (Item Type, Item ID)
self.loading_library = False self.loading_library = False
self.SIGTERM.connect(self.handleSIGTERM) self.SIGTERM.connect(self.handleSIGTERM)
@ -836,26 +838,14 @@ class QtDriver(QObject):
Threaded method that adds any known new files to the library and Threaded method that adds any known new files to the library and
initiates running default macros on them. initiates running default macros on them.
""" """
# logging.info(f'Start ANF: {QThread.currentThread()}') r = CustomRunnable(self.lib.add_new_files_as_entries)
new_ids: list[int] = self.lib.add_new_files_as_entries() r.done.connect(lambda new_ids: self.new_file_macros_runnable(new_ids))
# pb = QProgressDialog(f'Running Configured Macros on 1/{len(new_ids)} New Entries', None, 0,len(new_ids)) QThreadPool.globalInstance().start(r)
# pb.setFixedSize(432, 112)
# pb.setWindowFlags(pb.windowFlags() & ~Qt.WindowType.WindowCloseButtonHint)
# pb.setWindowTitle('Running Macros')
# pb.setWindowModality(Qt.WindowModality.ApplicationModal)
# pb.show()
# r = CustomRunnable(lambda: self.new_file_macros_runnable(pb, new_ids)) def new_file_macros_runnable(self, new_ids):
# r.done.connect(lambda: (pb.hide(), pb.deleteLater(), self.filter_items(''))) """Threaded method that runs macros on a set of Entry IDs."""
# r.run() # Run default macro on each new id
# # QThreadPool.globalInstance().start(r) iterator = FunctionIterator(lambda: self.run_default_macros(new_ids))
# # logging.info(f'{INFO} Running configured Macros on {len(new_ids)} new Entries...')
# # self.main_window.statusbar.showMessage(f'Running configured Macros on {len(new_ids)} new Entries...', 3)
# # pb.hide()
iterator = FunctionIterator(lambda: self.new_file_macros_runnable(new_ids))
pw = ProgressWidget( pw = ProgressWidget(
window_title="Running Macros on New Entries", window_title="Running Macros on New Entries",
label_text=f"Running Configured Macros on 1/{len(new_ids)} New Entries", label_text=f"Running Configured Macros on 1/{len(new_ids)} New Entries",
@ -871,11 +861,18 @@ class QtDriver(QObject):
) )
) )
r = CustomRunnable(lambda: iterator.run()) r = CustomRunnable(lambda: iterator.run())
r.done.connect(lambda: (pw.hide(), pw.deleteLater(), self.filter_items(""))) r.done.connect(
lambda: (
pw.hide(),
pw.deleteLater(),
self.filter_items(""),
self.new_files_added.emit(),
)
)
QThreadPool.globalInstance().start(r) QThreadPool.globalInstance().start(r)
def new_file_macros_runnable(self, new_ids): def run_default_macros(self, new_ids):
"""Threaded method that runs macros on a set of Entry IDs.""" """Runs the default Macro on newly created entries."""
# sleep(1) # sleep(1)
# logging.info(f'ANFR: {QThread.currentThread()}') # logging.info(f'ANFR: {QThread.currentThread()}')
# for i, id in enumerate(new_ids): # for i, id in enumerate(new_ids):
@ -1441,8 +1438,7 @@ class QtDriver(QObject):
def open_library(self, path: Path): def open_library(self, path: Path):
"""Opens a TagStudio library.""" """Opens a TagStudio library."""
# Prevents attemping to load a new library # Prevents attemping to load a new library while another is already loading.
# while another is already loading.
if self.loading_library: if self.loading_library:
return return
@ -1464,17 +1460,21 @@ class QtDriver(QObject):
QThreadPool.globalInstance().start(r) QThreadPool.globalInstance().start(r)
def library_load_callback(self, return_code: int): def library_load_callback(self, return_code: int):
"""Displays the newly loaded library.""" """Handles the result of trying to load the library."""
if return_code == 1: if return_code == 1:
logging.info(f"Succesfully loaded Library") logging.info(f"Succesfully loaded Library")
self.display_initial_library_load()
else: else:
logging.info( logging.info(
f"{ERROR} No existing TagStudio library found at '{self.loaded_library_path}'. Creating one." f"{ERROR} No existing TagStudio library found at '{self.loaded_library_path}'. Creating one."
) )
result = self.lib.create_library(self.loaded_library_path) return_code = self.lib.create_library(self.loaded_library_path)
logging.info(f"Library Creation Return Code: {result}") logging.info(f"Library Creation Return Code: {return_code}")
self.add_new_files_callback() self.add_new_files_callback()
self.new_files_added.connect(self.display_initial_library_load)
def display_initial_library_load(self):
"""Displays the newly loaded library."""
self.update_libs_list(self.loaded_library_path) self.update_libs_list(self.loaded_library_path)
title_text = f"{self.base_title} - Library '{self.lib.library_dir}'" title_text = f"{self.base_title} - Library '{self.lib.library_dir}'"
self.main_window.setWindowTitle(title_text) self.main_window.setWindowTitle(title_text)
@ -1486,7 +1486,10 @@ class QtDriver(QObject):
self.preview_panel.update_widgets() self.preview_panel.update_widgets()
self.filter_items() self.filter_items()
self.main_window.toggle_landing_page(False) self.main_window.toggle_landing_page(False)
self.loading_library = False self.loading_library = False
# Disconnect from the signal in order to no be called during "Refresh Dir" action
self.new_files_added.disconnect(self.display_initial_library_load)
def create_collage(self) -> None: def create_collage(self) -> None:
"""Generates and saves an image collage based on Library Entries.""" """Generates and saves an image collage based on Library Entries."""