File: chronoref.c

package info (click to toggle)
dia 0.98%2Bgit20250827-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,064 kB
  • sloc: ansic: 155,731; xml: 14,062; python: 6,250; cpp: 3,635; sh: 447; perl: 137; makefile: 29
file content (482 lines) | stat: -rw-r--r-- 15,162 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* Dia -- an diagram creation/manipulation program
 * Copyright (C) 1998 Alexander Larsson
 *
 * Chronogram objects support
 * Copyright (C) 2000, 2001 Cyrille Chepelov
 *
 * Ultimately forked from Flowchart toolbox -- objects for drawing flowcharts.
 * Copyright (C) 1999 James Henstridge.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <glib/gi18n-lib.h>

#include <math.h>
#include <string.h>
#include <glib.h>

#include "object.h"
#include "element.h"
#include "connectionpoint.h"
#include "diarenderer.h"
#include "attributes.h"
#include "text.h"
#include "connpoint_line.h"
#include "color.h"
#include "properties.h"

#include "chronogram.h"
#include "pixmaps/chronoref.xpm"

#define DEFAULT_WIDTH 7.0
#define DEFAULT_HEIGHT 5.0

typedef struct _Chronoref {
  Element element;

  real main_lwidth;
  real light_lwidth;
  Color color;
  real start_time;
  real end_time;
  real time_step;
  real time_lstep;

  DiaFont *font;
  real font_size;
  Color font_color;

  ConnPointLine *scale; /* not saved ; num_connections derived from
			   start_time, end_time, time_step. */
  real majgrad_height,mingrad_height;
  real firstmaj,firstmin; /* in time units */
  real firstmaj_x,firstmin_x,majgrad,mingrad; /* in dia graphic units */
  int  spec;
} Chronoref;

static real chronoref_distance_from(Chronoref *chronoref, Point *point);
static void chronoref_select(Chronoref *chronoref, Point *clicked_point,
		       DiaRenderer *interactive_renderer);
static DiaObjectChange *chronoref_move_handle  (Chronoref        *chronoref,
                                                Handle           *handle,
                                                Point            *to,
                                                ConnectionPoint  *cp,
                                                HandleMoveReason  reason,
                                                ModifierKeys      modifiers);
static DiaObjectChange *chronoref_move         (Chronoref        *chronoref,
                                                Point            *to);
static void chronoref_draw(Chronoref *chronoref, DiaRenderer *renderer);
static void chronoref_update_data(Chronoref *chronoref);
static DiaObject *chronoref_create(Point *startpoint,
			  void *user_data,
			  Handle **handle1,
			  Handle **handle2);
static void chronoref_destroy(Chronoref *chronoref);
static DiaObject *chronoref_load(ObjectNode obj_node, int version,
				 DiaContext *ctx);
static PropDescription *chronoref_describe_props(Chronoref *chronoref);
static void chronoref_get_props(Chronoref *chronoref,
                                 GPtrArray *props);
static void chronoref_set_props(Chronoref *chronoref,
                                 GPtrArray *props);



static ObjectTypeOps chronoref_type_ops =
{
  (CreateFunc) chronoref_create,
  (LoadFunc)   chronoref_load/*using properties*/,
  (SaveFunc)   object_save_using_properties,
  (GetDefaultsFunc)   NULL,
  (ApplyDefaultsFunc) NULL
};

DiaObjectType chronoref_type =
{
  "chronogram - reference", /* name */
  0,                     /* version */
  chronoref_xpm,          /* pixmap */
  &chronoref_type_ops        /* ops */
};


static ObjectOps chronoref_ops = {
  (DestroyFunc)         chronoref_destroy,
  (DrawFunc)            chronoref_draw,
  (DistanceFunc)        chronoref_distance_from,
  (SelectFunc)          chronoref_select,
  (CopyFunc)            object_copy_using_properties,
  (MoveFunc)            chronoref_move,
  (MoveHandleFunc)      chronoref_move_handle,
  (GetPropertiesFunc)   object_create_props_dialog,
  (ApplyPropertiesDialogFunc) object_apply_props_from_dialog,
  (ObjectMenuFunc)      NULL,
  (DescribePropsFunc)   chronoref_describe_props,
  (GetPropsFunc)        chronoref_get_props,
  (SetPropsFunc)        chronoref_set_props,
  (TextEditFunc) 0,
  (ApplyPropertiesListFunc) object_apply_props,
};

