adg: renamed AdgMixMethod to AdgMix
[adg.git] / src / adg / adg-title-block.c
blob2bbcf8eb5205f2f38ec4977b2062507da2c2f471
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.
21 /**
22 * SECTION:adg-title-block
23 * @short_description: A title block entity
25 * Title blocks are commonly used in technical drawings to include
26 * additional information not strictly related to physical dimensions,
27 * such as title, material of the represented part, special treatments,
28 * date and scale etc.
30 * Actually this entity is only a place-holder: it will be implemented
31 * properly in a 0.6.x release, after having AdgToyTable in place.
33 * Since: 1.0
34 **/
36 /**
37 * AdgTitleBlock:
39 * All fields are privates and should not be used directly.
40 * Use its public methods instead.
42 * Since: 1.0
43 **/
46 #include "adg-internal.h"
48 #include "adg-table.h"
49 #include "adg-table-row.h"
50 #include "adg-table-cell.h"
52 #include "adg-title-block.h"
53 #include "adg-title-block-private.h"
56 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_title_block_parent_class)
59 G_DEFINE_TYPE(AdgTitleBlock, adg_title_block, ADG_TYPE_TABLE);
61 enum {
62 PROP_0,
63 PROP_TITLE,
64 PROP_DRAWING,
65 PROP_SIZE,
66 PROP_SCALE,
67 PROP_AUTHOR,
68 PROP_DATE,
69 PROP_LOGO,
70 PROP_PROJECTION
74 static void _adg_finalize (GObject *object);
75 static void _adg_get_property (GObject *object,
76 guint prop_id,
77 GValue *value,
78 GParamSpec *pspec);
79 static void _adg_set_property (GObject *object,
80 guint prop_id,
81 const GValue *value,
82 GParamSpec *pspec);
85 static void
86 adg_title_block_class_init(AdgTitleBlockClass *klass)
88 GObjectClass *gobject_class;
89 GParamSpec *param;
91 gobject_class = (GObjectClass *) klass;
93 g_type_class_add_private(klass, sizeof(AdgTitleBlockPrivate));
95 gobject_class->finalize = _adg_finalize;
96 gobject_class->set_property = _adg_set_property;
97 gobject_class->get_property = _adg_get_property;
99 param = g_param_spec_string("title",
100 P_("Title"),
101 P_("A descriptive title of the drawing"),
102 NULL,
103 G_PARAM_READWRITE);
104 g_object_class_install_property(gobject_class, PROP_TITLE, param);
106 param = g_param_spec_string("drawing",
107 P_("Drawing Name"),
108 P_("The name of the drawing: the ADG canvas does not make any assumtpion on this text string"),
109 NULL,
110 G_PARAM_READWRITE);
111 g_object_class_install_property(gobject_class, PROP_DRAWING, param);
113 param = g_param_spec_string("size",
114 P_("Media Size"),
115 P_("The media size to be used to print the drawing, usually something like \"A3\" or \"Letter\""),
116 NULL,
117 G_PARAM_READWRITE);
118 g_object_class_install_property(gobject_class, PROP_SIZE, param);
120 param = g_param_spec_string("scale",
121 P_("Scale"),
122 P_("The scale of the drawing, if it makes sense"),
123 NULL,
124 G_PARAM_READWRITE);
125 g_object_class_install_property(gobject_class, PROP_SCALE, param);
127 param = g_param_spec_string("author",
128 P_("Author"),
129 P_("Name and last name of the author of the drawing"),
130 NULL,
131 G_PARAM_READWRITE);
132 g_object_class_install_property(gobject_class, PROP_AUTHOR, param);
134 param = g_param_spec_string("date",
135 P_("Date"),
136 P_("The date this drawing has been generated: setting it to an empty string will fallback to today in the preferred representation for the current locale"),
137 NULL,
138 G_PARAM_READWRITE);
139 g_object_class_install_property(gobject_class, PROP_DATE, param);
141 param = g_param_spec_object("logo",
142 P_("Logo"),
143 P_("An entity to be displayed in the title block as the logo of the owner: the containing cell has a 1:1 ratio"),
144 ADG_TYPE_ENTITY,
145 G_PARAM_READWRITE);
146 g_object_class_install_property(gobject_class, PROP_LOGO, param);
148 param = g_param_spec_object("projection",
149 P_("Projection Scheme"),
150 P_("The entity usually reserved to identify the projection scheme adopted by this drawing"),
151 ADG_TYPE_ENTITY,
152 G_PARAM_READWRITE);
153 g_object_class_install_property(gobject_class, PROP_PROJECTION, param);
156 static void
157 adg_title_block_init(AdgTitleBlock *title_block)
159 AdgTable *table = (AdgTable *) title_block;
160 AdgTableRow *row;
161 AdgTitleBlockPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(title_block,
162 ADG_TYPE_TITLE_BLOCK,
163 AdgTitleBlockPrivate);
165 data->author = NULL;
166 data->title = NULL;
167 data->drawing = NULL;
168 data->size = NULL;
169 data->scale = NULL;
170 data->author = NULL;
171 data->date = NULL;
172 data->projection = NULL;
174 title_block->data = data;
176 /* Create the title block template*/
178 /* First row */
179 row = adg_table_row_new(table);
180 adg_table_cell_new_with_width(row, 62);
181 adg_table_cell_new_full(row, 200, "title", _("TITLE"), TRUE);
183 /* Second row */
184 row = adg_table_row_new(table);
185 adg_table_cell_new_full(row, 62, "logo", NULL, FALSE);
186 adg_table_cell_new_full(row, 40, "size", _("SIZE"), TRUE);
187 adg_table_cell_new_full(row, 60, "scale", _("SCALE"), TRUE);
188 adg_table_cell_new_full(row, 100, "drawing", _("DRAWING"), TRUE);
190 /* Third row */
191 row = adg_table_row_new(table);
192 adg_table_cell_new_full(row, 62, "projection", NULL, TRUE);
193 adg_table_cell_new_full(row, 100, "author", _("AUTHOR"), TRUE);
194 adg_table_cell_new_full(row, 100, "date", _("DATE"), TRUE);
197 static void
198 _adg_finalize(GObject *object)
200 AdgTitleBlockPrivate *data = ((AdgTitleBlock *) object)->data;
202 g_free(data->title);
203 g_free(data->drawing);
204 g_free(data->size);
205 g_free(data->scale);
206 g_free(data->author);
207 g_free(data->date);
209 if (_ADG_OLD_OBJECT_CLASS->finalize)
210 _ADG_OLD_OBJECT_CLASS->finalize(object);
213 static void
214 _adg_get_property(GObject *object, guint prop_id,
215 GValue *value, GParamSpec *pspec)
217 AdgTitleBlockPrivate *data = ((AdgTitleBlock *) object)->data;
219 switch (prop_id) {
220 case PROP_TITLE:
221 g_value_set_string(value, data->title);
222 break;
223 case PROP_DRAWING:
224 g_value_set_string(value, data->drawing);
225 break;
226 case PROP_SIZE:
227 g_value_set_string(value, data->size);
228 break;
229 case PROP_SCALE:
230 g_value_set_string(value, data->scale);
231 break;
232 case PROP_AUTHOR:
233 g_value_set_string(value, data->author);
234 break;
235 case PROP_DATE:
236 g_value_set_string(value, data->date);
237 break;
238 case PROP_LOGO:
239 g_value_set_object(value, data->logo);
240 break;
241 case PROP_PROJECTION:
242 g_value_set_object(value, data->projection);
243 break;
244 default:
245 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
246 break;
250 static void
251 _adg_set_property(GObject *object, guint prop_id,
252 const GValue *value, GParamSpec *pspec)
254 AdgTitleBlock *title_block;
255 AdgTitleBlockPrivate *data;
256 AdgTable *table;
257 AdgTableCell *cell;
259 title_block = (AdgTitleBlock *) object;
260 data = title_block->data;
261 table = (AdgTable *) object;
263 switch (prop_id) {
264 case PROP_TITLE:
265 g_free(data->title);
266 data->title = g_value_dup_string(value);
267 cell = adg_table_get_cell(table, "title");
268 adg_table_cell_set_text_value(cell, data->title);
269 break;
270 case PROP_DRAWING:
271 g_free(data->drawing);
272 data->drawing = g_value_dup_string(value);
273 cell = adg_table_get_cell(table, "drawing");
274 adg_table_cell_set_text_value(cell, data->drawing);
275 break;
276 case PROP_SIZE:
277 g_free(data->size);
278 data->size = g_value_dup_string(value);
279 cell = adg_table_get_cell(table, "size");
280 adg_table_cell_set_text_value(cell, data->size);
281 break;
282 case PROP_SCALE:
283 g_free(data->scale);
284 data->scale = g_value_dup_string(value);
285 cell = adg_table_get_cell(table, "scale");
286 adg_table_cell_set_text_value(cell, data->scale);
287 break;
288 case PROP_AUTHOR:
289 g_free(data->author);
290 data->author = g_value_dup_string(value);
291 cell = adg_table_get_cell(table, "author");
292 adg_table_cell_set_text_value(cell, data->author);
293 break;
294 case PROP_DATE:
295 g_free(data->date);
296 if (g_value_get_string(value) == NULL) {
297 /* NULL means the date must be automatically updated */
298 GDate *gdate;
299 char buffer[100] = { 0 };
301 gdate = g_date_new();
302 g_date_set_time_t(gdate, time (NULL));
303 g_date_strftime(buffer, sizeof(buffer), "%x", gdate);
304 g_date_free(gdate);
306 data->date = g_strdup(buffer);
307 } else {
308 data->date = g_value_dup_string(value);
310 cell = adg_table_get_cell(table, "date");
311 adg_table_cell_set_text_value(cell, data->date);
312 break;
313 case PROP_LOGO:
314 data->logo = g_value_get_object(value);
315 cell = adg_table_get_cell(table, "logo");
316 adg_table_cell_set_value(cell, data->logo);
317 adg_table_cell_set_value_pos_explicit(cell, 0.5, 1, 0.5, 0.5);
318 break;
319 case PROP_PROJECTION:
320 data->projection = g_value_get_object(value);
321 cell = adg_table_get_cell(table, "projection");
322 adg_table_cell_set_value(cell, data->projection);
323 adg_table_cell_set_value_pos_explicit(cell, 0.5, 0.5, 0.5, 0.5);
324 break;
325 default:
326 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
327 break;
333 * adg_title_block_new:
335 * Creates a new empty title block entity. The #AdgEntity:local-mix
336 * property is set by default to #ADG_MIX_DISABLED, that is the
337 * title block is not subject to any local transformations.
339 * Returns: (transfer full): the newly created title block entity.
341 * Since: 1.0
343 AdgTitleBlock *
344 adg_title_block_new(void)
346 return g_object_new(ADG_TYPE_TITLE_BLOCK,
347 "local-mix", ADG_MIX_DISABLED, NULL);
351 * adg_title_block_set_title:
352 * @title_block: an #AdgTitleBlock entity
353 * @title: the new title
355 * Sets a new title on the title block.
357 * Since: 1.0
359 void
360 adg_title_block_set_title(AdgTitleBlock *title_block, const gchar *title)
362 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
363 g_object_set(title_block, "title", title, NULL);
367 * adg_title_block_get_title:
368 * @title_block: an #AdgTitleBlock entity
370 * Gets the descriptive title associated to this title block.
371 * The returned string is owned by @title_block and should not
372 * be modifed or freed.
374 * Returns: (transfer none): the title or %NULL on no title or errors.
376 * Since: 1.0
378 const gchar *
379 adg_title_block_get_title(AdgTitleBlock *title_block)
381 AdgTitleBlockPrivate *data;
383 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
385 data = title_block->data;
387 return data->title;
391 * adg_title_block_set_drawing:
392 * @title_block: an #AdgTitleBlock entity
393 * @drawing: the new drawing name
395 * Sets a new drawing name on the title block.
397 * Since: 1.0
399 void
400 adg_title_block_set_drawing(AdgTitleBlock *title_block, const gchar *drawing)
402 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
403 g_object_set(title_block, "drawing", drawing, NULL);
407 * adg_title_block_get_drawing:
408 * @title_block: an #AdgTitleBlock entity
410 * Gets the drawing name, commonly used to specify the file name.
411 * The returned string is owned by @title_block and should not
412 * be modifed or freed.
414 * Returns: (transfer none): the drawing name or %NULL on no name or errors.
416 * Since: 1.0
418 const gchar *
419 adg_title_block_get_drawing(AdgTitleBlock *title_block)
421 AdgTitleBlockPrivate *data;
423 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
425 data = title_block->data;
427 return data->drawing;
431 * adg_title_block_set_size:
432 * @title_block: an #AdgTitleBlock entity
433 * @size: the new size
435 * Sets a new size on the title block.
437 * Since: 1.0
439 void
440 adg_title_block_set_size(AdgTitleBlock *title_block, const gchar *size)
442 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
443 g_object_set(title_block, "size", size, NULL);
447 * adg_title_block_get_size:
448 * @title_block: an #AdgTitleBlock entity
450 * Gets the media size (a descriptive name) where this drawing will
451 * be printed. Usually contains something like "A4" or "Letter".
452 * The returned string is owned by @title_block and should not
453 * be modifed or freed.
455 * Returns: (transfer none): the size or %NULL on no size or errors.
457 * Since: 1.0
459 const gchar *
460 adg_title_block_get_size(AdgTitleBlock *title_block)
462 AdgTitleBlockPrivate *data;
464 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
466 data = title_block->data;
468 return data->size;
472 * adg_title_block_set_scale:
473 * @title_block: an #AdgTitleBlock entity
474 * @scale: the new scale
476 * Sets a new scale on the title block.
478 * Since: 1.0
480 void
481 adg_title_block_set_scale(AdgTitleBlock *title_block, const gchar *scale)
483 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
484 g_object_set(title_block, "scale", scale, NULL);
488 * adg_title_block_get_scale:
489 * @title_block: an #AdgTitleBlock entity
491 * Gets the scale descriptive name of the drawing.
493 * Returns: (transfer none): the scale text or %NULL on no scale or errors.
495 * Since: 1.0
497 const gchar *
498 adg_title_block_get_scale(AdgTitleBlock *title_block)
500 AdgTitleBlockPrivate *data;
502 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
504 data = title_block->data;
506 return data->scale;
510 * adg_title_block_set_author:
511 * @title_block: an #AdgTitleBlock entity
512 * @author: the new author
514 * Sets a new author on the title block.
516 * Since: 1.0
518 void
519 adg_title_block_set_author(AdgTitleBlock *title_block, const gchar *author)
521 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
522 g_object_set(title_block, "author", author, NULL);
526 * adg_title_block_get_author:
527 * @title_block: an #AdgTitleBlock entity
529 * Gets the author's name of the drawing.
531 * Returns: (transfer none): the author or %NULL on no author or errors.
533 * Since: 1.0
535 const gchar *
536 adg_title_block_get_author(AdgTitleBlock *title_block)
538 AdgTitleBlockPrivate *data;
540 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
542 data = title_block->data;
544 return data->author;
548 * adg_title_block_set_date:
549 * @title_block: an #AdgTitleBlock entity
550 * @date: the new date
552 * Sets a new date on the title block. By default the date is
553 * set to %NULL and it will be implicitely rendered using the
554 * preferred representation for the current local of the actual
555 * date. This is roughly equivalent to:
557 * |[
558 * strftime(buffer, sizeof(buffer), "%x", now);
559 * adg_title_block_set_date(title_block, buffer);
560 * ]|
562 * To not render any value, use an empty string as @date.
564 * Since: 1.0
566 void
567 adg_title_block_set_date(AdgTitleBlock *title_block, const gchar *date)
569 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
570 g_object_set(title_block, "date", date, NULL);
574 * adg_title_block_get_date:
575 * @title_block: an #AdgTitleBlock entity
577 * Gets the date of the rendering set on @title_block.
579 * Returns: (transfer none): the date or %NULL on no date or errors.
581 * Since: 1.0
583 const gchar *
584 adg_title_block_get_date(AdgTitleBlock *title_block)
586 AdgTitleBlockPrivate *data;
588 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
590 data = title_block->data;
592 return data->date;
596 * adg_title_block_set_logo:
597 * @title_block: an #AdgTitleBlock entity
598 * @logo: the new logo
600 * Sets a new logo on the title block. This function will add
601 * a reference to @logo, removing the eventual reference held
602 * to the old logo, hence possibly destroying the old endity.
604 * The space reserved for the logo is 56x56, so try to keep the
605 * new logo near this size or scale it accordingly.
607 * Since: 1.0
609 void
610 adg_title_block_set_logo(AdgTitleBlock *title_block, AdgEntity *logo)
612 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
613 g_object_set(title_block, "logo", logo, NULL);
617 * adg_title_block_logo:
618 * @title_block: an #AdgTitleBlock entity
620 * Gets the logo bound to this title block.
621 * The returned object is owned by @title_block and should not
622 * be unreferenced although can be freely modified.
624 * Returns: (transfer none): the logo or %NULL on no logo or errors.
626 * Since: 1.0
628 AdgEntity *
629 adg_title_block_logo(AdgTitleBlock *title_block)
631 AdgTitleBlockPrivate *data;
633 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
635 data = title_block->data;
637 return data->logo;
641 * adg_title_block_set_projection:
642 * @title_block: an #AdgTitleBlock entity
643 * @projection: the new projection
645 * Sets a new projection symbol on the title block. This function
646 * will add a reference to @projection, removing the eventual
647 * reference held to the old symbol, hence possibly destroying
648 * the old endity.
650 * The space reserved for the projection is 56x56, so try to keep the
651 * new projection near this size or scale it accordingly.
653 * Since: 1.0
655 void
656 adg_title_block_set_projection(AdgTitleBlock *title_block,
657 AdgEntity *projection)
659 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
660 g_object_set(title_block, "projection", projection, NULL);
664 * adg_title_block_projection:
665 * @title_block: an #AdgTitleBlock entity
667 * Gets the projection bound to this title block.
668 * The returned object is owned by @title_block and should not
669 * be unreferenced although can be freely modified.
671 * Returns: (transfer none): the projection or
672 * %NULL on no projection or errors.
674 * Since: 1.0
676 AdgEntity *
677 adg_title_block_projection(AdgTitleBlock *title_block)
679 AdgTitleBlockPrivate *data;
681 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
683 data = title_block->data;
685 return data->projection;