Version 1.0 de jsoninspector

This commit is contained in:
José Carlos Cuevas 2010-05-26 13:58:33 +02:00
parent 50b90e5e2a
commit c0450d7db6
2 changed files with 100 additions and 6 deletions

View file

@ -2,7 +2,14 @@
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkTreeStore" id="treestore1"/>
<object class="GtkTreeStore" id="treestore1">
<columns>
<!-- column-name key_val -->
<column type="gchararray"/>
<!-- column-name value_val -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkWindow" id="MainWindow">
<property name="width_request">400</property>
<property name="height_request">400</property>
@ -77,12 +84,44 @@
</packing>
</child>
<child>
<object class="GtkTreeView" id="treeview1">
<property name="width_request">300</property>
<property name="height_request">300</property>
<object class="GtkScrolledWindow" id="scrolledwindow1">
<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="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="headers_clickable">False</property>
<property name="expander_column">treeviewcolumn1</property>
<property name="search_column">0</property>
<property name="level_indentation">1</property>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn1">
<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="treeviewcolumn2">
<property name="resizable">True</property>
<property name="title">Valor</property>
<property name="expand">True</property>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="position">1</property>
@ -116,4 +155,11 @@
<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>
</interface>

View file

@ -26,6 +26,18 @@ class MainWindowMethods(object):
self.builder.add_from_file("jsoninspector.glade")
self.builder.connect_signals(self)
cell = gtk.CellRendererText()
columns = self.builder.get_object("treeviewcolumn1")
columns.pack_start(cell)
columns.add_attribute(cell, 'text', 0)
treeview = self.builder.get_object("treeview1")
treeview.set_expander_column(columns)
cell = gtk.CellRendererText()
columns = self.builder.get_object("treeviewcolumn2")
columns.pack_start(cell)
columns.add_attribute(cell, 'text', 1)
self.window = self.builder.get_object("MainWindow")
self.window.show_all()
@ -57,7 +69,10 @@ class MainWindowMethods(object):
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 hay JSON cargado")
@ -105,6 +120,39 @@ class LogicObject(object):
f.close()
return True
def loadTree(self, treestore):
"""
Loads the JSON into the tree store for display purposes
"""
for key_val in self.json.keys():
if type(self.json[key_val]) is dict:
parent_node = treestore.append(None, [str(key_val), ""])
self._loadTreeRec(treestore, self.json[key_val], parent_node)
else:
treestore.append(None, [str(key_val), str(self.json[key_val])])
def _loadTreeRec(self, treestore, elems, parent_node):
"""
Recursive element adding to the tree
"""
for key_val in elems.keys():
if type(elems[key_val]) is dict:
new_parent_node = treestore.append(parent_node,
[str(key_val),
""])
self._loadTreeRec(treestore, elems[key_val], new_parent_node)
else:
treestore.append(parent_node, [str(key_val), str(elems[key_val])])
# Ejecucion del programa principal