static PropNumData time_range = { -32767.0, 32768.0, 0.1};
static PropNumData step_range = { 0.0, 1000.0, 0.1};

static PropDescription chronoref_props[] = {
  ELEMENT_COMMON_PROPERTIES,
  PROP_MULTICOL_BEGIN("chronoref"),

  PROP_MULTICOL_COLUMN("time"),
  PROP_FRAME_BEGIN("time",0,N_("Time data")),
  { "start_time",PROP_TYPE_REAL,PROP_FLAG_VISIBLE,
    N_("Start time"),NULL,&time_range},
  { "end_time",PROP_TYPE_REAL,PROP_FLAG_VISIBLE,
    N_("End time"),NULL,&time_range},
  { "time_step",PROP_TYPE_REAL,PROP_FLAG_VISIBLE,
    N_("Major time step"),NULL,&step_range},
  { "time_lstep",PROP_TYPE_REAL,PROP_FLAG_VISIBLE,
    N_("Minor time step"),NULL,&step_range},
  PROP_FRAME_END("time",0),

  PROP_MULTICOL_COLUMN("aspect"),
  PROP_FRAME_BEGIN("aspect",0,N_("Aspect")),
  { "color", PROP_TYPE_COLOUR, PROP_FLAG_VISIBLE,
    N_("Line color"),NULL},
  { "main_lwidth", PROP_TYPE_LENGTH, PROP_FLAG_VISIBLE,
    N_("Line width"),NULL, &prop_std_line_width_data},
  { "light_lwidth", PROP_TYPE_LENGTH, PROP_FLAG_VISIBLE,
    N_("Minor step line width"),NULL, &prop_std_line_width_data},

  { "font", PROP_TYPE_FONT, PROP_FLAG_VISIBLE, N_("Font"), NULL, NULL },
  { "font_size", PROP_TYPE_FONTSIZE, PROP_FLAG_VISIBLE,
    N_("Font size"), NULL, &prop_std_text_height_data },
  { "font_color", PROP_TYPE_COLOUR, PROP_FLAG_VISIBLE,
    N_("Text color"), NULL, NULL },
  PROP_FRAME_END("aspect",0),

  PROP_MULTICOL_END("chronoref"),
  {NULL}
};

static PropDescription *
chronoref_describe_props(Chronoref *chronoref)
{
  if (chronoref_props[0].quark == 0) {
    prop_desc_list_calculate_quarks(chronoref_props);
  }
  return chronoref_props;
}

static PropOffset chronoref_offsets[] = {
  ELEMENT_COMMON_PROPERTIES_OFFSETS,
  PROP_OFFSET_MULTICOL_BEGIN("chronref"),

  PROP_OFFSET_MULTICOL_COLUMN("time"),
  PROP_OFFSET_FRAME_BEGIN("time"),
  { "start_time",PROP_TYPE_REAL, offsetof(Chronoref,start_time)},
  { "end_time",PROP_TYPE_REAL, offsetof(Chronoref,end_time)},
  { "time_step",PROP_TYPE_REAL, offsetof(Chronoref,time_step)},
  { "time_lstep",PROP_TYPE_REAL, offsetof(Chronoref,time_lstep)},
  PROP_OFFSET_FRAME_END("time"),

  PROP_OFFSET_MULTICOL_COLUMN("aspect"),
  PROP_OFFSET_FRAME_BEGIN("aspect"),
  { "color", PROP_TYPE_COLOUR, offsetof(Chronoref,color)},
  { "main_lwidth", PROP_TYPE_LENGTH, offsetof(Chronoref,main_lwidth)},
  { "light_lwidth", PROP_TYPE_LENGTH, offsetof(Chronoref,light_lwidth)},
  { "font", PROP_TYPE_FONT, offsetof(Chronoref,font)},
  { "font_size", PROP_TYPE_FONTSIZE, offsetof(Chronoref,font_size)},
  { "font_color", PROP_TYPE_COLOUR, offsetof(Chronoref,font_color)},
  PROP_OFFSET_FRAME_END("aspect"),

  PROP_OFFSET_MULTICOL_END("chronref"),
  {NULL}
};

static void
chronoref_get_props(Chronoref *chronoref, GPtrArray *props)
{
  object_get_props_from_offsets(&chronoref->element.object,
                                chronoref_offsets,props);
}

