QGIS API Documentation 3.41.0-Master (3440c17df1d)
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,
28 QString(),
29 false,
30 false,
31 QgsCodeEditor::Flag::CodeFolding,
32 mode )
33 , mLanguage( language )
34{
35 if ( mLanguage != Qgis::ScriptLanguage::Bash && mLanguage != Qgis::ScriptLanguage::Batch )
36 {
37#ifdef Q_OS_WIN
39#else
41#endif
42 }
43
44 if ( !parent )
45 {
46 switch ( mLanguage )
47 {
49 setTitle( tr( "Batch Editor" ) );
50 break;
52 setTitle( tr( "Bash Editor" ) );
53 break;
54
64 break;
65 }
66 }
67
69}
70
72{
73 return mLanguage;
74}
75
77{
78 QsciLexer *lexer = nullptr;
80 switch ( language )
81 {
84 break;
85
95#ifdef Q_OS_WIN
97#else
99#endif
100 break;
101 }
102
103 switch ( language )
104 {
106 lexer = new QgsQsciLexerBash();
107 break;
109 lexer = new QgsQsciLexerBatch();
110 break;
111
112 default:
113 return; // unreachable
114 }
115
116 QFont font = lexerFont();
117 lexer->setDefaultFont( font );
118 lexer->setFont( font, -1 );
119
120 switch ( language )
121 {
123 {
124 font.setItalic( true );
125 lexer->setFont( font, QgsQsciLexerBash::LineComment );
126
127 lexer->setDefaultColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Default ) );
130
131 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Default ), QgsQsciLexerBash::Default );
132 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Error ), QgsQsciLexerBash::Error );
133 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CommentLine ), QgsQsciLexerBash::LineComment );
134 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Number ), QgsQsciLexerBash::Number );
135 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Keyword ), QgsQsciLexerBash::Keyword );
136 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::DoubleQuote ), QgsQsciLexerBash::String );
137 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::SingleQuote ), QgsQsciLexerBash::SingleQuotedString );
138 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Operator ), QgsQsciLexerBash::Operator );
139 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Identifier ), QgsQsciLexerBash::Identifier );
140 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Identifier ), QgsQsciLexerBash::ScalarVariable );
141 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Tag ), QgsQsciLexerBash::BacktickQuotedCommand );
142 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Operator ), QgsQsciLexerBash::HeredocDelimiter );
143 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CommentBlock ), QgsQsciLexerBash::HeredocQuotedString );
144 break;
145 }
146
148 {
149 font.setItalic( true );
150 lexer->setFont( font, QgsQsciLexerBatch::Comment );
151
152 lexer->setDefaultColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Default ) );
155
156 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Default ), QgsQsciLexerBatch::Default );
157 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CommentLine ), QgsQsciLexerBatch::Comment );
158 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Keyword ), QgsQsciLexerBatch::Word );
159 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Tag ), QgsQsciLexerBatch::Label );
160 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Decoration ), QgsQsciLexerBatch::Hide );
161 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Method ), QgsQsciLexerBatch::Command );
162 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::DoubleQuote ), QgsQsciLexerBatch::Identifier );
163 lexer->setColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Operator ), QgsQsciLexerBatch::Operator );
164 break;
165 }
166
167 default:
168 break;
169 }
170
171
172 setLexer( lexer );
173 setLineNumbersVisible( true );
175}
176
178QgsQsciLexerBash::QgsQsciLexerBash( QObject *parent )
179 : QsciLexer( parent )
180{
181
182}
183
184const char *QgsQsciLexerBash::language() const
185{
186 return "bash";
187}
188
189const char *QgsQsciLexerBash::lexer() const
190{
191 return nullptr;
192}
193
194int QgsQsciLexerBash::lexerId() const
195{
196 return QsciScintillaBase::SCLEX_BASH;
197}
198
199QString QgsQsciLexerBash::description( int style ) const
200{
201 switch ( style )
202 {
203 case Default:
204 return tr( "Default" );
205 case Error:
206 return tr( "Error" );
207 case LineComment:
208 return tr( "Comment" );
209 case Number:
210 return tr( "Number" );
211 case Keyword:
212 return tr( "Keyword" );
213 case String:
214 return tr( "String" );
215 case SingleQuotedString:
216 return tr( "Single Quoted String" );
217 case Operator:
218 return tr( "Operator" );
219 case Identifier:
220 return tr( "Identifier" );
221 case ScalarVariable:
222 return tr( "Scale Variable" );
223 case Parameter:
224 return tr( "Parameter" );
225 case BacktickQuotedCommand:
226 return tr( "Backtick Quoted Command" );
227 case HeredocDelimiter:
228 return tr( "Heredoc Delimiter" );
229 case HeredocQuotedString:
230 return tr( "Heredoc Quoted String" );
231 }
232 return QString();
233}
234
235const char *QgsQsciLexerBash::keywords( int set ) const
236{
237 switch ( set )
238 {
239 case 1:
240 return "case cat do done echo else esac exit export fi find for if in set then while";
241 }
242
243 return nullptr;
244}
245
246
247
248QgsQsciLexerBatch::QgsQsciLexerBatch( QObject *parent )
249 : QsciLexer( parent )
250{
251
252}
253
254const char *QgsQsciLexerBatch::language() const
255{
256 return "batch";
257}
258
259const char *QgsQsciLexerBatch::lexer() const
260{
261 return nullptr;
262}
263
264int QgsQsciLexerBatch::lexerId() const
265{
266 return QsciScintillaBase::SCLEX_BATCH;
267}
268
269QString QgsQsciLexerBatch::description( int style ) const
270{
271 switch ( style )
272 {
273 case Default:
274 return tr( "Default" );
275 case Comment:
276 return tr( "Comment" );
277 case Word:
278 return tr( "Word" );
279 case Label:
280 return tr( "Label" );
281 case Hide:
282 return tr( "Hide" );
283 case Command:
284 return tr( "Command" );
285 case Operator:
286 return tr( "Operator" );
287 case Identifier:
288 return tr( "Identifier" );
289 }
290 return QString();
291}
292
293const char *QgsQsciLexerBatch::keywords( int set ) const
294{
295 switch ( set )
296 {
297 case 1:
298 return "call defined do echo else errorlevel exist exit for goto if in not set";
299 }
300
301 return nullptr;
302}
303
304
ScriptLanguage
Scripting languages.
Definition qgis.h:4181
@ 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.