Connect to the "data-changed" signal in LdDiagram.

Also rewrite some of the functions.
This commit is contained in:
Přemysl Eric Janouch 2011-01-29 15:19:52 +01:00
parent 400568ed7d
commit c07d5f552d
1 changed files with 96 additions and 70 deletions

View File

@ -49,6 +49,9 @@ static void ld_diagram_set_property (GObject *object, guint property_id,
static void ld_diagram_dispose (GObject *gobject); static void ld_diagram_dispose (GObject *gobject);
static void ld_diagram_finalize (GObject *gobject); static void ld_diagram_finalize (GObject *gobject);
static void on_object_data_changed (LdDiagramObject *self,
gchar **path, GValue *old_value, GValue *new_value, gpointer user_data);
static gboolean write_signature (GOutputStream *stream, GError **error); static gboolean write_signature (GOutputStream *stream, GError **error);
static gboolean check_node (JsonNode *node, JsonNodeType type, static gboolean check_node (JsonNode *node, JsonNodeType type,
@ -61,8 +64,9 @@ static JsonNode *serialize_diagram (LdDiagram *self);
static JsonNode *serialize_object (LdDiagramObject *object); static JsonNode *serialize_object (LdDiagramObject *object);
static const gchar *get_object_class_string (GType type); static const gchar *get_object_class_string (GType type);
static void install_object (LdDiagramObject *object, LdDiagram *self);
static void uninstall_object (LdDiagramObject *object, LdDiagram *self);
static void ld_diagram_real_changed (LdDiagram *self); static void ld_diagram_real_changed (LdDiagram *self);
static void ld_diagram_clear_internal (LdDiagram *self);
static void ld_diagram_unselect_all_internal (LdDiagram *self); static void ld_diagram_unselect_all_internal (LdDiagram *self);
@ -166,7 +170,7 @@ ld_diagram_dispose (GObject *gobject)
LdDiagram *self; LdDiagram *self;
self = LD_DIAGRAM (gobject); self = LD_DIAGRAM (gobject);
ld_diagram_clear_internal (self); ld_diagram_clear (self);
/* Chain up to the parent class. */ /* Chain up to the parent class. */
G_OBJECT_CLASS (ld_diagram_parent_class)->dispose (gobject); G_OBJECT_CLASS (ld_diagram_parent_class)->dispose (gobject);
@ -221,33 +225,37 @@ ld_diagram_new (void)
void void
ld_diagram_clear (LdDiagram *self) ld_diagram_clear (LdDiagram *self)
{ {
gboolean changed = FALSE;
gboolean selection_changed = FALSE;
g_return_if_fail (LD_IS_DIAGRAM (self)); g_return_if_fail (LD_IS_DIAGRAM (self));
ld_diagram_clear_internal (self); if (self->priv->selection)
g_signal_emit (self,
LD_DIAGRAM_GET_CLASS (self)->changed_signal, 0);
g_signal_emit (self,
LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0);
}
/*
* ld_diagram_clear_internal:
* @self: an #LdDiagram object.
*
* Do the same as ld_diagram_clear() does but don't emit signals.
*/
static void
ld_diagram_clear_internal (LdDiagram *self)
{ {
ld_diagram_unselect_all_internal (self); ld_diagram_unselect_all_internal (self);
selection_changed = TRUE;
}
if (self->priv->connections)
{
g_list_free (self->priv->connections); g_list_free (self->priv->connections);
self->priv->connections = NULL; self->priv->connections = NULL;
changed = TRUE;
g_list_foreach (self->priv->objects, (GFunc) g_object_unref, NULL); }
if (self->priv->objects)
{
g_list_foreach (self->priv->objects, (GFunc) uninstall_object, self);
g_list_free (self->priv->objects); g_list_free (self->priv->objects);
self->priv->objects = NULL; self->priv->objects = NULL;
changed = TRUE;
}
if (changed)
g_signal_emit (self,
LD_DIAGRAM_GET_CLASS (self)->changed_signal, 0);
if (selection_changed)
g_signal_emit (self,
LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0);
} }
/** /**
@ -530,6 +538,27 @@ ld_diagram_set_modified (LdDiagram *self, gboolean value)
g_object_notify (G_OBJECT (self), "modified"); g_object_notify (G_OBJECT (self), "modified");
} }
static void
on_object_data_changed (LdDiagramObject *self, gchar **path,
GValue *old_value, GValue *new_value, gpointer user_data)
{
}
static void
install_object (LdDiagramObject *object, LdDiagram *self)
{
g_signal_connect (object, "data-changed",
G_CALLBACK (on_object_data_changed), self);
g_object_ref (object);
}
static void
uninstall_object (LdDiagramObject *object, LdDiagram *self)
{
g_signal_handlers_disconnect_by_func (object, on_object_data_changed, self);
g_object_unref (object);
}
/** /**
* ld_diagram_get_objects: * ld_diagram_get_objects:
* @self: an #LdDiagram object. * @self: an #LdDiagram object.
@ -558,16 +587,15 @@ ld_diagram_insert_object (LdDiagram *self, LdDiagramObject *object, gint pos)
g_return_if_fail (LD_IS_DIAGRAM (self)); g_return_if_fail (LD_IS_DIAGRAM (self));
g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object)); g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object));
if (!g_list_find (self->priv->objects, object)) if (g_list_find (self->priv->objects, object))
{ return;
self->priv->objects =
g_list_insert (self->priv->objects, object, pos); self->priv->objects = g_list_insert (self->priv->objects, object, pos);
g_object_ref (object); install_object (object, self);
g_signal_emit (self, g_signal_emit (self,
LD_DIAGRAM_GET_CLASS (self)->changed_signal, 0); LD_DIAGRAM_GET_CLASS (self)->changed_signal, 0);
} }
}
/** /**
* ld_diagram_remove_object: * ld_diagram_remove_object:
@ -582,17 +610,17 @@ ld_diagram_remove_object (LdDiagram *self, LdDiagramObject *object)
g_return_if_fail (LD_IS_DIAGRAM (self)); g_return_if_fail (LD_IS_DIAGRAM (self));
g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object)); g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object));
if (g_list_find (self->priv->objects, object)) if (!g_list_find (self->priv->objects, object))
{ return;
ld_diagram_unselect (self, object); ld_diagram_unselect (self, object);
self->priv->objects = g_list_remove (self->priv->objects, object); self->priv->objects = g_list_remove (self->priv->objects, object);
g_object_unref (object); uninstall_object (object, self);
g_signal_emit (self, g_signal_emit (self,
LD_DIAGRAM_GET_CLASS (self)->changed_signal, 0); LD_DIAGRAM_GET_CLASS (self)->changed_signal, 0);
} }
}
/** /**
* ld_diagram_get_selection: * ld_diagram_get_selection:
@ -617,22 +645,22 @@ ld_diagram_get_selection (LdDiagram *self)
void void
ld_diagram_remove_selection (LdDiagram *self) ld_diagram_remove_selection (LdDiagram *self)
{ {
gboolean changed = FALSE; LdDiagramObject *object;
gboolean changed;
GList *iter; GList *iter;
g_return_if_fail (LD_IS_DIAGRAM (self)); g_return_if_fail (LD_IS_DIAGRAM (self));
for (iter = self->priv->selection; iter; iter = g_list_next (iter)) for (iter = self->priv->selection; iter; iter = g_list_next (iter))
{ {
LdDiagramObject *object;
changed = TRUE;
object = LD_DIAGRAM_OBJECT (iter->data); object = LD_DIAGRAM_OBJECT (iter->data);
g_object_unref (object);
self->priv->objects = g_list_remove (self->priv->objects, object); self->priv->objects = g_list_remove (self->priv->objects, object);
g_object_unref (object); uninstall_object (object, self);
g_object_unref (object);
} }
changed = self->priv->selection != NULL;
g_list_free (self->priv->selection); g_list_free (self->priv->selection);
self->priv->selection = NULL; self->priv->selection = NULL;
@ -657,19 +685,17 @@ ld_diagram_select (LdDiagram *self, LdDiagramObject *object)
{ {
g_return_if_fail (LD_IS_DIAGRAM (self)); g_return_if_fail (LD_IS_DIAGRAM (self));
g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object)); g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object));
g_return_if_fail (g_list_find (self->priv->objects, object) != NULL); g_return_if_fail (g_list_find (self->priv->objects, object) != NULL);
if (!g_list_find (self->priv->selection, object)) if (g_list_find (self->priv->selection, object))
{ return;
self->priv->selection =
g_list_insert (self->priv->selection, object, 0); self->priv->selection = g_list_insert (self->priv->selection, object, 0);
g_object_ref (object); g_object_ref (object);
g_signal_emit (self, g_signal_emit (self,
LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0); LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0);
} }
}
/** /**
* ld_diagram_unselect: * ld_diagram_unselect:
@ -684,15 +710,15 @@ ld_diagram_unselect (LdDiagram *self, LdDiagramObject *object)
g_return_if_fail (LD_IS_DIAGRAM (self)); g_return_if_fail (LD_IS_DIAGRAM (self));
g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object)); g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object));
if (g_list_find (self->priv->selection, object)) if (!g_list_find (self->priv->selection, object))
{ return;
self->priv->selection = g_list_remove (self->priv->selection, object); self->priv->selection = g_list_remove (self->priv->selection, object);
g_object_unref (object); g_object_unref (object);
g_signal_emit (self, g_signal_emit (self,
LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0); LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0);
} }
}
/** /**
* ld_diagram_select_all: * ld_diagram_select_all:
@ -706,7 +732,6 @@ ld_diagram_select_all (LdDiagram *self)
g_return_if_fail (LD_IS_DIAGRAM (self)); g_return_if_fail (LD_IS_DIAGRAM (self));
ld_diagram_unselect_all_internal (self); ld_diagram_unselect_all_internal (self);
self->priv->selection = g_list_copy (self->priv->objects); self->priv->selection = g_list_copy (self->priv->objects);
g_list_foreach (self->priv->selection, (GFunc) g_object_ref, NULL); g_list_foreach (self->priv->selection, (GFunc) g_object_ref, NULL);
@ -725,13 +750,14 @@ ld_diagram_unselect_all (LdDiagram *self)
{ {
g_return_if_fail (LD_IS_DIAGRAM (self)); g_return_if_fail (LD_IS_DIAGRAM (self));
if (self->priv->selection) if (!self->priv->selection)
{ return;
ld_diagram_unselect_all_internal (self); ld_diagram_unselect_all_internal (self);
g_signal_emit (self, g_signal_emit (self,
LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0); LD_DIAGRAM_GET_CLASS (self)->selection_changed_signal, 0);
} }
}
static void static void
ld_diagram_unselect_all_internal (LdDiagram *self) ld_diagram_unselect_all_internal (LdDiagram *self)