@@ -22,6 +22,7 @@ email : even.rouault at spatialys.com
22
22
#include " qgssqlstatement.h"
23
23
24
24
#include < QMessageBox>
25
+ #include < QKeyEvent>
25
26
26
27
#include < Qsci/qscilexer.h>
27
28
@@ -50,6 +51,8 @@ QgsSQLComposerDialog::QgsSQLComposerDialog( QWidget * parent, Qt::WindowFlags fl
50
51
mTableJoins ->installEventFilter ( this );
51
52
mWhereEditor ->installEventFilter ( this );
52
53
mOrderEditor ->installEventFilter ( this );
54
+ mTablesCombo ->view ()->installEventFilter ( this );
55
+
53
56
54
57
connect ( mButtonBox ->button ( QDialogButtonBox::Reset ), SIGNAL ( clicked () ),
55
58
this , SLOT ( reset () ) );
@@ -128,7 +131,56 @@ QgsSQLComposerDialog::~QgsSQLComposerDialog()
128
131
bool QgsSQLComposerDialog::eventFilter ( QObject *obj, QEvent *event )
129
132
{
130
133
if ( event->type () == QEvent::FocusIn )
131
- mFocusedObject = obj;
134
+ {
135
+ if ( obj == mTablesCombo ->view () )
136
+ lastSearchedText.clear ();
137
+ else
138
+ mFocusedObject = obj;
139
+ }
140
+
141
+ // Custom search in table combobox
142
+ if ( event->type () == QEvent::KeyPress && obj == mTablesCombo ->view () )
143
+ {
144
+ QString currentString = (( QKeyEvent* )event )->text ();
145
+ if ( !currentString.isEmpty () && (( currentString[0 ] >= ' a' && currentString[0 ] <= ' z' ) ||
146
+ ( currentString[0 ] >= ' A' && currentString[0 ] <= ' Z' ) ||
147
+ ( currentString[0 ] >= ' 0' && currentString[0 ] <= ' 9' ) ||
148
+ currentString[0 ] == ' :' || currentString[0 ] == ' _' || currentString[0 ] == ' ' ||
149
+ currentString[0 ] == ' (' || currentString[0 ] == ' )' ) )
150
+ {
151
+ // First attempt is concatenation of existing search text
152
+ // Second attempt is just the new character
153
+ int attemptCount = ( lastSearchedText.isEmpty () ) ? 1 : 2 ;
154
+ for ( int attempt = 0 ; attempt < attemptCount; attempt ++ )
155
+ {
156
+ if ( attempt == 0 )
157
+ lastSearchedText += currentString;
158
+ else
159
+ lastSearchedText = currentString;
160
+
161
+ // Find the string that contains the searched text, and in case
162
+ // of several matches, pickup the one where the searched text is the
163
+ // most at the beginning.
164
+ int iBestCandidate = 0 ;
165
+ int idxInTextOfBestCandidate = 1000 ;
166
+ for ( int i = 1 ; i < mTablesCombo ->count (); i++ )
167
+ {
168
+ int idxInText = mTablesCombo ->itemText ( i ).indexOf ( lastSearchedText, Qt::CaseInsensitive );
169
+ if ( idxInText >= 0 && idxInText < idxInTextOfBestCandidate )
170
+ {
171
+ iBestCandidate = i;
172
+ idxInTextOfBestCandidate = idxInText;
173
+ }
174
+ }
175
+ if ( iBestCandidate > 0 )
176
+ {
177
+ mTablesCombo ->view ()->setCurrentIndex ( mTablesCombo ->model ()->index ( 0 , 0 ).sibling ( iBestCandidate, 0 ) );
178
+ return true ;
179
+ }
180
+ }
181
+ lastSearchedText.clear ();
182
+ }
183
+ }
132
184
133
185
return QObject::eventFilter ( obj, event );
134
186
}
0 commit comments