alpar@174: /* -*- C++ -*- alpar@174: * alpar@174: * This file is a part of LEMON, a generic C++ optimization library alpar@174: * alpar@174: * Copyright (C) 2003-2006 alpar@174: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@174: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@174: * alpar@174: * Permission to use, modify and distribute this software is granted alpar@174: * provided that this copyright notice appears in all copies. For alpar@174: * precise terms see the accompanying LICENSE file. alpar@174: * alpar@174: * This software is provided "AS IS" with no warranty of any kind, alpar@174: * express or implied, and with no claim as to its suitability for any alpar@174: * purpose. alpar@174: * alpar@174: */ alpar@174: hegyi@89: #include "graph_displayer_canvas.h" alpar@59: #include hegyi@17: ladanyi@147: GraphDisplayerCanvas::EdgeBase::EdgeBase(Gnome::Canvas::Group& _group, Edge _edge, GraphDisplayerCanvas& _canvas) : ladanyi@147: Gnome::Canvas::Group(_group), edge(_edge), canvas(_canvas), arrow(*this) hegyi@17: { ladanyi@147: arrow.property_fill_color().set_value("red"); ladanyi@147: arrow.lower_to_bottom(); ladanyi@152: lower_to_bottom(); ladanyi@147: } ladanyi@147: ladanyi@147: GraphDisplayerCanvas::EdgeBase::~EdgeBase() ladanyi@147: { ladanyi@147: } ladanyi@147: ladanyi@147: void GraphDisplayerCanvas::EdgeBase::drawArrow(XY unit_vector_in_dir) ladanyi@147: { ladanyi@147: MapStorage& ms = canvas.mytab.mapstorage; ladanyi@147: XY center(ms.arrow_pos[edge]); ladanyi@147: XY unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x); ladanyi@147: ladanyi@147: // /\ // top ladanyi@147: // / \ // ladanyi@147: // - - // c(enter)l(eft), ccl, ccr, cr ladanyi@147: // || // ladanyi@147: // || // b(ottom)l, br ladanyi@147: ladanyi@147: double size=3; ladanyi@147: ladanyi@147: XY bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size ); ladanyi@147: XY br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size ); ladanyi@147: XY ccl(center + unit_vector_in_dir * size + unit_norm_vector * size ); ladanyi@147: XY ccr(center + unit_vector_in_dir * size - unit_norm_vector * size ); ladanyi@147: XY cl (center + unit_vector_in_dir * size + unit_norm_vector * 2 * size ); ladanyi@147: XY cr (center + unit_vector_in_dir * size - unit_norm_vector * 2 * size ); ladanyi@147: XY top(center + unit_vector_in_dir * 3 * size); ladanyi@147: ladanyi@147: Gnome::Canvas::Points arrow_points; ladanyi@147: arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y ) ); ladanyi@147: arrow_points.push_back(Gnome::Art::Point( br.x , br.y ) ); ladanyi@147: arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) ); ladanyi@147: arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y ) ); ladanyi@147: arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) ); ladanyi@147: arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y ) ); ladanyi@147: arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) ); ladanyi@147: ladanyi@147: arrow.property_points().set_value(arrow_points); ladanyi@147: } ladanyi@147: ladanyi@147: GraphDisplayerCanvas::BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, ladanyi@147: Edge _edge, GraphDisplayerCanvas & gc) : EdgeBase(g, _edge, gc), ladanyi@147: isbutton(false), line(*this) ladanyi@147: { ladanyi@147: arrow.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler)); ladanyi@147: ladanyi@147: line.property_fill_color().set_value("green"); ladanyi@147: line.property_width_units().set_value(10); ladanyi@147: line.lower_to_bottom(); ladanyi@147: ladanyi@98: draw(); hegyi@19: } hegyi@19: hegyi@89: GraphDisplayerCanvas::BrokenEdge::~BrokenEdge() hegyi@19: { hegyi@19: } hegyi@19: ladanyi@98: void GraphDisplayerCanvas::BrokenEdge::draw() hegyi@19: { ladanyi@147: MapStorage& ms = canvas.mytab.mapstorage; ladanyi@147: ladanyi@147: //calculating coordinates of the direction indicator arrow ladanyi@147: XY head(ms.coords[ms.graph.target(edge)]); ladanyi@147: XY center(ms.arrow_pos[edge]); ladanyi@147: ladanyi@147: XY unit_vector_in_dir(head-center); ladanyi@147: double length=sqrt( unit_vector_in_dir.normSquare() ); ladanyi@147: ladanyi@147: unit_vector_in_dir/=length; ladanyi@147: ladanyi@147: // update the arrow ladanyi@147: drawArrow(unit_vector_in_dir); ladanyi@98: ladanyi@98: // update the edge ladanyi@147: Gnome::Canvas::Points points; ladanyi@147: Node source = ms.graph.source(edge); ladanyi@147: Node target = ms.graph.target(edge); ladanyi@147: points.push_back(Gnome::Art::Point(ms.coords[source].x, ladanyi@147: ms.coords[source].y)); ladanyi@147: points.push_back(Gnome::Art::Point(ms.arrow_pos[edge].x, ladanyi@147: ms.arrow_pos[edge].y)); ladanyi@147: points.push_back(Gnome::Art::Point(ms.coords[target].x, ladanyi@147: ms.coords[target].y)); ladanyi@147: line.property_points().set_value(points); hegyi@17: } hegyi@19: hegyi@89: bool GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler(GdkEvent* e) hegyi@19: { hegyi@19: switch(e->type) ladanyi@147: { hegyi@19: case GDK_BUTTON_PRESS: ladanyi@147: //we mark the location of the event to be able to calculate parameters ladanyi@147: //of dragging ladanyi@147: if(canvas.getActualTool()!=CREATE_NODE) ladanyi@147: { ladanyi@147: canvas.toggleEdgeActivity(this, true); ladanyi@147: clicked_x=e->button.x; ladanyi@147: clicked_y=e->button.y; ladanyi@147: isbutton=true; ladanyi@147: } hegyi@19: break; hegyi@19: case GDK_BUTTON_RELEASE: ladanyi@147: if(canvas.getActualTool()!=CREATE_NODE) ladanyi@147: { ladanyi@147: canvas.toggleEdgeActivity(this, false); ladanyi@147: isbutton=false; ladanyi@147: } hegyi@19: break; hegyi@19: case GDK_MOTION_NOTIFY: hegyi@19: //we only have to do sg. if the mouse button is pressed hegyi@19: if(isbutton) ladanyi@147: { ladanyi@147: //new coordinates will be the old values, ladanyi@147: //because the item will be moved to the ladanyi@147: //new coordinate therefore the new movement ladanyi@147: //has to be calculated from here hegyi@19: ladanyi@147: double dx=e->motion.x-clicked_x; ladanyi@147: double dy=e->motion.y-clicked_y; hegyi@19: ladanyi@147: Gnome::Canvas::Points points_new; hegyi@19: ladanyi@147: canvas.mytab.mapstorage.arrow_pos.set(edge, canvas.mytab.mapstorage.arrow_pos[edge] + XY(dx, dy)); hegyi@19: ladanyi@147: draw(); ladanyi@147: canvas.textReposition(canvas.mytab.mapstorage.arrow_pos[edge]); hegyi@19: ladanyi@147: clicked_x=e->motion.x; ladanyi@147: clicked_y=e->motion.y; hegyi@19: ladanyi@147: } hegyi@19: default: break; ladanyi@147: } hegyi@19: hegyi@19: return true; hegyi@19: } ladanyi@147: ladanyi@147: void GraphDisplayerCanvas::BrokenEdge::setLineWidth(int w) ladanyi@147: { ladanyi@147: line.property_width_units().set_value(w); ladanyi@147: } ladanyi@147: ladanyi@147: void GraphDisplayerCanvas::BrokenEdge::setFillColor(Gdk::Color c) ladanyi@147: { ladanyi@147: line.property_fill_color_gdk().set_value(c); ladanyi@147: } ladanyi@147: ladanyi@147: GraphDisplayerCanvas::LoopEdge::LoopEdge(Gnome::Canvas::Group& _group, ladanyi@147: Edge _edge, GraphDisplayerCanvas& _canvas) : ladanyi@151: EdgeBase(_group, _edge, _canvas), line(*this), isbutton(false) ladanyi@147: { ladanyi@151: arrow.signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::LoopEdge::edgeFormerEventHandler)); ladanyi@151: ladanyi@151: line.property_outline_color().set_value("green"); ladanyi@151: line.property_width_units().set_value(10); ladanyi@147: line.lower_to_bottom(); ladanyi@147: ladanyi@147: draw(); ladanyi@147: } ladanyi@147: ladanyi@147: GraphDisplayerCanvas::LoopEdge::~LoopEdge() ladanyi@147: { ladanyi@147: } ladanyi@147: ladanyi@147: void GraphDisplayerCanvas::LoopEdge::draw() ladanyi@147: { ladanyi@147: MapStorage& ms = canvas.mytab.mapstorage; ladanyi@147: ladanyi@147: Node node = ms.graph.source(edge); ladanyi@147: XY center = (ms.coords[node] + ms.arrow_pos[edge]) / 2.0; ladanyi@147: ladanyi@147: XY unit_vector_in_dir(rot90(center - ms.arrow_pos[edge])); ladanyi@147: double length = sqrt(unit_vector_in_dir.normSquare()); ladanyi@147: unit_vector_in_dir /= length; ladanyi@147: ladanyi@147: drawArrow(unit_vector_in_dir); ladanyi@147: ladanyi@147: double radius = ladanyi@147: sqrt((ms.arrow_pos[edge] - ms.coords[node]).normSquare()) / 2.0; ladanyi@147: ladanyi@147: XY p1 = center + XY(-radius, radius); ladanyi@147: XY p2 = center + XY( radius, -radius); ladanyi@147: line.property_x1().set_value(p1.x); ladanyi@147: line.property_y1().set_value(p1.y); ladanyi@147: line.property_x2().set_value(p2.x); ladanyi@147: line.property_y2().set_value(p2.y); ladanyi@147: } ladanyi@147: ladanyi@147: void GraphDisplayerCanvas::LoopEdge::setLineWidth(int w) ladanyi@147: { ladanyi@147: line.property_width_units().set_value(w); ladanyi@147: } ladanyi@147: ladanyi@147: void GraphDisplayerCanvas::LoopEdge::setFillColor(Gdk::Color c) ladanyi@147: { ladanyi@151: line.property_outline_color_gdk().set_value(c); ladanyi@147: } ladanyi@151: ladanyi@151: bool GraphDisplayerCanvas::LoopEdge::edgeFormerEventHandler(GdkEvent* e) ladanyi@151: { ladanyi@151: switch(e->type) ladanyi@151: { ladanyi@151: case GDK_BUTTON_PRESS: ladanyi@151: if(canvas.getActualTool()!=CREATE_NODE) ladanyi@151: { ladanyi@151: canvas.toggleEdgeActivity(this, true); ladanyi@151: isbutton=true; ladanyi@151: } ladanyi@151: break; ladanyi@151: case GDK_BUTTON_RELEASE: ladanyi@151: if(canvas.getActualTool()!=CREATE_NODE) ladanyi@151: { ladanyi@151: canvas.toggleEdgeActivity(this, false); ladanyi@151: isbutton=false; ladanyi@151: } ladanyi@151: break; ladanyi@151: case GDK_MOTION_NOTIFY: ladanyi@151: if(isbutton) ladanyi@151: { ladanyi@151: canvas.mytab.mapstorage.arrow_pos.set(edge, XY(e->motion.x, e->motion.y)); ladanyi@151: ladanyi@151: draw(); ladanyi@151: canvas.textReposition(canvas.mytab.mapstorage.arrow_pos[edge]); ladanyi@151: } ladanyi@151: default: break; ladanyi@151: } ladanyi@151: return true; ladanyi@151: }