Use JsonObject for LdDiagramObject parameters.

Make LdDiagramObject a regular class.
This commit is contained in:
Přemysl Eric Janouch 2011-01-15 15:31:01 +01:00
parent 9c5ade156a
commit 1201caf8a4
5 changed files with 212 additions and 70 deletions

View File

@ -2,12 +2,14 @@
* ld-diagram-object.c
*
* This file is a part of logdiag.
* Copyright Přemysl Janouch 2010. All rights reserved.
* Copyright Přemysl Janouch 2010 - 2011. All rights reserved.
*
* See the file LICENSE for licensing information.
*
*/
#include <string.h>
#include "liblogdiag.h"
#include "config.h"
@ -22,18 +24,17 @@
/*
* LdDiagramObjectPrivate:
* @x: The X coordinate of this object.
* @y: The Y coordinate of this object.
* @storage: Storage for object parameters.
*/
struct _LdDiagramObjectPrivate
{
gdouble x;
gdouble y;
JsonObject *storage;
};
enum
{
PROP_0,
PROP_STORAGE,
PROP_X,
PROP_Y
};
@ -42,9 +43,10 @@ static void ld_diagram_object_get_property (GObject *object, guint property_id,
GValue *value, GParamSpec *pspec);
static void ld_diagram_object_set_property (GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec);
static void ld_diagram_object_dispose (GObject *gobject);
G_DEFINE_ABSTRACT_TYPE (LdDiagramObject, ld_diagram_object, G_TYPE_OBJECT);
G_DEFINE_TYPE (LdDiagramObject, ld_diagram_object, G_TYPE_OBJECT);
static void
ld_diagram_object_class_init (LdDiagramObjectClass *klass)
@ -55,6 +57,17 @@ ld_diagram_object_class_init (LdDiagramObjectClass *klass)
object_class = G_OBJECT_CLASS (klass);
object_class->get_property = ld_diagram_object_get_property;
object_class->set_property = ld_diagram_object_set_property;
object_class->dispose = ld_diagram_object_dispose;
/**
* LdDiagramObject:storage:
*
* Storage for object parameters.
*/
pspec = g_param_spec_boxed ("storage", "Storage",
"Storage for object parameters.",
JSON_TYPE_OBJECT, G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_STORAGE, pspec);
/**
* LdDiagramObject:x:
@ -95,11 +108,12 @@ ld_diagram_object_get_property (GObject *object, guint property_id,
self = LD_DIAGRAM_OBJECT (object);
switch (property_id)
{
case PROP_X:
g_value_set_double (value, ld_diagram_object_get_x (self));
case PROP_STORAGE:
g_value_set_boxed (value, ld_diagram_object_get_storage (self));
break;
case PROP_X:
case PROP_Y:
g_value_set_double (value, ld_diagram_object_get_y (self));
ld_diagram_object_get_data (self, value, pspec);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@ -115,17 +129,166 @@ ld_diagram_object_set_property (GObject *object, guint property_id,
self = LD_DIAGRAM_OBJECT (object);
switch (property_id)
{
case PROP_X:
ld_diagram_object_set_x (self, g_value_get_double (value));
case PROP_STORAGE:
ld_diagram_object_set_storage (self, g_value_get_boxed (value));
break;
case PROP_X:
case PROP_Y:
ld_diagram_object_set_y (self, g_value_get_double (value));
ld_diagram_object_set_data (self, value, pspec);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
ld_diagram_object_dispose (GObject *gobject)
{
LdDiagramObject *self;
self = LD_DIAGRAM_OBJECT (gobject);
if (self->priv->storage)
{
json_object_unref (self->priv->storage);
self->priv->storage = NULL;
}
/* Chain up to the parent class. */
G_OBJECT_CLASS (ld_diagram_object_parent_class)->dispose (gobject);
}
/**
* ld_diagram_object_new:
* @storage: A storage backend.
*
* Return value: A new #LdDiagramObject object.
*/
LdDiagramObject *
ld_diagram_object_new (JsonObject *storage)
{
LdDiagramObject *self;
self = g_object_new (LD_TYPE_DIAGRAM_OBJECT, "storage", storage, NULL);
return self;
}
/**
* ld_diagram_object_get_storage:
* @self: An #LdDiagramObject object.
*
* Get the storage for object parameters.
*
* Return value: (transfer none): A #JsonObject boxed type.
*/
JsonObject *
ld_diagram_object_get_storage (LdDiagramObject *self)
{
g_return_val_if_fail (LD_IS_DIAGRAM_OBJECT (self), NULL);
if (!self->priv->storage)
self->priv->storage = json_object_new ();
return self->priv->storage;
}
/**
* ld_diagram_object_set_storage:
* @self: An #LdDiagramObject object.
* @storage: (transfer none): A #JsonObject boxed type.
*
* Set the storage for object parameters.
*/
void
ld_diagram_object_set_storage (LdDiagramObject *self, JsonObject *storage)
{
g_return_if_fail (LD_IS_DIAGRAM_OBJECT (self));
if (self->priv->storage)
json_object_unref (self->priv->storage);
if (storage)
self->priv->storage = json_object_ref (storage);
else
self->priv->storage = NULL;
}
/**
* ld_diagram_object_get_data:
* @self: An #LdDiagramObject object.
* @value: (out): Where the data will be stored.
* @pspec: The parameter to read data for.
*
* Retrieve data from internal storage.
*/
void
ld_diagram_object_get_data (LdDiagramObject *self,
GValue *value, GParamSpec *pspec)
{
JsonObject *storage;
JsonNode *node;
const gchar *name;
GValue json_value;
gboolean result;
g_return_if_fail (LD_IS_DIAGRAM_OBJECT (self));
g_return_if_fail (G_IS_VALUE (value));
g_return_if_fail (G_IS_PARAM_SPEC (pspec));
storage = ld_diagram_object_get_storage (self);
name = g_param_spec_get_name (pspec);
node = json_object_get_member (storage, name);
if (!node || json_node_is_null (node))
goto ld_diagram_object_get_data_default;
if (!JSON_NODE_HOLDS_VALUE (node))
goto ld_diagram_object_get_data_warn;
memset (&json_value, 0, sizeof (json_value));
json_node_get_value (node, &json_value);
result = g_param_value_convert (pspec, &json_value, value, FALSE);
g_value_unset (&json_value);
if (!result)
goto ld_diagram_object_get_data_warn;
return;
ld_diagram_object_get_data_warn:
g_warning ("%s: unable to set property `%s' of type `%s'"
" from node of type `%s'; setting the property to it's default value",
G_STRFUNC, name, G_PARAM_SPEC_TYPE_NAME (pspec),
json_node_type_name (node));
ld_diagram_object_get_data_default:
g_param_value_set_default (pspec, value);
g_object_set_property (G_OBJECT (self), name, value);
}
/**
* ld_diagram_object_set_data:
* @self: An #LdDiagramObject object.
* @value: The data.
* @pspec: The parameter to set data for.
*
* Set data in internal storage.
*/
void
ld_diagram_object_set_data (LdDiagramObject *self,
const GValue *value, GParamSpec *pspec)
{
JsonObject *storage;
const gchar *name;
JsonNode *node;
g_return_if_fail (LD_IS_DIAGRAM_OBJECT (self));
g_return_if_fail (G_IS_VALUE (value));
g_return_if_fail (G_IS_PARAM_SPEC (pspec));
storage = ld_diagram_object_get_storage (self);
name = g_param_spec_get_name (pspec);
node = json_node_new (JSON_NODE_VALUE);
json_node_set_value (node, value);
/* We have to remove it first due to a bug in json-glib. */
json_object_remove_member (storage, name);
json_object_set_member (storage, name, node);
}
/**
* ld_diagram_object_get_x:
@ -136,8 +299,11 @@ ld_diagram_object_set_property (GObject *object, guint property_id,
gdouble
ld_diagram_object_get_x (LdDiagramObject *self)
{
gdouble x;
g_return_val_if_fail (LD_IS_DIAGRAM_OBJECT (self), 0);
return self->priv->x;
g_object_get (self, "x", &x, NULL);
return x;
}
/**
@ -149,8 +315,11 @@ ld_diagram_object_get_x (LdDiagramObject *self)
gdouble
ld_diagram_object_get_y (LdDiagramObject *self)
{
gdouble y;
g_return_val_if_fail (LD_IS_DIAGRAM_OBJECT (self), 0);
return self->priv->y;
g_object_get (self, "y", &y, NULL);
return y;
}
/**
@ -164,9 +333,7 @@ void
ld_diagram_object_set_x (LdDiagramObject *self, gdouble x)
{
g_return_if_fail (LD_IS_DIAGRAM_OBJECT (self));
self->priv->x = x;
g_object_notify (G_OBJECT (self), "x");
g_object_set (self, "x", x, NULL);
}
/**
@ -180,7 +347,5 @@ void
ld_diagram_object_set_y (LdDiagramObject *self, gdouble y)
{
g_return_if_fail (LD_IS_DIAGRAM_OBJECT (self));
self->priv->y = y;
g_object_notify (G_OBJECT (self), "y");
g_object_set (self, "y", y, NULL);
}

View File

@ -2,7 +2,7 @@
* ld-diagram-object.h
*
* This file is a part of logdiag.
* Copyright Přemysl Janouch 2010. All rights reserved.
* Copyright Přemysl Janouch 2010 - 2011. All rights reserved.
*
* See the file LICENSE for licensing information.
*
@ -53,6 +53,13 @@ struct _LdDiagramObjectClass
GType ld_diagram_object_get_type (void) G_GNUC_CONST;
LdDiagramObject *ld_diagram_object_new (JsonObject *storage);
JsonObject *ld_diagram_object_get_storage (LdDiagramObject *self);
void ld_diagram_object_set_storage (LdDiagramObject *self, JsonObject *storage);
void ld_diagram_object_get_data (LdDiagramObject *self,
GValue *value, GParamSpec *pspec);
void ld_diagram_object_set_data (LdDiagramObject *self,
const GValue *value, GParamSpec *pspec);
gdouble ld_diagram_object_get_x (LdDiagramObject *self);
gdouble ld_diagram_object_get_y (LdDiagramObject *self);
void ld_diagram_object_set_x (LdDiagramObject *self, gdouble x);

View File

@ -20,15 +20,6 @@
* #LdDiagramSymbol is an implementation of #LdDiagramObject.
*/
/*
* LdDiagramSymbolPrivate:
* @klass: The class of this symbol.
*/
struct _LdDiagramSymbolPrivate
{
gchar *klass;
};
enum
{
PROP_0,
@ -39,7 +30,6 @@ static void ld_diagram_symbol_get_property (GObject *object, guint property_id,
GValue *value, GParamSpec *pspec);
static void ld_diagram_symbol_set_property (GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec);
static void ld_diagram_symbol_finalize (GObject *gobject);
G_DEFINE_TYPE (LdDiagramSymbol, ld_diagram_symbol, LD_TYPE_DIAGRAM_OBJECT);
@ -53,7 +43,6 @@ ld_diagram_symbol_class_init (LdDiagramSymbolClass *klass)
object_class = G_OBJECT_CLASS (klass);
object_class->get_property = ld_diagram_symbol_get_property;
object_class->set_property = ld_diagram_symbol_set_property;
object_class->finalize = ld_diagram_symbol_finalize;
/**
* LdDiagramSymbol:class:
@ -64,42 +53,24 @@ ld_diagram_symbol_class_init (LdDiagramSymbolClass *klass)
"The class of this symbol.",
"", G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_CLASS, pspec);
g_type_class_add_private (klass, sizeof (LdDiagramSymbolPrivate));
}
static void
ld_diagram_symbol_init (LdDiagramSymbol *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE
(self, LD_TYPE_DIAGRAM_SYMBOL, LdDiagramSymbolPrivate);
}
static void
ld_diagram_symbol_finalize (GObject *gobject)
{
LdDiagramSymbol *self;
self = LD_DIAGRAM_SYMBOL (gobject);
if (self->priv->klass)
g_free (self->priv->klass);
/* Chain up to the parent class. */
G_OBJECT_CLASS (ld_diagram_symbol_parent_class)->finalize (gobject);
}
static void
ld_diagram_symbol_get_property (GObject *object, guint property_id,
GValue *value, GParamSpec *pspec)
{
LdDiagramSymbol *self;
LdDiagramObject *self;
self = LD_DIAGRAM_SYMBOL (object);
self = LD_DIAGRAM_OBJECT (object);
switch (property_id)
{
case PROP_CLASS:
g_value_set_string (value, ld_diagram_symbol_get_class (self));
ld_diagram_object_get_data (self, value, pspec);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@ -110,13 +81,13 @@ static void
ld_diagram_symbol_set_property (GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec)
{
LdDiagramSymbol *self;
LdDiagramObject *self;
self = LD_DIAGRAM_SYMBOL (object);
self = LD_DIAGRAM_OBJECT (object);
switch (property_id)
{
case PROP_CLASS:
ld_diagram_symbol_set_class (self, g_value_get_string (value));
ld_diagram_object_set_data (self, value, pspec);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@ -126,17 +97,16 @@ ld_diagram_symbol_set_property (GObject *object, guint property_id,
/**
* ld_diagram_symbol_new:
* @klass: The class of the new symbol.
* @storage: A storage backend.
*
* Return value: A new #LdDiagramSymbol object.
*/
LdDiagramSymbol *
ld_diagram_symbol_new (const gchar *klass)
ld_diagram_symbol_new (JsonObject *storage)
{
LdDiagramSymbol *self;
self = g_object_new (LD_TYPE_DIAGRAM_SYMBOL, NULL);
ld_diagram_symbol_set_class (self, klass);
self = g_object_new (LD_TYPE_DIAGRAM_SYMBOL, "storage", storage, NULL);
return self;
}
@ -149,12 +119,15 @@ ld_diagram_symbol_new (const gchar *klass)
const gchar *
ld_diagram_symbol_get_class (LdDiagramSymbol *self)
{
const gchar *klass;
g_return_val_if_fail (LD_IS_DIAGRAM_SYMBOL (self), NULL);
return self->priv->klass;
g_object_get (self, "class", &klass, NULL);
return klass;
}
/**
* ld_diagram_symbol_get_class:
* ld_diagram_symbol_set_class:
* @self: An #LdDiagramSymbol object.
* @klass: The class.
*
@ -164,8 +137,5 @@ void
ld_diagram_symbol_set_class (LdDiagramSymbol *self, const gchar *klass)
{
g_return_if_fail (LD_IS_DIAGRAM_SYMBOL (self));
if (self->priv->klass)
g_free (self->priv->klass);
self->priv->klass = g_strdup (klass);
g_object_set (self, "class", klass, NULL);
}

View File

@ -2,7 +2,7 @@
* ld-diagram-symbol.h
*
* This file is a part of logdiag.
* Copyright Přemysl Janouch 2010. All rights reserved.
* Copyright Přemysl Janouch 2010 - 2011. All rights reserved.
*
* See the file LICENSE for licensing information.
*
@ -38,7 +38,6 @@ struct _LdDiagramSymbol
{
/*< private >*/
LdDiagramObject parent_instance;
LdDiagramSymbolPrivate *priv;
};
/**
@ -53,7 +52,7 @@ struct _LdDiagramSymbolClass
GType ld_diagram_symbol_get_type (void) G_GNUC_CONST;
LdDiagramSymbol *ld_diagram_symbol_new (const gchar *klass);
LdDiagramSymbol *ld_diagram_symbol_new (JsonObject *storage);
const gchar *ld_diagram_symbol_get_class (LdDiagramSymbol *self);
void ld_diagram_symbol_set_class (LdDiagramSymbol *self, const gchar *klass);

View File

@ -844,7 +844,8 @@ on_canvas_button_release (GtkWidget *widget, GdkEventButton *event,
klass = g_build_path (LD_LIBRARY_IDENTIFIER_SEPARATOR,
category_name, symbol_name, NULL);
symbol = ld_diagram_symbol_new (klass);
symbol = ld_diagram_symbol_new (NULL);
ld_diagram_symbol_set_class (symbol, klass);
g_free (klass);
ld_canvas_add_object_begin (self->priv->canvas,