00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "graphvizgraph.h"
00012
00013 #include <graphviz/graph.h>
00014 #include <kdebug.h>
00015
00016 #define DPI 96
00017
00018 char* _strcpy(const char* name)
00019 {
00020 char *a;
00021 strlen(name);
00022 a=new char[strlen(name)+1];
00023 a=strcpy(a,name);
00024 return a;
00025 }
00026 namespace Autolayout
00027 {
00028
00033 GraphvizGraph::GraphvizGraph()
00034 : Autolayout::Graph()
00035 {
00036 aginit();
00037 empty_flag=true;
00038 _agraph = agopen("graph",AGDIGRAPH);
00039 a_width= agnodeattr(_agraph, "width", "");;
00040 a_height= agnodeattr(_agraph, "height", "");
00041 a_label = agnodeattr(_agraph, "label", "");
00042 a_weight= agedgeattr(_agraph,"weight","");
00043 agnodeattr(_agraph, "fixedsize", "true");
00044 agnodeattr(_agraph, "margin", "0.01,0.01");
00045 agnodeattr(_agraph, "shape", "box");
00046 agraphattr(_agraph, "dpi", "DPI/0");
00047
00048
00049 }
00050
00051
00052 GraphvizGraph::~GraphvizGraph()
00053 {
00054 nodelist.clear();
00055 agclose(_agraph);
00056
00057
00058
00059
00060
00061
00062
00063 }
00064
00065
00066
00067
00068 void GraphvizGraph::addEdge(const char* nodea, const char* nodeb, int weight)
00069 {
00070 char *a=_strcpy(nodea);
00071 char *b=_strcpy(nodeb);
00072 char *weight_str;
00073 asprintf(&weight_str,"%d",weight);
00074 Agedge_t* e= agedge(_agraph,agnode(_agraph,a),agnode(_agraph,b));
00075 delete[](a);
00076 delete[](b);
00077 agxset(e,a_weight->index,weight_str);
00078
00079
00080 }
00081
00082 void GraphvizGraph::addNode(const char* name, int width, int height)
00083 {
00084 char *a =_strcpy(name);
00085 char *w_str,*h_str;
00086 Agnode_t* node =agnode(_agraph,a);
00087 delete[](a);
00088 agxset(node, a_label->index, "a");
00089 asprintf(&h_str,"%f",((float)height)/DPI);
00090 asprintf(&w_str,"%f",((float)width)/DPI);
00091 agxset(node, a_height->index,h_str);
00092 free (h_str);
00093 agxset(node, a_width->index, w_str);
00094 free (w_str);
00095 empty_flag = false;
00096 }
00097
00098
00099 void Autolayout::GraphvizGraph::setCompressShapes( bool b )
00100 {
00101 if (empty())
00102 {
00103 if (b) agraphattr(_agraph,"ratio","compress");
00104 else agraphattr(_agraph,"ratio","");
00105 }
00106 }
00107
00108 void Autolayout::GraphvizGraph::setCenterDiagram( bool b )
00109 {
00110 if (empty())
00111 {
00112 if (b) agraphattr(_agraph,"center","true");
00113 else agraphattr(_agraph,"center","false");
00114 }
00115 }
00116
00117 void Autolayout::GraphvizGraph::setShapeSeparation( int i )
00118 {
00119 char* a;
00120 asprintf(&a,"%f",((float) i)/10.0);
00121 agraphattr(_agraph,"nodesep",a);
00122 free (a);
00123 }
00124
00125 bool Autolayout::GraphvizGraph::empty( )
00126 {
00127 return empty_flag;
00128 }
00129
00130 Autolayout::Node* Autolayout::GraphvizGraph::getNode( const char * arg1 )
00131 {
00132 char *a = _strcpy(arg1);
00133 Autolayout::GraphvizNode* b=
00134 new Autolayout::GraphvizNode(agnode(_agraph,a));
00135 delete[](a);
00136 nodelist.push_back(b);
00137 return b;
00138 }
00139
00140 void GraphvizGraph::setCanvas( Autolayout::Canvas * canvas)
00141 {
00142 char buf[100];
00143 sprintf(buf,"%f,%f",((float)canvas->getMaxX()/DPI),((float)canvas->getMaxY()/DPI));
00144 kDebug() << "size: " << buf << endl;
00145 agraphattr(_agraph, "size", buf);
00146 agraphattr(_agraph, "page", buf);
00147 }
00148
00149 }
00150