1 | #include <graph_displayer_canvas.h> |
---|
2 | #include <broken_edge.h> |
---|
3 | #include <math.h> |
---|
4 | |
---|
5 | |
---|
6 | bool GraphDisplayerCanvas::on_expose_event(GdkEventExpose *event) |
---|
7 | { |
---|
8 | Gnome::Canvas::CanvasAA::on_expose_event(event); |
---|
9 | //usleep(10000); |
---|
10 | //rezoom(); |
---|
11 | return true; |
---|
12 | } |
---|
13 | |
---|
14 | void GraphDisplayerCanvas::changeEditorialTool(int newtool) |
---|
15 | { |
---|
16 | if(actual_tool!=newtool) |
---|
17 | { |
---|
18 | |
---|
19 | actual_handler.disconnect(); |
---|
20 | |
---|
21 | switch(actual_tool) |
---|
22 | { |
---|
23 | case CREATE_EDGE: |
---|
24 | { |
---|
25 | GdkEvent * generated=new GdkEvent(); |
---|
26 | generated->type=GDK_BUTTON_RELEASE; |
---|
27 | generated->button.button=3; |
---|
28 | createEdgeEventHandler(generated); |
---|
29 | break; |
---|
30 | } |
---|
31 | case EDGE_MAP_EDIT: |
---|
32 | //has to do the same thing as in the case of NODE_MAP_EDIT |
---|
33 | case NODE_MAP_EDIT: |
---|
34 | { |
---|
35 | GdkEvent * generated=new GdkEvent(); |
---|
36 | generated->type=GDK_KEY_PRESS; |
---|
37 | ((GdkEventKey*)generated)->keyval=GDK_KP_Enter; |
---|
38 | entryWidgetChangeHandler(generated); |
---|
39 | entrywidget.hide(); |
---|
40 | break; |
---|
41 | } |
---|
42 | default: |
---|
43 | break; |
---|
44 | } |
---|
45 | |
---|
46 | active_item=NULL; |
---|
47 | target_item=NULL; |
---|
48 | active_edge=INVALID; |
---|
49 | active_node=INVALID; |
---|
50 | |
---|
51 | |
---|
52 | actual_tool=newtool; |
---|
53 | |
---|
54 | switch(newtool) |
---|
55 | { |
---|
56 | case MOVE: |
---|
57 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false); |
---|
58 | break; |
---|
59 | |
---|
60 | case CREATE_NODE: |
---|
61 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createNodeEventHandler), false); |
---|
62 | break; |
---|
63 | |
---|
64 | case CREATE_EDGE: |
---|
65 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::createEdgeEventHandler), false); |
---|
66 | break; |
---|
67 | |
---|
68 | case ERASER: |
---|
69 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::eraserEventHandler), false); |
---|
70 | break; |
---|
71 | |
---|
72 | case EDGE_MAP_EDIT: |
---|
73 | grab_focus(); |
---|
74 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::edgeMapEditEventHandler), false); |
---|
75 | break; |
---|
76 | |
---|
77 | case NODE_MAP_EDIT: |
---|
78 | actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::nodeMapEditEventHandler), false); |
---|
79 | break; |
---|
80 | |
---|
81 | default: |
---|
82 | break; |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | int GraphDisplayerCanvas::getActualTool() |
---|
88 | { |
---|
89 | return actual_tool; |
---|
90 | } |
---|
91 | |
---|
92 | bool GraphDisplayerCanvas::moveEventHandler(GdkEvent* e) |
---|
93 | { |
---|
94 | switch(e->type) |
---|
95 | { |
---|
96 | case GDK_BUTTON_PRESS: |
---|
97 | //we mark the location of the event to be able to calculate parameters of dragging |
---|
98 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
99 | |
---|
100 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
101 | active_node=INVALID; |
---|
102 | for (NodeIt i(g); i!=INVALID; ++i) |
---|
103 | { |
---|
104 | if(nodesmap[i]==active_item) |
---|
105 | { |
---|
106 | active_node=i; |
---|
107 | } |
---|
108 | } |
---|
109 | switch(e->button.button) |
---|
110 | { |
---|
111 | case 3: |
---|
112 | isbutton=3; |
---|
113 | break; |
---|
114 | default: |
---|
115 | isbutton=1; |
---|
116 | break; |
---|
117 | } |
---|
118 | break; |
---|
119 | case GDK_BUTTON_RELEASE: |
---|
120 | isbutton=0; |
---|
121 | active_item=NULL; |
---|
122 | active_node=INVALID; |
---|
123 | break; |
---|
124 | case GDK_MOTION_NOTIFY: |
---|
125 | //we only have to do sg. if the mouse button is pressed AND the click was on a node that was found in the set of nodes |
---|
126 | if(active_node!=INVALID) |
---|
127 | { |
---|
128 | //new coordinates will be the old values, |
---|
129 | //because the item will be moved to the |
---|
130 | //new coordinate therefore the new movement |
---|
131 | //has to be calculated from here |
---|
132 | |
---|
133 | double new_x, new_y; |
---|
134 | |
---|
135 | window_to_world (e->motion.x, e->motion.y, new_x, new_y); |
---|
136 | |
---|
137 | double dx=new_x-clicked_x; |
---|
138 | double dy=new_y-clicked_y; |
---|
139 | |
---|
140 | //repositioning node and its text |
---|
141 | active_item->move(dx, dy); |
---|
142 | nodetextmap[active_node]->move(dx, dy); |
---|
143 | |
---|
144 | clicked_x=new_x; |
---|
145 | clicked_y=new_y; |
---|
146 | |
---|
147 | //all the edges connected to the moved point has to be redrawn |
---|
148 | EdgeIt ei; |
---|
149 | |
---|
150 | g.firstOut(ei,active_node); |
---|
151 | |
---|
152 | for(;ei!=INVALID;g.nextOut(ei)) |
---|
153 | { |
---|
154 | Gnome::Canvas::Points coos; |
---|
155 | double x1, x2, y1, y2; |
---|
156 | |
---|
157 | nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2); |
---|
158 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
159 | |
---|
160 | nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2); |
---|
161 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
162 | |
---|
163 | if(isbutton==3) |
---|
164 | { |
---|
165 | edgesmap[ei]->setPoints(coos); |
---|
166 | } |
---|
167 | else |
---|
168 | { |
---|
169 | edgesmap[ei]->setPoints(coos,true); |
---|
170 | } |
---|
171 | |
---|
172 | //reposition of edgetext |
---|
173 | xy<double> text_pos=edgesmap[ei]->getArrowPos(); |
---|
174 | text_pos+=(xy<double>(10,10)); |
---|
175 | edgetextmap[ei]->property_x().set_value(text_pos.x); |
---|
176 | edgetextmap[ei]->property_y().set_value(text_pos.y); |
---|
177 | } |
---|
178 | |
---|
179 | g.firstIn(ei,active_node); |
---|
180 | for(;ei!=INVALID;g.nextIn(ei)) |
---|
181 | { |
---|
182 | Gnome::Canvas::Points coos; |
---|
183 | double x1, x2, y1, y2; |
---|
184 | |
---|
185 | nodesmap[g.source(ei)]->get_bounds(x1, y1, x2, y2); |
---|
186 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
187 | |
---|
188 | nodesmap[g.target(ei)]->get_bounds(x1, y1, x2, y2); |
---|
189 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
190 | |
---|
191 | if(isbutton==3) |
---|
192 | { |
---|
193 | edgesmap[ei]->setPoints(coos); |
---|
194 | } |
---|
195 | else |
---|
196 | { |
---|
197 | edgesmap[ei]->setPoints(coos,true); |
---|
198 | } |
---|
199 | |
---|
200 | xy<double> text_pos=edgesmap[ei]->getArrowPos(); |
---|
201 | text_pos+=(xy<double>(10,10)); |
---|
202 | edgetextmap[ei]->property_x().set_value(text_pos.x); |
---|
203 | edgetextmap[ei]->property_y().set_value(text_pos.y); |
---|
204 | } |
---|
205 | } |
---|
206 | default: break; |
---|
207 | } |
---|
208 | |
---|
209 | return false; |
---|
210 | } |
---|
211 | |
---|
212 | bool GraphDisplayerCanvas::createNodeEventHandler(GdkEvent* e) |
---|
213 | { |
---|
214 | switch(e->type) |
---|
215 | { |
---|
216 | |
---|
217 | //draw the new node in red at the clicked place |
---|
218 | case GDK_2BUTTON_PRESS: |
---|
219 | std::cout << "double click" << std::endl; |
---|
220 | break; |
---|
221 | case GDK_BUTTON_PRESS: |
---|
222 | isbutton=1; |
---|
223 | |
---|
224 | active_node=NodeIt(g,g.addNode()); |
---|
225 | |
---|
226 | //initiating values corresponding to new node in maps |
---|
227 | |
---|
228 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
229 | |
---|
230 | nodesmap[active_node]=new Gnome::Canvas::Ellipse(displayed_graph, clicked_x-20, clicked_y-20, clicked_x+20, clicked_y+20); |
---|
231 | active_item=(Gnome::Canvas::Item *)(nodesmap[active_node]); |
---|
232 | *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); |
---|
233 | *(nodesmap[active_node]) << Gnome::Canvas::Properties::outline_color("black"); |
---|
234 | (nodesmap[active_node])->show(); |
---|
235 | |
---|
236 | nodetextmap[active_node]=new Gnome::Canvas::Text(displayed_graph, clicked_x+node_property_defaults[N_RADIUS]+5, clicked_y+node_property_defaults[N_RADIUS]+5, ""); |
---|
237 | nodetextmap[active_node]->property_fill_color().set_value("darkblue"); |
---|
238 | |
---|
239 | mapwin->updateNode(active_node); |
---|
240 | |
---|
241 | break; |
---|
242 | |
---|
243 | //move the new node |
---|
244 | case GDK_MOTION_NOTIFY: |
---|
245 | { |
---|
246 | GdkEvent * generated=new GdkEvent(); |
---|
247 | generated->motion.x=e->motion.x; |
---|
248 | generated->motion.y=e->motion.y; |
---|
249 | generated->type=GDK_MOTION_NOTIFY; |
---|
250 | moveEventHandler(generated); |
---|
251 | break; |
---|
252 | } |
---|
253 | |
---|
254 | //finalize the new node |
---|
255 | case GDK_BUTTON_RELEASE: |
---|
256 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
257 | |
---|
258 | active_item->lower_to_bottom(); |
---|
259 | |
---|
260 | target_item=NULL; |
---|
261 | target_item=get_item_at(clicked_x, clicked_y); |
---|
262 | |
---|
263 | active_item->raise_to_top(); |
---|
264 | |
---|
265 | isbutton=0; |
---|
266 | if(target_item==active_item) |
---|
267 | { |
---|
268 | //Its appropriate color is given by update. |
---|
269 | *active_item << Gnome::Canvas::Properties::fill_color("blue"); |
---|
270 | } |
---|
271 | else |
---|
272 | { |
---|
273 | //In this case the given color has to be overwritten, because the noe covers an other item. |
---|
274 | *active_item << Gnome::Canvas::Properties::fill_color("lightblue"); |
---|
275 | } |
---|
276 | target_item=NULL; |
---|
277 | active_item=NULL; |
---|
278 | active_node=INVALID; |
---|
279 | break; |
---|
280 | default: |
---|
281 | break; |
---|
282 | } |
---|
283 | return false; |
---|
284 | } |
---|
285 | |
---|
286 | bool GraphDisplayerCanvas::createEdgeEventHandler(GdkEvent* e) |
---|
287 | { |
---|
288 | switch(e->type) |
---|
289 | { |
---|
290 | case GDK_BUTTON_PRESS: |
---|
291 | //in edge creation right button has special meaning |
---|
292 | if(e->button.button!=3) |
---|
293 | { |
---|
294 | //there is not yet selected node |
---|
295 | if(active_node==INVALID) |
---|
296 | { |
---|
297 | //we mark the location of the event to be able to calculate parameters of dragging |
---|
298 | |
---|
299 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
300 | |
---|
301 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
302 | active_node=INVALID; |
---|
303 | for (NodeIt i(g); i!=INVALID; ++i) |
---|
304 | { |
---|
305 | if(nodesmap[i]==active_item) |
---|
306 | { |
---|
307 | active_node=i; |
---|
308 | } |
---|
309 | } |
---|
310 | //the clicked item is really a node |
---|
311 | if(active_node!=INVALID) |
---|
312 | { |
---|
313 | *(nodesmap[active_node]) << Gnome::Canvas::Properties::fill_color("red"); |
---|
314 | isbutton=1; |
---|
315 | } |
---|
316 | //clicked item was not a node. It could be e.g. edge. |
---|
317 | else |
---|
318 | { |
---|
319 | active_item=NULL; |
---|
320 | } |
---|
321 | } |
---|
322 | //we only have to do sg. if the mouse button |
---|
323 | // is pressed already once AND the click was |
---|
324 | // on a node that was found in the set of |
---|
325 | //nodes, and now we only search for the second |
---|
326 | //node |
---|
327 | else |
---|
328 | { |
---|
329 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
330 | target_item=(get_item_at(clicked_x, clicked_y)); |
---|
331 | Graph::NodeIt target_node=INVALID; |
---|
332 | for (NodeIt i(g); i!=INVALID; ++i) |
---|
333 | { |
---|
334 | if(nodesmap[i]==target_item) |
---|
335 | { |
---|
336 | target_node=i; |
---|
337 | } |
---|
338 | } |
---|
339 | //the clicked item is a node, the edge can be drawn |
---|
340 | if(target_node!=INVALID) |
---|
341 | { |
---|
342 | if(target_node!=active_node) |
---|
343 | { |
---|
344 | *(nodesmap[target_node]) << Gnome::Canvas::Properties::fill_color("red"); |
---|
345 | |
---|
346 | //creating new edge |
---|
347 | active_edge=EdgeIt(g,g.addEdge(active_node, target_node)); |
---|
348 | |
---|
349 | //initiating values corresponding to new edge in maps |
---|
350 | mapstorage.initMapsForEdge(active_edge); |
---|
351 | |
---|
352 | //calculating coordinates of new edge |
---|
353 | Gnome::Canvas::Points coos; |
---|
354 | double x1, x2, y1, y2; |
---|
355 | |
---|
356 | active_item->get_bounds(x1, y1, x2, y2); |
---|
357 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
358 | |
---|
359 | target_item->get_bounds(x1, y1, x2, y2); |
---|
360 | coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2)); |
---|
361 | |
---|
362 | //drawing new edge |
---|
363 | edgesmap[active_edge]=new BrokenEdge(displayed_graph, coos, *this); |
---|
364 | *(edgesmap[active_edge]) << Gnome::Canvas::Properties::fill_color("green"); |
---|
365 | edgesmap[active_edge]->property_width_pixels().set_value(10); |
---|
366 | |
---|
367 | //redraw nodes to blank terminations of the new edge |
---|
368 | target_item->raise_to_top(); |
---|
369 | active_item->raise_to_top(); |
---|
370 | |
---|
371 | //initializing edge-text as well, to empty string |
---|
372 | xy<double> text_pos=edgesmap[active_edge]->getArrowPos(); |
---|
373 | text_pos+=(xy<double>(10,10)); |
---|
374 | |
---|
375 | edgetextmap[active_edge]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, ""); |
---|
376 | edgetextmap[active_edge]->property_fill_color().set_value("darkgreen"); |
---|
377 | |
---|
378 | //updating its properties |
---|
379 | mapwin->updateEdge(active_edge); |
---|
380 | } |
---|
381 | else |
---|
382 | { |
---|
383 | target_node=INVALID; |
---|
384 | std::cout << "Loop edge is not yet implemented!" << std::endl; |
---|
385 | } |
---|
386 | } |
---|
387 | //clicked item was not a node. it could be an e.g. edge. we do not deal with it furthermore. |
---|
388 | else |
---|
389 | { |
---|
390 | target_item=NULL; |
---|
391 | } |
---|
392 | } |
---|
393 | } |
---|
394 | break; |
---|
395 | case GDK_BUTTON_RELEASE: |
---|
396 | isbutton=0; |
---|
397 | //we clear settings in two cases |
---|
398 | //1: the edge is ready (target_item has valid value) |
---|
399 | //2: the edge creation is cancelled with right button |
---|
400 | if((target_item)||(e->button.button==3)) |
---|
401 | { |
---|
402 | if(active_item) |
---|
403 | { |
---|
404 | *active_item << Gnome::Canvas::Properties::fill_color("blue"); |
---|
405 | active_item=NULL; |
---|
406 | } |
---|
407 | if(target_item) |
---|
408 | { |
---|
409 | *target_item << Gnome::Canvas::Properties::fill_color("blue"); |
---|
410 | target_item=NULL; |
---|
411 | } |
---|
412 | active_node=INVALID; |
---|
413 | active_edge=INVALID; |
---|
414 | } |
---|
415 | break; |
---|
416 | default: |
---|
417 | break; |
---|
418 | } |
---|
419 | return false; |
---|
420 | } |
---|
421 | |
---|
422 | bool GraphDisplayerCanvas::eraserEventHandler(GdkEvent* e) |
---|
423 | { |
---|
424 | switch(e->type) |
---|
425 | { |
---|
426 | case GDK_BUTTON_PRESS: |
---|
427 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
428 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
429 | active_node=INVALID; |
---|
430 | active_edge=INVALID; |
---|
431 | for (NodeIt i(g); i!=INVALID; ++i) |
---|
432 | { |
---|
433 | if(nodesmap[i]==active_item) |
---|
434 | { |
---|
435 | active_node=i; |
---|
436 | } |
---|
437 | } |
---|
438 | if(active_node==INVALID) |
---|
439 | { |
---|
440 | for (EdgeIt i(g); i!=INVALID; ++i) |
---|
441 | { |
---|
442 | if(edgesmap[i]==active_item) |
---|
443 | { |
---|
444 | active_edge=i; |
---|
445 | } |
---|
446 | } |
---|
447 | } |
---|
448 | if(active_item) |
---|
449 | { |
---|
450 | *active_item << Gnome::Canvas::Properties::fill_color("red"); |
---|
451 | } |
---|
452 | break; |
---|
453 | |
---|
454 | case GDK_BUTTON_RELEASE: |
---|
455 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
456 | if(active_item) |
---|
457 | { |
---|
458 | if( active_item == ( get_item_at (clicked_x, clicked_y) ) ) |
---|
459 | { |
---|
460 | if(active_node!=INVALID) |
---|
461 | { |
---|
462 | |
---|
463 | //collecting edges to delete |
---|
464 | EdgeIt e; |
---|
465 | std::set<Graph::Edge> edges_to_delete; |
---|
466 | |
---|
467 | g.firstOut(e,active_node); |
---|
468 | for(;e!=INVALID;g.nextOut(e)) |
---|
469 | { |
---|
470 | edges_to_delete.insert(e); |
---|
471 | } |
---|
472 | |
---|
473 | g.firstIn(e,active_node); |
---|
474 | for(;e!=INVALID;g.nextIn(e)) |
---|
475 | { |
---|
476 | edges_to_delete.insert(e); |
---|
477 | } |
---|
478 | |
---|
479 | //deleting collected edges |
---|
480 | for(std::set<Graph::Edge>::iterator edge_set_it=edges_to_delete.begin();edge_set_it!=edges_to_delete.end();edge_set_it++) |
---|
481 | { |
---|
482 | deleteItem(*edge_set_it); |
---|
483 | } |
---|
484 | deleteItem(active_node); |
---|
485 | } |
---|
486 | //a simple edge was chosen |
---|
487 | else |
---|
488 | { |
---|
489 | deleteItem(active_edge); |
---|
490 | } |
---|
491 | } |
---|
492 | //pointer was moved, deletion is cancelled |
---|
493 | else |
---|
494 | { |
---|
495 | if(active_node!=INVALID) |
---|
496 | { |
---|
497 | *active_item << Gnome::Canvas::Properties::fill_color("blue"); |
---|
498 | } |
---|
499 | else |
---|
500 | { |
---|
501 | *active_item << Gnome::Canvas::Properties::fill_color("green"); |
---|
502 | } |
---|
503 | } |
---|
504 | } |
---|
505 | //reseting datas |
---|
506 | active_item=NULL; |
---|
507 | active_edge=INVALID; |
---|
508 | active_node=INVALID; |
---|
509 | break; |
---|
510 | |
---|
511 | case GDK_MOTION_NOTIFY: |
---|
512 | break; |
---|
513 | |
---|
514 | default: |
---|
515 | break; |
---|
516 | } |
---|
517 | return false; |
---|
518 | } |
---|
519 | |
---|
520 | bool GraphDisplayerCanvas::edgeMapEditEventHandler(GdkEvent* e) |
---|
521 | { |
---|
522 | switch(e->type) |
---|
523 | { |
---|
524 | case GDK_KEY_PRESS: |
---|
525 | { |
---|
526 | nodeMapEditEventHandler(e); |
---|
527 | break; |
---|
528 | } |
---|
529 | case GDK_BUTTON_PRESS: |
---|
530 | { |
---|
531 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
532 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
533 | Graph::EdgeIt clicked_edge=INVALID; |
---|
534 | for (EdgeIt i(g); i!=INVALID; ++i) |
---|
535 | { |
---|
536 | if(edgesmap[i]==active_item) |
---|
537 | { |
---|
538 | clicked_edge=i; |
---|
539 | } |
---|
540 | } |
---|
541 | if(clicked_edge!=INVALID) |
---|
542 | { |
---|
543 | if(edgetextmap[clicked_edge]->property_text().get_value()!="") |
---|
544 | { |
---|
545 | active_edge=clicked_edge; |
---|
546 | if(canvasentrywidget) |
---|
547 | { |
---|
548 | delete(canvasentrywidget); |
---|
549 | } |
---|
550 | entrywidget.show(); |
---|
551 | entrywidget.set_text(edgetextmap[active_edge]->property_text().get_value()); |
---|
552 | xy<double> entry_coos; |
---|
553 | entry_coos.x=(edgetextmap[active_edge])->property_x().get_value(); |
---|
554 | entry_coos.x-=edgetextmap[active_edge]->property_text_width().get_value()/2; |
---|
555 | entry_coos.y=(edgetextmap[active_edge])->property_y().get_value(); |
---|
556 | entry_coos.y-=edgetextmap[active_edge]->property_text_height().get_value()*1.5/2; |
---|
557 | canvasentrywidget=new Gnome::Canvas::Widget(displayed_graph, entry_coos.x, entry_coos.y, entrywidget); |
---|
558 | canvasentrywidget->property_width().set_value(edgetextmap[active_edge]->property_text_width().get_value()*1.5); |
---|
559 | canvasentrywidget->property_height().set_value(edgetextmap[active_edge]->property_text_height().get_value()*1.5); |
---|
560 | } |
---|
561 | } |
---|
562 | else |
---|
563 | { |
---|
564 | GdkEvent * generated=new GdkEvent(); |
---|
565 | generated->type=GDK_KEY_PRESS; |
---|
566 | ((GdkEventKey*)generated)->keyval=GDK_KP_Enter; |
---|
567 | entryWidgetChangeHandler(generated); |
---|
568 | } |
---|
569 | break; |
---|
570 | } |
---|
571 | default: |
---|
572 | break; |
---|
573 | } |
---|
574 | return false; |
---|
575 | } |
---|
576 | |
---|
577 | bool GraphDisplayerCanvas::nodeMapEditEventHandler(GdkEvent* e) |
---|
578 | { |
---|
579 | switch(e->type) |
---|
580 | { |
---|
581 | case GDK_KEY_PRESS: |
---|
582 | { |
---|
583 | switch(((GdkEventKey*)e)->keyval) |
---|
584 | { |
---|
585 | case GDK_Escape: |
---|
586 | entrywidget.hide(); |
---|
587 | break; |
---|
588 | case GDK_Return: |
---|
589 | case GDK_KP_Enter: |
---|
590 | entrywidget.hide(); |
---|
591 | break; |
---|
592 | default: |
---|
593 | break; |
---|
594 | } |
---|
595 | |
---|
596 | break; |
---|
597 | } |
---|
598 | case GDK_BUTTON_PRESS: |
---|
599 | { |
---|
600 | window_to_world (e->button.x, e->button.y, clicked_x, clicked_y); |
---|
601 | active_item=(get_item_at(clicked_x, clicked_y)); |
---|
602 | Graph::NodeIt clicked_node=INVALID; |
---|
603 | for (NodeIt i(g); i!=INVALID; ++i) |
---|
604 | { |
---|
605 | if(nodesmap[i]==active_item) |
---|
606 | { |
---|
607 | clicked_node=i; |
---|
608 | } |
---|
609 | } |
---|
610 | if(clicked_node!=INVALID) |
---|
611 | { |
---|
612 | //If there is already edited edge, it has to be saved first |
---|
613 | if(entrywidget.is_visible()) |
---|
614 | { |
---|
615 | GdkEvent * generated=new GdkEvent(); |
---|
616 | generated->type=GDK_KEY_PRESS; |
---|
617 | ((GdkEventKey*)generated)->keyval=GDK_KP_Enter; |
---|
618 | entryWidgetChangeHandler(generated); |
---|
619 | } |
---|
620 | //If the previous value could be saved, we can go further, otherwise not |
---|
621 | if(!entrywidget.is_visible()) |
---|
622 | { |
---|
623 | |
---|
624 | if(nodetextmap[clicked_node]->property_text().get_value()!="") |
---|
625 | { |
---|
626 | active_node=clicked_node; |
---|
627 | if(canvasentrywidget) |
---|
628 | { |
---|
629 | delete(canvasentrywidget); |
---|
630 | } |
---|
631 | entrywidget.show(); |
---|
632 | entrywidget.set_text(nodetextmap[active_node]->property_text().get_value()); |
---|
633 | xy<double> entry_coos; |
---|
634 | entry_coos.x=(nodetextmap[active_node])->property_x().get_value(); |
---|
635 | entry_coos.x-=nodetextmap[active_node]->property_text_width().get_value()/2; |
---|
636 | entry_coos.y=(nodetextmap[active_node])->property_y().get_value(); |
---|
637 | entry_coos.y-=nodetextmap[active_node]->property_text_height().get_value()*1.5/2; |
---|
638 | canvasentrywidget=new Gnome::Canvas::Widget(displayed_graph, entry_coos.x, entry_coos.y, entrywidget); |
---|
639 | canvasentrywidget->property_width().set_value(nodetextmap[active_node]->property_text_width().get_value()*1.5); |
---|
640 | canvasentrywidget->property_height().set_value(nodetextmap[active_node]->property_text_height().get_value()*1.5); |
---|
641 | } |
---|
642 | } |
---|
643 | } |
---|
644 | else |
---|
645 | { |
---|
646 | GdkEvent * generated=new GdkEvent(); |
---|
647 | generated->type=GDK_KEY_PRESS; |
---|
648 | ((GdkEventKey*)generated)->keyval=GDK_KP_Enter; |
---|
649 | entryWidgetChangeHandler(generated); |
---|
650 | } |
---|
651 | break; |
---|
652 | } |
---|
653 | default: |
---|
654 | break; |
---|
655 | } |
---|
656 | return false; |
---|
657 | } |
---|
658 | |
---|
659 | bool GraphDisplayerCanvas::entryWidgetChangeHandler(GdkEvent* e) |
---|
660 | { |
---|
661 | if(entrywidget.is_visible()) |
---|
662 | { |
---|
663 | if(e->type==GDK_KEY_PRESS) |
---|
664 | { |
---|
665 | switch(((GdkEventKey*)e)->keyval) |
---|
666 | { |
---|
667 | case GDK_Escape: |
---|
668 | entrywidget.hide(); |
---|
669 | break; |
---|
670 | case GDK_KP_Enter: |
---|
671 | case GDK_Return: |
---|
672 | { |
---|
673 | Glib::ustring mapvalue = entrywidget.get_text(); |
---|
674 | |
---|
675 | double double_map_fract_value=0; |
---|
676 | double double_map_value=0; |
---|
677 | int offset=0; |
---|
678 | int found_letter=0; |
---|
679 | //converting text to double |
---|
680 | for(int i=0;i<(int)(mapvalue.length());i++) |
---|
681 | { |
---|
682 | if(((mapvalue[i]<='9')&&(mapvalue[i]>='0'))||(mapvalue[i]=='.')) |
---|
683 | { |
---|
684 | if(mapvalue[i]=='.') |
---|
685 | { |
---|
686 | //for calculating non-integer part of double we step backward from the end |
---|
687 | //after each step the number will be divided by ten, and the new value will be added |
---|
688 | //to step backward from the end until the point the actual character of the string is the following: |
---|
689 | // mapvalue.length()-(i-position_of_point) |
---|
690 | //if i was the number of the first character after the decimal point the selected character will be the last |
---|
691 | //if i was the number of the last character, the selected character will be the first after the decimal point |
---|
692 | offset=mapvalue.length()+i; |
---|
693 | } |
---|
694 | else |
---|
695 | { |
---|
696 | if(!offset) |
---|
697 | { |
---|
698 | double_map_value=10*double_map_value+mapvalue[i]-'0'; |
---|
699 | } |
---|
700 | else |
---|
701 | { |
---|
702 | double_map_fract_value=double_map_fract_value/10+(double)(mapvalue[offset-i]-'0')/10; |
---|
703 | } |
---|
704 | } |
---|
705 | } |
---|
706 | else |
---|
707 | { |
---|
708 | found_letter++; |
---|
709 | continue; |
---|
710 | } |
---|
711 | } |
---|
712 | |
---|
713 | if(!found_letter) |
---|
714 | { |
---|
715 | switch(actual_tool) |
---|
716 | { |
---|
717 | case EDGE_MAP_EDIT: |
---|
718 | edgetextmap[active_edge]->property_text().set_value(mapvalue); |
---|
719 | (*(mapstorage.edgemap_storage)[edgemap_to_edit])[active_edge]=double_map_value+double_map_fract_value; |
---|
720 | break; |
---|
721 | case NODE_MAP_EDIT: |
---|
722 | nodetextmap[active_node]->property_text().set_value(mapvalue); |
---|
723 | (*(mapstorage.nodemap_storage)[nodemap_to_edit])[active_node]=double_map_value+double_map_fract_value; |
---|
724 | break; |
---|
725 | default: |
---|
726 | break; |
---|
727 | } |
---|
728 | entrywidget.hide(); |
---|
729 | } |
---|
730 | else |
---|
731 | { |
---|
732 | std::cout << "ERROR: only handling of double values is implemented yet!" << std::endl; |
---|
733 | } |
---|
734 | |
---|
735 | break; |
---|
736 | } |
---|
737 | default: |
---|
738 | break; |
---|
739 | } |
---|
740 | } |
---|
741 | } |
---|
742 | return false; |
---|
743 | } |
---|
744 | |
---|
745 | void GraphDisplayerCanvas::deleteItem(NodeIt node_to_delete) |
---|
746 | { |
---|
747 | delete(nodetextmap[node_to_delete]); |
---|
748 | delete(nodesmap[node_to_delete]); |
---|
749 | g.erase(node_to_delete); |
---|
750 | } |
---|
751 | |
---|
752 | void GraphDisplayerCanvas::deleteItem(EdgeIt edge_to_delete) |
---|
753 | { |
---|
754 | delete(edgetextmap[edge_to_delete]); |
---|
755 | delete(edgesmap[edge_to_delete]); |
---|
756 | g.erase(edge_to_delete); |
---|
757 | } |
---|
758 | |
---|
759 | void GraphDisplayerCanvas::deleteItem(Graph::Edge edge_to_delete) |
---|
760 | { |
---|
761 | delete(edgetextmap[edge_to_delete]); |
---|
762 | delete(edgesmap[edge_to_delete]); |
---|
763 | g.erase(edge_to_delete); |
---|
764 | } |
---|
765 | |
---|
766 | void GraphDisplayerCanvas::textReposition(xy<double> new_place) |
---|
767 | { |
---|
768 | new_place+=(xy<double>(10,10)); |
---|
769 | edgetextmap[forming_edge]->property_x().set_value(new_place.x); |
---|
770 | edgetextmap[forming_edge]->property_y().set_value(new_place.y); |
---|
771 | } |
---|
772 | |
---|
773 | void GraphDisplayerCanvas::toggleEdgeActivity(BrokenEdge* active_bre, bool on) |
---|
774 | { |
---|
775 | if(on) |
---|
776 | { |
---|
777 | if(forming_edge!=INVALID) |
---|
778 | { |
---|
779 | std::cout << "ERROR!!!! Valid edge found!" << std::endl; |
---|
780 | } |
---|
781 | else |
---|
782 | { |
---|
783 | for (EdgeIt i(g); i!=INVALID; ++i) |
---|
784 | { |
---|
785 | if(edgesmap[i]==active_bre) |
---|
786 | { |
---|
787 | forming_edge=i; |
---|
788 | } |
---|
789 | } |
---|
790 | } |
---|
791 | } |
---|
792 | else |
---|
793 | { |
---|
794 | if(forming_edge!=INVALID) |
---|
795 | { |
---|
796 | forming_edge=INVALID; |
---|
797 | } |
---|
798 | else |
---|
799 | { |
---|
800 | std::cout << "ERROR!!!! Invalid edge found!" << std::endl; |
---|
801 | } |
---|
802 | } |
---|
803 | |
---|
804 | } |
---|
805 | |
---|
806 | void GraphDisplayerCanvas::addNewEdgeMap() |
---|
807 | { |
---|
808 | std::cout << "Add New EdgeMap is not yet implemented." << std::endl; |
---|
809 | } |
---|
810 | |
---|
811 | void GraphDisplayerCanvas::addNewNodeMap() |
---|
812 | { |
---|
813 | std::cout << "Add New NodeMap is not yet implemented." << std::endl; |
---|
814 | } |
---|
815 | |
---|