QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 #include <QLabel>
24 
25 
27  : QgsCodeEditor( parent )
28 {
29  if ( !parent )
30  {
31  setTitle( tr( "SQL Editor" ) );
32  }
33  setMarginVisible( false );
34  setFoldingVisible( true );
35  setAutoCompletionCaseSensitivity( false );
36  setSciLexerSQL();
37 }
38 
39 
40 void QgsCodeEditorSQL::setSciLexerSQL()
41 {
42  QHash< QString, QColor > colors;
43  if ( QgsApplication::instance()->themeName() != QStringLiteral( "default" ) )
44  {
45  QSettings ini( QgsApplication::instance()->uiThemes().value( QgsApplication::instance()->themeName() ) + "/qscintilla.ini", QSettings::IniFormat );
46  for ( const auto &key : ini.allKeys() )
47  {
48  colors.insert( key, QgsSymbolLayerUtils::decodeColor( ini.value( key ).toString() ) );
49  }
50  }
51 
52  QFont font = getMonospaceFont();
53 #ifdef Q_OS_MAC
54  // The font size gotten from getMonospaceFont() is too small on Mac
55  font.setPointSize( QLabel().font().pointSize() );
56 #endif
57  QColor defaultColor = colors.value( QStringLiteral( "sql/defaultFontColor" ), Qt::black );
58 
59  QsciLexerSQL *sqlLexer = new QgsCaseInsensitiveLexerSQL( this );
60  sqlLexer->setDefaultFont( font );
61  sqlLexer->setDefaultColor( defaultColor );
62  sqlLexer->setDefaultPaper( colors.value( QStringLiteral( "sql/paperBackgroundColor" ), Qt::white ) );
63  sqlLexer->setFont( font, -1 );
64  font.setBold( true );
65  sqlLexer->setFont( font, QsciLexerSQL::Keyword );
66 
67  sqlLexer->setColor( defaultColor, QsciLexerSQL::Default );
68  sqlLexer->setColor( colors.value( QStringLiteral( "sql/commentFontColor" ), QColor( 142, 144, 140 ) ), QsciLexerSQL::Comment );
69  sqlLexer->setColor( colors.value( QStringLiteral( "sql/commentLineFontColor" ), QColor( 142, 144, 140 ) ), QsciLexerSQL::CommentLine );
70  sqlLexer->setColor( colors.value( QStringLiteral( "sql/numberFontColor" ), QColor( 200, 40, 41 ) ), QsciLexerSQL::Number );
71  sqlLexer->setColor( colors.value( QStringLiteral( "sql/keywordFontColor" ), QColor( 137, 89, 168 ) ), QsciLexerSQL::Keyword );
72  sqlLexer->setColor( colors.value( QStringLiteral( "sql/singleQuoteFontColor" ), QColor( 113, 140, 0 ) ), QsciLexerSQL::SingleQuotedString );
73  sqlLexer->setColor( colors.value( QStringLiteral( "sql/doubleQuoteFontColor" ), QColor( 234, 183, 0 ) ), QsciLexerSQL::DoubleQuotedString );
74  sqlLexer->setColor( colors.value( QStringLiteral( "sql/operatorFontColor" ), QColor( 66, 113, 174 ) ), QsciLexerSQL::Operator );
75  sqlLexer->setColor( colors.value( QStringLiteral( "sql/identifierFontColor" ), QColor( 62, 153, 159 ) ), QsciLexerSQL::Identifier );
76  sqlLexer->setColor( colors.value( QStringLiteral( "sql/QuotedIdentifierFontColor" ), Qt::black ), QsciLexerSQL::QuotedIdentifier );
77  sqlLexer->setColor( colors.value( QStringLiteral( "sql/QuotedOperatorFontColor" ), Qt::black ), QsciLexerSQL::QuotedOperator );
78 
79  setLexer( sqlLexer );
80 }
A text editor based on QScintilla2.
Definition: qgscodeeditor.h:38
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
void setFoldingVisible(bool folding)
Set folding visible state.
QFont getMonospaceFont()
QgsCodeEditorSQL(QWidget *parent=nullptr)
Constructor for QgsCodeEditorSQL.
void setMarginVisible(bool margin)
Set margin visible state.
void setTitle(const QString &title)
Set the widget title.
static QColor decodeColor(const QString &str)