static void
chronoref_set_props(Chronoref *chronoref, GPtrArray *props)
{
  object_set_props_from_offsets(&chronoref->element.object,
                                chronoref_offsets,props);
  chronoref_update_data(chronoref);
}

static real
chronoref_distance_from(Chronoref *chronoref, Point *point)
{
  DiaObject *obj = &chronoref->element.object;
  return distance_rectangle_point(&obj->bounding_box, point);
}

static void
chronoref_select(Chronoref *chronoref, Point *clicked_point,
	   DiaRenderer *interactive_renderer)
{
  element_update_handles(&chronoref->element);
}


static DiaObjectChange *
chronoref_move_handle (Chronoref        *chronoref,
                       Handle           *handle,
                       Point            *to,
                       ConnectionPoint  *cp,
                       HandleMoveReason  reason,
                       ModifierKeys      modifiers)
{
  g_assert(chronoref!=NULL);
  g_assert(handle!=NULL);
  g_assert(to!=NULL);

  element_move_handle (&chronoref->element,
                       handle->id,
                       to,
                       cp,
                       reason,
                       modifiers);
  chronoref_update_data (chronoref);

  return NULL;
}


static DiaObjectChange *
chronoref_move (Chronoref *chronoref, Point *to)
{
  chronoref->element.corner = *to;
  chronoref_update_data (chronoref);

  return NULL;
}


static void
chronoref_draw (Chronoref *chronoref, DiaRenderer *renderer)
{
  Element *elem;
  Point lr_corner;
  real t;
  Point p1,p2,p3;

  g_return_if_fail (renderer != NULL);

  elem = &chronoref->element;

  dia_renderer_set_linestyle (renderer, DIA_LINE_STYLE_SOLID, 0.0);
  dia_renderer_set_linejoin (renderer, DIA_LINE_JOIN_MITER);


  lr_corner.x = elem->corner.x + elem->width;
  lr_corner.y = elem->corner.y + elem->height;

  p1.y = p2.y = elem->corner.y;

  dia_renderer_set_font (renderer, chronoref->font, chronoref->font_size);
  p3.y = p2.y + chronoref->majgrad_height +
    dia_font_ascent ("1",chronoref->font, chronoref->font_size);

  dia_renderer_set_linewidth (renderer, chronoref->light_lwidth);
  if (chronoref->time_lstep > 0.0) {
    p2.y = p1.y + chronoref->mingrad_height;
    for (t = chronoref->firstmaj, p1.x = chronoref->firstmin_x;
         p1.x <= lr_corner.x;
         t += chronoref->time_lstep, p1.x += chronoref->mingrad) {
      p2.x = p1.x;

      dia_renderer_draw_line (renderer,&p1,&p2,&chronoref->color);
    }
  }

  dia_renderer_set_linewidth (renderer, chronoref->main_lwidth);
  if (chronoref->time_step > 0.0) {
    p2.y = p1.y + chronoref->majgrad_height;

    for (t = chronoref->firstmaj, p1.x = chronoref->firstmaj_x;
         p1.x <= lr_corner.x;
         t += chronoref->time_step, p1.x += chronoref->majgrad) {
      char time[10];
      p3.x = p2.x = p1.x;

      dia_renderer_draw_line (renderer,&p1,&p2,&chronoref->color);
      g_snprintf (time,sizeof(time),"%.*f",chronoref->spec,t);
      dia_renderer_draw_string (renderer,
                                time,
                                &p3,
                                DIA_ALIGN_CENTRE,
                                &chronoref->font_color);
    }
  }
  p1.x = elem->corner.x;
  p2.x = lr_corner.x;
  p1.y = p2.y = elem->corner.y;

  dia_renderer_draw_line (renderer,&p1,&p2,&chronoref->color);
}

