summaryrefslogtreecommitdiffstats
path: root/src/treewidget.cpp
blob: db0a26b3ce91f4aa45a80c12513f65bab91ca6ed (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
/***************************************************************************
 *
 * 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 "treewidget.h"
#include "queryviewdriver.h"

/**
 * Class constructor.
 * @param	pParent	Parent widget
 * @param	szName	The name of the widget
 */
TreeWidget::TreeWidget(TQWidget* pParent, const char* szName) :
	QueryView(pParent, szName),
	m_nQueryType(CscopeFrontend::Called)
{
	setRootIsDecorated(true);
	
	// Create a driver object
	m_pDriver = new QueryViewDriver(this, this);
	
	// Query a tree item when it is expanded for the first time
	connect(this, SIGNAL(expanded(TQListViewItem*)), this,
		SLOT(slotQueryItem(TQListViewItem*)));
}

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

/**
 * Determines the mode of the tree.
 * @param	mode	The new mode (@see Mode)
 */
void TreeWidget::setMode(Mode mode)
{
	m_nQueryType = (mode == Called) ? CscopeFrontend::Called :
		CscopeFrontend::Calling;
}

/**
 * Sets a new root item for the tree.
 * @param	sFunc	The name of the function to serve as root
 */
void TreeWidget::setRoot(const TQString& sFunc)
{
	TQListViewItem* pRoot;
	
	// Remove the current root, if any
	if ((pRoot = firstChild()) != NULL)
		delete pRoot;
	
	// Create a new root item
	pRoot = new TQListViewItem(this, sFunc);
	pRoot->setExpandable(true);
}

/**
 * Runs a query on the root item.
 */
void TreeWidget::queryRoot()
{
	TQListViewItem* pRoot;
	
	if ((pRoot = firstChild()) != NULL)
		slotQueryItem(pRoot);
}

/**
 * Stores the tree contents in the given file.
 * @param	pFile	An open file to write to
 */
void TreeWidget::save(FILE* pFile)
{
	TQTextStream str(pFile, IO_WriteOnly);
	TQListViewItem* pRoot;
	Encoder enc;
	
	if (m_nQueryType == CscopeFrontend::Called)
		str << "calltree {" << endl;
	else
		str << "callingtree {" << endl;

	// Write the tree to the file
	pRoot = firstChild();
	str << pRoot->text(0) << endl;
	str << '{' << endl;
	saveItems(pRoot->firstChild(), str, enc);
	str << '}' << endl;
	str << '}' << endl;
}

/**
 * Recursively writes tree items to a file.
 * Given an item, the method writes this item and all of its siblings.
 * Child items are written recursively.
 * @param	pItem	The first item to write
 * @param	str		An initialised text stream to use for writing
 * @param	enc		An encoder for free-text strings
 */
void TreeWidget::saveItems(TQListViewItem* pItem, TQTextStream& str, Encoder& enc)
{
	// Iterate over all items in this level
	for (; pItem != NULL; pItem = pItem->nextSibling()) {
		// Write function parameters
		str << pItem->text(0) << " [ "
			<< "kscope_file=\"" << pItem->text(1) << "\", "
			<< "kscope_line=" << pItem->text(2) << ", "
			<< "kscope_text=\"" << enc.encode(pItem->text(3)) << "\""
			<< "]" << endl;
		
		// Write child items
		str << "{" << endl;
		saveItems(pItem->firstChild(), str, enc);
		str << "}" << endl;
	}
}

/**
 * Creates a new tree item showing a query result record.
 * @param	sFunc	The name of the function
 * @param	sFile	The file path
 * @param	sLine	The line number in the above file
 * @param	sText	The line's text
 * @param	pParent	The parent for the new item
 */
void TreeWidget::addRecord(const TQString& sFunc, const TQString& sFile,
	const TQString& sLine, const TQString& sText, TQListViewItem* pParent)
{
	TQListViewItem* pItem;
	
	pItem = new QueryViewItem(pParent, m_pLastItem, 2);
	pItem->setText(0, sFunc);
	pItem->setText(1, sFile);
	pItem->setText(2, sLine);
	pItem->setText(3, sText);
	
	pItem->setExpandable(true);
	m_pLastItem = pItem;
}

/**
 * Called when a query running on the tree terminates.
 * If there were no results, the item becomes non-expandable.
 * NOTE: On top of its current behaviour, this function is required in order to
 * override the default behaviour, as specified by QueryView.
 * @param	nResults	Number of results
 * @param	pParent		The item for which the query was executed
 */
void TreeWidget::queryFinished(uint nResults, TQListViewItem* pParent)
{
	if (nResults == 0)
		pParent->setExpandable(false);
	else
		pParent->setOpen(true);
}

/**
 * Runs a query on the given item, unless it was queried before.
 * This slot is connected to the expanded() signal of the list view.
 * @param	pItem	The item to query
 */
void TreeWidget::slotQueryItem(TQListViewItem* pItem)
{
	// Do nothing if the item was already queried
	// An item has been queried if it has children or marked as non-expandable
	if (pItem->firstChild() != NULL || !pItem->isExpandable())
		return;
		
	// Run the query
	m_pDriver->query(m_nQueryType, pItem->text(0), true, pItem);
}

/**
 * Hides all descendant that do not meet the given search criteria.
 * This slot is connected to the search() signal of the QueryResultsMenu
 * object.
 * The search is incremental: only visible items are checked, so that a new
 * search goes over the results of the previous one.
 * @param	pParent	The parent item whose child are searched
 * @param	re		The pattern to search
 * @param	nCol	The list column to search in
 */
void TreeWidget::slotSearch(TQListViewItem* pParent, const TQRegExp& re, 
	int nCol)
{
	TQListViewItem* pItem;
	
	// Get the first child
	if (pParent != NULL)
		pItem = pParent->firstChild();
	else
		pItem = firstChild();
	
	// Iterate over all child items
	while (pItem != NULL) {
		// Filter visible items only
		if (pItem->isVisible() && re.search(pItem->text(nCol)) == -1)
			pItem->setVisible(false);
		
		// Search child items recursively
		slotSearch(pItem, re, nCol);
		
		pItem = pItem->nextSibling();
	}
}

/**
 * Makes all descendants of the given item visible.
 * This slot is connected to the showAll() signal of the QueryResultsMenu
 * object.
 */
void TreeWidget::slotShowAll(TQListViewItem* pParent)
{
	TQListViewItem* pItem;
	
	// Get the first child
	if (pParent != NULL)
		pItem = pParent->firstChild();
	else
		pItem = firstChild();
	
	// Iterate over all child items
	while (pItem != NULL) {
		pItem->setVisible(true);
		
		// Show child items recursively
		slotShowAll(pItem);
			
		pItem = pItem->nextSibling();
	}
}

#include "treewidget.moc"