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