[graph-gtk] Added graph_gtk_view_arrange function



commit f843e62b12239dd4874640f571a1238b6f713e59
Author: Isaac Wagner <isaacbw src gnome org>
Date:   Tue Aug 7 10:09:59 2012 -0400

    Added graph_gtk_view_arrange function

 graph-gtk/graph-gtk-node.h |    2 +
 graph-gtk/graph-gtk-view.c |   81 ++++++++++++++++++++++++++++++++++++++++++++
 graph-gtk/graph-gtk-view.h |    3 +-
 3 files changed, 85 insertions(+), 1 deletions(-)
---
diff --git a/graph-gtk/graph-gtk-node.h b/graph-gtk/graph-gtk-node.h
index 3b07e7b..c557c01 100644
--- a/graph-gtk/graph-gtk-node.h
+++ b/graph-gtk/graph-gtk-node.h
@@ -53,6 +53,8 @@ struct _GraphGtkNode
 
   const gchar *name;
 
+  gint rank;
+
   gpointer user_data; //intended to be used by the user, e.g. for tying GraphGtkNodes to GeglNodes. Not used for anything internally
 
   gboolean resizable;
diff --git a/graph-gtk/graph-gtk-view.c b/graph-gtk/graph-gtk-view.c
index d69b032..8364dd0 100644
--- a/graph-gtk/graph-gtk-view.c
+++ b/graph-gtk/graph-gtk-view.c
@@ -550,3 +550,84 @@ graph_gtk_view_set_bg(GraphGtkView* self, cairo_surface_t* bg)
   self->bg = bg;
   REDRAW();
 }
+
+static void assign_rank(GraphGtkNode *node, gint rank)
+{
+  if(rank > node->rank)
+    node->rank = rank;
+
+  GList *output_pads;
+  for(output_pads = graph_gtk_node_get_output_pads(node); output_pads != NULL; output_pads = output_pads->next)
+    {
+      GraphGtkPad *pad = output_pads->data;
+      GList *connections;
+      for(connections = pad->connections; connections != NULL; connections = connections->next)
+	{
+	  GraphGtkConnection *conn = connections->data;
+	  assign_rank(conn->sink->node, rank+1);
+	}
+    }
+}
+
+void
+graph_gtk_view_arrange(GraphGtkView* self)
+{
+  GList *list;
+  for(list = self->nodes; list != NULL; list = list->next)
+    {
+      GraphGtkNode *node = GRAPH_GTK_NODE(list->data);
+      node->rank = 0;
+    }
+
+  GList *roots = NULL;
+  for(list = self->nodes; list != NULL; list = list->next)
+    {
+      GraphGtkNode *node = GRAPH_GTK_NODE(list->data);
+
+      gboolean root = TRUE;
+
+      GList *input_pads;
+      for(input_pads = graph_gtk_node_get_input_pads(node); input_pads != NULL; input_pads = input_pads->next)
+	{
+	  GraphGtkPad *pad = input_pads->data;
+	  if(g_list_length(pad->connections) > 0)
+	    root = FALSE;
+	}
+      
+      if(root)
+	{
+	  roots = g_list_append(roots, node);
+	}
+    }
+
+  for(list = roots; list != NULL; list = list->next)
+    {
+      GraphGtkNode *node = GRAPH_GTK_NODE(list->data);
+      assign_rank(node, 1);
+    }
+
+  GHashTable *hash = g_hash_table_new(g_int_hash, g_int_equal);
+
+  for(list = self->nodes; list != NULL; list = list->next)
+    {
+      GraphGtkNode *node = GRAPH_GTK_NODE(list->data);
+
+      int *rank_num = g_hash_table_lookup(hash, &(node->rank));
+
+      if(rank_num == NULL)
+	{
+	  rank_num = g_new(gint, 1);
+	  *rank_num = 1;
+	  g_hash_table_insert(hash, &(node->rank), rank_num);
+	}
+      else
+	{
+	  *rank_num += 1;
+	}
+
+      node->x = (node->rank-1) * 160;
+      node->y = (*rank_num-1) * 100;
+
+      //g_hash_table_insert(hash, (gpointer)node->rank, (gpointer)((gint)g_hash_table_lookup(hash, (gpointer)node->rank)+1));
+    }
+}
diff --git a/graph-gtk/graph-gtk-view.h b/graph-gtk/graph-gtk-view.h
index a8b1c07..6d57791 100644
--- a/graph-gtk/graph-gtk-view.h
+++ b/graph-gtk/graph-gtk-view.h
@@ -81,7 +81,8 @@ void		graph_gtk_view_remove_node(GraphGtkView* self, GraphGtkNode* node);
 void		graph_gtk_view_remove_selected_nodes(GraphGtkView* self);
 void		graph_gtk_view_clear(GraphGtkView* self);
 GList*		graph_gtk_view_get_nodes(GraphGtkView* self);
-void graph_gtk_view_set_bg(GraphGtkView* self, cairo_surface_t* bg); //NULL means no background should be rendered
+void		graph_gtk_view_set_bg(GraphGtkView* self, cairo_surface_t* bg);	//NULL means no background should be rendered
+void		graph_gtk_view_arrange(GraphGtkView* self);	//attempts to auto-arrange nodes in a simple way
 
 G_END_DECLS
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]