QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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 "qgsapplication.h"
17#include "qgscodeeditorsql.h"
18#include "qgssymbollayerutils.h"
19
20#include <QWidget>
21#include <QString>
22#include <QFont>
23
24
26 : QgsCodeEditor( parent )
27{
28 if ( !parent )
29 {
30 setTitle( tr( "SQL Editor" ) );
31 }
32 setAutoCompletionCaseSensitivity( false );
33 QgsCodeEditorSQL::initializeLexer(); // avoid cppcheck warning by explicitly specifying namespace
34}
35
37{
38 if ( mApis )
39 {
40 mApis->cancelPreparation( );
41 }
42}
43
45{
46 QFont font = lexerFont();
48
49 mSqlLexer = new QgsCaseInsensitiveLexerSQL( this );
50 mSqlLexer->setDefaultFont( font );
51 mSqlLexer->setDefaultColor( defaultColor );
52 mSqlLexer->setDefaultPaper( lexerColor( QgsCodeEditorColorScheme::ColorRole::Background ) );
53 mSqlLexer->setFont( font, -1 );
54 font.setBold( true );
55 mSqlLexer->setFont( font, QsciLexerSQL::Keyword );
56
57 font.setBold( false );
58 font.setItalic( true );
59 mSqlLexer->setFont( font, QsciLexerSQL::Comment );
60 mSqlLexer->setFont( font, QsciLexerSQL::CommentLine );
61
62 mSqlLexer->setColor( defaultColor, QsciLexerSQL::Default );
63 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Comment ), QsciLexerSQL::Comment );
64 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CommentLine ), QsciLexerSQL::CommentLine );
65 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Number ), QsciLexerSQL::Number );
66 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Keyword ), QsciLexerSQL::Keyword );
67 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::SingleQuote ), QsciLexerSQL::SingleQuotedString );
68 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::DoubleQuote ), QsciLexerSQL::DoubleQuotedString );
69 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Operator ), QsciLexerSQL::Operator );
70 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Identifier ), QsciLexerSQL::Identifier );
71 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::QuotedIdentifier ), QsciLexerSQL::QuotedIdentifier );
72 mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::QuotedOperator ), QsciLexerSQL::QuotedOperator );
73
74 setLexer( mSqlLexer );
75
77}
78
80{
81
82 QStringList fieldNames;
83
84 for ( const QgsField &field : std::as_const( fields ) )
85 {
86 fieldNames.push_back( field.name() );
87 }
88
90
91}
92
93void QgsCodeEditorSQL::updateApis()
94{
95 mApis = new QsciAPIs( mSqlLexer );
96
97 for ( const QString &fieldName : std::as_const( mFieldNames ) )
98 {
99 mApis->add( fieldName );
100 }
101
102 for ( const QString &keyword : std::as_const( mExtraKeywords ) )
103 {
104 mApis->add( keyword );
105 }
106
107 mApis->prepare();
108 mSqlLexer->setAPIs( mApis );
109}
110
112{
113 return mExtraKeywords.values();
114}
115
116void QgsCodeEditorSQL::setExtraKeywords( const QStringList &extraKeywords )
117{
118 mExtraKeywords = qgis::listToSet( extraKeywords );
119 updateApis();
120}
121
123{
124 return mFieldNames.values();
125}
126
127void QgsCodeEditorSQL::setFieldNames( const QStringList &fieldNames )
128{
129 mFieldNames = qgis::listToSet( fieldNames );
130 updateApis();
131}
132
133
@ 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.
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:42
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:51
QString name
Definition: qgsfield.h:60
Container of fields for a vector layer.
Definition: qgsfields.h:45
const QgsField & field
Definition: qgsfield.h:463