Add cards to want lists from search view.

This commit is contained in:
luxick
2017-05-23 22:06:22 +02:00
parent fdcaac6e21
commit 69a90c2050
8 changed files with 140 additions and 61 deletions

View File

@@ -13,6 +13,8 @@ except ImportError as ex:
import os
import copy
import re
import mtgsdk
from typing import Type, Dict, List
from cardvault import handlers
from cardvault import util
@@ -21,6 +23,8 @@ from cardvault import lib_funct
from cardvault import wants_funct
class Application:
# ---------------------------------Initialize the Application----------------------------------------------
def __init__(self):
@@ -57,15 +61,15 @@ class Application:
self.sets = util.load_sets(util.get_root_filename("sets"))
self.library = None
self.tags = None
self.wants = None
self.library = Dict[str, Type[mtgsdk.Card]]
self.tags = Dict[str, str]
self.wants = Dict[str, List[Type[mtgsdk.Card]]]
self.load_library()
self.handlers = handlers.Handlers(self)
self.ui.connect_signals(self.handlers)
# Inizialize the views
# Initialize the views
search_funct.init_search_view(self)
@@ -207,6 +211,8 @@ class Application:
util.save_file(util.get_root_filename("library"), self.library)
# Save tags file
util.save_file(util.get_root_filename("tags"), self.tags)
# Save wants file
util.save_file(util.get_root_filename("wants"), self.wants)
self.unsaved_changes = False
self.push_status("Library saved")
@@ -283,12 +289,24 @@ class Application:
util.log("Tag '" + old + "' renamed to '" + new + "'", util.LogLevel.Info)
self.unsaved_changes = True
def get_wanted_card_ids(self) -> List[str]:
all_ids = []
for cards in self.wants.values():
next_ids = [card.multiverse_id for card in cards]
all_ids = list(set(all_ids) | set(next_ids))
return all_ids
def add_want_list(self, name):
self.wants[name] = {}
self.wants[name] = []
util.log("Want list '" + name + "' created", util.LogLevel.Info)
self.push_status("Created want list '" + name + "'")
self.unsaved_changes = True
def add_card_to_want_list(self, list_name, card):
self.wants[list_name].append(card)
util.log(card.name + " added to want list " + list_name, util.LogLevel.Info)
self.unsaved_changes = True
def add_card_to_lib(self, card, tag=None):
if tag is not None:
self.tag_card(card, tag)

View File

@@ -1,5 +1,6 @@
import gi
from cardvault import util
from cardvault import application
from gi.repository import Gtk, GdkPixbuf, Gdk
import time
gi.require_version('Gtk', '3.0')
@@ -7,7 +8,7 @@ gi.require_version('Gdk', '3.0')
class CardList(Gtk.ScrolledWindow):
def __init__(self, with_filter, app):
def __init__(self, with_filter, app : 'application.Application'):
Gtk.ScrolledWindow.__init__(self)
self.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
self.set_hexpand(True)
@@ -134,11 +135,16 @@ class CardList(Gtk.ScrolledWindow):
util.log("Updating tree view", util.LogLevel.Info)
start = time.time()
for card_id, card in library.items():
all_wants = self.app.get_wanted_card_ids()
for card_id, card in library.items():
if card.multiverse_id is not None:
if self.app.library.__contains__(card_id) and colorize:
color = util.card_view_colors["owned"]
elif all_wants.__contains__(card_id) and colorize:
color = util.card_view_colors["wanted"]
else:
color = util.card_view_colors["unowned"]
if card.type == "Land":

View File

@@ -100,7 +100,6 @@
<property name="model">tagStore</property>
<property name="search_column">1</property>
<signal name="button-press-event" handler="do_tag_tree_press_event" swapped="no"/>
<signal name="drag-data-received" handler="on_drag_data_received" swapped="no"/>
<signal name="row-activated" handler="on_tag_selected" object="tagTreeSelection" swapped="no"/>
<child internal-child="selection">
<object class="GtkTreeSelection" id="tagTreeSelection"/>

View File

