Graph API & internal structures redesign
[hypercube:hypercube.git] / GUI / graphtab.cpp
1 #include <QtGui>
2 #include <QFileInfo>
3 #include "CORE/vertex.h"
4 #include "CORE/edge.h"
5 #include "IO/modules.h"
6 #include "vertexitem.h"
7 #include "edgeitem.h"
8 #include "graphtab.h"
9
10
11 GraphTab::GraphTab()
12 {
13         _graph = new Graph();
14         _view = new GraphView();
15         _sa = new SA();
16
17         _inputEncoding = 0;
18         _inputProvider = 0;
19         _outputProvider = 0;
20
21         QVBoxLayout *layout = new QVBoxLayout;
22         layout->setContentsMargins(0, 0, 0, 0);
23         layout->addWidget(_view);
24         setLayout(layout);
25 }
26
27 GraphTab::~GraphTab()
28 {
29         delete _view;
30         delete _sa;
31         delete _graph;
32 }
33
34 IO::Error GraphTab::readGraph(const QString &fileName)
35 {
36         IO::Error error;
37         QByteArray ba = fileName.toLocal8Bit();
38         const char *cFileName = ba.data();
39
40         InputProvider **p = inputProviders;
41         while (*p) {
42                 error = (*p)->readGraph(_graph, cFileName, _inputEncoding);
43                 if (error == IO::Ok) {
44                         _inputProvider = *p;
45                         break;
46                 } else if (error != IO::FormatError)
47                         return error;
48                 p++;
49         }
50
51         if (!_inputProvider)
52                 return IO::FormatError;
53
54         _inputFileName = fileName;
55
56         setGraphProperties();
57         loadGraph();
58
59         return IO::Ok;
60 }
61
62 IO::Error GraphTab::readGraph()
63 {
64         _graph->clear();
65
66         QByteArray ba = _inputFileName.toLocal8Bit();
67         const char *cFileName = ba.data();
68
69         IO::Error error = _inputProvider->readGraph(_graph, cFileName,
70           _inputEncoding);
71         if (error != IO::Ok)
72                 return error;
73
74         setGraphProperties();
75         loadGraph();
76
77         return IO::Ok;
78 }
79
80 IO::Error GraphTab::writeGraph(const QString &fileName, OutputProvider *provider)
81 {
82         _outputProvider = provider;
83         _outputFileName = fileName;
84
85         return writeGraph();
86 }
87
88 IO::Error GraphTab::writeGraph(void)
89 {
90         if (!_outputProvider)
91                 return IO::OpenError;
92         else {
93                 storeGraph();
94                 QByteArray ba = _outputFileName.toLocal8Bit();
95                 const char *cFileName = ba.data();
96                 return _outputProvider->writeGraph(_graph, cFileName);
97         }
98 }
99
100 Graph *GraphTab::graph()
101 {
102         storeGraph();
103         return _graph;
104 }
105
106 void GraphTab::transformGraph(void)
107 {
108         storeGraph();
109         _sa->compute(_graph);
110         _graph->center();
111         loadGraph();
112 }
113
114 void GraphTab::bindTo(Graph *graph)
115 {
116         storeGraph();
117         _graph->bindTo(graph);
118         loadGraph();
119 }
120
121 void GraphTab::project(Graph* graph)
122 {
123         storeGraph();
124         _graph->project(graph);
125         loadGraph();
126 }
127
128 void GraphTab::setDimensions(const QPoint &dimensions)
129 {
130         _view->setDimensions(dimensions);
131         _dimensions = dimensions;
132 }
133
134 void GraphTab::setVertexColor(const QColor &color)
135 {
136         _view->setVertexColor(color);
137         _vertexColor = color;
138 }
139
140 void GraphTab::setEdgeColor(const QColor &color)
141 {
142         _view->setEdgeColor(color);
143         _edgeColor = color;
144 }
145
146 void GraphTab::setVertexSize(int size)
147 {
148         _view->setVertexSize(size);
149         _vertexSize = size;
150 }
151
152 void GraphTab::setEdgeSize(int size)
153 {
154         _view->setEdgeSize(size);
155         _edgeSize = size;
156 }
157
158 void GraphTab::setVertexFontSize(int size)
159 {
160         _vertexFontSize = size;
161         _view->setVertexFontSize(_showVertexIDs ? size : 0);
162 }
163
164 void GraphTab::setEdgeFontSize(int size)
165 {
166         _edgeFontSize = size;
167         _view->setEdgeFontSize(_showEdgeValues ? size : 0);
168 }
169
170 void GraphTab::showVertexIDs(bool show)
171 {
172         _showVertexIDs = show;
173         _view->setVertexFontSize(show ? _vertexFontSize : 0);
174 }
175
176 void GraphTab::showEdgeValues(bool show)
177 {
178         _showEdgeValues = show;
179         _view->setEdgeFontSize(show ? _edgeFontSize : 0);
180 }
181
182 void GraphTab::colorizeEdges(bool colorize)
183 {
184         _coloredEdges = colorize;
185         if (colorize) {
186                 storeGraph();
187                 _graph->colorize();
188                 loadGraph();
189         } else
190                 _view->setEdgeColor(_edgeColor);
191 }
192
193 void GraphTab::setEdgeZValue(int value)
194 {
195         _view->setEdgeZValue(value);
196 }
197
198 void GraphTab::setInputEncoding(Encoding *encoding)
199 {
200         _inputEncoding = encoding;
201         if (_inputProvider)
202                 readGraph();
203 }
204
205 bool GraphTab::antialiasing()
206 {
207         return (_view->renderHints() & QPainter::Antialiasing)
208           ? true : false;
209 }
210
211 void GraphTab::setAntialiasing(bool value)
212 {
213         QPainter::RenderHints hints = _view->renderHints();
214         hints = (value) ? hints | QPainter::Antialiasing
215           : hints & ~QPainter::Antialiasing;
216
217         _view->setRenderHints(hints);
218 }
219
220
221 void GraphTab::loadGraph()
222 {
223         Vertex *vtx;
224         Edge *edg;
225         VertexItem *v;
226         EdgeItem *e;
227         Coordinates c;
228
229
230         /* Clear the old graph if any */
231         _view->clear();
232
233         /* Add the new graph */
234         _view->setDimensions(QPoint(_graph->dimensions().x(),
235           _graph->dimensions().y()));
236
237         for (size_t i = 0; i < _graph->vertex_size(); i++) {
238                 vtx = _graph->vertex(i);
239                 v = _view->addVertex();
240
241                 c = vtx->coordinates();
242                 v->setColor(QColor(vtx->color().rgb()));
243                 v->setSize(vtx->size());
244                 v->setText(QString::fromStdWString(vtx->text()));
245                 v->setFontSize(vtx->fontSize());
246                 v->setPos(c.x(), c.y());
247         }
248
249         for (size_t i = 0; i < _graph->edge_size(); i++) {
250                 edg = _graph->edge(i);
251                 e = _view->addEdge(edg->src()->id(), edg->dst()->id());
252
253                 e->setColor(QColor(edg->color().rgb()));
254                 e->setSize(edg->size());
255                 e->setText(QString::fromStdWString(edg->text()));
256                 e->setFontSize(edg->fontSize());
257                 e->setZValue(edg->zValue());
258         }
259 }
260
261 void GraphTab::storeGraph()
262 {
263         Vertex *vtx;
264         Edge *edg;
265         VertexItem *v;
266         EdgeItem *e;
267         QPointF pos;
268
269
270         /* Clear the old graph if any */
271         _graph->clear();
272
273         /* Add the new graph */
274         _graph->setDimensions(Coordinates(_view->dimensions().x(),
275           _view->dimensions().y()));
276
277         for (int i = 0; i < _view->graphSize(); i++) {
278                 v = _view->vertex(i);
279                 pos = v->scenePos();
280                 vtx = _graph->addVertex();
281
282                 vtx->setCoordinates(Coordinates(pos.x(), pos.y()));
283                 vtx->setColor(v->color().rgb());
284                 vtx->setSize(v->size());
285                 vtx->setText(v->text().toStdWString());
286                 vtx->setFontSize(v->fontSize());
287         }
288
289         for (int i = 0; i < _view->graphSize(); i++) {
290                 for (int j = 0; j < i; j++) {
291                         if ((e = _view->edge(i, j))) {
292                                 edg = _graph->addEdge(_graph->vertex(i), _graph->vertex(j));
293
294                                 edg->setColor(e->color().rgb());
295                                 edg->setSize(e->size());
296                                 edg->setText(e->text().toStdWString());
297                                 edg->setFontSize(e->fontSize());
298                                 edg->setZValue(e->zValue());
299                         }
300                 }
301         }
302 }
303
304 void GraphTab::setGraphProperties()
305 {
306         _graph->setVertexColor(_vertexColor.rgb());
307         _graph->setVertexSize(_vertexSize);
308         if (_coloredEdges)
309                 _graph->colorize();
310         else
311                 _graph->setEdgeColor(_edgeColor.rgb());
312         _graph->setEdgeSize(_edgeSize);
313         _graph->setVertexFontSize(_showVertexIDs ? _vertexFontSize : 0);
314         _graph->setEdgeFontSize(_showEdgeValues ? _edgeFontSize : 0);
315
316         _graph->setDimensions(Coordinates(_dimensions.x(), _dimensions.y()));
317         _graph->randomize();
318 }