QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgscodeeditorsql.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscodeeditorsql.cpp - A SQL editor based on QScintilla
3 --------------------------------------
4 Date : 06-Oct-2013
5 Copyright : (C) 2013 by Salvatore Larosa
6 Email : lrssvtml (at) gmail (dot) com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgscodeeditorsql.h"
17
18#include <QWidget>
19#include <QString>
20#include <QFont>
21
22
24 : QgsCodeEditor( parent )
25{
26 if ( !parent )
27 {
28 setTitle( tr( "SQL Editor" ) );
29 }
30 setAutoCompletionCaseSensitivity( false );
31 QgsCodeEditorSQL::initializeLexer(); // avoid cppcheck warning by explicitly specifying namespace
32}
33
35{
37}
38
40{
41 if ( mApis )
42 {
43 mApis->cancelPreparation( );
44 }
45}
46
48{
49 QFont font = lexerFont();
51
52 mSqlLexer = new QgsCaseInsensitiveLexerSQL( this );
53 mSqlLexer->setDefaultFont( font );
54 mSqlLexer->setDefaultColor( defaultColor );
55 mSqlLexer->setDefaultPaper( lexerColor( QgsCodeEditorColorScheme::ColorRole::Background ) );
56 mSqlLexer->setFont( font, -1 );
57 font.setBold( true );
58 mSqlLexer->setFont( font, QsciLexerSQL::Keyword );
59
60 font.setBold( false );
61 font.setItalic( true );
62 mSqlLexer->setFont( font, QsciLexerSQL::Comment );
63 mSqlLexer->setFont( font, QsciLexerSQL::CommentLine );
64
65 mSqlLexer->setColor( defaultColor, QsciLexerSQL::Default );
66 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Comment ), QsciLexerSQL::Comment );
67 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CommentLine ), QsciLexerSQL::CommentLine );
68 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Number ), QsciLexerSQL::Number );
69 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Keyword ), QsciLexerSQL::Keyword );
70 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::SingleQuote ), QsciLexerSQL::SingleQuotedString );
71 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::DoubleQuote ), QsciLexerSQL::DoubleQuotedString );
72 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Operator ), QsciLexerSQL::Operator );
73 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Identifier ), QsciLexerSQL::Identifier );
74 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::QuotedIdentifier ), QsciLexerSQL::QuotedIdentifier );
75 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::QuotedOperator ), QsciLexerSQL::QuotedOperator );
76
77 setLexer( mSqlLexer );
78
80}
81
83{
84
85 QStringList fieldNames;
86
87 for ( const QgsField &field : std::as_const( fields ) )
88 {
89 fieldNames.push_back( field.name() );
90 }
91
93
94}
95
96void QgsCodeEditorSQL::updateApis()
97{
98 mApis = new QsciAPIs( mSqlLexer );
99
100 for ( const QString &fieldName : std::as_const( mFieldNames ) )
101 {
102 mApis->add( fieldName );
103 }
104
105 for ( const QString &keyword : std::as_const( mExtraKeywords ) )
106 {
107 mApis->add( keyword );
108 }
109
110 mApis->prepare();
111 mSqlLexer->setAPIs( mApis );
112}
113
115{
116 return mExtraKeywords.values();
117}
118
119void QgsCodeEditorSQL::setExtraKeywords( const QStringList &extraKeywords )
120{
121 mExtraKeywords = qgis::listToSet( extraKeywords );
122 updateApis();
123}
124
126{
127 return mFieldNames.values();
128}
129
130void QgsCodeEditorSQL::setFieldNames( const QStringList &fieldNames )
131{
132 mFieldNames = qgis::listToSet( fieldNames );
133 updateApis();
134}
135
136
ScriptLanguage
Scripting languages.
Definition: qgis.h:3718
@ QuotedOperator
Quoted operator color.
@ DoubleQuote
Double quote color.
@ QuotedIdentifier
Quoted identifier color.
@ CommentLine
Line comment color.
@ SingleQuote
Single quote color.
QStringList extraKeywords() const
Returns the extra keywords.
Qgis::ScriptLanguage language() const override
Returns the associated scripting language.
void setExtraKeywords(const QStringList &extraKeywords)
Set extra keywords to extraKeywords.
QStringList fieldNames() const
Returns field names from the lexer API.
virtual ~QgsCodeEditorSQL()
QgsCodeEditorSQL(QWidget *parent=nullptr)
Constructor for QgsCodeEditorSQL.
void setFieldNames(const QStringList &fieldNames)
Set field names to fieldNames to be added to the lexer API.
void initializeLexer() override
Called when the dialect specific code lexer needs to be initialized (or reinitialized).
void setFields(const QgsFields &fields)
Set field names to be added to the lexer API.
A text editor based on QScintilla2.
Definition: qgscodeeditor.h:93
void runPostLexerConfigurationTasks()
Performs tasks which must be run after a lexer has been set for the widget.
void setTitle(const QString &title)
Set the widget title.
QFont lexerFont() const
Returns the font to use in the lexer.
QColor lexerColor(QgsCodeEditorColorScheme::ColorRole role) const
Returns the color to use in the lexer for the specified role.
static QColor defaultColor(QgsCodeEditorColorScheme::ColorRole role, const QString &theme=QString())
Returns the default color for the specified role.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:53
Container of fields for a vector layer.
Definition: qgsfields.h:45