[frogr] Added support to reorder pictures in the model.



commit 54d5cbd67e97006492afb639b68613b5368cec46
Author: Mario Sanchez Prada <msanchez igalia com>
Date:   Sat Apr 23 18:38:29 2011 +0200

    Added support to reorder pictures in the model.
    
    Also, emit a signal indicating the new order.

 src/frogr-main-view-model.c |   45 +++++++++++++++++++++++++++++++++++++++++++
 src/frogr-main-view-model.h |    3 ++
 2 files changed, 48 insertions(+), 0 deletions(-)
---
diff --git a/src/frogr-main-view-model.c b/src/frogr-main-view-model.c
index 5d1a6a3..868ed0a 100644
--- a/src/frogr-main-view-model.c
+++ b/src/frogr-main-view-model.c
@@ -56,6 +56,7 @@ struct _FrogrMainViewModelPrivate
 enum {
   PICTURE_ADDED,
   PICTURE_REMOVED,
+  PICTURES_REORDERED,
   DESCRIPTION_UPDATED,
   N_SIGNALS
 };
@@ -132,6 +133,14 @@ frogr_main_view_model_class_init(FrogrMainViewModelClass *klass)
                   g_cclosure_marshal_VOID__OBJECT,
                   G_TYPE_NONE, 1, FROGR_TYPE_PICTURE);
 
+  signals[PICTURES_REORDERED] =
+    g_signal_new ("pictures-reordered",
+                  G_OBJECT_CLASS_TYPE (klass),
+                  G_SIGNAL_RUN_FIRST,
+                  0, NULL, NULL,
+                  g_cclosure_marshal_VOID__POINTER,
+                  G_TYPE_NONE, 1, G_TYPE_POINTER);
+
   signals[DESCRIPTION_UPDATED] =
     g_signal_new ("description-updated",
                   G_OBJECT_CLASS_TYPE (klass),
@@ -230,6 +239,42 @@ frogr_main_view_model_get_pictures (FrogrMainViewModel *self)
 }
 
 void
+frogr_main_view_model_reorder_pictures (FrogrMainViewModel *self,
+                                        GCompareFunc compare_func)
+{
+  g_return_if_fail(FROGR_IS_MAIN_VIEW_MODEL (self));
+
+  FrogrMainViewModelPrivate *priv = NULL;
+  GSList *old_list = NULL;
+  GSList *old_item = NULL;
+  gint *new_order = 0;
+  gint old_pos = 0;
+  gint new_pos = 0;
+
+  priv = FROGR_MAIN_VIEW_MODEL_GET_PRIVATE (self);
+
+  /* Temporarily save the current list, and alloc an array to
+     represent the new order compared to the old positions */
+  old_list = g_slist_copy (priv->pictures_list);
+  new_order = g_new0 (gint, g_slist_length (old_list));
+
+  priv->pictures_list = g_slist_sort (priv->pictures_list, compare_func);
+
+  /* Build the new_order array */
+  old_pos = 0;
+  for (old_item = old_list; old_item; old_item = g_slist_next (old_item))
+    {
+      new_pos = g_slist_index (priv->pictures_list, old_item->data);
+      new_order[new_pos] = old_pos++;
+    }
+
+  g_signal_emit (self, signals[PICTURES_REORDERED], 0, new_order);
+
+  g_slist_free (old_list);
+  g_free (new_order);
+}
+
+void
 frogr_main_view_model_add_set (FrogrMainViewModel *self,
                                FrogrPhotoSet *set)
 {
diff --git a/src/frogr-main-view-model.h b/src/frogr-main-view-model.h
index d1633c2..c63329a 100644
--- a/src/frogr-main-view-model.h
+++ b/src/frogr-main-view-model.h
@@ -66,6 +66,9 @@ guint frogr_main_view_model_n_pictures (FrogrMainViewModel *self);
 
 GSList *frogr_main_view_model_get_pictures (FrogrMainViewModel *self);
 
+void frogr_main_view_model_reorder_pictures (FrogrMainViewModel *self,
+                                             GCompareFunc compare_func);
+
 void frogr_main_view_model_add_set (FrogrMainViewModel *self,
                                     FrogrPhotoSet *fset);
 void frogr_main_view_model_remove_set (FrogrMainViewModel *self,



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