Add more properties to LdSymbol.

Also move the actual storage of symbol properties to implementations
of the abstract LdSymbol class. All the properties are read only.
This commit is contained in:
Přemysl Eric Janouch 2010-10-27 15:52:36 +02:00
parent aa33db342b
commit 9156953cc9
5 changed files with 142 additions and 74 deletions

View File

@ -19,10 +19,16 @@ G_BEGIN_DECLS
/* /*
* LdLuaSymbolPrivate: * LdLuaSymbolPrivate:
* @lua: Parent #LdLua object. * @lua: Parent #LdLua object.
* @name: Name of this symbol.
* @human_name: Localized human name of this symbol.
* @area: Area of this symbol.
*/ */
struct _LdLuaSymbolPrivate struct _LdLuaSymbolPrivate
{ {
LdLua *lua; LdLua *lua;
gchar *name;
gchar *human_name;
LdSymbolArea area;
}; };

View File

@ -34,6 +34,9 @@ G_DEFINE_TYPE (LdLuaSymbol, ld_lua_symbol, LD_TYPE_SYMBOL);
static void ld_lua_symbol_finalize (GObject *gobject); static void ld_lua_symbol_finalize (GObject *gobject);
static const gchar *ld_lua_symbol_get_name (LdSymbol *symbol);
static const gchar *ld_lua_symbol_get_human_name (LdSymbol *symbol);
static void ld_lua_symbol_get_area (LdSymbol *symbol, LdSymbolArea *area);
static void ld_lua_symbol_draw (LdSymbol *symbol, cairo_t *cr); static void ld_lua_symbol_draw (LdSymbol *symbol, cairo_t *cr);
@ -45,6 +48,9 @@ ld_lua_symbol_class_init (LdLuaSymbolClass *klass)
object_class = G_OBJECT_CLASS (klass); object_class = G_OBJECT_CLASS (klass);
object_class->finalize = ld_lua_symbol_finalize; object_class->finalize = ld_lua_symbol_finalize;
klass->parent_class.get_name = ld_lua_symbol_get_name;
klass->parent_class.get_human_name = ld_lua_symbol_get_human_name;
klass->parent_class.get_area = ld_lua_symbol_get_area;
klass->parent_class.draw = ld_lua_symbol_draw; klass->parent_class.draw = ld_lua_symbol_draw;
g_type_class_add_private (klass, sizeof (LdLuaSymbolPrivate)); g_type_class_add_private (klass, sizeof (LdLuaSymbolPrivate));
@ -67,18 +73,49 @@ ld_lua_symbol_finalize (GObject *gobject)
ld_lua_private_unregister (self->priv->lua, self); ld_lua_private_unregister (self->priv->lua, self);
g_object_unref (self->priv->lua); g_object_unref (self->priv->lua);
if (self->priv->name)
g_free (self->priv->name);
if (self->priv->human_name)
g_free (self->priv->human_name);
/* Chain up to the parent class. */ /* Chain up to the parent class. */
G_OBJECT_CLASS (ld_lua_symbol_parent_class)->finalize (gobject); G_OBJECT_CLASS (ld_lua_symbol_parent_class)->finalize (gobject);
} }
const gchar *
ld_lua_symbol_get_name (LdSymbol *symbol)
{
g_return_val_if_fail (LD_IS_LUA_SYMBOL (symbol), NULL);
return LD_LUA_SYMBOL (symbol)->priv->name;
}
const gchar *
ld_lua_symbol_get_human_name (LdSymbol *symbol)
{
g_return_val_if_fail (LD_IS_LUA_SYMBOL (symbol), NULL);
return LD_LUA_SYMBOL (symbol)->priv->human_name;
}
void
ld_lua_symbol_get_area (LdSymbol *symbol, LdSymbolArea *area)
{
LdLuaSymbol *self;
g_return_if_fail (LD_IS_LUA_SYMBOL (symbol));
g_return_if_fail (area != NULL);
self = LD_LUA_SYMBOL (symbol);
*area = self->priv->area;
}
static void static void
ld_lua_symbol_draw (LdSymbol *symbol, cairo_t *cr) ld_lua_symbol_draw (LdSymbol *symbol, cairo_t *cr)
{ {
LdLuaSymbol *self; LdLuaSymbol *self;
g_return_if_fail (LD_IS_LUA_SYMBOL (symbol));
g_return_if_fail (cr != NULL); g_return_if_fail (cr != NULL);
g_return_if_fail (LD_IS_SYMBOL (symbol));
self = LD_LUA_SYMBOL (symbol); self = LD_LUA_SYMBOL (symbol);
ld_lua_private_draw (self->priv->lua, self, cr); ld_lua_private_draw (self->priv->lua, self, cr);

View File

@ -363,14 +363,6 @@ ld_lua_logdiag_register (lua_State *L)
lua_pop (L, 1); lua_pop (L, 1);
g_return_val_if_fail (ud != NULL, 0); g_return_val_if_fail (ud != NULL, 0);
/* TODO: Create a symbol using the given parameters:
* 1. name
* 2. names (table) -> use g_get_language_names ()
* 3. area (table)
* 4. terminals (table)
* 5. render function
*/
/* Check and retrieve arguments. */ /* Check and retrieve arguments. */
name = lua_tostring (L, 1); name = lua_tostring (L, 1);
if (!name) if (!name)
@ -378,12 +370,16 @@ ld_lua_logdiag_register (lua_State *L)
if (!lua_isfunction (L, 5)) if (!lua_isfunction (L, 5))
luaL_error (L, "register: bad or missing argument #%d", 5); luaL_error (L, "register: bad or missing argument #%d", 5);
/* Create a symbol object. */ /* TODO: Create a symbol using the given parameters:
* 2. names (table) -> use g_get_language_names ()
* 3. area (table)
* 4. terminals (table)
*/
symbol = g_object_new (LD_TYPE_LUA_SYMBOL, NULL); symbol = g_object_new (LD_TYPE_LUA_SYMBOL, NULL);
symbol->priv->lua = ud->self; symbol->priv->lua = ud->self;
g_object_ref (ud->self); g_object_ref (ud->self);
ld_symbol_set_name (LD_SYMBOL (symbol), name); symbol->priv->name = g_strdup (name);
/* Create an entry in the symbol table. */ /* Create an entry in the symbol table. */
lua_getfield (L, LUA_REGISTRYINDEX, LD_LUA_SYMBOLS_INDEX); lua_getfield (L, LUA_REGISTRYINDEX, LD_LUA_SYMBOLS_INDEX);

View File

@ -26,21 +26,14 @@
* drawn onto the #LdCanvas. * drawn onto the #LdCanvas.
*/ */
/*
* LdSymbolPrivate:
* @name: The name of this symbol.
*/
struct _LdSymbolPrivate
{
gchar *name;
};
G_DEFINE_ABSTRACT_TYPE (LdSymbol, ld_symbol, G_TYPE_OBJECT); G_DEFINE_ABSTRACT_TYPE (LdSymbol, ld_symbol, G_TYPE_OBJECT);
enum enum
{ {
PROP_0, PROP_0,
PROP_NAME PROP_NAME,
PROP_HUMAN_NAME,
/* TODO: Property for the area. */
}; };
static void static void
@ -63,7 +56,6 @@ ld_symbol_class_init (LdSymbolClass *klass)
object_class = G_OBJECT_CLASS (klass); object_class = G_OBJECT_CLASS (klass);
object_class->get_property = ld_symbol_get_property; object_class->get_property = ld_symbol_get_property;
object_class->set_property = ld_symbol_set_property; object_class->set_property = ld_symbol_set_property;
object_class->finalize = ld_symbol_finalize;
/** /**
* LdSymbol:name: * LdSymbol:name:
@ -72,17 +64,23 @@ ld_symbol_class_init (LdSymbolClass *klass)
*/ */
pspec = g_param_spec_string ("name", "Name", pspec = g_param_spec_string ("name", "Name",
"The name of this symbol.", "The name of this symbol.",
"", G_PARAM_READWRITE); "", G_PARAM_READABLE);
g_object_class_install_property (object_class, PROP_NAME, pspec); g_object_class_install_property (object_class, PROP_NAME, pspec);
g_type_class_add_private (klass, sizeof (LdSymbolPrivate)); /**
* LdSymbol:human-name:
*
* The localized human name of this symbol.
*/
pspec = g_param_spec_string ("human-name", "Human name",
"The localized human name of this symbol.",
"", G_PARAM_READABLE);
g_object_class_install_property (object_class, PROP_HUMAN_NAME, pspec);
} }
static void static void
ld_symbol_init (LdSymbol *self) ld_symbol_init (LdSymbol *self)
{ {
self->priv = G_TYPE_INSTANCE_GET_PRIVATE
(self, LD_TYPE_SYMBOL, LdSymbolPrivate);
} }
static void static void
@ -97,6 +95,9 @@ ld_symbol_get_property (GObject *object, guint property_id,
case PROP_NAME: case PROP_NAME:
g_value_set_string (value, ld_symbol_get_name (self)); g_value_set_string (value, ld_symbol_get_name (self));
break; break;
case PROP_HUMAN_NAME:
g_value_set_string (value, ld_symbol_get_human_name (self));
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
} }
@ -106,50 +107,9 @@ static void
ld_symbol_set_property (GObject *object, guint property_id, ld_symbol_set_property (GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec) const GValue *value, GParamSpec *pspec)
{ {
LdSymbol *self;
self = LD_SYMBOL (object);
switch (property_id)
{
case PROP_NAME:
ld_symbol_set_name (self, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
} }
static void
ld_symbol_finalize (GObject *gobject)
{
LdSymbol *self;
self = LD_SYMBOL (gobject);
if (self->priv->name)
g_free (self->priv->name);
/* Chain up to the parent class. */
G_OBJECT_CLASS (ld_symbol_parent_class)->finalize (gobject);
}
/**
* ld_symbol_set_name:
* @self: An #LdSymbol object.
* @name: A new name for the symbol.
*
* Set the name of a symbol.
*/
void
ld_symbol_set_name (LdSymbol *self, const gchar *name)
{
g_return_if_fail (LD_IS_SYMBOL (self));
g_return_if_fail (name != NULL);
if (self->priv->name)
g_free (self->priv->name);
self->priv->name = g_strdup (name);
}
/** /**
* ld_symbol_get_name: * ld_symbol_get_name:
@ -160,8 +120,51 @@ ld_symbol_set_name (LdSymbol *self, const gchar *name)
const gchar * const gchar *
ld_symbol_get_name (LdSymbol *self) ld_symbol_get_name (LdSymbol *self)
{ {
LdSymbolClass *klass;
g_return_if_fail (LD_IS_SYMBOL (self)); g_return_if_fail (LD_IS_SYMBOL (self));
return self->priv->name;
klass = LD_SYMBOL_GET_CLASS (self);
g_return_val_if_fail (klass->get_name != NULL, NULL);
return klass->get_name (self);
}
/**
* ld_symbol_get_human_name:
* @self: An #LdSymbol object.
*
* Return value: The localised human name of the symbol.
*/
const gchar *
ld_symbol_get_human_name (LdSymbol *self)
{
LdSymbolClass *klass;
g_return_if_fail (LD_IS_SYMBOL (self));
klass = LD_SYMBOL_GET_CLASS (self);
g_return_val_if_fail (klass->get_human_name != NULL, NULL);
return klass->get_human_name (self);
}
/**
* ld_symbol_get_area:
* @self: A symbol object.
* @area: Where the area of the symbol will be returned.
*
* Get the area of the symbol.
*/
void
ld_symbol_get_area (LdSymbol *self, LdSymbolArea *area)
{
LdSymbolClass *klass;
g_return_if_fail (LD_IS_SYMBOL (self));
g_return_if_fail (area != NULL);
klass = LD_SYMBOL_GET_CLASS (self);
g_return_if_fail (klass->get_area != NULL);
klass->get_area (self, area);
} }
/** /**
@ -180,6 +183,6 @@ ld_symbol_draw (LdSymbol *self, cairo_t *cr)
g_return_if_fail (cr != NULL); g_return_if_fail (cr != NULL);
klass = LD_SYMBOL_GET_CLASS (self); klass = LD_SYMBOL_GET_CLASS (self);
if (klass->draw) g_return_if_fail (klass->draw != NULL);
klass->draw (self, cr); klass->draw (self, cr);
} }

View File

@ -30,6 +30,8 @@ typedef struct _LdSymbol LdSymbol;
typedef struct _LdSymbolPrivate LdSymbolPrivate; typedef struct _LdSymbolPrivate LdSymbolPrivate;
typedef struct _LdSymbolClass LdSymbolClass; typedef struct _LdSymbolClass LdSymbolClass;
typedef struct _LdSymbolArea LdSymbolArea;
/** /**
* LdSymbol: * LdSymbol:
@ -42,27 +44,51 @@ struct _LdSymbol
LdSymbolPrivate *priv; LdSymbolPrivate *priv;
}; };
/**
* LdSymbolArea:
* @x1: Left-top X coordinate.
* @y1: Left-top Y coordinate.
* @x2: Right-bottom X coordinate.
* @y2: Right-bottom Y coordinate.
*
* Defines the area of the symbol relative to the center of the symbol,
* which is at the (0, 0) coordinates.
*/
struct _LdSymbolArea
{
gdouble x1, y1;
gdouble x2, y2;
};
/** /**
* LdSymbolClass: * LdSymbolClass:
* @parent_class: The parent class. * @parent_class: The parent class.
* @get_name: Get the name of the symbol.
* @get_human_name: Get the localized human name of the symbol.
* @get_area: Get the area of the symbol.
* @draw: Draw the symbol on a Cairo surface. * @draw: Draw the symbol on a Cairo surface.
*/ */
struct _LdSymbolClass struct _LdSymbolClass
{ {
GObjectClass parent_class; GObjectClass parent_class;
const gchar *(*get_name) (LdSymbol *self);
const gchar *(*get_human_name) (LdSymbol *self);
void (*get_area) (LdSymbol *self, LdSymbolArea *area);
void (*draw) (LdSymbol *self, cairo_t *cr); void (*draw) (LdSymbol *self, cairo_t *cr);
}; };
GType ld_symbol_get_type (void) G_GNUC_CONST; GType ld_symbol_get_type (void) G_GNUC_CONST;
void ld_symbol_set_name (LdSymbol *self, const gchar *name);
const gchar *ld_symbol_get_name (LdSymbol *self); const gchar *ld_symbol_get_name (LdSymbol *self);
const gchar *ld_symbol_get_human_name (LdSymbol *self);
void ld_symbol_get_area (LdSymbol *self, LdSymbolArea *area);
void ld_symbol_draw (LdSymbol *self, cairo_t *cr); void ld_symbol_draw (LdSymbol *self, cairo_t *cr);
/* TODO: Interface for symbol terminals. */ /* TODO: Interface for terminals.
* Something like a list of gdouble pairs (-> a new structure).
*/
G_END_DECLS G_END_DECLS