Refactor signal handlers into separate classes.

This commit is contained in:
luxick
2017-07-05 13:58:48 +02:00
parent bd8ec22dc0
commit 0518775205
9 changed files with 868 additions and 734 deletions

View File

@@ -18,8 +18,6 @@ from typing import Type, Dict, List
from cardvault import handlers from cardvault import handlers
from cardvault import util from cardvault import util
from cardvault import search_funct
from cardvault import lib_funct
from cardvault import wants_funct from cardvault import wants_funct
@@ -73,14 +71,6 @@ class Application:
self.handlers = handlers.Handlers(self) self.handlers = handlers.Handlers(self)
self.ui.connect_signals(self.handlers) self.ui.connect_signals(self.handlers)
# Initialize the views
search_funct.init_search_view(self)
lib_funct.init_library_view(self)
wants_funct.init_wants_view(self)
self.ui.get_object("mainWindow").connect('delete-event', Gtk.main_quit) self.ui.get_object("mainWindow").connect('delete-event', Gtk.main_quit)
self.ui.get_object("mainWindow").show_all() self.ui.get_object("mainWindow").show_all()
self.push_status("Card Vault ready.") self.push_status("Card Vault ready.")
@@ -89,7 +79,6 @@ class Application:
start_page = [page for page in view_menu.get_children() if page.get_name() == util.START_PAGE] start_page = [page for page in view_menu.get_children() if page.get_name() == util.START_PAGE]
start_page[0].activate() start_page[0].activate()
util.log("Launching Card Vault version {}".format(util.VERSION), util.LogLevel.Info) util.log("Launching Card Vault version {}".format(util.VERSION), util.LogLevel.Info)
def push_status(self, msg): def push_status(self, msg):

View File

@@ -0,0 +1,126 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkListStore" id="cardStore">
<columns>
<!-- column-name multiverse_id -->
<column type="gint"/>
<!-- column-name name -->
<column type="gchararray"/>
<!-- column-name supertypes -->
<column type="gchararray"/>
<!-- column-name types -->
<column type="gchararray"/>
<!-- column-name rarity -->
<column type="gchararray"/>
<!-- column-name power -->
<column type="gchararray"/>
<!-- column-name toughness -->
<column type="gchararray"/>
<!-- column-name printings -->
<column type="gchararray"/>
<!-- column-name mana_cost -->
<column type="GdkPixbuf"/>
<!-- column-name cmc -->
<column type="gint"/>
<!-- column-name set_name -->
<column type="gchararray"/>
<!-- column-name color -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkTreeView" id="cardTree">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">cardStore</property>
<property name="search_column">2</property>
<property name="enable_grid_lines">both</property>
<child internal-child="selection">
<object class="GtkTreeSelection"/>
</child>
<child>
<object class="GtkTreeViewColumn" id="col_name">
<property name="resizable">True</property>
<property name="title" translatable="yes">Card Name</property>
<property name="sort_indicator">True</property>
<property name="sort_column_id">1</property>
<child>
<object class="GtkCellRendererText" id="cell_bold">
<property name="weight">800</property>
</object>
<attributes>
<attribute name="foreground">11</attribute>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="col_supertypes">
<property name="title" translatable="yes">Supertypes</property>
<property name="sort_column_id">2</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="foreground">11</attribute>
<attribute name="text">2</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="col_types">
<property name="title" translatable="yes">Types</property>
<property name="sort_column_id">3</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="foreground">11</attribute>
<attribute name="text">3</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="col_rarity">
<property name="title" translatable="yes">Rarity</property>
<property name="sort_column_id">4</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="foreground">11</attribute>
<attribute name="text">4</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="col_set">
<property name="title" translatable="yes">Edition</property>
<property name="sort_column_id">10</property>
<child>
<object class="GtkCellRendererText"/>
<attributes>
<attribute name="foreground">11</attribute>
<attribute name="text">10</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="col_mana">
<property name="title" translatable="yes">Mana Cost</property>
<property name="sort_column_id">9</property>
<child>
<object class="GtkCellRendererPixbuf">
<property name="xalign">0.019999999552965164</property>
</object>
<attributes>
<attribute name="pixbuf">8</attribute>
</attributes>
</child>
</object>
</child>
</object>
</interface>

View File

