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

View File

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

View File

@@ -52,7 +52,7 @@ class Handlers:
results = search_funct.search_cards(search_term)
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)

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.rulings = response_dict.get('rulings')
self.foreign_names = response_dict.get('foreignNames')
self.owned = None
@staticmethod
def find(id):

View File

@@ -3,6 +3,7 @@ import datetime
import gi
import re
import config
import enum
import network
from gi.repository import GdkPixbuf, Gtk
from PIL import Image as PImage
@@ -14,6 +15,8 @@ gi.require_version('Gtk', '3.0')
# Locally stored images for faster loading times
imagecache = {}
manaicons = {}
mana_icons_preconstructed = {}
set_list = []
set_dict = {}
@@ -49,6 +52,7 @@ rarity_dict = {
}
card_types = ["Creature", "Artifact", "Instant", "Enchantment", "Sorcery", "Land", "Planeswalker"]
def export_library():
dialog = Gtk.FileChooserDialog("Export Library", app.ui.get_object("mainWindow"),
Gtk.FileChooserAction.SAVE,
@@ -171,6 +175,25 @@ def reload_image_cache():
except OSError as 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
@@ -288,6 +311,17 @@ def load_card_image(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):
# Convert the string to a List
list = re.findall("{(.*?)}", str(mana_string))
@@ -307,13 +341,12 @@ def create_mana_icons(mana_string):
else:
image.paste(loaded, (xpos, 0))
poscounter += 1
filename = "icon.png"
path = config.cache_path + filename
path = config.icon_cache_path + mana_string.replace("/", "") + ".png"
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:
return
# os.remove(path)
mana_icons_preconstructed[mana_string.replace("/", "") + ".png"] = pixbuf
return pixbuf

View File

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