[AdgModel] Reworked public API
[adg.git] / adg / adg-model.c
blob35fa8f6d551bd17534c47828c8a971aa191b9e4a
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:adg-model
23 * @short_description: The base class of the ADG model infrastructure
25 * A model is a conceptual representation of something. From an ADG
26 * user point of view, it is a collection of data and rules that
27 * defines how an object should be represented on a drawing.
29 * Because #AdgModel instances are only a conceptual idea, they are
30 * not renderable (that is, #AdgModel is not derived from #AdgEntity).
31 * Instead, it must be passed as subject to entities such as #AdgStroke
32 * or #AdgHatch.
33 **/
35 /**
36 * AdgModel:
38 * All fields are private and should not be used directly.
39 * Use its public methods instead.
40 **/
43 #include "adg-model.h"
44 #include "adg-model-private.h"
45 #include "adg-marshal.h"
46 #include "adg-intl.h"
48 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_model_parent_class)
51 enum {
52 PROP_0,
53 PROP_DEPENDENCY
56 enum {
57 ADD_DEPENDENCY,
58 REMOVE_DEPENDENCY,
59 CLEAR,
60 CHANGED,
61 LAST_SIGNAL
65 static void dispose (GObject *object);
66 static void set_property (GObject *object,
67 guint prop_id,
68 const GValue *value,
69 GParamSpec *pspec);
71 static void add_dependency (AdgModel *model,
72 AdgEntity *entity);
73 static void remove_dependency (AdgModel *model,
74 AdgEntity *entity);
75 static void changed (AdgModel *model);
77 static guint signals[LAST_SIGNAL] = { 0 };
80 G_DEFINE_ABSTRACT_TYPE(AdgModel, adg_model, G_TYPE_OBJECT);
83 static void
84 adg_model_class_init(AdgModelClass *klass)
86 GObjectClass *gobject_class;
87 GParamSpec *param;
89 gobject_class = (GObjectClass *) klass;
91 g_type_class_add_private(klass, sizeof(AdgModelPrivate));
93 gobject_class->dispose = dispose;
94 gobject_class->set_property = set_property;
96 klass->add_dependency = add_dependency;
97 klass->remove_dependency = remove_dependency;
98 klass->clear = NULL;
99 klass->changed = changed;
101 param = g_param_spec_object("dependency",
102 P_("Dependency"),
103 P_("Can be used to add a new dependency from this model (this entity will be invalidated on model changed)"),
104 ADG_TYPE_ENTITY, G_PARAM_WRITABLE);
105 g_object_class_install_property(gobject_class, PROP_DEPENDENCY, param);
108 * AdgModel::add-dependency:
109 * @model: an #AdgModel
110 * @entity: an #AdgEntity that depends on @model
112 * Adds @entity to @model. After that @entity will depend on @model,
113 * that is #AdgModel::changed on @model will invalidate @entity.
115 signals[ADD_DEPENDENCY] = g_signal_new("add-dependency",
116 G_OBJECT_CLASS_TYPE(gobject_class),
117 G_SIGNAL_RUN_FIRST,
118 G_STRUCT_OFFSET(AdgModelClass, add_dependency),
119 NULL, NULL,
120 adg_marshal_VOID__OBJECT,
121 G_TYPE_NONE, 1, ADG_TYPE_ENTITY);
124 * AdgModel::remove-dependency:
125 * @model: an #AdgModel
126 * @entity: the #AdgEntity that does not depend on @model anymore
128 * Removes the @entity from @model, that is @entity will not depend
129 * on @model anymore.
131 signals[REMOVE_DEPENDENCY] = g_signal_new("remove-dependency",
132 G_OBJECT_CLASS_TYPE(gobject_class),
133 G_SIGNAL_RUN_FIRST,
134 G_STRUCT_OFFSET(AdgModelClass, remove_dependency),
135 NULL, NULL,
136 adg_marshal_VOID__OBJECT,
137 G_TYPE_NONE, 1, ADG_TYPE_ENTITY);
140 * AdgModel::clear:
141 * @model: an #AdgModel
143 * Removes any cached information from @model.
145 signals[CLEAR] = g_signal_new("clear", ADG_TYPE_MODEL,
146 G_SIGNAL_RUN_LAST|G_SIGNAL_NO_RECURSE,
147 G_STRUCT_OFFSET(AdgModelClass, clear),
148 NULL, NULL,
149 adg_marshal_VOID__VOID,
150 G_TYPE_NONE, 0);
153 * AdgModel::changed:
154 * @model: an #AdgModel
156 * Notificates that the model has changed. By default, all the
157 * dependent entities are invalidated.
159 signals[CHANGED] = g_signal_new("changed", ADG_TYPE_MODEL,
160 G_SIGNAL_RUN_LAST|G_SIGNAL_NO_RECURSE,
161 G_STRUCT_OFFSET(AdgModelClass, changed),
162 NULL, NULL,
163 adg_marshal_VOID__VOID,
164 G_TYPE_NONE, 0);
167 static void
168 adg_model_init(AdgModel *model)
170 AdgModelPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(model, ADG_TYPE_MODEL,
171 AdgModelPrivate);
173 data->dependencies = NULL;
175 model->data = data;
178 static void
179 dispose(GObject *object)
181 AdgModel *model;
182 AdgModelPrivate *data;
183 AdgEntity *entity;
185 model = (AdgModel *) object;
186 data = model->data;
188 /* Remove all the dependencies from the model: this will emit
189 * a "remove" signal for every dependency and will drop all the
190 * references from those entities to this model */
191 while (data->dependencies != NULL) {
192 entity = (AdgEntity *) data->dependencies->data;
193 adg_model_remove_dependency(model, entity);
196 if (PARENT_OBJECT_CLASS->dispose != NULL)
197 PARENT_OBJECT_CLASS->dispose(object);
200 static void
201 set_property(GObject *object,
202 guint prop_id, const GValue *value, GParamSpec *pspec)
204 AdgModel *model = (AdgModel *) object;
206 switch (prop_id) {
207 case PROP_DEPENDENCY:
208 adg_model_add_dependency(model, g_value_get_object(value));
209 break;
210 default:
211 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
217 * adg_model_add_dependency:
218 * @model: an #AdgModel
219 * @entity: an #AdgEntity
221 * <note><para>
222 * This function is only useful in entity implementations.
223 * </para></note>
225 * Emits a #AdgModel::add-dependency signal on @model passing @entity
226 * as argument. This will add a reference to @entity owned by @model.
228 void
229 adg_model_add_dependency(AdgModel *model, AdgEntity *entity)
231 g_return_if_fail(ADG_IS_MODEL(model));
232 g_return_if_fail(ADG_IS_ENTITY(entity));
234 g_signal_emit(model, signals[ADD_DEPENDENCY], 0, entity);
238 * adg_model_remove_dependency:
239 * @model: an #AdgModel
240 * @entity: an #AdgEntity
242 * <note><para>
243 * This function is only useful in entity implementations.
244 * </para></note>
246 * Emits a #AdgModel::remove-dependency signal on @model passing
247 * @entity as argument. @entity must be inside @model.
249 * Note that @model will own a reference to @entity and it
250 * may be the last reference held: this means removing an entity
251 * from the model can destroy it.
253 void
254 adg_model_remove_dependency(AdgModel *model, AdgEntity *entity)
256 g_return_if_fail(ADG_IS_MODEL(model));
257 g_return_if_fail(ADG_IS_ENTITY(entity));
259 g_signal_emit(model, signals[REMOVE_DEPENDENCY], 0, entity);
263 * adg_model_dependencies:
264 * @model: an #AdgModel
266 * Gets the list of entities dependending on @model. This list
267 * is owned by @model and must not be modified or freed.
269 * Returns: a #GSList of dependencies or %NULL on error
271 const GSList *
272 adg_model_dependencies(AdgModel *model)
274 AdgModelPrivate *data;
276 g_return_val_if_fail(ADG_IS_MODEL(model), NULL);
278 data = model->data;
280 return data->dependencies;
284 * adg_model_foreach_dependency:
285 * @model: an #AdgModel
286 * @callback: the entity callback
287 * @user_data: callback general purpose user data
289 * Invokes @callback on each entity linked to @model.
291 void
292 adg_model_foreach_dependency(AdgModel *model, AdgEntityCallback callback,
293 gpointer user_data)
295 AdgModelPrivate *data;
296 GSList *dependency;
297 AdgEntity *entity;
299 g_return_if_fail(ADG_IS_MODEL(model));
300 g_return_if_fail(callback != NULL);
302 data = model->data;
303 dependency = data->dependencies;
305 while (dependency != NULL) {
306 entity = dependency->data;
308 if (entity != NULL && ADG_IS_ENTITY(entity))
309 callback(entity, user_data);
311 dependency = dependency->next;
316 * adg_model_clear:
317 * @model: an #AdgModel
319 * <note><para>
320 * This function is only useful in entity implementations.
321 * </para></note>
323 * Emits the #AdgModel::clear signal on @model.
325 void
326 adg_model_clear(AdgModel *model)
328 g_return_if_fail(ADG_IS_MODEL(model));
330 g_signal_emit(model, signals[CLEAR], 0);
334 * adg_model_changed:
335 * @model: an #AdgModel
337 * <note><para>
338 * This function is only useful in entity implementations.
339 * </para></note>
341 * Emits the #AdgModel::changed signal on @model.
343 void
344 adg_model_changed(AdgModel *model)
346 g_return_if_fail(ADG_IS_MODEL(model));
348 g_signal_emit(model, signals[CHANGED], 0);
352 static void
353 add_dependency(AdgModel *model, AdgEntity *entity)
355 AdgModelPrivate *data = model->data;
357 /* The prepend operation is more efficient */
358 data->dependencies = g_slist_prepend(data->dependencies, entity);
360 g_object_ref(entity);
363 static void
364 remove_dependency(AdgModel *model, AdgEntity *entity)
366 AdgModelPrivate *data;
367 GSList *node;
369 data = model->data;
370 node = g_slist_find(data->dependencies, entity);
372 if (node == NULL) {
373 g_warning("Attempting to remove an entity with type %s from a "
374 "model of type %s, but the entity is not present.",
375 g_type_name(G_OBJECT_TYPE(entity)),
376 g_type_name(G_OBJECT_TYPE(model)));
377 return;
380 data->dependencies = g_slist_delete_link(data->dependencies, node);
381 g_object_unref(entity);
384 static void
385 changed(AdgModel *model)
387 /* Invalidate all the entities dependent on this model */
388 adg_model_foreach_dependency(model,
389 (AdgEntityCallback) adg_entity_invalidate,
390 NULL);