diff --git a/gui.py b/gui.py new file mode 100644 index 0000000..1d9f76e --- /dev/null +++ b/gui.py @@ -0,0 +1,183 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from gi.repository import Gtk, Gio + +import os.path +import sys + +# Internationalization support +import gettext +import locale + + +if os.path.exists('locale/po') and os.path.exists('res'): + # We're in the development tree + DIR = "locale/po/" + RESOURCES = "res/" + +elif sys.platform != 'win32' and sys.platform != 'darwin': + DIR = "/usr/share/locale/" + RESOURCES = "/usr/local/share/jsoninspector" + +else: + DIR = "po" + RESOURCES = "res/" + +# Internationalization code, we'll cross that bridge when we get there +# APP = "smallprint" +# +# +# locale.setlocale(locale.LC_ALL, '') +# +# gettext.bindtextdomain(APP, DIR) +# locale.bindtextdomain(APP, DIR) +# +# +# gettext.textdomain(APP) +# _ = gettext.gettext +# + + +class MainWindowMethods(Gtk.Application): + """ + Main Application object with the main window signals + """ + def __init__(self): + Gtk.Application.__init__(self, application_id="apps.gnome.smallprint", + flags=Gio.ApplicationFlags.FLAGS_NONE) + + self.connect("activate", self.on_app_start) + + def on_app_start(self, data=None): + """ + Loads the MainWindow widgets, shows it up and starts the main loop + """ + self.builder = Gtk.Builder() + # self.builder.set_translation_domain(APP) + self.builder.add_from_file(os.path.join(RESOURCES, 'smallprint.gtkbuilder')) + self.builder.connect_signals(self) + + # We get the window and show it + self.window = self.builder.get_object("MainWindow") + self.add_window(self.window) + self.window.show_all() + + def onOpenMenuClicked(self, event): + """ + User has pressed Open in the menu + """ + # Create the FileChooser Dialog + chooser = Gtk.FileChooserDialog(_("Open JSON text file"), self.window, + Gtk.FileChooserAction.OPEN, + (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, + Gtk.STOCK_OPEN, Gtk.ResponseType.OK)) + + # Launch it + response = chooser.run() + + # If we choose a file, we load it + if response == Gtk.ResponseType.OK: + filename = chooser.get_filename() + label = self.builder.get_object("StatusLabel") + + # We update the statusbar accordingly to what has happened + if self.logicObj.loadjson(filename): + + label.set_text(filename) + treeStore = self.builder.get_object("treestore1") + treeStore.clear() + self.logicObj.loadTree(treeStore) + + else: + + label.set_text(_("No JSON loaded.")) + + # We finish the dialog + chooser.destroy() + + def onCopyJSONClicked(self, widget): + """ + User asked to paste a JSON code + """ + # Show up the TextWindow + textWindow = self.builder.get_object("TextWindow") + textWindow.show_all() + + def onCopyJSONDelete(self, widget, event): + """ + We've been told to close the CopyJSON Window + """ + textWindow = self.builder.get_object("TextWindow") + textWindow.hide() + + return True + + def onCopyJSONDestroy(self, widget): + """ + We've been tasked with removing the CopyJSON window + """ + textWindow = self.builder.get_object("TextWindow") + textWindow.hide() + + return True + + def onCopyJSONAcceptClicked(self, widget): + """ + The input is finished and accepted. + """ + textView = self.builder.get_object("textview1") + jsonBuffer = textView.get_buffer() + jsonText = jsonBuffer.get_text(jsonBuffer.get_start_iter(), + jsonBuffer.get_end_iter(), True) + + textWindow = self.builder.get_object("TextWindow") + textWindow.hide() + treestore = self.builder.get_object("treestore1") + treestore.clear() + + if self.logicObj.loadJSONText(jsonText): + + status_label = self.builder.get_object("StatusLabel") + status_label.set_text(_("Loaded from the clipboard.")) + self.logicObj.loadTree(treestore) + + def onCopyJSONCancelClicked(self, widget): + """ + The user changed its mind and pressed cancel + """ + textWindow = self.builder.get_object("TextWindow") + textWindow.hide() + + def onExitMenuClicked(self, widget): + """ + Menu option Exit has been clicked + """ + self.quit() + + def onAboutMenuActivate(self, widget): + """ + About option clicked + """ + about_dialog = self.builder.get_object("AboutDialog") + about_dialog.run() + about_dialog.hide() + + def onMainWindowDelete(self, widget, event): + """ + Our MainWindow has been deleted or closed + """ + pass + + def onAboutDialogClose(self, widget, event=None): + pass + + def onAboutDialogDeleteEvent(self, widget, event=None): + pass + + +# Main procedure +if __name__ == "__main__": + + mainWindow = MainWindowMethods() + mainWindow.run(None)