[gegl-edit] Fixed loading and added in/out icons to indicate if a node is a pad proxy



commit 1e7b688d7c1feefcd01c9a6e26b21e1b95edb5a9
Author: Isaac Wagner <isaacbw src gnome org>
Date:   Wed Aug 8 21:17:09 2012 -0400

    Fixed loading and added in/out icons to indicate if a node is a pad proxy

 gegl-edit/Makefile.am   |    2 +-
 gegl-edit/data/in.png   |  Bin 0 -> 256 bytes
 gegl-edit/data/out.png  |  Bin 0 -> 317 bytes
 gegl-edit/gegl-edit.c   |  185 +++++++++++++++++++++++++++++++++++++----------
 gegl-edit/gresource.xml |    2 +
 5 files changed, 151 insertions(+), 38 deletions(-)
---
diff --git a/gegl-edit/Makefile.am b/gegl-edit/Makefile.am
index 0cc8073..3ee5aff 100644
--- a/gegl-edit/Makefile.am
+++ b/gegl-edit/Makefile.am
@@ -4,7 +4,7 @@ LIBS = $(GTK_LIBS) $(GRAPH_GTK_LIBS) $(GEGL_LIBS)
 resources.h: gresource.xml
 	glib-compile-resources --target=$@ --generate-header gresource.xml
 
-resources.c: gresource.xml menubar.ui resources.h
+resources.c: gresource.xml menubar.ui resources.h data/in.png data/out.png
 	glib-compile-resources --target=$@ --generate-source gresource.xml
 
 bin_PROGRAMS = gegl-edit
diff --git a/gegl-edit/data/in.png b/gegl-edit/data/in.png
new file mode 100644
index 0000000..e2bc05c
Binary files /dev/null and b/gegl-edit/data/in.png differ
diff --git a/gegl-edit/data/out.png b/gegl-edit/data/out.png
new file mode 100644
index 0000000..8607031
Binary files /dev/null and b/gegl-edit/data/out.png differ
diff --git a/gegl-edit/gegl-edit.c b/gegl-edit/gegl-edit.c
index 9977c8f..4fdcd8f 100644
--- a/gegl-edit/gegl-edit.c
+++ b/gegl-edit/gegl-edit.c
@@ -8,7 +8,7 @@
 
 #include "resources.h"
 
-typedef struct CallbackData
+typedef struct
 {
   GtkWidget	*window;
   GraphGtkNode	*context;
@@ -19,6 +19,8 @@ typedef struct CallbackData
   GtkMenu       *popup;
   GtkMenu       *popup2;
   const gchar *filename;
+  cairo_surface_t *in;
+  cairo_surface_t *out;
 } CallbackData;
 
 static const gchar* query_proxy(GeglGtkPropertyView *view, GeglNode *node, const gchar *property, CallbackData* data);
