diff --git a/mtg-collector/cardlist.py b/mtg-collector/cardlist.py index 55dbdbb..541a26b 100644 --- a/mtg-collector/cardlist.py +++ b/mtg-collector/cardlist.py @@ -105,6 +105,33 @@ class CardList(Gtk.ScrolledWindow): self.list.append_column(col_mana) self.list.append_column(col_cmc) + def update(self, library, progressbar=None): + self.store.clear() + progress_step = 1 / len(library) + progress = 0.0 + if progressbar is not None: + progressbar.set_fraction(progress) + for multiverse_id, card in library.items(): + if card.multiverse_id is not None: + if card.supertypes is None: + card.supertypes = "" + self.store.append([ + 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]) + + progress += progress_step + if progressbar is not None: + progressbar.set_fraction(progress) + def compare_rarity(self, model, row1, row2, user_data): # Column for rarity sort_column = 4 diff --git a/mtg-collector/library.py b/mtg-collector/library.py index 6eea1e4..7d143b4 100644 --- a/mtg-collector/library.py +++ b/mtg-collector/library.py @@ -1,3 +1,5 @@ +import threading + import config import util import details @@ -78,13 +80,17 @@ class LibraryView(Gtk.Grid): self.attach(right_pane, 4, 0, 1, 3) - self.fill_lib_list() + self.refresh_library(self.refresh_button) def refresh_library(self, button): - self.fill_lib_list() self.search_entry.set_text("") self.search_entry.activate() + self.fill_lib_list() + # load_thread = threading.Thread(target=self.fill_lib_list) + # load_thread.setDaemon(True) + # load_thread.start() + def lib_filter_func(self, model, iter, data): if self.search_entry.get_text() is None or self.search_entry.get_text() == "": return True @@ -110,27 +116,11 @@ class LibraryView(Gtk.Grid): self.remove_button.set_visible(True) def fill_lib_list(self): - self.lib_list.store.clear() + self.lib_list.update(util.library) self.details.reset() self.current_card = None self.remove_button.set_visible(False) - for id, card in util.library.items(): - if card.supertypes is None: - card.supertypes = "" - self.lib_list.store.append([ - 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]) - def card_clicked(self, flowbox, flowboxchild): card_id = self.flowbox_ids[flowboxchild.get_index()] card = util.library[card_id] diff --git a/mtg-collector/search.py b/mtg-collector/search.py index ebd2e96..66d1cd3 100644 --- a/mtg-collector/search.py +++ b/mtg-collector/search.py @@ -17,6 +17,7 @@ class SearchView(Gtk.Grid): Gtk.Grid.__init__(self) self.set_column_spacing(5) self.current_card = None + self.card_lib = {} # region Search Box self.searchbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=5, @@ -225,8 +226,8 @@ class SearchView(Gtk.Grid): card_id = model.get_value(iter, 0) selected_card = None - for card in self.cards: - if card.multiverse_id == card_id: + for id, card in self.card_lib.items(): + if id == card_id: selected_card = card if selected_card is not None: self.details.set_card_detail(selected_card) @@ -245,6 +246,7 @@ class SearchView(Gtk.Grid): term = self.searchEntry.get_text() # Lock down search controls GObject.idle_add(self._do_activate_controls, False, priorty=GObject.PRIORITY_DEFAULT) + # Get filter rules colorlist = self._get_color_filter() tree_iter = self.rarity_combo.get_active_iter() @@ -263,7 +265,7 @@ class SearchView(Gtk.Grid): print("\nStart online search") GObject.idle_add(util.push_status, "Searching for cards", priorty=GObject.PRIORITY_DEFAULT) try: - self.cards = Card.where(name=term) \ + cards = Card.where(name=term) \ .where(colorIdentity=",".join(colorlist)) \ .where(types=typefilter) \ .where(set=set_filter) \ @@ -276,8 +278,8 @@ class SearchView(Gtk.Grid): GObject.idle_add(self._do_activate_controls, True, priorty=GObject.PRIORITY_DEFAULT) return - print("Done. Found " + str(len(self.cards)) + " cards") - if len(self.cards) == 0: + print("Done. Found " + str(len(cards)) + " cards") + if len(cards) == 0: messagetext = "No cards with name \"" + term + "\" found" GObject.idle_add(util.show_message, "No Results", messagetext, priority=GObject.PRIORITY_DEFAULT) # Reactivate search controls @@ -287,46 +289,17 @@ class SearchView(Gtk.Grid): # Remove duplicate entries if config.show_from_all_sets is False: - unique_cards = [] - unique_names = [] - # Reverse cardlist so we get the version with the most modern art - for card in reversed(self.cards): - if card.name not in unique_names: - unique_names.append(card.name) - unique_cards.append(card) - duplicatecounter = len(self.cards) - len(unique_cards) - self.cards.clear() - for unique_card in reversed(unique_cards): - self.cards.append(unique_card) + cards = self.remove_duplicates(cards) - # Show count of removed duplicates - print("Removed " + str(duplicatecounter) + " duplicate entries") - - loadprogress_step = 1 / len(self.cards) - progress = 0.0 GObject.idle_add(self.progressbar.set_visible, True, priorty=GObject.PRIORITY_DEFAULT) - GObject.idle_add(self.progressbar.set_fraction, 0.0, priorty=GObject.PRIORITY_DEFAULT) GObject.idle_add(util.push_status, "Loading cards...", priorty=GObject.PRIORITY_DEFAULT) - for card in self.cards: - if card.multiverse_id is not None: - if card.supertypes is None: - card.supertypes = "" - self.search_results.store.append([ - 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]) - # update progress bar - progress += loadprogress_step - GObject.idle_add(self.progressbar.set_fraction, progress, priorty=GObject.PRIORITY_DEFAULT) + # Add search results to list + self.card_lib.clear() + for card in cards: + self.card_lib[card.multiverse_id] = card + self.search_results.update(self.card_lib, self.progressbar) + # Reactivate search controls GObject.idle_add(self._do_activate_controls, True, priority=GObject.PRIORITY_DEFAULT) GObject.idle_add(util.push_status, "", priorty=GObject.PRIORITY_DEFAULT) @@ -339,6 +312,20 @@ class SearchView(Gtk.Grid): # region Private Functions + def remove_duplicates(self, cards): + unique_cards = [] + unique_names = [] + # Reverse cardlist so we get the version with the most modern art + for card in reversed(cards): + if card.name not in unique_names: + unique_names.append(card.name) + unique_cards.append(card) + counter = len(cards) - len(unique_cards) + + # Show count of removed duplicates + print("Removed " + str(counter) + " duplicate entries") + return unique_cards + def _do_update_add_button(self): if not util.library.__contains__(self.current_card.multiverse_id): self.add_delete_button.set_label("Add to Library") diff --git a/mtg-collector/util.py b/mtg-collector/util.py index a04b1c3..68f293e 100644 --- a/mtg-collector/util.py +++ b/mtg-collector/util.py @@ -1,5 +1,4 @@ import os - import datetime import gi import re @@ -241,9 +240,9 @@ def create_mana_icons(mana_string): else: image.paste(loaded, (xpos, 0)) poscounter += 1 - - image.save(config.cache_path + "manaicon.png", "PNG") - pixbuf = GdkPixbuf.Pixbuf.new_from_file(config.cache_path + "manaicon.png") + 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 + "manaicon.png") + os.remove(config.cache_path + filename) return pixbuf