[AdgStroke] Using adg_entity_apply_local_matrix()
[adg.git] / adg / adg-model.c
blobb68b1dd51045afb304fe4477fbf6878f23eac1e4
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-intl.h"
47 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_model_parent_class)
50 enum {
51 PROP_0,
52 PROP_DEPENDENCY
55 enum {
56 ADD_DEPENDENCY,
57 REMOVE_DEPENDENCY,
58 CHANGED,
59 LAST_SIGNAL
63 static void dispose (GObject *object);
64 static void set_property (GObject *object,
65 guint prop_id,
66 const GValue *value,
67 GParamSpec *pspec);
69 static GSList * get_dependencies (AdgModel *model);
70 static void add_dependency (AdgModel *model,
71 AdgEntity *entity);
72 static void remove_dependency (AdgModel *model,
73 AdgEntity *entity);
74 static void changed (AdgModel *model);
76 static guint signals[LAST_SIGNAL] = { 0 };
79 G_DEFINE_ABSTRACT_TYPE(AdgModel, adg_model, G_TYPE_OBJECT);
82 static void
83 adg_model_class_init(AdgModelClass *klass)
85 GObjectClass *gobject_class;
86 GParamSpec *param;
88 gobject_class = (GObjectClass *) klass;
90 g_type_class_add_private(klass, sizeof(AdgModelPrivate));
92 gobject_class->dispose = dispose;
93 gobject_class->set_property = set_property;
95 klass->get_dependencies = get_dependencies;
96 klass->add_dependency = add_dependency;
97 klass->remove_dependency = remove_dependency;
98 klass->changed = changed;
100 param = g_param_spec_object("entity",
101 P_("Entity"),
102 P_("Can be used to associate a new entity to this model (will be invalidated on model changed)"),
103 ADG_TYPE_ENTITY, G_PARAM_WRITABLE);
104 g_object_class_install_property(gobject_class, PROP_DEPENDENCY, param);
107 * AdgModel::add-dependency:
108 * @model: an #AdgModel
109 * @entity: an #AdgEntity that depends on @model
111 * Adds @entity to @model. After that @entity will depend on @model,
112 * that is #AdgModel::changed on @model will invalidate @entity.
114 signals[ADD_DEPENDENCY] = g_signal_new("add-dependency",
115 G_OBJECT_CLASS_TYPE(gobject_class),
116 G_SIGNAL_RUN_FIRST,
117 G_STRUCT_OFFSET(AdgModelClass, add_dependency),
118 NULL, NULL,
119 g_cclosure_marshal_VOID__OBJECT,
120 G_TYPE_NONE, 1, ADG_TYPE_ENTITY);
123 * AdgModel::remove-dependency:
124 * @model: an #AdgModel
125 * @entity: the #AdgEntity that does not depend on @model anymore
127 * Removes the @entity from @model, that is @entity will not depend
128 * on @model anymore.
130 signals[REMOVE_DEPENDENCY] = g_signal_new("remove-dependency",
131 G_OBJECT_CLASS_TYPE(gobject_class),
132 G_SIGNAL_RUN_FIRST,
133 G_STRUCT_OFFSET(AdgModelClass, remove_dependency),
134 NULL, NULL,
135 g_cclosure_marshal_VOID__OBJECT,
136 G_TYPE_NONE, 1, ADG_TYPE_ENTITY);
139 * AdgModel::changed:
140 * @model: an #AdgModel
142 * Notificates that the model has changed. By default, the model
143 * cache is invalidated.
145 signals[CHANGED] = g_signal_new("changed", ADG_TYPE_MODEL,
146 G_SIGNAL_RUN_LAST|G_SIGNAL_NO_RECURSE,
147 G_STRUCT_OFFSET(AdgModelClass, changed),
148 NULL, NULL,
149 g_cclosure_marshal_VOID__VOID,
150 G_TYPE_NONE, 0);
153 static void
154 adg_model_init(AdgModel *model)
156 AdgModelPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(model, ADG_TYPE_MODEL,
157 AdgModelPrivate);
159 data->dependencies = NULL;
161 model->data = data;
164 static void
165 dispose(GObject *object)
167 AdgModel *model;
168 AdgModelPrivate *data;
170 model = (AdgModel *) object;
171 data = model->data;
173 /* Remove all the dependencies from the model: this will emit
174 * a "remove" signal for every dependency and will drop all the
175 * references from those entities to this model */
176 while (data->dependencies != NULL)
177 adg_model_remove_dependency(model,
178 (AdgEntity *) data->dependencies->data);
180 if (PARENT_OBJECT_CLASS->dispose != NULL)
181 PARENT_OBJECT_CLASS->dispose(object);
184 static void
185 set_property(GObject *object,
186 guint prop_id, const GValue *value, GParamSpec *pspec)
188 AdgModel *model = (AdgModel *) object;
190 switch (prop_id) {
191 case PROP_DEPENDENCY:
192 adg_model_add_dependency(model, g_value_get_object(value));
193 break;
194 default:
195 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
201 * adg_model_add_dependency:
202 * @model: an #AdgModel
203 * @entity: an #AdgEntity
205 * <note><para>
206 * This function is only useful in entity implementations.
207 * </para></note>
209 * Emits a #AdgModel::add-dependency signal on @model passing @entity
210 * as argument. This will add a reference to @entity owned by @model.
212 void
213 adg_model_add_dependency(AdgModel *model, AdgEntity *entity)
215 g_return_if_fail(ADG_IS_MODEL(model));
216 g_return_if_fail(ADG_IS_ENTITY(entity));
218 g_signal_emit(model, signals[ADD_DEPENDENCY], 0, entity);
222 * adg_model_remove_dependency:
223 * @model: an #AdgModel
224 * @entity: an #AdgEntity
226 * <note><para>
227 * This function is only useful in entity implementations.
228 * </para></note>
230 * Emits a #AdgModel::remove-dependency signal on @model passing
231 * @entity as argument. @entity must be inside @model.
233 * Note that @model will own a reference to @entity and it
234 * may be the last reference held: this means removing an entity
235 * from the model can destroy it.
237 void
238 adg_model_remove_dependency(AdgModel *model, AdgEntity *entity)
240 g_return_if_fail(ADG_IS_MODEL(model));
241 g_return_if_fail(ADG_IS_ENTITY(entity));
243 g_signal_emit(model, signals[REMOVE_DEPENDENCY], 0, entity);
247 * adg_model_get_dependencies:
248 * @model: an #AdgModel
250 * Gets a list of entities dependent on @model.
251 * This list must be freed with g_slist_free() when no longer user.
253 * Returns: a newly allocated #GSList or %NULL on error
255 GSList *
256 adg_model_get_dependencies(AdgModel *model)
258 g_return_val_if_fail(ADG_IS_MODEL(model), NULL);
260 return ADG_MODEL_GET_CLASS(model)->get_dependencies(model);
264 * adg_model_foreach_dependency:
265 * @model: an #AdgModel
266 * @callback: a callback
267 * @user_data: callback user data
269 * Invokes @callback on each entity linked to @model.
270 * The callback should be declared as:
272 * |[
273 * void callback(AdgEntity *entity, gpointer user_data);
274 * ]|
276 void
277 adg_model_foreach_dependency(AdgModel *model, GCallback callback,
278 gpointer user_data)
280 GSList *dependencies;
282 g_return_if_fail(ADG_IS_MODEL(model));
283 g_return_if_fail(callback != NULL);
285 dependencies = adg_model_get_dependencies(model);
287 while (dependencies) {
288 if (dependencies->data)
289 ((void (*) (gpointer, gpointer)) callback) (dependencies->data,
290 user_data);
292 dependencies = g_slist_delete_link(dependencies, dependencies);
297 * adg_model_changed:
298 * @model: an #AdgModel
300 * <note><para>
301 * This function is only useful in entity implementations.
302 * </para></note>
304 * Emits the #AdgModel::changed signal on @model.
306 void
307 adg_model_changed(AdgModel *model)
309 g_return_if_fail(ADG_IS_MODEL(model));
311 g_signal_emit(model, signals[CHANGED], 0);
315 static void
316 add_dependency(AdgModel *model, AdgEntity *entity)
318 AdgModelPrivate *data = model->data;
320 /* The prepend operation is more efficient */
321 data->dependencies = g_slist_prepend(data->dependencies, entity);
323 g_object_ref(entity);
326 static void
327 remove_dependency(AdgModel *model, AdgEntity *entity)
329 AdgModelPrivate *data;
330 GSList *node;
332 data = model->data;
333 node = g_slist_find(data->dependencies, entity);
335 if (node == NULL) {
336 g_warning("Attempting to remove an entity with type %s from a "
337 "model of type %s, but the entity is not present.",
338 g_type_name(G_OBJECT_TYPE(entity)),
339 g_type_name(G_OBJECT_TYPE(model)));
340 return;
343 data->dependencies = g_slist_delete_link(data->dependencies, node);
344 g_object_unref(entity);
347 static GSList *
348 get_dependencies(AdgModel *model)
350 AdgModelPrivate *data = model->data;
352 /* The NULL case is yet managed by GLib */
353 return g_slist_copy(data->dependencies);
356 static void
357 changed(AdgModel *model)
359 /* Invalidate all the entities dependent on this model */
360 adg_model_foreach_dependency(model,
361 G_CALLBACK(adg_entity_invalidate), NULL);