static void
chronoref_update_data(Chronoref *chronoref)
{
  Element *elem = &chronoref->element;
  DiaObject *obj = &elem->object;
  real time_span,t;
  Point p1,p2;
  Point ur_corner;
  int shouldbe,i;
  real labelwidth;
  char biglabel[10];
  ElementBBExtras *extra = &elem->extra_spacing;

  chronoref->majgrad_height = elem->height;
  chronoref->mingrad_height = elem->height / 3.0;

  /* build i = -log_{10}(time_step), then make a %.if format out of it. */
  t = 1;
  i = 0;

  while (t > chronoref->time_step) {
    t /= 10;
    i++;
  }
  chronoref->spec = i; /* update precision */
  g_snprintf(biglabel,sizeof(biglabel),"%.*f", chronoref->spec,
	   MIN(-ABS(chronoref->start_time),-ABS(chronoref->end_time)));

  labelwidth = dia_font_string_width(biglabel,chronoref->font,
                                     chronoref->font_size);

  /* Now, update the drawing helper counters */
  time_span = chronoref->end_time - chronoref->start_time;
  if (time_span == 0) {
    chronoref->end_time = chronoref->start_time + .1;
    time_span = .1;
  } else if (time_span < 0) {
    chronoref->start_time = chronoref->end_time;
    time_span = -time_span;
    chronoref->end_time = chronoref->start_time + time_span;
  }

  chronoref->firstmaj = chronoref->time_step *
    ceil(chronoref->start_time / chronoref->time_step);
  if (chronoref->firstmaj < chronoref->start_time)
    chronoref->firstmaj += chronoref->time_step;
  chronoref->firstmin = chronoref->time_lstep *
    ceil(chronoref->start_time / chronoref->time_lstep);
  if (chronoref->firstmin < chronoref->start_time)
    chronoref->firstmin += chronoref->time_lstep;

  chronoref->firstmaj_x = elem->corner.x +
    elem->width*((chronoref->firstmaj-chronoref->start_time)/time_span);
  chronoref->firstmin_x = elem->corner.x +
    elem->width*((chronoref->firstmin-chronoref->start_time)/time_span);
  chronoref->majgrad = (chronoref->time_step * elem->width) / time_span;
  chronoref->mingrad = (chronoref->time_lstep * elem->width) / time_span;

  extra->border_trans = chronoref->main_lwidth/2;
  element_update_boundingbox(elem);

  /* fix boundingbox for special extras: */
  obj->bounding_box.left -= (chronoref->font_size + labelwidth)/2;
  obj->bounding_box.bottom += chronoref->font_size;
  obj->bounding_box.right += (chronoref->font_size + labelwidth)/2;

  obj->position = elem->corner;

  element_update_handles(elem);

  /* Update connections: */
  ur_corner.x = elem->corner.x + elem->width;
  ur_corner.y = elem->corner.y;

  shouldbe = (int)(ceil((chronoref->end_time-chronoref->firstmin)/
			   chronoref->time_lstep));
  if (shouldbe == 0) shouldbe++;
  if (shouldbe < 0) shouldbe = 0;
  shouldbe++; /* off by one.. */

  connpointline_adjust_count(chronoref->scale,shouldbe,&ur_corner);
  connpointline_update(chronoref->scale);

  point_copy(&p1,&elem->corner); point_copy(&p2,&ur_corner);
  p1.x -= chronoref->mingrad;
  p2.x += chronoref->mingrad;
  connpointline_putonaline(chronoref->scale,&p1,&p2, DIR_SOUTH);
}

static DiaObject *
chronoref_create(Point *startpoint,
	   void *user_data,
	   Handle **handle1,
	   Handle **handle2)
{
  Chronoref *chronoref;
  Element *elem;
  DiaObject *obj;

  chronoref = g_new0(Chronoref,1);
  elem = &(chronoref->element);

  obj =  &(chronoref->element.object);
  obj->type = &chronoref_type;
  obj->ops = &chronoref_ops;

  chronoref->scale = connpointline_create(obj,0);

  elem->corner = *startpoint;
  elem->width = 20.0;
  elem->height = 1.0;

  element_init(elem, 8, 0);

  chronoref->font = dia_font_new_from_style (DIA_FONT_SANS,1.0);
  chronoref->font_size = 1.0;
  chronoref->font_color = color_black;
  chronoref->start_time = 0.0;
  chronoref->end_time = 20.0;
  chronoref->time_step = 5.0;
  chronoref->time_lstep = 1.0;
  chronoref->color = color_black;
  chronoref->main_lwidth = .1;
  chronoref->light_lwidth = .05;

  chronoref_update_data(chronoref);

  *handle1 = NULL;
  *handle2 = obj->handles[7];
  return &chronoref->element.object;
}

static void
chronoref_destroy(Chronoref *chronoref)
{
  g_clear_object (&chronoref->font);
  connpointline_destroy (chronoref->scale);
  element_destroy (&chronoref->element);
}

static DiaObject *
chronoref_load(ObjectNode obj_node, int version, DiaContext *ctx)
{
  return object_load_using_properties(&chronoref_type,
                                      obj_node,version,ctx);
}