summaryrefslogtreecommitdiffstats
path: root/src/graphnode.cpp
blob: 28a219dbf4dca16ff28b35b83707707ac72e18af (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/***************************************************************************
 *
 * Copyright (C) 2005 Elad Lahav ([email protected])
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ***************************************************************************/

#include <qpainter.h>
#include <qfontmetrics.h>
#include "graphnode.h"

int GraphNode::RTTI = 1001;

/**
 * Class constructor.
 * @param	pCanvas		The owner canvas
 * @param	sFunc		The node's function
 * @param	bMultiCall	Whether this node represents multiple calls
 */
GraphNode::GraphNode(QCanvas* pCanvas, const QString& sFunc, bool bMultiCall) : 
	QCanvasPolygon(pCanvas),
	m_sFunc(sFunc),
	m_bMultiCall(bMultiCall),
	m_bDfsFlag(false)
{
	// Every node deletes its out-edges only
	m_dictOutEdges.setAutoDelete(true);
}

/**
 * Class destructor.
 */
GraphNode::~GraphNode()
{
}

/**
 * Finds an edge leaving this node and reaching the given node.
 * If such an edge does not exist, a new one is created.
 * @param	pTail	The destination node
 * @return	The edge that ends at the given node
 */
GraphEdge* GraphNode::addOutEdge(GraphNode* pTail)
{
	GraphEdge* pEdge;
	
	// Look for the edge
	if ((pEdge = m_dictOutEdges.find(pTail->getFunc())) == NULL) {
		// Create a new edge
		pEdge = new GraphEdge(canvas(), this, pTail);
		m_dictOutEdges.insert(pTail->getFunc(), pEdge);
		pTail->m_dictInEdges.replace(m_sFunc, pEdge);
	}
	
	// Return the new/constructed edge
	return pEdge;
}

/**
 * Performs a weak depth-first-search on the graph.
 * The search continues along all edges, both incoming and outgoing.
 */
void GraphNode::dfs()
{
	// Stop if this node is already marked
	if (m_bDfsFlag)
		return;
		
	// Mark the node as visited
	m_bDfsFlag = true;
		
	// Continue along outgoing edges
	QDictIterator<GraphEdge> itrOut(m_dictOutEdges);
	for (; itrOut.current(); ++itrOut)
		(*itrOut)->getTail()->dfs();
		
	// Continue along incoming edges
	QDictIterator<GraphEdge> itrIn(m_dictInEdges);
	for (; itrIn.current(); ++itrIn)
		(*itrIn)->getHead()->dfs();
}
	
/**
 * Deletes all outgoing edges.
 * Uses the auto-delete property of the dictionary.
 */
void GraphNode::removeOutEdges()
{
	m_dictOutEdges.clear();
}

/**
 * Deletes all incoming edges.
 * To avoid double deletions, the function lets the head node of the edge remove
 * it.
 */
void GraphNode::removeInEdges()
{
	QDictIterator<GraphEdge> itr(m_dictInEdges);
	
	// Delete edges through their head nodes
	for (; itr.current(); ++itr)
		(*itr)->getHead()->m_dictOutEdges.remove(m_sFunc);
	
	// remove edges from the local dictionary (will not delete them)
	m_dictInEdges.clear();
}

/**
 * Returns the first found node connected to this one.
 * This function is used with multi-call nodes for retrieving the parent node.
 * @param	pNode
 * @param	bCalled
 */
void GraphNode::getFirstNeighbour(GraphNode*& pNode, bool& bCalled)
{
	QDictIterator<GraphEdge> itrIn(m_dictInEdges);
	QDictIterator<GraphEdge> itrOut(m_dictOutEdges);
	
	if (itrIn.current()) {
		pNode = itrIn.current()->getHead();
		bCalled = false;
	}
	else if (itrOut.current()) {
		pNode = itrOut.current()->getTail();
		bCalled = true;
	}
	else {
		pNode = NULL;
	}
}

/**
 * Modifies the bounding rectangle of the node.
 * @param	rect	The new coordinates to set
 */
void GraphNode::setRect(const QRect& rect)
{
	QPointArray arr(4);
	
	m_rect = rect;
	
	arr.setPoint(0, m_rect.topLeft());
	arr.setPoint(1, m_rect.topRight());
	arr.setPoint(2, m_rect.bottomRight());
	arr.setPoint(3, m_rect.bottomLeft());
	setPoints(arr);
}

/**
 * Draws the node.
 * @param	painter	Used for drawing the item on the canvas view
 */
void GraphNode::drawShape(QPainter& painter)
{
	const QPen& pen = painter.pen();
	const QFont& font = painter.font();
	
	// Draw the rectangle
	painter.setPen(QPen(Qt::black));
	painter.drawRect(m_rect);
	
	// Draw the text
	painter.setPen(pen);
	painter.setFont(m_font);
	if (m_bMultiCall)
		painter.drawText(m_rect, Qt::AlignCenter, "...");
	else
		painter.drawText(m_rect, Qt::AlignCenter, m_sFunc);
	
	painter.setFont(font);
}