@@ -1,22 +1,28 @@
import gi import gi
gi.require_version('Gtk', '3.0') gi.require_version('Gtk', '3.0')
import datetime import datetime
import os import os
from gi.repository import Gtk from gi.repository import Gtk
from cardvault import lib_funct
from cardvault import search_funct
from cardvault import wants_funct
from cardvault import util from cardvault import util
from cardvault import application from cardvault import application
from search import SearchHandlers
from library import LibraryHandlers
from wants import WantsHandlers
class Handlers:
class Handlers(SearchHandlers, LibraryHandlers, WantsHandlers):
def __init__(self, app: 'application.Application'): def __init__(self, app: 'application.Application'):
"""Initialize handlers for UI signals"""
self.app = app self.app = app
# ---------------------------------Main Window---------------------------------------------- # Call constructor of view handlers classes
SearchHandlers.__init__(self, app)
LibraryHandlers.__init__(self, app)
WantsHandlers.__init__(self, app)
# --------------------------------- Main Window Handlers ----------------------------------------------
def do_save_library(self, item): def do_save_library(self, item):
self.app.save_library() self.app.save_library()
@@ -85,441 +91,3 @@ class Handlers:
return False return False
elif response == Gtk.ResponseType.CANCEL: elif response == Gtk.ResponseType.CANCEL:
return True return True
# ---------------------------------Search----------------------------------------------
def do_search_cards(self, sender):
search_term = self.app.ui.get_object("searchEntry").get_text()
filters = search_funct.get_filters(self.app)
results = search_funct.search_cards(search_term, filters)
card_list = self.app.ui.get_object("searchResults").get_child()
card_list.update(results)
self.app.ui.get_object("searchOverlay").set_visible(False)
@staticmethod
def do_clear_mana_filter(mana_filter_grid):
for toggle_button in mana_filter_grid.get_children():
if isinstance(toggle_button, Gtk.ToggleButton):
toggle_button.set_active(False)
@staticmethod
def do_clear_set_filter(entry, icon_pos, button):
entry.set_text("")
def do_add_clicked(self, button):
card_view = self.app.ui.get_object("searchResults").get_child()
(model, pathlist) = card_view.selection.get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
card_id = model.get_value(tree_iter, 0)
card = card_view.lib[card_id]
self.app.add_card_to_lib(card)
search_funct.reload_search_view(self.app)
self.app.ui.get_object("searchEntry").grab_focus()
def search_tree_popup_showed(self, menu):
# Create tag submenu
tags_item = self.app.ui.get_object("searchListPopupAddTag")
tags_sub = Gtk.Menu()
tags_item.set_submenu(tags_sub)
for list_name in self.app.tags.keys():
item = Gtk.MenuItem()
tags_sub.add(item)
item.set_label(list_name)
item.connect('activate', self.search_popup_add_tags)
# Add separator
tags_sub.add(Gtk.SeparatorMenuItem())
# Add new tag item
new_tag = Gtk.MenuItem("New Tag")
new_tag.connect('activate', self.new_tag_and_add)
tags_sub.add(new_tag)
tags_item.show_all()
# Create wants Submenu
wants_item = self.app.ui.get_object("searchListPopupWants")
wants_sub = Gtk.Menu()
wants_item.set_submenu(wants_sub)
for list_name in self.app.wants.keys():
item = Gtk.MenuItem()
wants_sub.add(item)
item.set_label(list_name)
item.connect('activate', self.search_popup_add_wants)
# Add separator
wants_sub.add(Gtk.SeparatorMenuItem())
# Add new tag item
new_want = Gtk.MenuItem("New Want List")
new_want.connect('activate', self.new_wants_and_add)
wants_sub.add(new_want)
wants_item.show_all()
def new_tag_and_add(self, menu_item):
# Get selected cards
card_list = self.app.ui.get_object("searchResults").get_child()
cards = card_list.get_selected_cards()
response = self.app.show_name_enter_dialog("Enter name for new Tag", "")
if not response == "":
self.app.add_tag(response)
for card in cards.values():
self.app.add_card_to_lib(card, response)
else:
util.log("No tag name entered", util.LogLevel.Warning)
self.app.push_status("No name for new tag entered")
search_funct.reload_search_view(self.app)
def new_wants_and_add(self, menu_item):
# Get selected cards
card_list = self.app.ui.get_object("searchResults").get_child()
cards = card_list.get_selected_cards()
response = self.app.show_name_enter_dialog("Enter name for new Want List", "")
if not response == "":
self.app.add_want_list(response)
for card in cards.values():
self.app.add_card_to_want_list(response, card)
else:
util.log("No list name entered", util.LogLevel.Warning)
self.app.push_status("No name for new wants list entered")
search_funct.reload_search_view(self.app)
def search_popup_add_tags(self, item):
# Get selected cards
card_list = self.app.ui.get_object("searchResults").get_child()
cards = card_list.get_selected_cards()
for card in cards.values():
self.app.add_card_to_lib(card, item.get_label())
search_funct.reload_search_view(self.app)
self.app.push_status("Added " + str(len(cards)) + " card(s) to library.")
def search_popup_add_wants(self, item):
# Get selected cards
card_list = self.app.ui.get_object("searchResults").get_child()
cards = card_list.get_selected_cards()
for card in cards.values():
self.app.add_card_to_want_list(item.get_label(), card)
search_funct.reload_search_view(self.app)
self.app.push_status("Added " + str(len(cards)) + " card(s) to Want List '" + item.get_label() + "'")
def do_search_clear_all_clicked(self, button):
""" Rest all controls in search view """
self.app.ui.get_object("searchEntry").set_text("")
self.do_clear_mana_filter(self.app.ui.get_object("manaFilterGrid"))
self.app.ui.get_object("rarityCombo").set_active(0)
self.app.ui.get_object("typeCombo").set_active(0)
self.app.ui.get_object("setEntry").set_text("")
def do_show_card_details(self, menu_item):
tree = self.app.ui.get_object("searchResults").get_child()
cards = tree.get_selected_cards()
for card in cards.values():
self.app.show_card_details(card)
def do_search_add_to_lib(self, menu_item):
tree = self.app.ui.get_object("searchResults").get_child()
cards = tree.get_selected_cards()
for card in cards.values():
self.app.add_card_to_lib(card)
search_funct.reload_search_view(self.app)
# ---------------------------------Library----------------------------------------------
def do_reload_library(self, view):
lib_funct.reload_library(self.app)
def do_tag_entry_changed(self, entry):
input_valid = entry.get_text() and entry.get_text() != ""
self.app.ui.get_object("newTagButton").set_sensitive(input_valid)
def do_new_tag_clicked(self, entry):
lib_funct.add_new_tag(entry.get_text(), self.app)
entry.set_text("")
def do_show_all_clicked(self, button):
# Clear selection in tag list
self.app.ui.get_object("tagTree").get_selection().unselect_all()
self.app.current_lib_tag = "All"
lib_funct.reload_library(self.app)
def do_show_untagged_clicked(self, button):
# Clear selection in tag list
self.app.ui.get_object("tagTree").get_selection().unselect_all()
self.app.current_lib_tag = "Untagged"
lib_funct.reload_library(self.app, "Untagged")
def do_tag_cards(self, entry):
card_view = self.app.ui.get_object("libraryContainer").get_child()
selected_cards = card_view.get_selected_cards()
tag = entry.get_text()
if tag != "":
lib_funct.tag_cards(selected_cards, tag, self.app)
lib_funct.reload_library(self.app, tag)
entry.set_text("")
def on_tag_selected(self, selection, path, column):
(model, pathlist) = selection.get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
tag = model.get_value(tree_iter, 0)
self.app.current_lib_tag = tag
lib_funct.reload_library(self.app, tag)
def do_tag_tree_press_event(self, treeview, event):
if event.button == 3: # right click
path = treeview.get_path_at_pos(int(event.x), int(event.y))
if path:
tree_iter = treeview.get_model().get_iter(path[0])
tag = treeview.get_model().get_value(tree_iter, 0)
self.app.ui.get_object("tagListPopup").popup(None, None, None, None, 0, event.time)
def do_tag_list_rename(self, tree):
(model, pathlist) = tree.get_selection().get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
tag = model.get_value(tree_iter, 0)
new_name = self.app.show_name_enter_dialog("Rename Tag", tag)
self.app.rename_tag(tag, new_name)
self.app.current_page.emit('show')
def do_tag_list_delete(self, tree):
(model, pathlist) = tree.get_selection().get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
tag = model.get_value(tree_iter, 0)
self.app.remove_tag(tag)
self.app.current_page.emit('show')
def do_refilter_library(self, container):
# Access Card View inside of container
container.get_child().filter.refilter()
def lib_tree_popup_showed(self, menu):
# Get selected cards
card_list = self.app.ui.get_object("libraryContainer").get_child()
cards = card_list.get_selected_cards()
# Check if a tag is selected
current_tag = self.app.current_lib_tag
if current_tag == "All" or current_tag == "Untagged":
return
# Check if selected Cards are tagged
for id_list in self.app.tags.values():
for card_id in cards.keys():
if id_list.__contains__(card_id):
# Enable untag menu item
self.app.ui.get_object("untagItem").set_sensitive(True)
return
def do_popup_untag_cards(self, item):
# Get selected cards
card_list = self.app.ui.get_object("libraryContainer").get_child()
cards = card_list.get_selected_cards()
tag = self.app.current_lib_tag
for card in cards.values():
self.app.untag_card(card, tag)
lib_funct.reload_library(self.app, tag)
lib_funct.reload_tag_list(self.app, preserve=True)
def do_popup_remove_card(self, item):
# Get selected cards
card_list = self.app.ui.get_object("libraryContainer").get_child()
cards = card_list.get_selected_cards()
# Remove selected cards
for card in cards.values():
self.app.remove_card_from_lib(card)
lib_funct.reload_library(self.app, self.app.current_lib_tag)
lib_funct.reload_tag_list(self.app, preserve=True)
# ---------------------------------Wants----------------------------------------------
def do_reload_wants(self, view):
wants_funct.reload_wants_view(self.app)
def on_new_wants_list_clicked(self, entry):
name = entry.get_text()
entry.set_text("")
# Check if list name already exists
if self.app.wants.__contains__(name):
return
self.app.add_want_list(name)
wants_funct.reload_wants_view(self.app)
def on_want_list_selected(self, selection, path, column):
(model, pathlist) = selection.get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
list_name = model.get_value(tree_iter, 0)
wants_funct.reload_wants_view(self.app, list_name)
def do_wants_tree_press_event(self, treeview, event):
if event.button == 3: # right click
path = treeview.get_path_at_pos(int(event.x), int(event.y))
# Get the selection
selection = treeview.get_selection()
# Get the selected path(s)
rows = selection.get_selected_rows()
# If not clicked on selection, change selected rows
if path:
if path[0] not in rows[1]:
selection.unselect_all()
selection.select_path(path[0])
self.app.ui.get_object("wants_wantsListPopup").popup(None, None, None, None, 0, event.time)
return True
def do_rename_wants_list(self, tree):
(model, pathlist) = tree.get_selection().get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
tag = model.get_value(tree_iter, 0)
new_name = self.app.show_name_enter_dialog("Rename Want List", tag)
if not tag == new_name:
self.app.rename_want_list(tag, new_name)
self.app.current_page.emit('show')
def do_delete_wants_list(self, tree):
(model, pathlist) = tree.get_selection().get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
name = model.get_value(tree_iter, 0)
self.app.delete_wants_list(name)
self.app.current_page.emit('show')
def on_want_cards_add_activated(self, menu_item):
# Get selected cards
tree = self.app.ui.get_object("wantsListContainer").get_child()
selected = tree.get_selected_cards()
# Get selected list
list_tree = self.app.ui.get_object("wantsListsTree")
(model, pathlist) = list_tree.get_selection().get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
list_name = model.get_value(tree_iter, 0)
for card in selected.values():
self.app.add_card_to_lib(card)
self.app.remove_card_from_want_list(card, list_name)
wants_funct.reload_wants_view(self.app, list_name)
def on_want_cards_remove_activated(self, menu_item):
# Get selected cards
tree = self.app.ui.get_object("wantsListContainer").get_child()
selected = tree.get_selected_cards()
# Get selected list
list_tree = self.app.ui.get_object("wantsListsTree")
(model, pathlist) = list_tree.get_selection().get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
list_name = model.get_value(tree_iter, 0)
for card in selected.values():
self.app.remove_card_from_want_list(card, list_name)
wants_funct.reload_wants_view(self.app, list_name)
# Handlers for TreeViews etc. which have been not added by Glade
# ---------------------------------Search Tree----------------------------------------------
def on_search_card_selected(self, tree, row_no, column):
(model, path_list) = tree.get_selection().get_selected_rows()
for path in path_list:
tree_iter = model.get_iter(path)
card_id = model.get_value(tree_iter, 0)
card_list = self.app.ui.get_object("searchResults").get_child()
card = card_list.lib[card_id]
self.app.show_card_details(card)
def on_search_selection_changed(self, selection):
(model, pathlist) = selection.get_selected_rows()
tools = self.app.ui.get_object("selectionToolsBox")
add_remove_button = self.app.ui.get_object("addRemoveButton")
if pathlist:
add_remove_button.set_sensitive(True)
else:
add_remove_button.set_sensitive(False)
def on_search_tree_press_event(self, treeview, event):
if event.button == 3: # right click
path = treeview.get_path_at_pos(int(event.x), int(event.y))
# Get the selection
selection = treeview.get_selection()
# Get the selected path(s)
rows = selection.get_selected_rows()
# If not clicked on selection, change selected rows
if path:
if path[0] not in rows[1]:
selection.unselect_all()
selection.select_path(path[0])
self.app.ui.get_object("searchListPopup").emit('show')
self.app.ui.get_object("searchListPopup").popup(None, None, None, None, 0, event.time)
return True
# ---------------------------------Library Tree----------------------------------------------
def on_library_card_selected(self, tree, row_no, column):
(model, path_list) = tree.get_selection().get_selected_rows()
for path in path_list:
tree_iter = model.get_iter(path)
card_id = model.get_value(tree_iter, 0)
card_list = self.app.ui.get_object("libraryContainer").get_child()
card = card_list.lib[card_id]
self.app.show_card_details(card)
def on_library_tree_press_event(self, treeview, event):
if event.button == 3: # right click
path = treeview.get_path_at_pos(int(event.x), int(event.y))
# Get the selection
selection = treeview.get_selection()
# Get the selected path(s)
rows = selection.get_selected_rows()
# If not clicked on selection, change selected rows
if path[0] not in rows[1]:
selection.unselect_all()
selection.select_path(path[0])
self.app.ui.get_object("libListPopup").emit('show')
self.app.ui.get_object("libListPopup").popup(None, None, None, None, 0, event.time)
return True
# ---------------------------------Wants Tree----------------------------------------------
def on_wants_card_selected(self, tree, row, column):
(model, path_list) = tree.get_selection().get_selected_rows()
for path in path_list:
tree_iter = model.get_iter(path)
card_id = model.get_value(tree_iter, 0)
card_list = self.app.ui.get_object("wantsListContainer").get_child()
card = card_list.lib[card_id]
self.app.show_card_details(card)
def on_wants_cards_press_event(self, treeview, event):
if event.button == 3: # right click
path = treeview.get_path_at_pos(int(event.x), int(event.y))
# Get the selection
selection = treeview.get_selection()
# Get the selected path(s)
rows = selection.get_selected_rows()
# If not clicked on selection, change selected rows
if path[0] not in rows[1]:
selection.unselect_all()
selection.select_path(path[0])
# Show popup and emit 'show' to trigger update function of popup
self.app.ui.get_object("wants_cardListPopup").emit('show')
self.app.ui.get_object("wants_cardListPopup").popup(None, None, None, None, 0, event.time)
return True

