[dia] Implement accessors for nth object/layer, more consistent API



commit e3235ae534cdd1a18e7ecddfdab4b826dfd21a04
Author: Hans Breuer <hans breuer org>
Date:   Mon Apr 20 19:54:54 2009 +0200

    Implement accessors for nth object/layer, more consistent API
    
    Rename layer_object_index() to layer_object_get_index() for
    symmetry with data_layer_get_index(). Implement data_layer_get_nth()
    as well as layer_object_get_nth(). Removed two bugs in comment.
---
 app/diagram.c                 |    4 ++--
 lib/diagramdata.c             |    7 +++++++
 lib/diagramdata.h             |    5 ++++-
 lib/layer.c                   |   21 ++++++++++++++++-----
 lib/libdia.def                |    3 ++-
 plug-ins/python/pydia-layer.c |    8 ++++----
 6 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/app/diagram.c b/app/diagram.c
index cf96a74..14e4b01 100644
--- a/app/diagram.c
+++ b/app/diagram.c
@@ -1299,8 +1299,8 @@ void diagram_ungroup_selected(Diagram *dia)
       group_list = group_objects(group);
       diagram_select_list(dia, group_list);
 
-      group_index = layer_object_index(dia->data->active_layer, group);
-    
+      group_index = layer_object_get_index(dia->data->active_layer, group);
+
       change = undo_ungroup_objects(dia, group_list, group, group_index);
       (change->apply)(change, dia);
 
diff --git a/lib/diagramdata.c b/lib/diagramdata.c
index 1fdce14..b022b14 100644
--- a/lib/diagramdata.c
+++ b/lib/diagramdata.c
@@ -329,6 +329,13 @@ data_layer_get_index (const DiagramData *data, const Layer *layer)
   }
   return -1;
 }
+Layer *
+data_layer_get_nth (const DiagramData *data, guint index)
+{
+  if (data->layers->len > index)
+    return g_ptr_array_index(data->layers, index);
+  return NULL;
+}
 
 /** Set which layer is the active layer in a diagram.
  * @param data The diagram in which to set the active layer.
diff --git a/lib/diagramdata.h b/lib/diagramdata.h
index b9e8cfa..ad37915 100644
--- a/lib/diagramdata.h
+++ b/lib/diagramdata.h
@@ -131,6 +131,7 @@ void data_add_layer_at(DiagramData *data, Layer *layer, int pos);
 void data_set_active_layer(DiagramData *data, Layer *layer);
 void data_delete_layer(DiagramData *data, Layer *layer);
 int  data_layer_get_index (const DiagramData *data, const Layer *layer);
+Layer *data_layer_get_nth (const DiagramData *data, guint index);
 
 void data_select(DiagramData *data, DiaObject *obj);
 void data_unselect(DiagramData *data, DiaObject *obj);
@@ -160,7 +161,8 @@ void layer_render(Layer *layer, DiaRenderer *renderer, Rectangle *update,
 		  gpointer data,
 		  int active_layer);
 
-int layer_object_index(Layer *layer, DiaObject *obj);
+int layer_object_get_index(Layer *layer, DiaObject *obj);
+DiaObject *layer_object_get_nth(Layer *layer, guint index);
 void layer_add_object(Layer *layer, DiaObject *obj);
 void layer_add_object_at(Layer *layer, DiaObject *obj, int pos);
 void layer_add_objects(Layer *layer, GList *obj_list);
@@ -182,6 +184,7 @@ void layer_replace_object_with_list(Layer *layer, DiaObject *obj,
 				    GList *list);
 void layer_set_object_list(Layer *layer, GList *list);
 DiagramData *layer_get_parent_diagram(Layer *layer);
+
 /* Make sure all objects that are in the layer and not in the new
    list eventually gets destroyed. */
 
diff --git a/lib/layer.c b/lib/layer.c
index 3041ef5..9a5feaa 100644
--- a/lib/layer.c
+++ b/lib/layer.c
@@ -113,17 +113,28 @@ set_parent_layer(gpointer element, gpointer user_data)
  * @param obj The object to look for.
  * @return The index of the object in the layers list of objects.  This is also
  *  the vertical position of the object.
- * @bug This should be in a separate layer.c file.
- * @bug The layer could be inferred from the object, in which case the
- *  layer arg is not needed, and we would be sure we always looked in the
- *  right layer.
  */
 int
-layer_object_index(Layer *layer, DiaObject *obj)
+layer_object_get_index(Layer *layer, DiaObject *obj)
 {
   return (int)g_list_index(layer->objects, (gpointer) obj);
 }
 
+/**
+ * Get the object a index or NULL
+ * @param layer The layer to query for the nth object
+ * @param index The zero-based indexed of the object
+ */
+DiaObject *
+layer_object_get_nth (Layer *layer, guint index)
+{
+  if (g_list_length(layer->objects) > index) {
+    g_assert(g_list_nth(layer->objects, index));
+    return (DiaObject *)g_list_nth(layer->objects, index)->data;
+  }
+  return NULL;
+}
+
 /** Add an object to the top of a layer.
  * @param layer The layer to add the object to.
  * @param obj The object to add.  This must not already be part of another
diff --git a/lib/libdia.def b/lib/libdia.def
index 3776a9d..d12ec4f 100644
--- a/lib/libdia.def
+++ b/lib/libdia.def
@@ -152,6 +152,7 @@ EXPORTS
  data_get_sorted_selected_remove
  data_int
  data_layer_get_index
+ data_layer_get_nth
  data_lower_layer
  data_next
  data_point
@@ -441,7 +442,7 @@ EXPORTS
  layer_find_objects_in_rectangle
  layer_find_objects_intersecting_rectangle
  layer_get_parent_diagram
- layer_object_index
+ layer_object_get_index
  layer_remove_object
  layer_remove_objects
  layer_render
diff --git a/plug-ins/python/pydia-layer.c b/plug-ins/python/pydia-layer.c
index ceaf940..245ad99 100644
--- a/plug-ins/python/pydia-layer.c
+++ b/plug-ins/python/pydia-layer.c
@@ -75,14 +75,14 @@ PyDiaLayer_Destroy(PyDiaLayer *self, PyObject *args)
 }
 
 static PyObject *
-PyDiaLayer_ObjectIndex(PyDiaLayer *self, PyObject *args)
+PyDiaLayer_ObjectGetIndex(PyDiaLayer *self, PyObject *args)
 {
     PyDiaObject *obj;
 
-    if (!PyArg_ParseTuple(args, "O!:Layer.object_index",
+    if (!PyArg_ParseTuple(args, "O!:Layer.object_get_index",
 			  &PyDiaObject_Type, &obj))
 	return NULL;
-    return PyInt_FromLong(layer_object_index(self->layer, obj->object));
+    return PyInt_FromLong(layer_object_get_index(self->layer, obj->object));
 }
 
 static PyObject *
@@ -195,7 +195,7 @@ PyDiaLayer_UpdateExtents(PyDiaLayer *self, PyObject *args)
 
 static PyMethodDef PyDiaLayer_Methods[] = {
     {"destroy", (PyCFunction)PyDiaLayer_Destroy, 1},
-    {"object_index", (PyCFunction)PyDiaLayer_ObjectIndex, 1},
+    {"object_get_index", (PyCFunction)PyDiaLayer_ObjectGetIndex, 1},
     {"add_object", (PyCFunction)PyDiaLayer_AddObject, 1},
     {"remove_object", (PyCFunction)PyDiaLayer_RemoveObject, 1},
     {"find_objects_in_rectangle",



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