Move card list update to CardList Class

This commit is contained in:
luxick
2017-04-05 15:56:24 +02:00
parent ac8b453081
commit 0e9dba59c0
4 changed files with 68 additions and 65 deletions

View File

@@ -105,6 +105,33 @@ class CardList(Gtk.ScrolledWindow):
self.list.append_column(col_mana) self.list.append_column(col_mana)
self.list.append_column(col_cmc) 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): def compare_rarity(self, model, row1, row2, user_data):
# Column for rarity # Column for rarity
sort_column = 4 sort_column = 4

View File

@@ -1,3 +1,5 @@
import threading
import config import config
import util import util
import details import details
@@ -78,13 +80,17 @@ class LibraryView(Gtk.Grid):
self.attach(right_pane, 4, 0, 1, 3) self.attach(right_pane, 4, 0, 1, 3)
self.fill_lib_list() self.refresh_library(self.refresh_button)
def refresh_library(self, button): def refresh_library(self, button):
self.fill_lib_list()
self.search_entry.set_text("") self.search_entry.set_text("")
self.search_entry.activate() 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): def lib_filter_func(self, model, iter, data):
if self.search_entry.get_text() is None or self.search_entry.get_text() == "": if self.search_entry.get_text() is None or self.search_entry.get_text() == "":
return True return True
@@ -110,27 +116,11 @@ class LibraryView(Gtk.Grid):
self.remove_button.set_visible(True) self.remove_button.set_visible(True)
def fill_lib_list(self): def fill_lib_list(self):
self.lib_list.store.clear() self.lib_list.update(util.library)
self.details.reset() self.details.reset()
self.current_card = None self.current_card = None
self.remove_button.set_visible(False) 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): def card_clicked(self, flowbox, flowboxchild):
card_id = self.flowbox_ids[flowboxchild.get_index()] card_id = self.flowbox_ids[flowboxchild.get_index()]
card = util.library[card_id] card = util.library[card_id]

View File

@@ -17,6 +17,7 @@ class SearchView(Gtk.Grid):
Gtk.Grid.__init__(self) Gtk.Grid.__init__(self)
self.set_column_spacing(5) self.set_column_spacing(5)
self.current_card = None self.current_card = None
self.card_lib = {}
# region Search Box # region Search Box
self.searchbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=5, 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) card_id = model.get_value(iter, 0)
selected_card = None selected_card = None
for card in self.cards: for id, card in self.card_lib.items():
if card.multiverse_id == card_id: if id == card_id:
selected_card = card selected_card = card
if selected_card is not None: if selected_card is not None:
self.details.set_card_detail(selected_card) self.details.set_card_detail(selected_card)
@@ -245,6 +246,7 @@ class SearchView(Gtk.Grid):
term = self.searchEntry.get_text() term = self.searchEntry.get_text()
# Lock down search controls # Lock down search controls
GObject.idle_add(self._do_activate_controls, False, priorty=GObject.PRIORITY_DEFAULT) GObject.idle_add(self._do_activate_controls, False, priorty=GObject.PRIORITY_DEFAULT)
# Get filter rules # Get filter rules
colorlist = self._get_color_filter() colorlist = self._get_color_filter()
tree_iter = self.rarity_combo.get_active_iter() tree_iter = self.rarity_combo.get_active_iter()
@@ -263,7 +265,7 @@ class SearchView(Gtk.Grid):
print("\nStart online search") print("\nStart online search")
GObject.idle_add(util.push_status, "Searching for cards", priorty=GObject.PRIORITY_DEFAULT) GObject.idle_add(util.push_status, "Searching for cards", priorty=GObject.PRIORITY_DEFAULT)
try: try:
self.cards = Card.where(name=term) \ cards = Card.where(name=term) \
.where(colorIdentity=",".join(colorlist)) \ .where(colorIdentity=",".join(colorlist)) \
.where(types=typefilter) \ .where(types=typefilter) \
.where(set=set_filter) \ .where(set=set_filter) \
@@ -276,8 +278,8 @@ class SearchView(Gtk.Grid):
GObject.idle_add(self._do_activate_controls, True, priorty=GObject.PRIORITY_DEFAULT) GObject.idle_add(self._do_activate_controls, True, priorty=GObject.PRIORITY_DEFAULT)
return return
print("Done. Found " + str(len(self.cards)) + " cards") print("Done. Found " + str(len(cards)) + " cards")
if len(self.cards) == 0: if len(cards) == 0:
messagetext = "No cards with name \"" + term + "\" found" messagetext = "No cards with name \"" + term + "\" found"
GObject.idle_add(util.show_message, "No Results", messagetext, priority=GObject.PRIORITY_DEFAULT) GObject.idle_add(util.show_message, "No Results", messagetext, priority=GObject.PRIORITY_DEFAULT)
# Reactivate search controls # Reactivate search controls
@@ -287,46 +289,17 @@ class SearchView(Gtk.Grid):
# Remove duplicate entries # Remove duplicate entries
if config.show_from_all_sets is False: if config.show_from_all_sets is False:
unique_cards = [] cards = self.remove_duplicates(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)
# 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_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) GObject.idle_add(util.push_status, "Loading cards...", priorty=GObject.PRIORITY_DEFAULT)
for card in self.cards: # Add search results to list
if card.multiverse_id is not None: self.card_lib.clear()
if card.supertypes is None: for card in cards:
card.supertypes = "" self.card_lib[card.multiverse_id] = card
self.search_results.store.append([ self.search_results.update(self.card_lib, self.progressbar)
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)
# Reactivate search controls # Reactivate search controls
GObject.idle_add(self._do_activate_controls, True, priority=GObject.PRIORITY_DEFAULT) GObject.idle_add(self._do_activate_controls, True, priority=GObject.PRIORITY_DEFAULT)
GObject.idle_add(util.push_status, "", priorty=GObject.PRIORITY_DEFAULT) GObject.idle_add(util.push_status, "", priorty=GObject.PRIORITY_DEFAULT)
@@ -339,6 +312,20 @@ class SearchView(Gtk.Grid):
# region Private Functions # 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): def _do_update_add_button(self):
if not util.library.__contains__(self.current_card.multiverse_id): if not util.library.__contains__(self.current_card.multiverse_id):
self.add_delete_button.set_label("Add to Library") self.add_delete_button.set_label("Add to Library")

View File

@@ -1,5 +1,4 @@
import os import os
import datetime import datetime
import gi import gi
import re import re
@@ -241,9 +240,9 @@ def create_mana_icons(mana_string):
else: else:
image.paste(loaded, (xpos, 0)) image.paste(loaded, (xpos, 0))
poscounter += 1 poscounter += 1
filename = "icon.png"
image.save(config.cache_path + "manaicon.png", "PNG") image.save(config.cache_path + filename)
pixbuf = GdkPixbuf.Pixbuf.new_from_file(config.cache_path + "manaicon.png") pixbuf = GdkPixbuf.Pixbuf.new_from_file(config.cache_path + filename)
pixbuf = pixbuf.scale_simple(image.width / 5, image.height / 5, GdkPixbuf.InterpType.HYPER) 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 return pixbuf