View File

@@ -1,70 +0,0 @@
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from cardvault import util
from cardvault import cardlist
def init_library_view(app):
# Create Tree View for library
container = app.ui.get_object("libraryContainer")
card_list = cardlist.CardList(True, app, util.GENERIC_TREE_COLORS)
card_list.set_name("libScroller")
# Show details
card_list.list.connect("row-activated", app.handlers.on_library_card_selected)
# Show Context menu
card_list.list.connect("button-press-event", app.handlers.on_library_tree_press_event)
card_list.filter.set_visible_func(app.filter_lib_func)
container.add(card_list)
container.add_overlay(app.ui.get_object("noResults"))
container.show_all()
app.ui.get_object("noResults").set_visible(False)
def reload_library(app, tag=None):
if tag == "Untagged" or tag == "All":
lib = app.get_untagged_cards()
tag = None
else:
lib = app.get_tagged_cards(tag)
reload_tag_list(app, tag)
tag_combo = app.ui.get_object("tagCardCombo")
tag_combo.set_model(app.ui.get_object("tagStore"))
card_tree = app.ui.get_object("libraryContainer").get_child()
if lib:
app.ui.get_object("noResults").set_visible(False)
card_tree.update(lib)
else:
card_tree.store.clear()
app.ui.get_object("noResults").set_visible(True)
def add_new_tag(name, app):
app.add_tag(name)
reload_tag_list(app, True)
def reload_tag_list(app, preserve=False):
tree = app.ui.get_object("tagTree")
(path, column) = tree.get_cursor()
store = tree.get_model()
store.clear()
for tag, ids in app.tags.items():
store.append([tag, tag + " (" + str(len(ids)) + ")"])
if preserve:
tree.set_cursor(path if path else 0)
store.set_sort_column_id(1, Gtk.SortType.ASCENDING)
def tag_cards(card_list, tag, app):
# Check if tag exist and create if necessary
if not app.tags.__contains__(tag):
app.add_tag(tag)
for card in card_list.values():
if not app.tags[tag].__contains__(card.multiverse_id):
app.tag_card(card, tag)

