Skip to content

Commit

Permalink
Added field name autocomplete to the query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
uclaros authored and nyalldawson committed Apr 19, 2020
1 parent 664e8da commit 86ac754
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 22 deletions.
7 changes: 7 additions & 0 deletions python/gui/auto_generated/qgscodeeditorsql.sip.in
Expand Up @@ -29,6 +29,13 @@ code autocompletion.
QgsCodeEditorSQL( QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsCodeEditorSQL
%End

void setFields( const QgsFields &fields );
%Docstring
Set field names to be added to the lexer API.

.. versionadded:: 3.14
%End

};
Expand Down
69 changes: 47 additions & 22 deletions src/gui/qgscodeeditorsql.cpp
Expand Up @@ -33,11 +33,11 @@ QgsCodeEditorSQL::QgsCodeEditorSQL( QWidget *parent )
setMarginVisible( false );
setFoldingVisible( true );
setAutoCompletionCaseSensitivity( false );
setSciLexerSQL();
initializeLexer();
}


void QgsCodeEditorSQL::setSciLexerSQL()
void QgsCodeEditorSQL::initializeLexer()
{
QHash< QString, QColor > colors;
if ( QgsApplication::instance()->themeName() != QStringLiteral( "default" ) )
Expand All @@ -56,25 +56,50 @@ void QgsCodeEditorSQL::setSciLexerSQL()
#endif
QColor defaultColor = colors.value( QStringLiteral( "sql/defaultFontColor" ), Qt::black );

QsciLexerSQL *sqlLexer = new QgsCaseInsensitiveLexerSQL( this );
sqlLexer->setDefaultFont( font );
sqlLexer->setDefaultColor( defaultColor );
sqlLexer->setDefaultPaper( colors.value( QStringLiteral( "sql/paperBackgroundColor" ), Qt::white ) );
sqlLexer->setFont( font, -1 );
mSqlLexer = new QgsCaseInsensitiveLexerSQL( this );
mSqlLexer->setDefaultFont( font );
mSqlLexer->setDefaultColor( defaultColor );
mSqlLexer->setDefaultPaper( colors.value( QStringLiteral( "sql/paperBackgroundColor" ), Qt::white ) );
mSqlLexer->setFont( font, -1 );
font.setBold( true );
sqlLexer->setFont( font, QsciLexerSQL::Keyword );

sqlLexer->setColor( defaultColor, QsciLexerSQL::Default );
sqlLexer->setColor( colors.value( QStringLiteral( "sql/commentFontColor" ), QColor( 142, 144, 140 ) ), QsciLexerSQL::Comment );
sqlLexer->setColor( colors.value( QStringLiteral( "sql/commentLineFontColor" ), QColor( 142, 144, 140 ) ), QsciLexerSQL::CommentLine );
sqlLexer->setColor( colors.value( QStringLiteral( "sql/numberFontColor" ), QColor( 200, 40, 41 ) ), QsciLexerSQL::Number );
sqlLexer->setColor( colors.value( QStringLiteral( "sql/keywordFontColor" ), QColor( 137, 89, 168 ) ), QsciLexerSQL::Keyword );
sqlLexer->setColor( colors.value( QStringLiteral( "sql/singleQuoteFontColor" ), QColor( 113, 140, 0 ) ), QsciLexerSQL::SingleQuotedString );
sqlLexer->setColor( colors.value( QStringLiteral( "sql/doubleQuoteFontColor" ), QColor( 234, 183, 0 ) ), QsciLexerSQL::DoubleQuotedString );
sqlLexer->setColor( colors.value( QStringLiteral( "sql/operatorFontColor" ), QColor( 66, 113, 174 ) ), QsciLexerSQL::Operator );
sqlLexer->setColor( colors.value( QStringLiteral( "sql/identifierFontColor" ), QColor( 62, 153, 159 ) ), QsciLexerSQL::Identifier );
sqlLexer->setColor( colors.value( QStringLiteral( "sql/QuotedIdentifierFontColor" ), Qt::black ), QsciLexerSQL::QuotedIdentifier );
sqlLexer->setColor( colors.value( QStringLiteral( "sql/QuotedOperatorFontColor" ), Qt::black ), QsciLexerSQL::QuotedOperator );

setLexer( sqlLexer );
mSqlLexer->setFont( font, QsciLexerSQL::Keyword );

mSqlLexer->setColor( defaultColor, QsciLexerSQL::Default );
mSqlLexer->setColor( colors.value( QStringLiteral( "sql/commentFontColor" ), QColor( 142, 144, 140 ) ), QsciLexerSQL::Comment );
mSqlLexer->setColor( colors.value( QStringLiteral( "sql/commentLineFontColor" ), QColor( 142, 144, 140 ) ), QsciLexerSQL::CommentLine );
mSqlLexer->setColor( colors.value( QStringLiteral( "sql/numberFontColor" ), QColor( 200, 40, 41 ) ), QsciLexerSQL::Number );
mSqlLexer->setColor( colors.value( QStringLiteral( "sql/keywordFontColor" ), QColor( 137, 89, 168 ) ), QsciLexerSQL::Keyword );
mSqlLexer->setColor( colors.value( QStringLiteral( "sql/singleQuoteFontColor" ), QColor( 113, 140, 0 ) ), QsciLexerSQL::SingleQuotedString );
mSqlLexer->setColor( colors.value( QStringLiteral( "sql/doubleQuoteFontColor" ), QColor( 234, 183, 0 ) ), QsciLexerSQL::DoubleQuotedString );
mSqlLexer->setColor( colors.value( QStringLiteral( "sql/operatorFontColor" ), QColor( 66, 113, 174 ) ), QsciLexerSQL::Operator );
mSqlLexer->setColor( colors.value( QStringLiteral( "sql/identifierFontColor" ), QColor( 62, 153, 159 ) ), QsciLexerSQL::Identifier );
mSqlLexer->setColor( colors.value( QStringLiteral( "sql/QuotedIdentifierFontColor" ), Qt::black ), QsciLexerSQL::QuotedIdentifier );
mSqlLexer->setColor( colors.value( QStringLiteral( "sql/QuotedOperatorFontColor" ), Qt::black ), QsciLexerSQL::QuotedOperator );

setLexer( mSqlLexer );
}

