Display card images and local image caching for faster loading times
This commit is contained in:
5
config.py
Normal file
5
config.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# Title of the Program Window
|
||||||
|
applicationtitle="MTG Collector (working title) v0.1"
|
||||||
|
|
||||||
|
# Path of image cache
|
||||||
|
cachepath=".cache/"
|
||||||
8
gui.py
8
gui.py
@@ -1,14 +1,15 @@
|
|||||||
import gi
|
import gi
|
||||||
import collection
|
import collection
|
||||||
import search
|
import search
|
||||||
|
import config
|
||||||
|
import util
|
||||||
gi.require_version('Gtk', '3.0')
|
gi.require_version('Gtk', '3.0')
|
||||||
from gi.repository import Gtk
|
from gi.repository import Gtk
|
||||||
|
|
||||||
|
|
||||||
class MainWindow(Gtk.Window):
|
class MainWindow(Gtk.Window):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
Gtk.Window.__init__(self, title="MTG Collector (working title) v0.1")
|
Gtk.Window.__init__(self, title=config.applicationtitle)
|
||||||
self.set_border_width(2)
|
self.set_border_width(2)
|
||||||
self.set_size_request(1000, 700)
|
self.set_size_request(1000, 700)
|
||||||
|
|
||||||
@@ -30,7 +31,10 @@ class MainWindow(Gtk.Window):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
win = MainWindow()
|
win = MainWindow()
|
||||||
|
# Load local image Data
|
||||||
|
util.imagecache = util.reload_image_cache()
|
||||||
win.connect('delete-event', Gtk.main_quit)
|
win.connect('delete-event', Gtk.main_quit)
|
||||||
win.show_all()
|
win.show_all()
|
||||||
Gtk.main()
|
Gtk.main()
|
||||||
|
|||||||
24
search.py
24
search.py
@@ -1,7 +1,6 @@
|
|||||||
import gi
|
import gi
|
||||||
from gi.repository import Pango
|
from gi.repository import Pango
|
||||||
from psutil._compat import xrange
|
import util
|
||||||
|
|
||||||
gi.require_version('Gtk', '3.0')
|
gi.require_version('Gtk', '3.0')
|
||||||
from gi.repository import Gtk, GdkPixbuf
|
from gi.repository import Gtk, GdkPixbuf
|
||||||
from mtgsdk import Card
|
from mtgsdk import Card
|
||||||
@@ -71,14 +70,19 @@ class SearchView(Gtk.Grid):
|
|||||||
|
|
||||||
def online_search_clicked(self, button):
|
def online_search_clicked(self, button):
|
||||||
term = self.searchEntry.get_text()
|
term = self.searchEntry.get_text()
|
||||||
print("Search for \"" + term + "\" online.")
|
if not term == "":
|
||||||
|
print("Search for \"" + term + "\" online.")
|
||||||
|
|
||||||
|
cards = Card.where(name=term).all()
|
||||||
|
self.store.clear()
|
||||||
|
for card in cards:
|
||||||
|
if card.multiverse_id is not None:
|
||||||
|
print("Found ID: " + card.multiverse_id.__str__() + " | " + card.name)
|
||||||
|
self.store.append([util.load_card_image(card),
|
||||||
|
card.name,
|
||||||
|
card.original_text])
|
||||||
|
util.reload_image_cache()
|
||||||
|
|
||||||
|
|
||||||
cards = Card.where(name=term).all()
|
|
||||||
self.store.clear()
|
|
||||||
for card in cards:
|
|
||||||
self.store.append([self.add_test_image(), card.name, card.original_text])
|
|
||||||
print("Found: " + card.name)
|
|
||||||
|
|
||||||
|
|
||||||
def add_test_image(self):
|
|
||||||
return GdkPixbuf.Pixbuf.new_from_file_at_size('./resources/images/demo.jpg', 63*2, 88*2)
|
|
||||||
|
|||||||
52
util.py
Normal file
52
util.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import os
|
||||||
|
import gi
|
||||||
|
|
||||||
|
import config
|
||||||
|
|
||||||
|
gi.require_version('Gtk', '3.0')
|
||||||
|
from gi.repository import GdkPixbuf
|
||||||
|
from PIL import Image as PImage
|
||||||
|
from urllib import request
|
||||||
|
|
||||||
|
# Loacally stored images for faster loading times
|
||||||
|
imagecache = []
|
||||||
|
|
||||||
|
|
||||||
|
def reload_image_cache():
|
||||||
|
if not os.path.exists(config.cachepath):
|
||||||
|
os.makedirs(config.cachepath)
|
||||||
|
|
||||||
|
# return array of images
|
||||||
|
imageslist = os.listdir(config.cachepath)
|
||||||
|
loadedimages = []
|
||||||
|
for image in imageslist:
|
||||||
|
img = PImage.open(config.cachepath + image)
|
||||||
|
loadedimages.append(img)
|
||||||
|
return loadedimages
|
||||||
|
|
||||||
|
|
||||||
|
def add_test_image():
|
||||||
|
return GdkPixbuf.Pixbuf.new_from_file_at_size('./resources/images/demo.jpg', 63 * 2, 88 * 2)
|
||||||
|
|
||||||
|
|
||||||
|
def load_card_image_online(card):
|
||||||
|
url = card.image_url
|
||||||
|
if url is None:
|
||||||
|
print("No Image URL provided")
|
||||||
|
return add_test_image()
|
||||||
|
filename = ".cache/" + card.multiverse_id.__str__() + ".PNG"
|
||||||
|
print("Loading image from: " + url)
|
||||||
|
response = request.urlretrieve(url, filename)
|
||||||
|
return GdkPixbuf.Pixbuf.new_from_file_at_size(filename, 63 * 2, 88 * 2)
|
||||||
|
|
||||||
|
|
||||||
|
def load_card_image(card):
|
||||||
|
# Try loading from disk, if file exists
|
||||||
|
for image in imagecache:
|
||||||
|
filename = os.path.basename(image.filename)
|
||||||
|
if filename == card.multiverse_id.__str__() + ".PNG":
|
||||||
|
print("Using local file: " + 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)
|
||||||
Reference in New Issue
Block a user