Redesign the library and related components.

This step is needed, since the current design is quite chaotic
and it would not be possible to finish the application.

There's still a lot to be done.

After this step, it's time to implement the rest of LdLua.
This commit is contained in:
Přemysl Eric Janouch 2010-09-30 05:19:31 +02:00
parent 0c9b297e50
commit e0e63649dc
10 changed files with 138 additions and 84 deletions

View File

@ -12,9 +12,10 @@
#include "config.h" #include "config.h"
#include "ld-library.h"
#include "ld-symbol-category.h"
#include "ld-symbol.h" #include "ld-symbol.h"
#include "ld-symbol-category.h"
#include "ld-library.h"
#include "ld-lua.h" #include "ld-lua.h"
@ -132,9 +133,10 @@ load_category (LdLibrary *self, const char *path, const char *name)
/* TODO: Search for category.json and read the category name from it. */ /* TODO: Search for category.json and read the category name from it. */
/* TODO: Search for xyz.lua and load the objects into the category. */ /* TODO: Search for xyz.lua and load the objects into the category. */
cat = ld_symbol_category_new (self); cat = ld_symbol_category_new (name);
cat->name = g_strdup (name); ld_symbol_category_set_image_path (cat, icon_file);
cat->image_path = icon_file;
g_free (icon_file);
return cat; return cat;
} }
@ -167,7 +169,8 @@ ld_library_load (LdLibrary *self, const char *path)
categ_path = g_build_filename (path, item, NULL); categ_path = g_build_filename (path, item, NULL);
cat = load_category (self, categ_path, item); cat = load_category (self, categ_path, item);
if (cat) if (cat)
g_hash_table_insert (self->categories, cat->name, cat); g_hash_table_insert (self->categories,
g_strdup (ld_symbol_category_get_name (cat)), cat);
g_free (categ_path); g_free (categ_path);
changed = TRUE; changed = TRUE;

View File

@ -59,8 +59,7 @@ struct _LdLibraryClass
GType ld_library_get_type (void) G_GNUC_CONST; GType ld_library_get_type (void) G_GNUC_CONST;
LdLibrary *ld_library_new (void); LdLibrary *ld_library_new (void);
gboolean ld_library_load (LdLibrary *self, gboolean ld_library_load (LdLibrary *self, const gchar *directory);
const gchar *directory);
void ld_library_clear (LdLibrary *self); void ld_library_clear (LdLibrary *self);

View File

