Updated to GTK3.

This commit is contained in:
José Carlos Cuevas 2014-07-14 10:37:32 +02:00
parent cb9fb18911
commit c8140f5abe
2 changed files with 241 additions and 167 deletions

View file

@ -1,7 +1,117 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<requires lib="gtk+" version="3.0"/>
<object class="GtkWindow" id="TextWindow">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Introducir texto JSON</property>
<property name="modal">True</property>
<property name="window_position">center</property>
<property name="default_width">200</property>
<property name="default_height">180</property>
<signal name="delete-event" handler="onCopyJSONDelete" swapped="no"/>
<signal name="destroy" handler="onCopyJSONDestroy" swapped="no"/>
<child>
<object class="GtkBox" id="box2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow2">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkTextView" id="textview1">
<property name="width_request">200</property>
<property name="height_request">140</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="wrap_mode">char</property>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkBox" id="box3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="button2">
<property name="label">gtk-cancel</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<signal name="clicked" handler="onCopyJSONCancelClicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">5</property>
<property name="pack_type">end</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label">gtk-ok</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<signal name="clicked" handler="onCopyJSONAcceptClicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">5</property>
<property name="pack_type">end</property>
<property name="position">1</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
<object class="GtkAdjustment" id="adjustment1">
<property name="upper">100</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
<property name="page_size">10</property>
</object>
<object class="GtkAdjustment" id="adjustment2">
<property name="upper">100</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
<property name="page_size">10</property>
</object>
<object class="GtkImage" id="image1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-copy</property>
</object>
<object class="GtkTreeSelection" id="treeselection1"/>
<object class="GtkTreeStore" id="treestore1">
<columns>
<!-- column-name key_val -->
@ -15,53 +125,64 @@
<object class="GtkWindow" id="MainWindow">
<property name="width_request">400</property>
<property name="height_request">400</property>
<property name="can_focus">False</property>
<property name="title" translatable="yes">JSON Inspector</property>
<property name="window_position">center</property>
<signal name="delete_event" handler="onMainWindowDelete"/>
<signal name="delete-event" handler="onMainWindowDelete" swapped="no"/>
<child>
<object class="GtkVBox" id="vbox1">
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkMenuBar" id="menubar1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuItem" id="menuitem1">
<property name="visible">True</property>
<property name="label" translatable="yes">_Archivo</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_File</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="menu1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem2">
<property name="label">gtk-open</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<signal name="activate" handler="onOpenMenuClicked"/>
<signal name="activate" handler="onOpenMenuClicked" swapped="no"/>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem1">
<property name="label">Copiar JSON</property>
<property name="label" translatable="yes">Cop_y JSON</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="image">image1</property>
<property name="use_stock">False</property>
<signal name="activate" handler="onCopyJSONClicked"/>
<signal name="activate" handler="onCopyJSONClicked" swapped="no"/>
</object>
</child>
<child>
<object class="GtkSeparatorMenuItem" id="separatormenuitem1">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem5">
<property name="label">gtk-quit</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<signal name="activate" handler="onExitMenuClicked"/>
<signal name="activate" handler="onExitMenuClicked" swapped="no"/>
</object>
</child>
</object>
@ -71,17 +192,21 @@
<child>
<object class="GtkMenuItem" id="menuitem4">
<property name="visible">True</property>
<property name="label" translatable="yes">Ay_uda</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Help</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="menu3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem10">
<property name="label">gtk-about</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<signal name="activate" handler="onAboutMenuActivate" swapped="no"/>
</object>
</child>
</object>
@ -91,6 +216,7 @@
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
@ -100,22 +226,23 @@
<property name="can_focus">True</property>
<property name="hadjustment">adjustment1</property>
<property name="vadjustment">adjustment2</property>
<property name="hscrollbar_policy">automatic</property>
<property name="vscrollbar_policy">automatic</property>
<child>
<object class="GtkTreeView" id="treeview1">
<property name="width_request">300</property>
<property name="height_request">300</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">treestore1</property>
<property name="hadjustment">adjustment1</property>
<property name="vadjustment">adjustment2</property>
<property name="model">treestore1</property>
<property name="headers_clickable">False</property>
<property name="expander_column">treeviewcolumn1</property>
<property name="search_column">0</property>
<property name="level_indentation">1</property>
<property name="enable_grid_lines">both</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="treeview-selection"/>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn1">
<property name="resizable">True</property>
@ -143,125 +270,87 @@
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkStatusbar" id="statusbar1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">2</property>
<child>
<object class="GtkLabel" id="StatusLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">No hay JSON cargado</property>
<attributes>
<attribute name="gravity" value="west"/>
</attributes>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
<object class="GtkAdjustment" id="adjustment1">
<property name="upper">100</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
<property name="page_size">10</property>
</object>
<object class="GtkTreeSelection" id="treeselection1"/>
<object class="GtkAdjustment" id="adjustment2">
<property name="upper">100</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
<property name="page_size">10</property>
</object>
<object class="GtkWindow" id="TextWindow">
<property name="title" translatable="yes">Introducir texto JSON</property>
<property name="modal">True</property>
<property name="window_position">center</property>
<property name="default_width">200</property>
<property name="default_height">180</property>
<signal name="destroy" handler="onCopyJSONDestroy"/>
<signal name="delete_event" handler="onCopyJSONDelete"/>
<object class="GtkScrolledWindow" id="scrolledwindow3">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hadjustment">adjustment1</property>
<property name="vadjustment">adjustment2</property>
<child>
<object class="GtkVBox" id="vbox2">
<object class="GtkTreeView" id="treeview2">
<property name="width_request">300</property>
<property name="height_request">300</property>
<property name="visible">True</property>
<child>
<object class="GtkTextView" id="textview1">
<property name="width_request">200</property>
<property name="height_request">140</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="wrap_mode">char</property>
</object>
<packing>
<property name="padding">5</property>
<property name="position">0</property>
</packing>
<property name="can_focus">True</property>
<property name="hadjustment">adjustment1</property>
<property name="vadjustment">adjustment2</property>
<property name="model">treestore1</property>
<property name="headers_clickable">False</property>
<property name="expander_column">treeviewcolumn1</property>
<property name="search_column">0</property>
<property name="level_indentation">1</property>
<property name="enable_grid_lines">both</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="treeview-selection1"/>
</child>
<child>
<object class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label">gtk-ok</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<signal name="clicked" handler="onCopyJSONAcceptClicked"/>
</object>
<packing>
<property name="expand">False</property>
<property name="padding">5</property>
<property name="position">1</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label">gtk-cancel</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
<signal name="clicked" handler="onCopyJSONCancelClicked"/>
</object>
<packing>
<property name="expand">False</property>
<property name="padding">5</property>
<property name="position">3</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<object class="GtkTreeViewColumn" id="treeviewcolumn4">
<property name="resizable">True</property>
<property name="title">Nodo</property>
<property name="expand">True</property>
<property name="clickable">True</property>
<property name="sort_column_id">0</property>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn5">
<property name="resizable">True</property>
<property name="title">Valor</property>
<property name="expand">True</property>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn6">
<property name="resizable">True</property>
<property name="title">Tipo</property>
<property name="expand">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="padding">5</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
<object class="GtkImage" id="image1">
<property name="visible">True</property>
<property name="stock">gtk-copy</property>
</object>
</interface>

