1. Santiago Payà Miralta
  2. genet

Commits

Santiago Payà Miralta  committed 29671ab

Treeviews refresh without forgetting selection.

  • Participants
  • Parent commits be22803
  • Branches default

Comments (0)

Files changed (3)

File control/genet_browser.py

View file
     """List nodes from db.
 
     If level is passed returns from that level only, if node type 
-    is passed returns of that type only. All are returned if not.
+    is passed returns of that type only. If not all are returned.
 
     @param level from which the nodes are
     @param node_type

File view/genet.glade

View file
   <widget class="GtkWindow" id="genet">
     <property name="visible">True</property>
     <property name="title" translatable="yes">genet</property>
+    <property name="default_width">800</property>
+    <property name="default_height">600</property>
     <signal name="destroy" handler="on_genet_destroy"/>
     <child>
       <widget class="GtkVBox" id="vbox">
                   </packing>
                 </child>
                 <child>
-                  <placeholder/>
+                  <widget class="GtkButton" id="button_conn_nodes_refresh">
+                    <property name="label" translatable="yes">Refresh</property>
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="receives_default">True</property>
+                    <signal name="clicked" handler="on_button_conn_nodes_refresh_clicked"/>
+                  </widget>
+                  <packing>
+                    <property name="left_attach">1</property>
+                    <property name="right_attach">2</property>
+                    <property name="top_attach">2</property>
+                    <property name="bottom_attach">3</property>
+                    <property name="x_options">GTK_FILL</property>
+                    <property name="y_options"></property>
+                  </packing>
                 </child>
                 <child>
-                  <placeholder/>
+                  <widget class="GtkComboBox" id="combobox_conn_nodes_type">
+                    <property name="visible">True</property>
+                  </widget>
+                  <packing>
+                    <property name="left_attach">1</property>
+                    <property name="right_attach">2</property>
+                    <property name="x_options">GTK_FILL</property>
+                    <property name="y_options"></property>
+                  </packing>
                 </child>
               </widget>
               <packing>

File view/genet_window.py

View file
 
 from view import genet_treemodels
         
-class window:
+class GenetWindow(gtk.Window):
     def __init__(self):
         """
         The window.
         column01.set_clickable(True)
         tree.append_column(column01)
 
-        column02 = gtk.TreeViewColumn("Type", cell, text=2)
+        column02 = gtk.TreeViewColumn("Attribute", cell, text=2)
         column02.set_clickable(True)
         tree.append_column(column02)
 
+        tree.set_expander_column(tree.get_column(1))
+
         ##
         # Connect nodes B tree
         tree = self.wTree.get_widget("treeview_conn_nodes_B")
         column01.set_clickable(True)
         tree.append_column(column01)
 
-        column02 = gtk.TreeViewColumn("Type", cell, text=2)
+        column02 = gtk.TreeViewColumn("Attribute", cell, text=2)
         column02.set_clickable(True)
         tree.append_column(column02)
-        
+
+        tree.set_expander_column(tree.get_column(1))
+
         ##
         # Create node types tree
         tree = self.wTree.get_widget("treeview_create_node_types")
         column02.set_clickable(True)
         tree.append_column(column02)
 
+        tree.set_expander_column(tree.get_column(1))
+
         ##
         # Create node parents tree
         tree = self.wTree.get_widget("treeview_create_node_parents")
         column02.set_clickable(True)
         tree.append_column(column02)
 
+        tree.set_expander_column(tree.get_column(1))
+
         ##
         # Admin levels tree
         tree = self.wTree.get_widget("treeview_admin_levels")
         column02.set_clickable(True)
         tree.append_column(column02)
 
-        # Declare dictionary.
+        tree.set_expander_column(tree.get_column(1))
+
+        # Declare actions dictionary.
         dic = {
             # Browse tab.
             "on_button_connect_nodes_clicked" : 
 
             # Connect nodes tab.
             "on_button_conn_nodes_clicked" :
-            self.button_conn_nodes_clicked,
-
+                self.button_conn_nodes_clicked,
+            "on_button_conn_nodes_refresh_clicked" :
+                self.button_conn_nodes_refresh_clicked,
+            
             # Create tab.
             "on_button_create_node_clicked" :
-            self.button_create_node_clicked,
-
+                self.button_create_node_clicked,
+            
             # Admin tab.
             "on_button_create_level_clicked" : 
             self.button_create_level_clicked,
             "on_button_drop_item_clicked" : 
             self.button_drop_item_clicked,
             "on_button_create_node_type_clicked" :
-            self.button_create_node_type_clicked,
+                self.button_create_node_type_clicked,
             "on_button_create_edge_type_clicked" :
-            self.button_create_edge_type_clicked,
+                self.button_create_edge_type_clicked,
             "on_button_admin_reset_clicked" :
-            self.button_admin_reset_clicked,
+                self.button_admin_reset_clicked,
             "on_genet_destroy" : (gtk.main_quit) }
         self.wTree.signal_autoconnect(dic)
         return
     def button_conn_nodes_clicked(self, widget):
         print 'Connect Clicked!!'
 
