QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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  setMarginVisible( false );
33  setFoldingVisible( true );
34  setAutoCompletionCaseSensitivity( false );
35  initializeLexer();
36 }
37 
38 
39 void QgsCodeEditorSQL::initializeLexer()
40 {
41  QHash< QString, QColor > colors;
42  if ( QgsApplication::instance()->themeName() != QStringLiteral( "default" ) )
43  {
44  QSettings ini( QgsApplication::instance()->uiThemes().value( QgsApplication::instance()->themeName() ) + "/qscintilla.ini", QSettings::IniFormat );
45  for ( const auto &key : ini.allKeys() )
46  {
47  colors.insert( key, QgsSymbolLayerUtils::decodeColor( ini.value( key ).toString() ) );
48  }
49  }
50 
51  QFont font = getMonospaceFont();
52  QColor defaultColor = colors.value( QStringLiteral( "sql/defaultFontColor" ), Qt::black );
53 
54  mSqlLexer = new QgsCaseInsensitiveLexerSQL( this );
55  mSqlLexer->setDefaultFont( font );
56  mSqlLexer->setDefaultColor( defaultColor );
57  mSqlLexer->setDefaultPaper( colors.value( QStringLiteral( "sql/paperBackgroundColor" ), Qt::white ) );
58  mSqlLexer->setFont( font, -1 );
59  font.setBold( true );
60  mSqlLexer->setFont( font, QsciLexerSQL::Keyword );
61 
62  mSqlLexer->setColor( defaultColor, QsciLexerSQL::Default );
63  mSqlLexer->setColor( colors.value( QStringLiteral( "sql/commentFontColor" ), QColor( 142, 144, 140 ) ), QsciLexerSQL::Comment );
64  mSqlLexer->setColor( colors.value( QStringLiteral( "sql/commentLineFontColor" ), QColor( 142, 144, 140 ) ), QsciLexerSQL::CommentLine );
65  mSqlLexer->setColor( colors.value( QStringLiteral( "sql/numberFontColor" ), QColor( 200, 40, 41 ) ), QsciLexerSQL::Number );
66  mSqlLexer->setColor( colors.value( QStringLiteral( "sql/keywordFontColor" ), QColor( 137, 89, 168 ) ), QsciLexerSQL::Keyword );
67  mSqlLexer->setColor( colors.value( QStringLiteral( "sql/singleQuoteFontColor" ), QColor( 113, 140, 0 ) ), QsciLexerSQL::SingleQuotedString );
68  mSqlLexer->setColor( colors.value( QStringLiteral( "sql/doubleQuoteFontColor" ), QColor( 234, 183, 0 ) ), QsciLexerSQL::DoubleQuotedString );
69  mSqlLexer->setColor( colors.value( QStringLiteral( "sql/operatorFontColor" ), QColor( 66, 113, 174 ) ), QsciLexerSQL::Operator );
70  mSqlLexer->setColor( colors.value( QStringLiteral( "sql/identifierFontColor" ), QColor( 62, 153, 159 ) ), QsciLexerSQL::Identifier );
71  mSqlLexer->setColor( colors.value( QStringLiteral( "sql/QuotedIdentifierFontColor" ), Qt::black ), QsciLexerSQL::QuotedIdentifier );
72  mSqlLexer->setColor( colors.value( QStringLiteral( "sql/QuotedOperatorFontColor" ), Qt::black ), QsciLexerSQL::QuotedOperator );
73 
74  setLexer( mSqlLexer );
75 }
76 
78 {
79  mFieldNames.clear();
80 
81  for ( const QgsField &field : fields )
82  {
83  mFieldNames << field.name();
84  }
85 
86  updateApis();
87 }
88 
89 void QgsCodeEditorSQL::updateApis()
90 {
91  mApis = new QsciAPIs( mSqlLexer );
92 
93  for ( const QString &fieldName : qgis::as_const( mFieldNames ) )
94  {
95  mApis->add( fieldName );
96  }
97 
98  mApis->prepare();
99  mSqlLexer->setAPIs( mApis );
100 }
qgssymbollayerutils.h
QgsFields
Definition: qgsfields.h:44
QgsCodeEditor::setMarginVisible
void setMarginVisible(bool margin)
Set margin visible state.
Definition: qgscodeeditor.cpp:146
QgsCodeEditor
Definition: qgscodeeditor.h:38
QgsApplication::instance
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
Definition: qgsapplication.cpp:390
QgsSymbolLayerUtils::decodeColor
static QColor decodeColor(const QString &str)
Definition: qgssymbollayerutils.cpp:57
QgsCodeEditor::setTitle
void setTitle(const QString &title)
Set the widget title.
Definition: qgscodeeditor.cpp:141
qgsapplication.h
qgscodeeditorsql.h
QgsCodeEditorSQL::QgsCodeEditorSQL
QgsCodeEditorSQL(QWidget *parent=nullptr)
Constructor for QgsCodeEditorSQL.
Definition: qgscodeeditorsql.cpp:25
QgsCodeEditorSQL::setFields
void setFields(const QgsFields &fields)
Set field names to be added to the lexer API.
Definition: qgscodeeditorsql.cpp:77
QgsCodeEditor::setFoldingVisible
void setFoldingVisible(bool folding)
Set folding visible state.
Definition: qgscodeeditor.cpp:164
QgsCodeEditor::getMonospaceFont
QFont getMonospaceFont()
Definition: qgscodeeditor.cpp:199
QgsField
Definition: qgsfield.h:49