Cache combined mana icons. Improve list load times.

This commit is contained in:
luxick
2017-04-16 19:58:48 +02:00
parent a6897f0d5c
commit 8da089d39f
7 changed files with 72 additions and 44 deletions

View File

@@ -1,9 +1,10 @@
import gi import gi
import util import util
from logger import *
from gi.repository import Gtk, GdkPixbuf, Gdk
import time
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0') gi.require_version('Gdk', '3.0')
from gi.repository import Gtk, GdkPixbuf, Gdk
class CardList(Gtk.ScrolledWindow): class CardList(Gtk.ScrolledWindow):
@@ -119,19 +120,23 @@ class CardList(Gtk.ScrolledWindow):
output[card_id] = card output[card_id] = card
return output return output
def update(self, library): def update(self, library, colorize=False):
self.store.clear() self.store.clear()
if library is None: if library is None:
return return
self.lib = library self.lib = library
if self.filtered: if self.filtered:
self.list.freeze_child_notify() self.list.freeze_child_notify()
self.list.set_model(None) self.list.set_model(None)
for multiverse_id, card in library.items(): start = time.time()
for card_id, card in library.items():
if card.multiverse_id is not None: if card.multiverse_id is not None:
color = ""
if util.library.__contains__(multiverse_id): if util.library.__contains__(card_id) and colorize:
color = util.card_view_colors["owned"] color = util.card_view_colors["owned"]
else: else:
color = util.card_view_colors["unowned"] color = util.card_view_colors["unowned"]
@@ -145,48 +150,20 @@ class CardList(Gtk.ScrolledWindow):
card.power, card.power,
card.toughness, card.toughness,
", ".join(card.printings), ", ".join(card.printings),
util.create_mana_icons(card.mana_cost), util.get_mana_icons(card.mana_cost),
card.cmc, card.cmc,
card.set_name, card.set_name,
color] color]
self.store.append(item) self.store.append(item)
end = time.time()
log("Time to build Table: " + str(round(end - start, 3)), LogLevel.Info)
if self.filtered: if self.filtered:
self.list.set_model(self.filter_and_sort) self.list.set_model(self.filter_and_sort)
self.list.thaw_child_notify() 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): def compare_rarity(self, model, row1, row2, user_data):
# Column for rarity # Column for rarity
sort_column = 4 sort_column = 4

View File

@@ -11,6 +11,7 @@ application_title = "Card Vault v0.5"
# Path of image cache # Path of image cache
cache_path = os.path.dirname(__file__) + "/.cache/" cache_path = os.path.dirname(__file__) + "/.cache/"
image_cache_path = os.path.dirname(__file__) + "/.cache/images/" image_cache_path = os.path.dirname(__file__) + "/.cache/images/"
icon_cache_path = os.path.dirname(__file__) + "/.cache/icons/"
# Colors to use in the Application # Colors to use in the Application
green_color = Gdk.color_parse('#87ff89') green_color = Gdk.color_parse('#87ff89')
@@ -20,3 +21,5 @@ red_color = Gdk.color_parse('#ff6d6d')
show_from_all_sets = True show_from_all_sets = True
start_page = "search" start_page = "search"
log_level = 3

View File

@@ -52,7 +52,7 @@ class Handlers:
results = search_funct.search_cards(search_term) results = search_funct.search_cards(search_term)
card_list = self.app.ui.get_object("searchResults").get_child() card_list = self.app.ui.get_object("searchResults").get_child()
card_list.update(results) card_list.update(results, colorize=True)
self.app.ui.get_object("searchOverlay").set_visible(False) self.app.ui.get_object("searchOverlay").set_visible(False)

14
cardvault/logger.py Normal file
View File

@@ -0,0 +1,14 @@
import config
import enum
class LogLevel(enum.Enum):
Error = 1
Warning = 2
Info = 3
def log(message, log_level):
if log_level.value <= config.log_level:
level_string = "[" + log_level.name + "] "
print(level_string + message)

View File

@@ -53,6 +53,7 @@ class Card(object):
self.legalities = response_dict.get('legalities') self.legalities = response_dict.get('legalities')
self.rulings = response_dict.get('rulings') self.rulings = response_dict.get('rulings')
self.foreign_names = response_dict.get('foreignNames') self.foreign_names = response_dict.get('foreignNames')
self.owned = None
@staticmethod @staticmethod
def find(id): def find(id):

