Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abort loading popup #188

Merged
merged 5 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions MangaManager/src/Common/progressbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def stop(self):

class ProgressBar(abc.ABC):
running = False
PROCESSED_TAG = "$processed"
TOTAL_TAG = "$total"
ERRORS_TAG = "$errors"
ELAPSED_TIME_TAG = "$elapsed_time"
ESTIMATED_TIME_TAG = "$estimated_time"

def __init__(self):

self.timer = RepeatedTimer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ def backcover_action(self, loaded_cinfo: LoadedComicInfo = None, auto_trigger=Fa
self.update()

def clear(self):
self.tooltip_filename.text = "No file selected"
try:
self.cover_canvas.itemconfig(self.cover_canvas.image_id, state="hidden")
except _tkinter.TclError as e:
Expand All @@ -282,6 +283,7 @@ def clear(self):
else:
# If the error is caused by something else, re-raise the exception
raise e

self.backcover_canvas.itemconfig(self.backcover_canvas.image_id, state="hidden")
self.hide_actions()

Expand Down
79 changes: 79 additions & 0 deletions MangaManager/src/MetadataManager/GUI/windows/LoadingWindow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import tkinter
from unittest.mock import Mock

from src.Common.progressbar import ProgressBar
from src.MetadataManager.GUI.utils import center
from src.MetadataManager.GUI.widgets import ProgressBarWidget


class LoadingWindow(tkinter.Toplevel):
initialized: bool = False
# abort_flag:bool = None
def __new__(cls, total, *args, **kwargs):
if total <=1:
a = Mock()
a.is_abort = lambda :False
a.abort_flag = False
return a

else:
return super(LoadingWindow, cls).__new__(cls)

def __init__(self, total):
super().__init__()
content = tkinter.Frame(self,background="white",borderwidth=3,border=3,highlightcolor="black",highlightthickness=2,highlightbackground="black")
content.pack(ipadx=20, ipady=20,expand=False,fill="both")

self.title = "Loading Files"
self.loading_label_value = tkinter.StringVar(content, name="Loading_label")
self.loading_label = tkinter.Label(content, textvariable=self.loading_label_value)
# Removing titlebar from the Dialogue
self.geometry("300x100+30+30")
# Make the windows always on top
self.attributes("-topmost", True)
self.lift()
self.abort_flag = False
# Force focus on this window
self.grab_set()
center(self)

self.overrideredirect(True)

self.pb = ProgressBarWidget(content)

self.pb.pb_label.configure(justify="center",background="white")
self.pb.pb_label.pack(expand=False, fill="x", side="top")
self.pb.set_template(f"Loaded:{ProgressBar.PROCESSED_TAG}/{ProgressBar.TOTAL_TAG}\n")
self.pb.start(total)

abort_btn = tkinter.Button(content,text="Abort",command=self.set_abort)
abort_btn.pack()
self.initialized = True
def is_abort(self):
return self.abort_flag
def set_abort(self,*_):
if self.initialized:
self.abort_flag = True
self.pb.set_template("Aborting...\n")
self.after(2000, self.finish_loading)
#
# self.pb = ProgressBar()
# self.pb.set_template(f"Loaded:{ProgressBar.PROCESSED_TAG}/{ProgressBar.TOTAL_TAG}\n")

def loaded_file(self, value: str):
if self.initialized:
self.pb.set_template(f"Loading: {ProgressBar.PROCESSED_TAG}/{ProgressBar.TOTAL_TAG}\nLast loaded: '{value}'")
self.pb.increase_processed()

def finish_loading(self):
if self.initialized:
self.grab_release()
self.destroy()



if __name__ == '__main__':
root = tkinter.Tk()
a = LoadingWindow(2,False)
a.loaded_file("asda")
root.mainloop()
34 changes: 28 additions & 6 deletions MangaManager/src/MetadataManager/MetadataManagerGUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from src.MetadataManager.GUI.ControlManager import ControlManager
from src.MetadataManager.GUI.MessageBox import MessageBoxWidgetFactory as mb
from src.MetadataManager.GUI.windows.AboutWindow import AboutWindow
from src.MetadataManager.GUI.windows.LoadingWindow import LoadingWindow
from src.Settings import SettingHeading
from src.Settings.Settings import Settings

Expand All @@ -39,6 +40,7 @@ class GUIApp(Tk, MetadataManagerLib):
inserting_files = False
widget_mngr = WidgetManager()
control_mngr = ControlManager() # widgets that should be disabled while processing
loading_window: LoadingWindow | None = None

def __init__(self):
super(GUIApp, self).__init__()
Expand Down Expand Up @@ -149,9 +151,14 @@ def select_files(self):
self.selected_files_path = [file.name for file in selected_paths_list]

self.log.debug(f"Selected files [{', '.join(self.selected_files_path)}]")
self.open_cinfo_list()
self.loading_window = LoadingWindow(len(self.selected_files_path))

self._serialize_cinfolist_to_gui()
if self.open_cinfo_list(abort_load_check=self.loading_window.is_abort):
self._serialize_cinfolist_to_gui()
else:
self.clean_selected()
self.loading_window.finish_loading()
self.loading_window = None
self.inserting_files = False
self.control_mngr.unlock()
self.widget_mngr.toggle_widgets(enabled=True)
Expand Down Expand Up @@ -182,9 +189,14 @@ def select_folder(self):
# self.selected_files_path = [str(Path(folder_path, file)) for file in os.listdir(folder_path) if file.endswith(".cbz")]

self.log.debug(f"Selected files [{', '.join(self.selected_files_path)}]")
self.open_cinfo_list()
self.loading_window = LoadingWindow(len(self.selected_files_path))

self._serialize_cinfolist_to_gui()
if self.open_cinfo_list(self.loading_window.is_abort):
self._serialize_cinfolist_to_gui()
else:
self.clean_selected()
self.loading_window.finish_loading()
self.loading_window = None
self.inserting_files = False
self.control_mngr.unlock()
self.widget_mngr.toggle_widgets(enabled=True)
Expand Down Expand Up @@ -237,16 +249,19 @@ def show_not_saved_indicator(self, loaded_cinfo_list=None):
# INTERFACE IMPLEMENTATIONS
############

def on_item_loaded(self, loaded_cinfo: LoadedComicInfo):
def on_item_loaded(self, loaded_cinfo: LoadedComicInfo, cursor, total) -> bool:
"""
Called by backend when an item gets added to the loaded comic info list
:param loaded_cinfo:
:return:
"""
if self.loading_window.initialized:
self.loading_window.update()
self.loading_window.loaded_file(loaded_cinfo.file_name)
self.selected_files_treeview.insert(loaded_cinfo)
self.image_cover_frame.update_cover_image([loaded_cinfo])
self.update()

return self.loading_window.abort_flag
#########################################################
# Errors handling / hooks implementations
############
Expand Down Expand Up @@ -503,3 +518,10 @@ def process_fetch_online(self, *_):
return

self._serialize_cinfolist_to_gui([LoadedComicInfo(None, cinfo, load_default_metadata=False)])

def clean_selected(self):

self.widget_mngr.clean_widgets()
self.image_cover_frame.clear()
self.selected_files_path = list()
self.selected_files_treeview.clear()
18 changes: 14 additions & 4 deletions MangaManager/src/MetadataManager/MetadataManagerLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


class _IMetadataManagerLib(abc.ABC):
def on_item_loaded(self, loaded_cinfo: LoadedComicInfo):
def on_item_loaded(self, loaded_cinfo: LoadedComicInfo,cursor,total):
"""
Called when a loadedcomicinfo is loaded
:return:
Expand Down Expand Up @@ -173,7 +173,7 @@ def merge_changed_metadata(self, loaded_cinfo_list: list[LoadedComicInfo]) -> bo
self.new_edited_cinfo = None
return any_has_changes

def open_cinfo_list(self) -> None:
def open_cinfo_list(self, abort_load_check:callable) -> bool:
"""
Creates a list of comicinfo with the comicinfo metadata from the selected files.

Expand All @@ -185,7 +185,14 @@ def open_cinfo_list(self) -> None:
self.loaded_cinfo_list: list[LoadedComicInfo] = list()
# Skip warnings if one was already displayed
missing_rar_tool = False
for file_path in self.selected_files_path:
total_files = len(self.selected_files_path)
if total_files == 0:
return False
for i, file_path in enumerate(self.selected_files_path):
if abort_load_check():
logger.info("Abort loading")
self.loaded_cinfo_list: list[LoadedComicInfo] = list()
return False
try:
loaded_cinfo = LoadedComicInfo(path=file_path)
if Settings().get(SettingHeading.Main, 'cache_cover_images') and not self.is_cli:
Expand Down Expand Up @@ -213,8 +220,11 @@ def open_cinfo_list(self) -> None:
continue

self.loaded_cinfo_list.append(loaded_cinfo)
self.on_item_loaded(loaded_cinfo)
self.on_item_loaded(loaded_cinfo=loaded_cinfo, cursor=i, total=total_files)

# self.on_item_loaded(loaded_cinfo)
logger.debug("Files selected")
return True

def preview_export(self, loaded_cinfo):
"""
Expand Down
2 changes: 1 addition & 1 deletion MangaManager/src/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.0.3:stable:3d10f3c"
__version__ = "1.0.3:stable:a12c343"
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_selected_files_loaded(self):
# Setup
self.test_files_names = create_dummy_files(2)
self.instance.selected_files_path = self.test_files_names
self.instance.open_cinfo_list()
self.instance.open_cinfo_list(lambda : False)
self.assertEqual(2, len(self.instance.loaded_cinfo_list))

def test_process_should_raise_exception_if_no_new_cinfo(self):
Expand Down