From 9993c9646ee9a4c781972b33d125e58530ac9c14 Mon Sep 17 00:00:00 2001 From: luxick Date: Thu, 16 Feb 2017 14:31:18 +0100 Subject: [PATCH] Search results show mana icons in the list --- gui.py | 1 + resources/mana_icons/{b.png => B.png} | Bin resources/mana_icons/{g.png => G.png} | Bin resources/mana_icons/{r.png => R.png} | Bin resources/mana_icons/{u.png => U.png} | Bin resources/mana_icons/{x.png => X.png} | Bin resources/mana_icons/{y.png => Y.png} | Bin resources/mana_icons/{z.png => Z.png} | Bin search.py | 31 ++++++++++--------- util.py | 43 ++++++++++++++++++++++++-- 10 files changed, 58 insertions(+), 17 deletions(-) rename resources/mana_icons/{b.png => B.png} (100%) rename resources/mana_icons/{g.png => G.png} (100%) rename resources/mana_icons/{r.png => R.png} (100%) rename resources/mana_icons/{u.png => U.png} (100%) rename resources/mana_icons/{x.png => X.png} (100%) rename resources/mana_icons/{y.png => Y.png} (100%) rename resources/mana_icons/{z.png => Z.png} (100%) diff --git a/gui.py b/gui.py index 73d0659..62da124 100644 --- a/gui.py +++ b/gui.py @@ -35,6 +35,7 @@ class MainWindow(Gtk.Window): win = MainWindow() # Load local image Data util.reload_image_cache() +util.load_mana_icons() win.connect('delete-event', Gtk.main_quit) win.show_all() Gtk.main() diff --git a/resources/mana_icons/b.png b/resources/mana_icons/B.png similarity index 100% rename from resources/mana_icons/b.png rename to resources/mana_icons/B.png diff --git a/resources/mana_icons/g.png b/resources/mana_icons/G.png similarity index 100% rename from resources/mana_icons/g.png rename to resources/mana_icons/G.png diff --git a/resources/mana_icons/r.png b/resources/mana_icons/R.png similarity index 100% rename from resources/mana_icons/r.png rename to resources/mana_icons/R.png diff --git a/resources/mana_icons/u.png b/resources/mana_icons/U.png similarity index 100% rename from resources/mana_icons/u.png rename to resources/mana_icons/U.png diff --git a/resources/mana_icons/x.png b/resources/mana_icons/X.png similarity index 100% rename from resources/mana_icons/x.png rename to resources/mana_icons/X.png diff --git a/resources/mana_icons/y.png b/resources/mana_icons/Y.png similarity index 100% rename from resources/mana_icons/y.png rename to resources/mana_icons/Y.png diff --git a/resources/mana_icons/z.png b/resources/mana_icons/Z.png similarity index 100% rename from resources/mana_icons/z.png rename to resources/mana_icons/Z.png diff --git a/search.py b/search.py index cbf1c46..d914dfd 100644 --- a/search.py +++ b/search.py @@ -36,7 +36,7 @@ class SearchView(Gtk.Grid): self.searchresults = Gtk.ScrolledWindow(hexpand=True, vexpand=True) self.searchresults.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC) - self.store = Gtk.ListStore(GdkPixbuf.Pixbuf, str, str, str) + self.store = Gtk.ListStore(GdkPixbuf.Pixbuf, str, str, GdkPixbuf.Pixbuf) self.list = Gtk.TreeView(self.store) self.searchresults.add(self.list) @@ -50,18 +50,18 @@ class SearchView(Gtk.Grid): info.set_property("wrap-width", 100) info.set_padding = 2 - manacost = Gtk.CellRendererText() + # manacost = Gtk.CellRendererText() self.column1 = Gtk.TreeViewColumn(title="Image", cell_renderer=image, pixbuf=0) - self.column2 = Gtk.TreeViewColumn(title="Card Name", cell_renderer=title, text=1) + self.column1.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE) + self.column2 = Gtk.TreeViewColumn(title="Name", cell_renderer=title, text=1) + self.column2.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE) self.column3 = Gtk.TreeViewColumn(title="Card Text", cell_renderer=info, text=2) - self.column4 = Gtk.TreeViewColumn(title="Mana Cost", cell_renderer=manacost, text=3) - self.column3.set_max_width(100) - - self.column1.pack_start(image, True) - self.column2.pack_start(title, True) - self.column3.pack_start(info, True) - self.column4.pack_start(manacost, True) + self.column3.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE) + self.column3.set_resizable(True) + self.column3.set_expand(True) + self.column4 = Gtk.TreeViewColumn(title="Mana Cost", cell_renderer=image, pixbuf=3) + self.column4.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE) self.list.append_column(self.column1) self.list.append_column(self.column2) @@ -76,21 +76,24 @@ class SearchView(Gtk.Grid): def online_search_clicked(self, button): term = self.searchEntry.get_text() if not term == "": - print("Search for \"" + term + "\" online.") + print("Search for \"" + term + "\" online. \n") cards = Card.where(name=term).where(pageSize=50).where(page=1).all() self.store.clear() for card in cards: if card.multiverse_id is not None: print("Found: " + card.name - + " (" + card.multiverse_id.__str__() + ")") + + " (" + card.multiverse_id.__str__() + ")") + self.store.append([util.load_card_image(card), card.name, card.original_text, - card.mana_cost]) - + util.create_mana_icons(card.mana_cost)]) + print("\n") util.reload_image_cache() + def create_row_entry(self, card): + cardname = card.name diff --git a/util.py b/util.py index 1cbb062..1294208 100644 --- a/util.py +++ b/util.py @@ -1,8 +1,7 @@ import os import gi - +import re import config - gi.require_version('Gtk', '3.0') from gi.repository import GdkPixbuf from PIL import Image as PImage @@ -10,8 +9,21 @@ from urllib import request # Loacally stored images for faster loading times imagecache = [] +manaicons ={} +def load_mana_icons(): + path = "resources/mana_icons/" + if not os.path.exists(path): + print("ERROR: Directory for mana icons not found") + return + # return array of icons + imagelist = os.listdir(path) + manaicons.clear() + for image in imagelist: + img = PImage.open(path + image) + manaicons[os.path.splitext(image)[0]] = img + def reload_image_cache(): if not os.path.exists(config.cachepath): os.makedirs(config.cachepath) @@ -44,8 +56,33 @@ def load_card_image(card): for image in imagecache: filename = os.path.basename(image.filename) if filename == card.multiverse_id.__str__() + ".PNG": - print("Using local file: " + filename) + print("Using local file for image: " + filename) return GdkPixbuf.Pixbuf.new_from_file_at_size(image.filename, 63 * 2, 88 * 2) # No file in local cache found return load_card_image_online(card) + + +def create_mana_icons(mana_string): + # Convert the string to a List + list = re.findall("\{(.*?)\}", mana_string) + # Compute horizontal size for the final image + imagesize = len(list) * 105 + image = PImage.new("RGBA", (imagesize, 105)) + # incerment for each position of an icon (Workaround: 2 or more of the same icon will be rendered in the same poisition) + poscounter = 0 + # Go through all entries an add the correspondent icon to the final image + for icon in list: + xpos = poscounter * 105 + loaded = manaicons.get(icon) + if loaded is None: + print("ERROR: No icon file named \"" + icon + "\" found.") + else: + image.paste(loaded, (xpos, 0)) + poscounter += 1 + + image.save(config.cachepath + "manaicon.png", "PNG") + pixbuf = GdkPixbuf.Pixbuf.new_from_file(config.cachepath + "manaicon.png") + pixbuf = pixbuf.scale_simple(image.width / 5, image.height / 5, GdkPixbuf.InterpType.HYPER) + os.remove(config.cachepath + "manaicon.png") + return pixbuf