211
cardvault/library.py Normal file
View File

@@ -0,0 +1,211 @@
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from cardvault import application
from cardvault import util
from cardvault import cardlist
class LibraryHandlers:
def __init__(self, app: 'application.Application'):
self.app = app
self.init_library_view()
def do_reload_library(self, view):
self.reload_library()
def do_tag_entry_changed(self, entry):
input_valid = entry.get_text() and entry.get_text() != ""
self.app.ui.get_object("newTagButton").set_sensitive(input_valid)
def do_new_tag_clicked(self, entry):
self.add_new_tag(entry.get_text())
entry.set_text("")
def do_show_all_clicked(self, button):
# Clear selection in tag list
self.app.ui.get_object("tagTree").get_selection().unselect_all()
self.app.current_lib_tag = "All"
self.reload_library("All")
def do_show_untagged_clicked(self, button):
# Clear selection in tag list
self.app.ui.get_object("tagTree").get_selection().unselect_all()
self.app.current_lib_tag = "Untagged"
self.reload_library("Untagged")
def do_tag_cards(self, entry):
card_view = self.app.ui.get_object("libraryContainer").get_child()
selected_cards = card_view.get_selected_cards()
tag = entry.get_text()
if tag != "":
self.tag_cards(selected_cards, tag)
self.reload_library(tag)
entry.set_text("")
def on_tag_selected(self, selection, path, column):
(model, pathlist) = selection.get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
tag = model.get_value(tree_iter, 0)
self.app.current_lib_tag = tag
self.reload_library(tag)
def do_tag_tree_press_event(self, treeview, event):
if event.button == 3: # right click
path = treeview.get_path_at_pos(int(event.x), int(event.y))
if path:
tree_iter = treeview.get_model().get_iter(path[0])
tag = treeview.get_model().get_value(tree_iter, 0)
self.app.ui.get_object("tagListPopup").popup(None, None, None, None, 0, event.time)
def do_tag_list_rename(self, tree):
(model, pathlist) = tree.get_selection().get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
tag = model.get_value(tree_iter, 0)
new_name = self.app.show_name_enter_dialog("Rename Tag", tag)
self.app.rename_tag(tag, new_name)
self.app.current_page.emit('show')
def do_tag_list_delete(self, tree):
(model, pathlist) = tree.get_selection().get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
tag = model.get_value(tree_iter, 0)
self.app.remove_tag(tag)
self.app.current_page.emit('show')
def do_refilter_library(self, container):
# Access Card View inside of container
container.get_child().filter.refilter()
def lib_tree_popup_showed(self, menu):
# Get selected cards
card_list = self.app.ui.get_object("libraryContainer").get_child()
cards = card_list.get_selected_cards()
# Check if a tag is selected
current_tag = self.app.current_lib_tag
if current_tag == "All" or current_tag == "Untagged":
return
# Check if selected Cards are tagged
for id_list in self.app.tags.values():
for card_id in cards.keys():
if id_list.__contains__(card_id):
# Enable untag menu item
self.app.ui.get_object("untagItem").set_sensitive(True)
return
def do_popup_untag_cards(self, item):
# Get selected cards
card_list = self.app.ui.get_object("libraryContainer").get_child()
cards = card_list.get_selected_cards()
tag = self.app.current_lib_tag
for card in cards.values():
self.app.untag_card(card, tag)
self.reload_library(tag)
self.reload_tag_list(preserve=True)
def do_popup_remove_card(self, item):
# Get selected cards
card_list = self.app.ui.get_object("libraryContainer").get_child()
cards = card_list.get_selected_cards()
# Remove selected cards
for card in cards.values():
self.app.remove_card_from_lib(card)
self.reload_library(self.app.current_lib_tag)
self.reload_tag_list(preserve=True)
# ---------------------------------Library Tree----------------------------------------------
def on_library_card_selected(self, tree, row_no, column):
(model, path_list) = tree.get_selection().get_selected_rows()
for path in path_list:
tree_iter = model.get_iter(path)
card_id = model.get_value(tree_iter, 0)
card_list = self.app.ui.get_object("libraryContainer").get_child()
card = card_list.lib[card_id]
self.app.show_card_details(card)
def on_library_tree_press_event(self, treeview, event):
if event.button == 3: # right click
path = treeview.get_path_at_pos(int(event.x), int(event.y))
# Get the selection
selection = treeview.get_selection()
# Get the selected path(s)
rows = selection.get_selected_rows()
# If not clicked on selection, change selected rows
if path[0] not in rows[1]:
selection.unselect_all()
selection.select_path(path[0])
self.app.ui.get_object("libListPopup").emit('show')
self.app.ui.get_object("libListPopup").popup(None, None, None, None, 0, event.time)
return True
# -------------------------- Class Functions -------------------------------
def init_library_view(self):
"""Initialize the library view"""
# Create Tree View for library
container = self.app.ui.get_object("libraryContainer")
card_list = cardlist.CardList(True, self.app, util.GENERIC_TREE_COLORS)
card_list.set_name("libScroller")
# Show details
card_list.list.connect("row-activated", self.on_library_card_selected)
# Show Context menu
card_list.list.connect("button-press-event", self.on_library_tree_press_event)
card_list.filter.set_visible_func(self.app.filter_lib_func)
container.add(card_list)
container.add_overlay(self.app.ui.get_object("noResults"))
container.show_all()
self.app.ui.get_object("noResults").set_visible(False)
def reload_library(self, tag="All"):
if tag == "Untagged":
lib = self.app.get_untagged_cards()
elif tag == "All":
lib = self.app.library
else:
lib = self.app.get_tagged_cards(tag)
self.reload_tag_list(True)
tag_combo = self.app.ui.get_object("tagCardCombo")
tag_combo.set_model(self.app.ui.get_object("tagStore"))
card_tree = self.app.ui.get_object("libraryContainer").get_child()
if lib:
self.app.ui.get_object("noResults").set_visible(False)
card_tree.update(lib)
else:
card_tree.store.clear()
self.app.ui.get_object("noResults").set_visible(True)
def add_new_tag(self, name):
self.app.add_tag(name)
self.reload_tag_list(True)
def reload_tag_list(self, preserve=False):
"""Reload left pane tag list"""
tree = self.app.ui.get_object("tagTree")
(path, column) = tree.get_cursor()
store = tree.get_model()
store.clear()
for tag, ids in self.app.tags.items():
store.append([tag, tag + " (" + str(len(ids)) + ")"])
if preserve:
tree.set_cursor(path if path else 0)
store.set_sort_column_id(1, Gtk.SortType.ASCENDING)
def tag_cards(self, card_list, tag):
# Check if tag exist and create if necessary
if not self.app.tags.__contains__(tag):
self.app.add_tag(tag)
for card in card_list.values():
if not self.app.tags[tag].__contains__(card.multiverse_id):
self.app.tag_card(card, tag)

