summaryrefslogtreecommitdiffstats
path: root/src/graphwidget.h
blob: 095b45b023b921694423634f9f5977b2607f5cee (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/***************************************************************************
 *
 * Copyright (C) 2005 Elad Lahav (elad_lahav@users.sourceforge.net)
 *
 * 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.
 *
 ***************************************************************************/

#ifndef GRAPHWIDGET_H
#define GRAPHWIDGET_H

#include <tqcanvas.h>
#include <tqpopupmenu.h>
#include <tqdict.h>
#include "cscopefrontend.h"
#include "graphnode.h"
#include "dotfrontend.h"

class ProgressDlg;

/**
 * A widget that displays call tree graphs generated by graphviz.
 * This class is based on a TQCanvasView widget, and displays two types of
 * canvas items: GraphNode, which draws the name of a function inside a
 * polygon, and ArrowEdge, which is a directed graph edge shaped like an
 * arrow.
 * The call tree graph is populated using the addNode() method. The first
 * call creates a root node. Subsequent calls add nodes which represent either
 * functions called by a previously inserted node, or that are calling such
 * a node. A directed edge is created to depict the relationship between a
 * node and its parent.
 * Drawing is done through the embedded Agraph_t object (a graph, as defined
 * by the graphviz libraray). When the draw() method is called, the graph is
 * layed out in memory. The class then uses the CodeGenerator class to
 * obtain a set of instructions on how to actually draw the graph. These
 * instructions are used to create the appropriate canvas items.
 * An application _must_ call GraphWidget::init() before attempting to use
 * this class. It should also call GraphWidget::fini() when it no longer needs
 * any of these widgets.
 * @author Elad Lahav
 */
class GraphWidget : public TQCanvasView
{
	TQ_OBJECT
	
public:
	GraphWidget(TQWidget* pParent = 0, const char* szName = 0);
	~GraphWidget();
	
	/**
	 * Information on a function call, as produced by a Cscope query.
	 * This structure is used for adding calls to the graph.
	 * @see addCall()
	 */
	struct CallData
	{
		/** The name of the calling function. */
		TQString m_sCaller;
		
		/** The name of the called function. */
		TQString m_sCallee;
		
		/** Path of the file in which the call appears. */
		TQString m_sFile;
		
		/** The line number of the call. */
		TQString m_sLine;
		
		/** The call's text. */
		TQString m_sText;
	};

	/** Graph orientation values. */
	enum Orientation { Portrait, Landscape };

	void setRoot(const TQString&);
	GraphNode* addNode(const TQString&, bool bMultiCall = false);
	void addCall(const CallData&);
	void addMultiCall(const TQString&, bool);
	void draw();
	void save(FILE*);
	void save(const TQString&);
	void zoom(bool);
	void setZoom(double);
	void rotate();
	TQString getTip(const TQPoint&, TQRect&);
	
	void resize(int, int);
	void drawNode(const TQString&, const TQRect&);
	void drawEdge(const TQString&, const TQString&, const TQPointArray&);
	
	/**
	 * Adjusts the maximal number of calling/called functions shown for
	 * every node (@see m_nMaxNodeDegree).
	 * @param	nMaxNodeDegree	The new value to set
	 */
	void setMaxNodeDegree(int nMaxNodeDegree) { m_nMaxNodeDegree =
		nMaxNodeDegree; }
	
	static void setArrowInfo(int, int);
	
signals:
	/**
	 * Emitted when the user makes a request to view the contents of a
	 * location in the source code.
	 * This can be the location of a call, the definition of a function,
	 * etc.
	 * @param	sPath	The full path of the file to show
	 * @param	nLine	The line number in this file
	 */
	void lineRequested(const TQString& sPath, uint nLine);
	
protected:
	virtual void drawContents(TQPainter*, int, int, int, int);
	virtual void contentsMousePressEvent(TQMouseEvent*);
	
private:
	/** The graph is stored as a map of nodes indexed by their names. 
		Each node holds a list of outgoing edges. */
	TQDict<GraphNode> m_dictNodes;
	
	/** A Cscope process to use for running queries. */
	CscopeFrontend* m_pCscope;
	
	/** Displays query progress information. */
	CscopeProgress m_progress;
	
	/** A Dot process used to draw the graph. */
	DotFrontend m_dot;
	
	/** Remembers the function the was last queried for calling/called
		functions. */
	TQString m_sQueriedFunc;
	
	/** Remembers whether the last query was for calling or called
		functions. */
	bool m_bCalled;
	
	/** The node over which the popup menu has been invoked. */
	TQCanvasPolygonalItem* m_pMenuItem;
	
	/** A popup menu that appears when a node is right-clicked. */
	TQPopupMenu* m_pNodePopup;
	
	/** A popup menu that appears when a node is right-clicked. */
	TQPopupMenu* m_pMultiCallPopup;
	
	/** A popup menu that appears when an edge is right-clicked. */
	TQPopupMenu* m_pEdgePopup;

	/** The zoom factor for the graph. */
	double m_dZoom;
	
	/** Maximal number of in/out edges per node. If this number is exceeded,
		the graph shows a single "multi-call" node. */
	int m_nMaxNodeDegree;
	
	/** Holds information used to draw arrow heads. */
	static ArrowInfo s_ai;
	
	/** Used for generating unique names for multi-call nodes. */
	uint m_nMultiCallNum;
	
	/** Holds the path of the temporary dot file used for drawing the graph. */
	TQString m_sDrawFilePath;
	
	/** Allows lengthy drawing operations to be cancelled. */
	ProgressDlg* m_pProgressDlg;
	
	void write(TQTextStream&, const TQString&, const TQString&, bool);
	void removeEdges(GraphNode*, bool);
	void removeDisconnected(GraphNode*);
	void showNodeMenu(GraphNode*, const TQPoint&);
	void showEdgeMenu(GraphEdge*, const TQPoint&);
	
private slots:
	void slotDotFinished();
	void slotDataReady(FrontendToken*);
	void slotProgress(int, int);
	void slotFinished(uint);
	void slotAborted();
	void slotShowCalled();
	void slotListCalled();
	void slotHideCalled();
	void slotShowCalling();
	void slotListCalling();
	void slotHideCalling();
	void slotFindDef();
	void slotRemoveNode();
	void slotMultiCallDetails();
	void slotOpenCall();
};

#endif