Load library list in separate thread
This commit is contained in:
@@ -2,7 +2,7 @@ import gi
|
|||||||
import util
|
import util
|
||||||
|
|
||||||
gi.require_version('Gtk', '3.0')
|
gi.require_version('Gtk', '3.0')
|
||||||
from gi.repository import Gtk, GdkPixbuf, Pango
|
from gi.repository import Gtk, GdkPixbuf, GObject
|
||||||
|
|
||||||
|
|
||||||
class CardList(Gtk.ScrolledWindow):
|
class CardList(Gtk.ScrolledWindow):
|
||||||
@@ -12,6 +12,8 @@ class CardList(Gtk.ScrolledWindow):
|
|||||||
self.set_hexpand(True)
|
self.set_hexpand(True)
|
||||||
self.set_vexpand(True)
|
self.set_vexpand(True)
|
||||||
|
|
||||||
|
self.filtered = with_filter
|
||||||
|
|
||||||
# Columns are these:
|
# Columns are these:
|
||||||
# 0 Multiverse ID
|
# 0 Multiverse ID
|
||||||
# 1 Card Name
|
# 1 Card Name
|
||||||
@@ -25,7 +27,7 @@ class CardList(Gtk.ScrolledWindow):
|
|||||||
# 9 CMC
|
# 9 CMC
|
||||||
# 10 Edition
|
# 10 Edition
|
||||||
self.store = Gtk.ListStore(int, str, str, str, str, str, str, str, GdkPixbuf.Pixbuf, int, str)
|
self.store = Gtk.ListStore(int, str, str, str, str, str, str, str, GdkPixbuf.Pixbuf, int, str)
|
||||||
if with_filter:
|
if self.filtered:
|
||||||
self.filter = self.store.filter_new()
|
self.filter = self.store.filter_new()
|
||||||
self.filter_and_sort = Gtk.TreeModelSort(self.filter)
|
self.filter_and_sort = Gtk.TreeModelSort(self.filter)
|
||||||
self.filter_and_sort.set_sort_func(4, self.compare_rarity, None)
|
self.filter_and_sort.set_sort_func(4, self.compare_rarity, None)
|
||||||
@@ -38,8 +40,6 @@ class CardList(Gtk.ScrolledWindow):
|
|||||||
self.list.set_rules_hint(True)
|
self.list.set_rules_hint(True)
|
||||||
self.selection = self.list.get_selection()
|
self.selection = self.list.get_selection()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bold_renderer = Gtk.CellRendererText(xalign=0.5, yalign=0.5)
|
bold_renderer = Gtk.CellRendererText(xalign=0.5, yalign=0.5)
|
||||||
bold_renderer.set_property("weight", 800)
|
bold_renderer.set_property("weight", 800)
|
||||||
|
|
||||||
@@ -109,6 +109,10 @@ class CardList(Gtk.ScrolledWindow):
|
|||||||
progress_step = 1 / len(library)
|
progress_step = 1 / len(library)
|
||||||
progress = 0.0
|
progress = 0.0
|
||||||
|
|
||||||
|
if self.filtered:
|
||||||
|
self.list.freeze_child_notify()
|
||||||
|
self.list.set_model(None)
|
||||||
|
|
||||||
self.store.clear()
|
self.store.clear()
|
||||||
if progressbar is not None:
|
if progressbar is not None:
|
||||||
progressbar.set_fraction(progress)
|
progressbar.set_fraction(progress)
|
||||||
@@ -116,7 +120,7 @@ class CardList(Gtk.ScrolledWindow):
|
|||||||
if card.multiverse_id is not None:
|
if card.multiverse_id is not None:
|
||||||
if card.supertypes is None:
|
if card.supertypes is None:
|
||||||
card.supertypes = ""
|
card.supertypes = ""
|
||||||
self.store.append([
|
item =[
|
||||||
card.multiverse_id,
|
card.multiverse_id,
|
||||||
card.name,
|
card.name,
|
||||||
" ".join(card.supertypes),
|
" ".join(card.supertypes),
|
||||||
@@ -127,10 +131,46 @@ class CardList(Gtk.ScrolledWindow):
|
|||||||
", ".join(card.printings),
|
", ".join(card.printings),
|
||||||
util.create_mana_icons(card.mana_cost),
|
util.create_mana_icons(card.mana_cost),
|
||||||
card.cmc,
|
card.cmc,
|
||||||
card.set_name])
|
card.set_name]
|
||||||
|
self.store.append(item)
|
||||||
progress += progress_step
|
progress += progress_step
|
||||||
if progressbar is not None:
|
if progressbar is not None:
|
||||||
progressbar.set_fraction(progress)
|
progressbar.set_fraction(progress)
|
||||||
|
if self.filtered:
|
||||||
|
self.list.set_model(self.store)
|
||||||
|
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
|
||||||
|
|||||||
@@ -110,13 +110,9 @@ class MainWindow(Gtk.Window):
|
|||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
self.library = library.LibraryView()
|
self.views = {
|
||||||
|
"library": library.LibraryView(),
|
||||||
self.search = search.SearchView()
|
"search": search.SearchView()
|
||||||
|
|
||||||
self.view_dict = {
|
|
||||||
"library": self.library,
|
|
||||||
"search": self.search
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.view_buttons = {
|
self.view_buttons = {
|
||||||
@@ -124,7 +120,7 @@ class MainWindow(Gtk.Window):
|
|||||||
"search": self.view_search
|
"search": self.view_search
|
||||||
}
|
}
|
||||||
|
|
||||||
self.view = self.view_dict[config.start_view]
|
self.view = self.views[config.start_view]
|
||||||
self.view_buttons[config.start_view].set_sensitive(False)
|
self.view_buttons[config.start_view].set_sensitive(False)
|
||||||
|
|
||||||
self.view_container = Gtk.Box()
|
self.view_container = Gtk.Box()
|
||||||
@@ -146,7 +142,7 @@ class MainWindow(Gtk.Window):
|
|||||||
self.view_container.show_all()
|
self.view_container.show_all()
|
||||||
|
|
||||||
def mb_view_switch(self, menu_item):
|
def mb_view_switch(self, menu_item):
|
||||||
new_view = self.view_dict[menu_item.get_name()]
|
new_view = self.views[menu_item.get_name()]
|
||||||
self.switch_view(new_view)
|
self.switch_view(new_view)
|
||||||
for item in self.view_buttons.values():
|
for item in self.view_buttons.values():
|
||||||
item.set_sensitive(True)
|
item.set_sensitive(True)
|
||||||
@@ -157,9 +153,9 @@ class MainWindow(Gtk.Window):
|
|||||||
def mb_export_lib(menu_item):
|
def mb_export_lib(menu_item):
|
||||||
util.export_library()
|
util.export_library()
|
||||||
|
|
||||||
@staticmethod
|
def mb_import_lib(self, menu_item):
|
||||||
def mb_import_lib(menu_item):
|
|
||||||
util.import_library()
|
util.import_library()
|
||||||
|
self.view.reload()
|
||||||
|
|
||||||
def mb_search_add_card(self, menu_item):
|
def mb_search_add_card(self, menu_item):
|
||||||
self.search.on_add_delete(self.search.add_delete_button)
|
self.search.on_add_delete(self.search.add_delete_button)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import details
|
|||||||
import cardlist
|
import cardlist
|
||||||
import gi
|
import gi
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
class LibraryView(Gtk.Grid):
|
class LibraryView(Gtk.Grid):
|
||||||
@@ -80,15 +80,11 @@ class LibraryView(Gtk.Grid):
|
|||||||
|
|
||||||
self.attach(right_pane, 4, 0, 1, 3)
|
self.attach(right_pane, 4, 0, 1, 3)
|
||||||
|
|
||||||
self.refresh_library(self.refresh_button)
|
|
||||||
|
|
||||||
def reload(self):
|
def reload(self):
|
||||||
self.refresh_library()
|
self.refresh_library()
|
||||||
|
|
||||||
def refresh_library(self, button=None):
|
def refresh_library(self, button=None):
|
||||||
self.search_entry.set_text("")
|
|
||||||
self.search_entry.activate()
|
self.search_entry.activate()
|
||||||
|
|
||||||
self.fill_lib_list()
|
self.fill_lib_list()
|
||||||
|
|
||||||
def lib_filter_func(self, model, iter, data):
|
def lib_filter_func(self, model, iter, data):
|
||||||
@@ -116,7 +112,15 @@ class LibraryView(Gtk.Grid):
|
|||||||
self.remove_button.set_visible(True)
|
self.remove_button.set_visible(True)
|
||||||
|
|
||||||
def fill_lib_list(self):
|
def fill_lib_list(self):
|
||||||
self.lib_list.update(util.library)
|
# Fill List in thread
|
||||||
|
load_thread = threading.Thread(target=self.lib_list.update, args=(util.library, ))
|
||||||
|
load_thread.setDaemon(True)
|
||||||
|
load_thread.start()
|
||||||
|
|
||||||
|
# Fill list during idle cycles
|
||||||
|
# loader = self.lib_list.update_generate(util.library)
|
||||||
|
# GObject.idle_add(loader.__next__)
|
||||||
|
|
||||||
self.details.reset()
|
self.details.reset()
|
||||||
self.current_card = None
|
self.current_card = None
|
||||||
self.remove_button.set_visible(False)
|
self.remove_button.set_visible(False)
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ library = {}
|
|||||||
|
|
||||||
window = None
|
window = None
|
||||||
status_bar = None
|
status_bar = None
|
||||||
|
current_view = None
|
||||||
|
|
||||||
unsaved_changes = False
|
unsaved_changes = False
|
||||||
|
|
||||||
@@ -65,16 +66,11 @@ def import_library():
|
|||||||
"Importing a library will override your current library. "
|
"Importing a library will override your current library. "
|
||||||
"Proceed?")
|
"Proceed?")
|
||||||
if override_question == Gtk.ResponseType.YES:
|
if override_question == Gtk.ResponseType.YES:
|
||||||
imported = None
|
|
||||||
# try:
|
|
||||||
imported = pickle.load(open(dialog.get_filename(), 'rb'))
|
imported = pickle.load(open(dialog.get_filename(), 'rb'))
|
||||||
# except:
|
|
||||||
|
|
||||||
library.clear()
|
library.clear()
|
||||||
for id, card in imported.items():
|
for id, card in imported.items():
|
||||||
library[id] = card
|
library[id] = card
|
||||||
save_library()
|
unsaved_changes = True
|
||||||
|
|
||||||
push_status("Library imported")
|
push_status("Library imported")
|
||||||
print("Library imported")
|
print("Library imported")
|
||||||
dialog.destroy()
|
dialog.destroy()
|
||||||
@@ -109,7 +105,7 @@ def load_library():
|
|||||||
show_message("Error", "Error while loading library from disk")
|
show_message("Error", "Error while loading library from disk")
|
||||||
else:
|
else:
|
||||||
save_library()
|
save_library()
|
||||||
print("No library file found on disk, created new one")
|
print("No library file found, created new one")
|
||||||
|
|
||||||
|
|
||||||
def load_sets():
|
def load_sets():
|
||||||
@@ -241,8 +237,13 @@ def create_mana_icons(mana_string):
|
|||||||
image.paste(loaded, (xpos, 0))
|
image.paste(loaded, (xpos, 0))
|
||||||
poscounter += 1
|
poscounter += 1
|
||||||
filename = "icon.png"
|
filename = "icon.png"
|
||||||
image.save(config.cache_path + filename)
|
path = config.cache_path + filename
|
||||||
pixbuf = GdkPixbuf.Pixbuf.new_from_file(config.cache_path + filename)
|
image.save(path)
|
||||||
pixbuf = pixbuf.scale_simple(image.width / 5, image.height / 5, GdkPixbuf.InterpType.HYPER)
|
try:
|
||||||
os.remove(config.cache_path + filename)
|
pixbuf = GdkPixbuf.Pixbuf.new_from_file(path)
|
||||||
|
pixbuf = pixbuf.scale_simple(image.width / 5, image.height / 5, GdkPixbuf.InterpType.HYPER)
|
||||||
|
except:
|
||||||
|
print("Error while loading file " + path)
|
||||||
|
return
|
||||||
|
# os.remove(config.cache_path + filename)
|
||||||
return pixbuf
|
return pixbuf
|
||||||
|
|||||||
Reference in New Issue
Block a user