Skip to content

Commit 773d613

Browse files
committedJun 7, 2016
SQL composer dialog: improve search in table combobox
1 parent 5301a97 commit 773d613

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed
 

‎src/gui/qgssqlcomposerdialog.cpp

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ email : even.rouault at spatialys.com
2222
#include "qgssqlstatement.h"
2323

2424
#include <QMessageBox>
25+
#include <QKeyEvent>
2526

2627
#include <Qsci/qscilexer.h>
2728

@@ -50,6 +51,8 @@ QgsSQLComposerDialog::QgsSQLComposerDialog( QWidget * parent, Qt::WindowFlags fl
5051
mTableJoins->installEventFilter( this );
5152
mWhereEditor->installEventFilter( this );
5253
mOrderEditor->installEventFilter( this );
54+
mTablesCombo->view()->installEventFilter( this );
55+
5356

5457
connect( mButtonBox->button( QDialogButtonBox::Reset ), SIGNAL( clicked() ),
5558
this, SLOT( reset() ) );
@@ -128,7 +131,56 @@ QgsSQLComposerDialog::~QgsSQLComposerDialog()
128131
bool QgsSQLComposerDialog::eventFilter( QObject *obj, QEvent *event )
129132
{
130133
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+
}
132184

133185
return QObject::eventFilter( obj, event );
134186
}

‎src/gui/qgssqlcomposerdialog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ class GUI_EXPORT QgsSQLComposerDialog : public QDialog, private Ui::QgsSQLCompos
176176
QMap<QString, QString> mapColumnEntryTextToName;
177177
QMap<QString, QString> mapSpatialPredicateEntryTextToName;
178178
QMap<QString, QString> mapFunctionEntryTextToName;
179+
QString lastSearchedText;
180+
179181

180182
void loadTableColumns( const QString& table );
181183
void functionCurrentIndexChanged( QComboBox* combo,

0 commit comments

Comments
 (0)
Please sign in to comment.