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.
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
38 * All fields are private and should not be used directly.
39 * Use its public methods instead.
43 #include "adg-model.h"
44 #include "adg-model-private.h"
45 #include "adg-marshal.h"
48 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_model_parent_class)
65 static void dispose (GObject
*object
);
66 static void set_property (GObject
*object
,
71 static void add_dependency (AdgModel
*model
,
73 static void remove_dependency (AdgModel
*model
,
75 static void changed (AdgModel
*model
);
77 static guint signals
[LAST_SIGNAL
] = { 0 };
80 G_DEFINE_ABSTRACT_TYPE(AdgModel
, adg_model
, G_TYPE_OBJECT
);
84 adg_model_class_init(AdgModelClass
*klass
)
86 GObjectClass
*gobject_class
;
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
;
99 klass
->changed
= changed
;
101 param
= g_param_spec_object("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
),
118 G_STRUCT_OFFSET(AdgModelClass
, add_dependency
),
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
131 signals
[REMOVE_DEPENDENCY
] = g_signal_new("remove-dependency",
132 G_OBJECT_CLASS_TYPE(gobject_class
),
134 G_STRUCT_OFFSET(AdgModelClass
, remove_dependency
),
136 adg_marshal_VOID__OBJECT
,
137 G_TYPE_NONE
, 1, ADG_TYPE_ENTITY
);
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
),
149 adg_marshal_VOID__VOID
,
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
),
163 adg_marshal_VOID__VOID
,
168 adg_model_init(AdgModel
*model
)
170 AdgModelPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(model
, ADG_TYPE_MODEL
,
173 data
->dependencies
= NULL
;
179 dispose(GObject
*object
)
182 AdgModelPrivate
*data
;
185 model
= (AdgModel
*) object
;
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
);
201 set_property(GObject
*object
,
202 guint prop_id
, const GValue
*value
, GParamSpec
*pspec
)
204 AdgModel
*model
= (AdgModel
*) object
;
207 case PROP_DEPENDENCY
:
208 adg_model_add_dependency(model
, g_value_get_object(value
));
211 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
217 * adg_model_add_dependency:
218 * @model: an #AdgModel
219 * @entity: an #AdgEntity
222 * This function is only useful in entity implementations.
225 * Emits a #AdgModel::add-dependency signal on @model passing @entity
226 * as argument. This will add a reference to @entity owned by @model.
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
243 * This function is only useful in entity implementations.
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.
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
272 adg_model_dependencies(AdgModel
*model
)
274 AdgModelPrivate
*data
;
276 g_return_val_if_fail(ADG_IS_MODEL(model
), NULL
);
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.
292 adg_model_foreach_dependency(AdgModel
*model
, AdgEntityCallback callback
,
295 AdgModelPrivate
*data
;
299 g_return_if_fail(ADG_IS_MODEL(model
));
300 g_return_if_fail(callback
!= NULL
);
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
;
317 * @model: an #AdgModel
320 * This function is only useful in entity implementations.
323 * Emits the #AdgModel::clear signal on @model.
326 adg_model_clear(AdgModel
*model
)
328 g_return_if_fail(ADG_IS_MODEL(model
));
330 g_signal_emit(model
, signals
[CLEAR
], 0);
335 * @model: an #AdgModel
338 * This function is only useful in entity implementations.
341 * Emits the #AdgModel::changed signal on @model.
344 adg_model_changed(AdgModel
*model
)
346 g_return_if_fail(ADG_IS_MODEL(model
));
348 g_signal_emit(model
, signals
[CHANGED
], 0);
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
);
364 remove_dependency(AdgModel
*model
, AdgEntity
*entity
)
366 AdgModelPrivate
*data
;
370 node
= g_slist_find(data
->dependencies
, entity
);
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
)));
380 data
->dependencies
= g_slist_delete_link(data
->dependencies
, node
);
381 g_object_unref(entity
);
385 changed(AdgModel
*model
)
387 /* Invalidate all the entities dependent on this model */
388 adg_model_foreach_dependency(model
,
389 (AdgEntityCallback
) adg_entity_invalidate
,