[AdgDim] Emits the "arrange" signal before getting the extents
[adg.git] / adg / adg-adim.c
blob00d2a11702c58466e20d5ee9b77464497f983bb6
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-adim
23 * @short_description: Angular dimensions
25 * The #AdgADim entity defines an angular dimension.
28 /**
29 * AdgADim:
31 * All fields are privates and should not be used directly.
32 * Use its public methods instead.
33 **/
36 #include "adg-adim.h"
37 #include "adg-adim-private.h"
38 #include "adg-intl.h"
40 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_adim_parent_class)
41 #define PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_adim_parent_class)
44 enum {
45 PROP_0,
46 PROP_ORG1,
47 PROP_ORG2,
48 PROP_HAS_EXTENSION1,
49 PROP_HAS_EXTENSION2
53 static void dispose (GObject *object);
54 static void get_property (GObject *object,
55 guint param_id,
56 GValue *value,
57 GParamSpec *pspec);
58 static void set_property (GObject *object,
59 guint param_id,
60 const GValue *value,
61 GParamSpec *pspec);
62 static void local_changed (AdgEntity *entity);
63 static void invalidate (AdgEntity *entity);
64 static void arrange (AdgEntity *entity);
65 static void render (AdgEntity *entity,
66 cairo_t *cr);
67 static gchar * default_value (AdgDim *dim);
68 static void update_geometry (AdgADim *adim);
69 static void update_entities (AdgADim *adim);
70 static gboolean set_org1 (AdgADim *adim,
71 const AdgPair *org1);
72 static gboolean set_org2 (AdgADim *adim,
73 const AdgPair *org2);
74 static void unset_trail (AdgADim *adim);
75 static void dispose_markers (AdgADim *adim);
76 static gboolean get_info (AdgADim *adim,
77 CpmlVector vector[],
78 AdgPair *center,
79 gdouble *distance);
80 static CpmlPath * trail_callback (AdgTrail *trail,
81 gpointer user_data);
84 G_DEFINE_TYPE(AdgADim, adg_adim, ADG_TYPE_DIM);
87 static void
88 adg_adim_class_init(AdgADimClass *klass)
90 GObjectClass *gobject_class;
91 AdgEntityClass *entity_class;
92 AdgDimClass *dim_class;
93 GParamSpec *param;
95 gobject_class = (GObjectClass *) klass;
96 entity_class = (AdgEntityClass *) klass;
97 dim_class = (AdgDimClass *) klass;
99 g_type_class_add_private(klass, sizeof(AdgADimPrivate));
101 gobject_class->dispose = dispose;
102 gobject_class->get_property = get_property;
103 gobject_class->set_property = set_property;
105 entity_class->local_changed = local_changed;
106 entity_class->invalidate = invalidate;
107 entity_class->arrange = arrange;
108 entity_class->render = render;
110 dim_class->default_value = default_value;
112 param = g_param_spec_boxed("org1",
113 P_("First Origin"),
114 P_("Where the first line comes from: this point is used toghether with \"ref1\" to align the first extension line"),
115 ADG_TYPE_PAIR,
116 G_PARAM_READWRITE);
117 g_object_class_install_property(gobject_class, PROP_ORG1, param);
119 param = g_param_spec_boxed("org2",
120 P_("Second Origin"),
121 P_("Where the second line comes from: this point is used toghether with \"ref2\" to align the second extension line"),
122 ADG_TYPE_PAIR,
123 G_PARAM_READWRITE);
124 g_object_class_install_property(gobject_class, PROP_ORG2, param);
127 static void
128 adg_adim_init(AdgADim *adim)
130 AdgADimPrivate *data;
131 cairo_path_data_t move_to, line_to, arc_to;
133 data = G_TYPE_INSTANCE_GET_PRIVATE(adim, ADG_TYPE_ADIM, AdgADimPrivate);
134 move_to.header.type = CAIRO_PATH_MOVE_TO;
135 move_to.header.length = 2;
136 line_to.header.type = CAIRO_PATH_LINE_TO;
137 line_to.header.length = 2;
138 arc_to.header.type = CAIRO_PATH_ARC_TO;
139 arc_to.header.length = 3;
141 data->org1.x = data->org1.y = 0;
142 data->org2.x = data->org2.y = 0;
143 data->has_extension1 = TRUE;
144 data->has_extension2 = TRUE;
146 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
147 data->cpml.path.data = data->cpml.data;
148 data->cpml.path.num_data = G_N_ELEMENTS(data->cpml.data);
149 data->cpml.path.data[0] = move_to;
150 data->cpml.path.data[2] = arc_to;
151 data->cpml.path.data[5] = move_to;
152 data->cpml.path.data[7] = line_to;
153 data->cpml.path.data[9] = move_to;
154 data->cpml.path.data[11] = line_to;
156 data->trail = NULL;
157 data->marker1 = NULL;
158 data->marker2 = NULL;
160 data->geometry_arranged = FALSE;
162 adim->data = data;
165 static void
166 dispose(GObject *object)
168 dispose_markers((AdgADim *) object);
170 if (PARENT_OBJECT_CLASS->dispose != NULL)
171 PARENT_OBJECT_CLASS->dispose(object);
174 static void
175 get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
177 AdgADimPrivate *data = ((AdgADim *) object)->data;
179 switch (prop_id) {
180 case PROP_ORG1:
181 g_value_set_boxed(value, &data->org1);
182 break;
183 case PROP_ORG2:
184 g_value_set_boxed(value, &data->org2);
185 break;
186 case PROP_HAS_EXTENSION1:
187 g_value_set_boolean(value, data->has_extension1);
188 break;
189 case PROP_HAS_EXTENSION2:
190 g_value_set_boolean(value, data->has_extension2);
191 break;
192 default:
193 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
194 break;
198 static void
199 set_property(GObject *object, guint prop_id,
200 const GValue *value, GParamSpec *pspec)
202 AdgADim *adim;
203 AdgADimPrivate *data;
205 adim = (AdgADim *) object;
206 data = adim->data;
208 switch (prop_id) {
209 case PROP_ORG1:
210 set_org1(adim, g_value_get_boxed(value));
211 break;
212 case PROP_ORG2:
213 set_org2(adim, g_value_get_boxed(value));
214 break;
215 case PROP_HAS_EXTENSION1:
216 data->has_extension1 = g_value_get_boolean(value);
217 break;
218 case PROP_HAS_EXTENSION2:
219 data->has_extension2 = g_value_get_boolean(value);
220 break;
221 default:
222 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
223 break;
229 * adg_adim_new:
231 * Creates a new - undefined - angular dimension. You must, at least,
232 * define the reference points with adg_dim_set_ref(), the origins of
233 * the lines ending with the reference points with adg_adim_set_org()
234 * and the reference for positioning the quote with adg_dim_set_pos().
236 * Returns: the newly created angular dimension entity
238 AdgADim *
239 adg_adim_new(void)
241 return g_object_new(ADG_TYPE_ADIM, NULL);
245 * adg_adim_new_full:
246 * @ref1: first reference point
247 * @ref2: second reference point
248 * @org1: first origin point
249 * @org2: second origin point
251 * Creates a new angular dimension, specifing all the needed
252 * properties in one shot.
254 * Returns: the newly created angular dimension entity
256 AdgADim *
257 adg_adim_new_full(const AdgPair *ref1, const AdgPair *ref2,
258 const AdgPair *org1, const AdgPair *org2,
259 const AdgPair *pos)
261 return g_object_new(ADG_TYPE_ADIM, "ref1", ref1, "ref2", ref2,
262 "org1", org1, "org2", org2, "pos", pos, NULL);
266 * adg_adim_new_full_explicit:
267 * @ref1_x: the x coordinate of the first reference point
268 * @ref1_y: the y coordinate of the first reference point
269 * @ref2_x: the x coordinate of the second reference point
270 * @ref2_y: the y coordinate of the second reference point
271 * @direction: angle where to extend the dimension
272 * @pos_x: the x coordinate of the position reference
273 * @pos_y: the y coordinate of the position reference
275 * Wrappes adg_adim_new_full() with explicit values.
277 * Returns: the newly created linear dimension entity
279 AdgADim *
280 adg_adim_new_full_explicit(gdouble ref1_x, gdouble ref1_y,
281 gdouble ref2_x, gdouble ref2_y,
282 gdouble org1_x, gdouble org1_y,
283 gdouble org2_x, gdouble org2_y,
284 gdouble pos_x, gdouble pos_y)
286 AdgPair ref1, ref2, org1, org2, pos;
288 ref1.x = ref1_x;
289 ref1.y = ref1_y;
290 ref2.x = ref2_x;
291 ref2.y = ref2_y;
292 org1.x = org1_x;
293 org1.y = org1_y;
294 org2.x = org2_x;
295 org2.y = org2_y;
296 pos.x = pos_x;
297 pos.y = pos_y;
299 return adg_adim_new_full(&ref1, &ref2, &org1, &org2, &pos);
303 * adg_adim_get_org1:
304 * @adim: an #AdgADim
306 * Gets the first origin of @adim. The returned pair is owned by
307 * @adim and should not be modified or freed.
309 * Returns: a pointer to the internal #AdgPair or %NULL on errors
311 const AdgPair *
312 adg_adim_get_org1(AdgADim *adim)
314 AdgADimPrivate *data;
316 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
318 data = adim->data;
320 return &data->org1;
324 * adg_adim_get_org2:
325 * @adim: an #AdgADim
327 * Gets the second origin of @adim. The returned pair is owned by
328 * @adim and should not be modified or freed.
330 * Returns: a pointer to the internal #AdgPair or %NULL on errors
332 const AdgPair *
333 adg_adim_get_org2(AdgADim *adim)
335 AdgADimPrivate *data;
337 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
339 data = adim->data;
341 return &data->org2;
345 * adg_adim_set_org:
346 * @adim: an #AdgADim
347 * @org1: the first origin
348 * @org2: the second origin
350 * Sets at once the two origins on @adim.
352 void
353 adg_adim_set_org(AdgADim *adim, const AdgPair *org1, const AdgPair *org2)
355 GObject *object;
357 g_return_if_fail(ADG_IS_ADIM(adim));
359 object = (GObject *) adim;
361 g_object_freeze_notify(object);
363 if (set_org1(adim, org1))
364 g_object_notify(object, "org1");
366 if (set_org2(adim, org1))
367 g_object_notify(object, "org2");
369 g_object_thaw_notify(object);
373 static void
374 local_changed(AdgEntity *entity)
376 unset_trail((AdgADim *) entity);
378 PARENT_ENTITY_CLASS->local_changed(entity);
381 static void
382 invalidate(AdgEntity *entity)
384 AdgADim *adim;
385 AdgADimPrivate *data;
387 adim = (AdgADim *) entity;
388 data = adim->data;
390 dispose_markers(adim);
391 data->geometry_arranged = FALSE;
392 unset_trail(adim);
394 if (PARENT_ENTITY_CLASS->invalidate != NULL)
395 PARENT_ENTITY_CLASS->invalidate(entity);
398 static void
399 arrange(AdgEntity *entity)
401 AdgADim *adim;
402 AdgDim *dim;
403 AdgADimPrivate *data;
404 AdgContainer *quote;
405 const AdgMatrix *local;
406 AdgPair ref1, ref2, base1, base12, base2;
407 AdgPair pair;
409 PARENT_ENTITY_CLASS->arrange(entity);
411 adim = (AdgADim *) entity;
412 dim = (AdgDim *) adim;
413 data = adim->data;
414 quote = adg_dim_get_quote(dim);
416 update_geometry(adim);
417 update_entities(adim);
419 if (data->cpml.path.status == CAIRO_STATUS_SUCCESS) {
420 AdgEntity *quote_entity = (AdgEntity *) quote;
421 adg_entity_set_global_map(quote_entity, &data->quote.global_map);
422 adg_entity_set_local_map(quote_entity, &data->quote.local_map);
423 return;
426 local = adg_entity_local_matrix(entity);
428 /* Apply the local matrix to the relevant points */
429 cpml_pair_transform(cpml_pair_copy(&ref1, adg_dim_get_ref1(dim)), local);
430 cpml_pair_transform(cpml_pair_copy(&ref2, adg_dim_get_ref2(dim)), local);
431 cpml_pair_transform(cpml_pair_copy(&base1, &data->point.base1), local);
432 cpml_pair_transform(cpml_pair_copy(&base12, &data->point.base12), local);
433 cpml_pair_transform(cpml_pair_copy(&base2, &data->point.base2), local);
435 /* Combine points and global shifts to build the path */
436 cpml_pair_add(cpml_pair_copy(&pair, &ref1), &data->shift.from1);
437 cpml_pair_to_cairo(&pair, &data->cpml.data[6]);
439 cpml_pair_add(cpml_pair_copy(&pair, &base1), &data->shift.base1);
440 cpml_pair_to_cairo(&pair, &data->cpml.data[1]);
442 cpml_pair_add(&pair, &data->shift.to1);
443 cpml_pair_to_cairo(&pair, &data->cpml.data[8]);
445 cpml_pair_add(cpml_pair_copy(&pair, &base12), &data->shift.base12);
446 cpml_pair_to_cairo(&pair, &data->cpml.data[3]);
448 cpml_pair_add(cpml_pair_copy(&pair, &ref2), &data->shift.from2);
449 cpml_pair_to_cairo(&pair, &data->cpml.data[10]);
451 cpml_pair_add(cpml_pair_copy(&pair, &base2), &data->shift.base2);
452 cpml_pair_to_cairo(&pair, &data->cpml.data[4]);
454 cpml_pair_add(&pair, &data->shift.to2);
455 cpml_pair_to_cairo(&pair, &data->cpml.data[12]);
457 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
459 if (quote != NULL) {
460 /* Update global and local map of the quote container */
461 AdgEntity *quote_entity;
462 gdouble angle;
463 AdgMatrix matrix;
465 quote_entity = (AdgEntity *) quote;
466 angle = adg_dim_quote_angle(dim, (data->angle1 + data->angle2) / 2 + G_PI_2);
467 adg_matrix_copy(&matrix, local);
468 cairo_matrix_invert(&matrix);
470 cpml_pair_from_cairo(&pair, &data->cpml.data[3]);
471 cairo_matrix_transform_point(&matrix, &pair.x, &pair.y);
472 cairo_matrix_init_translate(&matrix, pair.x, pair.y);
473 adg_entity_set_local_map(quote_entity, &matrix);
475 cairo_matrix_init_rotate(&matrix, angle);
476 adg_entity_transform_global_map(quote_entity, &matrix, ADG_TRANSFORM_BEFORE);
478 adg_entity_get_global_map(quote_entity, &data->quote.global_map);
479 adg_entity_get_local_map(quote_entity, &data->quote.local_map);
482 /* Signal to the markers (if any) that the path has changed */
483 if (data->marker1 != NULL) {
484 adg_marker_set_segment(data->marker1, data->trail, 1);
485 adg_entity_local_changed((AdgEntity *) data->marker1);
488 if (data->marker2 != NULL) {
489 adg_marker_set_segment(data->marker2, data->trail, 1);
490 adg_entity_local_changed((AdgEntity *) data->marker2);
493 /* TODO: compute the extents */
496 static void
497 render(AdgEntity *entity, cairo_t *cr)
499 AdgADim *adim;
500 AdgDim *dim;
501 AdgADimPrivate *data;
502 AdgDimStyle *dim_style;
503 AdgDress dress;
504 const cairo_path_t *cairo_path;
506 adim = (AdgADim *) entity;
507 dim = (AdgDim *) entity;
508 data = adim->data;
509 dim_style = adg_dim_get_dim_style(dim);
511 dress = adg_dim_style_get_color_dress(dim_style);
512 adg_entity_apply_dress(entity, dress, cr);
514 if (data->marker1 != NULL)
515 adg_entity_render((AdgEntity *) data->marker1, cr);
517 if (data->marker2 != NULL)
518 adg_entity_render((AdgEntity *) data->marker2, cr);
520 adg_entity_render((AdgEntity *) adg_dim_get_quote(dim), cr);
522 dress = adg_dim_style_get_line_dress(dim_style);
523 adg_entity_apply_dress(entity, dress, cr);
525 cairo_path = adg_trail_get_cairo_path(data->trail);
526 cairo_append_path(cr, cairo_path);
527 cairo_stroke(cr);
530 static gchar *
531 default_value(AdgDim *dim)
533 AdgADim *adim;
534 AdgADimPrivate *data;
535 AdgDimStyle *dim_style;
536 gdouble angle;
537 const gchar *format;
539 adim = (AdgADim *) dim;
540 data = adim->data;
541 dim_style = adg_dim_get_dim_style(dim);
542 format = adg_dim_style_get_number_format(dim_style);
544 update_geometry(adim);
545 angle = (data->angle2 - data->angle1) * 180 / M_PI;
547 return g_strdup_printf(format, angle);
550 /* With "geometry" is considered any data (point, vector or angle)
551 * that can be cached: this is strictly related on how the arrange()
552 * method works */
553 static void
554 update_geometry(AdgADim *adim)
556 AdgADimPrivate *data;
557 AdgDimStyle *dim_style;
558 gdouble from_offset, to_offset;
559 gdouble spacing, level;
560 CpmlVector vector[3];
561 CpmlPair center;
562 gdouble distance;
564 data = adim->data;
566 if (data->geometry_arranged)
567 return;
569 if (!get_info(adim, vector, &center, &distance)) {
570 /* Parallel lines: hang with an error message */
571 g_warning("%s: trying to set an angular dimension on parallel lines",
572 G_STRLOC);
573 return;
576 dim_style = adg_dim_get_dim_style((AdgDim *) adim);
577 from_offset = adg_dim_style_get_from_offset(dim_style);
578 to_offset = adg_dim_style_get_to_offset(dim_style);
579 spacing = adg_dim_style_get_baseline_spacing(dim_style);
580 level = adg_dim_get_level((AdgDim *) adim);
582 /* shift.from1 */
583 cpml_vector_set_length(&vector[0], from_offset);
584 cpml_pair_copy(&data->shift.from1, &vector[0]);
586 /* shift.base1 */
587 cpml_vector_set_length(&vector[0], level * spacing);
588 cpml_pair_copy(&data->shift.base1, &vector[0]);
590 /* shift.to1 */
591 cpml_vector_set_length(&vector[0], to_offset);
592 cpml_pair_copy(&data->shift.to1, &vector[0]);
594 /* shift.from2 */
595 cpml_vector_set_length(&vector[2], from_offset);
596 cpml_pair_copy(&data->shift.from2, &vector[2]);
598 /* shift.base2 */
599 cpml_vector_set_length(&vector[2], level * spacing);
600 cpml_pair_copy(&data->shift.base2, &vector[2]);
602 /* shift.to2 */
603 cpml_vector_set_length(&vector[2], to_offset);
604 cpml_pair_copy(&data->shift.to2, &vector[2]);
606 /* shift.base12 */
607 cpml_vector_set_length(&vector[1], level * spacing);
608 cpml_pair_copy(&data->shift.base12, &vector[1]);
610 /* Distance can be 0, so the following will leave the
611 * vector array in undefined state */
613 /* point.base1 */
614 cpml_vector_set_length(&vector[0], distance);
615 cpml_pair_add(cpml_pair_copy(&data->point.base1, &vector[0]), &center);
617 /* point.base2 */
618 cpml_vector_set_length(&vector[2], distance);
619 cpml_pair_add(cpml_pair_copy(&data->point.base2, &vector[2]), &center);
621 /* point.base12 */
622 cpml_vector_set_length(&vector[1], distance);
623 cpml_pair_add(cpml_pair_copy(&data->point.base12, &vector[1]), &center);
625 data->geometry_arranged = TRUE;
628 static void
629 update_entities(AdgADim *adim)
631 AdgADimPrivate *data;
632 AdgDimStyle *dim_style;
634 data = adim->data;
635 dim_style = adg_dim_get_dim_style((AdgDim *) adim);
637 if (data->trail == NULL)
638 data->trail = adg_trail_new(trail_callback, adim);
640 if (data->marker1 == NULL)
641 data->marker1 = adg_dim_style_marker1_new(dim_style);
643 if (data->marker2 == NULL)
644 data->marker2 = adg_dim_style_marker2_new(dim_style);
647 static gboolean
648 set_org1(AdgADim *adim, const AdgPair *org1)
650 AdgADimPrivate *data = adim->data;
652 if (adg_pair_equal(&data->org1, org1))
653 return FALSE;
655 data->org1 = *org1;
657 return TRUE;
660 static gboolean
661 set_org2(AdgADim *adim, const AdgPair *org2)
663 AdgADimPrivate *data = adim->data;
665 if (adg_pair_equal(&data->org2, org2))
666 return FALSE;
668 data->org2 = *org2;
670 return TRUE;
673 static void
674 unset_trail(AdgADim *adim)
676 AdgADimPrivate *data = adim->data;
678 if (data->trail != NULL)
679 adg_model_clear((AdgModel *) data->trail);
681 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
684 static void
685 dispose_markers(AdgADim *adim)
687 AdgADimPrivate *data = adim->data;
689 if (data->trail != NULL) {
690 g_object_unref(data->trail);
691 data->trail = NULL;
694 if (data->marker1 != NULL) {
695 g_object_unref(data->marker1);
696 data->marker1 = NULL;
699 if (data->marker2 != NULL) {
700 g_object_unref(data->marker2);
701 data->marker2 = NULL;
705 static gboolean
706 get_info(AdgADim *adim, CpmlVector vector[],
707 AdgPair *center, gdouble *distance)
709 AdgDim *dim;
710 AdgADimPrivate *data;
711 const AdgPair *ref1, *ref2;
712 gdouble factor;
714 dim = (AdgDim *) adim;
715 data = adim->data;
716 ref1 = adg_dim_get_ref1(dim);
717 ref2 = adg_dim_get_ref2(dim);
719 cpml_pair_sub(cpml_pair_copy(&vector[0], ref1), &data->org1);
720 cpml_pair_sub(cpml_pair_copy(&vector[2], ref2), &data->org2);
722 factor = vector[0].x * vector[2].y - vector[0].y * vector[2].x;
723 if (factor == 0)
724 return FALSE;
726 factor = ((ref1->y - ref2->y) * vector[2].x -
727 (ref1->x - ref2->x) * vector[2].y) / factor;
729 center->x = ref1->x + vector[0].x * factor;
730 center->y = ref1->y + vector[0].y * factor;
731 *distance = cpml_pair_distance(center, adg_dim_get_pos(dim));
732 data->angle1 = cpml_vector_angle(&vector[0]);
733 data->angle2 = cpml_vector_angle(&vector[2]);
734 while (data->angle2 < data->angle1)
735 data->angle2 += M_PI * 2;
737 cpml_vector_from_angle(&vector[1], (data->angle1 + data->angle2) / 2);
739 return TRUE;
742 static CpmlPath *
743 trail_callback(AdgTrail *trail, gpointer user_data)
745 AdgADim *adim;
746 AdgADimPrivate *data;
748 adim = (AdgADim *) user_data;
749 data = adim->data;
751 return &data->cpml.path;