1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2015 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: Root abstract class for all dimension entities
25 * The #AdgDim class is the base stub of all the dimension entities.
33 * All fields are private and should not be used directly.
34 * Use its public methods instead.
41 * @quote_angle: virtual method that must return the rotation angle of the
42 * quote (in radians) of the current dimension.
43 * @default_value: abstract virtual method that must return the default value
44 * (as a newly allocated string to be freed with g_free()) of
45 * the current dimension.
47 * The default implementation of @quote_angle flips the quote if it should be
48 * rotated in the bottom right half of the circle, that is:
50 * <informalexample><programlisting>
51 * if 1/3 PI <= angle <= -3/4 PI; then angle += PI.
52 * </programlisting></informalexample>
54 * The virtual method @default_value instead *must* be implemented by any
55 * derived class. The default implementation will trigger an error if called.
61 #include "adg-internal.h"
62 #include "adg-text-internal.h"
64 #include "adg-container.h"
65 #include "adg-alignment.h"
66 #include "adg-model.h"
67 #include "adg-trail.h"
68 #include "adg-point.h"
69 #include "adg-marker.h"
70 #include "adg-dim-style.h"
71 #include "adg-dress.h"
72 #include "adg-param-dress.h"
75 #include "adg-dim-private.h"
81 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_dim_parent_class)
82 #define _ADG_OLD_ENTITY_CLASS ((AdgEntityClass *) adg_dim_parent_class)
84 /* A convenience macro for ORing two AdgThreeState values */
85 #define OR_3S(a,b) ( \
86 ((a) == ADG_THREE_STATE_ON || (b) == ADG_THREE_STATE_ON) ? ADG_THREE_STATE_ON : \
87 ((a) == ADG_THREE_STATE_UNKNOWN && (b) == ADG_THREE_STATE_UNKNOWN) ? ADG_THREE_STATE_UNKNOWN : \
91 G_DEFINE_ABSTRACT_TYPE(AdgDim
, adg_dim
, ADG_TYPE_ENTITY
)
108 static void _adg_dispose (GObject
*object
);
109 static void _adg_finalize (GObject
*object
);
110 static void _adg_get_property (GObject
*object
,
114 static void _adg_set_property (GObject
*object
,
118 static void _adg_global_changed (AdgEntity
*entity
);
119 static void _adg_local_changed (AdgEntity
*entity
);
120 static void _adg_invalidate (AdgEntity
*entity
);
121 static void _adg_arrange (AdgEntity
*entity
);
122 static gchar
* _adg_default_value (AdgDim
*dim
);
123 static gdouble
_adg_quote_angle (gdouble angle
);
124 static gboolean
_adg_set_outside (AdgDim
*dim
,
125 AdgThreeState outside
);
126 static gboolean
_adg_set_detached (AdgDim
*dim
,
127 AdgThreeState detached
);
128 static gboolean
_adg_set_value (AdgDim
*dim
,
130 static gboolean
_adg_set_min (AdgDim
*dim
,
132 static gboolean
_adg_set_max (AdgDim
*dim
,
134 static gboolean
_adg_replace (const GMatchInfo
*match_info
,
137 static gchar
* _adg_text_expand (AdgDimReplaceData
*data
);
141 adg_dim_class_init(AdgDimClass
*klass
)
143 GObjectClass
*gobject_class
;
144 AdgEntityClass
*entity_class
;
147 gobject_class
= (GObjectClass
*) klass
;
148 entity_class
= (AdgEntityClass
*) klass
;
150 g_type_class_add_private(klass
, sizeof(AdgDimPrivate
));
152 gobject_class
->dispose
= _adg_dispose
;
153 gobject_class
->finalize
= _adg_finalize
;
154 gobject_class
->get_property
= _adg_get_property
;
155 gobject_class
->set_property
= _adg_set_property
;
157 entity_class
->global_changed
= _adg_global_changed
;
158 entity_class
->local_changed
= _adg_local_changed
;
159 entity_class
->invalidate
= _adg_invalidate
;
160 entity_class
->arrange
= _adg_arrange
;
162 klass
->quote_angle
= _adg_quote_angle
;
163 klass
->default_value
= _adg_default_value
;
165 param
= adg_param_spec_dress("dim-dress",
166 P_("Dimension Dress"),
167 P_("The dress to use for rendering this dimension"),
170 g_object_class_install_property(gobject_class
, PROP_DIM_DRESS
, param
);
172 param
= g_param_spec_boxed("ref1",
173 P_("First Reference"),
174 P_("First reference point of the dimension"),
177 g_object_class_install_property(gobject_class
, PROP_REF1
, param
);
179 param
= g_param_spec_boxed("ref2",
180 P_("Second Reference"),
181 P_("Second reference point of the dimension"),
184 g_object_class_install_property(gobject_class
, PROP_REF2
, param
);
186 param
= g_param_spec_boxed("pos",
188 P_("The reference position of the quote: it will be combined with \"level\" to get the real quote position"),
191 g_object_class_install_property(gobject_class
, PROP_POS
, param
);
193 param
= g_param_spec_double("level",
195 P_("The dimension level, that is the factor to multiply the baseline spacing (defined in the dimension style) to get the offset from pos where the quote should be rendered"),
196 -G_MAXDOUBLE
, G_MAXDOUBLE
, 1.0,
198 g_object_class_install_property(gobject_class
, PROP_LEVEL
, param
);
200 param
= g_param_spec_enum("outside",
202 P_("Whether the arrows must be inside the extension lines (ADG_THREE_STATE_OFF), must be extended outside the extension lines (ADG_THREE_STATE_ON) or should be automatically handled depending on the available space"),
203 ADG_TYPE_THREE_STATE
, ADG_THREE_STATE_UNKNOWN
,
205 g_object_class_install_property(gobject_class
, PROP_OUTSIDE
, param
);
207 param
= g_param_spec_enum("detached",
208 P_("Detached Quote"),
209 P_("Where the quote must be positioned: in the middle of the base line (ADG_THREE_STATE_OFF), near the pos point (ADG_THREE_STATE_ON) or should be automatically deducted depending on the available space"),
210 ADG_TYPE_THREE_STATE
, ADG_THREE_STATE_UNKNOWN
,
212 g_object_class_install_property(gobject_class
, PROP_DETACHED
, param
);
214 param
= g_param_spec_string("value",
215 P_("Value Template"),
216 P_("The template string to be used for generating the set value of the quote"),
218 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
);
219 g_object_class_install_property(gobject_class
, PROP_VALUE
, param
);
221 param
= g_param_spec_string("min",
222 P_("Minimum Value or Low Tolerance"),
223 P_("The minimum value allowed or the lowest tolerance from value (depending of the dimension style): set to NULL to suppress"),
226 g_object_class_install_property(gobject_class
, PROP_MIN
, param
);
228 param
= g_param_spec_string("max",
229 P_("Maximum Value or High Tolerance"),
230 P_("The maximum value allowed or the highest tolerance from value (depending of the dimension style): set to NULL to suppress"),
233 g_object_class_install_property(gobject_class
, PROP_MAX
, param
);
237 adg_dim_init(AdgDim
*dim
)
239 AdgDimPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(dim
, ADG_TYPE_DIM
,
242 data
->dim_dress
= ADG_DRESS_DIMENSION
;
247 data
->outside
= ADG_THREE_STATE_UNKNOWN
;
248 data
->detached
= ADG_THREE_STATE_UNKNOWN
;
253 /* This one is G_PARAM_CONSTRUCT, so set by property inizialization */
261 _adg_dispose(GObject
*object
)
266 entity
= (AdgEntity
*) object
;
267 data
= ((AdgDim
*) object
)->data
;
269 if (data
->quote
.entity
) {
270 g_object_unref(data
->quote
.entity
);
271 data
->quote
.entity
= NULL
;
275 data
->ref1
= adg_entity_point(entity
, data
->ref1
, NULL
);
278 data
->ref2
= adg_entity_point(entity
, data
->ref2
, NULL
);
281 data
->pos
= adg_entity_point(entity
, data
->pos
, NULL
);
283 if (_ADG_OLD_OBJECT_CLASS
->dispose
)
284 _ADG_OLD_OBJECT_CLASS
->dispose(object
);
288 _adg_finalize(GObject
*object
)
290 AdgDimPrivate
*data
= ((AdgDim
*) object
)->data
;
296 if (_ADG_OLD_OBJECT_CLASS
->finalize
)
297 _ADG_OLD_OBJECT_CLASS
->finalize(object
);
301 _adg_get_property(GObject
*object
, guint prop_id
,
302 GValue
*value
, GParamSpec
*pspec
)
304 AdgDimPrivate
*data
= ((AdgDim
*) object
)->data
;
308 g_value_set_enum(value
, data
->dim_dress
);
311 g_value_set_boxed(value
, data
->ref1
);
314 g_value_set_boxed(value
, data
->ref2
);
317 g_value_set_boxed(value
, data
->pos
);
320 g_value_set_double(value
, data
->level
);
323 g_value_set_enum(value
, data
->outside
);
326 g_value_set_enum(value
, data
->detached
);
329 g_value_set_string(value
, data
->value
);
332 g_value_set_string(value
, data
->min
);
335 g_value_set_string(value
, data
->max
);
338 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
344 _adg_set_property(GObject
*object
, guint prop_id
,
345 const GValue
*value
, GParamSpec
*pspec
)
351 entity
= (AdgEntity
*) object
;
352 dim
= (AdgDim
*) object
;
357 data
->dim_dress
= g_value_get_enum(value
);
360 data
->ref1
= adg_entity_point(entity
, data
->ref1
,
361 g_value_get_boxed(value
));
364 data
->ref2
= adg_entity_point(entity
, data
->ref2
,
365 g_value_get_boxed(value
));
368 data
->pos
= adg_entity_point(entity
, data
->pos
,
369 g_value_get_boxed(value
));
372 data
->level
= g_value_get_double(value
);
375 _adg_set_outside(dim
, g_value_get_enum(value
));
378 _adg_set_detached(dim
, g_value_get_enum(value
));
381 _adg_set_value(dim
, g_value_get_string(value
));
384 _adg_set_min(dim
, g_value_get_string(value
));
387 _adg_set_max(dim
, g_value_get_string(value
));
390 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
397 * adg_dim_set_dim_dress:
399 * @dress: the new #AdgDress to use
401 * Sets a new dimension dress to @dim. The new dress must be
402 * related to the original dress for this property: you cannot
403 * set a dress used for line styles to a dress managing fonts.
405 * The check is done by calling adg_dress_are_related() with
406 * @dress and the previous dress as arguments. Check out its
407 * documentation for details on what is a related dress.
412 adg_dim_set_dim_dress(AdgDim
*dim
, AdgDress dress
)
414 g_return_if_fail(ADG_IS_DIM(dim
));
415 g_object_set(dim
, "dim-dress", dress
, NULL
);
419 * adg_dim_get_dim_dress:
422 * Gets the dimension dress to be used in rendering @dim.
424 * Returns: (transfer none): the current dimension dress.
429 adg_dim_get_dim_dress(AdgDim
*dim
)
433 g_return_val_if_fail(ADG_IS_DIM(dim
), ADG_DRESS_UNDEFINED
);
437 return data
->dim_dress
;
443 * @ref1: the new point to use as first reference
445 * Sets the #AdgDim:ref1 property to @ref1. The old point
446 * is silently discarded, unreferencing its model if that
447 * point was bound to a named pair (hence, possibly destroying
448 * the model if this was the last reference).
450 * @ref1 can be <constant>NULL</constant>, in which case the
451 * point is destroyed.
456 adg_dim_set_ref1(AdgDim
*dim
, const AdgPoint
*ref1
)
458 g_return_if_fail(ADG_IS_DIM(dim
));
459 g_object_set(dim
, "ref1", ref1
, NULL
);
463 * adg_dim_set_ref1_explicit:
465 * @x: x coordinate of the first reference point
466 * @y: y coordinate of the first reference point
468 * Sets the #AdgDim:ref1 property to the (@x, @y) explicit
469 * coordinates. The old point is silently discarded,
470 * unreferencing its model if that point was bound to a named
471 * pair (hence, possibly destroying the model if this was the
477 adg_dim_set_ref1_explicit(AdgDim
*dim
, gdouble x
, gdouble y
)
479 AdgPoint
*point
= adg_point_new();
481 adg_point_set_pair_explicit(point
, x
, y
);
482 adg_dim_set_ref1(dim
, point
);
484 adg_point_destroy(point
);
488 * adg_dim_set_ref1_from_pair:
490 * @ref1: the coordinates pair of the first reference point
492 * Convenient function to set the #AdgDim:ref1 property using a
493 * pair instead of explicit coordinates.
498 adg_dim_set_ref1_from_pair(AdgDim
*dim
, const CpmlPair
*ref1
)
500 g_return_if_fail(ref1
!= NULL
);
502 adg_dim_set_ref1_explicit(dim
, ref1
->x
, ref1
->y
);
506 * adg_dim_set_ref1_from_model:
508 * @model: the source #AdgModel
509 * @ref1: a named pair in @model
511 * Binds #AdgDim:ref1 to the @ref1 named pair of @model. If @model
512 * is <constant>NULL</constant>, the point will be unset. In any case,
513 * the old point is silently discarded, unreferencing its model
514 * if that point was bound to a named pair (hence, possibly destroying
515 * the model if this was the last reference).
517 * The assignment is lazy so @ref1 could be not be present in @model.
518 * Anyway, at the first access to this point an error will be raised
519 * if the named pair is still missing.
524 adg_dim_set_ref1_from_model(AdgDim
*dim
, AdgModel
*model
, const gchar
*ref1
)
526 AdgPoint
*point
= adg_point_new();
528 adg_point_set_pair_from_model(point
, model
, ref1
);
529 adg_dim_set_ref1(dim
, point
);
531 adg_point_destroy(point
);
538 * Gets the #AdgDim:ref1 point of @dim.
540 * The returned point is internally owned and must not be freed
541 * or modified. Anyway it is not const because a call to
542 * adg_point_update() with the returned value must be able to
543 * modify the internal cache.
545 * Returns: (transfer none): the first reference point.
550 adg_dim_get_ref1(AdgDim
*dim
)
554 g_return_val_if_fail(ADG_IS_DIM(dim
), NULL
);
564 * @ref2: the new point to use as second reference
566 * Sets the #AdgDim:ref2 property to @ref2. The old point
567 * is silently discarded, unreferencing its model if that
568 * point was bound to a named pair (hence, possibly destroying
569 * the model if it was the last reference).
571 * @ref2 can be <constant>NULL</constant>, in which case
572 * the point is destroyed.
577 adg_dim_set_ref2(AdgDim
*dim
, const AdgPoint
*ref2
)
579 g_return_if_fail(ADG_IS_DIM(dim
));
580 g_object_set(dim
, "ref2", ref2
, NULL
);
584 * adg_dim_set_ref2_explicit:
586 * @x: x coordinate of the second reference point
587 * @y: y coordinate of the second reference point
589 * Sets the #AdgDim:ref2 property to the (@x, @y) explicit
590 * coordinates. The old point is silently discarded,
591 * unreferencing its model if that point was bound to a named
592 * pair (hence, possibly destroying the model if this was the
598 adg_dim_set_ref2_explicit(AdgDim
*dim
, gdouble x
, gdouble y
)
600 AdgPoint
*point
= adg_point_new();
602 adg_point_set_pair_explicit(point
, x
, y
);
603 adg_dim_set_ref2(dim
, point
);
605 adg_point_destroy(point
);
609 * adg_dim_set_ref2_from_pair:
611 * @ref2: the coordinates pair of the second reference point
613 * Convenient function to set the #AdgDim:ref2 property using a
614 * pair instead of explicit coordinates.
619 adg_dim_set_ref2_from_pair(AdgDim
*dim
, const CpmlPair
*ref2
)
621 g_return_if_fail(ref2
!= NULL
);
623 adg_dim_set_ref2_explicit(dim
, ref2
->x
, ref2
->y
);
627 * adg_dim_set_ref2_from_model:
629 * @model: the source #AdgModel
630 * @ref2: a named pair in @model
632 * Binds #AdgDim:ref2 to the @ref2 named pair of @model. If @model
633 * is <constant>NULL</constant>, the point will be unset. In any
634 * case, the old point is silently discarded, unreferencing its
635 * model if that point was bound to a named pair (hence, possibly
636 * destroying the model if this was the last reference).
638 * The assignment is lazy so @ref2 could be not be present in @model.
639 * Anyway, at the first access to this point an error will be raised
640 * if the named pair is still missing.
645 adg_dim_set_ref2_from_model(AdgDim
*dim
, AdgModel
*model
, const gchar
*ref2
)
647 AdgPoint
*point
= adg_point_new();
649 adg_point_set_pair_from_model(point
, model
, ref2
);
650 adg_dim_set_ref2(dim
, point
);
652 adg_point_destroy(point
);
659 * Gets the #AdgDim:ref2 point of @dim.
661 * The returned point is internally owned and must not be freed
662 * or modified. Anyway it is not const because a call to
663 * adg_point_update() with the returned value must be able to
664 * modify the internal cache.
666 * Returns: (transfer none): the second reference point.
671 adg_dim_get_ref2(AdgDim
*dim
)
675 g_return_val_if_fail(ADG_IS_DIM(dim
), NULL
);
685 * @pos: the new point to use as position
687 * Sets the #AdgDim:pos property of @dim to @pos. The old point
688 * is silently discarded, unreferencing its model if that
689 * point was bound to a named pair (hence, possibly destroying
690 * the model if it was the last reference).
692 * @pos can be <constant>NULL</constant>, in which case the
693 * point is destroyed.
698 adg_dim_set_pos(AdgDim
*dim
, const AdgPoint
*pos
)
700 g_return_if_fail(ADG_IS_DIM(dim
));
701 g_object_set(dim
, "pos", pos
, NULL
);
705 * adg_dim_set_pos_explicit:
707 * @x: x coordinate of the position
708 * @y: y coordinate of the position
710 * Sets the #AdgDim:pos property to the (@x, @y) explicit
711 * coordinates. The old point is silently discarded,
712 * unreferencing its model if that point was bound to a named
713 * pair (hence, possibly destroying the model if this was the
719 adg_dim_set_pos_explicit(AdgDim
*dim
, gdouble x
, gdouble y
)
721 AdgPoint
*point
= adg_point_new();
723 adg_point_set_pair_explicit(point
, x
, y
);
724 adg_dim_set_pos(dim
, point
);
726 adg_point_destroy(point
);
730 * adg_dim_set_pos_from_pair:
732 * @pos: the coordinates pair of the position point
734 * Convenient function to set the #AdgDim:pos property using a
735 * pair instead of explicit coordinates.
740 adg_dim_set_pos_from_pair(AdgDim
*dim
, const CpmlPair
*pos
)
742 g_return_if_fail(pos
!= NULL
);
744 adg_dim_set_pos_explicit(dim
, pos
->x
, pos
->y
);
748 * adg_dim_set_pos_from_model:
750 * @model: the source #AdgModel
751 * @pos: a named pair in @model
753 * Binds #AdgDim:pos to the @pos named pair of @model. If @model
754 * is <constant>NULL</constant>, the point will be unset. In any
755 * case, the old point is silently discarded, unreferencing its
756 * model if that point was bound to a named pair (hence,
757 * possibly destroying the model if this was the last reference).
759 * The assignment is lazy so @pos could be not be present in @model.
760 * Anyway, at the first access to this point an error will be raised
761 * if the named pair is still missing.
766 adg_dim_set_pos_from_model(AdgDim
*dim
, AdgModel
*model
, const gchar
*pos
)
768 AdgPoint
*point
= adg_point_new();
770 adg_point_set_pair_from_model(point
, model
, pos
);
771 adg_dim_set_pos(dim
, point
);
773 adg_point_destroy(point
);
780 * Gets the #AdgDim:pos point of @dim.
782 * The returned point is internally owned and must not be freed
783 * or modified. Anyway it is not const because a call to
784 * adg_point_update() with the returned value must be able to
785 * modify the internal cache.
787 * Returns: (transfer none): the position point.
792 adg_dim_get_pos(AdgDim
*dim
)
796 g_return_val_if_fail(ADG_IS_DIM(dim
), NULL
);
806 * @level: the new level
808 * Sets a new level for this dimension. The level is used to
809 * stack the quotes using a spacing value from dim_style
810 * (specified in global space).
815 adg_dim_set_level(AdgDim
*dim
, gdouble level
)
819 g_return_if_fail(ADG_IS_DIM(dim
));
824 g_object_notify((GObject
*) dim
, "level");
831 * Gets the level of this dimension.
833 * Returns: the level value.
838 adg_dim_get_level(AdgDim
*dim
)
842 g_return_val_if_fail(ADG_IS_DIM(dim
), 0);
850 * adg_dim_set_outside:
852 * @outside: the new outside state
854 * Sets a new state for the #AdgDim:outside flag: check the property
855 * documentation for further details.
860 adg_dim_set_outside(AdgDim
*dim
, AdgThreeState outside
)
862 g_return_if_fail(ADG_IS_DIM(dim
));
864 if (_adg_set_outside(dim
, outside
))
865 g_object_notify((GObject
*) dim
, "outside");
869 * adg_dim_get_outside:
872 * Gets the state of the #AdgDim:outside property: check the property
873 * documentation for further details.
875 * Returns: the current flag state.
880 adg_dim_get_outside(AdgDim
*dim
)
884 g_return_val_if_fail(ADG_IS_DIM(dim
), ADG_THREE_STATE_UNKNOWN
);
888 return data
->outside
;
892 * adg_dim_set_detached:
894 * @detached: the new detached state
896 * Sets a new state for the #AdgDim:detached flag: check the property
897 * documentation for further details.
899 * This is used only by dimensions where detaching has meaning.
900 * In some cases, such as with #AdgRDim dimensions, this property is
906 adg_dim_set_detached(AdgDim
*dim
, AdgThreeState detached
)
908 g_return_if_fail(ADG_IS_DIM(dim
));
910 if (_adg_set_detached(dim
, detached
))
911 g_object_notify((GObject
*) dim
, "detached");
915 * adg_dim_get_detached:
918 * Gets the state of the #AdgDim:detached property: check the property
919 * documentation for further details.
921 * Returns: the current flag state.
926 adg_dim_get_detached(AdgDim
*dim
)
930 g_return_val_if_fail(ADG_IS_DIM(dim
), ADG_THREE_STATE_UNKNOWN
);
934 return data
->detached
;
940 * @value: (allow-none): the value text
942 * Explicitely sets the text to use as value. If @value
943 * is <constant>NULL</constant> or was never set, an automatic
944 * text is calculated using the format specified in the current
945 * #AdgDimStyle and getting its value by calling
946 * the <function>default_value</function> virtual method.
948 * Inside the template string, the "<>" tag (or whatever specified
949 * by the #AdgDimStyle:number-tag property) is substituted with the
950 * string returned by <function>default_value</function>.
955 adg_dim_set_value(AdgDim
*dim
, const gchar
*value
)
957 g_return_if_fail(ADG_IS_DIM(dim
));
959 if (_adg_set_value(dim
, value
))
960 g_object_notify((GObject
*) dim
, "value");
967 * Gets the value text. The string is internally owned and
968 * must not be freed or modified.
970 * Returns: (transfer none): the value text.
975 adg_dim_get_value(AdgDim
*dim
)
979 g_return_val_if_fail(ADG_IS_DIM(dim
), NULL
);
989 * @value: the raw value of the quote
991 * Gets the final text to show as nominal value into the quote. The string is
992 * the same returned by adg_dim_get_value() with the tag properly expanded.
994 * The string substituted to the tag is formatted according to the
995 * #AdgDimStyle:number-format and #AdgDimStyle:number-arguments properties.
996 * See the #AdgDimStyle documentation for further details.
998 * Returns: (transfer full): the final text of the quote.
1003 adg_dim_get_text(AdgDim
*dim
, gdouble value
)
1005 AdgDimStyle
*dim_style
;
1006 const gchar
*format
;
1007 const gchar
*arguments
;
1008 AdgDimReplaceData data
;
1009 gchar
*raw
, *result
;
1012 g_return_val_if_fail(ADG_IS_DIM(dim
), NULL
);
1014 dim_style
= _ADG_GET_DIM_STYLE(dim
);
1015 if (dim_style
== NULL
) {
1016 dim_style
= (AdgDimStyle
*) adg_entity_style((AdgEntity
*) dim
,
1017 adg_dim_get_dim_dress(dim
));
1020 format
= adg_dim_style_get_number_format(dim_style
);
1021 if (format
== NULL
) {
1025 arguments
= adg_dim_style_get_number_arguments(dim_style
);
1026 if (arguments
== NULL
) {
1027 return g_strdup(format
);
1030 /* Expand the values */
1031 data
.dim_style
= dim_style
;
1033 data
.format
= format
;
1034 data
.argument
= arguments
;
1035 data
.regex
= g_regex_new("(?<!%)%(?!%).*[eEfFgG]", G_REGEX_UNGREEDY
, 0, NULL
);
1036 raw
= _adg_text_expand(&data
);
1037 g_regex_unref(data
.regex
);
1039 /* Substitute the escape sequences ("%%", "((" and "))") */
1040 regex
= g_regex_new("([%()])\\1", G_REGEX_UNGREEDY
, 0, NULL
);
1041 result
= g_regex_replace(regex
, raw
, -1, 0, "\\1", 0, NULL
);
1043 g_regex_unref(regex
);
1049 * adg_dim_set_limits:
1051 * @min: (allow-none): the new minumum value
1052 * @max: (allow-none): the new maximum value
1054 * Shortcut to set both the limits at once.
1059 adg_dim_set_limits(AdgDim
*dim
, const gchar
*min
, const gchar
*max
)
1061 g_return_if_fail(ADG_IS_DIM(dim
));
1063 g_object_freeze_notify((GObject
*) dim
);
1064 adg_dim_set_min(dim
, min
);
1065 adg_dim_set_max(dim
, max
);
1066 g_object_thaw_notify((GObject
*) dim
);
1072 * @min: (allow-none): the new minimum limit
1074 * Sets the minimum value. Use <constant>NULL</constant>
1075 * as @min to disable it.
1080 adg_dim_set_min(AdgDim
*dim
, const gchar
*min
)
1082 g_return_if_fail(ADG_IS_DIM(dim
));
1084 if (_adg_set_min(dim
, min
))
1085 g_object_notify((GObject
*) dim
, "min");
1092 * Gets the minimum value text or <constant>NULL</constant>
1093 * on minimum value disabled.
1095 * The string is internally owned and must not be freed or modified.
1097 * Returns: (transfer none): the mimimum value text.
1102 adg_dim_get_min(AdgDim
*dim
)
1104 AdgDimPrivate
*data
;
1106 g_return_val_if_fail(ADG_IS_DIM(dim
), NULL
);
1116 * @max: (allow-none): the new maximum value
1118 * Sets the maximum value. Use <constant>NULL</constant>
1119 * as @max to disable it.
1124 adg_dim_set_max(AdgDim
*dim
, const gchar
*max
)
1126 g_return_if_fail(ADG_IS_DIM(dim
));
1128 if (_adg_set_max(dim
, max
))
1129 g_object_notify((GObject
*) dim
, "max");
1136 * Gets the maximum value text or <constant>NULL</constant>
1137 * on maximum value disabled.
1139 * The string is internally owned and must not be freed or modified.
1141 * Returns: (transfer none): the maximum value text.
1146 adg_dim_get_max(AdgDim
*dim
)
1148 AdgDimPrivate
*data
;
1150 g_return_val_if_fail(ADG_IS_DIM(dim
), NULL
);
1158 * adg_dim_get_quote:
1161 * Gets the quote entity, if any. This function is valid only after
1162 * the #AdgDim implementation of the arrange() virtual method has
1165 * The returned entity is owned by @dim and should not be
1166 * modified or freed.
1169 * This function is only useful in new dimension implementations.
1172 * Returns: (transfer none): the quote entity.
1177 adg_dim_get_quote(AdgDim
*dim
)
1179 AdgDimPrivate
*data
;
1181 g_return_val_if_fail(ADG_IS_DIM(dim
), NULL
);
1185 return data
->quote
.entity
;
1189 * adg_dim_quote_angle:
1191 * @angle: an angle (in radians)
1194 * This function is only useful in new dimension implementations.
1197 * Converts @angle accordling to the style of @dim. Any quote angle
1198 * should be validated by this method because every dimensioning
1199 * style has its own convention regardling the text rotation.
1201 * Returns: the angle to use (always in radians).
1206 adg_dim_quote_angle(AdgDim
*dim
, gdouble angle
)
1210 g_return_val_if_fail(ADG_IS_DIM(dim
), angle
);
1212 klass
= ADG_DIM_GET_CLASS(dim
);
1214 if (klass
->quote_angle
== NULL
)
1217 return klass
->quote_angle(angle
);
1222 _adg_global_changed(AdgEntity
*entity
)
1224 AdgDimPrivate
*data
= ((AdgDim
*) entity
)->data
;
1226 if (_ADG_OLD_ENTITY_CLASS
->global_changed
)
1227 _ADG_OLD_ENTITY_CLASS
->global_changed(entity
);
1229 if (data
->quote
.entity
)
1230 adg_entity_global_changed((AdgEntity
*) data
->quote
.entity
);
1234 _adg_local_changed(AdgEntity
*entity
)
1236 AdgDimPrivate
*data
= ((AdgDim
*) entity
)->data
;
1238 if (_ADG_OLD_ENTITY_CLASS
->local_changed
)
1239 _ADG_OLD_ENTITY_CLASS
->local_changed(entity
);
1241 if (data
->quote
.entity
)
1242 adg_entity_local_changed((AdgEntity
*) data
->quote
.entity
);
1246 _adg_invalidate(AdgEntity
*entity
)
1248 AdgDimPrivate
*data
= ((AdgDim
*) entity
)->data
;
1250 if (data
->quote
.value
) {
1251 g_object_unref(data
->quote
.value
);
1252 data
->quote
.value
= NULL
;
1254 if (data
->quote
.entity
)
1255 adg_entity_invalidate((AdgEntity
*) data
->quote
.entity
);
1257 adg_point_invalidate(data
->ref1
);
1259 adg_point_invalidate(data
->ref2
);
1261 adg_point_invalidate(data
->pos
);
1263 if (_ADG_OLD_ENTITY_CLASS
->invalidate
)
1264 _ADG_OLD_ENTITY_CLASS
->invalidate(entity
);
1268 _adg_arrange(AdgEntity
*entity
)
1271 AdgDimPrivate
*data
;
1272 AdgEntity
*quote_entity
;
1273 AdgContainer
*quote_container
;
1274 AdgEntity
*value_entity
;
1275 AdgEntity
*min_entity
;
1276 AdgEntity
*max_entity
;
1277 const CpmlPair
*shift
;
1280 dim
= (AdgDim
*) entity
;
1283 /* Resolve the dim style */
1284 if (data
->dim_style
== NULL
)
1285 data
->dim_style
= (AdgDimStyle
*)
1286 adg_entity_style(entity
, data
->dim_dress
);
1288 if (data
->quote
.entity
== NULL
)
1289 data
->quote
.entity
= g_object_new(ADG_TYPE_ALIGNMENT
,
1290 "local-mix", ADG_MIX_NONE
,
1291 "parent", dim
, NULL
);
1293 quote_entity
= (AdgEntity
*) data
->quote
.entity
;
1294 quote_container
= (AdgContainer
*) data
->quote
.entity
;
1296 if (data
->quote
.value
== NULL
) {
1303 klass
= ADG_DIM_GET_CLASS(dim
);
1304 dress
= adg_dim_style_get_value_dress(data
->dim_style
);
1305 tag
= adg_dim_style_get_number_tag(data
->dim_style
);
1306 value
= klass
->default_value
? klass
->default_value(dim
) : NULL
;
1308 data
->quote
.value
= g_object_new(ADG_TYPE_BEST_TEXT
,
1309 "local-mix", ADG_MIX_PARENT
,
1310 "font-dress", dress
, NULL
);
1311 adg_container_add(quote_container
, (AdgEntity
*) data
->quote
.value
);
1314 text
= adg_string_replace(data
->value
, tag
, value
);
1316 text
= g_strdup(value
);
1320 adg_textual_set_text(data
->quote
.value
, text
);
1324 if (data
->quote
.min
== NULL
&& data
->min
!= NULL
) {
1325 AdgDress dress
= adg_dim_style_get_min_dress(data
->dim_style
);
1327 data
->quote
.min
= g_object_new(ADG_TYPE_BEST_TEXT
,
1328 "local-mix", ADG_MIX_PARENT
,
1329 "font-dress", dress
, NULL
);
1331 adg_container_add(quote_container
, (AdgEntity
*) data
->quote
.min
);
1332 adg_textual_set_text(data
->quote
.min
, data
->min
);
1335 if (data
->quote
.max
== NULL
&& data
->max
!= NULL
) {
1336 AdgDress dress
= adg_dim_style_get_max_dress(data
->dim_style
);
1338 data
->quote
.max
= g_object_new(ADG_TYPE_BEST_TEXT
,
1339 "local-mix", ADG_MIX_PARENT
,
1340 "font-dress", dress
, NULL
);
1342 adg_container_add(quote_container
, (AdgEntity
*) data
->quote
.max
);
1343 adg_textual_set_text(data
->quote
.max
, data
->max
);
1346 value_entity
= (AdgEntity
*) data
->quote
.value
;
1347 min_entity
= (AdgEntity
*) data
->quote
.min
;
1348 max_entity
= (AdgEntity
*) data
->quote
.max
;
1349 shift
= adg_dim_style_get_quote_shift(data
->dim_style
);
1351 adg_entity_set_global_map(quote_entity
, adg_matrix_identity());
1352 adg_entity_global_changed(quote_entity
);
1354 cairo_matrix_init_translate(&map
, 0, shift
->y
);
1355 adg_entity_set_global_map(value_entity
, &map
);
1356 adg_entity_arrange(value_entity
);
1358 /* Limit values (min and max) */
1359 if (min_entity
!= NULL
|| max_entity
!= NULL
) {
1360 const CpmlPair
*limits_shift
;
1363 CpmlPair org_min
, org_max
;
1365 limits_shift
= adg_dim_style_get_limits_shift(data
->dim_style
);
1366 spacing
= adg_dim_style_get_limits_spacing(data
->dim_style
);
1367 size
= adg_entity_get_extents(value_entity
)->size
;
1368 org_min
.x
= size
.x
+ limits_shift
->x
;
1369 org_min
.y
= -size
.y
/ 2 + limits_shift
->y
;
1372 if (min_entity
&& max_entity
) {
1373 /* Prearrange the min entity to get its extents */
1374 adg_entity_arrange(min_entity
);
1375 size
= adg_entity_get_extents(min_entity
)->size
;
1377 org_min
.y
+= spacing
/ 2;
1378 org_max
.y
= org_min
.y
- size
.y
- spacing
/ 2;
1381 if (min_entity
!= NULL
) {
1382 cairo_matrix_init_translate(&map
, org_min
.x
, org_min
.y
);
1383 adg_entity_set_global_map(min_entity
, &map
);
1384 adg_entity_arrange(min_entity
);
1387 if (max_entity
!= NULL
) {
1388 cairo_matrix_init_translate(&map
, org_max
.x
, org_max
.y
);
1389 adg_entity_set_global_map(max_entity
, &map
);
1390 adg_entity_arrange(max_entity
);
1396 _adg_default_value(AdgDim
*dim
)
1398 g_warning(_("AdgDim::default_value not implemented for '%s'"),
1399 g_type_name(G_TYPE_FROM_INSTANCE(dim
)));
1400 return g_strdup("undef");
1404 _adg_quote_angle(gdouble angle
)
1406 angle
= cpml_angle(angle
);
1408 if (angle
> G_PI_4
* 4 / 3 || angle
<= -G_PI_4
* 3)
1409 angle
= cpml_angle(angle
+ G_PI
);
1415 _adg_set_outside(AdgDim
*dim
, AdgThreeState outside
)
1417 AdgDimPrivate
*data
;
1419 g_return_val_if_fail(adg_is_enum_value(outside
, ADG_TYPE_THREE_STATE
),
1424 if (data
->outside
== outside
)
1427 data
->outside
= outside
;
1433 _adg_set_detached(AdgDim
*dim
, AdgThreeState detached
)
1435 AdgDimPrivate
*data
;
1437 g_return_val_if_fail(adg_is_enum_value(detached
, ADG_TYPE_THREE_STATE
),
1442 if (data
->detached
== detached
)
1445 data
->detached
= detached
;
1451 _adg_set_value(AdgDim
*dim
, const gchar
*value
)
1453 AdgDimPrivate
*data
;
1457 if (g_strcmp0(value
, data
->value
) == 0)
1460 g_free(data
->value
);
1461 data
->value
= g_strdup(value
);
1463 if (data
->quote
.value
) {
1464 g_object_unref(data
->quote
.value
);
1465 data
->quote
.value
= NULL
;
1472 _adg_set_min(AdgDim
*dim
, const gchar
*min
)
1474 AdgDimPrivate
*data
= dim
->data
;
1476 if (g_strcmp0(min
, data
->min
) == 0)
1480 data
->min
= g_strdup(min
);
1482 if (data
->quote
.min
) {
1483 g_object_unref(data
->quote
.min
);
1484 data
->quote
.min
= NULL
;
1491 _adg_set_max(AdgDim
*dim
, const gchar
*max
)
1493 AdgDimPrivate
*data
= dim
->data
;
1495 if (g_strcmp0(max
, data
->max
) == 0)
1499 data
->max
= g_strdup(max
);
1501 if (data
->quote
.max
) {
1502 g_object_unref(data
->quote
.max
);
1503 data
->quote
.max
= NULL
;
1510 _adg_replace(const GMatchInfo
*match_info
, GString
*result
, gpointer user_data
)
1512 AdgDimReplaceData
*data
;
1517 data
= (AdgDimReplaceData
*) user_data
;
1518 value
= data
->value
;
1520 if (! adg_dim_style_convert(data
->dim_style
, &value
, *data
->argument
)) {
1521 /* Conversion failed: invalid argument? */
1522 g_return_val_if_reached(TRUE
);
1526 format
= g_match_info_fetch(match_info
, 0);
1528 /* This should never happen */
1529 g_return_val_if_fail(format
!= NULL
, TRUE
);
1531 /* Consume the recently used argument */
1534 g_ascii_formatd(buffer
, 256, format
, value
);
1536 g_string_append(result
, buffer
);
1538 /* Set the valorized flag */
1540 data
->valorized
= ADG_THREE_STATE_ON
;
1541 } else if (data
->valorized
== ADG_THREE_STATE_UNKNOWN
) {
1542 data
->valorized
= ADG_THREE_STATE_OFF
;
1549 _adg_text_expand(AdgDimReplaceData
*data
)
1552 const gchar
*bog
, *eog
;
1554 AdgThreeState valorized
;
1557 valorized
= ADG_THREE_STATE_UNKNOWN
;
1558 result
= g_string_new("");
1559 eog
= adg_single_strchr(data
->format
, ')');
1561 /* Expand eventual groups found in the same nesting level */
1562 while ((bog
= adg_single_strchr(data
->format
, '(')) != NULL
) {
1563 /* If eog precedes bog, it means that bog is in another nest */
1564 if (eog
!= NULL
&& eog
< bog
) {
1568 len
= bog
- data
->format
;
1570 /* Parse template before the bog */
1571 data
->valorized
= ADG_THREE_STATE_UNKNOWN
;
1572 string
= g_regex_replace_eval(data
->regex
,
1573 data
->format
, len
, 0,
1577 valorized
= OR_3S(valorized
, data
->valorized
);
1579 data
->format
+= len
+1;
1580 g_string_append(result
, string
);
1583 /* Recursively expand the group */
1584 string
= _adg_text_expand(data
);
1585 valorized
= OR_3S(valorized
, data
->valorized
);
1587 g_string_append(result
, string
);
1590 /* Ensure there is a matching closing parenthesis */
1591 if (*data
->format
!= ')') {
1592 g_string_free(result
, TRUE
);
1593 g_return_val_if_reached(NULL
);
1597 /* Skip the closing parenthesis */
1599 eog
= adg_single_strchr(data
->format
, ')');
1602 /* Expand until closing parenthesis (End Of Group) or '\0' */
1603 len
= eog
== NULL
? strlen(data
->format
) : (eog
- data
->format
);
1604 data
->valorized
= ADG_THREE_STATE_UNKNOWN
;
1605 string
= g_regex_replace_eval(data
->regex
,
1606 data
->format
, len
, 0,
1611 data
->format
+= len
;
1612 g_string_append(result
, string
);
1615 /* Store the final valorized state */
1616 valorized
= OR_3S(valorized
, data
->valorized
);
1617 data
->valorized
= valorized
;
1619 /* Drop the result only if we are inside a group */
1620 if (*data
->format
&& valorized
== ADG_THREE_STATE_OFF
) {
1621 g_string_free(result
, TRUE
);
1622 return g_strdup("");
1625 return g_string_free(result
, FALSE
);