Lock search controls while search is running

This commit is contained in:
luxick
2017-02-19 23:41:54 +01:00
parent 0b26c57a1d
commit e857aed150
2 changed files with 37 additions and 40 deletions

View File

@@ -4,7 +4,7 @@ import search
import config import config
import util import util
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
from gi.repository import Gtk from gi.repository import Gtk, GObject
class MainWindow(Gtk.Window): class MainWindow(Gtk.Window):
@@ -36,5 +36,6 @@ class MainWindow(Gtk.Window):
win = MainWindow() win = MainWindow()
win.connect('delete-event', Gtk.main_quit) win.connect('delete-event', Gtk.main_quit)
GObject.threads_init()
win.show_all() win.show_all()
Gtk.main() Gtk.main()

View File

@@ -9,9 +9,6 @@ from gi.repository import Gtk, GdkPixbuf, GObject
from mtgsdk import Card from mtgsdk import Card
import threading import threading
GObject.threads_init()
class SearchView(Gtk.Grid): class SearchView(Gtk.Grid):
def __init__(self): def __init__(self):
Gtk.Grid.__init__(self) Gtk.Grid.__init__(self)
@@ -118,22 +115,35 @@ class SearchView(Gtk.Grid):
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)
def on_appering(self): def do_activate_controls(self, active):
self.details.rulings.set_visible(False) self.searchEntry.set_editable(active)
self.searchEntry.set_sensitive(active)
self.searchbutton.set_sensitive(active)
self.red_mana_button.set_sensitive(active)
self.blue_mana_button.set_sensitive(active)
self.black_mana_button.set_sensitive(active)
self.green_mana_button.set_sensitive(active)
self.white_mana_button.set_sensitive(active)
self.colorless_mana_button.set_sensitive(active)
def online_search_clicked(self, button): def online_search_clicked(self, button):
# Clear old data from liststore
self.store.clear() self.store.clear()
self.searchEntry.set_editable(False) # Define the function to load cards in a seperate thread, so the UI is not blocked
self.searchbutton.set_sensitive(False) self.loadthread = threading.Thread(target=self.load_cards)
# Deamonize Thread so it tops if the main thread exits
threading.Thread(target=self.load_cards).start() self.loadthread.setDaemon(True)
# Start to load cards
self.loadthread.start()
def load_cards(self): def load_cards(self):
# Get search term
term = self.searchEntry.get_text() term = self.searchEntry.get_text()
print("Search for \"" + term + "\" online.") print("Search for \"" + term + "\" online.")
# Lock down search controls
GObject.idle_add(self.do_activate_controls, False, priorty=GObject.PRIORITY_DEFAULT)
# Get filter rules
colorlist = self.get_color_filter() colorlist = self.get_color_filter()
print("Filtering color(s): " + ','.join(colorlist) + "\n")
# Load card info from internet # Load card info from internet
self.cards = Card.where(name=term)\ self.cards = Card.where(name=term)\
.where(colorIdentity=','.join(colorlist))\ .where(colorIdentity=','.join(colorlist))\
@@ -142,7 +152,6 @@ class SearchView(Gtk.Grid):
# Remove duplicate entries # Remove duplicate entries
if config.show_from_all_sets is False: if config.show_from_all_sets is False:
counter = 0
unique_cards = [] unique_cards = []
unique_names = [] unique_names = []
# Reverse cardlist so we get the version with the most modern art # Reverse cardlist so we get the version with the most modern art
@@ -150,13 +159,13 @@ class SearchView(Gtk.Grid):
if card.name not in unique_names: if card.name not in unique_names:
unique_names.append(card.name) unique_names.append(card.name)
unique_cards.append(card) unique_cards.append(card)
else: duplicatecounter = len(self.cards) - len(unique_cards)
counter += 1
self.cards.clear() self.cards.clear()
for unique_card in reversed(unique_cards): for unique_card in reversed(unique_cards):
self.cards.append(unique_card) self.cards.append(unique_card)
print("Removed " + str(counter) + " duplicate entries") # Show count of removed duplicates
print("Removed " + str(duplicatecounter) + " duplicate entries")
for card in self.cards: for card in self.cards:
if card.multiverse_id is not None: if card.multiverse_id is not None:
@@ -169,10 +178,10 @@ class SearchView(Gtk.Grid):
card.original_text, card.original_text,
util.create_mana_icons(card.mana_cost)]) util.create_mana_icons(card.mana_cost)])
print("") print("")
# Reload image cache to include new cards
util.reload_image_cache() util.reload_image_cache()
self.searchEntry.set_editable(True) # Reactivate search controls
self.searchbutton.set_sensitive(True) GObject.idle_add(self.do_activate_controls, True, priority=GObject.PRIORITY_DEFAULT)
def on_card_selected(self, selection): def on_card_selected(self, selection):
(model, pathlist) = selection.get_selected_rows() (model, pathlist) = selection.get_selected_rows()
@@ -189,19 +198,11 @@ class SearchView(Gtk.Grid):
def get_color_filter(self): def get_color_filter(self):
colorlist = [] colorlist = []
# Go through mana color buttons an get the active filters
if not self.white_mana_button.get_active(): for widget in self.color_chooser:
colorlist.append("W") if isinstance(widget, Gtk.ToggleButton):
if not self.blue_mana_button.get_active(): if not widget.get_active():
colorlist.append("U") colorlist.append(widget.get_name())
if not self.black_mana_button.get_active():
colorlist.append("B")
if not self.red_mana_button.get_active():
colorlist.append("R")
if not self.green_mana_button.get_active():
colorlist.append("G")
if not self.colorless_mana_button.get_active():
colorlist.append("C")
return colorlist return colorlist
def mana_toggled(self, toggle_button): def mana_toggled(self, toggle_button):
@@ -216,11 +217,6 @@ class SearchView(Gtk.Grid):
def init_mana_buttons(self): def init_mana_buttons(self):
# Toggle each Button to deactivate filter an load icon # Toggle each Button to deactivate filter an load icon
self.red_mana_button.toggled() for widget in self.color_chooser:
self.blue_mana_button.toggled() if isinstance(widget, Gtk.ToggleButton):
self.green_mana_button.toggled() widget.toggled()
self.black_mana_button.toggled()
self.white_mana_button.toggled()
self.colorless_mana_button.toggled()
return