1 | /* -*- C++ -*- |
---|
2 | * |
---|
3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
4 | * |
---|
5 | * Copyright (C) 2003-2006 |
---|
6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
8 | * |
---|
9 | * Permission to use, modify and distribute this software is granted |
---|
10 | * provided that this copyright notice appears in all copies. For |
---|
11 | * precise terms see the accompanying LICENSE file. |
---|
12 | * |
---|
13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
14 | * express or implied, and with no claim as to its suitability for any |
---|
15 | * purpose. |
---|
16 | * |
---|
17 | */ |
---|
18 | |
---|
19 | #include <graph_displayer_canvas.h> |
---|
20 | #include <mapstorage.h> |
---|
21 | #include <nbtab.h> |
---|
22 | #include <cmath> |
---|
23 | |
---|
24 | GraphDisplayerCanvas::EdgeBase::EdgeBase(Gnome::Canvas::Group& _group, Edge _edge, GraphDisplayerCanvas& _canvas) : |
---|
25 | Gnome::Canvas::Group(_group), edge(_edge), canvas(_canvas), arrow(*this) |
---|
26 | { |
---|
27 | arrow.property_fill_color().set_value("red"); |
---|
28 | arrow.lower_to_bottom(); |
---|
29 | lower_to_bottom(); |
---|
30 | } |
---|
31 | |
---|
32 | GraphDisplayerCanvas::EdgeBase::~EdgeBase() |
---|
33 | { |
---|
34 | } |
---|
35 | |
---|
36 | void GraphDisplayerCanvas::EdgeBase::drawArrow(XY unit_vector_in_dir) |
---|
37 | { |
---|
38 | MapStorage& ms = *canvas.mytab.mapstorage; |
---|
39 | XY center(ms.arrow_pos[edge]); |
---|
40 | XY unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x); |
---|
41 | |
---|
42 | // /\ // top |
---|
43 | // / \ // |
---|
44 | // - - // c(enter)l(eft), ccl, ccr, cr |
---|
45 | // || // |
---|
46 | // || // b(ottom)l, br |
---|
47 | |
---|
48 | double size=3; |
---|
49 | |
---|
50 | XY bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size ); |
---|
51 | XY br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size ); |
---|
52 | XY ccl(center + unit_vector_in_dir * size + unit_norm_vector * size ); |
---|
53 | XY ccr(center + unit_vector_in_dir * size - unit_norm_vector * size ); |
---|
54 | XY cl (center + unit_vector_in_dir * size + unit_norm_vector * 2 * size ); |
---|
55 | XY cr (center + unit_vector_in_dir * size - unit_norm_vector * 2 * size ); |
---|
56 | XY top(center + unit_vector_in_dir * 3 * size); |
---|
57 | |
---|
58 | Gnome::Canvas::Points arrow_points; |
---|
59 | arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y ) ); |
---|
60 | arrow_points.push_back(Gnome::Art::Point( br.x , br.y ) ); |
---|
61 | arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) ); |
---|
62 | arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y ) ); |
---|
63 | arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) ); |
---|
64 | arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y ) ); |
---|
65 | arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) ); |
---|
66 | |
---|
67 | arrow.property_points().set_value(arrow_points); |
---|
68 | } |
---|
69 | |
---|
70 | GraphDisplayerCanvas::BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, |
---|
71 | Edge _edge, GraphDisplayerCanvas & gc) : EdgeBase(g, _edge, gc), |
---|
72 | isbutton(false), line(*this) |
---|
73 | { |
---|
74 | arrow.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler)); |
---|
75 | |
---|
76 | line.property_fill_color().set_value("green"); |
---|
77 | line.property_width_units().set_value(10); |
---|
78 | line.lower_to_bottom(); |
---|
79 | |
---|
80 | draw(); |
---|
81 | } |
---|
82 | |
---|
83 | GraphDisplayerCanvas::BrokenEdge::~BrokenEdge() |
---|
84 | { |
---|
85 | } |
---|
86 | |
---|
87 | void GraphDisplayerCanvas::BrokenEdge::draw() |
---|
88 | { |
---|
89 | MapStorage& ms = *canvas.mytab.mapstorage; |
---|
90 | |
---|
91 | //calculating coordinates of the direction indicator arrow |
---|
92 | XY head(ms.coords[ms.graph.target(edge)]); |
---|
93 | XY center(ms.arrow_pos[edge]); |
---|
94 | |
---|
95 | XY unit_vector_in_dir(head-center); |
---|
96 | double length=sqrt( unit_vector_in_dir.normSquare() ); |
---|
97 | |
---|
98 | if(length!=0) |
---|
99 | { |
---|
100 | unit_vector_in_dir/=length; |
---|
101 | } |
---|
102 | |
---|
103 | // update the arrow |
---|
104 | drawArrow(unit_vector_in_dir); |
---|
105 | |
---|
106 | // update the edge |
---|
107 | Gnome::Canvas::Points points; |
---|
108 | Node source = ms.graph.source(edge); |
---|
109 | Node target = ms.graph.target(edge); |
---|
110 | points.push_back(Gnome::Art::Point(ms.coords[source].x, |
---|
111 | ms.coords[source].y)); |
---|
112 | points.push_back(Gnome::Art::Point(ms.arrow_pos[edge].x, |
---|
113 | ms.arrow_pos[edge].y)); |
---|
114 | points.push_back(Gnome::Art::Point(ms.coords[target].x, |
---|
115 | ms.coords[target].y)); |
---|
116 | line.property_points().set_value(points); |
---|
117 | } |
---|
118 | |
---|
119 | bool GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler(GdkEvent* e) |
---|
120 | { |
---|
121 | switch(e->type) |
---|
122 | { |
---|
123 | case GDK_BUTTON_PRESS: |
---|
124 | //we mark the location of the event to be able to calculate parameters |
---|
125 | //of dragging |
---|
126 | if(canvas.getActualTool()!=CREATE_NODE) |
---|
127 | { |
---|
128 | canvas.toggleEdgeActivity(this, true); |
---|
129 | clicked_x=e->button.x; |
---|
130 | clicked_y=e->button.y; |
---|
131 | isbutton=true; |
---|
132 | } |
---|
133 | break; |
---|
134 | case GDK_BUTTON_RELEASE: |
---|
135 | if(canvas.getActualTool()!=CREATE_NODE) |
---|
136 | { |
---|
137 | canvas.toggleEdgeActivity(this, false); |
---|
138 | isbutton=false; |
---|
139 | } |
---|
140 | break; |
---|
141 | case GDK_MOTION_NOTIFY: |
---|
142 | //we only have to do sg. if the mouse button is pressed |
---|
143 | if(isbutton) |
---|
144 | { |
---|
145 | //new coordinates will be the old values, |
---|
146 | //because the item will be moved to the |
---|
147 | //new coordinate therefore the new movement |
---|
148 | //has to be calculated from here |
---|
149 | |
---|
150 | double dx=e->motion.x-clicked_x; |
---|
151 | double dy=e->motion.y-clicked_y; |
---|
152 | |
---|
153 | Gnome::Canvas::Points points_new; |
---|
154 | |
---|
155 | canvas.mytab.mapstorage->arrow_pos.set(edge, canvas.mytab.mapstorage->arrow_pos[edge] + XY(dx, dy)); |
---|
156 | |
---|
157 | draw(); |
---|
158 | canvas.textReposition(canvas.mytab.mapstorage->arrow_pos[edge]); |
---|
159 | |
---|
160 | clicked_x=e->motion.x; |
---|
161 | clicked_y=e->motion.y; |
---|
162 | |
---|
163 | } |
---|
164 | default: break; |
---|
165 | } |
---|
166 | |
---|
167 | return true; |
---|
168 | } |
---|
169 | |
---|
170 | void GraphDisplayerCanvas::BrokenEdge::setLineWidth(int w) |
---|
171 | { |
---|
172 | line.property_width_units().set_value(w); |
---|
173 | } |
---|
174 | |
---|
175 | void GraphDisplayerCanvas::BrokenEdge::setFillColor(Gdk::Color c) |
---|
176 | { |
---|
177 | line.property_fill_color_gdk().set_value(c); |
---|
178 | } |
---|
179 | |
---|
180 | GraphDisplayerCanvas::LoopEdge::LoopEdge(Gnome::Canvas::Group& _group, |
---|
181 | Edge _edge, GraphDisplayerCanvas& _canvas) : |
---|
182 | EdgeBase(_group, _edge, _canvas), line(*this), isbutton(false) |
---|
183 | { |
---|
184 | arrow.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::LoopEdge::edgeFormerEventHandler)); |
---|
185 | |
---|
186 | line.property_outline_color().set_value("green"); |
---|
187 | line.property_width_units().set_value(10); |
---|
188 | line.lower_to_bottom(); |
---|
189 | |
---|
190 | draw(); |
---|
191 | } |
---|
192 | |
---|
193 | GraphDisplayerCanvas::LoopEdge::~LoopEdge() |
---|
194 | { |
---|
195 | } |
---|
196 | |
---|
197 | void GraphDisplayerCanvas::LoopEdge::draw() |
---|
198 | { |
---|
199 | MapStorage& ms = *canvas.mytab.mapstorage; |
---|
200 | |
---|
201 | Node node = ms.graph.source(edge); |
---|
202 | XY center = (ms.coords[node] + ms.arrow_pos[edge]) / 2.0; |
---|
203 | |
---|
204 | XY unit_vector_in_dir(rot90(center - ms.arrow_pos[edge])); |
---|
205 | double length = sqrt(unit_vector_in_dir.normSquare()); |
---|
206 | unit_vector_in_dir /= length; |
---|
207 | |
---|
208 | drawArrow(unit_vector_in_dir); |
---|
209 | |
---|
210 | double radius = |
---|
211 | sqrt((ms.arrow_pos[edge] - ms.coords[node]).normSquare()) / 2.0; |
---|
212 | |
---|
213 | XY p1 = center + XY(-radius, radius); |
---|
214 | XY p2 = center + XY( radius, -radius); |
---|
215 | line.property_x1().set_value(p1.x); |
---|
216 | line.property_y1().set_value(p1.y); |
---|
217 | line.property_x2().set_value(p2.x); |
---|
218 | line.property_y2().set_value(p2.y); |
---|
219 | } |
---|
220 | |
---|
221 | void GraphDisplayerCanvas::LoopEdge::setLineWidth(int w) |
---|
222 | { |
---|
223 | line.property_width_units().set_value(w); |
---|
224 | } |
---|
225 | |
---|
226 | void GraphDisplayerCanvas::LoopEdge::setFillColor(Gdk::Color c) |
---|
227 | { |
---|
228 | line.property_outline_color_gdk().set_value(c); |
---|
229 | } |
---|
230 | |
---|
231 | bool GraphDisplayerCanvas::LoopEdge::edgeFormerEventHandler(GdkEvent* e) |
---|
232 | { |
---|
233 | switch(e->type) |
---|
234 | { |
---|
235 | case GDK_BUTTON_PRESS: |
---|
236 | if(canvas.getActualTool()!=CREATE_NODE) |
---|
237 | { |
---|
238 | canvas.toggleEdgeActivity(this, true); |
---|
239 | isbutton=true; |
---|
240 | } |
---|
241 | break; |
---|
242 | case GDK_BUTTON_RELEASE: |
---|
243 | if(canvas.getActualTool()!=CREATE_NODE) |
---|
244 | { |
---|
245 | canvas.toggleEdgeActivity(this, false); |
---|
246 | isbutton=false; |
---|
247 | } |
---|
248 | break; |
---|
249 | case GDK_MOTION_NOTIFY: |
---|
250 | if(isbutton) |
---|
251 | { |
---|
252 | canvas.mytab.mapstorage->arrow_pos.set(edge, XY(e->motion.x, e->motion.y)); |
---|
253 | |
---|
254 | draw(); |
---|
255 | canvas.textReposition(canvas.mytab.mapstorage->arrow_pos[edge]); |
---|
256 | } |
---|
257 | default: break; |
---|
258 | } |
---|
259 | return true; |
---|
260 | } |
---|