343
cardvault/search.py Normal file
View File

@@ -0,0 +1,343 @@
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from urllib.error import URLError, HTTPError
import time
from mtgsdk import Card
from cardvault import application
from cardvault import cardlist
from cardvault import util
class SearchHandlers:
def __init__(self, app: 'application.Application'):
self.app = app
self.init_search_view()
def do_search_cards(self, sender):
search_term = self.app.ui.get_object("searchEntry").get_text()
filters = self.get_filters()
results = self.search_cards(search_term, filters)
card_list = self.app.ui.get_object("searchResults").get_child()
card_list.update(results)
self.app.ui.get_object("searchOverlay").set_visible(False)
@staticmethod
def do_clear_mana_filter(mana_filter_grid):
for toggle_button in mana_filter_grid.get_children():
if isinstance(toggle_button, Gtk.ToggleButton):
toggle_button.set_active(False)
@staticmethod
def do_clear_set_filter(entry, icon_pos, button):
entry.set_text("")
def do_add_clicked(self, button):
card_view = self.app.ui.get_object("searchResults").get_child()
(model, pathlist) = card_view.selection.get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
card_id = model.get_value(tree_iter, 0)
card = card_view.lib[card_id]
self.app.add_card_to_lib(card)
self.reload_search_view()
self.app.ui.get_object("searchEntry").grab_focus()
def search_tree_popup_showed(self, menu):
# Create tag submenu
tags_item = self.app.ui.get_object("searchListPopupAddTag")
tags_sub = Gtk.Menu()
tags_item.set_submenu(tags_sub)
for list_name in self.app.tags.keys():
item = Gtk.MenuItem()
tags_sub.add(item)
item.set_label(list_name)
item.connect('activate', self.search_popup_add_tags)
# Add separator
tags_sub.add(Gtk.SeparatorMenuItem())
# Add new tag item
new_tag = Gtk.MenuItem("New Tag")
new_tag.connect('activate', self.new_tag_and_add)
tags_sub.add(new_tag)
tags_item.show_all()
# Create wants Submenu
wants_item = self.app.ui.get_object("searchListPopupWants")
wants_sub = Gtk.Menu()
wants_item.set_submenu(wants_sub)
for list_name in self.app.wants.keys():
item = Gtk.MenuItem()
wants_sub.add(item)
item.set_label(list_name)
item.connect('activate', self.search_popup_add_wants)
# Add separator
wants_sub.add(Gtk.SeparatorMenuItem())
# Add new tag item
new_want = Gtk.MenuItem("New Want List")
new_want.connect('activate', self.new_wants_and_add)
wants_sub.add(new_want)
wants_item.show_all()
def new_tag_and_add(self, menu_item):
# Get selected cards
card_list = self.app.ui.get_object("searchResults").get_child()
cards = card_list.get_selected_cards()
response = self.app.show_name_enter_dialog("Enter name for new Tag", "")
if not response == "":
self.app.add_tag(response)
for card in cards.values():
self.app.add_card_to_lib(card, response)
else:
util.log("No tag name entered", util.LogLevel.Warning)
self.app.push_status("No name for new tag entered")
self.reload_search_view()
def new_wants_and_add(self, menu_item):
# Get selected cards
card_list = self.app.ui.get_object("searchResults").get_child()
cards = card_list.get_selected_cards()
response = self.app.show_name_enter_dialog("Enter name for new Want List", "")
if not response == "":
self.app.add_want_list(response)
for card in cards.values():
self.app.add_card_to_want_list(response, card)
else:
util.log("No list name entered", util.LogLevel.Warning)
self.app.push_status("No name for new wants list entered")
self.reload_search_view()
def search_popup_add_tags(self, item):
# Get selected cards
card_list = self.app.ui.get_object("searchResults").get_child()
cards = card_list.get_selected_cards()
for card in cards.values():
self.app.add_card_to_lib(card, item.get_label())
self.reload_search_view()
self.app.push_status("Added " + str(len(cards)) + " card(s) to library.")
def search_popup_add_wants(self, item):
# Get selected cards
card_list = self.app.ui.get_object("searchResults").get_child()
cards = card_list.get_selected_cards()
for card in cards.values():
self.app.add_card_to_want_list(item.get_label(), card)
self.reload_search_view()
self.app.push_status("Added " + str(len(cards)) + " card(s) to Want List '" + item.get_label() + "'")
def do_search_clear_all_clicked(self, button):
""" Rest all controls in search view """
self.app.ui.get_object("searchEntry").set_text("")
self.do_clear_mana_filter(self.app.ui.get_object("manaFilterGrid"))
self.app.ui.get_object("rarityCombo").set_active(0)
self.app.ui.get_object("typeCombo").set_active(0)
self.app.ui.get_object("setEntry").set_text("")
def do_show_card_details(self, menu_item):
tree = self.app.ui.get_object("searchResults").get_child()
cards = tree.get_selected_cards()
for card in cards.values():
self.app.show_card_details(card)
def do_search_add_to_lib(self, menu_item):
tree = self.app.ui.get_object("searchResults").get_child()
cards = tree.get_selected_cards()
for card in cards.values():
self.app.add_card_to_lib(card)
self.reload_search_view()
def init_search_view(self):
# set mana icons on filter buttons
buttons = [x for x in self.app.ui.get_object("manaFilterGrid").get_children()
if isinstance(x, Gtk.ToggleButton)]
self._init_mana_buttons(buttons)
# set auto completion for filter entry
self._init_set_entry(self.app.ui.get_object("setEntry"))
# Fill rarity box
self._init_combo_box(self.app.ui.get_object("rarityCombo"), util.rarity_dict.keys())
# Fill type box
self._init_combo_box(self.app.ui.get_object("typeCombo"), util.card_types)
# Create Model for search results
self._init_results_tree()
def reload_search_view(self):
""" Reload the card tree """
results_tree = self.app.ui.get_object("searchResults").get_child()
cards = results_tree.lib
results_tree.update(cards)
def get_filters(self) -> dict:
""" Read selected filters from UI and return values as dict """
output = {}
# Mana colors
color_list = []
# Go through mana color buttons an get the active filters
for button in self.app.ui.get_object("manaFilterGrid").get_children():
if isinstance(button, Gtk.ToggleButton):
if button.get_active():
color_list.append(button.get_name())
output["mana"] = ",".join(color_list)
# Rarity
combo = self.app.ui.get_object("rarityCombo")
output["rarity"] = self._get_combo_value(combo)
# Type
combo = self.app.ui.get_object("typeCombo")
output["type"] = self._get_combo_value(combo)
# Set
name = self.app.ui.get_object("setEntry").get_text()
output["set"] = ""
for mtgset in self.app.sets.values():
if mtgset.name == name:
output["set"] = mtgset.code
return output
def search_cards(self, term: str, filters: dict) -> dict:
util.log("Starting online search for '" + term + "'", util.LogLevel.Info)
util.log("Used Filters: " + str(filters), util.LogLevel.Info)
# Load card info from internet
try:
util.log("Fetching card info ...", util.LogLevel.Info)
start = time.time()
cards = Card.where(name=term) \
.where(colorIdentity=filters["mana"]) \
.where(types=filters["type"]) \
.where(set=filters["set"]) \
.where(rarity=filters["rarity"]) \
.where(pageSize=50) \
.where(page=1).all()
end = time.time()
util.log("Card info fetched in {}s".format(round(end - start, 3)), util.LogLevel.Info)
except (URLError, HTTPError) as err:
util.log(err, util.LogLevel.Error)
return {}
# Check if results were found
if len(cards) == 0:
# TODO UI show no cards found
util.log("No Cards found", util.LogLevel.Info)
return {}
util.log("Found " + str(len(cards)) + " cards", util.LogLevel.Info)
# Remove duplicate entries
if util.SHOW_FROM_ALL_SETS is False:
cards = self._remove_duplicates(cards)
# Pack results in a dictionary
lib = {}
for card in cards:
lib[card.multiverse_id] = card
return lib
# ---------------------------------Search Tree----------------------------------------------
def on_search_card_selected(self, tree, row_no, column):
(model, path_list) = tree.get_selection().get_selected_rows()
for path in path_list:
tree_iter = model.get_iter(path)
card_id = model.get_value(tree_iter, 0)
card_list = self.app.ui.get_object("searchResults").get_child()
card = card_list.lib[card_id]
self.app.show_card_details(card)
def on_search_selection_changed(self, selection):
(model, pathlist) = selection.get_selected_rows()
tools = self.app.ui.get_object("selectionToolsBox")
add_remove_button = self.app.ui.get_object("addRemoveButton")
if pathlist:
add_remove_button.set_sensitive(True)
else:
add_remove_button.set_sensitive(False)
def on_search_tree_press_event(self, treeview, event):
if event.button == 3: # right click
path = treeview.get_path_at_pos(int(event.x), int(event.y))
# Get the selection
selection = treeview.get_selection()
# Get the selected path(s)
rows = selection.get_selected_rows()
# If not clicked on selection, change selected rows
if path:
if path[0] not in rows[1]:
selection.unselect_all()
selection.select_path(path[0])
self.app.ui.get_object("searchListPopup").emit('show')
self.app.ui.get_object("searchListPopup").popup(None, None, None, None, 0, event.time)
return True
# -------------------------- Class Functions -------------------------------
def _init_results_tree(self):
overlay = self.app.ui.get_object("searchResults")
card_list = cardlist.CardList(False, self.app, util.SEARCH_TREE_COLORS)
card_list.set_name("resultsScroller")
card_list.list.connect("row-activated", self.on_search_card_selected)
card_list.selection.connect("changed", self.on_search_selection_changed)
overlay.add(card_list)
overlay.add_overlay(self.app.ui.get_object("searchOverlay"))
overlay.show_all()
# Connect signal for context menu
card_list.list.connect("button-press-event", self.on_search_tree_press_event)
@staticmethod
def _init_combo_box(combo, card_list: list):
""" Initialize a combo box model """
model = Gtk.ListStore(str)
model.append(["All"])
for entry in card_list:
model.append([entry.title()])
combo.set_model(model)
cell = Gtk.CellRendererText()
combo.pack_start(cell, True)
combo.add_attribute(cell, "text", 0)
combo.set_active(0)
@staticmethod
def _remove_duplicates(cards: list) -> list:
""" Remove cards with the same name from a list """
unique_cards = []
unique_names = []
# Reverse cardlist so we get the version with the most modern art
for card in reversed(cards):
if card.name not in unique_names:
unique_names.append(card.name)
unique_cards.append(card)
return unique_cards
@staticmethod
def _get_combo_value(combo) -> str:
""" Get value from a combo box control """
tree_iter = combo.get_active_iter()
value = combo.get_model().get_value(tree_iter, 0)
return value.replace("All", "")
def _init_mana_buttons(self, button_list):
""" Initialize mana buttons """
for button in button_list:
image = Gtk.Image.new_from_pixbuf(self.app.get_mana_icons("{" + button.get_name() + "}"))
button.set_image(image)
def _init_set_entry(self, entry):
""" Initialize model for set entry """
set_store = Gtk.ListStore(str, str)
for mtgset in self.app.sets.values():
set_store.append([mtgset.name, mtgset.code])
completer = Gtk.EntryCompletion()
completer.set_model(set_store)
completer.set_text_column(0)
entry.set_completion(completer)

