1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 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: Base entity for markers
25 * A marker is an entity to be applied at the start or end of a segment.
26 * Typical examples include arrows, ticks, dots and so on.
28 * The #AdgMarker:trail and #AdgMarker:n-segment properties specify the
29 * segment where the marker should be applied. Similarly to the
30 * #AdgStroke type, if the associated trail is destroyed the above
31 * properties are unset.
33 * The local map is used internally to align the marker to the trail end,
34 * so adg_entity_set_local_map() and friends is reserved. Therefore, if
35 * the trail is modified and the marker had no way to know it, you should
36 * call adg_entity_local_changed() to update the marker position.
38 * Use adg_marker_set_pos() to select the position where the marker
39 * should be put: %0 means the start point of the segment while %1
40 * means the end point.
42 * The #AdgMarker:model property and APIs are intended only for marker
43 * implementation purposes.
51 * All fields are privates and should not be used directly.
52 * Use its public methods instead.
59 * @create_model: abstract virtual method that creates a model template for
60 * all the markers used by this class.
62 * The @create_model method must be implemented by any #AdgMarker derived
63 * classes. The derived classes are expected to apply a single model (the one
64 * returned by this method) to every path endings by using different
71 #include "adg-internal.h"
72 #include "adg-model.h"
73 #include "adg-trail.h"
76 #include "adg-marker.h"
77 #include "adg-marker-private.h"
80 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_marker_parent_class)
81 #define _ADG_OLD_ENTITY_CLASS ((AdgEntityClass *) adg_marker_parent_class)
84 G_DEFINE_ABSTRACT_TYPE(AdgMarker
, adg_marker
, ADG_TYPE_ENTITY
)
96 static void _adg_dispose (GObject
*object
);
97 static void _adg_get_property (GObject
*object
,
101 static void _adg_set_property (GObject
*object
,
105 static void _adg_local_changed (AdgEntity
*entity
);
106 static void _adg_invalidate (AdgEntity
*entity
);
107 static void _adg_clear_trail (AdgMarker
*marker
);
108 static gboolean
_adg_set_segment (AdgMarker
*marker
,
111 static AdgModel
* _adg_create_model (AdgMarker
*marker
);
115 adg_marker_class_init(AdgMarkerClass
*klass
)
117 GObjectClass
*gobject_class
;
118 AdgEntityClass
*entity_class
;
121 gobject_class
= (GObjectClass
*) klass
;
122 entity_class
= (AdgEntityClass
*) klass
;
124 g_type_class_add_private(klass
, sizeof(AdgMarkerPrivate
));
126 gobject_class
->dispose
= _adg_dispose
;
127 gobject_class
->set_property
= _adg_set_property
;
128 gobject_class
->get_property
= _adg_get_property
;
130 entity_class
->local_changed
= _adg_local_changed
;
131 entity_class
->invalidate
= _adg_invalidate
;
133 klass
->create_model
= _adg_create_model
;
135 param
= g_param_spec_object("trail",
137 P_("The subject AdgTrail for this marker"),
139 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
);
140 g_object_class_install_property(gobject_class
, PROP_TRAIL
, param
);
142 param
= g_param_spec_uint("n-segment",
144 P_("The segment on trail where this marker should be applied (where 0 means undefined segment, 1 the first segment and so on)"),
147 g_object_class_install_property(gobject_class
, PROP_N_SEGMENT
, param
);
149 param
= g_param_spec_double("pos",
151 P_("The position ratio inside the segment where to put the marker (0 means the start point while 1 means the end point)"),
153 G_PARAM_READWRITE
|G_PARAM_CONSTRUCT
);
154 g_object_class_install_property(gobject_class
, PROP_POS
, param
);
156 param
= g_param_spec_double("size",
158 P_("The size (in global space) of the marker"),
161 g_object_class_install_property(gobject_class
, PROP_SIZE
, param
);
163 param
= g_param_spec_object("model",
165 P_("A general purpose model usable by the marker implementations"),
168 g_object_class_install_property(gobject_class
, PROP_MODEL
, param
);
172 adg_marker_init(AdgMarker
*marker
)
174 AdgMarkerPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(marker
,
179 data
->backup_segment
= NULL
;
180 memset(&data
->segment
, 0, sizeof(data
->segment
));
189 _adg_dispose(GObject
*object
)
191 AdgMarker
*marker
= (AdgMarker
*) object
;
193 adg_marker_set_model(marker
, NULL
);
194 adg_marker_set_segment(marker
, NULL
, 0);
196 if (_ADG_OLD_OBJECT_CLASS
->dispose
)
197 _ADG_OLD_OBJECT_CLASS
->dispose(object
);
202 _adg_get_property(GObject
*object
, guint prop_id
,
203 GValue
*value
, GParamSpec
*pspec
)
205 AdgMarkerPrivate
*data
= ((AdgMarker
*) object
)->data
;
209 g_value_set_object(value
, data
->trail
);
212 g_value_set_uint(value
, data
->n_segment
);
215 g_value_set_double(value
, data
->pos
);
218 g_value_set_double(value
, data
->size
);
221 g_value_set_object(value
, data
->model
);
224 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
230 _adg_set_property(GObject
*object
, guint prop_id
,
231 const GValue
*value
, GParamSpec
*pspec
)
233 AdgMarker
*marker
= (AdgMarker
*) object
;
234 AdgMarkerPrivate
*data
= ((AdgMarker
*) object
)->data
;
239 _adg_set_segment(marker
, g_value_get_object(value
), data
->n_segment
);
242 _adg_set_segment(marker
, data
->trail
, g_value_get_uint(value
));
245 data
->pos
= g_value_get_double(value
);
248 data
->size
= g_value_get_double(value
);
251 old_model
= data
->model
;
252 data
->model
= g_value_get_object(value
);
255 g_object_ref((GObject
*) data
->model
);
256 adg_entity_local_changed((AdgEntity
*) object
);
259 g_object_unref(old_model
);
262 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
269 * adg_marker_set_trail:
270 * @marker: an #AdgMarker
271 * @trail: the new trail to use
273 * Sets the #AdgMarker:trail property to @trail. It is allowed to pass
274 * %NULL to clear the current trail.
276 * This method could fail unexpectedly if the segment index specified
277 * by the #AdgMarker:n-segment property is not present inside the new
278 * segment: if you want to set a new segment it is more convenient to
279 * change both properties (#AdgMarker:trail and #AdgMarker:n-segment)
280 * at once with adg_marker_set_segment().
285 adg_marker_set_trail(AdgMarker
*marker
, AdgTrail
*trail
)
287 g_return_if_fail(ADG_IS_MARKER(marker
));
288 g_object_set(marker
, "trail", trail
, NULL
);
292 * adg_marker_get_trail:
293 * @marker: an #AdgMarker
295 * Gets the trail where this marker should be applied.
296 * The returned object is owned by @marker and should not be
299 * Returns: (transfer none): the requested trail or %NULL on errors.
304 adg_marker_get_trail(AdgMarker
*marker
)
306 AdgMarkerPrivate
*data
;
308 g_return_val_if_fail(ADG_IS_MARKER(marker
), NULL
);
316 * adg_marker_set_n_segment:
317 * @marker: an #AdgMarker
318 * @n_segment: the new segment index
320 * Sets the #AdgMarker:n-segment property to @n_segment. The trail
321 * is unchanged. If you want to set both properties at once (as
322 * usually requested to refer to a specific segment),
323 * adg_marker_set_segment() should be more convenient.
328 adg_marker_set_n_segment(AdgMarker
*marker
, guint n_segment
)
330 g_return_if_fail(ADG_IS_MARKER(marker
));
331 g_object_set(marker
, "n-segment", n_segment
, NULL
);
335 * adg_marker_get_n_segment:
336 * @marker: an #AdgMarker
338 * Returns the segment of the associated trail where this marker
339 * will be applied, where %1 is the first segment.
341 * Returns: an index greather than %0 on success or %0 on errors
346 adg_marker_get_n_segment(AdgMarker
*marker
)
348 AdgMarkerPrivate
*data
;
350 g_return_val_if_fail(ADG_IS_MARKER(marker
), 0);
354 return data
->n_segment
;
358 * adg_marker_set_segment:
359 * @marker: an #AdgMarker
360 * @trail: the #AdgTrail
361 * @n_segment: a segment index
363 * Sets a new segment where the marker should be applied at once.
364 * A dependency between @trail and @marker is added, so when @trail
365 * changes @marker is invalidated.
367 * A callback is added to #AdgTrail::remove-dependency so manually
368 * removing the dependency (such as when @trail is destroyed) will
369 * unlink @marker from it.
374 adg_marker_set_segment(AdgMarker
*marker
, AdgTrail
*trail
, guint n_segment
)
376 g_return_if_fail(ADG_IS_MARKER(marker
));
377 /* To avoid referring to an inexistent trail/n-segment couple, the
378 * "n-segment" property is reset before to avoid segment validation */
379 g_object_set(marker
, "n-segment", 0,
380 "trail", trail
, "n-segment", n_segment
, NULL
);
384 * adg_marker_get_segment:
385 * @marker: an #AdgMarker
388 * This function is only useful in marker implementations.
391 * Gets the segment where the marker will be applied. This segment
392 * is eventually a modified version of the backup segment, after
393 * having applied the marker.
395 * Returns: the segment or %NULL on errors
400 adg_marker_get_segment(AdgMarker
*marker
)
402 AdgMarkerPrivate
*data
;
404 g_return_val_if_fail(ADG_IS_MARKER(marker
), NULL
);
408 return &data
->segment
;
412 * adg_marker_backup_segment:
413 * @marker: an #AdgMarker
416 * This function is only useful in marker implementations.
419 * Duplicates the current subject segment for backup purpose: this
420 * segment can be accessed by adg_marker_get_backup_segment().
421 * Obviously, a current segment should exist (either the
422 * #AdgMarker:trail and #AdgMarker:n-segment properties must be
423 * properly defined) or this method will fail without further
426 * When the subject segment is changed (either by changing
427 * #AdgMarker:trail or #AdgMarker:n-segment) the original segment
428 * is automatically restored.
433 adg_marker_backup_segment(AdgMarker
*marker
)
435 AdgMarkerPrivate
*data
;
437 g_return_if_fail(ADG_IS_MARKER(marker
));
441 if (data
->n_segment
> 0) {
442 g_return_if_fail(data
->trail
!= NULL
);
444 g_free(data
->backup_segment
);
446 /* Backup the segment, if a segment to backup exists */
447 if (adg_trail_put_segment(data
->trail
, data
->n_segment
,
449 data
->backup_segment
= cpml_segment_deep_dup(&data
->segment
);
454 * adg_marker_get_backup_segment:
455 * @marker: an #AdgMarker
458 * This function is only useful in marker implementations.
461 * Gets the original segment where the marker has been applied.
462 * Applying a marker could modify the underlying trail, usually
463 * by trimming the original segment of a #AdgMarker:size dependent
464 * length from the ends. The marker instance holds a copy of the
465 * original segment, generated by adg_marker_backup_segment(),
466 * to be used in recomputation, for example when the marker
469 * When the subject segment is changed (either by changing
470 * #AdgMarker:trail or #AdgMarker:n-segment) the original segment
471 * is automatically restored.
473 * Returns: the original segment or %NULL on errors
478 adg_marker_get_backup_segment(AdgMarker
*marker
)
480 AdgMarkerPrivate
*data
;
482 g_return_val_if_fail(ADG_IS_MARKER(marker
), NULL
);
486 return data
->backup_segment
;
490 * adg_marker_set_pos:
491 * @marker: an #AdgMarker
494 * Sets a new position on @marker. Check out adg_marker_get_pos() for
495 * details on what @pos represents.
500 adg_marker_set_pos(AdgMarker
*marker
, gdouble pos
)
502 g_return_if_fail(ADG_IS_MARKER(marker
));
503 g_object_set(marker
, "pos", pos
, NULL
);
507 * adg_marker_get_pos:
508 * @marker: an #AdgMarker
510 * Gets the current position of @marker. The returned value is a ratio
511 * position referred to the segment associated to @marker: %0 means the
512 * start point and %1 means the end point of the segment.
514 * Returns: the marker position
519 adg_marker_get_pos(AdgMarker
*marker
)
521 AdgMarkerPrivate
*data
;
523 g_return_val_if_fail(ADG_IS_MARKER(marker
), 0);
531 * adg_marker_set_size:
532 * @marker: an #AdgMarker
533 * @size: the new size
535 * Sets a new size on @marker. The @size is an implementation-dependent
536 * property: it has meaning only when used by an #AdgMarker derived type.
541 adg_marker_set_size(AdgMarker
*marker
, gdouble size
)
543 g_return_if_fail(ADG_IS_MARKER(marker
));
544 g_object_set(marker
, "size", size
, NULL
);
548 * adg_marker_get_size:
549 * @marker: an #AdgMarker
551 * Gets the current size of @marker.
553 * Returns: the marker size, in global space
558 adg_marker_get_size(AdgMarker
*marker
)
560 AdgMarkerPrivate
*data
;
562 g_return_val_if_fail(ADG_IS_MARKER(marker
), 0);
570 * adg_marker_set_model:
571 * @marker: an #AdgMarker
572 * @model: a new #AdgModel
575 * This function is only useful in marker implementations.
578 * Sets a new model for @marker. The reference to the old model (if an
579 * old model was present) is dropped while a new reference is added to
585 adg_marker_set_model(AdgMarker
*marker
, AdgModel
*model
)
587 g_return_if_fail(ADG_IS_MARKER(marker
));
588 g_object_set(marker
, "model", model
, NULL
);
592 * adg_marker_get_model:
593 * @marker: an #AdgMarker
596 * This function is only useful in marker implementations.
599 * Gets the current model of @marker. This is an accessor method:
600 * if you need to get the model for rendering, use adg_marker_model()
601 * instead. The returned object is owned by @marker and should not be
604 * Returns: (transfer none): the cached model or %NULL on errors.
609 adg_marker_get_model(AdgMarker
*marker
)
611 AdgMarkerPrivate
*data
;
613 g_return_val_if_fail(ADG_IS_MARKER(marker
), NULL
);
622 * @marker: an #AdgMarker
625 * This function is only useful in marker implementations.
628 * Gets the model of @marker. If the model is not found, it is
629 * automatically created by calling the create_model() virtual method.
630 * The returned object is owned by @marker and should not be
633 * Returns: (transfer none): the current model or %NULL on errors.
638 adg_marker_model(AdgMarker
*marker
)
640 AdgMarkerPrivate
*data
;
642 g_return_val_if_fail(ADG_IS_MARKER(marker
), NULL
);
646 if (data
->model
== NULL
) {
647 /* Model not found: regenerate it */
648 AdgMarkerClass
*marker_class
= ADG_MARKER_GET_CLASS(marker
);
650 if (marker_class
->create_model
)
651 adg_marker_set_model(marker
, marker_class
->create_model(marker
));
659 _adg_local_changed(AdgEntity
*entity
)
661 AdgMarkerPrivate
*data
;
666 data
= ((AdgMarker
*) entity
)->data
;
667 if (data
->trail
== NULL
)
670 cpml_segment_put_pair_at(&data
->segment
, data
->pos
, &pair
);
671 cpml_segment_put_vector_at(&data
->segment
, data
->pos
, &vector
);
672 cpml_vector_set_length(&vector
, data
->size
);
674 if (data
->pos
> 0.5) {
675 vector
.x
= -vector
.x
;
676 vector
.y
= -vector
.y
;
679 cairo_matrix_init(&map
, vector
.x
, vector
.y
,
680 -vector
.y
, vector
.x
, pair
.x
, pair
.y
);
682 adg_entity_set_local_map(entity
, &map
);
684 if (_ADG_OLD_ENTITY_CLASS
->local_changed
)
685 _ADG_OLD_ENTITY_CLASS
->local_changed(entity
);
689 _adg_invalidate(AdgEntity
*entity
)
691 adg_marker_set_model((AdgMarker
*) entity
, NULL
);
696 _adg_clear_trail(AdgMarker
*marker
)
698 AdgMarkerPrivate
*data
= marker
->data
;
700 if (data
->trail
&& data
->backup_segment
) {
701 /* Restore the original segment in the old trail */
702 cpml_segment_deep_copy(&data
->segment
, data
->backup_segment
);
703 g_free(data
->backup_segment
);
704 data
->backup_segment
= NULL
;
712 _adg_set_segment(AdgMarker
*marker
, AdgTrail
*trail
, guint n_segment
)
714 AdgMarkerPrivate
*data
;
715 CpmlSegment segment
= { 0 };
717 g_return_val_if_fail(trail
== NULL
|| ADG_IS_TRAIL(trail
), FALSE
);
719 /* Segment validation, but only if n_segment is specified */
720 if (trail
&& n_segment
> 0 && !adg_trail_put_segment(trail
, n_segment
, &segment
))
725 /* Do not try to cache results! Although @trail and @n_segment
726 * could be the same, the internal CpmlSegment could change.
727 * This is the case when AdgLDim arranges the layout and changes
728 * the path data (to force outside arrows for example) reusing
729 * the same CpmlPath. In other words, do not do this:
731 * if (trail == data->trail && n_segment == data->n_segment)
734 * Incidentally, on a 64 bit platform this issue has been never
735 * exposed. Avoiding the cache will solve the issue 33:
737 * http://dev.entidi.com/p/adg/issues/33/
740 if (trail
!= data
->trail
) {
741 AdgEntity
*entity
= (AdgEntity
*) marker
;
744 adg_model_remove_dependency((AdgModel
*) data
->trail
, entity
);
749 adg_model_add_dependency((AdgModel
*) trail
, entity
);
750 g_signal_connect_swapped(trail
, "remove-dependency",
751 G_CALLBACK(_adg_clear_trail
), marker
);
755 cpml_segment_copy(&data
->segment
, &segment
);
756 data
->n_segment
= n_segment
;
762 _adg_create_model(AdgMarker
*marker
)
764 g_warning(_("%s: `create_model' method not implemented for type `%s'"),
765 G_STRLOC
, g_type_name(G_OBJECT_TYPE(marker
)));