ges: Keep layers sorted by priorities in the timeline
authorThibault Saunier <thibault.saunier@collabora.com>
Fri, 3 Jun 2011 02:03:19 +0000 (22:03 -0400)
committerThibault Saunier <thibault.saunier@collabora.com>
Wed, 11 Jan 2012 14:56:14 +0000 (11:56 -0300)
ges/ges-timeline.c

index c04185a10d2b8a4b17100f930dff7a0e43ca84ca..ea883900160753ca062bf87fec45cf301bb9477e 100644 (file)
@@ -246,6 +246,26 @@ ges_timeline_init (GESTimeline * self)
   gst_discoverer_start (self->priv->discoverer);
 }
 
+static gint
+sort_layers (gpointer a, gpointer b)
+{
+  GESTimelineLayer *layer_a, *layer_b;
+  guint prio_a, prio_b;
+
+  layer_a = GES_TIMELINE_LAYER (a);
+  layer_b = GES_TIMELINE_LAYER (b);
+
+  prio_a = ges_timeline_layer_get_priority (layer_a);
+  prio_b = ges_timeline_layer_get_priority (layer_b);
+
+  if ((gint) prio_a > (guint) prio_b)
+    return 1;
+  if ((guint) prio_a < (guint) prio_b)
+    return -1;
+
+  return 0;
+}
+
 /**
  * ges_timeline_new:
  *
@@ -613,6 +633,13 @@ layer_object_added_cb (GESTimelineLayer * layer, GESTimelineObject * object,
   GST_DEBUG ("done");
 }
 
+static void
+layer_priority_changed_cb (GESTimelineLayer * layer,
+    GParamSpec * arg G_GNUC_UNUSED, GESTimeline * timeline)
+{
+  timeline->priv->layers = g_list_sort (timeline->priv->layers, (GCompareFunc)
+      sort_layers);
+}
 
 static void
 layer_object_removed_cb (GESTimelineLayer * layer, GESTimelineObject * object,
@@ -688,7 +715,8 @@ ges_timeline_add_layer (GESTimeline * timeline, GESTimelineLayer * layer)
   }
 
   g_object_ref_sink (layer);
-  priv->layers = g_list_append (priv->layers, layer);
+  priv->layers = g_list_insert_sorted (priv->layers, layer,
+      (GCompareFunc) sort_layers);
 
   /* Inform the layer that it belongs to a new timeline */
   ges_timeline_layer_set_timeline (layer, timeline);
@@ -698,6 +726,8 @@ ges_timeline_add_layer (GESTimeline * timeline, GESTimelineLayer * layer)
       timeline);
   g_signal_connect (layer, "object-removed",
       G_CALLBACK (layer_object_removed_cb), timeline);
+  g_signal_connect (layer, "notify::priority",
+      G_CALLBACK (layer_priority_changed_cb), timeline);
 
   GST_DEBUG ("Done adding layer, emitting 'layer-added' signal");
   g_signal_emit (timeline, ges_timeline_signals[LAYER_ADDED], 0, layer);
@@ -1022,7 +1052,7 @@ ges_timeline_get_tracks (GESTimeline * timeline)
  * Get the list of #GESTimelineLayer present in the Timeline.
  *
  * Returns: (transfer full) (element-type GESTimelineLayer): the list of
- * #GESTimelineLayer present in the Timeline.
+ * #GESTimelineLayer present in the Timeline sorted by priority.
  * The caller should unref each Layer once he is done with them.
  */
 GList *
@@ -1031,7 +1061,8 @@ ges_timeline_get_layers (GESTimeline * timeline)
   GList *tmp, *res = NULL;
 
   for (tmp = timeline->priv->layers; tmp; tmp = g_list_next (tmp)) {
-    res = g_list_append (res, g_object_ref (tmp->data));
+    res = g_list_insert_sorted (res, g_object_ref (tmp->data),
+        (GCompareFunc) sort_layers);
   }
 
   return res;