QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgscodeeditorshell.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscodeeditorshell.cpp
3 ---------------------
4 Date : April 2023
5 Copyright : (C) 2023 by Nyall Dawson
6 Email : nyall dot dawson 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
17#include "qgscodeeditorshell.h"
18#include "moc_qgscodeeditorshell.cpp"
19
20#include <QWidget>
21#include <QString>
22#include <QFont>
23#include <Qsci/qscilexerjson.h>
24
25
27 : QgsCodeEditor( parent, QString(), false, false, QgsCodeEditor::Flag::CodeFolding, mode )
28 , mLanguage( language )
29{
30 if ( mLanguage != Qgis::ScriptLanguage::Bash && mLanguage != Qgis::ScriptLanguage::Batch )
31 {
32#ifdef Q_OS_WIN
34#else
36#endif
37 }
38
39 if ( !parent )
40 {
41 switch ( mLanguage )
42 {
44 setTitle( tr( "Batch Editor" ) );
45 break;
47 setTitle( tr( "Bash Editor" ) );
48 break;
49
59 break;
60 }
61 }
62
64}
65
67{
68 return mLanguage;
69}
70
72{
73 QsciLexer *lexer = nullptr;
75 switch ( language )
76 {
79 break;
80
90#ifdef Q_OS_WIN
92#else
94#endif
95 break;
96 }
97
98 switch ( language )
99 {
101 lexer = new QgsQsciLexerBash();
102 break;
104 lexer = new QgsQsciLexerBatch();
105 break;
106
107 default:
108 return; // unreachable
109 }
110
111 QFont font = lexerFont();
112 lexer->setDefaultFont( font );
113 lexer->setFont( font, -1 );
114
115 switch ( language )
116 {
118 {
119 font.setItalic( true );
120 lexer->setFont( font, QgsQsciLexerBash::LineComment );
121
122 lexer->setDefaultColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Default ) );
125
126 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Default ), QgsQsciLexerBash::Default );
127 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Error ), QgsQsciLexerBash::Error );
128 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CommentLine ), QgsQsciLexerBash::LineComment );
129 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Number ), QgsQsciLexerBash::Number );
130 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Keyword ), QgsQsciLexerBash::Keyword );
131 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::DoubleQuote ), QgsQsciLexerBash::String );
132 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::SingleQuote ), QgsQsciLexerBash::SingleQuotedString );
133 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Operator ), QgsQsciLexerBash::Operator );
134 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Identifier ), QgsQsciLexerBash::Identifier );
135 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Identifier ), QgsQsciLexerBash::ScalarVariable );
136 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Tag ), QgsQsciLexerBash::BacktickQuotedCommand );
137 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Operator ), QgsQsciLexerBash::HeredocDelimiter );
138 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CommentBlock ), QgsQsciLexerBash::HeredocQuotedString );
139 break;
140 }
141
143 {
144 font.setItalic( true );
145 lexer->setFont( font, QgsQsciLexerBatch::Comment );
146
147 lexer->setDefaultColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Default ) );
150
151 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Default ), QgsQsciLexerBatch::Default );
152 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CommentLine ), QgsQsciLexerBatch::Comment );
153 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Keyword ), QgsQsciLexerBatch::Word );
154 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Tag ), QgsQsciLexerBatch::Label );
155 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Decoration ), QgsQsciLexerBatch::Hide );
156 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Method ), QgsQsciLexerBatch::Command );
157 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::DoubleQuote ), QgsQsciLexerBatch::Identifier );
158 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Operator ), QgsQsciLexerBatch::Operator );
159 break;
160 }
161
162 default:
163 break;
164 }
165
166
167 setLexer( lexer );
168 setLineNumbersVisible( true );
170}
171
173QgsQsciLexerBash::QgsQsciLexerBash( QObject *parent )
174 : QsciLexer( parent )
175{
176}
177
178const char *QgsQsciLexerBash::language() const
179{
180 return "bash";
181}
182
183const char *QgsQsciLexerBash::lexer() const
184{
185 return nullptr;
186}
187
188int QgsQsciLexerBash::lexerId() const
189{
190 return QsciScintillaBase::SCLEX_BASH;
191}
192
193QString QgsQsciLexerBash::description( int style ) const
194{
195 switch ( style )
196 {
197 case Default:
198 return tr( "Default" );
199 case Error:
200 return tr( "Error" );
201 case LineComment:
202 return tr( "Comment" );
203 case Number:
204 return tr( "Number" );
205 case Keyword:
206 return tr( "Keyword" );
207 case String:
208 return tr( "String" );
209 case SingleQuotedString:
210 return tr( "Single Quoted String" );
211 case Operator:
212 return tr( "Operator" );
213 case Identifier:
214 return tr( "Identifier" );
215 case ScalarVariable:
216 return tr( "Scale Variable" );
217 case Parameter:
218 return tr( "Parameter" );
219 case BacktickQuotedCommand:
220 return tr( "Backtick Quoted Command" );
221 case HeredocDelimiter:
222 return tr( "Heredoc Delimiter" );
223 case HeredocQuotedString:
224 return tr( "Heredoc Quoted String" );
225 }
226 return QString();
227}
228
229const char *QgsQsciLexerBash::keywords( int set ) const
230{
231 switch ( set )
232 {
233 case 1:
234 return "case cat do done echo else esac exit export fi find for if in set then while";
235 }
236
237 return nullptr;
238}
239
240
241QgsQsciLexerBatch::QgsQsciLexerBatch( QObject *parent )
242 : QsciLexer( parent )
243{
244}
245
246const char *QgsQsciLexerBatch::language() const
247{
248 return "batch";
249}
250
251const char *QgsQsciLexerBatch::lexer() const
252{
253 return nullptr;
254}
255
256int QgsQsciLexerBatch::lexerId() const
257{
258 return QsciScintillaBase::SCLEX_BATCH;
259}
260
261QString QgsQsciLexerBatch::description( int style ) const
262{
263 switch ( style )
264 {
265 case Default:
266 return tr( "Default" );
267 case Comment:
268 return tr( "Comment" );
269 case Word:
270 return tr( "Word" );
271 case Label:
272 return tr( "Label" );
273 case Hide:
274 return tr( "Hide" );
275 case Command:
276 return tr( "Command" );
277 case Operator:
278 return tr( "Operator" );
279 case Identifier:
280 return tr( "Identifier" );
281 }
282 return QString();
283}
284
285const char *QgsQsciLexerBatch::keywords( int set ) const
286{
287 switch ( set )
288 {
289 case 1:
290 return "call defined do echo else errorlevel exist exit for goto if in not set";
291 }
292
293 return nullptr;
294}
295
296
ScriptLanguage
Scripting languages.
Definition qgis.h:4244
@ QgisExpression
QGIS expressions.
@ Batch
Windows batch files.
@ JavaScript
JavaScript.
@ Bash
Bash scripts.
@ Unknown
Unknown/other language.
@ CommentBlock
Comment block color.
@ DoubleQuote
Double quote color.
@ CommentLine
Line comment color.
@ SingleQuote
Single quote color.
void initializeLexer() override
Called when the dialect specific code lexer needs to be initialized (or reinitialized).
Qgis::ScriptLanguage language() const override
Returns the associated scripting language.
QgsCodeEditorShell(QWidget *parent=nullptr, QgsCodeEditor::Mode mode=QgsCodeEditor::Mode::ScriptEditor, Qgis::ScriptLanguage language=Qgis::ScriptLanguage::Unknown)
Constructor for QgsCodeEditorShell.
A text editor based on QScintilla2.
Mode
Code editor modes.
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.
Flag
Flags controlling behavior of code editor.
void setLineNumbersVisible(bool visible)
Sets whether line numbers should be visible in the editor.
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.