-        # TODO Refresh tree without forget selection
-        tree = self.wTree.get_widget("treeview_conn_nodes_A")
-        tree_model = tree.get_model()
-        tree_model.reload()
-        tree = self.wTree.get_widget("treeview_conn_nodes_B")
-        tree_model = tree.get_model()
-        tree_model.reload()
+        # Refresh tree
+        self.tree_refresh("treeview_conn_nodes_A")
+        self.tree_refresh("treeview_conn_nodes_B")
+        
+    def button_conn_nodes_refresh_clicked(self, widget):
+        """ Refresh trees from data model.
+        """
+        self.tree_refresh("treeview_conn_nodes_A")
+        self.tree_refresh("treeview_conn_nodes_B")
+        
 
     ############################################################################
     # Create tab.
         genet_creator.createNode(level_id, parent_id, 
                                  parentlevel_id, type_id, attributes)
 
-        # TODO Refresh tree without forget selection
-        tree = self.wTree.get_widget("treeview_create_node_types")
-        tree_model = tree.get_model()
-        tree_model.reload()
-        tree = self.wTree.get_widget("treeview_create_node_parents")
-        tree_model = tree.get_model()
-        tree_model.reload()
+        # Refresh tree
+        self.tree_refresh("treeview_create_node_types")
+        self.tree_refresh("treeview_create_node_parents")
 
     ############################################################################
     # Admin tab.
         genet_creator.createLevel(description)
 
         # Refresh tree
-        tree = self.wTree.get_widget("treeview_admin_levels")
-        tree_model = tree.get_model()
-        tree_model.reload()
+        self.tree_refresh("treeview_admin_levels")
 
     def button_create_node_type_clicked(self, widget):
         # Get level
         # Create type
         genet_creator.createNodeType(level, description)
 
-        # TODO Refresh tree without forget selection
-        tree = self.wTree.get_widget("treeview_admin_levels")
-        tree_model = tree.get_model()
-        tree_model.reload()
+        # Refresh tree
+        self.tree_refresh("treeview_admin_levels")
 
     def button_create_edge_type_clicked(self, widget):
         # Get level
         # Create type
         genet_creator.createEdgeType(level, description)
 
-        # TODO Refresh tree without forget selection
-        tree = self.wTree.get_widget("treeview_admin_levels")
-        tree_model = tree.get_model()
-        tree_model.reload()
+        # Refresh tree
+        self.tree_refresh("treeview_admin_levels")
 
     def button_drop_item_clicked(self, widget):
         values = self.get_tree_selected_values("treeview_admin_levels")
             # TODO raise exception. Never should be here.
             print "*** Error in code ***"
 
-        # TODO Refresh tree without forget selection
-        tree = self.wTree.get_widget("treeview_admin_levels")
-        tree_model = tree.get_model()
-        tree_model.reload()
+        # Refresh tree
+        self.tree_refresh("treeview_admin_levels")
 
     def button_admin_reset_clicked(self, widget):
         genet_global.restartDatabase()
         
-        # TODO Refresh tree without forget selection
-        tree = self.wTree.get_widget("treeview_admin_levels")
-        tree_model = tree.get_model()
-        tree_model.reload()
+        # Refresh tree
+        self.tree_refresh("treeview_admin_levels")
 
     def get_admin_tree_level(self):
-        # Get all information from the selected row in the tree
+        """Get selected level. From the level row or any selected child.
+        """
         tree = self.wTree.get_widget("treeview_admin_levels")
         tree_selection = tree.get_selection()
         tree_selection_model = tree_selection.get_selected()[0]
         tree_selection = tree.get_selection()
         tree_selection_model = tree_selection.get_selected()[0]
         tree_selection_iter = tree_selection.get_selected()[1]
-        tree_columns = tree_selection_model.get_n_columns()
+        tree_n_columns = tree_selection_model.get_n_columns()
         
         # Get the root from selection. Could be in root now, so root iter is
         # the actually selected iter.
             parent_iter = tree_selection_model.iter_parent(parent_iter)
 
         values = ()
-        for c in range(tree_columns):
+        for c in range(tree_n_columns):
             values += tree_selection_model.get(root_iter, c)
 
         return values
         tree_selection = tree.get_selection()
         tree_selection_model = tree_selection.get_selected()[0]
         tree_selection_iter = tree_selection.get_selected()[1]
-        tree_columns = tree_selection_model.get_n_columns()
+        tree_n_columns = tree_selection_model.get_n_columns()
         
         values = ()
-        for c in range(tree_columns):
+        for c in range(tree_n_columns):
             values += tree_selection_model.get(tree_selection_iter, c)
 
         return values
 
-app = window()
-app.main()
+    def tree_refresh(self, tree_widget):
+        """Refresh tree data without forget selection.
+        """
+        tree_selected_path = (0,)
 
-# If run as script, print some information
-#if __name__ == '__main__':
-#    print __doc__
+        tree = self.wTree.get_widget(tree_widget)
+        tree_selection = tree.get_selection()
+        tree_selection_model = tree_selection.get_selected()[0]
+        tree_selection_iter = tree_selection.get_selected()[1]
+        
+        # Save selected item or path
+        if tree_selection_iter is not None:
+            tree_selected_path = tree_selection_model.get_path(tree_selection_iter)
+
+        # Refresh data, reload data into model
+        tree.get_model().reload()
+
+        # Expand old selection
+        tree.expand_to_path(tree_selected_path)
+        #tree_selection.select_path(tree_selected_path)
+
+if __name__ == '__main__':
+    app = GenetWindow()
+    app.main()