summaryrefslogtreecommitdiffstats
path: root/umbrello/umbrello/autolayout/graphvizgraph.cpp
blob: 36a50170e47b02474cac1c70d56856a598f1bda6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/***************************************************************************
 *  copyright (C) 2005
 *  Umbrello UML Modeller Authors <uml-devel @uml.sf.net>
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include "graphvizgraph.h"

#include <graphviz/graph.h>
#include <kdebug.h>

#define DPI 96

char* _strcpy(const char* name)
{
    char *a;
    strlen(name);
    a=new char[strlen(name)+1];
    a=strcpy(a,name);
    return a;
}
namespace Autotqlayout
{

/**
 *
 * @return
 */
GraphvizGraph::GraphvizGraph()
        : Autotqlayout::Graph()
{
    aginit();
    empty_flag=true;
    _agraph = agopen("graph",AGDIGRAPH);
    a_width= agnodeattr(_agraph, "width", "");;
    a_height= agnodeattr(_agraph, "height", "");
    a_label = agnodeattr(_agraph, "label", "");
    a_weight= agedgeattr(_agraph,"weight","");
    agnodeattr(_agraph, "fixedsize", "true");
    agnodeattr(_agraph, "margin", "0.01,0.01");
    agnodeattr(_agraph, "shape", "box");
    agraphattr(_agraph, "dpi", "DPI/0");


}


GraphvizGraph::~GraphvizGraph()
{
    nodelist.clear();
    agclose(_agraph);
    /* Free graph structures */
    //#ifndef internal_renderizer

    /* Clean up output file and errors */
    // dotneato_terminate(gvc);
    //dotneato_eof(gvc);

}




void GraphvizGraph::addEdge(const char* nodea, const char* nodeb, int weight)
{
    char *a=_strcpy(nodea);
    char *b=_strcpy(nodeb);
    char *weight_str;
    asprintf(&weight_str,"%d",weight);
    Agedge_t* e= agedge(_agraph,agnode(_agraph,a),agnode(_agraph,b));
    delete[](a);
    delete[](b);
    agxset(e,a_weight->index,weight_str);


}

void GraphvizGraph::addNode(const char* name, int width, int height)
{
    char *a =_strcpy(name);
    char *w_str,*h_str;
    Agnode_t* node =agnode(_agraph,a);
    delete[](a);
    agxset(node, a_label->index, "a");
    asprintf(&h_str,"%f",((float)height)/DPI);
    asprintf(&w_str,"%f",((float)width)/DPI);
    agxset(node, a_height->index,h_str);// sprintf("%d",height));
    free (h_str);
    agxset(node, a_width->index, w_str);
    free (w_str);
    empty_flag = false;
}


void Autotqlayout::GraphvizGraph::setCompressShapes( bool b )
{
    if (empty())
    {
        if (b) agraphattr(_agraph,"ratio","compress");
        else agraphattr(_agraph,"ratio","");
    }
}

void Autotqlayout::GraphvizGraph::setCenterDiagram( bool b )
{
    if (empty())
    {
        if (b)  agraphattr(_agraph,"center","true");
        else  agraphattr(_agraph,"center","false");
    }
}

void Autotqlayout::GraphvizGraph::setShapeSeparation( int i )
{
    char* a;
    asprintf(&a,"%f",((float) i)/10.0);
    agraphattr(_agraph,"nodesep",a);
    free (a);
}

bool Autotqlayout::GraphvizGraph::empty( )
{
    return empty_flag;
}

Autotqlayout::Node* Autotqlayout::GraphvizGraph::getNode( const char * arg1 )
{
    char *a = _strcpy(arg1);
    Autotqlayout::GraphvizNode* b=
        new Autotqlayout::GraphvizNode(agnode(_agraph,a));
    delete[](a);
    nodelist.push_back(b);
    return b;
}

void GraphvizGraph::setCanvas( Autotqlayout::Canvas * canvas)
{
    char buf[100];
    sprintf(buf,"%f,%f",((float)canvas->getMaxX()/DPI),((float)canvas->getMaxY()/DPI));
    kDebug() << "size: " << buf << endl;
    agraphattr(_agraph, "size", buf);
    agraphattr(_agraph, "page", buf);
}

}  // end namespace Autotqlayout