[gnome-todo/wip/gbsneto/subtasks: 9/11] task: add ::depth property and API



commit af558ceb43e68d5884dc0a8f518c12a5f36276af
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Mon Oct 10 21:20:43 2016 -0300

    task: add ::depth property and API
    
    This property makes GtdTask track its own depth in the
    subtask tree.

 src/gtd-task-row.c |   26 +++++---------------
 src/gtd-task.c     |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gtd-task.h     |    2 +
 3 files changed, 75 insertions(+), 19 deletions(-)
---
diff --git a/src/gtd-task-row.c b/src/gtd-task-row.c
index 8d0c9ed..396a344 100644
--- a/src/gtd-task-row.c
+++ b/src/gtd-task-row.c
@@ -263,23 +263,11 @@ complete_changed_cb (GtdTaskRow *self,
 }
 
 static void
-parent_changed_cb (GtdTaskRow *self,
-                   GParamSpec *pspec,
-                   GtdTask    *task)
+depth_changed_cb (GtdTaskRow *self,
+                  GParamSpec *pspec,
+                  GtdTask    *task)
 {
-  GtdTask *aux;
-  guint depth;
-
-  depth = 0;
-  aux = gtd_task_get_parent (task);
-
-  while (aux)
-    {
-      aux = gtd_task_get_parent (aux);
-      depth++;
-    }
-
-  gtk_widget_set_margin_start (self->dnd_box, 48 * depth);
+  gtk_widget_set_margin_start (self->dnd_box, 32 * gtd_task_get_depth (task));
 }
 
 static gboolean
@@ -899,10 +887,10 @@ gtd_task_row_set_task (GtdTaskRow *row,
                                     G_CALLBACK (complete_changed_cb),
                                     row);
 
-          parent_changed_cb (row, NULL, task);
+          depth_changed_cb (row, NULL, task);
           g_signal_connect_swapped (task,
-                                    "notify::parent",
-                                    G_CALLBACK (parent_changed_cb),
+                                    "notify::depth",
+                                    G_CALLBACK (depth_changed_cb),
                                     row);
         }
 
diff --git a/src/gtd-task.c b/src/gtd-task.c
index 02dbccd..a177ae2 100644
--- a/src/gtd-task.c
+++ b/src/gtd-task.c
@@ -42,6 +42,7 @@ typedef struct
   ECalComponent   *component;
   GtdTask         *parent;
   GList           *subtasks;
+  gint             depth;
 } GtdTaskPrivate;
 
 G_DEFINE_TYPE_WITH_PRIVATE (GtdTask, gtd_task, GTD_TYPE_OBJECT)
@@ -51,6 +52,7 @@ enum
   PROP_0,
   PROP_COMPLETE,
   PROP_COMPONENT,
+  PROP_DEPTH,
   PROP_DESCRIPTION,
   PROP_CREATION_DATE,
   PROP_DUE_DATE,
@@ -89,6 +91,20 @@ gtd_task__convert_icaltime (const icaltimetype *date)
 }
 
 static void
+set_depth (GtdTask *self,
+           gint     depth)
+{
+  GtdTaskPrivate *priv = gtd_task_get_instance_private (self);
+  GList *l;
+
+  priv->depth = depth;
+  g_object_notify (G_OBJECT (self), "depth");
+
+  for (l = priv->subtasks; l != NULL; l = l->next)
+    set_depth (l->data, depth + 1);
+}
+
+static void
 real_add_subtask (GtdTask *self,
                   GtdTask *subtask)
 {
@@ -107,6 +123,10 @@ real_add_subtask (GtdTask *self,
   subtask_priv = gtd_task_get_instance_private (subtask);
   comp = subtask_priv->component;
 
+  /* First, remove the subtask from it's parent's subtasks list */
+  if (subtask_priv->parent)
+    gtd_task_remove_subtask (subtask_priv->parent, subtask);
+
   ical_comp = e_cal_component_get_icalcomponent (comp);
   property = icalcomponent_get_first_property (ical_comp, ICAL_RELATEDTO_PROPERTY);
 
@@ -122,6 +142,9 @@ real_add_subtask (GtdTask *self,
   subtask_priv->parent = self;
   g_object_notify (G_OBJECT (subtask), "parent");
 
+  /* And also the task's depth */
+  set_depth (subtask, priv->depth + 1);
+
   e_cal_component_free_id (id);
 }
 
@@ -155,6 +178,9 @@ real_remove_subtask (GtdTask *self,
   /* Update the subtask's parent property */
   subtask_priv->parent = NULL;
   g_object_notify (G_OBJECT (subtask), "parent");
+
+  /* And also the task's depth */
+  set_depth (subtask, 0);
 }
 
 static void
@@ -230,6 +256,10 @@ gtd_task_get_property (GObject    *object,
       g_value_set_boxed (value, gtd_task_get_creation_date (self));
       break;
 
+    case PROP_DEPTH:
+      g_value_set_uint (value, priv->depth);
+      break;
+
     case PROP_DESCRIPTION:
       g_value_set_string (value, gtd_task_get_description (self));
       break;
@@ -374,6 +404,22 @@ gtd_task_class_init (GtdTaskClass *klass)
                             G_PARAM_READABLE));
 
   /**
+   * GtdTask::depth:
+   *
+   * The depth of the task inside the subtask tree.
+   */
+  g_object_class_install_property (
+        object_class,
+        PROP_DEPTH,
+        g_param_spec_uint ("depth",
+                           "Depth of the task",
+                           "The depth of the task inside the subtasks tree.",
+                           0,
+                           G_MAXUINT,
+                           0,
+                           G_PARAM_READABLE));
+
+  /**
    * GtdTask::description:
    *
    * Description of the task.
@@ -1333,3 +1379,23 @@ gtd_task_is_subtask (GtdTask *self,
 
   return is_subtask;
 }
+
+/**
+ * gtd_task_get_depth:
+ * @self: a #GtdTask
+ *
+ * Retrieves the depth of @self in the subtasks tree.
+ *
+ * Returns: the depth of the task.
+ */
+gint
+gtd_task_get_depth (GtdTask *self)
+{
+  GtdTaskPrivate *priv;
+
+  g_return_val_if_fail (GTD_IS_TASK (self), 0);
+
+  priv = gtd_task_get_instance_private (self);
+
+  return priv->depth;
+}
diff --git a/src/gtd-task.h b/src/gtd-task.h
index daf3c70..3ed4121 100644
--- a/src/gtd-task.h
+++ b/src/gtd-task.h
@@ -100,6 +100,8 @@ void                gtd_task_remove_subtask           (GtdTask              *sel
 gboolean            gtd_task_is_subtask               (GtdTask              *self,
                                                        GtdTask              *subtask);
 
+gint                gtd_task_get_depth                (GtdTask              *self);
+
 G_END_DECLS
 
 #endif /* GTD_TASK_H */


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