Changeset View
Changeset View
Standalone View
Standalone View
src/part/graphelement.cpp
Show All 24 Lines | |||||
25 | 25 | | |||
26 | #include <QDebug> | 26 | #include <QDebug> | ||
27 | 27 | | |||
28 | #include <QRegExp> | 28 | #include <QRegExp> | ||
29 | #include <graphviz/gvc.h> | 29 | #include <graphviz/gvc.h> | ||
30 | 30 | | |||
31 | namespace KGraphViewer | 31 | namespace KGraphViewer | ||
32 | { | 32 | { | ||
33 | | ||||
34 | const QString GraphElement::KEY_ID = QLatin1String("id"); | 33 | const QString GraphElement::KEY_ID = QLatin1String("id"); | ||
35 | const QString GraphElement::KEY_STYLE = QLatin1String("style"); | 34 | const QString GraphElement::KEY_STYLE = QLatin1String("style"); | ||
36 | const QString GraphElement::KEY_LABEL = QLatin1String("label"); | 35 | const QString GraphElement::KEY_LABEL = QLatin1String("label"); | ||
37 | const QString GraphElement::KEY_SHAPE = QLatin1String("shape"); | 36 | const QString GraphElement::KEY_SHAPE = QLatin1String("shape"); | ||
38 | const QString GraphElement::KEY_SHAPEFILE = QLatin1String("shapefile"); | 37 | const QString GraphElement::KEY_SHAPEFILE = QLatin1String("shapefile"); | ||
39 | const QString GraphElement::KEY_COLOR = QLatin1String("color"); | 38 | const QString GraphElement::KEY_COLOR = QLatin1String("color"); | ||
40 | const QString GraphElement::KEY_BGCOLOR = QLatin1String("bgcolor"); | 39 | const QString GraphElement::KEY_BGCOLOR = QLatin1String("bgcolor"); | ||
41 | const QString GraphElement::KEY_URL = QLatin1String("URL"); | 40 | const QString GraphElement::KEY_URL = QLatin1String("URL"); | ||
42 | const QString GraphElement::KEY_FONTSIZE = QLatin1String("fontsize"); | 41 | const QString GraphElement::KEY_FONTSIZE = QLatin1String("fontsize"); | ||
43 | const QString GraphElement::KEY_FONTNAME = QLatin1String("fontname"); | 42 | const QString GraphElement::KEY_FONTNAME = QLatin1String("fontname"); | ||
44 | const QString GraphElement::KEY_FONTCOLOR = QLatin1String("fontcolor"); | 43 | const QString GraphElement::KEY_FONTCOLOR = QLatin1String("fontcolor"); | ||
45 | const QString GraphElement::KEY_FILLCOLOR = QLatin1String("fillcolor"); | 44 | const QString GraphElement::KEY_FILLCOLOR = QLatin1String("fillcolor"); | ||
46 | 45 | | |||
47 | GraphElement::GraphElement() : | 46 | GraphElement::GraphElement() | ||
48 | QObject(), | 47 | : QObject() | ||
49 | m_attributes(), | 48 | , m_attributes() | ||
50 | m_originalAttributes(), | 49 | , m_originalAttributes() | ||
51 | m_ce(nullptr), | 50 | , m_ce(nullptr) | ||
52 | m_z(1.0), | 51 | , m_z(1.0) | ||
53 | m_renderOperations(), | 52 | , m_renderOperations() | ||
54 | m_renderOperationsRevision(0), | 53 | , m_renderOperationsRevision(0) | ||
55 | m_selected(false) | 54 | , m_selected(false) | ||
56 | { | 55 | { | ||
57 | /* label(""); | 56 | /* label(""); | ||
58 | id(""); | 57 | id(""); | ||
59 | style(DOT_DEFAULT_STYLE); | 58 | style(DOT_DEFAULT_STYLE); | ||
60 | shape(DOT_DEFAULT_SHAPE); | 59 | shape(DOT_DEFAULT_SHAPE); | ||
61 | lineColor(DOT_DEFAULT_LINECOLOR); | 60 | lineColor(DOT_DEFAULT_LINECOLOR); | ||
62 | backColor(DOT_DEFAULT_BACKCOLOR); | 61 | backColor(DOT_DEFAULT_BACKCOLOR); | ||
63 | fontName(DOT_DEFAULT_FONTNAME); | 62 | fontName(DOT_DEFAULT_FONTNAME); | ||
64 | fontColor(DOT_DEFAULT_FONTCOLOR); | 63 | fontColor(DOT_DEFAULT_FONTCOLOR); | ||
65 | url(""); | 64 | url(""); | ||
66 | shapeFile("");*/ | 65 | shapeFile("");*/ | ||
67 | setFontSize(DOT_DEFAULT_FONTSIZE); | 66 | setFontSize(DOT_DEFAULT_FONTSIZE); | ||
68 | } | 67 | } | ||
69 | 68 | | |||
70 | GraphElement::GraphElement(const GraphElement& element) : QObject(), | 69 | GraphElement::GraphElement(const GraphElement &element) | ||
71 | m_attributes(), | 70 | : QObject() | ||
72 | m_originalAttributes(), | 71 | , m_attributes() | ||
73 | m_ce(element.m_ce), | 72 | , m_originalAttributes() | ||
74 | m_z(element.m_z), | 73 | , m_ce(element.m_ce) | ||
75 | m_renderOperations(), | 74 | , m_z(element.m_z) | ||
76 | m_renderOperationsRevision(0), | 75 | , m_renderOperations() | ||
77 | m_selected(element.m_selected) | 76 | , m_renderOperationsRevision(0) | ||
77 | , m_selected(element.m_selected) | ||||
78 | { | 78 | { | ||
79 | updateWithElement(element); | 79 | updateWithElement(element); | ||
80 | } | 80 | } | ||
81 | 81 | | |||
82 | void GraphElement::setRenderOperations(const DotRenderOpVec& drov) | 82 | void GraphElement::setRenderOperations(const DotRenderOpVec &drov) | ||
83 | { | 83 | { | ||
84 | m_renderOperations = drov; | 84 | m_renderOperations = drov; | ||
85 | ++m_renderOperationsRevision; | 85 | ++m_renderOperationsRevision; | ||
86 | } | 86 | } | ||
87 | 87 | | |||
88 | void GraphElement::updateWithElement(const GraphElement& element) | 88 | void GraphElement::updateWithElement(const GraphElement &element) | ||
89 | { | 89 | { | ||
90 | qCDebug(KGRAPHVIEWERLIB_LOG) << element.id(); | 90 | qCDebug(KGRAPHVIEWERLIB_LOG) << element.id(); | ||
91 | bool modified = false; | 91 | bool modified = false; | ||
92 | if (element.z() != m_z) | 92 | if (element.z() != m_z) { | ||
93 | { | | |||
94 | m_z = element.z(); | 93 | m_z = element.z(); | ||
95 | modified = true; | 94 | modified = true; | ||
96 | } | 95 | } | ||
97 | QMap <QString, QString >::const_iterator it = element.attributes().constBegin(); | 96 | QMap<QString, QString>::const_iterator it = element.attributes().constBegin(); | ||
98 | for (;it != element.attributes().constEnd(); it++) | 97 | for (; it != element.attributes().constEnd(); it++) { | ||
99 | { | | |||
100 | const QString &attrib = it.key(); | 98 | const QString &attrib = it.key(); | ||
101 | if ( (!m_attributes.contains(attrib)) || (m_attributes[attrib] != it.value()) ) | 99 | if ((!m_attributes.contains(attrib)) || (m_attributes[attrib] != it.value())) { | ||
102 | { | | |||
103 | m_attributes[attrib] = it.value(); | 100 | m_attributes[attrib] = it.value(); | ||
104 | if (attrib == "z") | 101 | if (attrib == "z") { | ||
105 | { | | |||
106 | bool ok; | 102 | bool ok; | ||
107 | setZ(m_attributes[attrib].toDouble(&ok)); | 103 | setZ(m_attributes[attrib].toDouble(&ok)); | ||
108 | } | 104 | } | ||
109 | modified = true; | 105 | modified = true; | ||
110 | } | 106 | } | ||
111 | } | 107 | } | ||
112 | if (modified) | 108 | if (modified) { | ||
113 | { | | |||
114 | qCDebug(KGRAPHVIEWERLIB_LOG) << "modified: update render operations"; | 109 | qCDebug(KGRAPHVIEWERLIB_LOG) << "modified: update render operations"; | ||
115 | setRenderOperations(element.m_renderOperations); | 110 | setRenderOperations(element.m_renderOperations); | ||
116 | /* foreach (DotRenderOp op, m_renderOperations) | 111 | /* foreach (DotRenderOp op, m_renderOperations) | ||
117 | { | 112 | { | ||
118 | QString msg; | 113 | QString msg; | ||
119 | QTextStream dd(&msg); | 114 | QTextStream dd(&msg); | ||
120 | dd << "an op: " << op.renderop << " "; | 115 | dd << "an op: " << op.renderop << " "; | ||
121 | foreach (int i, op.integers) | 116 | foreach (int i, op.integers) | ||
122 | { | 117 | { | ||
123 | dd << i << " "; | 118 | dd << i << " "; | ||
124 | } | 119 | } | ||
125 | dd << op.str; | 120 | dd << op.str; | ||
126 | qCDebug(KGRAPHVIEWERLIB_LOG) << msg; | 121 | qCDebug(KGRAPHVIEWERLIB_LOG) << msg; | ||
127 | } | 122 | } | ||
128 | g() << "modified: emiting changed";*/ | 123 | g() << "modified: emiting changed";*/ | ||
129 | emit changed(); | 124 | emit changed(); | ||
130 | } | 125 | } | ||
131 | qCDebug(KGRAPHVIEWERLIB_LOG) << "done" << m_renderOperations.size(); | 126 | qCDebug(KGRAPHVIEWERLIB_LOG) << "done" << m_renderOperations.size(); | ||
132 | } | 127 | } | ||
133 | 128 | | |||
134 | | ||||
135 | QString GraphElement::backColor() const | 129 | QString GraphElement::backColor() const | ||
136 | { | 130 | { | ||
137 | if (m_attributes.find(KEY_FILLCOLOR) != m_attributes.end()) | 131 | if (m_attributes.find(KEY_FILLCOLOR) != m_attributes.end()) { | ||
138 | { | | |||
139 | return m_attributes[KEY_FILLCOLOR]; | 132 | return m_attributes[KEY_FILLCOLOR]; | ||
140 | } | 133 | } else if ((m_attributes.find(KEY_COLOR) != m_attributes.end()) && (m_attributes[KEY_STYLE] == QLatin1String("filled"))) { | ||
141 | else if ( (m_attributes.find(KEY_COLOR) != m_attributes.end()) | | |||
142 | && (m_attributes[KEY_STYLE] == QLatin1String("filled")) ) | | |||
143 | { | | |||
144 | return m_attributes[KEY_COLOR]; | 134 | return m_attributes[KEY_COLOR]; | ||
145 | } | 135 | } else { | ||
146 | else | | |||
147 | { | | |||
148 | return DOT_DEFAULT_NODE_BACKCOLOR; | 136 | return DOT_DEFAULT_NODE_BACKCOLOR; | ||
149 | } | 137 | } | ||
150 | } | 138 | } | ||
151 | 139 | | |||
152 | void GraphElement::removeAttribute(const QString& attribName) | 140 | void GraphElement::removeAttribute(const QString &attribName) | ||
153 | { | 141 | { | ||
154 | qCDebug(KGRAPHVIEWERLIB_LOG) << attribName; | 142 | qCDebug(KGRAPHVIEWERLIB_LOG) << attribName; | ||
155 | m_attributes.remove(attribName); | 143 | m_attributes.remove(attribName); | ||
156 | emit changed(); | 144 | emit changed(); | ||
157 | } | 145 | } | ||
158 | 146 | | |||
159 | void GraphElement::exportToGraphviz(void* element) const | 147 | void GraphElement::exportToGraphviz(void *element) const | ||
160 | { | 148 | { | ||
161 | QMap<QString,QString>::const_iterator it, it_end; | 149 | QMap<QString, QString>::const_iterator it, it_end; | ||
162 | it = attributes().begin(); it_end = attributes().end(); | 150 | it = attributes().begin(); | ||
163 | for (;it != it_end; it++) | 151 | it_end = attributes().end(); | ||
164 | { | 152 | for (; it != it_end; it++) { | ||
165 | if (!it.value().isEmpty()) | 153 | if (!it.value().isEmpty()) { | ||
166 | { | 154 | if (it.key() == "label") { | ||
167 | if (it.key() == "label") | | |||
168 | { | | |||
169 | QString label = it.value(); | 155 | QString label = it.value(); | ||
170 | if (label != "label") | 156 | if (label != "label") { | ||
171 | { | | |||
172 | label.replace(QRegExp("\n"),"\\n"); | 157 | label.replace(QRegExp("\n"), "\\n"); | ||
173 | // qCDebug(KGRAPHVIEWERLIB_LOG) << it.key() << "=\"" << label << "\","; | 158 | // qCDebug(KGRAPHVIEWERLIB_LOG) << it.key() << "=\"" << label << "\","; | ||
174 | agsafeset(element, it.key().toUtf8().data(), label.toUtf8().data(), QString().toUtf8().data()); | 159 | agsafeset(element, it.key().toUtf8().data(), label.toUtf8().data(), QString().toUtf8().data()); | ||
175 | } | 160 | } | ||
176 | } | 161 | } else if (it.key() == "_draw_" || it.key() == "_ldraw_") { | ||
177 | else if (it.key() == "_draw_" || it.key() == "_ldraw_") | 162 | } else if (originalAttributes().isEmpty() || originalAttributes().contains(it.key())) { | ||
178 | { | | |||
179 | } | | |||
180 | else if (originalAttributes().isEmpty() || originalAttributes().contains(it.key())) | | |||
181 | { | | |||
182 | // qCDebug(KGRAPHVIEWERLIB_LOG) << it.key() << it.value(); | 163 | // qCDebug(KGRAPHVIEWERLIB_LOG) << it.key() << it.value(); | ||
183 | 164 | | |||
184 | agsafeset(element, it.key().toUtf8().data(), it.value().toUtf8().data(), QString().toUtf8().data()); | 165 | agsafeset(element, it.key().toUtf8().data(), it.value().toUtf8().data(), QString().toUtf8().data()); | ||
185 | } | 166 | } | ||
186 | } | 167 | } | ||
187 | } | 168 | } | ||
188 | } | 169 | } | ||
189 | 170 | | |||
190 | QTextStream& operator<<(QTextStream& s, const GraphElement& n) | 171 | QTextStream &operator<<(QTextStream &s, const GraphElement &n) | ||
191 | { | 172 | { | ||
192 | QMap<QString,QString>::const_iterator it, it_end; | 173 | QMap<QString, QString>::const_iterator it, it_end; | ||
193 | bool firstAttr = true; | 174 | bool firstAttr = true; | ||
194 | it = n.attributes().begin(); it_end = n.attributes().end(); | 175 | it = n.attributes().begin(); | ||
195 | for (;it != it_end; it++) | 176 | it_end = n.attributes().end(); | ||
196 | { | 177 | for (; it != it_end; it++) { | ||
197 | if (!it.value().isEmpty()) | 178 | if (!it.value().isEmpty()) { | ||
198 | { | 179 | if (it.key() == "label") { | ||
199 | if (it.key() == "label") | | |||
200 | { | | |||
201 | QString label = it.value(); | 180 | QString label = it.value(); | ||
202 | if (label != "label") | 181 | if (label != "label") { | ||
203 | { | | |||
204 | label.replace(QRegExp("\n"),"\\n"); | 182 | label.replace(QRegExp("\n"), "\\n"); | ||
205 | // qCDebug(KGRAPHVIEWERLIB_LOG) << it.key() << "=\"" << label << "\","; | 183 | // qCDebug(KGRAPHVIEWERLIB_LOG) << it.key() << "=\"" << label << "\","; | ||
206 | if (firstAttr) | 184 | if (firstAttr) | ||
207 | firstAttr = false; | 185 | firstAttr = false; | ||
208 | else | 186 | else | ||
209 | s << ','; | 187 | s << ','; | ||
210 | s << it.key() << "=\"" << label << '"'; | 188 | s << it.key() << "=\"" << label << '"'; | ||
211 | } | 189 | } | ||
212 | } | 190 | } else if (it.key() == "_draw_" || it.key() == "_ldraw_") { | ||
213 | else if (it.key() == "_draw_" || it.key() == "_ldraw_") | 191 | } else if (n.originalAttributes().isEmpty() || n.originalAttributes().contains(it.key())) { | ||
214 | { | | |||
215 | } | | |||
216 | else if (n.originalAttributes().isEmpty() || n.originalAttributes().contains(it.key())) | | |||
217 | { | | |||
218 | // qCDebug(KGRAPHVIEWERLIB_LOG) << it.key() << it.value(); | 192 | // qCDebug(KGRAPHVIEWERLIB_LOG) << it.key() << it.value(); | ||
219 | 193 | | |||
220 | if (firstAttr) | 194 | if (firstAttr) | ||
221 | firstAttr = false; | 195 | firstAttr = false; | ||
222 | else | 196 | else | ||
223 | s << ','; | 197 | s << ','; | ||
224 | s << it.key() << "=\"" << it.value() << '"'; | 198 | s << it.key() << "=\"" << it.value() << '"'; | ||
225 | } | 199 | } | ||
226 | } | 200 | } | ||
227 | } | 201 | } | ||
228 | return s; | 202 | return s; | ||
229 | } | 203 | } | ||
230 | 204 | | |||
231 | } | 205 | } |