QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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#include "moc_qgscodeeditorsql.cpp"
18
19#include <QWidget>
20#include <QString>
21#include <QFont>
22
23
25 : QgsCodeEditor( parent )
26{
27 if ( !parent )
28 {
29 setTitle( tr( "SQL Editor" ) );
30 }
31 setAutoCompletionCaseSensitivity( false );
32 QgsCodeEditorSQL::initializeLexer(); // avoid cppcheck warning by explicitly specifying namespace
33}
34
39
41{
42 if ( mApis )
43 {
44 mApis->cancelPreparation( );
45 }
46}
47
49{
50 QFont font = lexerFont();
52
53 mSqlLexer = new QgsCaseInsensitiveLexerSQL( this );
54 mSqlLexer->setDefaultFont( font );
55 mSqlLexer->setDefaultColor( defaultColor );
56 mSqlLexer->setDefaultPaper( lexerColor( QgsCodeEditorColorScheme::ColorRole::Background ) );
57 mSqlLexer->setFont( font, -1 );
58 font.setBold( true );
59 mSqlLexer->setFont( font, QsciLexerSQL::Keyword );
60
61 font.setBold( false );
62 font.setItalic( true );
63 mSqlLexer->setFont( font, QsciLexerSQL::Comment );
64 mSqlLexer->setFont( font, QsciLexerSQL::CommentLine );
65
66 mSqlLexer->setColor( defaultColor, QsciLexerSQL::Default );
67 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Comment ), QsciLexerSQL::Comment );
68 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CommentLine ), QsciLexerSQL::CommentLine );
69 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Number ), QsciLexerSQL::Number );
70 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Keyword ), QsciLexerSQL::Keyword );
71 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::SingleQuote ), QsciLexerSQL::SingleQuotedString );
72 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::DoubleQuote ), QsciLexerSQL::DoubleQuotedString );
73 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Operator ), QsciLexerSQL::Operator );
74 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Identifier ), QsciLexerSQL::Identifier );
75 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::QuotedIdentifier ), QsciLexerSQL::QuotedIdentifier );
76 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::QuotedOperator ), QsciLexerSQL::QuotedOperator );
77
78 setLexer( mSqlLexer );
79
81}
82
84{
85
86 QStringList fieldNames;
87
88 for ( const QgsField &field : std::as_const( fields ) )
89 {
90 fieldNames.push_back( field.name() );
91 }
92
94
95}
96
97void QgsCodeEditorSQL::updateApis()
98{
99 mApis = new QsciAPIs( mSqlLexer );
100
101 for ( const QString &fieldName : std::as_const( mFieldNames ) )
102 {
103 mApis->add( fieldName );
104 }
105
106 for ( const QString &keyword : std::as_const( mExtraKeywords ) )
107 {
108 mApis->add( keyword );
109 }
110
111 mApis->prepare();
112 mSqlLexer->setAPIs( mApis );
113}
114
116{
117 return mExtraKeywords.values();
118}
119
120void QgsCodeEditorSQL::setExtraKeywords( const QStringList &extraKeywords )
121{
122 mExtraKeywords = qgis::listToSet( extraKeywords );
123 updateApis();
124}
125
127{
128 return mFieldNames.values();
129}
130
131void QgsCodeEditorSQL::setFieldNames( const QStringList &fieldNames )
132{
133 mFieldNames = qgis::listToSet( fieldNames );
134 updateApis();
135}
136
137
ScriptLanguage
Scripting languages.
Definition qgis.h:4181
@ 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.
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.
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:46