1.1 --- a/src/work/peter/graph-displayer.cc Sun Apr 17 18:57:22 2005 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,285 +0,0 @@
1.4 -// This example was started by Guillaume Laurent.
1.5 -// It has become a place to dump code that tests parts of the
1.6 -// gnomemm canvas code. Little thought has been given to the
1.7 -// actual on-screen output.
1.8 -
1.9 -#include <libgnomecanvasmm.h>
1.10 -#include <libgnomecanvasmm/polygon.h>
1.11 -
1.12 -#include <fstream>
1.13 -#include <iostream>
1.14 -
1.15 -#include <lemon/list_graph.h>
1.16 -#include <lemon/graph_reader.h>
1.17 -#include <lemon/graph_writer.h>
1.18 -#include <lemon/graph_utils.h>
1.19 -#include <lemon/maps.h>
1.20 -#include <lemon/error.h>
1.21 -#include <lemon/xy.h>
1.22 -
1.23 -using namespace lemon;
1.24 -
1.25 -typedef xy<double> Coordinates;
1.26 -typedef ListGraph Graph;
1.27 -typedef Graph::NodeMap<Coordinates> CoordinatesMap;
1.28 -typedef Graph::Node Node;
1.29 -typedef Graph::EdgeIt EdgeIt;
1.30 -typedef Graph::NodeIt NodeIt;
1.31 -
1.32 -class GraphDisplayerCanvas : public Gnome::Canvas::CanvasAA
1.33 -{
1.34 - typedef Gnome::Canvas::CanvasAA Parent;
1.35 -
1.36 -public:
1.37 - GraphDisplayerCanvas(Graph &, CoordinatesMap &);
1.38 - virtual ~GraphDisplayerCanvas();
1.39 -
1.40 -private:
1.41 -
1.42 - ///Event handler function that handles dragging nodes of displayed_graph
1.43 - bool event_handler(GdkEvent* e, Node n);
1.44 -
1.45 - ///The graph, on which we work
1.46 - Graph g;
1.47 - ///Map of nodes of planefigure
1.48 - Graph::NodeMap<Gnome::Canvas::Ellipse *> nodesmap;
1.49 - ///Map of edges of planefigure
1.50 - Graph::EdgeMap<Gnome::Canvas::Line *> edgesmap;
1.51 - ///Group of graphical elements of displayed_graph
1.52 - Gnome::Canvas::Group displayed_graph;
1.53 -
1.54 - ///Indicates whether the button of mouse is pressed or not
1.55 - bool isbutton;
1.56 -
1.57 - ///At this location was the mousebutton pressed.
1.58 - ///It helps to calculate the distance of dragging.
1.59 - double clicked_x, clicked_y;
1.60 -
1.61 - ///Remembers which Gnome::Canvas::Item was pressed.
1.62 - ///this variable is needed, because
1.63 - ///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault
1.64 - ///2. we would like to handle only ony item per movement, therefore quering it is not a working solution
1.65 - Gnome::Canvas::Item * active_item;
1.66 -
1.67 -
1.68 -};
1.69 -
1.70 -
1.71 -///This function moves only one node of displayed_graph,
1.72 -///but recalculate the location of weight point,
1.73 -///and also redraw the sides of the planefigure.
1.74 -bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
1.75 -{
1.76 - switch(e->type)
1.77 - {
1.78 - case GDK_BUTTON_PRESS:
1.79 - clicked_x=e->button.x;
1.80 - clicked_y=e->button.y;
1.81 - active_item=(get_item_at(e->button.x, e->button.y));
1.82 - isbutton=true;
1.83 - break;
1.84 - case GDK_BUTTON_RELEASE:
1.85 - isbutton=false;
1.86 - active_item=NULL;
1.87 - break;
1.88 - case GDK_MOTION_NOTIFY:
1.89 - if(isbutton)
1.90 - {
1.91 - double dx=e->motion.x-clicked_x;
1.92 - double dy=e->motion.y-clicked_y;
1.93 - active_item->move(dx, dy);
1.94 - clicked_x=e->motion.x;
1.95 - clicked_y=e->motion.y;
1.96 -
1.97 - EdgeIt e;
1.98 -
1.99 - g.firstOut(e,n);
1.100 - for(;e!=INVALID;g.nextOut(e))
1.101 - {
1.102 - Gnome::Canvas::Points coos;
1.103 - double x1, x2, y1, y2;
1.104 -
1.105 - nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
1.106 - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
1.107 -
1.108 - nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
1.109 - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
1.110 -
1.111 - edgesmap[e]->property_points().set_value(coos);
1.112 - }
1.113 -
1.114 - g.firstIn(e,n);
1.115 - for(;e!=INVALID;g.nextIn(e))
1.116 - {
1.117 - Gnome::Canvas::Points coos;
1.118 - double x1, x2, y1, y2;
1.119 -
1.120 - nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
1.121 - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
1.122 -
1.123 - nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
1.124 - coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
1.125 -
1.126 - edgesmap[e]->property_points().set_value(coos);
1.127 - }
1.128 - }
1.129 - default: break;
1.130 - }
1.131 - return true;
1.132 -}
1.133 -
1.134 -GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & gr, CoordinatesMap & cm):g(gr),nodesmap(g),edgesmap(g),displayed_graph(*(root()), 0, 0),isbutton(false),active_item(NULL)
1.135 -{
1.136 - for (EdgeIt i(g); i!=INVALID; ++i)
1.137 - {
1.138 - Gnome::Canvas::Points coos;
1.139 - coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
1.140 - coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
1.141 - edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
1.142 - *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
1.143 - edgesmap[i]->property_width_pixels().set_value(10);
1.144 - }
1.145 - for (NodeIt i(g); i!=INVALID; ++i)
1.146 - {
1.147 - nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
1.148 - *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
1.149 - *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
1.150 - (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
1.151 - }
1.152 -
1.153 -}
1.154 -
1.155 -GraphDisplayerCanvas::~GraphDisplayerCanvas()
1.156 -{
1.157 - Graph::NodeMap <int> id(g);
1.158 - Graph::NodeMap <double> xc(g);
1.159 - Graph::NodeMap <double> yc(g);
1.160 -
1.161 - int j=1;
1.162 -
1.163 - for (NodeIt i(g); i!=INVALID; ++i)
1.164 - {
1.165 - double x1,y1,x2,y2;
1.166 - nodesmap[i]->get_bounds(x1, y1, x2, y2);
1.167 -
1.168 - id[i]=j++;
1.169 - xc[i]=(x1+x2)/2;
1.170 - yc[i]=(y1+y2)/2;
1.171 - }
1.172 -
1.173 - GraphWriter<Graph> writer(std::cout,g);
1.174 -
1.175 - writer.addNodeMap("id", id);
1.176 - writer.addNodeMap("coordinates_x", xc);
1.177 - writer.addNodeMap("coordinates_y", yc);
1.178 - writer.run();
1.179 -}
1.180 -
1.181 -
1.182 -//MainWin:
1.183 -class MainWin : public Gtk::Window
1.184 -{
1.185 -public:
1.186 - MainWin(const std::string& title, Graph &, CoordinatesMap &);
1.187 -
1.188 -protected:
1.189 - //Member widgets:
1.190 - GraphDisplayerCanvas gd_canvas;
1.191 -};
1.192 -
1.193 -MainWin::MainWin(const std::string& title, Graph & graph, CoordinatesMap & cm):gd_canvas(graph, cm)
1.194 -{
1.195 - set_title (title);
1.196 - add(gd_canvas);
1.197 - set_default_size(900,600);
1.198 -
1.199 - show_all();
1.200 -}
1.201 -
1.202 -
1.203 -///This class is responsible for being able
1.204 -///to read xy datastructure from file. It is
1.205 -///based on BaseMap. The set method sets the
1.206 -///appropriate value in the final xy NodeMap
1.207 -///that was given to the constructor.
1.208 -class CoordReaderMap: public MapBase <Node, double>
1.209 -{
1.210 - CoordinatesMap & cm;
1.211 - char xoy;
1.212 -
1.213 -public:
1.214 - CoordReaderMap(char xory, CoordinatesMap & coordmap);
1.215 - void set(Node node, double coord);
1.216 -};
1.217 -
1.218 -///The constructor expects for an xy NodeMap,
1.219 -///and we have to tell it, for which value
1.220 -///of the xy vector is responsible the actual
1.221 -///copy.
1.222 -CoordReaderMap::CoordReaderMap(char xory, CoordinatesMap & coordmap): cm(coordmap)
1.223 -{
1.224 - switch(xory)
1.225 - {
1.226 - case 'x':
1.227 - case 'y':
1.228 - xoy=xory;
1.229 - break;
1.230 - default:
1.231 - throw UninitializedParameter() ;
1.232 - }
1.233 -}
1.234 -
1.235 -///set method sets the appropriate value in the
1.236 -///xy type NodeMap that is under construction
1.237 -void CoordReaderMap::set(Node node, double coord)
1.238 -{
1.239 - switch(xoy)
1.240 - {
1.241 - case 'x':
1.242 - cm[node].x=coord;
1.243 - break;
1.244 - case 'y':
1.245 - cm[node].y=coord;
1.246 - break;
1.247 - default:
1.248 - throw UninitializedParameter() ;
1.249 - }
1.250 -}
1.251 -
1.252 -//main():
1.253 -
1.254 -int main(int argc, char *argv[])
1.255 -{
1.256 - if(argc<2)
1.257 - {
1.258 - std::cerr << "USAGE: gd <input filename.lgf>" << endl;
1.259 - return 0;
1.260 - }
1.261 -
1.262 - Coordinates coosvector;
1.263 -
1.264 - Graph g;
1.265 -
1.266 - CoordinatesMap cm(g);
1.267 - Graph::EdgeMap<double> cap(g);
1.268 -
1.269 - //we create one object to read x coordinates
1.270 - //and one to read y coordinate of nodes and write them to cm NodeMap.
1.271 - CoordReaderMap xreader('x',cm);
1.272 - CoordReaderMap yreader('y',cm);
1.273 -
1.274 - std::ifstream is(argv[1]);
1.275 -
1.276 - GraphReader<Graph> reader(is, g);
1.277 - reader.addNodeMap("coordinates_x", xreader);
1.278 - reader.addNodeMap("coordinates_y", yreader);
1.279 - reader.run();
1.280 -
1.281 - Gnome::Canvas::init();
1.282 - Gtk::Main app(argc, argv);
1.283 -
1.284 - MainWin mainwin("Displayed Graph", g, cm);
1.285 - app.run(mainwin);
1.286 -
1.287 - return 0;
1.288 -}