summaryrefslogtreecommitdiffstats
path: root/src/calltreedlg.cpp
blob: b0619f493a16275578c4c281bde3749e75e926b3 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/***************************************************************************
 *
 * 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.
 *
 ***************************************************************************/

#include <tqfile.h>
#include <tqtoolbutton.h>
#include <tqbuttongroup.h>
#include <tqwidgetstack.h>
#include <tdelocale.h>
#include <tdefiledialog.h>
#include "calltreedlg.h"
#include "graphwidget.h"
#include "treewidget.h"
#include "kscopepixmaps.h"
#include "kscopeconfig.h"
#include "graphprefdlg.h"

/** The currently supported version of saved call-tree files. */
#define FILE_VERSION		5

/** Window flags for call-tree widgets. */
#define CALL_TREE_W_FLAGS \
	WStyle_Customize | \
	WStyle_NormalBorder | \
	WStyle_Title | \
	WDestructiveClose

/** File Name index for the file name generation */
int CallTreeDlg::s_nFileNameIndex = 0;

/**
 * Class constructor.
 * @param	pParent	The parent widget
 * @param	szName	The widget's name
 */
CallTreeDlg::CallTreeDlg(TQWidget* pParent, const char* szName) :
	CallTreeLayout(pParent, szName, CALL_TREE_W_FLAGS)
{
	// Set button pixmaps
	m_pCalledButton->setPixmap(GET_PIXMAP(CalledTree));
	m_pCallingButton->setPixmap(GET_PIXMAP(CallingTree));
	m_pGraphButton->setPixmap(GET_PIXMAP(CallGraph));
	m_pSaveButton->setPixmap(GET_PIXMAP(ButtonSaveAs));
	m_pZoomInButton->setPixmap(GET_PIXMAP(ButtonZoomIn));
	m_pZoomOutButton->setPixmap(GET_PIXMAP(ButtonZoomOut));
	m_pRotateButton->setPixmap(GET_PIXMAP(ButtonRotate));
	m_pPrefButton->setPixmap(GET_PIXMAP(ButtonPref));
	
	// Open the location of a call
	connect(m_pGraphWidget, SIGNAL(lineRequested(const TQString&, uint)),
		this, SIGNAL(lineRequested(const TQString&, uint)));
	connect(m_pCalledWidget, SIGNAL(lineRequested(const TQString&, uint)),
		this, SIGNAL(lineRequested(const TQString&, uint)));
	connect(m_pCallingWidget, SIGNAL(lineRequested(const TQString&, uint)),
		this, SIGNAL(lineRequested(const TQString&, uint)));
	
	m_pCallingWidget->setMode(TreeWidget::Calling);
	
	// Get the default view from KScope's configuration
	m_nDefView = Config().getDefGraphView();
}

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

/**
 * @param	sFunc	The function to use as the root of the call tree
 */
void CallTreeDlg::setRoot(const TQString& sFunc)
{
	m_sRoot = sFunc;
	
	// Generate unique file name to save call tree later
	m_sFileName = sFunc;
	m_sFileName.replace(' ', '_');
	m_sFileName += TQString::number(++s_nFileNameIndex);
	
	// Set the root item in all views
	m_pGraphWidget->setRoot(sFunc);
	m_pCalledWidget->setRoot(sFunc);
	m_pCallingWidget->setRoot(sFunc);
}

/**
 * Displays the dialogue.
 */
void CallTreeDlg::show()
{
	// Set the default view.
	m_pViewGroup->setButton(m_nDefView);
	m_pStack->raiseWidget(m_nDefView);
	slotViewChanged(m_nDefView);
	
	CallTreeLayout::show();
}

/**
 * Informs the call tree manager that this object should be removed from the
 * list of call tree dialogues.
 * The close event is received when the dialogue is explicitly closed by the
 * user. This dialogue will not appear when the project is reopened, and it
 * is therefore safe to delete the graph file at this point.
 * @param	pEvent	Information on the closing event 
 */
void CallTreeDlg::closeEvent(TQCloseEvent* pEvent)
{
	if (!m_sFilePath.isEmpty())
		TQFile::remove(m_sFilePath);
		
	emit closed(this);
	TQWidget::closeEvent(pEvent);
}

extern void yyinit(CallTreeDlg*, FILE*, Encoder*);
extern int yyparse();

/**
 * Restores a call tree from the given call tree file.
 * NOTE: The call tree file is deleted when loading is complete.
 * @param	sProjPath	The full path of the project directory
 * @param	sFileName	The name of the call tree file to load
 * @return	true if successful, false otherwise
 */