View File

@@ -1,156 +0,0 @@
from urllib.error import URLError, HTTPError
import gi
import time
from gi.repository import Gtk, Gdk
from cardvault import cardlist
from cardvault import util
from cardvault import application
from mtgsdk import Card
gi.require_version('Gtk', '3.0')
def init_search_view(app: 'application.Application'):
# set mana icons on filter buttons
buttons = [x for x in app.ui.get_object("manaFilterGrid").get_children()
if isinstance(x, Gtk.ToggleButton)]
_init_mana_buttons(app, buttons)
# set auto completion for filter entry
_init_set_entry(app, app.ui.get_object("setEntry"))
# Fill rarity box
_init_combo_box(app.ui.get_object("rarityCombo"), util.rarity_dict.keys())
# Fill type box
_init_combo_box(app.ui.get_object("typeCombo"), util.card_types)
# Create Model for search results
_init_results_tree(app)
def reload_search_view(app: 'application.Application'):
results_tree = app.ui.get_object("searchResults").get_child()
cards = results_tree.lib
results_tree.update(cards)
def get_filters(app: 'application.Application') -> dict:
output = {}
# Mana colors
color_list = []
# Go through mana color buttons an get the active filters
for button in app.ui.get_object("manaFilterGrid").get_children():
if isinstance(button, Gtk.ToggleButton):
if button.get_active():
color_list.append(button.get_name())
output["mana"] = ",".join(color_list)
# Rarity
combo = app.ui.get_object("rarityCombo")
output["rarity"] = _get_combo_value(combo)
# Type
combo = app.ui.get_object("typeCombo")
output["type"] = _get_combo_value(combo)
# Set
name = app.ui.get_object("setEntry").get_text()
output["set"] = ""
for set in app.sets.values():
if set.name == name:
output["set"] = set.code
return output
def search_cards(term: str, filters: dict) -> dict:
util.log("Starting online search for '" + term + "'", util.LogLevel.Info)
util.log("Used Filters: " + str(filters), util.LogLevel.Info)
# Load card info from internet
try:
util.log("Fetching card info ...", util.LogLevel.Info)
start = time.time()
cards = Card.where(name=term) \
.where(colorIdentity=filters["mana"]) \
.where(types=filters["type"]) \
.where(set=filters["set"]) \
.where(rarity=filters["rarity"]) \
.where(pageSize=50) \
.where(page=1).all()
end = time.time()
util.log("Card info fetched in {}s".format(round(end - start, 3)), util.LogLevel.Info)
except (URLError, HTTPError) as err:
util.log(err, util.LogLevel.Error)
return
# Check if results were found
if len(cards) == 0:
# TODO UI show no cards found
util.log("No Cards found", util.LogLevel.Info)
return
util.log("Found " + str(len(cards)) + " cards", util.LogLevel.Info)
# Remove duplicate entries
if util.SHOW_FROM_ALL_SETS is False:
cards = _remove_duplicates(cards)
# Pack results in a dictionary
lib = {}
for card in cards:
lib[card.multiverse_id] = card
return lib
def _init_results_tree(app: 'application.Application'):
overlay = app.ui.get_object("searchResults")
card_list = cardlist.CardList(False, app, util.SEARCH_TREE_COLORS)
card_list.set_name("resultsScroller")
card_list.list.connect("row-activated", app.handlers.on_search_card_selected)
card_list.selection.connect("changed", app.handlers.on_search_selection_changed)
overlay.add(card_list)
overlay.add_overlay(app.ui.get_object("searchOverlay"))
overlay.show_all()
# Connect signal for context menu
card_list.list.connect("button-press-event", app.handlers.on_search_tree_press_event)
def _init_combo_box(combo, list: list):
model = Gtk.ListStore(str)
model.append(["All"])
for entry in list:
model.append([entry.title()])
combo.set_model(model)
cell = Gtk.CellRendererText()
combo.pack_start(cell, True)
combo.add_attribute(cell, "text", 0)
combo.set_active(0)
def _remove_duplicates(cards: list) -> list:
unique_cards = []
unique_names = []
# Reverse cardlist so we get the version with the most modern art
for card in reversed(cards):
if card.name not in unique_names:
unique_names.append(card.name)
unique_cards.append(card)
return unique_cards
def _get_combo_value(combo) -> str:
tree_iter = combo.get_active_iter()
value = combo.get_model().get_value(tree_iter, 0)
return value.replace("All", "")
def _init_mana_buttons(app, button_list):
for button in button_list:
image = Gtk.Image.new_from_pixbuf(app.get_mana_icons("{" + button.get_name() + "}"))
button.set_image(image)
def _init_set_entry(app: 'application.Application', entry):
set_store = Gtk.ListStore(str, str)
for set in app.sets.values():
set_store.append([set.name, set.code])
completer = Gtk.EntryCompletion()
completer.set_model(set_store)
completer.set_text_column(0)
entry.set_completion(completer)

