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