@@ -2,31 +2,24 @@
<!-- Generated with glade 3.20.0 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkGrid" id="searchView">
<property name="name">Search</property>
<object class="GtkMenu" id="searchListPopup">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="row_spacing">2</property>
<property name="column_spacing">2</property>
<signal name="show" handler="search_tree_popup_showed" swapped="no"/>
<child>
<object class="GtkOverlay" id="searchResults">
<object class="GtkMenuItem" id="searchListPopupTags">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<child>
<placeholder/>
<property name="label" translatable="yes">Tag Cards</property>
<property name="use_underline">True</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
</packing>
</child>
<object class="GtkPaned" id="searchView">
<property name="visible">True</property>
<property name="can_focus">True</property>
<child>
<object class="GtkBox" id="leftPane">
<object class="GtkBox" id="searchLeftPane">
<property name="name">leftPane</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
@@ -275,14 +268,20 @@
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="height">2</property>
<property name="resize">False</property>
<property name="shrink">True</property>
</packing>
</child>
<child>
<object class="GtkBox" id="searchRightPane">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child>
<object class="GtkBox" id="selectionToolsBox">
<property name="can_focus">False</property>
<property name="spacing">2</property>
<child>
<object class="GtkButton" id="addRemoveButton">
<property name="label" translatable="yes">Add to Library</property>
@@ -306,8 +305,31 @@
</child>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkOverlay" id="searchResults">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="resize">True</property>
<property name="shrink">True</property>
</packing>
</child>
</object>

View File

@@ -52,7 +52,7 @@
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>

View File

@@ -13,8 +13,7 @@ from cardvault import application
class Handlers:
def __init__(self, app):
self.app = Type[application.Application]
def __init__(self, app: 'application.Application'):
self.app = app
# ---------------------------------Main Window----------------------------------------------
@@ -115,6 +114,29 @@ class Handlers:
search_funct.reload_serach_view(self.app)
self.app.ui.get_object("searchEntry").grab_focus()
def search_tree_popup_showed(self, menu):
# Create wants Submenu
wants_item = self.app.ui.get_object("searchListPopupTags")
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)
wants_item.show_all()
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_serach_view(self.app)
self.app.push_status("Added " + str(len(cards)) + " card(s) to Want List '" + item.get_label() + "'")
# ---------------------------------Library----------------------------------------------
def do_reload_library(self, view):
@@ -149,9 +171,6 @@ class Handlers:
lib_funct.reload_library(self.app, tag)
entry.set_text("")
def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
print("drag received")
def on_tag_selected(self, selection, path, column):
(model, pathlist) = selection.get_selected_rows()
for path in pathlist:
@@ -262,6 +281,15 @@ class Handlers:
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))
if path:
tree_iter = treeview.get_model().get_iter(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):

View File

@@ -5,12 +5,13 @@ 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):
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)]
@@ -24,16 +25,14 @@ def init_search_view(app):
# Create Model for search results
_init_results_tree(app)
app.ui.get_object("tagTree").drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.COPY)
def reload_serach_view(app):
def reload_serach_view(app: 'application.Application'):
results_tree = app.ui.get_object("searchResults").get_child()
cards = results_tree.lib
results_tree.update(cards, True)
def get_filters(app):
def get_filters(app: 'application.Application') -> dict:
output = {}
# Mana colors
color_list = []
@@ -58,7 +57,7 @@ def get_filters(app):
return output
def search_cards(term, filters):
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)
@@ -72,12 +71,15 @@ def search_cards(term, filters):
.where(pageSize=50) \
.where(page=1).all()
except (URLError, HTTPError) as err:
print("Error connecting to the internet")
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:
@@ -90,7 +92,7 @@ def search_cards(term, filters):
return lib
def _init_results_tree(app):
def _init_results_tree(app: 'application.Application'):
overlay = app.ui.get_object("searchResults")
card_list = cardlist.CardList(False, app)
card_list.set_name("resultsScroller")
@@ -100,8 +102,11 @@ def _init_results_tree(app):
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):
def _init_combo_box(combo, list: list):
model = Gtk.ListStore(str)
model.append(["All"])
for entry in list:
@@ -113,7 +118,7 @@ def _init_combo_box(combo, list):
combo.set_active(0)
def _remove_duplicates(cards):
def _remove_duplicates(cards: list) -> list:
unique_cards = []
unique_names = []
# Reverse cardlist so we get the version with the most modern art
@@ -124,7 +129,7 @@ def _remove_duplicates(cards):
return unique_cards
def _get_combo_value(combo):
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", "")
@@ -136,7 +141,7 @@ def _init_mana_buttons(app, button_list):
button.set_image(image)
def _init_set_entry(app, entry):
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])

View File

@@ -55,6 +55,7 @@ class Card(object):
self.foreign_names = response_dict.get('foreignNames')
self.owned = None
self.tags = []
self.wanted = None
@staticmethod
def find(id):