Debian Codegraph @

cmd/smyrna/viewport.c

raw
1 /*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11 #include "config.h"
12
13 #ifdef _WIN32
14 #include <windows.h>
15 #endif
16 #include <assert.h>
17 #include "viewport.h"
18 #include "draw.h"
19 #include <common/color.h>
20 #include <glade/glade.h>
21 #include "gui/gui.h"
22 #include <limits.h>
23 #include "gui/menucallbacks.h"
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "gui/glcompui.h"
28 #include "gltemplate.h"
29 #include <common/colorprocs.h>
30 #include "gui/topviewsettings.h"
31 #include "arcball.h"
32 #include "hotkeymap.h"
33 #include "topviewfuncs.h"
34 #include <util/alloc.h>
35 #include <util/exit.h>
36 #include <util/strcasecmp.h>
37
38 static colorschemaset create_color_theme(int themeid);
39
40 ViewInfo *view;
41
42 static void clear_viewport(ViewInfo *vi) {
43 if (vi->graphCount)
44 agclose(vi->g[vi->activeGraph]);
45 }
46 static void *get_glut_font(int ind)
47 {
48
49 switch (ind) {
50 case 0:
51 return GLUT_BITMAP_9_BY_15;
52 break;
53 case 1:
54 return GLUT_BITMAP_8_BY_13;
55 break;
56 case 2:
57 return GLUT_BITMAP_TIMES_ROMAN_10;
58 break;
59 case 3:
60 return GLUT_BITMAP_HELVETICA_10;
61 break;
62 case 4:
63 return GLUT_BITMAP_HELVETICA_12;
64 break;
65 case 5:
66 return GLUT_BITMAP_HELVETICA_18;
67 break;
68 default:
69 return GLUT_BITMAP_TIMES_ROMAN_10;
70 }
71
72 }
73
74 void close_graph(ViewInfo *vi) {
75 if (vi->activeGraph < 0)
76 return;
77 clear_viewport(vi);
78 }
79
80 char *get_attribute_value(char *attr, ViewInfo *vi, Agraph_t *g) {
81 char *buf;
82 buf = agget(g, attr);
83 if (!buf || *buf == '\0')
84 buf = agget(vi->systemGraphs.def_attrs, attr);
85 return buf;
86 }
87
88 void set_viewport_settings_from_template(ViewInfo *vi, Agraph_t *g) {
89 gvcolor_t cl;
90 colorxlate(get_attribute_value("bordercolor", vi, g), &cl,
91 RGBA_DOUBLE);
92 vi->borderColor.R = cl.u.RGBA[0];
93 vi->borderColor.G = cl.u.RGBA[1];
94 vi->borderColor.B = cl.u.RGBA[2];
95
96 vi->borderColor.A = atof(get_attribute_value("bordercoloralpha", vi, g));
97
98 vi->bdVisible = atoi(get_attribute_value("bordervisible", vi, g));
99
100 const char *buf = get_attribute_value("gridcolor", vi, g);
101 colorxlate(buf, &cl, RGBA_DOUBLE);
102 vi->gridColor.R = cl.u.RGBA[0];
103 vi->gridColor.G = cl.u.RGBA[1];
104 vi->gridColor.B = cl.u.RGBA[2];
105 vi->gridColor.A = atof(get_attribute_value("gridcoloralpha", vi, g));
106
107 vi->gridSize = (float)atof(get_attribute_value("gridsize", vi, g));
108
109 vi->gridVisible = atoi(get_attribute_value("gridvisible", vi, g));
110
111 //background color , default white
112 colorxlate(get_attribute_value("bgcolor", vi, g), &cl, RGBA_DOUBLE);
113
114 vi->bgColor.R = cl.u.RGBA[0];
115 vi->bgColor.G = cl.u.RGBA[1];
116 vi->bgColor.B = cl.u.RGBA[2];
117 vi->bgColor.A = 1.0;
118
119 //selected nodes are drawn with this color
120 colorxlate(get_attribute_value("selectednodecolor", vi, g), &cl,
121 RGBA_DOUBLE);
122 vi->selectedNodeColor.R = cl.u.RGBA[0];
123 vi->selectedNodeColor.G = cl.u.RGBA[1];
124 vi->selectedNodeColor.B = cl.u.RGBA[2];
125 vi->selectedNodeColor.A =
126 atof(get_attribute_value("selectednodecoloralpha", vi, g));
127
128 vi->defaultnodealpha = atof(get_attribute_value("defaultnodealpha", vi, g));
129
130 /*default line width */
131 vi->LineWidth =
132 (float) atof(get_attribute_value("defaultlinewidth", vi, g));
133
134 vi->drawnodes = atoi(get_attribute_value("drawnodes", vi, g));
135 vi->drawedges = atoi(get_attribute_value("drawedges", vi, g));
136 vi->drawnodelabels=atoi(get_attribute_value("labelshownodes", vi, g));
137 vi->drawedgelabels=atoi(get_attribute_value("labelshowedges", vi, g));
138 vi->nodeScale=atof(get_attribute_value("nodesize", vi, g))*.30;
139
140 vi->glutfont =
141 get_glut_font(atoi(get_attribute_value("labelglutfont", vi, g)));
142 vi->labelnumberofnodes =
143 atoi(get_attribute_value("labelnumberofnodes", vi, g));
144 vi->colschms =
145 create_color_theme(atoi
146 (get_attribute_value("colortheme", vi, g)));
147
148 if (vi->graphCount > 0)
149 glClearColor((float)vi->bgColor.R, (float)vi->bgColor.G, (float)vi->bgColor.B,
150 (float)vi->bgColor.A); // background color
151 }
152
153 static gboolean gl_main_expose(void *data) {
154 (void)data;
155
156 if (view->activeGraph >= 0) {
157 if (view->Topview->fisheyeParams.animate == 1)
158 expose_event(view->drawing_area, NULL, NULL);
159 return 1;
160 }
161 return 1;
162 }
163
164 static void get_data_dir(void)
165 {
166 free(view->template_file);
167 view->template_file = smyrnaPath("template.dot");
168 }
169
170 static void clear_color_theme(colorschemaset *cs) {
171 *cs = (colorschemaset){0};
172 }
173
174 void init_viewport(ViewInfo *vi) {
175 FILE *input_file = NULL;
176 FILE *input_file2 = NULL;
177 get_data_dir();
178 input_file = fopen(vi->template_file, "rb");
179 if (!input_file) {
180 fprintf(stderr,
181 "default attributes template graph file \"%s\" not found\n",
182 vi->template_file);
183 graphviz_exit(-1);
184 }
185 vi->systemGraphs.def_attrs = agread(input_file, NULL);
186 fclose (input_file);
187
188 if (!vi->systemGraphs.def_attrs) {
189 fprintf(stderr,
190 "could not load default attributes template graph file \"%s\"\n",
191 vi->template_file);
192 graphviz_exit(-1);
193 }
194 {
195 char *path = smyrnaPath("attr_widgets.dot");
196 input_file2 = fopen(path, "rb");
197 free(path);
198 }
199 if (!input_file2) {
200 char *attrwidgets = smyrnaPath("attr_widgets.dot");
201 fprintf(stderr, "default attributes template graph file \"%s\" not found\n", attrwidgets);
202 free(attrwidgets);
203 graphviz_exit(-1);
204
205 }
206 vi->systemGraphs.attrs_widgets = agread(input_file2, NULL);
207 fclose (input_file2);
208 if (!vi->systemGraphs.attrs_widgets) {
209 char *attrwidgets = smyrnaPath("attr_widgets.dot");
210 fprintf(stderr,"could not load default attribute widgets graph file \"%s\"\n", attrwidgets);
211 free(attrwidgets);
212 graphviz_exit(-1);
213 }
214 //init graphs
215 vi->g = NULL; //no graph, gl screen should check it
216 vi->graphCount = 0; //and disable interactivity if count is zero
217
218 vi->bdxLeft = 0;
219 vi->bdxRight = 500;
220 vi->bdyBottom = 0;
221 vi->bdyTop = 500;
222
223 vi->borderColor.R = 1;
224 vi->borderColor.G = 0;
225 vi->borderColor.B = 0;
226 vi->borderColor.A = 1;
227
228 vi->bdVisible = 1; //show borders red
229
230 vi->gridSize = 10;
231 vi->gridColor.R = 0.5;
232 vi->gridColor.G = 0.5;
233 vi->gridColor.B = 0.5;
234 vi->gridColor.A = 1;
235 vi->gridVisible = 0; //show grids in light gray
236
237 //mouse mode=pan
238 //pen color
239 vi->penColor.R = 0;
240 vi->penColor.G = 0;
241 vi->penColor.B = 0;
242 vi->penColor.A = 1;
243
244 vi->fillColor.R = 1;
245 vi->fillColor.G = 0;
246 vi->fillColor.B = 0;
247 vi->fillColor.A = 1;
248 //background color , default white
249 vi->bgColor.R = 1;
250 vi->bgColor.G = 1;
251 vi->bgColor.B = 1;
252 vi->bgColor.A = 1;
253
254 //selected objets are drawn with this color
255 vi->selectedNodeColor.R = 1;
256 vi->selectedNodeColor.G = 0;
257 vi->selectedNodeColor.B = 0;
258 vi->selectedNodeColor.A = 1;
259
260 //default line width;
261 vi->LineWidth = 1;
262
263 //default view settings, camera is not active
264 vi->panx = 0;
265 vi->pany = 0;
266
267 vi->zoom = -20;
268
269 vi->mouse.down = false;
270 vi->activeGraph = -1;
271 vi->Topview = gv_alloc(sizeof(topview));
272 vi->Topview->fisheyeParams.fs = 0;
273 vi->Topview->xDot=NULL;
274
275 /* init topfish parameters */
276 vi->Topview->fisheyeParams.level.num_fine_nodes = 10;
277 vi->Topview->fisheyeParams.level.coarsening_rate = 2.5;
278 vi->Topview->fisheyeParams.dist2_limit = true;
279 vi->Topview->fisheyeParams.repos.width = round(vi->bdxRight - vi->bdxLeft);
280 vi->Topview->fisheyeParams.repos.height = round(vi->bdyTop - vi->bdyBottom);
281 vi->Topview->fisheyeParams.repos.distortion = 1.0;
282 /*create timer */
283 vi->timer = g_timer_new();
284 vi->timer2 = g_timer_new();
285 vi->timer3 = g_timer_new();
286
287 g_timer_stop(vi->timer);
288 vi->active_frame = 0;
289 vi->total_frames = 1500;
290 /*add a call back to the main() */
291 g_timeout_add_full(G_PRIORITY_DEFAULT, 100u, gl_main_expose, NULL, NULL);
292 vi->cameras = NULL;
293 vi->camera_count = 0;
294 vi->active_camera = SIZE_MAX;
295 set_viewport_settings_from_template(vi, vi->systemGraphs.def_attrs);
296 vi->Topview->Graphdata.GraphFileName = NULL;
297 clear_color_theme(&vi->colschms);
298 vi->arcball = gv_alloc(sizeof(ArcBall_t));
299 load_mouse_actions(vi);
300 if(vi->guiMode!=GUI_FULLSCREEN)
301 vi->guiMode=GUI_WINDOWED;
302
303 /*create glcomp menu system */
304 vi->widgets = glcreate_gl_topview_menu();
305 }
306
307
308 /* update_graph_params:
309 * adds gledit params
310 * assumes custom_graph_data has been attached to the graph.
311 */
312 static void update_graph_params(Agraph_t * graph)
313 {
314 agattr_text(graph, AGRAPH, "GraphFileName",
315 view->Topview->Graphdata.GraphFileName);
316 }
317
318 static Agraph_t *loadGraph(char *filename)
319 {
320 Agraph_t *g;
321 FILE *input_file;
322 if (!(input_file = fopen(filename, "r"))) {
323 g_print("Cannot open %s\n", filename);
324 return 0;
325 }
326 g = agread(input_file, NULL);
327 fclose (input_file);
328 if (!g) {
329 g_print("Cannot read graph in %s\n", filename);
330 return 0;
331 }
332
333 /* If no position info, run layout with -Txdot
334 */
335 if (!agattr_text(g, AGNODE, "pos", NULL)) {
336 g_print("There is no position info in graph %s in %s\n", agnameof(g), filename);
337 agclose (g);
338 return 0;
339 }
340 view->Topview->Graphdata.GraphFileName = gv_strdup(filename);
341 return g;
342 }
343
344 /* add_graph_to_viewport_from_file:
345 * returns 1 if successful else 0
346 */
347 int add_graph_to_viewport_from_file(char *fileName)
348 {
349 Agraph_t *graph = loadGraph(fileName);
350
351 return add_graph_to_viewport(graph, fileName);
352 }
353
354 /* updateRecord:
355 * Update fields which may be added dynamically.
356 */
357 void updateRecord (Agraph_t* g)
358 {
359 GN_size(g) = agattr_text (g, AGNODE, "size", 0);
360 GN_visible(g) = agattr_text (g, AGNODE, "visible", 0);
361 GN_selected(g) = agattr_text (g, AGNODE, "selected", 0);
362 GN_labelattribute(g) = agattr_text (g, AGNODE, "nodelabelattribute", 0);
363
364 GE_pos(g)=agattr_text(g,AGEDGE,"pos",0);
365 GE_visible(g) = agattr_text (g, AGEDGE, "visible", 0);
366 GE_selected(g) = agattr_text (g, AGEDGE, "selected", 0);
367 GE_labelattribute(g) = agattr_text (g, AGEDGE, "edgelabelattribute", 0);
368 }
369
370 /* graphRecord:
371 * add graphRec to graph if necessary.
372 * update fields of graphRec.
373 * We assume the graph has attributes nodelabelattribute, edgelabelattribute,
374 * nodelabelcolor and edgelabelcolor from template.dot.
375 * We assume nodes have pos attributes.
376 * Only size, visible, selected and edge pos may or may not be defined.
377 */
378 static void
379 graphRecord (Agraph_t* g)
380 {
381 agbindrec(g, "graphRec", sizeof(graphRec), true);
382
383 GG_nodelabelcolor(g) = agattr_text (g, AGRAPH, "nodelabelcolor", 0);
384 GG_edgelabelcolor(g) = agattr_text (g, AGRAPH, "edgelabelcolor", 0);
385 GG_labelattribute(g) = agattr_text (g, AGRAPH, "nodelabelattribute", 0);
386 GG_elabelattribute(g) = agattr_text (g, AGRAPH, "edgelabelattribute", 0);
387
388 GN_pos(g) = agattr_text (g, AGNODE, "pos", 0);
389
390
391 updateRecord (g);
392 }
393
394 void refreshViewport(void)
395 {
396 Agraph_t *graph = view->g[view->activeGraph];
397 load_settings_from_graph();
398
399 if(view->guiMode!=GUI_FULLSCREEN)
400 update_graph_from_settings(graph);
401 set_viewport_settings_from_template(view, graph);
402 graphRecord(graph);
403 initSmGraph(graph,view->Topview);
404
405 expose_event(view->drawing_area, NULL, NULL);
406 }
407
408 static void activate(int id)
409 {
410 view->activeGraph = id;
411 refreshViewport();
412 }
413
414 int add_graph_to_viewport(Agraph_t * graph, char *id)
415 {
416 if (graph) {
417 view->g = gv_recalloc(view->g, view->graphCount, view->graphCount + 1,
418 sizeof(Agraph_t*));
419 view->graphCount = view->graphCount + 1;
420 view->g[view->graphCount - 1] = graph;
421
422 gtk_combo_box_append_text(view->graphComboBox, id);
423 assert(view->graphCount <= INT_MAX);
424 gtk_combo_box_set_active(view->graphComboBox, (int)view->graphCount - 1);
425 activate((int)view->graphCount - 1);
426 return 1;
427 } else {
428 return 0;
429 }
430
431 }
432 void switch_graph(int graphId)
433 {
434 if (graphId < 0 || (size_t)graphId >= view->graphCount)
435 return; /*wrong entry */
436 else
437 activate(graphId);
438 }
439
440 /* save_graph_with_file_name:
441 * saves graph with file name; if file name is NULL save as is
442 */
443 int save_graph_with_file_name(Agraph_t * graph, char *fileName)
444 {
445 int ret;
446 FILE *output_file;
447 update_graph_params(graph);
448 if (fileName)
449 output_file = fopen(fileName, "w");
450 else if (view->Topview->Graphdata.GraphFileName)
451 output_file = fopen(view->Topview->Graphdata.GraphFileName, "w");
452 else {
453 g_print("there is no file name to save! Programmer error\n");
454 return 0;
455 }
456 if (output_file == NULL) {
457 g_print("Cannot create file \n");
458 return 0;
459 }
460
461 ret = agwrite(graph, output_file);
462 fclose (output_file);
463 if (ret) {
464 g_print("%s successfully saved \n", fileName);
465 return 1;
466 }
467 return 0;
468 }
469
470 /* save_graph:
471 * save without prompt
472 */
473 int save_graph(void)
474 {
475 //check if there is an active graph
476 if (view->activeGraph > -1)
477 {
478 //check if active graph has a file name
479 if (view->Topview->Graphdata.GraphFileName) {
480 return save_graph_with_file_name(view->g[view->activeGraph],
481 view->Topview->Graphdata.
482 GraphFileName);
483 } else
484 return save_as_graph();
485 }
486 return 1;
487
488 }
489
490 /* save_as_graph:
491 * save with prompt
492 */
493 int save_as_graph(void)
494 {
495 //check if there is an active graph
496 if (view->activeGraph > -1) {
497 GtkWidget *dialog = gtk_file_chooser_dialog_new("Save File",
498 NULL,
499 GTK_FILE_CHOOSER_ACTION_SAVE,
500 GTK_STOCK_CANCEL,
501 GTK_RESPONSE_CANCEL,
502 GTK_STOCK_SAVE,
503 GTK_RESPONSE_ACCEPT, NULL);
504 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER
505 (dialog), TRUE);
506 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
507 char *filename =
508 gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
509 save_graph_with_file_name(view->g[view->activeGraph],
510 filename);
511 g_free(filename);
512 gtk_widget_destroy(dialog);
513
514 return 1;
515 } else {
516 gtk_widget_destroy(dialog);
517 return 0;
518 }
519 }
520 return 0;
521 }
522
523 void glexpose(void)
524 {
525 expose_event(view->drawing_area, NULL, NULL);
526 }
527
528 static double interpol(double minv, double maxv, double minc, double maxc,
529 double x) {
530 return (x - minv) * (maxc - minc) / (maxv - minv) + minc;
531 }
532
533 void getcolorfromschema(const colorschemaset sc, float l, float maxl,
534 glCompColor *c) {
535 size_t ind;
536 float percl = l / maxl;
537
538 // For smooth schemas, s[0].perc = 0, so we start with ind=1
539 for (ind = 1; ind + 1 < sizeof(sc.s) / sizeof(sc.s[0]); ind++) {
540 if (percl < sc.s[ind].perc)
541 break;
542 }
543 c->R = interpol(sc.s[ind - 1].perc, sc.s[ind].perc, sc.s[ind - 1].c.R,
544 sc.s[ind].c.R, percl);
545 c->G = interpol(sc.s[ind - 1].perc, sc.s[ind].perc, sc.s[ind - 1].c.G,
546 sc.s[ind].c.G, percl);
547 c->B = interpol(sc.s[ind - 1].perc, sc.s[ind].perc, sc.s[ind - 1].c.B,
548 sc.s[ind].c.B, percl);
549
550 c->A = 1;
551 }
552
553 /* set_color_theme_color:
554 * Convert colors as strings to RGB
555 */
556 static void set_color_theme_color(colorschemaset *sc, const char **colorstr) {
557 const size_t colorcnt = sizeof(sc->s) / sizeof(sc->s[0]);
558 gvcolor_t cl;
559 float av_perc;
560
561 av_perc = 1.0f / (float)(colorcnt - 1);
562 for (size_t ind = 0; ind < colorcnt; ind++) {
563 colorxlate(colorstr[ind], &cl, RGBA_DOUBLE);
564 sc->s[ind].c.R = cl.u.RGBA[0];
565 sc->s[ind].c.G = cl.u.RGBA[1];
566 sc->s[ind].c.B = cl.u.RGBA[2];
567 sc->s[ind].c.A = cl.u.RGBA[3];
568 sc->s[ind].perc = (float)ind * av_perc;
569 }
570 }
571
572 static const char *deep_blue[SCHEMACOUNT] = {
573 "#C8CBED", "#9297D3", "#0000FF", "#2C2E41"
574 };
575 static const char *pastel[SCHEMACOUNT] = {
576 "#EBBE29", "#D58C4A", "#74AE09", "#893C49"
577 };
578 static const char *magma[SCHEMACOUNT] = {
579 "#E0061E", "#F0F143", "#95192B", "#EB712F"
580 };
581 static const char *rain_forest[SCHEMACOUNT] = {
582 "#1E6A10", "#2ABE0E", "#AEDD39", "#5EE88B"
583 };
584 typedef struct {
585 const char **colors;
586 } colordata;
587 static const colordata palette[] = {
588 {deep_blue},
589 {pastel},
590 {magma},
591 {rain_forest},
592 };
593 #define NUM_SCHEMES (sizeof(palette)/sizeof(colordata))
594
595 static colorschemaset create_color_theme(int themeid) {
596 if (themeid < 0 || (int)NUM_SCHEMES <= themeid) {
597 fprintf (stderr, "colorschemaset: illegal themeid %d\n", themeid);
598 return view->colschms;
599 }
600
601 colorschemaset s = {0};
602 clear_color_theme(&view->colschms);
603
604 set_color_theme_color(&s, palette[themeid].colors);
605
606 return s;
607 }
608