@ -12,9 +12,10 @@
#include "config.h" #include "config.h"
#include "ld-library.h"
#include "ld-symbol.h" #include "ld-symbol.h"
#include "ld-symbol-category.h" #include "ld-symbol-category.h"
#include "ld-library.h"
#include "ld-lua.h" #include "ld-lua.h"
#include "ld-lua-symbol.h" #include "ld-lua-symbol.h"
@ -35,6 +36,10 @@
struct _LdLuaSymbolPrivate struct _LdLuaSymbolPrivate
{ {
LdLua *lua; LdLua *lua;
/* XXX: Note that this identifier != symbol name,
* since there can be more symbols with the same name,
*      only in different categories.
*/
gchar *ident; gchar *ident;
}; };
@ -88,15 +93,18 @@ ld_lua_symbol_finalize (GObject *gobject)
* Load a symbol from a file into the library. * Load a symbol from a file into the library.
*/ */
LdSymbol * LdSymbol *
ld_lua_symbol_new (LdLua *lua, const gchar *ident) ld_lua_symbol_new (const gchar *name, LdLua *lua, const gchar *ident)
{ {
LdLuaSymbol *self; LdLuaSymbol *self;
g_return_val_if_fail (name != NULL, NULL);
g_return_val_if_fail (LD_IS_LUA (lua), NULL); g_return_val_if_fail (LD_IS_LUA (lua), NULL);
g_return_val_if_fail (ident != NULL, NULL); g_return_val_if_fail (ident != NULL, NULL);
self = g_object_new (LD_TYPE_LUA_SYMBOL, NULL); self = g_object_new (LD_TYPE_LUA_SYMBOL, NULL);
/* TODO: Set the symbol name. */
self->priv->lua = lua; self->priv->lua = lua;
g_object_ref (lua); g_object_ref (lua);

View File

@ -53,7 +53,7 @@ struct _LdLuaSymbolClass
GType ld_lua_symbol_get_type (void) G_GNUC_CONST; GType ld_lua_symbol_get_type (void) G_GNUC_CONST;
LdSymbol *ld_lua_symbol_new (LdLua *lua, const gchar *ident); LdSymbol *ld_lua_symbol_new (const gchar *name, LdLua *lua, const gchar *ident);
G_END_DECLS G_END_DECLS

View File

@ -15,9 +15,10 @@
#include "config.h" #include "config.h"
#include "ld-library.h"
#include "ld-symbol-category.h"
#include "ld-symbol.h" #include "ld-symbol.h"
#include "ld-symbol-category.h"
#include "ld-library.h"
#include "ld-lua.h" #include "ld-lua.h"

View File

@ -12,9 +12,9 @@
#include "config.h" #include "config.h"
#include "ld-library.h"
#include "ld-symbol-category.h"
#include "ld-symbol.h" #include "ld-symbol.h"
#include "ld-symbol-category.h"
#include "ld-library.h"
/** /**
@ -25,6 +25,19 @@
* #LdSymbolCategory represents a category of #LdSymbol objects. * #LdSymbolCategory represents a category of #LdSymbol objects.
*/ */
/*
* LdSymbolCategoryPrivate:
* @name: The name of this category.
* @image_path: Path to the image for this category.
* @children: Children of this category.
*/
struct _LdSymbolCategoryPrivate
{
gchar *name;
gchar *image_path;
GSList *children;
};
G_DEFINE_TYPE (LdSymbolCategory, ld_symbol_category, G_TYPE_OBJECT); G_DEFINE_TYPE (LdSymbolCategory, ld_symbol_category, G_TYPE_OBJECT);
static void static void
@ -38,16 +51,15 @@ ld_symbol_category_class_init (LdSymbolCategoryClass *klass)
object_class = G_OBJECT_CLASS (klass); object_class = G_OBJECT_CLASS (klass);
object_class->finalize = ld_symbol_category_finalize; object_class->finalize = ld_symbol_category_finalize;
g_type_class_add_private (klass, sizeof (LdSymbolCategoryPrivate));
} }
static void static void
ld_symbol_category_init (LdSymbolCategory *self) ld_symbol_category_init (LdSymbolCategory *self)
{ {
/* TODO: use _new_full, correct equal and specify destroy functions. */ self->priv = G_TYPE_INSTANCE_GET_PRIVATE
/* XXX: How's the situation with subcategory names and symbol names (self, LD_TYPE_SYMBOL_CATEGORY, LdSymbolCategoryPrivate);
* within the same hashtable?
*/
self->children = g_hash_table_new (g_str_hash, g_str_equal);
} }
static void static void
@ -57,13 +69,10 @@ ld_symbol_category_finalize (GObject *gobject)
self = LD_SYMBOL_CATEGORY (gobject); self = LD_SYMBOL_CATEGORY (gobject);
if (self->name) if (self->priv->name)
g_free (self->name); g_free (self->priv->name);
if (self->image_path) if (self->priv->image_path)
g_free (self->image_path); g_free (self->priv->image_path);
g_object_unref (self->parent);
g_hash_table_destroy (self->children);
/* Chain up to the parent class. */ /* Chain up to the parent class. */
G_OBJECT_CLASS (ld_symbol_category_parent_class)->finalize (gobject); G_OBJECT_CLASS (ld_symbol_category_parent_class)->finalize (gobject);
@ -76,15 +85,71 @@ ld_symbol_category_finalize (GObject *gobject)
* Create an instance. * Create an instance.
*/ */
LdSymbolCategory * LdSymbolCategory *
ld_symbol_category_new (LdLibrary *parent) ld_symbol_category_new (const gchar *name)
{ {
LdSymbolCategory *cat; LdSymbolCategory *cat;
cat = g_object_new (LD_TYPE_SYMBOL_CATEGORY, NULL); cat = g_object_new (LD_TYPE_SYMBOL_CATEGORY, NULL);
cat->priv->name = g_strdup (name);
cat->parent = parent;
g_object_ref (parent);
return cat; return cat;
} }
/**
* ld_symbol_category_set_name:
* @self: An #LdSymbolCategory object.
* @name: The new name for this category.
*/
void
ld_symbol_category_set_name (LdSymbolCategory *self, const gchar *name)
{
g_return_if_fail (LD_IS_SYMBOL_CATEGORY (self));
g_return_if_fail (name != NULL);
if (self->priv->name)
g_free (self->priv->name);
self->priv->name = g_strdup (name);
}
/**
* ld_symbol_category_get_name:
* @self: An #LdSymbolCategory object.
*
* Return the name of this category.
*/
const gchar *
ld_symbol_category_get_name (LdSymbolCategory *self)
{
g_return_val_if_fail (LD_IS_SYMBOL_CATEGORY (self), NULL);
return self->priv->name;
}
/**
* ld_symbol_category_set_image_path:
* @self: An #LdSymbolCategory object.
* @image_path: The new path to the image for this category. May be NULL.
*/
void
ld_symbol_category_set_image_path (LdSymbolCategory *self,
const gchar *image_path)
{
g_return_if_fail (LD_IS_SYMBOL_CATEGORY (self));
if (self->priv->image_path)
g_free (self->priv->image_path);
self->priv->image_path = g_strdup (image_path);
}
/**
* ld_symbol_category_get_image_path:
* @self: An #LdSymbolCategory object.
*
* Return the filesystem path to the image for this category. May be NULL.
*/
const gchar *
ld_symbol_category_get_image_path (LdSymbolCategory *self)
{
g_return_val_if_fail (LD_IS_SYMBOL_CATEGORY (self), NULL);
return self->priv->image_path;
}

