QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  setFoldingVisible( false );
33  setAutoCompletionCaseSensitivity( false );
34  QgsCodeEditorSQL::initializeLexer(); // avoid cppcheck warning by explicitly specifying namespace
35 }
36 
38 {
39  if ( mApis )
40  {
41  mApis->cancelPreparation( );
42  }
43 }
44 
46 {
47  QFont font = lexerFont();
49 
50  mSqlLexer = new QgsCaseInsensitiveLexerSQL( this );
51  mSqlLexer->setDefaultFont( font );
52  mSqlLexer->setDefaultColor( defaultColor );
53  mSqlLexer->setDefaultPaper( lexerColor( QgsCodeEditorColorScheme::ColorRole::Background ) );
54  mSqlLexer->setFont( font, -1 );
55  font.setBold( true );
56  mSqlLexer->setFont( font, QsciLexerSQL::Keyword );
57 
58  font.setBold( false );
59  font.setItalic( true );
60  mSqlLexer->setFont( font, QsciLexerSQL::Comment );
61  mSqlLexer->setFont( font, QsciLexerSQL::CommentLine );
62 
63  mSqlLexer->setColor( defaultColor, QsciLexerSQL::Default );
64  mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Comment ), QsciLexerSQL::Comment );
65  mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CommentLine ), QsciLexerSQL::CommentLine );
66  mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Number ), QsciLexerSQL::Number );
67  mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Keyword ), QsciLexerSQL::Keyword );
68  mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::SingleQuote ), QsciLexerSQL::SingleQuotedString );
69  mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::DoubleQuote ), QsciLexerSQL::DoubleQuotedString );
70  mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Operator ), QsciLexerSQL::Operator );
71  mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Identifier ), QsciLexerSQL::Identifier );
72  mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::QuotedIdentifier ), QsciLexerSQL::QuotedIdentifier );
73  mSqlLexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::QuotedOperator ), QsciLexerSQL::QuotedOperator );
74 
75  setLexer( mSqlLexer );
76 
78 }
79 
81 {
82 
83  QStringList fieldNames;
84 
85  for ( const QgsField &field : std::as_const( fields ) )
86  {
87  fieldNames.push_back( field.name() );
88  }
89 
91 
92 }
93 
94 void QgsCodeEditorSQL::updateApis()
95 {
96  mApis = new QsciAPIs( mSqlLexer );
97 
98  for ( const QString &fieldName : std::as_const( mFieldNames ) )
99  {
100  mApis->add( fieldName );
101  }
102 
103  for ( const QString &keyword : std::as_const( mExtraKeywords ) )
104  {
105  mApis->add( keyword );
106  }
107 
108  mApis->prepare();
109  mSqlLexer->setAPIs( mApis );
110 }
111 
113 {
114  return mExtraKeywords.values();
115 }
116 
117 void QgsCodeEditorSQL::setExtraKeywords( const QStringList &extraKeywords )
118 {
119  mExtraKeywords = qgis::listToSet( extraKeywords );
120  updateApis();
121 }
122 
123 QStringList QgsCodeEditorSQL::fieldNames() const
124 {
125  return mFieldNames.values();
126 }
127 
128 void QgsCodeEditorSQL::setFieldNames( const QStringList &fieldNames )
129 {
130  mFieldNames = qgis::listToSet( fieldNames );
131  updateApis();
132 }
133 
134 
QgsCodeEditor::lexerColor
QColor lexerColor(QgsCodeEditorColorScheme::ColorRole role) const
Returns the color to use in the lexer for the specified role.
Definition: qgscodeeditor.cpp:154
QgsCodeEditorColorScheme::ColorRole::Default
@ Default
Default text color.
QgsCodeEditorSQL::~QgsCodeEditorSQL
virtual ~QgsCodeEditorSQL()
Definition: qgscodeeditorsql.cpp:37
QgsCodeEditorColorScheme::ColorRole::CommentLine
@ CommentLine
Line comment color.
qgssymbollayerutils.h
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsCodeEditor
A text editor based on QScintilla2.
Definition: qgscodeeditor.h:41
QgsCodeEditorSQL::extraKeywords
QStringList extraKeywords() const
Returns the extra keywords.
Definition: qgscodeeditorsql.cpp:112
QgsCodeEditorColorScheme::ColorRole::Operator
@ Operator
Operator color.
QgsCodeEditorSQL::setExtraKeywords
void setExtraKeywords(const QStringList &extraKeywords)
Set extra keywords to extraKeywords.
Definition: qgscodeeditorsql.cpp:117
field
const QgsField & field
Definition: qgsfield.h:463
QgsField::name
QString name
Definition: qgsfield.h:60
QgsCodeEditorColorScheme::ColorRole::SingleQuote
@ SingleQuote
Single quote color.
QgsCodeEditor::setTitle
void setTitle(const QString &title)
Set the widget title.
Definition: qgscodeeditor.cpp:259
qgsapplication.h
QgsCodeEditorColorScheme::ColorRole::DoubleQuote
@ DoubleQuote
Double quote color.
QgsCodeEditorSQL::setFieldNames
void setFieldNames(const QStringList &fieldNames)
Set field names to fieldNames to be added to the lexer API.
Definition: qgscodeeditorsql.cpp:128
QgsCodeEditorColorScheme::ColorRole::Comment
@ Comment
Comment color.
QgsCodeEditor::lexerFont
QFont lexerFont() const
Returns the font to use in the lexer.
Definition: qgscodeeditor.cpp:170
QgsCodeEditorSQL::initializeLexer
void initializeLexer() override
Called when the dialect specific code lexer needs to be initialized (or reinitialized).
Definition: qgscodeeditorsql.cpp:45
QgsCodeEditor::runPostLexerConfigurationTasks
void runPostLexerConfigurationTasks()
Performs tasks which must be run after a lexer has been set for the widget.
Definition: qgscodeeditor.cpp:203
QgsCodeEditorColorScheme::ColorRole::Number
@ Number
Number color.
QgsCodeEditorSQL::fieldNames
QStringList fieldNames() const
Returns field names from the lexer API.
Definition: qgscodeeditorsql.cpp:123
QgsCodeEditorColorScheme::ColorRole::Identifier
@ Identifier
Identifier color.
QgsCodeEditorColorScheme::ColorRole::QuotedOperator
@ QuotedOperator
Quoted operator color.
qgscodeeditorsql.h
QgsCodeEditor::defaultColor
static QColor defaultColor(QgsCodeEditorColorScheme::ColorRole role, const QString &theme=QString())
Returns the default color for the specified role.
Definition: qgscodeeditor.cpp:342
QgsCodeEditorSQL::QgsCodeEditorSQL
QgsCodeEditorSQL(QWidget *parent=nullptr)
Constructor for QgsCodeEditorSQL.
Definition: qgscodeeditorsql.cpp:25
QgsCodeEditorColorScheme::ColorRole::QuotedIdentifier
@ QuotedIdentifier
Quoted identifier color.
QgsCodeEditorSQL::setFields
void setFields(const QgsFields &fields)
Set field names to be added to the lexer API.
Definition: qgscodeeditorsql.cpp:80
QgsCodeEditor::setFoldingVisible
void setFoldingVisible(bool folding)
Set whether the folding controls are visible in the editor.
Definition: qgscodeeditor.cpp:309
QgsCodeEditorColorScheme::ColorRole::Background
@ Background
Background color.
QgsCodeEditorColorScheme::ColorRole::Keyword
@ Keyword
Keyword color.
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50