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 | { | | |||
40 | 38 | | |||
41 | GraphExporter::GraphExporter() | 39 | GraphExporter::GraphExporter() {} | ||
42 | { | | |||
43 | } | | |||
44 | 40 | | |||
45 | GraphExporter::~GraphExporter() | 41 | GraphExporter::~GraphExporter() {} | ||
46 | { | | |||
47 | } | | |||
48 | 42 | | |||
49 | QString GraphExporter::writeDot(const DotGraph* graph, const QString& fileName) | 43 | QString GraphExporter::writeDot(const DotGraph *graph, | ||
50 | { | 44 | const QString &fileName) { | ||
51 | qCDebug(KGRAPHVIEWERLIB_LOG) << fileName; | 45 | qCDebug(KGRAPHVIEWERLIB_LOG) << fileName; | ||
52 | 46 | | |||
53 | QString actualFileName = fileName; | 47 | QString actualFileName = fileName; | ||
54 | 48 | | |||
55 | if (fileName.isEmpty()) | 49 | if (fileName.isEmpty()) { | ||
56 | { | | |||
57 | QTemporaryFile tempFile; | 50 | QTemporaryFile tempFile; | ||
58 | tempFile.setFileTemplate("XXXXXX.dot"); | 51 | tempFile.setFileTemplate("XXXXXX.dot"); | ||
59 | if (!tempFile.open()) | 52 | if (!tempFile.open()) { | ||
60 | { | 53 | qCWarning(KGRAPHVIEWERLIB_LOG) | ||
61 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Unable to open for temp file for writing " << tempFile.fileName() << endl; | 54 | << "Unable to open for temp file for writing " | ||
55 | << tempFile.fileName() << endl; | ||||
62 | exit(2); | 56 | exit(2); | ||
63 | } | 57 | } | ||
64 | actualFileName = tempFile.fileName(); | 58 | actualFileName = tempFile.fileName(); | ||
65 | qCDebug(KGRAPHVIEWERLIB_LOG) << "using " << actualFileName; | 59 | qCDebug(KGRAPHVIEWERLIB_LOG) << "using " << actualFileName; | ||
66 | } | 60 | } | ||
67 | 61 | | |||
68 | QFile f(actualFileName); | 62 | QFile f(actualFileName); | ||
69 | if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) | 63 | if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) { | ||
70 | { | 64 | qCWarning(KGRAPHVIEWERLIB_LOG) | ||
71 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Unable to open file for writing " << fileName << endl; | 65 | << "Unable to open file for writing " << fileName << endl; | ||
72 | exit(2); | 66 | exit(2); | ||
73 | } | 67 | } | ||
74 | 68 | | |||
75 | QTextStream stream(&f); | 69 | QTextStream stream(&f); | ||
76 | 70 | | |||
77 | stream << "digraph \""; | 71 | stream << "digraph \""; | ||
78 | if (graph->id()!="\"\"") | 72 | if (graph->id() != "\"\"") { | ||
79 | { | | |||
80 | stream <<graph->id(); | 73 | stream << graph->id(); | ||
81 | } | 74 | } | ||
82 | stream <<"\" {\n"; | 75 | stream << "\" {\n"; | ||
83 | 76 | | |||
84 | stream << "graph [" << *graph <<"]" << endl; | 77 | stream << "graph [" << *graph << "]" << endl; | ||
85 | 78 | | |||
86 | /// @TODO Subgraph are not represented as needed in DotGraph, so it is not | 79 | /// @TODO Subgraph are not represented as needed in DotGraph, so it is not | ||
87 | /// possible to save them back : to be changed ! | 80 | /// possible to save them back : to be changed ! | ||
88 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "writing subgraphs"; | 81 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "writing subgraphs"; | ||
89 | GraphSubgraphMap::const_iterator sit; | 82 | GraphSubgraphMap::const_iterator sit; | ||
90 | for ( sit = graph->subgraphs().begin(); | 83 | for (sit = graph->subgraphs().begin(); sit != graph->subgraphs().end(); | ||
91 | sit != graph->subgraphs().end(); ++sit ) | 84 | ++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 | { | | |||
122 | Agdesc_t type = Agstrictundirected; | 109 | Agdesc_t type = Agstrictundirected; | ||
123 | type.directed = graph->directed(); | 110 | type.directed = graph->directed(); | ||
124 | type.strict = graph->strict(); | 111 | type.strict = graph->strict(); | ||
125 | 112 | | |||
126 | graph_t* agraph = agopen((graph->id()!="\"\"")?graph->id().toUtf8().data():QString("unnamed").toUtf8().data(), type, nullptr); | 113 | graph_t *agraph = | ||
114 | agopen((graph->id() != "\"\"") ? graph->id().toUtf8().data() | ||||
115 | : QString("unnamed").toUtf8().data(), | ||||
116 | type, nullptr); | ||||
127 | 117 | | |||
128 | QTextStream stream; | 118 | QTextStream stream; | ||
129 | graph->exportToGraphviz(agraph); | 119 | graph->exportToGraphviz(agraph); | ||
130 | /// @TODO Subgraph are not represented as needed in DotGraph, so it is not | 120 | /// @TODO Subgraph are not represented as needed in DotGraph, so it is not | ||
131 | /// possible to save them back : to be changed ! | 121 | /// possible to save them back : to be changed ! | ||
132 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "writing subgraphs"; | 122 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "writing subgraphs"; | ||
133 | GraphSubgraphMap::const_iterator sit; | 123 | GraphSubgraphMap::const_iterator sit; | ||
134 | for ( sit = graph->subgraphs().begin(); | 124 | for (sit = graph->subgraphs().begin(); sit != graph->subgraphs().end(); | ||
135 | sit != graph->subgraphs().end(); ++sit ) | 125 | ++sit) { | ||
136 | { | | |||
137 | const GraphSubgraph& s = **sit; | 126 | const GraphSubgraph &s = **sit; | ||
138 | graph_t* subgraph = agsubg(agraph, s.id().toUtf8().data(), 1); | 127 | graph_t *subgraph = agsubg(agraph, s.id().toUtf8().data(), 1); | ||
139 | s.exportToGraphviz(subgraph); | 128 | s.exportToGraphviz(subgraph); | ||
140 | } | 129 | } | ||
141 | 130 | | |||
142 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "writing nodes"; | 131 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "writing nodes"; | ||
143 | GraphNodeMap::const_iterator nit; | 132 | GraphNodeMap::const_iterator nit; | ||
144 | foreach (GraphNode* n, graph->nodes()) | 133 | foreach (GraphNode *n, graph->nodes()) { | ||
145 | { | | |||
146 | node_t* node = agnode(agraph, n->id().toUtf8().data(), 1); | 134 | node_t *node = agnode(agraph, n->id().toUtf8().data(), 1); | ||
147 | n->exportToGraphviz(node); | 135 | n->exportToGraphviz(node); | ||
148 | } | 136 | } | ||
149 | 137 | | |||
150 | qCDebug(KGRAPHVIEWERLIB_LOG) << "writing edges"; | 138 | qCDebug(KGRAPHVIEWERLIB_LOG) << "writing edges"; | ||
151 | GraphEdgeMap::const_iterator eit; | 139 | GraphEdgeMap::const_iterator eit; | ||
152 | foreach (GraphEdge* e, graph->edges()) | 140 | foreach (GraphEdge *e, graph->edges()) { | ||
153 | { | | |||
154 | qCDebug(KGRAPHVIEWERLIB_LOG) << "writing edge" << e->id(); | 141 | qCDebug(KGRAPHVIEWERLIB_LOG) << "writing edge" << e->id(); | ||
155 | edge_t* edge = agedge(agraph, agnode(agraph, e->fromNode()->id().toUtf8().data(), 0), | 142 | edge_t *edge = agedge( | ||
143 | agraph, agnode(agraph, e->fromNode()->id().toUtf8().data(), 0), | ||||
156 | agnode(agraph, e->toNode()->id().toUtf8().data(), 0), nullptr, 1); | 144 | agnode(agraph, e->toNode()->id().toUtf8().data(), 0), nullptr, 1); | ||
157 | e->exportToGraphviz(edge); | 145 | e->exportToGraphviz(edge); | ||
158 | } | 146 | } | ||
159 | 147 | | |||
160 | return agraph; | 148 | return agraph; | ||
161 | } | 149 | } | ||
162 | 150 | | |||
163 | } | 151 | } // namespace KGraphViewer |