1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010 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.
22 * SECTION:adg-projection
23 * @short_description: The standard symbol for specifying the projection scheme
25 * The #AdgProjection is an entity representing the standard symbol
26 * of the projection scheme.
32 * All fields are private and should not be used directly.
33 * Use its public methods instead.
37 #include "adg-internal.h"
38 #include "adg-projection.h"
39 #include "adg-projection-private.h"
40 #include "adg-line-style.h"
41 #include "adg-dress-builtins.h"
43 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_projection_parent_class)
54 static void get_property (GObject
*object
,
58 static void set_property (GObject
*object
,
62 static void arrange (AdgEntity
*entity
);
63 static void render (AdgEntity
*entity
,
65 static void arrange_class (AdgProjectionClass
*projection_class
,
66 AdgProjectionScheme scheme
);
67 static gboolean
set_scheme (AdgProjection
*projection
,
68 AdgProjectionScheme scheme
);
71 G_DEFINE_TYPE(AdgProjection
, adg_projection
, ADG_TYPE_ENTITY
);
75 adg_projection_class_init(AdgProjectionClass
*klass
)
77 GObjectClass
*gobject_class
;
78 AdgEntityClass
*entity_class
;
80 AdgProjectionClassPrivate
*data_class
;
82 gobject_class
= (GObjectClass
*) klass
;
83 entity_class
= (AdgEntityClass
*) klass
;
85 g_type_class_add_private(klass
, sizeof(AdgProjectionPrivate
));
87 gobject_class
->get_property
= get_property
;
88 gobject_class
->set_property
= set_property
;
90 entity_class
->arrange
= arrange
;
91 entity_class
->render
= render
;
93 param
= adg_param_spec_dress("symbol-dress",
95 P_("The line dress to use for rendering the views of the projection"),
98 g_object_class_install_property(gobject_class
, PROP_SYMBOL_DRESS
, param
);
100 param
= adg_param_spec_dress("axis-dress",
102 P_("The line dress to use for rendering the axis of the projection scheme"),
105 g_object_class_install_property(gobject_class
, PROP_AXIS_DRESS
, param
);
107 param
= g_param_spec_enum("scheme",
108 P_("Projection Scheme"),
109 P_("The projection scheme to be represented"),
110 ADG_TYPE_PROJECTION_SCHEME
,
111 ADG_PROJECTION_UNDEFINED
,
113 g_object_class_install_property(gobject_class
, PROP_SCHEME
, param
);
115 /* Initialize the private class data: the allocated struct is
116 * never freed as this type is registered statically, hence
117 * never destroyed. A better approach would be to use the old
118 * type initialization (no G_TYPE_DEFINE and friends) that
119 * allows to specify a custom class finalization method */
120 data_class
= g_new(AdgProjectionClassPrivate
, 1);
122 data_class
->scheme
= ADG_PROJECTION_UNDEFINED
;
123 data_class
->symbol
= NULL
;
124 data_class
->axis
= NULL
;
125 data_class
->extents
.is_defined
= FALSE
;
127 klass
->data_class
= data_class
;
131 adg_projection_init(AdgProjection
*projection
)
133 AdgProjectionPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(projection
, ADG_TYPE_PROJECTION
,
134 AdgProjectionPrivate
);
136 data
->symbol_dress
= ADG_DRESS_LINE
;
137 data
->axis_dress
= ADG_DRESS_LINE
;
138 data
->scheme
= ADG_PROJECTION_UNDEFINED
;
140 projection
->data
= data
;
144 get_property(GObject
*object
, guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
146 AdgProjectionPrivate
*data
= ((AdgProjection
*) object
)->data
;
149 case PROP_SYMBOL_DRESS
:
150 g_value_set_int(value
, data
->symbol_dress
);
152 case PROP_AXIS_DRESS
:
153 g_value_set_int(value
, data
->axis_dress
);
156 g_value_set_enum(value
, data
->scheme
);
159 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
165 set_property(GObject
*object
, guint prop_id
,
166 const GValue
*value
, GParamSpec
*pspec
)
168 AdgProjection
*projection
;
169 AdgProjectionPrivate
*data
;
171 projection
= (AdgProjection
*) object
;
172 data
= projection
->data
;
175 case PROP_SYMBOL_DRESS
:
176 adg_dress_set(&data
->symbol_dress
, g_value_get_int(value
));
178 case PROP_AXIS_DRESS
:
179 adg_dress_set(&data
->axis_dress
, g_value_get_int(value
));
182 set_scheme(projection
, g_value_get_enum(value
));
185 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
192 * adg_projection_new:
193 * @scheme: the scheme represented by this projection
195 * Creates a new projection entity representing the selected @scheme.
196 * If @scheme is invalid, a projection symbol without a scheme is
197 * returned, that is #AdgProjection:scheme is set to
198 * #ADG_PROJECTION_UNDEFINED, and a warning is raised.
200 * Returns: the newly created projection entity
203 adg_projection_new(AdgProjectionScheme scheme
)
205 return g_object_new(ADG_TYPE_PROJECTION
, "scheme", scheme
, NULL
);
209 * adg_projection_set_symbol_dress:
210 * @projection: an #AdgProjection
211 * @dress: the new #AdgDress to use
213 * Sets a new line dress for rendering the symbol of @projection. The
214 * new dress must be a line dress: the check is done by calling
215 * adg_dress_are_related() with @dress and the old dress as
216 * arguments. Check out its documentation for further details.
218 * The default dress is a transparent line dress: the rendering
219 * callback will stroke the symbol using the default color with
220 * a predefined thickness.
223 adg_projection_set_symbol_dress(AdgProjection
*projection
, AdgDress dress
)
225 AdgProjectionPrivate
*data
;
227 g_return_if_fail(ADG_IS_PROJECTION(projection
));
229 data
= projection
->data
;
231 if (adg_dress_set(&data
->symbol_dress
, dress
))
232 g_object_notify((GObject
*) projection
, "symbol-dress");
236 * adg_projection_get_symbol_dress:
237 * @projection: an #AdgProjection
239 * Gets the line dress to be used in stroking the symbol of @projection.
241 * Returns: the requested line dress
244 adg_projection_get_symbol_dress(AdgProjection
*projection
)
246 AdgProjectionPrivate
*data
;
248 g_return_val_if_fail(ADG_IS_PROJECTION(projection
), ADG_DRESS_UNDEFINED
);
250 data
= projection
->data
;
252 return data
->symbol_dress
;
256 * adg_projection_set_axis_dress:
257 * @projection: an #AdgProjection
258 * @dress: the new #AdgDress to use
260 * Sets a new line dress for rendering the axis of @projection.
261 * The new dress must be a line dress: the check is done by
262 * calling adg_dress_are_related() with @dress and the old
263 * dress as arguments. Check out its documentation for
266 * The default dress is a transparent line dress: the rendering
267 * callback will stroke the axis using the default line style.
270 adg_projection_set_axis_dress(AdgProjection
*projection
, AdgDress dress
)
272 AdgProjectionPrivate
*data
;
274 g_return_if_fail(ADG_IS_PROJECTION(projection
));
276 data
= projection
->data
;
278 if (adg_dress_set(&data
->axis_dress
, dress
))
279 g_object_notify((GObject
*) projection
, "axis-dress");
283 * adg_projection_get_axis_dress:
284 * @projection: an #AdgProjection
286 * Gets the line dress to be used in stroking the axis of @projection.
288 * Returns: the requested line dress
291 adg_projection_get_axis_dress(AdgProjection
*projection
)
293 AdgProjectionPrivate
*data
;
295 g_return_val_if_fail(ADG_IS_PROJECTION(projection
), ADG_DRESS_UNDEFINED
);
297 data
= projection
->data
;
299 return data
->axis_dress
;
303 * adg_projection_set_scheme:
304 * @projection: an #AdgProjection
305 * @scheme: the new projection scheme
307 * Sets a new scheme on @projection. If @scheme is different
308 * from the old one, @projection is invalidated.
311 adg_projection_set_scheme(AdgProjection
*projection
,
312 AdgProjectionScheme scheme
)
314 g_return_if_fail(ADG_IS_PROJECTION(projection
));
316 if (set_scheme(projection
, scheme
))
317 g_object_notify((GObject
*) projection
, "scheme");
321 * adg_projection_get_scheme:
322 * @projection: an #AdgProjection
324 * Gets the scheme represented by @projection.
326 * Returns: the scheme of @projection
329 adg_projection_get_scheme(AdgProjection
*projection
)
331 AdgProjectionPrivate
*data
;
333 g_return_val_if_fail(ADG_IS_PROJECTION(projection
),
334 ADG_PROJECTION_UNDEFINED
);
336 data
= projection
->data
;
343 arrange(AdgEntity
*entity
)
345 AdgProjectionPrivate
*data
;
346 AdgProjectionClass
*projection_class
;
347 AdgProjectionClassPrivate
*data_class
;
350 data
= ((AdgProjection
*) entity
)->data
;
351 projection_class
= ADG_PROJECTION_GET_CLASS(entity
);
352 data_class
= projection_class
->data_class
;
354 arrange_class(projection_class
, data
->scheme
);
355 cpml_extents_copy(&extents
, &data_class
->extents
);
357 cpml_extents_transform(&extents
, adg_entity_get_local_matrix(entity
));
358 cpml_extents_transform(&extents
, adg_entity_get_global_matrix(entity
));
359 adg_entity_set_extents(entity
, &extents
);
363 arrange_class(AdgProjectionClass
*projection_class
, AdgProjectionScheme scheme
)
365 AdgProjectionClassPrivate
*data_class
;
366 AdgPath
*symbol
, *axis
;
368 data_class
= projection_class
->data_class
;
370 if (data_class
->scheme
== scheme
)
373 if (data_class
->symbol
!= NULL
)
374 g_object_unref(data_class
->symbol
);
376 if (data_class
->axis
!= NULL
)
377 g_object_unref(data_class
->axis
);
379 data_class
->scheme
= scheme
;
382 case ADG_PROJECTION_UNDEFINED
:
386 case ADG_PROJECTION_FIRST_ANGLE
:
387 symbol
= adg_path_new();
388 adg_path_move_to_explicit(symbol
, 4, 19);
389 adg_path_line_to_explicit(symbol
, 24, 24);
390 adg_path_line_to_explicit(symbol
, 24, 4);
391 adg_path_line_to_explicit(symbol
, 4, 9);
392 adg_path_close(symbol
);
393 adg_path_move_to_explicit(symbol
, 49, 14);
394 adg_path_arc_to_explicit(symbol
, 29, 14, 49, 14);
395 adg_path_move_to_explicit(symbol
, 44, 14);
396 adg_path_arc_to_explicit(symbol
, 34, 14, 44, 14);
398 axis
= adg_path_new();
399 adg_path_move_to_explicit(axis
, 0, 14);
400 adg_path_line_to_explicit(axis
, 53, 14);
401 adg_path_move_to_explicit(axis
, 39, 0);
402 adg_path_line_to_explicit(axis
, 39, 28);
404 case ADG_PROJECTION_THIRD_ANGLE
:
405 symbol
= adg_path_new();
406 adg_path_move_to_explicit(symbol
, 29, 19);
407 adg_path_line_to_explicit(symbol
, 49, 24);
408 adg_path_line_to_explicit(symbol
, 49, 4);
409 adg_path_line_to_explicit(symbol
, 29, 9);
410 adg_path_close(symbol
);
411 adg_path_move_to_explicit(symbol
, 24, 14);
412 adg_path_arc_to_explicit(symbol
, 4, 14, 24, 14);
413 adg_path_move_to_explicit(symbol
, 19, 14);
414 adg_path_arc_to_explicit(symbol
, 9, 14, 19, 14);
416 axis
= adg_path_new();
417 adg_path_move_to_explicit(axis
, 0, 14);
418 adg_path_line_to_explicit(axis
, 53, 14);
419 adg_path_move_to_explicit(axis
, 14, 0);
420 adg_path_line_to_explicit(axis
, 14, 28);
423 g_return_if_reached();
427 data_class
->symbol
= symbol
;
428 data_class
->axis
= axis
;
429 data_class
->extents
.is_defined
= FALSE
;
432 cpml_extents_add(&data_class
->extents
,
433 adg_trail_get_extents((AdgTrail
*) axis
));
436 cpml_extents_add(&data_class
->extents
,
437 adg_trail_get_extents((AdgTrail
*) symbol
));
441 render(AdgEntity
*entity
, cairo_t
*cr
)
443 AdgProjectionClassPrivate
*data_class
;
444 AdgProjectionPrivate
*data
;
445 const cairo_path_t
*cairo_path
;
447 data_class
= ADG_PROJECTION_GET_CLASS(entity
)->data_class
;
448 data
= ((AdgProjection
*) entity
)->data
;
450 if (data_class
->symbol
!= NULL
) {
451 cairo_path
= adg_trail_get_cairo_path((AdgTrail
*) data_class
->symbol
);
454 cairo_transform(cr
, adg_entity_get_local_matrix(entity
));
455 cairo_append_path(cr
, cairo_path
);
458 cairo_set_line_width(cr
, 2);
459 adg_entity_apply_dress(entity
, data
->symbol_dress
, cr
);
464 if (data_class
->axis
!= NULL
) {
465 const gdouble dashes
[] = { 5, 2, 1, 2 };
467 cairo_path
= adg_trail_get_cairo_path((AdgTrail
*) data_class
->axis
);
470 cairo_transform(cr
, adg_entity_get_local_matrix(entity
));
471 cairo_append_path(cr
, cairo_path
);
474 cairo_set_line_width(cr
, 1);
475 cairo_set_dash(cr
, dashes
, G_N_ELEMENTS(dashes
), -1.5);
476 adg_entity_apply_dress(entity
, data
->axis_dress
, cr
);
483 set_scheme(AdgProjection
*projection
, AdgProjectionScheme scheme
)
485 AdgProjectionPrivate
*data
;
487 g_return_val_if_fail(adg_is_enum_value(scheme
, ADG_TYPE_PROJECTION_SCHEME
),
490 data
= projection
->data
;
492 if (data
->scheme
== scheme
)
495 data
->scheme
= scheme
;
497 adg_entity_invalidate((AdgEntity
*) projection
);