bool CallTreeDlg::load(const TQString& sProjPath, const TQString& sFileName)
{
	TQString sPath;
	FILE* pFile;
	int nVersion, nView, nResult;
	Encoder enc;
	
	// Create the full path name
	sPath = sProjPath + "/" + sFileName;
	
	// Open the file for reading
	pFile = fopen(sPath.latin1(), "r");
	if (pFile == NULL)
		return false;
		
	// Check file version
	if ((fscanf(pFile, "VERSION=%d\n", &nVersion) != 1) || 
		(nVersion != FILE_VERSION)) {
		fclose(pFile);
		return false;
	}
		
	// Get default view
	if ((fscanf(pFile, "View=%d\n", &nView) == 1) &&
		(nView >= 0) &&
		(nView <= 2)) {
		m_nDefView = nView;
	}
	
	// Read the call trees and the graph stored on this file
	yyinit(this, pFile, &enc);
	nResult = yyparse();
		 
	// Close the file
	fclose(pFile);
	
	// Check the result returned by the parser
	if (nResult != 0)
		return false;
	
	// Store the file name
	m_sFileName = sFileName;
	m_sFilePath = sPath;
	
	// Draw the graph
	m_pGraphWidget->draw();
	return true;
}

/**
 * Writes the contents of the call tree dialog to a call tree file.
 * This method is called for call trees before the owner project is
 * closed.
 * @param	sProjPath	The full path of the project directory
 */
void CallTreeDlg::store(const TQString& sProjPath)
{
	TQString sPath;
	FILE* pFile;
	
	// Create the full file path
	sPath = sProjPath + "/" + m_sFileName;
	m_sFilePath = sPath;
	
	// Open a file for writing (create if necessary)
	pFile = fopen(sPath.latin1(), "w+");
	if (pFile == NULL)
		return;
		
	// Write header
	fprintf(pFile, "VERSION=%d\n", FILE_VERSION);
	fprintf(pFile, "View=%d\n", m_pViewGroup->selectedId());
	
	// Save the contents of all widgets
	m_pCalledWidget->save(pFile);
	m_pCallingWidget->save(pFile);
	m_pGraphWidget->save(pFile);
	
	// Close the file
	fclose(pFile);
}

/**
 * Saves the graph to a dot file.
 * The user is prompted for a name to use for the file, and then graph
 * widget writes its information to this file (using the dot language).
 * This slot is connected to the clicked() signal of the "Save As..." button.
 */
void CallTreeDlg::slotSaveClicked()
{
	TQString sFile;
	
	// Prompt the user for a file name
	sFile = KFileDialog::getSaveFileName(":kscope");
	
	// Save the graph to a file (unless the user did not give a file name)
	if (!sFile.isEmpty())
		m_pGraphWidget->save(sFile);
}

/**
 * Increases the zoom factor of the graph.
 * This slot is connected to the clicked() signal of the "Zoom In" button.
 */
void CallTreeDlg::slotZoomInClicked()
{
	m_pGraphWidget->zoom(true);
	m_pGraphWidget->draw();
}

/**
 * Decreases the zoom factor of the graph.
 * This slot is connected to the clicked() signal of the "Zoom Out" button.
 */
void CallTreeDlg::slotZoomOutClicked()
{
	m_pGraphWidget->zoom(false);
	m_pGraphWidget->draw();
}

/**
 * Changes the graph's layout direction.
 * This slot is connected to the clicked() signal of the "Rotate" button.
 */
void CallTreeDlg::slotRotateClicked()
{
	m_pGraphWidget->rotate();
	m_pGraphWidget->draw();
}

/**
 * Opens the call graph preferences dialogue.
 * This slot is connected to the clicked() signal of the "Preferences" button.
 */
void CallTreeDlg::slotPrefClicked()
{
	GraphPrefDlg dlg(this);
	int nMaxNodeDegree;
	
	if (dlg.exec() == TQDialog::Accepted) {
		nMaxNodeDegree = dlg.getMaxNodeDegree();
		Config().setGraphMaxNodeDegree(nMaxNodeDegree);
		m_pGraphWidget->setMaxNodeDegree(nMaxNodeDegree);
	}
}

/**
 * Prepares the selected view.
 * This slot is called when the user chooses a different view through the
 * toggle buttons in the dialogue's toolbar.
 * @param	nView	Identifies the selected view
 */
void CallTreeDlg::slotViewChanged(int nView)
{
	switch (nView) {
	case 0:
		// Call graph
		setCaption(i18n("Call Graph"));
		m_pGraphGroup->setEnabled(true);
		m_pHelpLabel->setText(i18n("Right-click a function node or an arrow "
			"head for more options."));
		break;
		
	case 1:
		// Called functions tree
		setCaption(i18n("Called Functions Tree"));
		m_pGraphGroup->setEnabled(false);
		m_pHelpLabel->setText(i18n("Right-click a tree item for more "
			"options."));
		m_pCalledWidget->queryRoot();
		break;
		
	case 2:
		// Calling functions tree
		setCaption(i18n("Calling Functions Tree"));
		m_pGraphGroup->setEnabled(false);
		m_pHelpLabel->setText(i18n("Right-click a tree item for more "
			"options."));
		m_pCallingWidget->queryRoot();
		break;
	}
	
	Config().setDefGraphView(nView);
}

#include "calltreedlg.moc"