From fe6241b83cc3f9e3f0c51ca92a5795351beb81b2 Mon Sep 17 00:00:00 2001 From: luxick Date: Thu, 6 Apr 2017 22:58:04 +0200 Subject: [PATCH] Load library list in separate thread --- mtg-collector/cardlist.py | 52 ++++++++++++++++++++++++++++++++++----- mtg-collector/gui.py | 18 ++++++-------- mtg-collector/library.py | 16 +++++++----- mtg-collector/util.py | 23 ++++++++--------- 4 files changed, 75 insertions(+), 34 deletions(-) diff --git a/mtg-collector/cardlist.py b/mtg-collector/cardlist.py index de1719e..85dda96 100644 --- a/mtg-collector/cardlist.py +++ b/mtg-collector/cardlist.py @@ -2,7 +2,7 @@ import gi import util gi.require_version('Gtk', '3.0') -from gi.repository import Gtk, GdkPixbuf, Pango +from gi.repository import Gtk, GdkPixbuf, GObject class CardList(Gtk.ScrolledWindow): @@ -12,6 +12,8 @@ class CardList(Gtk.ScrolledWindow): self.set_hexpand(True) self.set_vexpand(True) + self.filtered = with_filter + # Columns are these: # 0 Multiverse ID # 1 Card Name @@ -25,7 +27,7 @@ class CardList(Gtk.ScrolledWindow): # 9 CMC # 10 Edition self.store = Gtk.ListStore(int, str, str, str, str, str, str, str, GdkPixbuf.Pixbuf, int, str) - if with_filter: + if self.filtered: self.filter = self.store.filter_new() self.filter_and_sort = Gtk.TreeModelSort(self.filter) self.filter_and_sort.set_sort_func(4, self.compare_rarity, None) @@ -38,8 +40,6 @@ class CardList(Gtk.ScrolledWindow): self.list.set_rules_hint(True) self.selection = self.list.get_selection() - - bold_renderer = Gtk.CellRendererText(xalign=0.5, yalign=0.5) bold_renderer.set_property("weight", 800) @@ -109,6 +109,10 @@ class CardList(Gtk.ScrolledWindow): progress_step = 1 / len(library) progress = 0.0 + if self.filtered: + self.list.freeze_child_notify() + self.list.set_model(None) + self.store.clear() if progressbar is not None: progressbar.set_fraction(progress) @@ -116,7 +120,7 @@ class CardList(Gtk.ScrolledWindow): if card.multiverse_id is not None: if card.supertypes is None: card.supertypes = "" - self.store.append([ + item =[ card.multiverse_id, card.name, " ".join(card.supertypes), @@ -127,10 +131,46 @@ class CardList(Gtk.ScrolledWindow): ", ".join(card.printings), util.create_mana_icons(card.mana_cost), card.cmc, - card.set_name]) + card.set_name] + self.store.append(item) progress += progress_step if progressbar is not None: progressbar.set_fraction(progress) + if self.filtered: + self.list.set_model(self.store) + self.list.thaw_child_notify() + + def update_generate(self, library, step=128): + n = 0 + self.store.clear() + self.list.freeze_child_notify() + for multiverse_id, card in library.items(): + if card.multiverse_id is not None: + if card.supertypes is None: + card.supertypes = "" + item =[ + card.multiverse_id, + card.name, + " ".join(card.supertypes), + " ".join(card.types), + card.rarity, + card.power, + card.toughness, + ", ".join(card.printings), + util.create_mana_icons(card.mana_cost), + card.cmc, + card.set_name] + self.store.append(item) + + n += 1 + if (n % step) == 0: + self.list.thaw_child_notify() + yield True + self.list.freeze_child_notify() + + self.list.thaw_child_notify() + # stop idle_add() + yield False def compare_rarity(self, model, row1, row2, user_data): # Column for rarity diff --git a/mtg-collector/gui.py b/mtg-collector/gui.py index daa3f00..ede8371 100644 --- a/mtg-collector/gui.py +++ b/mtg-collector/gui.py @@ -110,13 +110,9 @@ class MainWindow(Gtk.Window): # endregion - self.library = library.LibraryView() - - self.search = search.SearchView() - - self.view_dict = { - "library": self.library, - "search": self.search + self.views = { + "library": library.LibraryView(), + "search": search.SearchView() } self.view_buttons = { @@ -124,7 +120,7 @@ class MainWindow(Gtk.Window): "search": self.view_search } - self.view = self.view_dict[config.start_view] + self.view = self.views[config.start_view] self.view_buttons[config.start_view].set_sensitive(False) self.view_container = Gtk.Box() @@ -146,7 +142,7 @@ class MainWindow(Gtk.Window): self.view_container.show_all() def mb_view_switch(self, menu_item): - new_view = self.view_dict[menu_item.get_name()] + new_view = self.views[menu_item.get_name()] self.switch_view(new_view) for item in self.view_buttons.values(): item.set_sensitive(True) @@ -157,9 +153,9 @@ class MainWindow(Gtk.Window): def mb_export_lib(menu_item): util.export_library() - @staticmethod - def mb_import_lib(menu_item): + def mb_import_lib(self, menu_item): util.import_library() + self.view.reload() def mb_search_add_card(self, menu_item): self.search.on_add_delete(self.search.add_delete_button) diff --git a/mtg-collector/library.py b/mtg-collector/library.py index 77b4f91..05222aa 100644 --- a/mtg-collector/library.py +++ b/mtg-collector/library.py @@ -6,7 +6,7 @@ import details import cardlist import gi gi.require_version('Gtk', '3.0') -from gi.repository import Gtk, GdkPixbuf +from gi.repository import Gtk, GdkPixbuf, GObject class LibraryView(Gtk.Grid): @@ -80,15 +80,11 @@ class LibraryView(Gtk.Grid): self.attach(right_pane, 4, 0, 1, 3) - self.refresh_library(self.refresh_button) - def reload(self): self.refresh_library() def refresh_library(self, button=None): - self.search_entry.set_text("") self.search_entry.activate() - self.fill_lib_list() def lib_filter_func(self, model, iter, data): @@ -116,7 +112,15 @@ class LibraryView(Gtk.Grid): self.remove_button.set_visible(True) def fill_lib_list(self): - self.lib_list.update(util.library) + # Fill List in thread + load_thread = threading.Thread(target=self.lib_list.update, args=(util.library, )) + load_thread.setDaemon(True) + load_thread.start() + + # Fill list during idle cycles + # loader = self.lib_list.update_generate(util.library) + # GObject.idle_add(loader.__next__) + self.details.reset() self.current_card = None self.remove_button.set_visible(False) diff --git a/mtg-collector/util.py b/mtg-collector/util.py index fe2a9fd..fa40b24 100644 --- a/mtg-collector/util.py +++ b/mtg-collector/util.py @@ -21,6 +21,7 @@ library = {} window = None status_bar = None +current_view = None unsaved_changes = False @@ -65,16 +66,11 @@ def import_library(): "Importing a library will override your current library. " "Proceed?") if override_question == Gtk.ResponseType.YES: - imported = None - # try: imported = pickle.load(open(dialog.get_filename(), 'rb')) - # except: - library.clear() for id, card in imported.items(): library[id] = card - save_library() - + unsaved_changes = True push_status("Library imported") print("Library imported") dialog.destroy() @@ -109,7 +105,7 @@ def load_library(): show_message("Error", "Error while loading library from disk") else: save_library() - print("No library file found on disk, created new one") + print("No library file found, created new one") def load_sets(): @@ -241,8 +237,13 @@ def create_mana_icons(mana_string): image.paste(loaded, (xpos, 0)) poscounter += 1 filename = "icon.png" - image.save(config.cache_path + filename) - pixbuf = GdkPixbuf.Pixbuf.new_from_file(config.cache_path + filename) - pixbuf = pixbuf.scale_simple(image.width / 5, image.height / 5, GdkPixbuf.InterpType.HYPER) - os.remove(config.cache_path + filename) + path = config.cache_path + filename + image.save(path) + try: + pixbuf = GdkPixbuf.Pixbuf.new_from_file(path) + pixbuf = pixbuf.scale_simple(image.width / 5, image.height / 5, GdkPixbuf.InterpType.HYPER) + except: + print("Error while loading file " + path) + return + # os.remove(config.cache_path + filename) return pixbuf