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"
47 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_model_parent_class)
63 static void dispose (GObject
*object
);
64 static void set_property (GObject
*object
,
69 static GSList
* get_dependencies (AdgModel
*model
);
70 static void add_dependency (AdgModel
*model
,
72 static void remove_dependency (AdgModel
*model
,
74 static void changed (AdgModel
*model
);
76 static guint signals
[LAST_SIGNAL
] = { 0 };
79 G_DEFINE_ABSTRACT_TYPE(AdgModel
, adg_model
, G_TYPE_OBJECT
);
83 adg_model_class_init(AdgModelClass
*klass
)
85 GObjectClass
*gobject_class
;
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("dependency",
102 P_("Can be used to add a new dependency from this model (this entity 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
),
117 G_STRUCT_OFFSET(AdgModelClass
, add_dependency
),
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
130 signals
[REMOVE_DEPENDENCY
] = g_signal_new("remove-dependency",
131 G_OBJECT_CLASS_TYPE(gobject_class
),
133 G_STRUCT_OFFSET(AdgModelClass
, remove_dependency
),
135 g_cclosure_marshal_VOID__OBJECT
,
136 G_TYPE_NONE
, 1, ADG_TYPE_ENTITY
);
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
),
149 g_cclosure_marshal_VOID__VOID
,
154 adg_model_init(AdgModel
*model
)
156 AdgModelPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(model
, ADG_TYPE_MODEL
,
159 data
->dependencies
= NULL
;
165 dispose(GObject
*object
)
168 AdgModelPrivate
*data
;
170 model
= (AdgModel
*) object
;
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
);
185 set_property(GObject
*object
,
186 guint prop_id
, const GValue
*value
, GParamSpec
*pspec
)
188 AdgModel
*model
= (AdgModel
*) object
;
191 case PROP_DEPENDENCY
:
192 adg_model_add_dependency(model
, g_value_get_object(value
));
195 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
201 * adg_model_add_dependency:
202 * @model: an #AdgModel
203 * @entity: an #AdgEntity
206 * This function is only useful in entity implementations.
209 * Emits a #AdgModel::add-dependency signal on @model passing @entity
210 * as argument. This will add a reference to @entity owned by @model.
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
227 * This function is only useful in entity implementations.
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.
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
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:
273 * void callback(AdgEntity *entity, gpointer user_data);
277 adg_model_foreach_dependency(AdgModel
*model
, GCallback callback
,
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
,
292 dependencies
= g_slist_delete_link(dependencies
, dependencies
);
298 * @model: an #AdgModel
301 * This function is only useful in entity implementations.
304 * Emits the #AdgModel::changed signal on @model.
307 adg_model_changed(AdgModel
*model
)
309 g_return_if_fail(ADG_IS_MODEL(model
));
311 g_signal_emit(model
, signals
[CHANGED
], 0);
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
);
327 remove_dependency(AdgModel
*model
, AdgEntity
*entity
)
329 AdgModelPrivate
*data
;
333 node
= g_slist_find(data
->dependencies
, entity
);
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
)));
343 data
->dependencies
= g_slist_delete_link(data
->dependencies
, node
);
344 g_object_unref(entity
);
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
);
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
);