From b88622239b80053556638847d645efe2d74d110b Mon Sep 17 00:00:00 2001 From: luxick Date: Fri, 10 Mar 2017 20:30:11 +0100 Subject: [PATCH] Implement library import. --- mtg-collector/gui.py | 9 +++++---- mtg-collector/library.py | 3 ++- mtg-collector/util.py | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/mtg-collector/gui.py b/mtg-collector/gui.py index 2203e4b..c0a43f0 100644 --- a/mtg-collector/gui.py +++ b/mtg-collector/gui.py @@ -14,7 +14,7 @@ class MainWindow(Gtk.Window): self.set_size_request(1000, 700) self.status_bar = Gtk.Statusbar() - self.status_bar.set_no_show_all(True) + self.status_bar.set_no_show_all(False) # Set reference to status bar in util util.status_bar = self.status_bar @@ -83,8 +83,8 @@ class MainWindow(Gtk.Window): vbox.pack_start(self.notebook, True, True, 0) vbox.pack_start(self.status_bar, False, False, 0) - self.library = Gtk.Box() - self.library.add(library.LibraryView()) + self.library = library.LibraryView() + # self.library.add(library.LibraryView()) self.search = Gtk.Box() self.search.add(search.SearchView()) @@ -102,7 +102,8 @@ class MainWindow(Gtk.Window): util.export_library() def mb_import_lib(self, menu_item): - print("Import Library") + util.import_library() + self.library.fill_lib_list() def mb_save_lib(self, menu_item): util.save_library() diff --git a/mtg-collector/library.py b/mtg-collector/library.py index 74166ef..15eda65 100644 --- a/mtg-collector/library.py +++ b/mtg-collector/library.py @@ -127,7 +127,7 @@ class LibraryView(Gtk.Grid): iter = model.get_iter(path) card_id = model.get_value(iter, 0) - if not card_id is None: + if not card_id is None and util.library.__contains__(card_id): selected_card = util.library[card_id] if not selected_card is None: @@ -158,4 +158,5 @@ class LibraryView(Gtk.Grid): def remove_button_clicked(self, button): util.remove_card_from_lib(self.current_card) + # Reset selection in list self.fill_lib_list() diff --git a/mtg-collector/util.py b/mtg-collector/util.py index f291104..ee542a8 100644 --- a/mtg-collector/util.py +++ b/mtg-collector/util.py @@ -41,11 +41,37 @@ def export_library(): pickle.dump(library, open(dialog.get_filename(), 'wb')) except: show_message("Error", "Error while saving library to disk") - push_status("Library exported to " + dialog.get_filename()) - print("Library exported to ", dialog.get_filename()) + push_status("Library exported to \"", dialog.get_filename() + "\"") + print("Library exported to \"", dialog.get_filename() + "\"") dialog.destroy() +def import_library(): + dialog = Gtk.FileChooserDialog("Import Library", window, + Gtk.FileChooserAction.OPEN, + (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, + Gtk.STOCK_OPEN, Gtk.ResponseType.OK)) + dialog.set_current_folder(os.path.expanduser("~")) + response = dialog.run() + if response == Gtk.ResponseType.OK: + override_question = show_question_dialog("Import Library", + "Importing a library will override your current library. " + "Proceed?") + if override_question == Gtk.ResponseType.YES: + imported = None + # try: + imported = pickle.load(open(dialog.get_filename(), 'rb')) + # except: + + library.clear() + for id, card in imported.items(): + library[id] = card + save_library() + + push_status("Library imported") + print("Library imported") + dialog.destroy() + def save_library(): if not os.path.exists(config.cache_path): os.makedirs(config.cache_path) @@ -111,6 +137,7 @@ def reload_image_cache(): # endregion + def add_card_to_lib(card): library[card.multiverse_id] = card global unsaved_changes @@ -132,7 +159,7 @@ def show_question_dialog(title, message): Gtk.ButtonsType.YES_NO, title) dialog.format_secondary_text(message) response = dialog.run() - dialog.destroy + dialog.destroy() return response def show_message(title, message):