comparison grapher.cpp @ 44:bed7ab59f62e

* A snatched bit of work on graph layout
author Chris Cannam
date Tue, 09 Nov 2010 17:51:12 +0000
parents
children 4286836bb3c9
comparison
equal deleted inserted replaced
43:c32067cd19f8 44:bed7ab59f62e
1
2 #include "grapher.h"
3
4 #include <QSet>
5 #include <QMap>
6
7 #include <iostream>
8
9 typedef QSet<int> ColumnSet;
10 typedef QMap<int, ColumnSet> GridAlloc;
11 typedef QMap<QString, Changeset *> IdChangesetMap;
12 typedef QSet<Changeset *> ChangesetSet;
13
14 ChangesetItem *
15 layout(Changeset *cs,
16 IdChangesetMap idCsetMap,
17 ChangesetItemMap items,
18 GridAlloc &alloc,
19 ChangesetSet &handled)
20 {
21 if (!cs) {
22 throw std::string("Null Changeset");
23 }
24 if (!items.contains(cs)) {
25 throw std::string("Changeset not in item map");
26 }
27 ChangesetItem *item = items[cs];
28 if (handled.contains(cs)) {
29 return item;
30 }
31 int row = 0;
32 int col = 0;
33 if (!cs->parents().empty()) {
34 bool haveRow = false;
35 foreach (QString parentId, cs->parents()) {
36 if (parentId == "") continue; //!!!
37 std::cerr << "recursing to parent \"" << parentId.toStdString() << "\" of \"" << cs->id().toStdString() << "\"" << std::endl;
38 ChangesetItem *parentItem =
39 layout(idCsetMap[parentId],
40 idCsetMap,
41 items,
42 alloc,
43 handled);
44 if (!haveRow || parentItem->row() < row) {
45 row = parentItem->row();
46 haveRow = true;
47 }
48 col += parentItem->column();
49 }
50 col /= cs->parents().size();
51 row = row - 1;
52 while (alloc[row].contains(col)) {
53 if (col > 0) col = -col;
54 else col = -col + 1;
55 }
56 alloc[row].insert(col);
57 }
58 item->setColumn(col);
59 item->setRow(row);
60 item->setX(col * 100);
61 item->setY(row * 100);
62 handled.insert(cs);
63 return item;
64 }
65
66 void
67 Grapher::layout(Changesets csets, ChangesetItemMap items)
68 {
69 IdChangesetMap idCsetMap;
70 foreach (Changeset *cs, csets) {
71 std::cerr << cs->id().toStdString() << std::endl;
72 if (cs->id() == "") {
73 throw std::string("Changeset has no ID");
74 }
75 if (idCsetMap.contains(cs->id())) {
76 throw std::string("Changeset ID is already in map");
77 }
78 idCsetMap[cs->id()] = cs;
79 }
80
81 GridAlloc alloc;
82 ChangesetSet handled;
83 foreach (Changeset *cs, csets) {
84 ::layout(cs, idCsetMap, items, alloc, handled);
85 }
86 }
87