@@ -30,8 +32,8 @@ static void node_rightclicked(GraphGtkView *view, GraphGtkNode *node, gpointer d
 static void nodes_connected(GraphGtkView *view, GraphGtkNode *from, const gchar* output, GraphGtkNode *to, const gchar* input, gpointer data);
 static void nodes_disconnected(GraphGtkView *view, GraphGtkNode *from, const gchar* output, GraphGtkNode *to, const gchar* input, gpointer data);
 
-static GraphGtkNode* add_gegl_node_to_view(GraphGtkView *view, GeglNode *node);
-static void load_graph(GraphGtkView *view, GeglNode *node);
+static GraphGtkNode* add_gegl_node_to_view(GraphGtkView *view, GeglNode *node, CallbackData *data);
+static void load_graph(GraphGtkView *view, GeglNode *node, CallbackData *data);
 
 static void update_images(GraphGtkView *view);
 static void update_bg_image(GraphGtkView *view);
@@ -43,7 +45,9 @@ GSList		*gegl_node_get_input_pads(GeglNode*);
 GSList		*gegl_node_get_pads(GeglNode *self);
 const gchar	*gegl_pad_get_name(gpointer);
 GeglNode	*gegl_node_get_nth_child (GeglNode*,gint);
-
+const gchar     *gegl_node_get_name (GeglNode *self);
+gboolean         gegl_pad_is_output(gpointer *self);
+gboolean         gegl_pad_is_input(gpointer *self);
 
 static void
 add_ui_to_builder(GResource *resource, GtkBuilder *builder, const gchar* resource_name, CallbackData *data)
@@ -56,6 +60,23 @@ add_ui_to_builder(GResource *resource, GtkBuilder *builder, const gchar* resourc
   g_bytes_unref(ui_bytes);
 }
 
+struct closure
+{
+  GBytes *bytes;
+  unsigned int offset;
+};
+
+static cairo_status_t cairo_read_bytes(struct closure *closure, unsigned char *dest, unsigned int length)
+{
+  gsize size = g_bytes_get_size(closure->bytes);
+  const gchar *data = g_bytes_get_data(closure->bytes, &size);
+
+  memcpy(dest, data+closure->offset, length);
+  closure->offset += length;
+
+  return CAIRO_STATUS_SUCCESS;
+}
+
 static void property_changed(GeglGtkPropertyView *view, gpointer user_data)
 {
   update_images(GRAPH_GTK_VIEW(user_data));
@@ -68,9 +89,9 @@ main (gint	  argc,
   gtk_init(&argc, &argv);
   gegl_init(&argc, &argv);
 
-
   GeglNode *gegl = gegl_node_new();
   GResource *resource = gresource_get_resource();
+
   GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   GtkWidget *view = graph_gtk_view_new();
   GtkWidget *props = gegl_gtk_property_view_new(NULL);
@@ -84,6 +105,19 @@ main (gint	  argc,
   g_queue_push_head(data->graph_stack, gegl);
   data->property_view = GTK_WIDGET(props);
 
+  //Load images from GResource
+  GBytes *in_bytes = g_resource_lookup_data(resource, "/gegl-edit/data/in.png", 0, NULL);
+  struct closure closure;
+  closure.bytes = in_bytes;
+  closure.offset = 0;
+  data->in = cairo_image_surface_create_from_png_stream( (cairo_read_func_t) cairo_read_bytes, &closure);
+
+  in_bytes = g_resource_lookup_data(resource, "/gegl-edit/data/out.png", 0, NULL);
+  closure.bytes = in_bytes;
+  closure.offset = 0;
+  data->out = cairo_image_surface_create_from_png_stream( (cairo_read_func_t) cairo_read_bytes, &closure);
+  //done loading images
+
   g_signal_connect(props, "property-changed", G_CALLBACK(property_changed), view);
   g_signal_connect(props, "query-proxy", G_CALLBACK(query_proxy), data);
 
@@ -136,6 +170,52 @@ main (gint	  argc,
 }
 
 ////////GraphGtk callbacks////////
+static void post_render(GraphGtkNode *node, cairo_t *cr, CallbackData *data)
+{
+  GeglNode *gegl = g_queue_peek_head(data->graph_stack);
+  GList *input_proxies = NULL;
+  GList *output_proxies = NULL;
+
+
+  //Note: gegl:nop has two pads but both lead to the same proxy. This is the only case in which is_input and is_output would both be true
+  GSList *pads;
+  gboolean is_input = FALSE;
+  gboolean is_output = FALSE;
+  for(pads = gegl_node_get_pads(gegl); pads != NULL; pads = pads->next)
+    {
+      if(gegl_pad_is_input(pads->data))
+	{
+	  GeglNode *proxy = gegl_node_get_input_proxy(gegl, gegl_pad_get_name(pads->data));
+	  if(node->user_data == proxy)
+	    is_input = TRUE;
+	}
+      else if(gegl_pad_is_output(pads->data))
+	{
+	  GeglNode *proxy = gegl_node_get_output_proxy(gegl, gegl_pad_get_name(pads->data));
+	  if(node->user_data == proxy)
+	    is_output = TRUE;
+	}
+    }
+
+  if (is_output && is_input)
+    {
+      cairo_set_source_surface(cr, data->out, node->x+node->offset_x+cairo_image_surface_get_width(data->in), node->y+node->offset_y+node->height-cairo_image_surface_get_height(data->out));
+      cairo_paint(cr);
+      cairo_set_source_surface(cr, data->in, node->x+node->offset_x, node->y+node->offset_y+node->height-cairo_image_surface_get_height(data->in));
+      cairo_paint(cr);
+    }
+  else if (is_output)
+    {
+      cairo_set_source_surface(cr, data->out, node->x+node->offset_x, node->y+node->offset_y+node->height-cairo_image_surface_get_height(data->out));
+      cairo_paint(cr);
+    }
+  else if (is_input)
+    {
+      cairo_set_source_surface(cr, data->in, node->x+node->offset_x, node->y+node->offset_y+node->height-cairo_image_surface_get_height(data->in));
+      cairo_paint(cr);
+    }
+}
+
 static const gchar* query_proxy(GeglGtkPropertyView *view, GeglNode *node, const gchar *property, CallbackData* data)
 {
   /*  GeglNode *graph = g_queue_peek_head(data->graph_stack);
@@ -310,7 +390,7 @@ G_MODULE_EXPORT void activated_context_edit(GtkMenuItem *menuitem, gpointer user
   if(data->context)
     {
       GtkWidget *view = graph_gtk_view_new();
-      load_graph(GRAPH_GTK_VIEW(view), data->context->user_data);
+      load_graph(GRAPH_GTK_VIEW(view), data->context->user_data, data);
 
       g_object_ref(g_queue_peek_head(data->view_stack));
       gtk_container_remove(GTK_CONTAINER(data->view_box), g_queue_peek_head(data->view_stack));
@@ -364,6 +444,8 @@ G_MODULE_EXPORT void activated_add_subgraph(GtkMenuItem *menuitem, gpointer user
     {
       const gchar *name = gtk_entry_get_text(GTK_ENTRY(name_entry));
       GraphGtkNode *node = graph_gtk_node_new();
+      g_signal_connect(node, "post-render", (GCallback)post_render, data);
+
       graph_gtk_node_set_name(node, name);
       GeglNode *subgraph = gegl_node_new();
       node->user_data = subgraph;
@@ -390,6 +472,7 @@ G_MODULE_EXPORT void activated_add_input(GtkMenuItem *menuitem, gpointer user_da
 
   GtkWidget* name_label = gtk_label_new("Name");
   GtkWidget* name_entry = gtk_entry_new();
+  gtk_entry_set_text(GTK_ENTRY(name_entry), "input");
   gtk_box_pack_start(GTK_BOX(vbox), name_label, FALSE, FALSE, 0);
   gtk_box_pack_start(GTK_BOX(vbox), name_entry, FALSE, FALSE, 0);
 
@@ -401,16 +484,13 @@ G_MODULE_EXPORT void activated_add_input(GtkMenuItem *menuitem, gpointer user_da
     {
       const gchar *name = gtk_entry_get_text(GTK_ENTRY(name_entry));
       GraphGtkNode *node = graph_gtk_node_new();
-
-      GString *str = g_string_new("input-proxy: ");
-      str = g_string_append(str, name);
-      graph_gtk_node_set_name(node, str->str);
-      g_string_free(str, TRUE);
+      g_signal_connect(node, "post-render", (GCallback)post_render, data);
 
       graph_gtk_node_add_pad(node, "output", TRUE);
       graph_gtk_view_add_node(g_queue_peek_head(data->view_stack), node);
       GeglNode *proxy = gegl_node_get_input_proxy(g_queue_peek_head(data->graph_stack), name);
       node->user_data = proxy;
+      graph_gtk_node_set_name(node, gegl_node_get_name (proxy));
     }
 
   gtk_widget_destroy(dialog);
@@ -433,6 +513,7 @@ G_MODULE_EXPORT void activated_add_output(GtkMenuItem *menuitem, gpointer user_d
 
   GtkWidget* name_label = gtk_label_new("Name");
   GtkWidget* name_entry = gtk_entry_new();
+  gtk_entry_set_text(GTK_ENTRY(name_entry), "output");
   gtk_box_pack_start(GTK_BOX(vbox), name_label, FALSE, FALSE, 0);
   gtk_box_pack_start(GTK_BOX(vbox), name_entry, FALSE, FALSE, 0);
 
@@ -444,16 +525,13 @@ G_MODULE_EXPORT void activated_add_output(GtkMenuItem *menuitem, gpointer user_d
     {
       const gchar *name = gtk_entry_get_text(GTK_ENTRY(name_entry));
       GraphGtkNode *node = graph_gtk_node_new();
-
-      GString *str = g_string_new("output-proxy: ");
-      str = g_string_append(str, name);
-      graph_gtk_node_set_name(node, str->str);
-      g_string_free(str, TRUE);
+      g_signal_connect(node, "post-render", (GCallback)post_render, data);
 
       graph_gtk_node_add_pad(node, "input", FALSE);
       graph_gtk_view_add_node(g_queue_peek_head(data->view_stack), node);
       GeglNode *proxy = gegl_node_get_output_proxy(g_queue_peek_head(data->graph_stack), name);
       node->user_data = proxy;
+      graph_gtk_node_set_name(node, gegl_node_get_name (proxy));
     }
 
   gtk_widget_destroy(dialog);
@@ -476,7 +554,23 @@ G_MODULE_EXPORT void activated_open(GtkMenuItem *menuitem, gpointer user_data)
       data->filename = filename;
       gtk_window_set_title(GTK_WINDOW(data->window), filename);
 
-      GeglNode* gegl = gegl_node_new_from_file(filename);
+      GeglNode *proxy = gegl_node_new_from_file(filename);
+      gchar *pad;
+      GeglNode *consumer = gegl_node_get_producer(proxy, "input", &pad);
+      g_print("Consumer: %s\n", gegl_node_get_operation(consumer));
+      GeglNode* gegl = gegl_node_new();
+
+      GSList *children;
+      for(children = gegl_node_get_children(proxy); children != NULL; children = children->next)
+	{
+	  GeglNode *child = children->data;
+	  g_object_ref(child);
+	  gegl_node_remove_child(proxy, child);
+	  gegl_node_add_child(gegl, child);
+	  g_object_unref(child);
+	}
+
+      gegl_node_connect_to(consumer, pad, gegl_node_get_output_proxy(gegl, "output"), "input");
 
       while(g_queue_get_length(data->graph_stack) > 0)
 	{
@@ -491,7 +585,7 @@ G_MODULE_EXPORT void activated_open(GtkMenuItem *menuitem, gpointer user_data)
       GtkWidget *view = graph_gtk_view_new();
       gtk_box_pack_start(GTK_BOX(data->view_box), view, TRUE, TRUE, 0);
       gtk_widget_show(view);
-      load_graph(GRAPH_GTK_VIEW(view), gegl);
+      load_graph(GRAPH_GTK_VIEW(view), gegl, data);
 
       g_queue_push_head(data->view_stack, view);
       g_queue_push_head(data->graph_stack, gegl);
@@ -626,7 +720,7 @@ G_MODULE_EXPORT void activated_new(GtkMenuItem *menuitem, gpointer user_data)
   GtkWidget *view = graph_gtk_view_new();
   gtk_box_pack_start(GTK_BOX(data->view_box), view, TRUE, TRUE, 0);
   gtk_widget_show(view);
-  load_graph(GRAPH_GTK_VIEW(view), gegl);
+  load_graph(GRAPH_GTK_VIEW(view), gegl, data);
 
   g_queue_push_head(data->view_stack, view);
   g_queue_push_head(data->graph_stack, gegl);
@@ -793,7 +887,7 @@ G_MODULE_EXPORT void activated_add(GtkMenuItem *menuitem, gpointer user_data)
 	  gtk_tree_model_get(model, &itr, 0, &operation, -1);
 	  GeglNode *node = gegl_node_create_child(g_queue_peek_head(data->graph_stack), operation);
 
-	  add_gegl_node_to_view(g_queue_peek_head(data->view_stack), node);
+	  add_gegl_node_to_view(g_queue_peek_head(data->view_stack), node, data);
 	}
     }
 
@@ -900,9 +994,10 @@ static void update_images(GraphGtkView *view)
 }
 
 static GraphGtkNode*
-add_gegl_node_to_view(GraphGtkView *view, GeglNode *node)
+add_gegl_node_to_view(GraphGtkView *view, GeglNode *node, CallbackData *data)
 {
   GraphGtkNode *view_node = graph_gtk_node_new();
+  g_signal_connect(view_node, "post-render", (GCallback)post_render, data);
   graph_gtk_node_set_name(view_node, gegl_node_get_operation(node));
   view_node->user_data = node;
 
@@ -920,49 +1015,64 @@ add_gegl_node_to_view(GraphGtkView *view, GeglNode *node)
   return view_node;
 }
 
-static void load_graph(GraphGtkView *view, GeglNode *gegl)
+static void load_graph(GraphGtkView *view, GeglNode *gegl, CallbackData *data)
 {
   GHashTable *hash_table = g_hash_table_new(g_direct_hash, g_direct_equal);
 
+  GList *nodes = NULL;
+
   GSList *list = gegl_node_get_children(gegl);
   for(;list != NULL; list = list->next)
     {
       GeglNode* node = GEGL_NODE(list->data);
       g_print("Loading %s\n", gegl_node_get_operation(node));
-      GraphGtkNode *view_node = add_gegl_node_to_view(view, node);
+      GraphGtkNode *view_node = add_gegl_node_to_view(view, node, data);
+      const gchar *name = gegl_node_get_name(node);
+      if(strcmp(name, "")) //!= ""
+	{
+	  graph_gtk_node_set_name(view_node,  name);
+	}
+
       g_hash_table_insert(hash_table, node, view_node);
+      nodes = g_list_append(nodes, node);
     }
 
+  /*
   GSList *pads;
   for(pads = gegl_node_get_input_pads(gegl); pads != NULL; pads = pads->next)
     {
       GeglNode *node = gegl_node_get_input_proxy(gegl, gegl_pad_get_name(pads->data));
-      GraphGtkNode *view_node = add_gegl_node_to_view(view, node);
+      if(!g_hash_table_lookup(hash_table, node))
+	{
+	  GraphGtkNode *view_node = add_gegl_node_to_view(view, node, data);
 
-      GString *str = g_string_new("input-proxy: ");
-      str = g_string_append(str, gegl_pad_get_name(pads->data));
-      graph_gtk_node_set_name(view_node, str->str);
-      g_string_free(str, TRUE);
+	  graph_gtk_node_set_name(view_node,  gegl_node_get_name (node));
 
-      g_hash_table_insert(hash_table, node, view_node);
+	  g_hash_table_insert(hash_table, node, view_node);
+	  nodes = g_list_append(nodes, node);
+	}
     }
 
   if(gegl_node_get_pad(gegl, "output"))
     {
       GeglNode *node = gegl_node_get_output_proxy(gegl, "output");
-      GraphGtkNode *view_node = add_gegl_node_to_view(view, node);
+      if(!g_hash_table_lookup(hash_table, node))
+	{
+	  GraphGtkNode *view_node = add_gegl_node_to_view(view, node, data);
 
-      GString *str = g_string_new("output-proxy: ");
-      str = g_string_append(str, "output");
-      graph_gtk_node_set_name(view_node, str->str);
-      g_string_free(str, TRUE);
+	  graph_gtk_node_set_name(view_node, gegl_node_get_name (node));
 
-      g_hash_table_insert(hash_table, node, view_node);
+	  g_hash_table_insert(hash_table, node, view_node);
+	  nodes = g_list_append(nodes, node);
+	}
     }
+  */
 
-  for(list = gegl_node_get_children(gegl); list != NULL; list = list->next)
+  //connect nodes
+  GList *itr;
+  for(itr = nodes; itr != NULL; itr = itr->next)
     {
-      GeglNode* node = GEGL_NODE(list->data);
+      GeglNode* node = GEGL_NODE(itr->data);
 
       GraphGtkNode *from = g_hash_table_lookup(hash_table, node);
 
@@ -988,6 +1098,7 @@ static void load_graph(GraphGtkView *view, GeglNode *gegl)
     }
 
   g_hash_table_destroy(hash_table);
+  g_list_free(nodes);
 
   graph_gtk_view_arrange(view);
 }
diff --git a/gegl-edit/gresource.xml b/gegl-edit/gresource.xml
index 5e7e45e..8204035 100644
--- a/gegl-edit/gresource.xml
+++ b/gegl-edit/gresource.xml
@@ -4,5 +4,7 @@
     <file preprocess="xml-stripblanks">menubar.ui</file>
     <file preprocess="xml-stripblanks">contextmenu.ui</file>
     <file preprocess="xml-stripblanks">contextmenu_canvas.ui</file>
+    <file>data/in.png</file>
+    <file>data/out.png</file>
   </gresource>
 </gresources>



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