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
|
/**********************************************************************
** Copyright (C) 2001 Trolltech AS. All rights reserved.
**
** This file is part of TQt Designer.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding valid TQt Enterprise Edition or TQt Professional Edition
** licenses may use this file in accordance with the TQt Commercial License
** Agreement provided with the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
** information about TQt Commercial License Agreements.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
#include "qcompletionedit.h"
#include <tqlistbox.h>
#include <tqsizegrip.h>
#include <tqapplication.h>
#include <tqvbox.h>
QCompletionEdit::QCompletionEdit( TQWidget *parent, const char *name )
: TQLineEdit( parent, name ), aAdd( FALSE ), caseSensitive( FALSE )
{
popup = new TQVBox( 0, 0, WType_Popup );
popup->setFrameStyle( TQFrame::Box | TQFrame::Plain );
popup->setLineWidth( 1 );
popup->hide();
listbox = new TQListBox( popup );
listbox->setFrameStyle( TQFrame::NoFrame );
listbox->setLineWidth( 1 );
listbox->installEventFilter( this );
listbox->setHScrollBarMode( TQScrollView::AlwaysOn );
listbox->setVScrollBarMode( TQScrollView::AlwaysOn );
listbox->setCornerWidget( new TQSizeGrip( listbox, "completion sizegrip" ) );
connect( this, TQT_SIGNAL( textChanged( const TQString & ) ),
this, TQT_SLOT( textDidChange( const TQString & ) ) );
popup->setFocusProxy( listbox );
installEventFilter( this );
}
bool QCompletionEdit::autoAdd() const
{
return aAdd;
}
TQStringList QCompletionEdit::completionList() const
{
return compList;
}
void QCompletionEdit::setCompletionList( const TQStringList &l )
{
compList = l;
}
void QCompletionEdit::setAutoAdd( bool add )
{
aAdd = add;
}
void QCompletionEdit::textDidChange( const TQString &text )
{
if ( text.isEmpty() ) {
popup->close();
return;
}
updateListBox();
placeListBox();
}
void QCompletionEdit::placeListBox()
{
if ( listbox->count() == 0 ) {
popup->close();
return;
}
popup->resize( TQMAX( listbox->sizeHint().width() + listbox->verticalScrollBar()->width() + 4, width() ),
listbox->sizeHint().height() + listbox->horizontalScrollBar()->height() + 4 );
TQPoint p( mapToGlobal( TQPoint( 0, 0 ) ) );
if ( p.y() + height() + popup->height() <= TQApplication::desktop()->height() )
popup->move( p.x(), p.y() + height() );
else
popup->move( p.x(), p.y() - listbox->height() );
popup->show();
listbox->setCurrentItem( 0 );
listbox->setSelected( 0, TRUE );
setFocus();
}
void QCompletionEdit::updateListBox()
{
listbox->clear();
if ( compList.isEmpty() )
return;
for ( TQStringList::Iterator it = compList.begin(); it != compList.end(); ++it ) {
if ( caseSensitive && (*it).left( text().length() ) == text() ||
!caseSensitive && (*it).left( text().length() ).lower() == text().lower() )
listbox->insertItem( *it );
}
}
bool QCompletionEdit::eventFilter( TQObject *o, TQEvent *e )
{
if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(popup) || TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(listbox) || TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(listbox->viewport()) ) {
if ( e->type() == TQEvent::KeyPress ) {
TQKeyEvent *ke = (TQKeyEvent*)e;
if ( ke->key() == Key_Enter || ke->key() == Key_Return || ke->key() == Key_Tab ) {
if ( ke->key() == Key_Tab && listbox->count() > 1 &&
listbox->currentItem() < (int)listbox->count() - 1 ) {
listbox->setCurrentItem( listbox->currentItem() + 1 );
return TRUE;
}
popup->close();
setFocus();
blockSignals( TRUE );
setText( listbox->currentText() );
blockSignals( FALSE );
emit chosen( text() );
return TRUE;
} else if ( ke->key() == Key_Left || ke->key() == Key_Right ||
ke->key() == Key_Up || ke->key() == Key_Down ||
ke->key() == Key_Home || ke->key() == Key_End ||
ke->key() == Key_Prior || ke->key() == Key_Next ) {
return FALSE;
} else if ( ke->key() == Key_Escape ) {
popup->close();
setFocus();
} else if ( ke->key() != Key_Shift && ke->key() != Key_Control &&
ke->key() != Key_Alt ) {
updateListBox();
if ( listbox->count() == 0 || text().length() == 0 ) {
popup->close();
setFocus();
}
TQApplication::sendEvent( this, e );
return TRUE;
}
} else if ( e->type() == TQEvent::MouseButtonDblClick ) {
popup->close();
setFocus();
blockSignals( TRUE );
setText( listbox->currentText() );
blockSignals( FALSE );
emit chosen( text() );
return TRUE;
}
} else if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(this) ) {
if ( e->type() == TQEvent::KeyPress ) {
TQKeyEvent *ke = (TQKeyEvent*)e;
if ( ke->key() == Key_Up ||
ke->key() == Key_Down ||
ke->key() == Key_Prior ||
ke->key() == Key_Next ||
ke->key() == Key_Return ||
ke->key() == Key_Enter ||
ke->key() == Key_Tab ||
ke->key() == Key_Escape ) {
TQApplication::sendEvent( listbox, e );
return TRUE;
}
}
}
return TQLineEdit::eventFilter( o, e );
}
void QCompletionEdit::addCompletionEntry( const TQString &entry )
{
if ( compList.find( entry ) == compList.end() ) {
compList << entry;
compList.sort();
}
}
void QCompletionEdit::removeCompletionEntry( const TQString &entry )
{
TQStringList::Iterator it = compList.find( entry );
if ( it != compList.end() )
compList.remove( it );
}
void QCompletionEdit::setCaseSensitive( bool b )
{
caseSensitive = b;
}
bool QCompletionEdit::isCaseSensitive() const
{
return caseSensitive;
}
void QCompletionEdit::clear()
{
TQLineEdit::clear();
compList.clear();
}
|