Changeset View
Changeset View
Standalone View
Standalone View
src/part/graphexporter.cpp
Show All 18 Lines | |||||
19 | /* This file was callgraphview.cpp, part of KCachegrind. | 19 | /* This file was callgraphview.cpp, part of KCachegrind. | ||
20 | Copyright (C) 2003 Josef Weidendorfer <Josef.Weidendorfer@gmx.de> | 20 | Copyright (C) 2003 Josef Weidendorfer <Josef.Weidendorfer@gmx.de> | ||
21 | 21 | | |||
22 | KCachegrind is free software; you can redistribute it and/or | 22 | KCachegrind is free software; you can redistribute it and/or | ||
23 | modify it under the terms of the GNU General Public | 23 | modify it under the terms of the GNU General Public | ||
24 | License as published by the Free Software Foundation, version 2. | 24 | License as published by the Free Software Foundation, version 2. | ||
25 | */ | 25 | */ | ||
26 | 26 | | |||
27 | | ||||
28 | #include "graphexporter.h" | 27 | #include "graphexporter.h" | ||
29 | #include "dotgraph.h" | 28 | #include "dotgraph.h" | ||
30 | #include "kgraphviewerlib_debug.h" | 29 | #include "kgraphviewerlib_debug.h" | ||
31 | 30 | | |||
32 | #include <QFile> | 31 | #include <QFile> | ||
33 | #include <QTextStream> | 32 | #include <QTextStream> | ||
34 | 33 | | |||
35 | #include <QDebug> | 34 | #include <QDebug> | ||
36 | #include <QTemporaryFile> | 35 | #include <QTemporaryFile> | ||
37 | 36 | | |||
38 | namespace KGraphViewer | 37 | namespace KGraphViewer | ||
39 | { | 38 | { | ||
40 | | ||||
41 | GraphExporter::GraphExporter() | 39 | GraphExporter::GraphExporter() | ||
42 | { | 40 | { | ||
43 | } | 41 | } | ||
44 | 42 | | |||
45 | GraphExporter::~GraphExporter() | 43 | GraphExporter::~GraphExporter() | ||
46 | { | 44 | { | ||
47 | } | 45 | } | ||
48 | 46 | | |||
49 | QString GraphExporter::writeDot(const DotGraph* graph, const QString& fileName) | 47 | QString GraphExporter::writeDot(const DotGraph *graph, const QString &fileName) | ||
50 | { | 48 | { | ||
51 | qCDebug(KGRAPHVIEWERLIB_LOG) << fileName; | 49 | qCDebug(KGRAPHVIEWERLIB_LOG) << fileName; | ||
52 | 50 | | |||
53 | QString actualFileName = fileName; | 51 | QString actualFileName = fileName; | ||
54 | 52 | | |||
55 | if (fileName.isEmpty()) | 53 | if (fileName.isEmpty()) { | ||
56 | { | | |||
57 | QTemporaryFile tempFile; | 54 | QTemporaryFile tempFile; | ||
58 | tempFile.setFileTemplate("XXXXXX.dot"); | 55 | tempFile.setFileTemplate("XXXXXX.dot"); | ||
59 | if (!tempFile.open()) | 56 | if (!tempFile.open()) { | ||
60 | { | | |||
61 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Unable to open for temp file for writing " << tempFile.fileName() << endl; | 57 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Unable to open for temp file for writing " << tempFile.fileName() << endl; | ||
62 | exit(2); | 58 | exit(2); | ||
63 | } | 59 | } | ||
64 | actualFileName = tempFile.fileName(); | 60 | actualFileName = tempFile.fileName(); | ||
65 | qCDebug(KGRAPHVIEWERLIB_LOG) << "using " << actualFileName; | 61 | qCDebug(KGRAPHVIEWERLIB_LOG) << "using " << actualFileName; | ||
66 | } | 62 | } | ||
67 | 63 | | |||
68 | QFile f(actualFileName); | 64 | QFile f(actualFileName); | ||
69 | if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) | 65 | if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) { | ||
70 | { | | |||
71 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Unable to open file for writing " << fileName << endl; | 66 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Unable to open file for writing " << fileName << endl; | ||
72 | exit(2); | 67 | exit(2); | ||
73 | } | 68 | } | ||
74 | 69 | | |||
75 | QTextStream stream(&f); | 70 | QTextStream stream(&f); | ||
76 | 71 | | |||
77 | stream << "digraph \""; | 72 | stream << "digraph \""; | ||
78 | if (graph->id()!="\"\"") | 73 | if (graph->id() != "\"\"") { | ||
79 | { | | |||
80 | stream <<graph->id(); | 74 | stream << graph->id(); | ||
81 | } | 75 | } | ||
82 | stream <<"\" {\n"; | 76 | stream << "\" {\n"; | ||
83 | 77 | | |||
84 | stream << "graph [" << *graph <<"]" << endl; | 78 | stream << "graph [" << *graph << "]" << endl; | ||
85 | 79 | | |||
86 | /// @TODO Subgraph are not represented as needed in DotGraph, so it is not | 80 | /// @TODO Subgraph are not represented as needed in DotGraph, so it is not | ||
87 | /// possible to save them back : to be changed ! | 81 | /// possible to save them back : to be changed ! | ||
88 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "writing subgraphs"; | 82 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "writing subgraphs"; | ||
89 | GraphSubgraphMap::const_iterator sit; | 83 | GraphSubgraphMap::const_iterator sit; | ||
90 | for ( sit = graph->subgraphs().begin(); | 84 | for (sit = graph->subgraphs().begin(); sit != graph->subgraphs().end(); ++sit) { | ||
91 | sit != graph->subgraphs().end(); ++sit ) | | |||
92 | { | | |||
93 | const GraphSubgraph& s = **sit; | 85 | const GraphSubgraph &s = **sit; | ||
94 | (stream) << s; | 86 | (stream) << s; | ||
95 | } | 87 | } | ||
96 | 88 | | |||
97 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "writing nodes"; | 89 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "writing nodes"; | ||
98 | GraphNodeMap::const_iterator nit; | 90 | GraphNodeMap::const_iterator nit; | ||
99 | for ( nit = graph->nodes().begin(); | 91 | for (nit = graph->nodes().begin(); nit != graph->nodes().end(); ++nit) { | ||
100 | nit != graph->nodes().end(); ++nit ) | | |||
101 | { | | |||
102 | (stream) << **nit; | 92 | (stream) << **nit; | ||
103 | } | 93 | } | ||
104 | 94 | | |||
105 | qCDebug(KGRAPHVIEWERLIB_LOG) << "writing edges"; | 95 | qCDebug(KGRAPHVIEWERLIB_LOG) << "writing edges"; | ||
106 | GraphEdgeMap::const_iterator eit; | 96 | GraphEdgeMap::const_iterator eit; | ||
107 | for ( eit = graph->edges().begin(); | 97 | for (eit = graph->edges().begin(); eit != graph->edges().end(); ++eit) { | ||
108 | eit != graph->edges().end(); ++eit ) | | |||
109 | { | | |||
110 | qCDebug(KGRAPHVIEWERLIB_LOG) << "writing edge" << (*eit)->id(); | 98 | qCDebug(KGRAPHVIEWERLIB_LOG) << "writing edge" << (*eit)->id(); | ||
111 | stream << **eit; | 99 | stream << **eit; | ||
112 | } | 100 | } | ||
113 | 101 | | |||
114 | stream << "}\n"; | 102 | stream << "}\n"; | ||
115 | 103 | | |||
116 | f.close(); | 104 | f.close(); | ||
117 | return actualFileName; | 105 | return actualFileName; | ||
118 | } | 106 | } | ||
119 | 107 | | |||
120 | graph_t* GraphExporter::exportToGraphviz(const DotGraph* graph) | 108 | graph_t *GraphExporter::exportToGraphviz(const DotGraph *graph) | ||
121 | { | 109 | { | ||
122 | Agdesc_t type = Agstrictundirected; | 110 | Agdesc_t type = Agstrictundirected; | ||
123 | type.directed = graph->directed(); | 111 | type.directed = graph->directed(); | ||
124 | type.strict = graph->strict(); | 112 | type.strict = graph->strict(); | ||
125 | 113 | | |||
126 | graph_t* agraph = agopen((graph->id()!="\"\"")?graph->id().toUtf8().data():QString("unnamed").toUtf8().data(), type, nullptr); | 114 | graph_t *agraph = agopen((graph->id() != "\"\"") ? graph->id().toUtf8().data() : QString("unnamed").toUtf8().data(), type, nullptr); | ||
127 | 115 | | |||
128 | QTextStream stream; | 116 | QTextStream stream; | ||
129 | graph->exportToGraphviz(agraph); | 117 | graph->exportToGraphviz(agraph); | ||
130 | /// @TODO Subgraph are not represented as needed in DotGraph, so it is not | 118 | /// @TODO Subgraph are not represented as needed in DotGraph, so it is not | ||
131 | /// possible to save them back : to be changed ! | 119 | /// possible to save them back : to be changed ! | ||
132 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "writing subgraphs"; | 120 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "writing subgraphs"; | ||
133 | GraphSubgraphMap::const_iterator sit; | 121 | GraphSubgraphMap::const_iterator sit; | ||
134 | for ( sit = graph->subgraphs().begin(); | 122 | for (sit = graph->subgraphs().begin(); sit != graph->subgraphs().end(); ++sit) { | ||
135 | sit != graph->subgraphs().end(); ++sit ) | | |||
136 | { | | |||
137 | const GraphSubgraph& s = **sit; | 123 | const GraphSubgraph &s = **sit; | ||
138 | graph_t* subgraph = agsubg(agraph, s.id().toUtf8().data(), 1); | 124 | graph_t *subgraph = agsubg(agraph, s.id().toUtf8().data(), 1); | ||
139 | s.exportToGraphviz(subgraph); | 125 | s.exportToGraphviz(subgraph); | ||
140 | } | 126 | } | ||
141 | 127 | | |||
142 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "writing nodes"; | 128 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "writing nodes"; | ||
143 | GraphNodeMap::const_iterator nit; | 129 | GraphNodeMap::const_iterator nit; | ||
144 | foreach (GraphNode* n, graph->nodes()) | 130 | foreach (GraphNode *n, graph->nodes()) { | ||
145 | { | | |||
146 | node_t* node = agnode(agraph, n->id().toUtf8().data(), 1); | 131 | node_t *node = agnode(agraph, n->id().toUtf8().data(), 1); | ||
147 | n->exportToGraphviz(node); | 132 | n->exportToGraphviz(node); | ||
148 | } | 133 | } | ||
149 | 134 | | |||
150 | qCDebug(KGRAPHVIEWERLIB_LOG) << "writing edges"; | 135 | qCDebug(KGRAPHVIEWERLIB_LOG) << "writing edges"; | ||
151 | GraphEdgeMap::const_iterator eit; | 136 | GraphEdgeMap::const_iterator eit; | ||
152 | foreach (GraphEdge* e, graph->edges()) | 137 | foreach (GraphEdge *e, graph->edges()) { | ||
153 | { | | |||
154 | qCDebug(KGRAPHVIEWERLIB_LOG) << "writing edge" << e->id(); | 138 | qCDebug(KGRAPHVIEWERLIB_LOG) << "writing edge" << e->id(); | ||
155 | edge_t* edge = agedge(agraph, agnode(agraph, e->fromNode()->id().toUtf8().data(), 0), | 139 | edge_t *edge = agedge(agraph, agnode(agraph, e->fromNode()->id().toUtf8().data(), 0), agnode(agraph, e->toNode()->id().toUtf8().data(), 0), nullptr, 1); | ||
156 | agnode(agraph, e->toNode()->id().toUtf8().data(), 0), nullptr, 1); | | |||
157 | e->exportToGraphviz(edge); | 140 | e->exportToGraphviz(edge); | ||
158 | } | 141 | } | ||
159 | 142 | | |||
160 | return agraph; | 143 | return agraph; | ||
161 | } | 144 | } | ||
162 | 145 | | |||
163 | } | 146 | } |