void QgsCodeEditorSQL::setFields( const QgsFields &fields )
{
mFieldNames.clear();

for ( const QgsField &field : fields )
{
mFieldNames << field.name();
}

updateApis();
}

void QgsCodeEditorSQL::updateApis()
{
mApis = new QsciAPIs( mSqlLexer );

for ( const QString &fieldName : qgis::as_const( mFieldNames ) )
{
mApis->add( fieldName );
}

mApis->prepare();
mSqlLexer->setAPIs( mApis );
}
14 changes: 14 additions & 0 deletions src/gui/qgscodeeditorsql.h
Expand Up @@ -19,6 +19,7 @@
#include "qgscodeeditor.h"
#include "qgis_sip.h"
#include "qgis_gui.h"
#include "qgsfeature.h"
#include <Qsci/qscilexersql.h>

SIP_IF_MODULE( HAVE_QSCI_SIP )
Expand All @@ -38,10 +39,23 @@ class GUI_EXPORT QgsCodeEditorSQL : public QgsCodeEditor
//! Constructor for QgsCodeEditorSQL
QgsCodeEditorSQL( QWidget *parent SIP_TRANSFERTHIS = nullptr );

/**
* Set field names to be added to the lexer API.
*
* \since QGIS 3.14
*/
void setFields( const QgsFields &fields );

private:
//QgsCodeEditor *mSciWidget;
//QWidget *mWidget;
void setSciLexerSQL();
void initializeLexer();
void updateApis();
QsciAPIs *mApis = nullptr;
QsciLexerSQL *mSqlLexer;

QStringList mFieldNames;
};

#ifndef SIP_RUN
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgsquerybuilder.cpp
Expand Up @@ -91,6 +91,7 @@ void QgsQueryBuilder::showEvent( QShowEvent *event )
void QgsQueryBuilder::populateFields()
{
const QgsFields &fields = mLayer->fields();
txtSQL->setFields( fields );
for ( int idx = 0; idx < fields.count(); ++idx )
{
if ( fields.fieldOrigin( idx ) != QgsFields::OriginProvider )
Expand Down

0 comments on commit 86ac754

Please sign in to comment.