176
cardvault/wants.py Normal file
View File

@@ -0,0 +1,176 @@
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from cardvault import cardlist
from cardvault import application
from cardvault import util
class WantsHandlers:
def __init__(self, app: 'application.Application'):
self.app = app
self.init_wants_view()
def do_reload_wants(self, view):
self.reload_wants_view()
def on_new_wants_list_clicked(self, entry):
name = entry.get_text()
entry.set_text("")
# Check if list name already exists
if self.app.wants.__contains__(name):
return
self.app.add_want_list(name)
self.reload_wants_view()
def on_want_list_selected(self, selection, path, column):
(model, pathlist) = selection.get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
list_name = model.get_value(tree_iter, 0)
self.reload_wants_view(list_name)
def do_wants_tree_press_event(self, treeview, event):
if event.button == 3: # right click
path = treeview.get_path_at_pos(int(event.x), int(event.y))
# Get the selection
selection = treeview.get_selection()
# Get the selected path(s)
rows = selection.get_selected_rows()
# If not clicked on selection, change selected rows
if path:
if path[0] not in rows[1]:
selection.unselect_all()
selection.select_path(path[0])
self.app.ui.get_object("wants_wantsListPopup").popup(None, None, None, None, 0, event.time)
return True
def do_rename_wants_list(self, tree):
(model, pathlist) = tree.get_selection().get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
tag = model.get_value(tree_iter, 0)
new_name = self.app.show_name_enter_dialog("Rename Want List", tag)
if not tag == new_name:
self.app.rename_want_list(tag, new_name)
self.app.current_page.emit('show')
def do_delete_wants_list(self, tree):
(model, pathlist) = tree.get_selection().get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
name = model.get_value(tree_iter, 0)
self.app.delete_wants_list(name)
self.app.current_page.emit('show')
def on_want_cards_add_activated(self, menu_item):
# Get selected cards
tree = self.app.ui.get_object("wantsListContainer").get_child()
selected = tree.get_selected_cards()
# Get selected list
list_tree = self.app.ui.get_object("wantsListsTree")
(model, pathlist) = list_tree.get_selection().get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
list_name = model.get_value(tree_iter, 0)
for card in selected.values():
self.app.add_card_to_lib(card)
self.app.remove_card_from_want_list(card, list_name)
self.reload_wants_view(list_name)
def on_want_cards_remove_activated(self, menu_item):
# Get selected cards
tree = self.app.ui.get_object("wantsListContainer").get_child()
selected = tree.get_selected_cards()
# Get selected list
list_tree = self.app.ui.get_object("wantsListsTree")
(model, pathlist) = list_tree.get_selection().get_selected_rows()
for path in pathlist:
tree_iter = model.get_iter(path)
list_name = model.get_value(tree_iter, 0)
for card in selected.values():
self.app.remove_card_from_want_list(card, list_name)
self.reload_wants_view(list_name)
# ---------------------------------Wants Tree----------------------------------------------
def on_wants_card_selected(self, tree, row, column):
(model, path_list) = tree.get_selection().get_selected_rows()
for path in path_list:
tree_iter = model.get_iter(path)
card_id = model.get_value(tree_iter, 0)
card_list = self.app.ui.get_object("wantsListContainer").get_child()
card = card_list.lib[card_id]
self.app.show_card_details(card)
def on_wants_cards_press_event(self, treeview, event):
if event.button == 3: # right click
path = treeview.get_path_at_pos(int(event.x), int(event.y))
# Get the selection
selection = treeview.get_selection()
# Get the selected path(s)
rows = selection.get_selected_rows()
# If not clicked on selection, change selected rows
if path[0] not in rows[1]:
selection.unselect_all()
selection.select_path(path[0])
# Show popup and emit 'show' to trigger update function of popup
self.app.ui.get_object("wants_cardListPopup").emit('show')
self.app.ui.get_object("wants_cardListPopup").popup(None, None, None, None, 0, event.time)
return True
# -------------------------- Class Functions -------------------------------
def init_wants_view(self):
# Get container for Cardlist Tree
container = self.app.ui.get_object("wantsListContainer")
# Create new Cardlist
card_list = cardlist.CardList(True, self.app, util.GENERIC_TREE_COLORS)
card_list.set_name("wantsScroller")
# Show details
card_list.list.connect("row-activated", self.on_wants_card_selected)
card_list.list.connect("button-press-event", self.on_wants_cards_press_event)
# Add card list to container
container.add(card_list)
container.add_overlay(self.app.ui.get_object("wantsOverlay"))
container.show_all()
# Hide no results overlay
self.app.ui.get_object("wantsOverlay").set_visible(False)
def reload_wants_view(self, selected_list: str = None):
tree = self.app.ui.get_object("wantsListContainer").get_child() # type: cardlist.CardList
cards = self.app.get_wanted_cards(selected_list)
self.reload_wants_list(True)
if cards:
self.app.ui.get_object("wantsOverlay").set_visible(False)
tree.update(cards)
else:
tree.store.clear()
self.app.ui.get_object("wantsOverlay").set_visible(True)
# Set Title
label = self.app.ui.get_object("wantsTileLabel") # type: Gtk.Label
label.set_markup("<big>" + str(selected_list) + "</big>")
def reload_wants_list(self, preserve=False):
tree = self.app.ui.get_object("wantsListsTree")
(path, column) = tree.get_cursor()
store = tree.get_model()
store.clear()
for list_name, cards in self.app.wants.items():
store.append([list_name, list_name + " (" + str(len(cards)) + ")"])
if preserve:
tree.set_cursor(path if path else 0)
store.set_sort_column_id(1, Gtk.SortType.ASCENDING)

