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