summaryrefslogtreecommitdiffstats
path: root/src/searchlist.cpp
blob: 38443f228969be35bda0c481b83f21d4f18bb300 (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
/***************************************************************************
 *
 * 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 <tqheader.h>
#include "searchlist.h"

/**
 * Intercepting additional key events of TQLineEdit to browse the list
 * @param	pKey	The pressed key event
 */
void SearchLineEdit::keyPressEvent(TQKeyEvent* pKey) 
{
	switch(pKey->key()) {
	case TQt::Key_Up:
	case TQt::Key_Down:
	case TQt::Key_PageUp:
	case TQt::Key_PageDown:
		emit keyPressed(pKey); 
		break;
	
	default:
		TQLineEdit::keyPressEvent(pKey);
		break;
	}
}
	
/**
 * Class constructor.
 * @param	pParent	Owner list view widget
 */
ListToolTip::ListToolTip(SearchList* pParent) : 
	TQToolTip(pParent->getList()->viewport()), 
	m_pList(pParent)
{
}

/**
 * Displays a tool-tip according to the current location of the mouse
 * pointer.
 * @param	pt	The mouse pointer coordinates
 */
void ListToolTip::maybeTip(const TQPoint& pt)
{
	TQString str;
	TQListView* pList;
	TQListViewItem* pItem;
	
	// Get the item at the given point
	pList = m_pList->getList();
	pItem = pList->itemAt(pt);
	if (pItem == NULL)
		return;

	// Get the tip string for this item
	if (!m_pList->getTip(pItem, str))
		return;

	// Get the bounding rectangle of the item
	const TQRect rcItem = pList->itemRect(pItem);
	if (!rcItem.isValid())
		return;

	// Get the header coordinates
	const TQRect rcHead = pList->header()->rect();
	if (!rcHead.isValid())
		return;

	// Calculate the tool-tip rectangle
	TQRect rcCell(rcHead.left(), rcItem.top(), rcItem.width(), rcItem.height());

	// Display the tool-tip
	tip(rcCell, str);
}

/**
 * Class constructor.
 * @param	nSearchCol	The list column on which to perform string look-ups
 * @param	pParent		The parent widget
 * @param	szName		The widget's name
 */
SearchList::SearchList(int nSearchCol, TQWidget* pParent, const char* szName) :
	TQVBox(pParent, szName),
	m_nSearchCol(nSearchCol)
{
	// Create the child widgets
	m_pEdit = new SearchLineEdit(this);
	m_pList = new TQListView(this);
	
	// Set up the tooltip generator
	TQToolTip::remove(m_pList);
	m_pToolTip = new ListToolTip(this);
	
	connect(m_pEdit, TQ_SIGNAL(textChanged(const TQString&)), this,
		TQ_SLOT(slotFindItem(const TQString&)));
	connect(m_pList, TQ_SIGNAL(doubleClicked(TQListViewItem*)), this,
		TQ_SLOT(slotItemSelected(TQListViewItem*)));
	connect(m_pList, TQ_SIGNAL(returnPressed(TQListViewItem*)), this,
		TQ_SLOT(slotItemSelected(TQListViewItem*)));
	connect(m_pEdit, TQ_SIGNAL(returnPressed()), this,
		TQ_SLOT(slotItemSelected()));
	connect(m_pEdit, TQ_SIGNAL(keyPressed(TQKeyEvent*)), this,
		TQ_SLOT(slotKeyPressed(TQKeyEvent*)));
}

/**
 * Class destructor.
 */
SearchList::~SearchList()
{
	delete m_pToolTip;
}

/**
 * Sets the keyboad focus to the search box.
 */
void SearchList::slotSetFocus()
{
	m_pEdit->setFocus();
}

/**
 * Selects a list item whose string begins with the text entered in the edit
 * widget.
 * This slot is connected to the textChanged() signal of the line edit widget.
 * @param	sText	The new text in the edit widget
 */
void SearchList::slotFindItem(const TQString& sText)
{
	TQListViewItem* pItem;
	
	// Try to find an item that contains this text
	// Priority to exactly matched, 
	// then try to find line begins with the text,
	// and if not found, then try to find the line contains the text
	pItem = m_pList->findItem(sText, m_nSearchCol, 
		ExactMatch | BeginsWith | Contains);

	// Select this item
	if (pItem != 0) {
		m_pList->setSelected(pItem, true);
		m_pList->ensureItemVisible(pItem);
	}
}

/**
 * Lets inheriting classes process an item selection made through the list 
 * widget.
 * This slot is connected to the doubleClicked() and returnPressed()
 * signals of the list widget.
 */
void SearchList::slotItemSelected(TQListViewItem* pItem)
{
	processItemSelected(pItem);
	m_pEdit->setText("");
}

/**
 * Lets inheriting classes process an item selection made through the edit 
 * widget.
 * This slot is connected to the returnPressed() signal of the edit widget.
 */
void SearchList::slotItemSelected()
{
	TQListViewItem* pItem;

	if ((pItem = m_pList->selectedItem()) != NULL) {
		m_pEdit->setText(pItem->text(m_nSearchCol));
		processItemSelected(pItem);
	}
	
	m_pEdit->setText("");
}

#define SEARCH_MATCH(pItem) \
	pItem->text(m_nSearchCol).startsWith(m_pEdit->text())

/**
 * Sets a new current item based on key events in the edit box.
 * This slot is connected to the keyPressed() signal of the edit widget.
 * @param	pKey	The key evant passed by the edit box
 */
void SearchList::slotKeyPressed(TQKeyEvent* pKey)
{
	TQListViewItem* pItem, * pNewItem;
	int nPageSize, nPos;

	// Select the current item, or the first one if there is no current item
	pItem = m_pList->currentItem();
		
	// Set a new current item based on the pressed key
	switch (pKey->key()) {
	case  TQt::Key_Up:
		if (pItem) {
			for (pNewItem = pItem->itemAbove(); 
				pNewItem && !SEARCH_MATCH(pNewItem);
				pNewItem = pNewItem->itemAbove());
				
			if (pNewItem)
				pItem = pNewItem;
		}
		break;
		
	case  TQt::Key_Down:
		if (pItem) {
			for (pNewItem = pItem->itemBelow(); 
				pNewItem && !SEARCH_MATCH(pNewItem);
				pNewItem = pNewItem->itemBelow());
				
			if (pNewItem)
				pItem = pNewItem;
		}
		break;
	
	case  TQt::Key_PageUp:
		nPageSize = m_pList->visibleHeight() / pItem->height();
		for (nPos = 0; 
			pItem && pItem->itemAbove() && (nPos < nPageSize); 
			nPos++)
			pItem = pItem->itemAbove();
		break;
		
	case  TQt::Key_PageDown:
		nPageSize = m_pList->visibleHeight() / pItem->height();
		for (nPos = 0; 
			pItem && pItem->itemBelow() && (nPos < nPageSize); 
			nPos++)
			pItem = pItem->itemBelow();
		break;
	
	default:
		pKey->ignore();
		return;
	}

	// Select the first item if no other item was selected
	if (pItem == NULL)
		pItem = m_pList->firstChild();
		
	// Select the new item
	if (pItem) {
		m_pList->setSelected(pItem, true);
		m_pList->ensureItemVisible(pItem);
	}
}

#include "searchlist.moc"