Performance: card search in separate thread

This commit is contained in:
luxick
2017-02-17 11:31:29 +01:00
parent c7275f9457
commit 0616df3292
2 changed files with 22 additions and 14 deletions

View File

@@ -2,9 +2,13 @@ import gi
from gi.repository import Pango from gi.repository import Pango
import util import util
import details import details
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf from gi.repository import Gtk, GdkPixbuf, GObject
from mtgsdk import Card from mtgsdk import Card
import threading
GObject.threads_init()
class SearchView(Gtk.Grid): class SearchView(Gtk.Grid):
@@ -12,6 +16,8 @@ class SearchView(Gtk.Grid):
Gtk.Grid.__init__(self) Gtk.Grid.__init__(self)
self.set_column_spacing(5) self.set_column_spacing(5)
self.cancelSearch = False
# Search Box # Search Box
self.searchbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=5, self.searchbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=5,
margin_end=5, margin_start=5, margin_top=5, margin_bottom=5) margin_end=5, margin_start=5, margin_top=5, margin_bottom=5)
@@ -36,8 +42,7 @@ class SearchView(Gtk.Grid):
self.filterBox.add(self.testRow) self.filterBox.add(self.testRow)
# Card List
#Card List
self.searchresults = Gtk.ScrolledWindow(hexpand=True, vexpand=True) self.searchresults = Gtk.ScrolledWindow(hexpand=True, vexpand=True)
self.searchresults.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC) self.searchresults.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
@@ -87,8 +92,6 @@ class SearchView(Gtk.Grid):
self.attach(Gtk.VSeparator(), 1, 0, 1, 2) self.attach(Gtk.VSeparator(), 1, 0, 1, 2)
self.attach(Gtk.VSeparator(), 3, 0, 1, 2) self.attach(Gtk.VSeparator(), 3, 0, 1, 2)
self.selection = self.list.get_selection() self.selection = self.list.get_selection()
self.selection.connect("changed", self.on_card_selected) self.selection.connect("changed", self.on_card_selected)
@@ -96,12 +99,19 @@ class SearchView(Gtk.Grid):
self.details.rulings.set_visible(False) self.details.rulings.set_visible(False)
def online_search_clicked(self, button): def online_search_clicked(self, button):
self.store.clear()
self.searchEntry.set_editable(False)
self.searchbutton.set_sensitive(False)
threading.Thread(target=self.load_cards).start()
def load_cards(self):
term = self.searchEntry.get_text() term = self.searchEntry.get_text()
if not term == "": if not term == "":
print("Search for \"" + term + "\" online. \n") print("Search for \"" + term + "\" online. \n")
self.cards = Card.where(name=term).where(pageSize=50).where(page=1).all() self.cards = Card.where(name=term).where(pageSize=50).where(page=1).all()
self.store.clear()
for card in self.cards: for card in self.cards:
if card.multiverse_id is not None: if card.multiverse_id is not None:
print("Found: " + card.name print("Found: " + card.name
@@ -114,6 +124,8 @@ class SearchView(Gtk.Grid):
util.create_mana_icons(card.mana_cost)]) util.create_mana_icons(card.mana_cost)])
print("\n") print("\n")
util.reload_image_cache() util.reload_image_cache()
self.searchEntry.set_editable(True)
self.searchbutton.set_sensitive(True)
def on_card_selected(self, selection): def on_card_selected(self, selection):
(model, pathlist) = selection.get_selected_rows() (model, pathlist) = selection.get_selected_rows()
@@ -127,7 +139,3 @@ class SearchView(Gtk.Grid):
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)

View File

@@ -39,7 +39,7 @@ def reload_image_cache():
def load_dummy_image(sizex, sizey): def load_dummy_image(sizex, sizey):
return GdkPixbuf.Pixbuf.new_from_file_at_size(os.path.dirname(__file__) + return GdkPixbuf.Pixbuf.new_from_file_at_size(os.path.dirname(__file__) +
'/resources/images/dummy.jpg', sizex, sizey) '/resources/images/dummy.jpg', sizex, sizey)
def load_card_image_online(card): def load_card_image_online(card, sizex, sizey):
url = card.image_url url = card.image_url
if url is None: if url is None:
print("No Image URL provided") print("No Image URL provided")
@@ -47,7 +47,7 @@ def load_card_image_online(card):
filename = config.cachepath + card.multiverse_id.__str__() + ".PNG" filename = config.cachepath + card.multiverse_id.__str__() + ".PNG"
print("Loading image from: " + url) print("Loading image from: " + url)
response = request.urlretrieve(url, filename) response = request.urlretrieve(url, filename)
return GdkPixbuf.Pixbuf.new_from_file_at_size(filename, 63 * 2, 88 * 2) return GdkPixbuf.Pixbuf.new_from_file_at_size(filename, sizex, sizey)
def load_card_image(card, sizex, sizey): def load_card_image(card, sizex, sizey):
@@ -59,7 +59,7 @@ def load_card_image(card, sizex, sizey):
return GdkPixbuf.Pixbuf.new_from_file_at_size(image.filename, sizex, sizey) return GdkPixbuf.Pixbuf.new_from_file_at_size(image.filename, sizex, sizey)
# No file in local cache found # No file in local cache found
return load_card_image_online(card) return load_card_image_online(card, sizex, sizey)
def create_mana_icons(mana_string): def create_mana_icons(mana_string):