-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgraphhandler.cpp
133 lines (99 loc) · 2.83 KB
/
graphhandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <cstdlib>
#include "graphhandler.h"
#include "qgraph.h"
#include "traceentry.h"
#include "common.h"
namespace Templar {
namespace {
inline QColor colorOfEntry(const TraceEntry& entry) {
return (entry.isBegin) ? QColor(common::colors::BEGIN)
: QColor(common::colors::END);
}
} // unnamed namespace
void GraphHandler::handleEvent(const TraceEntry &entry)
{
NodeId nodeId = entry.id;
changeGraph(nodeId);
QColor oldColor = colorOfNode(nodeId);
undoStack.push_back(std::make_pair(nodeId, oldColor));
QColor newColor = colorOfEntry(entry);
nodeColor[nodeId] = newColor;
theGraph->colorNode(QString::number(nodeId), newColor);
}
void GraphHandler::undoEvent()
{
if (undoStack.empty())
return;
UndoInfo undo = undoStack.back();
undoStack.pop_back();
NodeId nodeId = undo.first;
QColor color = undo.second;
changeGraph(nodeId);
nodeColor[nodeId] = color;
theGraph->colorNode(QString::number(nodeId), color);
}
void GraphHandler::reset()
{
undoStack.clear();
nodeColor.clear();
theGraph->getScene()->clear();
}
void GraphHandler::inspect(const TraceEntry &entry)
{
QGraphicsItem *item = theGraph->getNodeById(QString::number(entry.id));
theGraph->centerOn(item);
}
graph_t* GraphHandler::changeGraph(const NodeId& nodeName)
{
graph_t* graph = nodeToGraph[nodeName].data();
if (theGraph->getGraph() != graph) {
theGraph->renderGraph(gvc.data(), graph, "dot");
colorize();
}
return graph;
}
QColor GraphHandler::colorOfNode(const NodeId& nodeName)
{
if (nodeColor.contains(nodeName)) {
return nodeColor[nodeName];
} else {
return QColor(common::colors::DEFAULT);
}
}
void GraphHandler::colorize()
{
graph_t *graph = theGraph->getGraph();
for (node_t* node = agfstnode(graph); node != NULL; node = agnxtnode(graph, node)) {
NodeId ndId = std::atoi(agnameof(node));
theGraph->colorNode(agnameof(node), colorOfNode(ndId));
}
}
void GraphHandler::forward(const std::vector<TraceEntry> & entryVec)
{
bool follow = theGraph->follow(false);
for (unsigned int i = 0; i < entryVec.size() - 1; ++i)
{
const TraceEntry &entry = entryVec[i];
NodeId nodeId = entry.id;
undoStack.push_back(std::make_pair(nodeId, colorOfNode(nodeId)));
nodeColor[nodeId] = colorOfEntry(entry);
}
theGraph->follow(follow);
const TraceEntry &entry = entryVec.back();
NodeId nodeId = entry.id;
//removed unused: graph_t* graph = ;
changeGraph(nodeId);
colorize();
handleEvent(entryVec.back());
}
void GraphHandler::rewind(unsigned int count)
{
bool follow = theGraph->follow(false);
while (count-- > 1)
{
undoEvent();
}
theGraph->follow(follow);
undoEvent();
}
} // namespace Templar