Implement library export.

This commit is contained in:
luxick
2017-03-10 19:08:43 +01:00
parent 6ec512b037
commit d12d0b5195
2 changed files with 55 additions and 37 deletions

View File

@@ -37,14 +37,14 @@ class MainWindow(Gtk.Window):
mb_lib = Gtk.Menu()
self.menu_import = Gtk.MenuItem("Import Library")
self.menu_import.connect("activate", self.mb_import_lib)
self.menu_export = Gtk.MenuItem("Export Library")
self.menu_export.connect("activate", self.mb_export_lib)
self.menu_quit = Gtk.ImageMenuItem('Quit', Gtk.Image.new_from_icon_name(Gtk.STOCK_QUIT, 0))
self.menu_quit.connect("activate", Gtk.main_quit)
self.lib_save = Gtk.ImageMenuItem("Save", Gtk.Image.new_from_icon_name(Gtk.STOCK_SAVE, 0))
self.lib_save.connect("activate", self.mb_save_lib)
self.lib_debug_print = Gtk.MenuItem("DEBUG: Print Library")
self.lib_debug_print.connect("activate", util.print_lib)
mb_main.append(self.menu_import)
mb_main.append(self.menu_export)
@@ -52,7 +52,6 @@ class MainWindow(Gtk.Window):
mb_main.append(self.menu_quit)
mb_lib.append(self.lib_save)
mb_lib.append(self.lib_debug_print)
root_menu_main = Gtk.MenuItem("Main")
root_menu_main.set_submenu(mb_main)
@@ -99,6 +98,12 @@ class MainWindow(Gtk.Window):
self.add(vbox)
def mb_export_lib(self, menu_item):
util.export_library()
def mb_import_lib(self, menu_item):
print("Import Library")
def mb_save_lib(self, menu_item):
util.save_library()

View File

@@ -1,4 +1,6 @@
import os
import datetime
import gi
import re
import config
@@ -23,27 +25,25 @@ status_bar = None
unsaved_changes = False
def add_card_to_lib(card):
library[card.multiverse_id] = card
global unsaved_changes
unsaved_changes = True
# region File Access
def remove_card_from_lib(card):
del library[card.multiverse_id]
global unsaved_changes
unsaved_changes = True
# Debug function for library
def print_lib(menuItem):
print("Printing library:\n")
counter = 1
for card_id, card in library.items():
print(str(counter) + ": " + card.name + " (" + str(card_id) + ")")
counter += 1
print("\nDone.")
def export_library():
dialog = Gtk.FileChooserDialog("Export Library", window,
Gtk.FileChooserAction.SAVE,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
dialog.set_current_name("mtg_export-" + datetime.datetime.now().strftime("%Y-%m-%d"))
dialog.set_current_folder(os.path.expanduser("~"))
response = dialog.run()
if response == Gtk.ResponseType.OK:
try:
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())
dialog.destroy()
def save_library():
@@ -95,6 +95,34 @@ def load_sets():
set_list.append(set)
def reload_image_cache():
if not os.path.exists(config.image_cache_path):
os.makedirs(config.image_cache_path)
# return array of images
imageslist = os.listdir(config.image_cache_path)
imagecache.clear()
for image in imageslist:
try:
pixbuf = GdkPixbuf.Pixbuf.new_from_file(config.image_cache_path + image)
imagecache[image] = pixbuf
except OSError as err:
print("Error loading image: " + str(err))
# endregion
def add_card_to_lib(card):
library[card.multiverse_id] = card
global unsaved_changes
unsaved_changes = True
def remove_card_from_lib(card):
del library[card.multiverse_id]
global unsaved_changes
unsaved_changes = True
def push_status(msg):
status_bar.push(0, msg)
@@ -128,21 +156,6 @@ def load_mana_icons():
manaicons[os.path.splitext(image)[0]] = img
def reload_image_cache():
if not os.path.exists(config.image_cache_path):
os.makedirs(config.image_cache_path)
# return array of images
imageslist = os.listdir(config.image_cache_path)
imagecache.clear()
for image in imageslist:
try:
pixbuf = GdkPixbuf.Pixbuf.new_from_file(config.image_cache_path + image)
imagecache[image] = pixbuf
except OSError as err:
print("Error loading image: " + str(err))
def load_dummy_image(sizex, sizey):
return GdkPixbuf.Pixbuf.new_from_file_at_size(os.path.dirname(__file__)
+ '/resources/images/dummy.jpg', sizex, sizey)