View File

@@ -3,6 +3,7 @@ import datetime
import gi import gi
import re import re
import config import config
import enum
import network import network
from gi.repository import GdkPixbuf, Gtk from gi.repository import GdkPixbuf, Gtk
from PIL import Image as PImage from PIL import Image as PImage
@@ -14,6 +15,8 @@ gi.require_version('Gtk', '3.0')
# Locally stored images for faster loading times # Locally stored images for faster loading times
imagecache = {} imagecache = {}
manaicons = {} manaicons = {}
mana_icons_preconstructed = {}
set_list = [] set_list = []
set_dict = {} set_dict = {}
@@ -49,6 +52,7 @@ rarity_dict = {
} }
card_types = ["Creature", "Artifact", "Instant", "Enchantment", "Sorcery", "Land", "Planeswalker"] card_types = ["Creature", "Artifact", "Instant", "Enchantment", "Sorcery", "Land", "Planeswalker"]
def export_library(): def export_library():
dialog = Gtk.FileChooserDialog("Export Library", app.ui.get_object("mainWindow"), dialog = Gtk.FileChooserDialog("Export Library", app.ui.get_object("mainWindow"),
Gtk.FileChooserAction.SAVE, Gtk.FileChooserAction.SAVE,
@@ -171,6 +175,25 @@ def reload_image_cache():
except OSError as err: except OSError as err:
print("Error loading image: " + str(err)) print("Error loading image: " + str(err))
def reload_preconstructed_icons():
if not os.path.exists(config.icon_cache_path):
os.makedirs(config.icon_cache_path)
icon_list = os.listdir(config.icon_cache_path)
mana_icons_preconstructed.clear()
for icon in icon_list:
list = re.findall("{(.*?)}", str(icon))
pic_width = len(list) * 105
pic_height = 105
try:
pixbuf = GdkPixbuf.Pixbuf.new_from_file(config.icon_cache_path + icon)
pixbuf = pixbuf.scale_simple(pic_width / 5, pic_height / 5, GdkPixbuf.InterpType.HYPER)
mana_icons_preconstructed[icon] = pixbuf
except OSError as err:
print("Error loading icon: " + str(err))
# endregion # endregion
@@ -288,6 +311,17 @@ def load_card_image(card, sizex, sizey):
return load_card_image_online(card, sizex, sizey) return load_card_image_online(card, sizex, sizey)
def get_mana_icons(mana_string):
if not mana_string:
return
try:
icon = mana_icons_preconstructed[mana_string.replace("/", "") + ".png"]
except KeyError:
icon = create_mana_icons(mana_string)
mana_icons_preconstructed[mana_string] = icon
return icon
def create_mana_icons(mana_string): def create_mana_icons(mana_string):
# Convert the string to a List # Convert the string to a List
list = re.findall("{(.*?)}", str(mana_string)) list = re.findall("{(.*?)}", str(mana_string))
@@ -307,13 +341,12 @@ 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" path = config.icon_cache_path + mana_string.replace("/", "") + ".png"
path = config.cache_path + filename
image.save(path) image.save(path)
try: try:
pixbuf = GdkPixbuf.Pixbuf.new_from_file(path) pixbuf = GdkPixbuf.Pixbuf.new_from_file(path)
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)
except: except:
return return
# os.remove(path) mana_icons_preconstructed[mana_string.replace("/", "") + ".png"] = pixbuf
return pixbuf return pixbuf

View File

@@ -28,6 +28,7 @@ class MainWindow:
# Load local image Data # Load local image Data
util.reload_image_cache() util.reload_image_cache()
util.reload_preconstructed_icons()
util.load_mana_icons() util.load_mana_icons()
util.load_sets() util.load_sets()
@@ -125,7 +126,6 @@ class MainWindow:
window.show_all() window.show_all()
def eval_key_pressed(widget,event): def eval_key_pressed(widget,event):
key, modifier = Gtk.accelerator_parse('Escape') key, modifier = Gtk.accelerator_parse('Escape')
keyval = event.keyval keyval = event.keyval