View File

@ -27,33 +27,24 @@ G_BEGIN_DECLS
((obj), LD_SYMBOL_CATEGORY, LdSymbolCategoryClass)) ((obj), LD_SYMBOL_CATEGORY, LdSymbolCategoryClass))
typedef struct _LdSymbolCategory LdSymbolCategory; typedef struct _LdSymbolCategory LdSymbolCategory;
typedef struct _LdSymbolCategoryPrivate LdSymbolCategoryPrivate;
typedef struct _LdSymbolCategoryClass LdSymbolCategoryClass; typedef struct _LdSymbolCategoryClass LdSymbolCategoryClass;
/** /**
* LdSymbolCategory: * LdSymbolCategory:
* @parent: The parent object, may be #LdLibrary
* or another #LdSymbolCategory.
* @name: The name of the category.
* @image_path: Path to the image for this category.
* @children: Children of this category.
*/
/* TODO: Make the public fields private and set them as properties
* + implement setters and getters. On change the category
* shall emit a "changed" signal in the Library.
*/ */
struct _LdSymbolCategory struct _LdSymbolCategory
{ {
/*< private >*/ /*< private >*/
GObject parent_instance; GObject parent_instance;
LdSymbolCategoryPrivate *priv;
/*< public >*/
gpointer parent;
gchar *name;
gchar *image_path;
GHashTable *children;
}; };
/* TODO: If required sometime, categories (and maybe symbols) should implement
* a "changed" signal. This can be somewhat tricky. The library might be
* a good candidate for what they call a proxy. See GtkUIManager.
*/
struct _LdSymbolCategoryClass struct _LdSymbolCategoryClass
{ {
GObjectClass parent_class; GObjectClass parent_class;
@ -62,13 +53,20 @@ struct _LdSymbolCategoryClass
GType ld_symbol_category_get_type (void) G_GNUC_CONST; GType ld_symbol_category_get_type (void) G_GNUC_CONST;
LdSymbolCategory * LdSymbolCategory *ld_symbol_category_new (const gchar *name);
ld_symbol_category_new (LdLibrary *parent);
/* TODO: Methods for inserting and removing children. */ /* TODO: Create properties for "name" and "image_path". */
/* TODO: Create a separate ld-symbol-private.h, include it in this ld-s-c.c void ld_symbol_category_set_name (LdSymbolCategory *self, const gchar *name);
* and then assign and ref the parent category here. const gchar *ld_symbol_category_get_name (LdSymbolCategory *self);
*/ void ld_symbol_category_set_image_path (LdSymbolCategory *self,
const gchar *image_path);
const gchar *ld_symbol_category_get_image_path (LdSymbolCategory *self);
/* TODO: Implement. */
void ld_symbol_category_insert_child (LdSymbolCategory *self,
GObject *child, gint pos);
void ld_symbol_category_remove_child (LdSymbolCategory *self,
GObject *child);
GSList *ld_symbol_category_get_children (LdSymbolCategory *self);
G_END_DECLS G_END_DECLS

View File

@ -12,9 +12,9 @@
#include "config.h" #include "config.h"
#include "ld-library.h"
#include "ld-symbol-category.h"
#include "ld-symbol.h" #include "ld-symbol.h"
#include "ld-symbol-category.h"
#include "ld-library.h"
/** /**
@ -28,12 +28,11 @@
/* /*
* LdSymbolPrivate: * LdSymbolPrivate:
* @parent: The parent LdSymbolCategory. It is used to identify * @name: The name of this symbol.
* the object within it's library.
*/ */
struct _LdSymbolPrivate struct _LdSymbolPrivate
{ {
LdSymbolCategory *parent; gchar *name;
}; };
G_DEFINE_ABSTRACT_TYPE (LdSymbol, ld_symbol, G_TYPE_OBJECT); G_DEFINE_ABSTRACT_TYPE (LdSymbol, ld_symbol, G_TYPE_OBJECT);
@ -65,26 +64,11 @@ ld_symbol_finalize (GObject *gobject)
LdSymbol *self; LdSymbol *self;
self = LD_SYMBOL (gobject); self = LD_SYMBOL (gobject);
g_object_unref (self->priv->parent);
/* Chain up to the parent class. */ /* Chain up to the parent class. */
G_OBJECT_CLASS (ld_symbol_parent_class)->finalize (gobject); G_OBJECT_CLASS (ld_symbol_parent_class)->finalize (gobject);
} }
/**
* ld_symbol_build_identifier:
* @self: A symbol object.
*
* Build an identifier for the symbol.
* The identifier is in the format "Category/Category/Symbol".
*/
gchar *
ld_symbol_build_identifier (LdSymbol *self)
{
/* TODO: Implement. */
return NULL;
}
/** /**
* ld_symbol_draw: * ld_symbol_draw:
* @self: A symbol object. * @self: A symbol object.

View File

@ -40,9 +40,6 @@ struct _LdSymbol
/*< private >*/ /*< private >*/
GObject parent_instance; GObject parent_instance;
LdSymbolPrivate *priv; LdSymbolPrivate *priv;
/*< public >*/
gchar *name;
}; };
/** /**
@ -60,13 +57,10 @@ struct _LdSymbolClass
GType ld_symbol_get_type (void) G_GNUC_CONST; GType ld_symbol_get_type (void) G_GNUC_CONST;
gchar *ld_symbol_build_identifier (LdSymbol *self);
void ld_symbol_draw (LdSymbol *self, cairo_t *cr); void ld_symbol_draw (LdSymbol *self, cairo_t *cr);
/* TODO: An interface for symbol terminals etc. */ /* TODO: Interface for symbol terminals
/* TODO: Store a pointer to the parent, probably as a property, * Interface + property for symbol name (will be used by _new)
* using g_object_add_weak_pointer/remove_weak_pointer.
*/ */

View File

@ -13,11 +13,13 @@
#include "config.h" #include "config.h"
#include "ld-window-main.h" #include "ld-window-main.h"
#include "ld-symbol.h"
#include "ld-symbol-category.h"
#include "ld-library.h"
#include "ld-document.h" #include "ld-document.h"
#include "ld-canvas.h" #include "ld-canvas.h"
#include "ld-library.h"
#include "ld-symbol-category.h"
#include "ld-symbol.h"
/** /**
@ -286,7 +288,7 @@ cb_load_category (gpointer key, gpointer value, gpointer user_data)
g_return_if_fail (LD_IS_SYMBOL_CATEGORY (cat)); g_return_if_fail (LD_IS_SYMBOL_CATEGORY (cat));
pbuf = gdk_pixbuf_new_from_file_at_size pbuf = gdk_pixbuf_new_from_file_at_size
(cat->image_path, TOOLBAR_ICON_WIDTH, -1, NULL); (ld_symbol_category_get_image_path (cat), TOOLBAR_ICON_WIDTH, -1, NULL);
g_return_if_fail (pbuf != NULL); g_return_if_fail (pbuf != NULL);
img = gtk_image_new_from_pixbuf (pbuf); img = gtk_image_new_from_pixbuf (pbuf);