Changeset View
Changeset View
Standalone View
Standalone View
src/part/dotgraph.cpp
Show All 11 Lines | 1 | /* This file is part of KGraphViewer. | |||
---|---|---|---|---|---|
12 | 12 | | |||
13 | You should have received a copy of the GNU General Public License | 13 | You should have received a copy of the GNU General Public License | ||
14 | along with this program; if not, write to the Free Software | 14 | along with this program; if not, write to the Free Software | ||
15 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | 15 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
16 | 02110-1301, USA | 16 | 02110-1301, USA | ||
17 | */ | 17 | */ | ||
18 | 18 | | |||
19 | #include "dotgraph.h" | 19 | #include "dotgraph.h" | ||
20 | #include "dotgrammar.h" | | |||
21 | #include "graphexporter.h" | | |||
22 | #include "DotGraphParsingHelper.h" | 20 | #include "DotGraphParsingHelper.h" | ||
23 | #include "canvasedge.h" | 21 | #include "canvasedge.h" | ||
24 | #include "canvassubgraph.h" | 22 | #include "canvassubgraph.h" | ||
25 | #include "layoutagraphthread.h" | 23 | #include "dotgrammar.h" | ||
24 | #include "graphexporter.h" | ||||
26 | #include "kgraphviewerlib_debug.h" | 25 | #include "kgraphviewerlib_debug.h" | ||
26 | #include "layoutagraphthread.h" | ||||
27 | 27 | | |||
28 | #include <iostream> | | |||
29 | #include <stdio.h> | | |||
30 | #include <stdlib.h> | | |||
31 | #include "fdstream.hpp" | 28 | #include "fdstream.hpp" | ||
32 | #include <boost/spirit/include/classic_confix.hpp> | 29 | #include <boost/spirit/include/classic_confix.hpp> | ||
33 | #include <graphviz/gvc.h> | 30 | #include <graphviz/gvc.h> | ||
31 | #include <iostream> | ||||
32 | #include <stdio.h> | ||||
33 | #include <stdlib.h> | ||||
34 | 34 | | |||
35 | #include <QMessageBox> | 35 | #include <QMessageBox> | ||
36 | 36 | | |||
37 | #include <QByteArray> | ||||
37 | #include <QFile> | 38 | #include <QFile> | ||
39 | #include <QMutexLocker> | ||||
38 | #include <QPair> | 40 | #include <QPair> | ||
39 | #include <QByteArray> | | |||
40 | #include <QProcess> | 41 | #include <QProcess> | ||
41 | #include <QMutexLocker> | | |||
42 | #include <QUuid> | 42 | #include <QUuid> | ||
43 | #include <klocalizedstring.h> | 43 | #include <klocalizedstring.h> | ||
44 | 44 | | |||
45 | using namespace boost; | 45 | using namespace boost; | ||
46 | using namespace boost::spirit::classic; | 46 | using namespace boost::spirit::classic; | ||
47 | 47 | | |||
48 | extern KGraphViewer::DotGraphParsingHelper* phelper; | 48 | extern KGraphViewer::DotGraphParsingHelper *phelper; | ||
49 | 49 | | |||
50 | | ||||
51 | namespace KGraphViewer | 50 | namespace KGraphViewer | ||
52 | { | 51 | { | ||
53 | | ||||
54 | | ||||
55 | const distinct_parser<> keyword_p("0-9a-zA-Z_"); | 52 | const distinct_parser<> keyword_p("0-9a-zA-Z_"); | ||
56 | 53 | | |||
57 | DotGraph::DotGraph() : | 54 | DotGraph::DotGraph() | ||
58 | GraphElement(), | 55 | : GraphElement() | ||
59 | m_dotFileName(""),m_width(0.0), m_height(0.0),m_scale(1.0), | 56 | , m_dotFileName("") | ||
60 | m_directed(true),m_strict(false), | 57 | , m_width(0.0) | ||
61 | m_layoutCommand(""), | 58 | , m_height(0.0) | ||
62 | m_horizCellFactor(0), m_vertCellFactor(0), | 59 | , m_scale(1.0) | ||
63 | m_wdhcf(0), m_hdvcf(0), | 60 | , m_directed(true) | ||
64 | m_readWrite(false), | 61 | , m_strict(false) | ||
65 | m_dot(nullptr), | 62 | , m_layoutCommand("") | ||
66 | m_phase(Initial), | 63 | , m_horizCellFactor(0) | ||
67 | m_useLibrary(false) | 64 | , m_vertCellFactor(0) | ||
65 | , m_wdhcf(0) | ||||
66 | , m_hdvcf(0) | ||||
67 | , m_readWrite(false) | ||||
68 | , m_dot(nullptr) | ||||
69 | , m_phase(Initial) | ||||
70 | , m_useLibrary(false) | ||||
68 | { | 71 | { | ||
69 | setId("unnamed"); | 72 | setId("unnamed"); | ||
70 | } | 73 | } | ||
71 | 74 | | |||
72 | DotGraph::DotGraph(const QString& command, const QString& fileName) : | 75 | DotGraph::DotGraph(const QString &command, const QString &fileName) | ||
73 | GraphElement(), | 76 | : GraphElement() | ||
74 | m_dotFileName(fileName),m_width(0.0), m_height(0.0),m_scale(1.0), | 77 | , m_dotFileName(fileName) | ||
75 | m_directed(true),m_strict(false), | 78 | , m_width(0.0) | ||
76 | m_layoutCommand(command), | 79 | , m_height(0.0) | ||
77 | m_horizCellFactor(0), m_vertCellFactor(0), | 80 | , m_scale(1.0) | ||
78 | m_wdhcf(0), m_hdvcf(0), | 81 | , m_directed(true) | ||
79 | m_readWrite(false), | 82 | , m_strict(false) | ||
80 | m_dot(nullptr), | 83 | , m_layoutCommand(command) | ||
81 | m_phase(Initial), | 84 | , m_horizCellFactor(0) | ||
82 | m_useLibrary(false) | 85 | , m_vertCellFactor(0) | ||
86 | , m_wdhcf(0) | ||||
87 | , m_hdvcf(0) | ||||
88 | , m_readWrite(false) | ||||
89 | , m_dot(nullptr) | ||||
90 | , m_phase(Initial) | ||||
91 | , m_useLibrary(false) | ||||
83 | { | 92 | { | ||
84 | setId("unnamed"); | 93 | setId("unnamed"); | ||
85 | } | 94 | } | ||
86 | 95 | | |||
87 | DotGraph::~DotGraph() | 96 | DotGraph::~DotGraph() | ||
88 | { | 97 | { | ||
89 | qDeleteAll(m_subgraphsMap); | 98 | qDeleteAll(m_subgraphsMap); | ||
90 | m_subgraphsMap.clear(); | 99 | m_subgraphsMap.clear(); | ||
91 | qDeleteAll(m_nodesMap); | 100 | qDeleteAll(m_nodesMap); | ||
92 | m_nodesMap.clear(); | 101 | m_nodesMap.clear(); | ||
93 | qDeleteAll(m_edgesMap); | 102 | qDeleteAll(m_edgesMap); | ||
94 | m_edgesMap.clear(); | 103 | m_edgesMap.clear(); | ||
95 | } | 104 | } | ||
96 | 105 | | |||
97 | QString DotGraph::chooseLayoutProgramForFile(const QString& str) | 106 | QString DotGraph::chooseLayoutProgramForFile(const QString &str) | ||
98 | { | 107 | { | ||
99 | QFile iFILE(str); | 108 | QFile iFILE(str); | ||
100 | 109 | | |||
101 | if (!iFILE.open(QIODevice::ReadOnly)) | 110 | if (!iFILE.open(QIODevice::ReadOnly)) { | ||
102 | { | | |||
103 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Can't test dot file. Will try to use the dot command on the file: '" << str << "'" << endl; | 111 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Can't test dot file. Will try to use the dot command on the file: '" << str << "'" << endl; | ||
104 | return "dot";// -Txdot"; | 112 | return "dot"; // -Txdot"; | ||
105 | } | 113 | } | ||
106 | 114 | | |||
107 | QByteArray fileContent = iFILE.readAll(); | 115 | QByteArray fileContent = iFILE.readAll(); | ||
108 | if (fileContent.isEmpty()) return ""; | 116 | if (fileContent.isEmpty()) | ||
117 | return ""; | ||||
109 | std::string s = fileContent.data(); | 118 | std::string s = fileContent.data(); | ||
110 | std::string cmd = "dot"; | 119 | std::string cmd = "dot"; | ||
111 | parse(s.c_str(), | 120 | parse(s.c_str(), (!(keyword_p("strict")) >> (keyword_p("graph")[assign_a(cmd, "neato")])), (space_p | comment_p("/*", "*/"))); | ||
112 | ( | | |||
113 | !(keyword_p("strict")) >> (keyword_p("graph")[assign_a(cmd,"neato")]) | | |||
114 | ), (space_p|comment_p("/*", "*/")) ); | | |||
115 | 121 | | |||
116 | return QString::fromStdString(cmd);// + " -Txdot" ; | 122 | return QString::fromStdString(cmd); // + " -Txdot" ; | ||
117 | } | 123 | } | ||
118 | 124 | | |||
119 | bool DotGraph::parseDot(const QString& str) | 125 | bool DotGraph::parseDot(const QString &str) | ||
120 | { | 126 | { | ||
121 | qCDebug(KGRAPHVIEWERLIB_LOG) << str; | 127 | qCDebug(KGRAPHVIEWERLIB_LOG) << str; | ||
122 | m_useLibrary = false; | 128 | m_useLibrary = false; | ||
123 | if (m_layoutCommand.isEmpty()) | 129 | if (m_layoutCommand.isEmpty()) { | ||
124 | { | | |||
125 | m_layoutCommand = chooseLayoutProgramForFile(str); | 130 | m_layoutCommand = chooseLayoutProgramForFile(str); | ||
126 | if (m_layoutCommand.isEmpty()) | 131 | if (m_layoutCommand.isEmpty()) { | ||
127 | { | | |||
128 | m_layoutCommand = chooseLayoutProgramForFile(str); | 132 | m_layoutCommand = chooseLayoutProgramForFile(str); | ||
129 | return false; | 133 | return false; | ||
130 | } | 134 | } | ||
131 | } | 135 | } | ||
132 | 136 | | |||
133 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Running " << m_layoutCommand << str; | 137 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Running " << m_layoutCommand << str; | ||
134 | QStringList options; | 138 | QStringList options; | ||
135 | /// @TODO handle the non-dot commands that could don't know the -T option | 139 | /// @TODO handle the non-dot commands that could don't know the -T option | ||
136 | // if (m_readWrite && m_phase == Initial) | 140 | // if (m_readWrite && m_phase == Initial) | ||
137 | // { | 141 | // { | ||
138 | // options << "-Tdot"; | 142 | // options << "-Tdot"; | ||
139 | // } | 143 | // } | ||
140 | // else | 144 | // else | ||
141 | // { | 145 | // { | ||
142 | options << "-Txdot"; | 146 | options << "-Txdot"; | ||
143 | // } | 147 | // } | ||
144 | options << str; | 148 | options << str; | ||
145 | 149 | | |||
146 | qCDebug(KGRAPHVIEWERLIB_LOG) << "m_dot is " << m_dot << ". Acquiring mutex"; | 150 | qCDebug(KGRAPHVIEWERLIB_LOG) << "m_dot is " << m_dot << ". Acquiring mutex"; | ||
147 | QMutexLocker locker(&m_dotProcessMutex); | 151 | QMutexLocker locker(&m_dotProcessMutex); | ||
148 | qCDebug(KGRAPHVIEWERLIB_LOG) << "mutex acquired "; | 152 | qCDebug(KGRAPHVIEWERLIB_LOG) << "mutex acquired "; | ||
149 | if (m_dot) | 153 | if (m_dot) { | ||
150 | { | | |||
151 | disconnect(m_dot, nullptr, this, nullptr); | 154 | disconnect(m_dot, nullptr, this, nullptr); | ||
152 | m_dot->kill(); | 155 | m_dot->kill(); | ||
153 | delete m_dot; | 156 | delete m_dot; | ||
154 | } | 157 | } | ||
155 | m_dot = new QProcess(); | 158 | m_dot = new QProcess(); | ||
156 | connect(m_dot, static_cast<void(QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished), | 159 | connect(m_dot, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, &DotGraph::slotDotRunningDone); | ||
157 | this, &DotGraph::slotDotRunningDone); | 160 | connect(m_dot, static_cast<void (QProcess::*)(QProcess::ProcessError)>(&QProcess::error), this, &DotGraph::slotDotRunningError); | ||
158 | connect(m_dot, static_cast<void(QProcess::*)(QProcess::ProcessError)>(&QProcess::error), | | |||
159 | this, &DotGraph::slotDotRunningError); | | |||
160 | m_dot->start(m_layoutCommand, options); | 161 | m_dot->start(m_layoutCommand, options); | ||
161 | qCDebug(KGRAPHVIEWERLIB_LOG) << "process started"; | 162 | qCDebug(KGRAPHVIEWERLIB_LOG) << "process started"; | ||
162 | return true; | 163 | return true; | ||
163 | } | 164 | } | ||
164 | 165 | | |||
165 | bool DotGraph::update() | 166 | bool DotGraph::update() | ||
166 | { | 167 | { | ||
167 | GraphExporter exporter; | 168 | GraphExporter exporter; | ||
168 | if (!m_useLibrary) | 169 | if (!m_useLibrary) { | ||
169 | { | | |||
170 | qCDebug(KGRAPHVIEWERLIB_LOG) << "command"; | 170 | qCDebug(KGRAPHVIEWERLIB_LOG) << "command"; | ||
171 | QString str = exporter.writeDot(this); | 171 | QString str = exporter.writeDot(this); | ||
172 | return parseDot(str); | 172 | return parseDot(str); | ||
173 | } | 173 | } else { | ||
174 | else | | |||
175 | { | | |||
176 | qCDebug(KGRAPHVIEWERLIB_LOG) << "library"; | 174 | qCDebug(KGRAPHVIEWERLIB_LOG) << "library"; | ||
177 | graph_t* graph = exporter.exportToGraphviz(this); | 175 | graph_t *graph = exporter.exportToGraphviz(this); | ||
178 | 176 | | |||
179 | GVC_t* gvc = gvContext(); | 177 | GVC_t *gvc = gvContext(); | ||
180 | threadsafe_wrap_gvLayout(gvc, graph, m_layoutCommand.toUtf8().data()); | 178 | threadsafe_wrap_gvLayout(gvc, graph, m_layoutCommand.toUtf8().data()); | ||
181 | threadsafe_wrap_gvRender(gvc, graph, "xdot", nullptr); | 179 | threadsafe_wrap_gvRender(gvc, graph, "xdot", nullptr); | ||
182 | 180 | | |||
183 | updateWithGraph(graph); | 181 | updateWithGraph(graph); | ||
184 | 182 | | |||
185 | gvFreeLayout(gvc, graph); | 183 | gvFreeLayout(gvc, graph); | ||
186 | agclose(graph); | 184 | agclose(graph); | ||
187 | bool result = true; //(gvFreeContext(gvc) == 0); | 185 | bool result = true; //(gvFreeContext(gvc) == 0); | ||
188 | return result; | 186 | return result; | ||
189 | } | 187 | } | ||
190 | } | 188 | } | ||
191 | 189 | | |||
192 | QByteArray DotGraph::getDotResult(int , QProcess::ExitStatus ) | 190 | QByteArray DotGraph::getDotResult(int, QProcess::ExitStatus) | ||
193 | { | 191 | { | ||
194 | qCDebug(KGRAPHVIEWERLIB_LOG); | 192 | qCDebug(KGRAPHVIEWERLIB_LOG); | ||
195 | 193 | | |||
196 | QMutexLocker locker(&m_dotProcessMutex); | 194 | QMutexLocker locker(&m_dotProcessMutex); | ||
197 | if (m_dot == nullptr) | 195 | if (m_dot == nullptr) { | ||
198 | { | | |||
199 | return QByteArray(); | 196 | return QByteArray(); | ||
200 | } | 197 | } | ||
201 | QByteArray result = m_dot->readAll(); | 198 | QByteArray result = m_dot->readAll(); | ||
202 | delete m_dot; | 199 | delete m_dot; | ||
203 | m_dot = nullptr; | 200 | m_dot = nullptr; | ||
204 | return result; | 201 | return result; | ||
205 | } | 202 | } | ||
206 | 203 | | |||
207 | void DotGraph::slotDotRunningDone(int exitCode, QProcess::ExitStatus exitStatus) | 204 | void DotGraph::slotDotRunningDone(int exitCode, QProcess::ExitStatus exitStatus) | ||
208 | { | 205 | { | ||
209 | qCDebug(KGRAPHVIEWERLIB_LOG); | 206 | qCDebug(KGRAPHVIEWERLIB_LOG); | ||
210 | 207 | | |||
211 | QByteArray result = getDotResult(exitCode, exitStatus); | 208 | QByteArray result = getDotResult(exitCode, exitStatus); | ||
212 | result.replace("\\\n",""); | 209 | result.replace("\\\n", ""); | ||
213 | 210 | | |||
214 | qCDebug(KGRAPHVIEWERLIB_LOG) << "string content is:" << endl << result << endl << "=====================" << result.size(); | 211 | qCDebug(KGRAPHVIEWERLIB_LOG) << "string content is:" << endl << result << endl << "=====================" << result.size(); | ||
215 | std::string s = result.data(); | 212 | std::string s = result.data(); | ||
216 | // std::cerr << "stdstring content is:" << std::endl << s << std::endl << "===================== " << s.size() << std::endl; | 213 | // std::cerr << "stdstring content is:" << std::endl << s << std::endl << "===================== " << s.size() << std::endl; | ||
217 | if (phelper) | 214 | if (phelper) { | ||
218 | { | | |||
219 | phelper->graph = nullptr; | 215 | phelper->graph = nullptr; | ||
220 | delete phelper; | 216 | delete phelper; | ||
221 | } | 217 | } | ||
222 | // if (parsingResult) | 218 | // if (parsingResult) | ||
223 | // { | 219 | // { | ||
224 | // if (m_readWrite) | 220 | // if (m_readWrite) | ||
225 | // { | 221 | // { | ||
226 | // storeOriginalAttributes(); | 222 | // storeOriginalAttributes(); | ||
Show All 10 Lines | |||||
237 | phelper->uniq = 0; | 233 | phelper->uniq = 0; | ||
238 | 234 | | |||
239 | qCDebug(KGRAPHVIEWERLIB_LOG) << "parsing new dot"; | 235 | qCDebug(KGRAPHVIEWERLIB_LOG) << "parsing new dot"; | ||
240 | bool parsingResult = parse(s); | 236 | bool parsingResult = parse(s); | ||
241 | delete phelper; | 237 | delete phelper; | ||
242 | phelper = nullptr; | 238 | phelper = nullptr; | ||
243 | qCDebug(KGRAPHVIEWERLIB_LOG) << "phelper deleted"; | 239 | qCDebug(KGRAPHVIEWERLIB_LOG) << "phelper deleted"; | ||
244 | 240 | | |||
245 | if (parsingResult) | 241 | if (parsingResult) { | ||
246 | { | | |||
247 | qCDebug(KGRAPHVIEWERLIB_LOG) << "calling updateWithGraph"; | 242 | qCDebug(KGRAPHVIEWERLIB_LOG) << "calling updateWithGraph"; | ||
248 | updateWithGraph(newGraph); | 243 | updateWithGraph(newGraph); | ||
249 | } | 244 | } else { | ||
250 | else | | |||
251 | { | | |||
252 | qCWarning(KGRAPHVIEWERLIB_LOG) << "parsing failed"; | 245 | qCWarning(KGRAPHVIEWERLIB_LOG) << "parsing failed"; | ||
253 | } | 246 | } | ||
254 | // return parsingResult; | 247 | // return parsingResult; | ||
255 | // if (m_readWrite && m_phase == Initial) | 248 | // if (m_readWrite && m_phase == Initial) | ||
256 | // { | 249 | // { | ||
257 | // m_phase = Final; | 250 | // m_phase = Final; | ||
258 | // update(); | 251 | // update(); | ||
259 | // } | 252 | // } | ||
260 | // else | 253 | // else | ||
261 | // { | 254 | // { | ||
262 | qCDebug(KGRAPHVIEWERLIB_LOG) << "emiting readyToDisplay"; | 255 | qCDebug(KGRAPHVIEWERLIB_LOG) << "emiting readyToDisplay"; | ||
263 | emit(readyToDisplay()); | 256 | emit(readyToDisplay()); | ||
264 | // } | 257 | // } | ||
265 | } | 258 | } | ||
266 | 259 | | |||
267 | void DotGraph::slotDotRunningError(QProcess::ProcessError error) | 260 | void DotGraph::slotDotRunningError(QProcess::ProcessError error) | ||
268 | { | 261 | { | ||
269 | qCWarning(KGRAPHVIEWERLIB_LOG) << "DotGraph::slotDotRunningError" << error; | 262 | qCWarning(KGRAPHVIEWERLIB_LOG) << "DotGraph::slotDotRunningError" << error; | ||
270 | switch (error) | 263 | switch (error) { | ||
271 | { | | |||
272 | case QProcess::FailedToStart: | 264 | case QProcess::FailedToStart: | ||
273 | QMessageBox::critical(nullptr, i18n("Layout process failed"), i18n("Unable to start %1.", m_layoutCommand)); | 265 | QMessageBox::critical(nullptr, i18n("Layout process failed"), i18n("Unable to start %1.", m_layoutCommand)); | ||
274 | break; | 266 | break; | ||
275 | case QProcess::Crashed: | 267 | case QProcess::Crashed: | ||
276 | QMessageBox::critical(nullptr, i18n("Layout process failed"), i18n("%1 crashed.", m_layoutCommand)); | 268 | QMessageBox::critical(nullptr, i18n("Layout process failed"), i18n("%1 crashed.", m_layoutCommand)); | ||
277 | break; | 269 | break; | ||
278 | case QProcess::Timedout: | 270 | case QProcess::Timedout: | ||
279 | QMessageBox::critical(nullptr, i18n("Layout process failed"), i18n("%1 timed out.", m_layoutCommand)); | 271 | QMessageBox::critical(nullptr, i18n("Layout process failed"), i18n("%1 timed out.", m_layoutCommand)); | ||
280 | break; | 272 | break; | ||
281 | case QProcess::WriteError: | 273 | case QProcess::WriteError: | ||
282 | QMessageBox::critical(nullptr, i18n("Layout process failed"), i18n("Was not able to write data to the %1 process.", m_layoutCommand)); | 274 | QMessageBox::critical(nullptr, i18n("Layout process failed"), i18n("Was not able to write data to the %1 process.", m_layoutCommand)); | ||
283 | break; | 275 | break; | ||
284 | case QProcess::ReadError: | 276 | case QProcess::ReadError: | ||
285 | QMessageBox::critical(nullptr, i18n("Layout process failed"), i18n("Was not able to read data from the %1 process.", m_layoutCommand)); | 277 | QMessageBox::critical(nullptr, i18n("Layout process failed"), i18n("Was not able to read data from the %1 process.", m_layoutCommand)); | ||
286 | break; | 278 | break; | ||
287 | default: | 279 | default: | ||
288 | QMessageBox::critical(nullptr, i18n("Layout process failed"), i18n("Unknown error running %1.", m_layoutCommand)); | 280 | QMessageBox::critical(nullptr, i18n("Layout process failed"), i18n("Unknown error running %1.", m_layoutCommand)); | ||
289 | } | 281 | } | ||
290 | } | 282 | } | ||
291 | 283 | | |||
292 | unsigned int DotGraph::cellNumber(int x, int y) | 284 | unsigned int DotGraph::cellNumber(int x, int y) | ||
293 | { | 285 | { | ||
294 | /* qCDebug(KGRAPHVIEWERLIB_LOG) << "x= " << x << ", y= " << y << ", m_width= " << m_width << ", m_height= " << m_height << ", m_horizCellFactor= " << m_horizCellFactor << ", m_vertCellFactor= " << m_vertCellFactor << ", m_wdhcf= " << m_wdhcf << ", m_hdvcf= " << m_hdvcf;*/ | 286 | /* qCDebug(KGRAPHVIEWERLIB_LOG) << "x= " << x << ", y= " << y << ", m_width= " << m_width << ", m_height= " << m_height << ", m_horizCellFactor= " << m_horizCellFactor << ", m_vertCellFactor= " << m_vertCellFactor << ", m_wdhcf= " << | ||
287 | * m_wdhcf << ", m_hdvcf= " << m_hdvcf;*/ | ||||
295 | 288 | | |||
296 | unsigned int nx = (unsigned int)(( x - ( x % int(m_wdhcf) ) ) / m_wdhcf); | 289 | unsigned int nx = (unsigned int)((x - (x % int(m_wdhcf))) / m_wdhcf); | ||
297 | unsigned int ny = (unsigned int)(( y - ( y % int(m_hdvcf) ) ) / m_hdvcf); | 290 | unsigned int ny = (unsigned int)((y - (y % int(m_hdvcf))) / m_hdvcf); | ||
298 | /* qCDebug(KGRAPHVIEWERLIB_LOG) << "nx = " << (unsigned int)(( x - ( x % int(m_wdhcf) ) ) / m_wdhcf); | 291 | /* qCDebug(KGRAPHVIEWERLIB_LOG) << "nx = " << (unsigned int)(( x - ( x % int(m_wdhcf) ) ) / m_wdhcf); | ||
299 | qCDebug(KGRAPHVIEWERLIB_LOG) << "ny = " << (unsigned int)(( y - ( y % int(m_hdvcf) ) ) / m_hdvcf); | 292 | qCDebug(KGRAPHVIEWERLIB_LOG) << "ny = " << (unsigned int)(( y - ( y % int(m_hdvcf) ) ) / m_hdvcf); | ||
300 | qCDebug(KGRAPHVIEWERLIB_LOG) << "res = " << ny * m_horizCellFactor + nx;*/ | 293 | qCDebug(KGRAPHVIEWERLIB_LOG) << "res = " << ny * m_horizCellFactor + nx;*/ | ||
301 | 294 | | |||
302 | unsigned int res = ny * m_horizCellFactor + nx; | 295 | unsigned int res = ny * m_horizCellFactor + nx; | ||
303 | return res; | 296 | return res; | ||
304 | } | 297 | } | ||
305 | 298 | | |||
306 | #define MAXCELLWEIGHT 800 | 299 | #define MAXCELLWEIGHT 800 | ||
307 | 300 | | |||
308 | void DotGraph::computeCells() | 301 | void DotGraph::computeCells() | ||
309 | { | 302 | { | ||
310 | return; | 303 | return; | ||
311 | qCDebug(KGRAPHVIEWERLIB_LOG) << m_width << m_height << endl; | 304 | qCDebug(KGRAPHVIEWERLIB_LOG) << m_width << m_height << endl; | ||
312 | m_horizCellFactor = m_vertCellFactor = 1; | 305 | m_horizCellFactor = m_vertCellFactor = 1; | ||
313 | m_wdhcf = (int)ceil(((double)m_width) / m_horizCellFactor)+1; | 306 | m_wdhcf = (int)ceil(((double)m_width) / m_horizCellFactor) + 1; | ||
314 | m_hdvcf = (int)ceil(((double)m_height) / m_vertCellFactor)+1; | 307 | m_hdvcf = (int)ceil(((double)m_height) / m_vertCellFactor) + 1; | ||
315 | bool stop = true; | 308 | bool stop = true; | ||
316 | do | 309 | do { | ||
317 | { | | |||
318 | stop = true; | 310 | stop = true; | ||
319 | m_cells.clear(); | 311 | m_cells.clear(); | ||
320 | // m_cells.resize(m_horizCellFactor * m_vertCellFactor); | 312 | // m_cells.resize(m_horizCellFactor * m_vertCellFactor); | ||
321 | 313 | | |||
322 | GraphNodeMap::iterator it, it_end; | 314 | GraphNodeMap::iterator it, it_end; | ||
323 | it = m_nodesMap.begin(); it_end = m_nodesMap.end(); | 315 | it = m_nodesMap.begin(); | ||
324 | for (; it != it_end; it++) | 316 | it_end = m_nodesMap.end(); | ||
325 | { | 317 | for (; it != it_end; it++) { | ||
326 | GraphNode* gn = *it; | 318 | GraphNode *gn = *it; | ||
327 | // int cellNum = cellNumber(int(gn->x()), int(gn->y())); | 319 | // int cellNum = cellNumber(int(gn->x()), int(gn->y())); | ||
328 | int cellNum = cellNumber(0,0); | 320 | int cellNum = cellNumber(0, 0); | ||
329 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Found cell number " << cellNum; | 321 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Found cell number " << cellNum; | ||
330 | 322 | | |||
331 | if (m_cells.size() <= cellNum) | 323 | if (m_cells.size() <= cellNum) { | ||
332 | { | | |||
333 | m_cells.resize(cellNum+1); | 324 | m_cells.resize(cellNum + 1); | ||
334 | } | 325 | } | ||
335 | m_cells[cellNum].insert(gn); | 326 | m_cells[cellNum].insert(gn); | ||
336 | 327 | | |||
337 | qCDebug(KGRAPHVIEWERLIB_LOG) << "after insert"; | 328 | qCDebug(KGRAPHVIEWERLIB_LOG) << "after insert"; | ||
338 | if ( m_cells[cellNum].size() > MAXCELLWEIGHT ) | 329 | if (m_cells[cellNum].size() > MAXCELLWEIGHT) { | ||
339 | { | | |||
340 | qCDebug(KGRAPHVIEWERLIB_LOG) << "cell number " << cellNum << " contains " << m_cells[cellNum].size() << " nodes"; | 330 | qCDebug(KGRAPHVIEWERLIB_LOG) << "cell number " << cellNum << " contains " << m_cells[cellNum].size() << " nodes"; | ||
341 | if ((m_width/m_horizCellFactor) > (m_height/m_vertCellFactor)) | 331 | if ((m_width / m_horizCellFactor) > (m_height / m_vertCellFactor)) { | ||
342 | { | | |||
343 | m_horizCellFactor++; | 332 | m_horizCellFactor++; | ||
344 | m_wdhcf = m_width / m_horizCellFactor; | 333 | m_wdhcf = m_width / m_horizCellFactor; | ||
345 | } | 334 | } else { | ||
346 | else | | |||
347 | { | | |||
348 | m_vertCellFactor++; | 335 | m_vertCellFactor++; | ||
349 | m_hdvcf = m_height / m_vertCellFactor; | 336 | m_hdvcf = m_height / m_vertCellFactor; | ||
350 | } | 337 | } | ||
351 | qCDebug(KGRAPHVIEWERLIB_LOG) << "cell factor is now " << m_horizCellFactor << " / " << m_vertCellFactor; | 338 | qCDebug(KGRAPHVIEWERLIB_LOG) << "cell factor is now " << m_horizCellFactor << " / " << m_vertCellFactor; | ||
352 | stop = false; | 339 | stop = false; | ||
353 | break; | 340 | break; | ||
354 | } | 341 | } | ||
355 | } | 342 | } | ||
356 | } while (!stop); | 343 | } while (!stop); | ||
357 | qCDebug(KGRAPHVIEWERLIB_LOG) << "m_wdhcf=" << m_wdhcf << "; m_hdvcf=" << m_hdvcf << endl; | 344 | qCDebug(KGRAPHVIEWERLIB_LOG) << "m_wdhcf=" << m_wdhcf << "; m_hdvcf=" << m_hdvcf << endl; | ||
358 | qCDebug(KGRAPHVIEWERLIB_LOG) << "finished" << endl; | 345 | qCDebug(KGRAPHVIEWERLIB_LOG) << "finished" << endl; | ||
359 | } | 346 | } | ||
360 | 347 | | |||
361 | QSet< GraphNode* >& DotGraph::nodesOfCell(unsigned int id) | 348 | QSet<GraphNode *> &DotGraph::nodesOfCell(unsigned int id) | ||
362 | { | 349 | { | ||
363 | return m_cells[id]; | 350 | return m_cells[id]; | ||
364 | } | 351 | } | ||
365 | 352 | | |||
366 | void DotGraph::storeOriginalAttributes() | 353 | void DotGraph::storeOriginalAttributes() | ||
367 | { | 354 | { | ||
368 | foreach (GraphNode* node, nodes()) | 355 | foreach (GraphNode *node, nodes()) { | ||
369 | { | | |||
370 | node->storeOriginalAttributes(); | 356 | node->storeOriginalAttributes(); | ||
371 | } | 357 | } | ||
372 | foreach (GraphEdge* edge, edges()) | 358 | foreach (GraphEdge *edge, edges()) { | ||
373 | { | | |||
374 | edge->storeOriginalAttributes(); | 359 | edge->storeOriginalAttributes(); | ||
375 | } | 360 | } | ||
376 | foreach (GraphSubgraph* subgraph, subgraphs()) | 361 | foreach (GraphSubgraph *subgraph, subgraphs()) { | ||
377 | { | | |||
378 | subgraph->storeOriginalAttributes(); | 362 | subgraph->storeOriginalAttributes(); | ||
379 | } | 363 | } | ||
380 | GraphElement::storeOriginalAttributes(); | 364 | GraphElement::storeOriginalAttributes(); | ||
381 | } | 365 | } | ||
382 | 366 | | |||
383 | void DotGraph::saveTo(const QString& fileName) | 367 | void DotGraph::saveTo(const QString &fileName) | ||
384 | { | 368 | { | ||
385 | qCDebug(KGRAPHVIEWERLIB_LOG) << fileName; | 369 | qCDebug(KGRAPHVIEWERLIB_LOG) << fileName; | ||
386 | m_dotFileName = fileName; | 370 | m_dotFileName = fileName; | ||
387 | GraphExporter exporter; | 371 | GraphExporter exporter; | ||
388 | exporter.writeDot(this, fileName); | 372 | exporter.writeDot(this, fileName); | ||
389 | } | 373 | } | ||
390 | 374 | | |||
391 | void DotGraph::updateWithGraph(graph_t* newGraph) | 375 | void DotGraph::updateWithGraph(graph_t *newGraph) | ||
392 | { | 376 | { | ||
393 | qCDebug(KGRAPHVIEWERLIB_LOG); | 377 | qCDebug(KGRAPHVIEWERLIB_LOG); | ||
394 | 378 | | |||
395 | // copy global graph render operations and attributes | 379 | // copy global graph render operations and attributes | ||
396 | DotRenderOpVec ops; | 380 | DotRenderOpVec ops; | ||
397 | // decrease mem peak | 381 | // decrease mem peak | ||
398 | setRenderOperations(ops); | 382 | setRenderOperations(ops); | ||
399 | 383 | | |||
400 | if (agget(newGraph, (char*)"_draw_")) | 384 | if (agget(newGraph, (char *)"_draw_")) { | ||
401 | { | | |||
402 | parse_renderop(agget(newGraph, (char*)"_draw_"), ops); | 385 | parse_renderop(agget(newGraph, (char *)"_draw_"), ops); | ||
403 | qCDebug(KGRAPHVIEWERLIB_LOG) << "_draw_: element renderOperations size is now " << ops.size(); | 386 | qCDebug(KGRAPHVIEWERLIB_LOG) << "_draw_: element renderOperations size is now " << ops.size(); | ||
404 | } | 387 | } | ||
405 | if (agget(newGraph, (char*)"_ldraw_")) | 388 | if (agget(newGraph, (char *)"_ldraw_")) { | ||
406 | { | | |||
407 | parse_renderop(agget(newGraph, (char*)"_ldraw_"), ops); | 389 | parse_renderop(agget(newGraph, (char *)"_ldraw_"), ops); | ||
408 | qCDebug(KGRAPHVIEWERLIB_LOG) << "_ldraw_: element renderOperations size is now " << ops.size(); | 390 | qCDebug(KGRAPHVIEWERLIB_LOG) << "_ldraw_: element renderOperations size is now " << ops.size(); | ||
409 | } | 391 | } | ||
410 | 392 | | |||
411 | setRenderOperations(ops); | 393 | setRenderOperations(ops); | ||
412 | 394 | | |||
413 | Agsym_t *attr = agnxtattr(newGraph, AGRAPH, nullptr); | 395 | Agsym_t *attr = agnxtattr(newGraph, AGRAPH, nullptr); | ||
414 | while(attr) | 396 | while (attr) { | ||
415 | { | | |||
416 | qCDebug(KGRAPHVIEWERLIB_LOG) << agnameof(newGraph) << ":" << attr->name << agxget(newGraph,attr); | 397 | qCDebug(KGRAPHVIEWERLIB_LOG) << agnameof(newGraph) << ":" << attr->name << agxget(newGraph, attr); | ||
417 | m_attributes[attr->name] = agxget(newGraph,attr); | 398 | m_attributes[attr->name] = agxget(newGraph, attr); | ||
418 | attr = agnxtattr(newGraph, AGRAPH, attr); | 399 | attr = agnxtattr(newGraph, AGRAPH, attr); | ||
419 | } | 400 | } | ||
420 | 401 | | |||
421 | // copy subgraphs | 402 | // copy subgraphs | ||
422 | for (graph_t* sg = agfstsubg(newGraph); sg; sg = agnxtsubg(sg)) | 403 | for (graph_t *sg = agfstsubg(newGraph); sg; sg = agnxtsubg(sg)) { | ||
423 | { | | |||
424 | qCDebug(KGRAPHVIEWERLIB_LOG) << "subgraph:" << agnameof(sg); | 404 | qCDebug(KGRAPHVIEWERLIB_LOG) << "subgraph:" << agnameof(sg); | ||
425 | if (subgraphs().contains(agnameof(sg))) | 405 | if (subgraphs().contains(agnameof(sg))) { | ||
426 | { | | |||
427 | qCDebug(KGRAPHVIEWERLIB_LOG) << "known"; | 406 | qCDebug(KGRAPHVIEWERLIB_LOG) << "known"; | ||
428 | // ??? | 407 | // ??? | ||
429 | // nodes()[ngn->name]->setZ(ngn->z()); | 408 | // nodes()[ngn->name]->setZ(ngn->z()); | ||
430 | subgraphs()[agnameof(sg)]->updateWithSubgraph(sg); | 409 | subgraphs()[agnameof(sg)]->updateWithSubgraph(sg); | ||
431 | if (subgraphs()[agnameof(sg)]->canvasElement()) | 410 | if (subgraphs()[agnameof(sg)]->canvasElement()) { | ||
432 | { | | |||
433 | // nodes()[ngn->id()]->canvasElement()->setGh(m_height); | 411 | // nodes()[ngn->id()]->canvasElement()->setGh(m_height); | ||
434 | } | 412 | } | ||
435 | } | 413 | } else { | ||
436 | else | | |||
437 | { | | |||
438 | qCDebug(KGRAPHVIEWERLIB_LOG) << "new"; | 414 | qCDebug(KGRAPHVIEWERLIB_LOG) << "new"; | ||
439 | GraphSubgraph* newsg = new GraphSubgraph(sg); | 415 | GraphSubgraph *newsg = new GraphSubgraph(sg); | ||
440 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "new created"; | 416 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "new created"; | ||
441 | subgraphs().insert(agnameof(sg), newsg); | 417 | subgraphs().insert(agnameof(sg), newsg); | ||
442 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "new inserted"; | 418 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "new inserted"; | ||
443 | } | 419 | } | ||
444 | | ||||
445 | } | 420 | } | ||
446 | 421 | | |||
447 | // copy nodes | 422 | // copy nodes | ||
448 | node_t* ngn = agfstnode(newGraph); | 423 | node_t *ngn = agfstnode(newGraph); | ||
449 | qCDebug(KGRAPHVIEWERLIB_LOG) << "first node:" << (void*)ngn; | 424 | qCDebug(KGRAPHVIEWERLIB_LOG) << "first node:" << (void *)ngn; | ||
450 | 425 | | |||
451 | while (ngn) | 426 | while (ngn) | ||
452 | // foreach (GraphNode* ngn, newGraph.nodes()) | 427 | // foreach (GraphNode* ngn, newGraph.nodes()) | ||
453 | { | 428 | { | ||
454 | qCDebug(KGRAPHVIEWERLIB_LOG) << "node " << agnameof(ngn); | 429 | qCDebug(KGRAPHVIEWERLIB_LOG) << "node " << agnameof(ngn); | ||
455 | if (nodes().contains(agnameof(ngn))) | 430 | if (nodes().contains(agnameof(ngn))) { | ||
456 | { | | |||
457 | qCDebug(KGRAPHVIEWERLIB_LOG) << "known"; | 431 | qCDebug(KGRAPHVIEWERLIB_LOG) << "known"; | ||
458 | // ??? | 432 | // ??? | ||
459 | // nodes()[ngn->name]->setZ(ngn->z()); | 433 | // nodes()[ngn->name]->setZ(ngn->z()); | ||
460 | nodes()[agnameof(ngn)]->updateWithNode(ngn); | 434 | nodes()[agnameof(ngn)]->updateWithNode(ngn); | ||
461 | if (nodes()[agnameof(ngn)]->canvasElement()) | 435 | if (nodes()[agnameof(ngn)]->canvasElement()) { | ||
462 | { | | |||
463 | // nodes()[ngn->id()]->canvasElement()->setGh(m_height); | 436 | // nodes()[ngn->id()]->canvasElement()->setGh(m_height); | ||
464 | } | 437 | } | ||
465 | } | 438 | } else { | ||
466 | else | | |||
467 | { | | |||
468 | qCDebug(KGRAPHVIEWERLIB_LOG) << "new"; | 439 | qCDebug(KGRAPHVIEWERLIB_LOG) << "new"; | ||
469 | GraphNode* newgn = new GraphNode(ngn); | 440 | GraphNode *newgn = new GraphNode(ngn); | ||
470 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "new created"; | 441 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "new created"; | ||
471 | nodes().insert(agnameof(ngn), newgn); | 442 | nodes().insert(agnameof(ngn), newgn); | ||
472 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "new inserted"; | 443 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "new inserted"; | ||
473 | } | 444 | } | ||
474 | 445 | | |||
475 | // copy node edges | 446 | // copy node edges | ||
476 | edge_t* nge = agfstout(newGraph, ngn); | 447 | edge_t *nge = agfstout(newGraph, ngn); | ||
477 | while (nge) | 448 | while (nge) { | ||
478 | { | | |||
479 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "edge " << nge->id; | 449 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "edge " << nge->id; | ||
480 | const QString edgeName = QString::fromUtf8(agnameof(aghead(nge))) + QString::fromUtf8(agnameof(agtail(nge))); | 450 | const QString edgeName = QString::fromUtf8(agnameof(aghead(nge))) + QString::fromUtf8(agnameof(agtail(nge))); | ||
481 | if (edges().contains(edgeName)) | 451 | if (edges().contains(edgeName)) { | ||
482 | { | | |||
483 | // () << "edge known" << nge->id; | 452 | // () << "edge known" << nge->id; | ||
484 | // edges()[nge->name]->setZ(nge->z()); | 453 | // edges()[nge->name]->setZ(nge->z()); | ||
485 | edges()[edgeName]->updateWithEdge(nge); | 454 | edges()[edgeName]->updateWithEdge(nge); | ||
486 | if (edges()[edgeName]->canvasEdge()) | 455 | if (edges()[edgeName]->canvasEdge()) { | ||
487 | { | | |||
488 | // edges()[nge->id()]->canvasEdge()->setGh(m_height); | 456 | // edges()[nge->id()]->canvasEdge()->setGh(m_height); | ||
489 | } | 457 | } | ||
490 | } | 458 | } else { | ||
491 | else | | |||
492 | { | | |||
493 | qCDebug(KGRAPHVIEWERLIB_LOG) << "new edge" << edgeName; | 459 | qCDebug(KGRAPHVIEWERLIB_LOG) << "new edge" << edgeName; | ||
494 | { | 460 | { | ||
495 | GraphEdge* newEdge = new GraphEdge(); | 461 | GraphEdge *newEdge = new GraphEdge(); | ||
496 | newEdge->setId(edgeName); | 462 | newEdge->setId(edgeName); | ||
497 | newEdge->updateWithEdge(nge); | 463 | newEdge->updateWithEdge(nge); | ||
498 | if (elementNamed(agnameof(agtail(nge))) == nullptr) | 464 | if (elementNamed(agnameof(agtail(nge))) == nullptr) { | ||
499 | { | | |||
500 | GraphNode* newgn = new GraphNode(); | 465 | GraphNode *newgn = new GraphNode(); | ||
501 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "new created"; | 466 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "new created"; | ||
502 | nodes().insert(agnameof(agtail(nge)), newgn); | 467 | nodes().insert(agnameof(agtail(nge)), newgn); | ||
503 | } | 468 | } | ||
504 | newEdge->setFromNode(elementNamed(agnameof(agtail(nge)))); | 469 | newEdge->setFromNode(elementNamed(agnameof(agtail(nge)))); | ||
505 | if (elementNamed(agnameof(aghead(nge))) == nullptr) | 470 | if (elementNamed(agnameof(aghead(nge))) == nullptr) { | ||
506 | { | | |||
507 | GraphNode* newgn = new GraphNode(); | 471 | GraphNode *newgn = new GraphNode(); | ||
508 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "new created"; | 472 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "new created"; | ||
509 | nodes().insert(agnameof(aghead(nge)), newgn); | 473 | nodes().insert(agnameof(aghead(nge)), newgn); | ||
510 | } | 474 | } | ||
511 | newEdge->setToNode(elementNamed(agnameof(aghead(nge)))); | 475 | newEdge->setToNode(elementNamed(agnameof(aghead(nge)))); | ||
512 | edges().insert(edgeName, newEdge); | 476 | edges().insert(edgeName, newEdge); | ||
513 | } | 477 | } | ||
514 | } | 478 | } | ||
515 | nge = agnxtedge(newGraph, nge, ngn); | 479 | nge = agnxtedge(newGraph, nge, ngn); | ||
516 | } | 480 | } | ||
517 | ngn = agnxtnode(newGraph, ngn); | 481 | ngn = agnxtnode(newGraph, ngn); | ||
518 | } | 482 | } | ||
519 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Done"; | 483 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Done"; | ||
520 | emit readyToDisplay(); | 484 | emit readyToDisplay(); | ||
521 | computeCells(); | 485 | computeCells(); | ||
522 | } | 486 | } | ||
523 | 487 | | |||
524 | void DotGraph::updateWithGraph(const DotGraph& newGraph) | 488 | void DotGraph::updateWithGraph(const DotGraph &newGraph) | ||
525 | { | 489 | { | ||
526 | GraphElement::updateWithElement(newGraph); | 490 | GraphElement::updateWithElement(newGraph); | ||
527 | m_width=newGraph.width(); | 491 | m_width = newGraph.width(); | ||
528 | m_height=newGraph.height(); | 492 | m_height = newGraph.height(); | ||
529 | m_scale=newGraph.scale(); | 493 | m_scale = newGraph.scale(); | ||
530 | m_directed=newGraph.directed(); | 494 | m_directed = newGraph.directed(); | ||
531 | m_strict=newGraph.strict(); | 495 | m_strict = newGraph.strict(); | ||
532 | computeCells(); | 496 | computeCells(); | ||
533 | foreach (GraphSubgraph* nsg, newGraph.subgraphs()) | 497 | foreach (GraphSubgraph *nsg, newGraph.subgraphs()) { | ||
534 | { | | |||
535 | qCDebug(KGRAPHVIEWERLIB_LOG) << "subgraph" << nsg->id(); | 498 | qCDebug(KGRAPHVIEWERLIB_LOG) << "subgraph" << nsg->id(); | ||
536 | if (subgraphs().contains(nsg->id())) | 499 | if (subgraphs().contains(nsg->id())) { | ||
537 | { | | |||
538 | qCDebug(KGRAPHVIEWERLIB_LOG) << "subgraph known" << nsg->id(); | 500 | qCDebug(KGRAPHVIEWERLIB_LOG) << "subgraph known" << nsg->id(); | ||
539 | subgraphs().value(nsg->id())->updateWithSubgraph(*nsg); | 501 | subgraphs().value(nsg->id())->updateWithSubgraph(*nsg); | ||
540 | if (subgraphs().value(nsg->id())->canvasElement()) | 502 | if (subgraphs().value(nsg->id())->canvasElement()) { | ||
541 | { | | |||
542 | // subgraphs().value(nsg->id())->canvasElement()->setGh(m_height); | 503 | // subgraphs().value(nsg->id())->canvasElement()->setGh(m_height); | ||
543 | } | 504 | } | ||
544 | } | 505 | } else { | ||
545 | else | | |||
546 | { | | |||
547 | qCDebug(KGRAPHVIEWERLIB_LOG) << "new subgraph" << nsg->id(); | 506 | qCDebug(KGRAPHVIEWERLIB_LOG) << "new subgraph" << nsg->id(); | ||
548 | GraphSubgraph* newSubgraph = new GraphSubgraph(); | 507 | GraphSubgraph *newSubgraph = new GraphSubgraph(); | ||
549 | newSubgraph->updateWithSubgraph(*nsg); | 508 | newSubgraph->updateWithSubgraph(*nsg); | ||
550 | newSubgraph->setZ(0); | 509 | newSubgraph->setZ(0); | ||
551 | subgraphs().insert(nsg->id(), newSubgraph); | 510 | subgraphs().insert(nsg->id(), newSubgraph); | ||
552 | } | 511 | } | ||
553 | } | 512 | } | ||
554 | foreach (GraphNode* ngn, newGraph.nodes()) | 513 | foreach (GraphNode *ngn, newGraph.nodes()) { | ||
555 | { | | |||
556 | qCDebug(KGRAPHVIEWERLIB_LOG) << "node " << ngn->id(); | 514 | qCDebug(KGRAPHVIEWERLIB_LOG) << "node " << ngn->id(); | ||
557 | if (nodes().contains(ngn->id())) | 515 | if (nodes().contains(ngn->id())) { | ||
558 | { | | |||
559 | qCDebug(KGRAPHVIEWERLIB_LOG) << "known"; | 516 | qCDebug(KGRAPHVIEWERLIB_LOG) << "known"; | ||
560 | nodes()[ngn->id()]->setZ(ngn->z()); | 517 | nodes()[ngn->id()]->setZ(ngn->z()); | ||
561 | nodes()[ngn->id()]->updateWithNode(*ngn); | 518 | nodes()[ngn->id()]->updateWithNode(*ngn); | ||
562 | if (nodes()[ngn->id()]->canvasElement()) | 519 | if (nodes()[ngn->id()]->canvasElement()) { | ||
563 | { | | |||
564 | // nodes()[ngn->id()]->canvasElement()->setGh(m_height); | 520 | // nodes()[ngn->id()]->canvasElement()->setGh(m_height); | ||
565 | } | 521 | } | ||
566 | } | 522 | } else { | ||
567 | else | | |||
568 | { | | |||
569 | qCDebug(KGRAPHVIEWERLIB_LOG) << "new"; | 523 | qCDebug(KGRAPHVIEWERLIB_LOG) << "new"; | ||
570 | GraphNode* newgn = new GraphNode(*ngn); | 524 | GraphNode *newgn = new GraphNode(*ngn); | ||
571 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "new created"; | 525 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "new created"; | ||
572 | nodes().insert(ngn->id(), newgn); | 526 | nodes().insert(ngn->id(), newgn); | ||
573 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "new inserted"; | 527 | // qCDebug(KGRAPHVIEWERLIB_LOG) << "new inserted"; | ||
574 | } | 528 | } | ||
575 | } | 529 | } | ||
576 | foreach (GraphEdge* nge, newGraph.edges()) | 530 | foreach (GraphEdge *nge, newGraph.edges()) { | ||
577 | { | | |||
578 | qCDebug(KGRAPHVIEWERLIB_LOG) << "edge " << nge->id(); | 531 | qCDebug(KGRAPHVIEWERLIB_LOG) << "edge " << nge->id(); | ||
579 | if (edges().contains(nge->id())) | 532 | if (edges().contains(nge->id())) { | ||
580 | { | | |||
581 | qCDebug(KGRAPHVIEWERLIB_LOG) << "edge known" << nge->id(); | 533 | qCDebug(KGRAPHVIEWERLIB_LOG) << "edge known" << nge->id(); | ||
582 | edges()[nge->id()]->setZ(nge->z()); | 534 | edges()[nge->id()]->setZ(nge->z()); | ||
583 | edges()[nge->id()]->updateWithEdge(*nge); | 535 | edges()[nge->id()]->updateWithEdge(*nge); | ||
584 | if (edges()[nge->id()]->canvasEdge()) | 536 | if (edges()[nge->id()]->canvasEdge()) { | ||
585 | { | | |||
586 | // edges()[nge->id()]->canvasEdge()->setGh(m_height); | 537 | // edges()[nge->id()]->canvasEdge()->setGh(m_height); | ||
587 | } | 538 | } | ||
588 | } | 539 | } else { | ||
589 | else | | |||
590 | { | | |||
591 | qCDebug(KGRAPHVIEWERLIB_LOG) << "new edge" << nge->id(); | 540 | qCDebug(KGRAPHVIEWERLIB_LOG) << "new edge" << nge->id(); | ||
592 | { | 541 | { | ||
593 | GraphEdge* newEdge = new GraphEdge(); | 542 | GraphEdge *newEdge = new GraphEdge(); | ||
594 | newEdge->setId(nge->id()); | 543 | newEdge->setId(nge->id()); | ||
595 | newEdge->updateWithEdge(*nge); | 544 | newEdge->updateWithEdge(*nge); | ||
596 | newEdge->setFromNode(elementNamed(nge->fromNode()->id())); | 545 | newEdge->setFromNode(elementNamed(nge->fromNode()->id())); | ||
597 | newEdge->setToNode(elementNamed(nge->toNode()->id())); | 546 | newEdge->setToNode(elementNamed(nge->toNode()->id())); | ||
598 | edges().insert(nge->id(), newEdge); | 547 | edges().insert(nge->id(), newEdge); | ||
599 | } | 548 | } | ||
600 | } | 549 | } | ||
601 | } | 550 | } | ||
602 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Done"; | 551 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Done"; | ||
603 | computeCells(); | 552 | computeCells(); | ||
604 | } | 553 | } | ||
605 | 554 | | |||
606 | void DotGraph::removeNodeNamed(const QString& nodeName) | 555 | void DotGraph::removeNodeNamed(const QString &nodeName) | ||
607 | { | 556 | { | ||
608 | qCDebug(KGRAPHVIEWERLIB_LOG) << nodeName; | 557 | qCDebug(KGRAPHVIEWERLIB_LOG) << nodeName; | ||
609 | GraphNode* node = dynamic_cast<GraphNode*>(elementNamed(nodeName)); | 558 | GraphNode *node = dynamic_cast<GraphNode *>(elementNamed(nodeName)); | ||
610 | if (node == nullptr) | 559 | if (node == nullptr) { | ||
611 | { | | |||
612 | qCWarning(KGRAPHVIEWERLIB_LOG) << "No such node " << nodeName; | 560 | qCWarning(KGRAPHVIEWERLIB_LOG) << "No such node " << nodeName; | ||
613 | return; | 561 | return; | ||
614 | } | 562 | } | ||
615 | 563 | | |||
616 | GraphEdgeMap::iterator it, it_end; | 564 | GraphEdgeMap::iterator it, it_end; | ||
617 | it = m_edgesMap.begin(); it_end = m_edgesMap.end(); | 565 | it = m_edgesMap.begin(); | ||
618 | while (it != it_end) | 566 | it_end = m_edgesMap.end(); | ||
619 | { | 567 | while (it != it_end) { | ||
620 | if ( it.value()->fromNode() == node | 568 | if (it.value()->fromNode() == node || it.value()->toNode() == node) { | ||
621 | || it.value()->toNode() == node ) | | |||
622 | { | | |||
623 | GraphEdge* edge = it.value(); | 569 | GraphEdge *edge = it.value(); | ||
624 | if (edge->canvasEdge()) | 570 | if (edge->canvasEdge()) { | ||
625 | { | | |||
626 | edge->canvasEdge()->hide(); | 571 | edge->canvasEdge()->hide(); | ||
627 | delete edge->canvasEdge(); | 572 | delete edge->canvasEdge(); | ||
628 | delete edge; | 573 | delete edge; | ||
629 | } | 574 | } | ||
630 | it = edges().erase(it); | 575 | it = edges().erase(it); | ||
631 | } | 576 | } else { | ||
632 | else | | |||
633 | { | | |||
634 | ++it; | 577 | ++it; | ||
635 | } | 578 | } | ||
636 | } | 579 | } | ||
637 | 580 | | |||
638 | if (node->canvasNode()) | 581 | if (node->canvasNode()) { | ||
639 | { | | |||
640 | node->canvasNode()->hide(); | 582 | node->canvasNode()->hide(); | ||
641 | delete node->canvasNode(); | 583 | delete node->canvasNode(); | ||
642 | node->setCanvasNode(nullptr); | 584 | node->setCanvasNode(nullptr); | ||
643 | } | 585 | } | ||
644 | nodes().remove(nodeName); | 586 | nodes().remove(nodeName); | ||
645 | delete node; | 587 | delete node; | ||
646 | | ||||
647 | } | 588 | } | ||
648 | 589 | | |||
649 | void DotGraph::removeNodeFromSubgraph( | 590 | void DotGraph::removeNodeFromSubgraph(const QString &nodeName, const QString &subgraphName) | ||
650 | const QString& nodeName, | | |||
651 | const QString& subgraphName) | | |||
652 | { | 591 | { | ||
653 | qCDebug(KGRAPHVIEWERLIB_LOG) << nodeName << subgraphName; | 592 | qCDebug(KGRAPHVIEWERLIB_LOG) << nodeName << subgraphName; | ||
654 | GraphNode* node = dynamic_cast<GraphNode*>(elementNamed(nodeName)); | 593 | GraphNode *node = dynamic_cast<GraphNode *>(elementNamed(nodeName)); | ||
655 | if (node == nullptr) | 594 | if (node == nullptr) { | ||
656 | { | | |||
657 | qCWarning(KGRAPHVIEWERLIB_LOG) << "No such node " << nodeName; | 595 | qCWarning(KGRAPHVIEWERLIB_LOG) << "No such node " << nodeName; | ||
658 | return; | 596 | return; | ||
659 | } | 597 | } | ||
660 | 598 | | |||
661 | GraphSubgraph* subgraph = subgraphs()[subgraphName]; | 599 | GraphSubgraph *subgraph = subgraphs()[subgraphName]; | ||
662 | if (subgraph == nullptr) | 600 | if (subgraph == nullptr) { | ||
663 | { | | |||
664 | qCWarning(KGRAPHVIEWERLIB_LOG) << "No such subgraph " << subgraphName; | 601 | qCWarning(KGRAPHVIEWERLIB_LOG) << "No such subgraph " << subgraphName; | ||
665 | return; | 602 | return; | ||
666 | } | 603 | } | ||
667 | 604 | | |||
668 | subgraph->removeElement(node); | 605 | subgraph->removeElement(node); | ||
669 | if (subgraph->content().isEmpty()) | 606 | if (subgraph->content().isEmpty()) { | ||
670 | { | | |||
671 | removeSubgraphNamed(subgraphName); | 607 | removeSubgraphNamed(subgraphName); | ||
672 | } | 608 | } | ||
673 | } | 609 | } | ||
674 | 610 | | |||
675 | void DotGraph::removeSubgraphNamed(const QString& subgraphName) | 611 | void DotGraph::removeSubgraphNamed(const QString &subgraphName) | ||
676 | { | 612 | { | ||
677 | qCDebug(KGRAPHVIEWERLIB_LOG) << subgraphName << " from " << subgraphs().keys(); | 613 | qCDebug(KGRAPHVIEWERLIB_LOG) << subgraphName << " from " << subgraphs().keys(); | ||
678 | GraphSubgraph* subgraph = subgraphs()[subgraphName]; | 614 | GraphSubgraph *subgraph = subgraphs()[subgraphName]; | ||
679 | 615 | | |||
680 | if (subgraph == nullptr) | 616 | if (subgraph == nullptr) { | ||
681 | { | | |||
682 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Subgraph" << subgraphName << "not found"; | 617 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Subgraph" << subgraphName << "not found"; | ||
683 | return; | 618 | return; | ||
684 | } | 619 | } | ||
685 | GraphEdgeMap::iterator it, it_end; | 620 | GraphEdgeMap::iterator it, it_end; | ||
686 | it = m_edgesMap.begin(); it_end = m_edgesMap.end(); | 621 | it = m_edgesMap.begin(); | ||
687 | while (it != it_end) | 622 | it_end = m_edgesMap.end(); | ||
688 | { | 623 | while (it != it_end) { | ||
689 | if ( it.value()->fromNode() == subgraph | 624 | if (it.value()->fromNode() == subgraph || it.value()->toNode() == subgraph) { | ||
690 | || it.value()->toNode() == subgraph ) | | |||
691 | { | | |||
692 | GraphEdge* edge = it.value(); | 625 | GraphEdge *edge = it.value(); | ||
693 | if (edge->canvasEdge()) | 626 | if (edge->canvasEdge()) { | ||
694 | { | | |||
695 | edge->canvasEdge()->hide(); | 627 | edge->canvasEdge()->hide(); | ||
696 | delete edge->canvasEdge(); | 628 | delete edge->canvasEdge(); | ||
697 | delete edge; | 629 | delete edge; | ||
698 | } | 630 | } | ||
699 | it = edges().erase(it); | 631 | it = edges().erase(it); | ||
700 | } | 632 | } else { | ||
701 | else | | |||
702 | { | | |||
703 | ++it; | 633 | ++it; | ||
704 | } | 634 | } | ||
705 | } | 635 | } | ||
706 | 636 | | |||
707 | if (subgraph->canvasSubgraph()) | 637 | if (subgraph->canvasSubgraph()) { | ||
708 | { | | |||
709 | subgraph->canvasSubgraph()->hide(); | 638 | subgraph->canvasSubgraph()->hide(); | ||
710 | delete subgraph->canvasSubgraph(); | 639 | delete subgraph->canvasSubgraph(); | ||
711 | subgraph->setCanvasSubgraph(nullptr); | 640 | subgraph->setCanvasSubgraph(nullptr); | ||
712 | } | 641 | } | ||
713 | foreach(GraphElement* element, subgraph->content()) | 642 | foreach (GraphElement *element, subgraph->content()) { | ||
714 | { | 643 | if (dynamic_cast<GraphNode *>(element)) { | ||
715 | if (dynamic_cast<GraphNode*>(element)) | | |||
716 | { | | |||
717 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Adding" << element->id() << "to main graph"; | 644 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Adding" << element->id() << "to main graph"; | ||
718 | nodes()[element->id()] = dynamic_cast<GraphNode*>(element); | 645 | nodes()[element->id()] = dynamic_cast<GraphNode *>(element); | ||
719 | } | 646 | } else if (dynamic_cast<GraphSubgraph *>(element)) { | ||
720 | else if (dynamic_cast<GraphSubgraph*>(element)) | | |||
721 | { | | |||
722 | subgraphs()[element->id()] = dynamic_cast<GraphSubgraph*>(element); | 647 | subgraphs()[element->id()] = dynamic_cast<GraphSubgraph *>(element); | ||
723 | } | 648 | } else { | ||
724 | else | | |||
725 | { | | |||
726 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Don't know how to handle" << element->id(); | 649 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Don't know how to handle" << element->id(); | ||
727 | } | 650 | } | ||
728 | } | 651 | } | ||
729 | subgraph->content().clear(); | 652 | subgraph->content().clear(); | ||
730 | subgraphs().remove(subgraphName); | 653 | subgraphs().remove(subgraphName); | ||
731 | delete subgraph; | 654 | delete subgraph; | ||
732 | } | 655 | } | ||
733 | 656 | | |||
734 | void DotGraph::removeEdge(const QString& id) | 657 | void DotGraph::removeEdge(const QString &id) | ||
735 | { | 658 | { | ||
736 | qCDebug(KGRAPHVIEWERLIB_LOG) << id; | 659 | qCDebug(KGRAPHVIEWERLIB_LOG) << id; | ||
737 | GraphEdgeMap::iterator it = edges().begin(); | 660 | GraphEdgeMap::iterator it = edges().begin(); | ||
738 | for (; it != edges().end(); it++) | 661 | for (; it != edges().end(); it++) { | ||
739 | { | | |||
740 | GraphEdge* edge = it.value(); | 662 | GraphEdge *edge = it.value(); | ||
741 | if (edge->id() ==id) | 663 | if (edge->id() == id) { | ||
742 | { | 664 | if (edge->canvasEdge()) { | ||
743 | if (edge->canvasEdge()) | | |||
744 | { | | |||
745 | edge->canvasEdge()->hide(); | 665 | edge->canvasEdge()->hide(); | ||
746 | delete edge->canvasEdge(); | 666 | delete edge->canvasEdge(); | ||
747 | delete edge; | 667 | delete edge; | ||
748 | } | 668 | } | ||
749 | edges().remove(id); | 669 | edges().remove(id); | ||
750 | break; | 670 | break; | ||
751 | } | 671 | } | ||
752 | } | 672 | } | ||
753 | } | 673 | } | ||
754 | 674 | | |||
755 | void DotGraph::removeElement(const QString& id) | 675 | void DotGraph::removeElement(const QString &id) | ||
756 | { | 676 | { | ||
757 | qCDebug(KGRAPHVIEWERLIB_LOG) << id; | 677 | qCDebug(KGRAPHVIEWERLIB_LOG) << id; | ||
758 | GraphNodeMap::const_iterator itN = nodes().constBegin(); | 678 | GraphNodeMap::const_iterator itN = nodes().constBegin(); | ||
759 | for (; itN != nodes().constEnd(); itN++) | 679 | for (; itN != nodes().constEnd(); itN++) { | ||
760 | { | | |||
761 | const GraphNode* node = itN.value(); | 680 | const GraphNode *node = itN.value(); | ||
762 | if (node->id() == id) | 681 | if (node->id() == id) { | ||
763 | { | | |||
764 | removeNodeNamed(id); | 682 | removeNodeNamed(id); | ||
765 | return; | 683 | return; | ||
766 | } | 684 | } | ||
767 | } | 685 | } | ||
768 | GraphEdgeMap::const_iterator itE = edges().constBegin(); | 686 | GraphEdgeMap::const_iterator itE = edges().constBegin(); | ||
769 | for (; itE != edges().constEnd(); itE++) | 687 | for (; itE != edges().constEnd(); itE++) { | ||
770 | { | | |||
771 | const GraphEdge* edge = itE.value(); | 688 | const GraphEdge *edge = itE.value(); | ||
772 | if (edge->id() == id) | 689 | if (edge->id() == id) { | ||
773 | { | | |||
774 | removeEdge(id); | 690 | removeEdge(id); | ||
775 | return; | 691 | return; | ||
776 | } | 692 | } | ||
777 | } | 693 | } | ||
778 | GraphSubgraphMap::const_iterator itS = subgraphs().constBegin(); | 694 | GraphSubgraphMap::const_iterator itS = subgraphs().constBegin(); | ||
779 | for (; itS != subgraphs().constEnd(); itS++) | 695 | for (; itS != subgraphs().constEnd(); itS++) { | ||
780 | { | | |||
781 | const GraphSubgraph* subgraph = itS.value(); | 696 | const GraphSubgraph *subgraph = itS.value(); | ||
782 | if (subgraph->id() == id) | 697 | if (subgraph->id() == id) { | ||
783 | { | | |||
784 | removeSubgraphNamed(id); | 698 | removeSubgraphNamed(id); | ||
785 | return; | 699 | return; | ||
786 | } | 700 | } | ||
787 | } | 701 | } | ||
788 | } | 702 | } | ||
789 | 703 | | |||
790 | void DotGraph::setAttribute(const QString& elementId, const QString& attributeName, const QString& attributeValue) | 704 | void DotGraph::setAttribute(const QString &elementId, const QString &attributeName, const QString &attributeValue) | ||
791 | { | 705 | { | ||
792 | if (nodes().contains(elementId)) | 706 | if (nodes().contains(elementId)) { | ||
793 | { | | |||
794 | nodes()[elementId]->attributes()[attributeName] = attributeValue; | 707 | nodes()[elementId]->attributes()[attributeName] = attributeValue; | ||
795 | } | 708 | } else if (edges().contains(elementId)) { | ||
796 | else if (edges().contains(elementId)) | | |||
797 | { | | |||
798 | edges()[elementId]->attributes()[attributeName] = attributeValue; | 709 | edges()[elementId]->attributes()[attributeName] = attributeValue; | ||
799 | } | 710 | } else if (subgraphs().contains(elementId)) { | ||
800 | else if (subgraphs().contains(elementId)) | | |||
801 | { | | |||
802 | subgraphs()[elementId]->attributes()[attributeName] = attributeValue; | 711 | subgraphs()[elementId]->attributes()[attributeName] = attributeValue; | ||
803 | } | 712 | } | ||
804 | } | 713 | } | ||
805 | 714 | | |||
806 | GraphElement* DotGraph::elementNamed(const QString& id) | 715 | GraphElement *DotGraph::elementNamed(const QString &id) | ||
807 | { | 716 | { | ||
808 | GraphElement* ret = nullptr; | 717 | GraphElement *ret = nullptr; | ||
809 | if ((ret = m_nodesMap.value(id, nullptr))) { | 718 | if ((ret = m_nodesMap.value(id, nullptr))) { | ||
810 | return ret; | 719 | return ret; | ||
811 | } | 720 | } | ||
812 | if ((ret = m_edgesMap.value(id, nullptr))) { | 721 | if ((ret = m_edgesMap.value(id, nullptr))) { | ||
813 | return ret; | 722 | return ret; | ||
814 | } | 723 | } | ||
815 | foreach(GraphSubgraph* subGraph, m_subgraphsMap) { | 724 | foreach (GraphSubgraph *subGraph, m_subgraphsMap) { | ||
816 | if ((ret = subGraph->elementNamed(id))) { | 725 | if ((ret = subGraph->elementNamed(id))) { | ||
817 | return ret; | 726 | return ret; | ||
818 | } | 727 | } | ||
819 | } | 728 | } | ||
820 | return nullptr; | 729 | return nullptr; | ||
821 | } | 730 | } | ||
822 | 731 | | |||
823 | void DotGraph::setGraphAttributes(QMap<QString,QString> attribs) | 732 | void DotGraph::setGraphAttributes(QMap<QString, QString> attribs) | ||
824 | { | 733 | { | ||
825 | qCDebug(KGRAPHVIEWERLIB_LOG) << attribs; | 734 | qCDebug(KGRAPHVIEWERLIB_LOG) << attribs; | ||
826 | attributes() = attribs; | 735 | attributes() = attribs; | ||
827 | } | 736 | } | ||
828 | 737 | | |||
829 | | ||||
830 | void DotGraph::addNewNode(QMap<QString,QString> attribs) | 738 | void DotGraph::addNewNode(QMap<QString, QString> attribs) | ||
831 | { | 739 | { | ||
832 | qCDebug(KGRAPHVIEWERLIB_LOG) << attribs; | 740 | qCDebug(KGRAPHVIEWERLIB_LOG) << attribs; | ||
833 | GraphNode* newNode = new GraphNode(); | 741 | GraphNode *newNode = new GraphNode(); | ||
834 | newNode->attributes() = attribs; | 742 | newNode->attributes() = attribs; | ||
835 | nodes().insert(newNode->id(), newNode); | 743 | nodes().insert(newNode->id(), newNode); | ||
836 | qCDebug(KGRAPHVIEWERLIB_LOG) << "node added as" << newNode->id(); | 744 | qCDebug(KGRAPHVIEWERLIB_LOG) << "node added as" << newNode->id(); | ||
837 | } | 745 | } | ||
838 | 746 | | |||
839 | void DotGraph::addNewSubgraph(QMap<QString,QString> attribs) | 747 | void DotGraph::addNewSubgraph(QMap<QString, QString> attribs) | ||
840 | { | 748 | { | ||
841 | qCDebug(KGRAPHVIEWERLIB_LOG) << attribs; | 749 | qCDebug(KGRAPHVIEWERLIB_LOG) << attribs; | ||
842 | GraphSubgraph* newSG = new GraphSubgraph(); | 750 | GraphSubgraph *newSG = new GraphSubgraph(); | ||
843 | newSG->attributes() = attribs; | 751 | newSG->attributes() = attribs; | ||
844 | subgraphs()[newSG->id()] = newSG; | 752 | subgraphs()[newSG->id()] = newSG; | ||
845 | qCDebug(KGRAPHVIEWERLIB_LOG) << "subgraph added as" << newSG->id(); | 753 | qCDebug(KGRAPHVIEWERLIB_LOG) << "subgraph added as" << newSG->id(); | ||
846 | } | 754 | } | ||
847 | 755 | | |||
848 | void DotGraph::addNewNodeToSubgraph(QMap<QString,QString> attribs, QString subgraph) | 756 | void DotGraph::addNewNodeToSubgraph(QMap<QString, QString> attribs, QString subgraph) | ||
849 | { | 757 | { | ||
850 | qCDebug(KGRAPHVIEWERLIB_LOG) << attribs << "to" << subgraph; | 758 | qCDebug(KGRAPHVIEWERLIB_LOG) << attribs << "to" << subgraph; | ||
851 | GraphNode* newNode = new GraphNode(); | 759 | GraphNode *newNode = new GraphNode(); | ||
852 | newNode->attributes() = attribs; | 760 | newNode->attributes() = attribs; | ||
853 | subgraphs()[subgraph]->content().push_back(newNode); | 761 | subgraphs()[subgraph]->content().push_back(newNode); | ||
854 | 762 | | |||
855 | qCDebug(KGRAPHVIEWERLIB_LOG) << "node added as" << newNode->id() << "in" << subgraph; | 763 | qCDebug(KGRAPHVIEWERLIB_LOG) << "node added as" << newNode->id() << "in" << subgraph; | ||
856 | } | 764 | } | ||
857 | 765 | | |||
858 | void DotGraph::addExistingNodeToSubgraph(QMap<QString,QString> attribs,QString subgraph) | 766 | void DotGraph::addExistingNodeToSubgraph(QMap<QString, QString> attribs, QString subgraph) | ||
859 | { | 767 | { | ||
860 | qCDebug(KGRAPHVIEWERLIB_LOG) << attribs << "to" << subgraph; | 768 | qCDebug(KGRAPHVIEWERLIB_LOG) << attribs << "to" << subgraph; | ||
861 | GraphNode* node = dynamic_cast<GraphNode*>(elementNamed(attribs["id"])); | 769 | GraphNode *node = dynamic_cast<GraphNode *>(elementNamed(attribs["id"])); | ||
862 | if (node == nullptr) | 770 | if (node == nullptr) { | ||
863 | { | | |||
864 | qCWarning(KGRAPHVIEWERLIB_LOG) << "No such node" << attribs["id"]; | 771 | qCWarning(KGRAPHVIEWERLIB_LOG) << "No such node" << attribs["id"]; | ||
865 | return; | 772 | return; | ||
866 | } | 773 | } | ||
867 | if (nodes().contains(attribs["id"])) | 774 | if (nodes().contains(attribs["id"])) { | ||
868 | { | | |||
869 | nodes().remove(attribs["id"]); | 775 | nodes().remove(attribs["id"]); | ||
870 | node->attributes() = attribs; | 776 | node->attributes() = attribs; | ||
871 | subgraphs()[subgraph]->content().push_back(node); | 777 | subgraphs()[subgraph]->content().push_back(node); | ||
872 | qCDebug(KGRAPHVIEWERLIB_LOG) << "node " << node->id() << " added in " << subgraph; | 778 | qCDebug(KGRAPHVIEWERLIB_LOG) << "node " << node->id() << " added in " << subgraph; | ||
873 | } | 779 | } else { | ||
874 | else | 780 | foreach (GraphSubgraph *gs, subgraphs()) { | ||
875 | { | | |||
876 | foreach(GraphSubgraph* gs, subgraphs()) | | |||
877 | { | | |||
878 | GraphElement* elt = nullptr; | 781 | GraphElement *elt = nullptr; | ||
879 | foreach(GraphElement* element, gs->content()) | 782 | foreach (GraphElement *element, gs->content()) { | ||
880 | { | 783 | if (element == node) { | ||
881 | if (element == node) | | |||
882 | { | | |||
883 | elt = element; | 784 | elt = element; | ||
884 | break; | 785 | break; | ||
885 | } | 786 | } | ||
886 | } | 787 | } | ||
887 | if (elt) | 788 | if (elt) { | ||
888 | { | | |||
889 | qCDebug(KGRAPHVIEWERLIB_LOG) << "removing node " << elt->id() << " from " << gs->id(); | 789 | qCDebug(KGRAPHVIEWERLIB_LOG) << "removing node " << elt->id() << " from " << gs->id(); | ||
890 | gs->removeElement(elt); | 790 | gs->removeElement(elt); | ||
891 | subgraphs()[subgraph]->content().push_back(elt); | 791 | subgraphs()[subgraph]->content().push_back(elt); | ||
892 | qCDebug(KGRAPHVIEWERLIB_LOG) << "node " << elt->id() << " added in " << subgraph; | 792 | qCDebug(KGRAPHVIEWERLIB_LOG) << "node " << elt->id() << " added in " << subgraph; | ||
893 | break; | 793 | break; | ||
894 | } | 794 | } | ||
895 | } | 795 | } | ||
896 | } | 796 | } | ||
897 | } | 797 | } | ||
898 | 798 | | |||
899 | void DotGraph::moveExistingNodeToMainGraph(QMap<QString,QString> attribs) | 799 | void DotGraph::moveExistingNodeToMainGraph(QMap<QString, QString> attribs) | ||
900 | { | 800 | { | ||
901 | qCDebug(KGRAPHVIEWERLIB_LOG) << attribs; | 801 | qCDebug(KGRAPHVIEWERLIB_LOG) << attribs; | ||
902 | GraphNode* node = dynamic_cast<GraphNode*>(elementNamed(attribs["id"])); | 802 | GraphNode *node = dynamic_cast<GraphNode *>(elementNamed(attribs["id"])); | ||
903 | if (node == nullptr) | 803 | if (node == nullptr) { | ||
904 | { | | |||
905 | qCWarning(KGRAPHVIEWERLIB_LOG) << "No such node" << attribs["id"]; | 804 | qCWarning(KGRAPHVIEWERLIB_LOG) << "No such node" << attribs["id"]; | ||
906 | return; | 805 | return; | ||
907 | } | 806 | } else if (nodes().contains(attribs["id"])) { | ||
908 | else if (nodes().contains(attribs["id"])) | | |||
909 | { | | |||
910 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Node" << attribs["id"] << "already in main graph"; | 807 | qCWarning(KGRAPHVIEWERLIB_LOG) << "Node" << attribs["id"] << "already in main graph"; | ||
911 | return; | 808 | return; | ||
912 | } | 809 | } else { | ||
913 | else | 810 | foreach (GraphSubgraph *gs, subgraphs()) { | ||
914 | { | | |||
915 | foreach(GraphSubgraph* gs, subgraphs()) | | |||
916 | { | | |||
917 | bool found = false; | 811 | bool found = false; | ||
918 | foreach(GraphElement* element, gs->content()) | 812 | foreach (GraphElement *element, gs->content()) { | ||
919 | { | 813 | if (element == node) { | ||
920 | if (element == node) | | |||
921 | { | | |||
922 | found = true; | 814 | found = true; | ||
923 | break; | 815 | break; | ||
924 | } | 816 | } | ||
925 | } | 817 | } | ||
926 | if (found) | 818 | if (found) { | ||
927 | { | | |||
928 | qCDebug(KGRAPHVIEWERLIB_LOG) << "removing node " << node->id() << " from " << gs->id(); | 819 | qCDebug(KGRAPHVIEWERLIB_LOG) << "removing node " << node->id() << " from " << gs->id(); | ||
929 | gs->removeElement(node); | 820 | gs->removeElement(node); | ||
930 | nodes()[node->id()] = node; | 821 | nodes()[node->id()] = node; | ||
931 | qCDebug(KGRAPHVIEWERLIB_LOG) << "node " << node->id() << " moved to main graph"; | 822 | qCDebug(KGRAPHVIEWERLIB_LOG) << "node " << node->id() << " moved to main graph"; | ||
932 | break; | 823 | break; | ||
933 | } | 824 | } | ||
934 | } | 825 | } | ||
935 | } | 826 | } | ||
936 | } | 827 | } | ||
937 | 828 | | |||
938 | void DotGraph::addNewEdge(QString src, QString tgt, QMap<QString,QString> attribs) | 829 | void DotGraph::addNewEdge(QString src, QString tgt, QMap<QString, QString> attribs) | ||
939 | { | 830 | { | ||
940 | qCDebug(KGRAPHVIEWERLIB_LOG) << src << tgt << attribs; | 831 | qCDebug(KGRAPHVIEWERLIB_LOG) << src << tgt << attribs; | ||
941 | GraphEdge* newEdge = new GraphEdge(); | 832 | GraphEdge *newEdge = new GraphEdge(); | ||
942 | newEdge->attributes() = attribs; | 833 | newEdge->attributes() = attribs; | ||
943 | GraphElement* srcElement = elementNamed(src); | 834 | GraphElement *srcElement = elementNamed(src); | ||
944 | if (srcElement == nullptr) | 835 | if (srcElement == nullptr) { | ||
945 | { | | |||
946 | srcElement = elementNamed(QString("cluster_")+src); | 836 | srcElement = elementNamed(QString("cluster_") + src); | ||
947 | } | 837 | } | ||
948 | GraphElement* tgtElement = elementNamed(tgt); | 838 | GraphElement *tgtElement = elementNamed(tgt); | ||
949 | if (tgtElement == nullptr) | 839 | if (tgtElement == nullptr) { | ||
950 | { | | |||
951 | tgtElement = elementNamed(QString("cluster_")+tgt); | 840 | tgtElement = elementNamed(QString("cluster_") + tgt); | ||
952 | } | 841 | } | ||
953 | 842 | | |||
954 | if (srcElement == nullptr || tgtElement == nullptr) | 843 | if (srcElement == nullptr || tgtElement == nullptr) { | ||
955 | { | | |||
956 | qCWarning(KGRAPHVIEWERLIB_LOG) << src << "or" << tgt << "missing"; | 844 | qCWarning(KGRAPHVIEWERLIB_LOG) << src << "or" << tgt << "missing"; | ||
957 | return; | 845 | return; | ||
958 | } | 846 | } | ||
959 | if (attribs.contains("id")) | 847 | if (attribs.contains("id")) { | ||
960 | { | | |||
961 | newEdge->setId(attribs["id"]); | 848 | newEdge->setId(attribs["id"]); | ||
962 | } | 849 | } else { | ||
963 | else | | |||
964 | { | | |||
965 | newEdge->setId(src+tgt+QUuid::createUuid().toString().remove('{').remove('}').remove('-')); | 850 | newEdge->setId(src + tgt + QUuid::createUuid().toString().remove('{').remove('}').remove('-')); | ||
966 | } | 851 | } | ||
967 | newEdge->setFromNode(srcElement); | 852 | newEdge->setFromNode(srcElement); | ||
968 | newEdge->setToNode(tgtElement); | 853 | newEdge->setToNode(tgtElement); | ||
969 | edges().insert(newEdge->id(), newEdge); | 854 | edges().insert(newEdge->id(), newEdge); | ||
970 | } | 855 | } | ||
971 | 856 | | |||
972 | void DotGraph::removeAttribute(const QString& nodeName, const QString& attribName) | 857 | void DotGraph::removeAttribute(const QString &nodeName, const QString &attribName) | ||
973 | { | 858 | { | ||
974 | GraphElement* element = elementNamed(nodeName); | 859 | GraphElement *element = elementNamed(nodeName); | ||
975 | if (element) | 860 | if (element) { | ||
976 | { | | |||
977 | element->removeAttribute(attribName); | 861 | element->removeAttribute(attribName); | ||
978 | } | 862 | } | ||
979 | } | 863 | } | ||
980 | 864 | | |||
981 | void DotGraph::renameNode(const QString& oldNodeName, const QString& newNodeName) | 865 | void DotGraph::renameNode(const QString &oldNodeName, const QString &newNodeName) | ||
982 | { | 866 | { | ||
983 | if (oldNodeName != newNodeName) | 867 | if (oldNodeName != newNodeName) { | ||
984 | { | | |||
985 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Renaming " << oldNodeName << " into " << newNodeName; | 868 | qCDebug(KGRAPHVIEWERLIB_LOG) << "Renaming " << oldNodeName << " into " << newNodeName; | ||
986 | GraphNode* node = nodes()[oldNodeName]; | 869 | GraphNode *node = nodes()[oldNodeName]; | ||
987 | nodes().remove(oldNodeName); | 870 | nodes().remove(oldNodeName); | ||
988 | node->setId(newNodeName); | 871 | node->setId(newNodeName); | ||
989 | nodes()[newNodeName] = node; | 872 | nodes()[newNodeName] = node; | ||
990 | } | 873 | } | ||
991 | } | 874 | } | ||
992 | 875 | | |||
993 | QString DotGraph::backColor() const | 876 | QString DotGraph::backColor() const | ||
994 | { | 877 | { | ||
995 | if (m_attributes.find("bgcolor") != m_attributes.end()) | 878 | if (m_attributes.find("bgcolor") != m_attributes.end()) { | ||
996 | { | | |||
997 | return m_attributes["bgcolor"]; | 879 | return m_attributes["bgcolor"]; | ||
998 | } | 880 | } else { | ||
999 | else | | |||
1000 | { | | |||
1001 | return QString(); | 881 | return QString(); | ||
1002 | } | 882 | } | ||
1003 | } | 883 | } | ||
1004 | 884 | | |||
1005 | | ||||
1006 | } | 885 | } |