View File

@@ -1,53 +0,0 @@
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from cardvault import cardlist
from cardvault import application
from cardvault import util
def init_wants_view(app: 'application.Application'):
# Get container for Cardlist Tree
container = app.ui.get_object("wantsListContainer")
# Create new Cardlist
card_list = cardlist.CardList(True, app, util.GENERIC_TREE_COLORS)
card_list.set_name("wantsScroller")
# Show details
card_list.list.connect("row-activated", app.handlers.on_wants_card_selected)
card_list.list.connect("button-press-event", app.handlers.on_wants_cards_press_event)
# Add card list to container
container.add(card_list)
container.add_overlay(app.ui.get_object("wantsOverlay"))
container.show_all()
# Hide no results overlay
app.ui.get_object("wantsOverlay").set_visible(False)
def reload_wants_view(app: 'application.Application', selected_list: str = None):
tree = app.ui.get_object("wantsListContainer").get_child() # type: cardlist.CardList
cards = app.get_wanted_cards(selected_list)
reload_wants_list(app, True)
if cards:
app.ui.get_object("wantsOverlay").set_visible(False)
tree.update(cards)
else:
tree.store.clear()
app.ui.get_object("wantsOverlay").set_visible(True)
# Set Title
label = app.ui.get_object("wantsTileLabel") # type: Gtk.Label
label.set_markup("<big>" + str(selected_list) + "</big>")
def reload_wants_list(app: 'application.Application', preserve=False):
tree = app.ui.get_object("wantsListsTree")
(path, column) = tree.get_cursor()
store = tree.get_model()
store.clear()
for list_name, cards in app.wants.items():
store.append([list_name, list_name + " (" + str(len(cards)) + ")"])
if preserve:
tree.set_cursor(path if path else 0)
store.set_sort_column_id(1, Gtk.SortType.ASCENDING)