View file

@ -4,78 +4,73 @@
from pkg_resources import resource_filename
import json
import sys
import pygtk
try:
pygtk.require("2.0")
except:
print "Versión de PyGTK incorrecta!\n"
sys.exit(1)
from gi.repository import Gtk, Gdk, Gio
import gtk
class MainWindowMethods(object):
class MainWindowMethods(Gtk.Application):
"""
Clase que contiene los métodos de la ventana principal
Main Application object with the main window signals
"""
def __init__(self, logic):
Gtk.Application.__init__(self, application_id = "apps.gnome.stevedore",
flags = Gio.ApplicationFlags.FLAGS_NONE)
# We store the reference to the app logic
self.logicObj = logic
self.connect("activate", self.on_app_start)
def on_app_start(self, data = None):
"""
Carga y conecta el XML de gtkBuilder, además de mostrar la ventana
principal
Loads the MainWindow widgets, shows it up and starts the main loop
"""
self.builder = gtk.Builder()
self.builder = Gtk.Builder()
self.builder.add_from_file(resource_filename(__name__,'jsoninspector.glade'))
self.builder.connect_signals(self)
# Prepara los renderizados de columna y las asigna a los valores
cell = gtk.CellRendererText()
# Prepares the renders of columns and assigns the values
cell = Gtk.CellRendererText()
columns = self.builder.get_object("treeviewcolumn1")
columns.pack_start(cell)
columns.pack_start(cell, True)
columns.add_attribute(cell, 'text', 0)
treeview = self.builder.get_object("treeview1")
# Esta columna es la que tiene los nodos de apertura y cierre
# This column has the open and collapse nodes
treeview.set_expander_column(columns)
cell = gtk.CellRendererText()
cell = Gtk.CellRendererText()
columns = self.builder.get_object("treeviewcolumn2")
columns.pack_start(cell)
columns.pack_start(cell, True)
columns.add_attribute(cell, 'text', 1)
cell = gtk.CellRendererText()
cell = Gtk.CellRendererText()
columns = self.builder.get_object("treeviewcolumn3")
columns.pack_start(cell)
columns.pack_start(cell, True)
columns.add_attribute(cell, 'text', 2)
# Obtenemos la ventana y la mostramos completamente
# We get the window and show it
self.window = self.builder.get_object("MainWindow")
self.add_window(self.window)
self.window.show_all()
# Obtenemos un enlace al objeto de lógica de aplicación
self.logicObj = logic
def onOpenMenuClicked(self, event):
"""
El usuario ha presionado Abrir en el menu
User has pressed Open in the menu
"""
# Creamos el dialogo de abrir fichero
chooser = gtk.FileChooserDialog(title=None,
action=gtk.FILE_CHOOSER_ACTION_OPEN,
buttons=(gtk.STOCK_CANCEL,
gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN,
gtk.RESPONSE_OK))
chooser.set_default_response(gtk.RESPONSE_OK)
# 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))
# Lo lanzamos
# Launch it
response = chooser.run()
# Si hemos seleccionado un fichero, lo cargamos
if response == gtk.RESPONSE_OK:
# If we choose a file, we load it
if response == Gtk.ResponseType.OK:
filename = chooser.get_filename()
label = self.builder.get_object("StatusLabel")
# Según todo haya ido, actualizamos la barra de estado
# We update the statusbar accordingly to what has happened
if self.logicObj.loadjson(filename):
label.set_text(filename)
@ -87,20 +82,20 @@ class MainWindowMethods(object):
label.set_text("No hay JSON cargado")
# Nos deshacemos del dialogo
# We finish the dialog
chooser.destroy()
def onCopyJSONClicked(self, widget):
"""
Se ha pulsado Copiar JSON
User asked to paste a JSON code
"""
# Mostrar la ventana de texto
# Show up the TextWindow
textWindow = self.builder.get_object("TextWindow")
textWindow.show_all()
def onCopyJSONDelete(self, widget, event):
"""
Se ha dado a cerrar la ventana de copiar texto
We've been told to close the CopyJSON Window
"""
textWindow = self.builder.get_object("TextWindow")
textWindow.hide()
@ -109,7 +104,7 @@ class MainWindowMethods(object):
def onCopyJSONDestroy(self, widget):
"""
Se ha cerrado la ventana
We've been tasked with removing the CopyJSON window
"""
textWindow = self.builder.get_object("TextWindow")
textWindow.hide()
@ -118,12 +113,12 @@ class MainWindowMethods(object):
def onCopyJSONAcceptClicked(self, widget):
"""
Se ha aceptado el código
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())
jsonBuffer.get_end_iter(), True)
textWindow = self.builder.get_object("TextWindow")
textWindow.hide()
@ -138,24 +133,25 @@ class MainWindowMethods(object):
def onCopyJSONCancelClicked(self, widget):
"""
Se ha cancelado
The user changed its minds and pressed cancel
"""
textWindow = self.builder.get_object("TextWindow")
textWindow.hide()
def onExitMenuClicked(self, widget):
"""
Se ha pulsado salir
Menu option Exit has been clicked
"""
gtk.main_quit()
sys.exit(0)
self.quit()
def onAboutMenuActivate(self, widget):
pass
def onMainWindowDelete(self, widget, event):
"""
Se ha destruido o mandado cerrar la ventana principal
Our MainWindow has been deleted or closed
"""
gtk.main_quit()
sys.exit(0)
pass
class LogicObject(object):
@ -246,21 +242,10 @@ class LogicObject(object):
str(type(elems[key_val]))])
class MainApp(object):
"""
Clase principal
"""
@staticmethod
def start():
logicObject = LogicObject()
mainWindow = MainWindowMethods(logicObject)
gtk.main()
# Ejecucion del programa principal
# Main procedure
if __name__ == "__main__":
app = MainApp()
app.start()
logicObject = LogicObject()
mainWindow = MainWindowMethods(logicObject)
mainWindow.run(None)