QGIS API Documentation  3.27.0-Master (597e8eebd4)
qgscodeeditor.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscodeeditor.cpp - A base code editor for QGIS and plugins. Provides
3  a base editor using QScintilla for editors
4  --------------------------------------
5  Date : 06-Oct-2013
6  Copyright : (C) 2013 by Salvatore Larosa
7  Email : lrssvtml (at) gmail (dot) com
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "qgsapplication.h"
18 #include "qgscodeeditor.h"
19 #include "qgssettings.h"
20 #include "qgssymbollayerutils.h"
21 #include "qgsgui.h"
23 
24 #include <QLabel>
25 #include <QWidget>
26 #include <QFont>
27 #include <QFontDatabase>
28 #include <QDebug>
29 #include <QFocusEvent>
30 #include <Qsci/qscistyle.h>
31 
32 QMap< QgsCodeEditorColorScheme::ColorRole, QString > QgsCodeEditor::sColorRoleToSettingsKey
33 {
34  {QgsCodeEditorColorScheme::ColorRole::Default, QStringLiteral( "defaultFontColor" ) },
35  {QgsCodeEditorColorScheme::ColorRole::Keyword, QStringLiteral( "keywordFontColor" ) },
36  {QgsCodeEditorColorScheme::ColorRole::Class, QStringLiteral( "classFontColor" ) },
37  {QgsCodeEditorColorScheme::ColorRole::Method, QStringLiteral( "methodFontColor" ) },
38  {QgsCodeEditorColorScheme::ColorRole::Decoration, QStringLiteral( "decoratorFontColor" ) },
39  {QgsCodeEditorColorScheme::ColorRole::Number, QStringLiteral( "numberFontColor" ) },
40  {QgsCodeEditorColorScheme::ColorRole::Comment, QStringLiteral( "commentFontColor" ) },
41  {QgsCodeEditorColorScheme::ColorRole::CommentLine, QStringLiteral( "commentLineFontColor" ) },
42  {QgsCodeEditorColorScheme::ColorRole::CommentBlock, QStringLiteral( "commentBlockFontColor" ) },
43  {QgsCodeEditorColorScheme::ColorRole::Background, QStringLiteral( "paperBackgroundColor" ) },
44  {QgsCodeEditorColorScheme::ColorRole::Cursor, QStringLiteral( "cursorColor" ) },
45  {QgsCodeEditorColorScheme::ColorRole::CaretLine, QStringLiteral( "caretLineColor" ) },
46  {QgsCodeEditorColorScheme::ColorRole::Operator, QStringLiteral( "operatorFontColor" ) },
47  {QgsCodeEditorColorScheme::ColorRole::QuotedOperator, QStringLiteral( "quotedOperatorFontColor" ) },
48  {QgsCodeEditorColorScheme::ColorRole::Identifier, QStringLiteral( "identifierFontColor" ) },
49  {QgsCodeEditorColorScheme::ColorRole::QuotedIdentifier, QStringLiteral( "quotedIdentifierFontColor" ) },
50  {QgsCodeEditorColorScheme::ColorRole::Tag, QStringLiteral( "tagFontColor" ) },
51  {QgsCodeEditorColorScheme::ColorRole::UnknownTag, QStringLiteral( "unknownTagFontColor" ) },
52  {QgsCodeEditorColorScheme::ColorRole::SingleQuote, QStringLiteral( "singleQuoteFontColor" ) },
53  {QgsCodeEditorColorScheme::ColorRole::DoubleQuote, QStringLiteral( "doubleQuoteFontColor" ) },
54  {QgsCodeEditorColorScheme::ColorRole::TripleSingleQuote, QStringLiteral( "tripleSingleQuoteFontColor" ) },
55  {QgsCodeEditorColorScheme::ColorRole::TripleDoubleQuote, QStringLiteral( "tripleDoubleQuoteFontColor" ) },
56  {QgsCodeEditorColorScheme::ColorRole::MarginBackground, QStringLiteral( "marginBackgroundColor" ) },
57  {QgsCodeEditorColorScheme::ColorRole::MarginForeground, QStringLiteral( "marginForegroundColor" ) },
58  {QgsCodeEditorColorScheme::ColorRole::SelectionBackground, QStringLiteral( "selectionBackgroundColor" ) },
59  {QgsCodeEditorColorScheme::ColorRole::SelectionForeground, QStringLiteral( "selectionForegroundColor" ) },
60  {QgsCodeEditorColorScheme::ColorRole::MatchedBraceBackground, QStringLiteral( "matchedBraceBackground" ) },
61  {QgsCodeEditorColorScheme::ColorRole::MatchedBraceForeground, QStringLiteral( "matchedBraceColor" ) },
62  {QgsCodeEditorColorScheme::ColorRole::Edge, QStringLiteral( "edgeColor" ) },
63  {QgsCodeEditorColorScheme::ColorRole::Fold, QStringLiteral( "foldColor" ) },
64  {QgsCodeEditorColorScheme::ColorRole::Error, QStringLiteral( "stderrFontColor" ) },
65  {QgsCodeEditorColorScheme::ColorRole::ErrorBackground, QStringLiteral( "stderrBackgroundColor" ) },
66  {QgsCodeEditorColorScheme::ColorRole::FoldIconForeground, QStringLiteral( "foldIconForeground" ) },
67  {QgsCodeEditorColorScheme::ColorRole::FoldIconHalo, QStringLiteral( "foldIconHalo" ) },
68  {QgsCodeEditorColorScheme::ColorRole::IndentationGuide, QStringLiteral( "indentationGuide" ) },
69 };
70 
71 
72 QgsCodeEditor::QgsCodeEditor( QWidget *parent, const QString &title, bool folding, bool margin )
73  : QsciScintilla( parent )
74  , mWidgetTitle( title )
75  , mFolding( folding )
76  , mMargin( margin )
77 {
78  if ( !parent && mWidgetTitle.isEmpty() )
79  {
80  setWindowTitle( QStringLiteral( "Text Editor" ) );
81  }
82  else
83  {
84  setWindowTitle( mWidgetTitle );
85  }
86  setSciWidget();
87  setHorizontalScrollBarPolicy( Qt::ScrollBarAsNeeded );
88 
89  SendScintilla( SCI_SETADDITIONALSELECTIONTYPING, 1 );
90  SendScintilla( SCI_SETMULTIPASTE, 1 );
91  SendScintilla( SCI_SETVIRTUALSPACEOPTIONS, SCVS_RECTANGULARSELECTION );
92 
93  SendScintilla( SCI_SETMARGINTYPEN, QgsCodeEditor::MarginRole::ErrorIndicators, SC_MARGIN_SYMBOL );
94  SendScintilla( SCI_SETMARGINMASKN, QgsCodeEditor::MarginRole::ErrorIndicators, 1 << MARKER_NUMBER );
95  setMarginWidth( QgsCodeEditor::MarginRole::ErrorIndicators, 0 );
96  setAnnotationDisplay( QsciScintilla::AnnotationBoxed );
97 
98  connect( QgsGui::instance(), &QgsGui::optionsChanged, this, [ = ]
99  {
100  setSciWidget();
101  initializeLexer();
102  } );
103 }
104 
105 // Workaround a bug in QScintilla 2.8.X
106 void QgsCodeEditor::focusOutEvent( QFocusEvent *event )
107 {
108 #if QSCINTILLA_VERSION >= 0x020800 && QSCINTILLA_VERSION < 0x020900
109  if ( event->reason() != Qt::ActiveWindowFocusReason )
110  {
111  /* There's a bug in all QScintilla 2.8.X, where
112  a focus out event that is not due to ActiveWindowFocusReason doesn't
113  lead to the bliking caret being disabled. The hack consists in making
114  QsciScintilla::focusOutEvent believe that the event is a ActiveWindowFocusReason
115  The bug was fixed in 2.9 per:
116  2015-04-14 Phil Thompson <[email protected]>
117 
118  * qt/qsciscintillabase.cpp:
119  Fixed a problem notifying when focus is lost to another application
120  widget.
121  [41734678234e]
122  */
123  QFocusEvent newFocusEvent( QEvent::FocusOut, Qt::ActiveWindowFocusReason );
124  QsciScintilla::focusOutEvent( &newFocusEvent );
125  }
126  else
127 #endif
128  {
129  QsciScintilla::focusOutEvent( event );
130  }
131 }
132 
133 // This workaround a likely bug in QScintilla. The ESC key should not be consumned
134 // by the main entry, so that the default behavior (Dialog closing) can trigger,
135 // but only is the auto-completion suggestion list isn't displayed
136 void QgsCodeEditor::keyPressEvent( QKeyEvent *event )
137 {
138  if ( event->key() == Qt::Key_Escape && !isListActive() )
139  {
140  // Shortcut QScintilla and redirect the event to the QWidget handler
141  QWidget::keyPressEvent( event ); // clazy:exclude=skipped-base-method
142  }
143  else
144  {
145  QsciScintilla::keyPressEvent( event );
146  }
147 }
148 
150 {
151 
152 }
153 
155 {
156  if ( mUseDefaultSettings )
157  return color( role );
158 
159  if ( !mOverrideColors )
160  {
161  return defaultColor( role, mColorScheme );
162  }
163  else
164  {
165  const QColor color = mCustomColors.value( role );
166  return !color.isValid() ? defaultColor( role ) : color;
167  }
168 }
169 
171 {
172  if ( mUseDefaultSettings )
173  return getMonospaceFont();
174 
175  QFont font = QFontDatabase::systemFont( QFontDatabase::FixedFont );
176 
177  const QgsSettings settings;
178  if ( !mFontFamily.isEmpty() )
179  font.setFamily( mFontFamily );
180 
181 #ifdef Q_OS_MAC
182  if ( mFontSize > 0 )
183  font.setPointSize( mFontSize );
184  else
185  {
186  // The font size gotten from getMonospaceFont() is too small on Mac
187  font.setPointSize( QLabel().font().pointSize() );
188  }
189 #else
190  if ( mFontSize > 0 )
191  font.setPointSize( mFontSize );
192  else
193  {
194  const int fontSize = settings.value( QStringLiteral( "qgis/stylesheet/fontPointSize" ), 10 ).toInt();
195  font.setPointSize( fontSize );
196  }
197 #endif
198  font.setBold( false );
199 
200  return font;
201 }
202 
204 {
205  setMatchedBraceForegroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MatchedBraceForeground ) );
206  setMatchedBraceBackgroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MatchedBraceBackground ) );
207 
208  SendScintilla( SCI_MARKERSETFORE, SC_MARKNUM_FOLDEROPEN, lexerColor( QgsCodeEditorColorScheme::ColorRole::FoldIconHalo ) );
209  SendScintilla( SCI_MARKERSETBACK, SC_MARKNUM_FOLDEROPEN, lexerColor( QgsCodeEditorColorScheme::ColorRole::FoldIconForeground ) );
210  SendScintilla( SCI_MARKERSETFORE, SC_MARKNUM_FOLDER, lexerColor( QgsCodeEditorColorScheme::ColorRole::FoldIconHalo ) );
211  SendScintilla( SCI_MARKERSETBACK, SC_MARKNUM_FOLDER, lexerColor( QgsCodeEditorColorScheme::ColorRole::FoldIconForeground ) );
212  SendScintilla( SCI_STYLESETFORE, STYLE_INDENTGUIDE, lexerColor( QgsCodeEditorColorScheme::ColorRole::IndentationGuide ) );
213  SendScintilla( SCI_STYLESETBACK, STYLE_INDENTGUIDE, lexerColor( QgsCodeEditorColorScheme::ColorRole::IndentationGuide ) );
214 }
215 
216 void QgsCodeEditor::setSciWidget()
217 {
218  const QFont font = lexerFont();
219  setFont( font );
220 
221  setUtf8( true );
222  setCaretLineVisible( true );
223  setCaretLineBackgroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::CaretLine ) );
224  setCaretForegroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::Cursor ) );
227 
228  setBraceMatching( QsciScintilla::SloppyBraceMatch );
229  setMatchedBraceForegroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MatchedBraceForeground ) );
230  setMatchedBraceBackgroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MatchedBraceBackground ) );
231 
232  setLineNumbersVisible( false );
233  setFoldingVisible( false );
234 
235  setMarginWidth( QgsCodeEditor::MarginRole::ErrorIndicators, 0 );
236 
239  setIndentationGuidesForegroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MarginForeground ) );
240  setIndentationGuidesBackgroundColor( lexerColor( QgsCodeEditorColorScheme::ColorRole::MarginBackground ) );
241  // whether margin will be shown
242  setFoldingVisible( mFolding );
243  const QColor foldColor = lexerColor( QgsCodeEditorColorScheme::ColorRole::Fold );
244  setFoldMarginColors( foldColor, foldColor );
245  // indentation
246  setAutoIndent( true );
247  setIndentationWidth( 4 );
248  setTabIndents( true );
249  setBackspaceUnindents( true );
250  setTabWidth( 4 );
251  // autocomplete
252  setAutoCompletionThreshold( 2 );
253  setAutoCompletionSource( QsciScintilla::AcsAPIs );
254 
255  markerDefine( QgsApplication::getThemePixmap( "console/iconSyntaxErrorConsoleParams.svg", lexerColor( QgsCodeEditorColorScheme::ColorRole::Error ),
257 }
258 
259 void QgsCodeEditor::setTitle( const QString &title )
260 {
261  setWindowTitle( title );
262 }
263 
265 {
266  mMargin = margin;
267  if ( margin )
268  {
269  QFont marginFont = lexerFont();
270  marginFont.setPointSize( 10 );
271  setMarginLineNumbers( 0, true );
272  setMarginsFont( marginFont );
273  setMarginWidth( QgsCodeEditor::MarginRole::LineNumbers, QStringLiteral( "00000" ) );
276  }
277  else
278  {
279  setMarginWidth( QgsCodeEditor::MarginRole::LineNumbers, 0 );
280  setMarginWidth( QgsCodeEditor::MarginRole::ErrorIndicators, 0 );
281  setMarginWidth( QgsCodeEditor::MarginRole::FoldingControls, 0 );
282  }
283 }
284 
286 {
287  if ( visible )
288  {
289  QFont marginFont = lexerFont();
290  marginFont.setPointSize( 10 );
291  setMarginLineNumbers( QgsCodeEditor::MarginRole::LineNumbers, true );
292  setMarginsFont( marginFont );
293  setMarginWidth( QgsCodeEditor::MarginRole::LineNumbers, QStringLiteral( "00000" ) );
296  }
297  else
298  {
299  setMarginLineNumbers( QgsCodeEditor::MarginRole::LineNumbers, false );
300  setMarginWidth( QgsCodeEditor::MarginRole::LineNumbers, 0 );
301  }
302 }
303 
305 {
306  return marginLineNumbers( QgsCodeEditor::MarginRole::LineNumbers );
307 }
308 
310 {
311  mFolding = folding;
312  if ( folding )
313  {
314  setMarginWidth( QgsCodeEditor::MarginRole::FoldingControls, "0" );
317  setFolding( QsciScintilla::PlainFoldStyle );
318  }
319  else
320  {
321  setFolding( QsciScintilla::NoFoldStyle );
322  setMarginWidth( QgsCodeEditor::MarginRole::FoldingControls, 0 );
323  }
324 }
325 
326 void QgsCodeEditor::insertText( const QString &text )
327 {
328  // Insert the text or replace selected text
329  if ( hasSelectedText() )
330  {
331  replaceSelectedText( text );
332  }
333  else
334  {
335  int line, index;
336  getCursorPosition( &line, &index );
337  insertAt( text, line, index );
338  setCursorPosition( line, index + text.length() );
339  }
340 }
341 
343 {
344  if ( theme.isEmpty() && QgsApplication::themeName() == QLatin1String( "default" ) )
345  {
346  // if using default theme, take certain colors from the palette
347  const QPalette pal = qApp->palette();
348 
349  switch ( role )
350  {
352  return pal.color( QPalette::Highlight );
354  return pal.color( QPalette::HighlightedText );
355  default:
356  break;
357  }
358  }
359  else if ( theme.isEmpty() )
360  {
361  // non default theme (e.g. Blend of Gray). Take colors from theme ini file...
362  const QSettings ini( QgsApplication::uiThemes().value( QgsApplication::themeName() ) + "/qscintilla.ini", QSettings::IniFormat );
363 
364  static const QMap< QgsCodeEditorColorScheme::ColorRole, QString > sColorRoleToIniKey
365  {
366  {QgsCodeEditorColorScheme::ColorRole::Default, QStringLiteral( "python/defaultFontColor" ) },
367  {QgsCodeEditorColorScheme::ColorRole::Keyword, QStringLiteral( "python/keywordFontColor" ) },
368  {QgsCodeEditorColorScheme::ColorRole::Class, QStringLiteral( "python/classFontColor" ) },
369  {QgsCodeEditorColorScheme::ColorRole::Method, QStringLiteral( "python/methodFontColor" ) },
370  {QgsCodeEditorColorScheme::ColorRole::Decoration, QStringLiteral( "python/decoratorFontColor" ) },
371  {QgsCodeEditorColorScheme::ColorRole::Number, QStringLiteral( "python/numberFontColor" ) },
372  {QgsCodeEditorColorScheme::ColorRole::Comment, QStringLiteral( "python/commentFontColor" ) },
373  {QgsCodeEditorColorScheme::ColorRole::CommentLine, QStringLiteral( "sql/commentLineFontColor" ) },
374  {QgsCodeEditorColorScheme::ColorRole::CommentBlock, QStringLiteral( "python/commentBlockFontColor" ) },
375  {QgsCodeEditorColorScheme::ColorRole::Background, QStringLiteral( "python/paperBackgroundColor" ) },
376  {QgsCodeEditorColorScheme::ColorRole::Cursor, QStringLiteral( "cursorColor" ) },
377  {QgsCodeEditorColorScheme::ColorRole::CaretLine, QStringLiteral( "caretLineColor" ) },
378  {QgsCodeEditorColorScheme::ColorRole::Operator, QStringLiteral( "sql/operatorFontColor" ) },
379  {QgsCodeEditorColorScheme::ColorRole::QuotedOperator, QStringLiteral( "sql/QuotedOperatorFontColor" ) },
380  {QgsCodeEditorColorScheme::ColorRole::Identifier, QStringLiteral( "sql/identifierFontColor" ) },
381  {QgsCodeEditorColorScheme::ColorRole::QuotedIdentifier, QStringLiteral( "sql/QuotedIdentifierFontColor" ) },
382  {QgsCodeEditorColorScheme::ColorRole::Tag, QStringLiteral( "html/tagFontColor" ) },
383  {QgsCodeEditorColorScheme::ColorRole::UnknownTag, QStringLiteral( "html/unknownTagFontColor" ) },
384  {QgsCodeEditorColorScheme::ColorRole::SingleQuote, QStringLiteral( "sql/singleQuoteFontColor" ) },
385  {QgsCodeEditorColorScheme::ColorRole::DoubleQuote, QStringLiteral( "sql/doubleQuoteFontColor" ) },
386  {QgsCodeEditorColorScheme::ColorRole::TripleSingleQuote, QStringLiteral( "python/tripleSingleQuoteFontColor" ) },
387  {QgsCodeEditorColorScheme::ColorRole::TripleDoubleQuote, QStringLiteral( "python/tripleDoubleQuoteFontColor" ) },
388  {QgsCodeEditorColorScheme::ColorRole::MarginBackground, QStringLiteral( "marginBackgroundColor" ) },
389  {QgsCodeEditorColorScheme::ColorRole::MarginForeground, QStringLiteral( "marginForegroundColor" ) },
390  {QgsCodeEditorColorScheme::ColorRole::SelectionBackground, QStringLiteral( "selectionBackgroundColor" ) },
391  {QgsCodeEditorColorScheme::ColorRole::SelectionForeground, QStringLiteral( "selectionForegroundColor" ) },
392  {QgsCodeEditorColorScheme::ColorRole::MatchedBraceBackground, QStringLiteral( "matchedBraceBackground" ) },
393  {QgsCodeEditorColorScheme::ColorRole::MatchedBraceForeground, QStringLiteral( "matchedBraceColor" ) },
394  {QgsCodeEditorColorScheme::ColorRole::Edge, QStringLiteral( "edgeColor" ) },
395  {QgsCodeEditorColorScheme::ColorRole::Fold, QStringLiteral( "foldColor" ) },
396  {QgsCodeEditorColorScheme::ColorRole::Error, QStringLiteral( "stderrFontColor" ) },
397  {QgsCodeEditorColorScheme::ColorRole::ErrorBackground, QStringLiteral( "stderrBackground" ) },
398  {QgsCodeEditorColorScheme::ColorRole::FoldIconForeground, QStringLiteral( "foldIconForeground" ) },
399  {QgsCodeEditorColorScheme::ColorRole::FoldIconHalo, QStringLiteral( "foldIconHalo" ) },
400  {QgsCodeEditorColorScheme::ColorRole::IndentationGuide, QStringLiteral( "indentationGuide" ) },
401  };
402 
403  const QgsCodeEditorColorScheme defaultScheme = QgsGui::codeEditorColorSchemeRegistry()->scheme( QStringLiteral( "default" ) );
404  return QgsSymbolLayerUtils::decodeColor( ini.value( sColorRoleToIniKey.value( role ), defaultScheme.color( role ).name() ).toString() );
405  }
406 
407  const QgsCodeEditorColorScheme scheme = QgsGui::codeEditorColorSchemeRegistry()->scheme( theme.isEmpty() ? QStringLiteral( "default" ) : theme );
408  return scheme.color( role );
409 }
410 
412 {
413  const QgsSettings settings;
414  if ( !settings.value( QStringLiteral( "codeEditor/overrideColors" ), false, QgsSettings::Gui ).toBool() )
415  {
416  const QString theme = settings.value( QStringLiteral( "codeEditor/colorScheme" ), QString(), QgsSettings::Gui ).toString();
417  return defaultColor( role, theme );
418  }
419  else
420  {
421  const QString color = settings.value( QStringLiteral( "codeEditor/%1" ).arg( sColorRoleToSettingsKey.value( role ) ), QString(), QgsSettings::Gui ).toString();
422  return color.isEmpty() ? defaultColor( role ) : QgsSymbolLayerUtils::decodeColor( color );
423  }
424 }
425 
427 {
428  QgsSettings settings;
429  if ( color.isValid() )
430  {
431  settings.setValue( QStringLiteral( "codeEditor/%1" ).arg( sColorRoleToSettingsKey.value( role ) ), color.name(), QgsSettings::Gui );
432  }
433  else
434  {
435  settings.remove( QStringLiteral( "codeEditor/%1" ).arg( sColorRoleToSettingsKey.value( role ) ), QgsSettings::Gui );
436  }
437 }
438 
439 // Settings for font and fontsize
440 bool QgsCodeEditor::isFixedPitch( const QFont &font )
441 {
442  return font.fixedPitch();
443 }
444 
446 {
447  QFont font = QFontDatabase::systemFont( QFontDatabase::FixedFont );
448 
449  const QgsSettings settings;
450  if ( !settings.value( QStringLiteral( "codeEditor/fontfamily" ), QString(), QgsSettings::Gui ).toString().isEmpty() )
451  font.setFamily( settings.value( QStringLiteral( "codeEditor/fontfamily" ), QString(), QgsSettings::Gui ).toString() );
452 
453  const int fontSize = settings.value( QStringLiteral( "codeEditor/fontsize" ), 0, QgsSettings::Gui ).toInt();
454 
455 #ifdef Q_OS_MAC
456  if ( fontSize > 0 )
457  font.setPointSize( fontSize );
458  else
459  {
460  // The font size gotten from getMonospaceFont() is too small on Mac
461  font.setPointSize( QLabel().font().pointSize() );
462  }
463 #else
464  if ( fontSize > 0 )
465  font.setPointSize( fontSize );
466  else
467  {
468  const int fontSize = settings.value( QStringLiteral( "qgis/stylesheet/fontPointSize" ), 10 ).toInt();
469  font.setPointSize( fontSize );
470  }
471 #endif
472  font.setBold( false );
473 
474  return font;
475 }
476 
477 void QgsCodeEditor::setCustomAppearance( const QString &scheme, const QMap<QgsCodeEditorColorScheme::ColorRole, QColor> &customColors, const QString &fontFamily, int fontSize )
478 {
479  mUseDefaultSettings = false;
480  mOverrideColors = !customColors.isEmpty();
481  mColorScheme = scheme;
482  mCustomColors = customColors;
483  mFontFamily = fontFamily;
484  mFontSize = fontSize;
485 
486  setSciWidget();
487  initializeLexer();
488 }
489 
490 void QgsCodeEditor::addWarning( const int lineNumber, const QString &warning )
491 {
492  setMarginWidth( QgsCodeEditor::MarginRole::ErrorIndicators, "000" );
493  markerAdd( lineNumber, MARKER_NUMBER );
494  QFont font = lexerFont();
495  font.setItalic( true );
496  const QsciStyle styleAnn = QsciStyle( -1, QStringLiteral( "Annotation" ),
499  font,
500  true );
501  annotate( lineNumber, warning, styleAnn );
502  mWarningLines.push_back( lineNumber );
503 }
504 
506 {
507  for ( const int line : mWarningLines )
508  {
509  markerDelete( line );
510  clearAnnotations( line );
511  }
512  setMarginWidth( QgsCodeEditor::MarginRole::ErrorIndicators, 0 );
513  mWarningLines.clear();
514 }
static QPixmap getThemePixmap(const QString &name, const QColor &foreColor=QColor(), const QColor &backColor=QColor(), int size=16)
Helper to get a theme icon as a pixmap.
static QHash< QString, QString > uiThemes()
All themes found in ~/.qgis3/themes folder.
static QString themeName()
Set the active theme to the specified theme.
QgsCodeEditorColorScheme scheme(const QString &id) const
Returns the color scheme with matching id.
Defines a color scheme for use in QgsCodeEditor widgets.
@ TripleSingleQuote
Triple single quote color.
@ CommentBlock
Comment block color.
@ QuotedOperator
Quoted operator color.
@ DoubleQuote
Double quote color.
@ QuotedIdentifier
Quoted identifier color.
@ SelectionForeground
Selection foreground color.
@ CommentLine
Line comment color.
@ FoldIconForeground
Fold icon foreground color.
@ MarginForeground
Margin foreground color.
@ ErrorBackground
Error background color.
@ MatchedBraceBackground
Matched brace background color.
@ IndentationGuide
Indentation guide line.
@ SingleQuote
Single quote color.
@ MarginBackground
Margin background color.
@ SelectionBackground
Selection background color.
@ MatchedBraceForeground
Matched brace foreground color.
@ TripleDoubleQuote
Triple double quote color.
@ FoldIconHalo
Fold icon halo color.
QColor color(ColorRole role) const
Returns the color to use in the editor for the specified role.
void setCustomAppearance(const QString &scheme=QString(), const QMap< QgsCodeEditorColorScheme::ColorRole, QColor > &customColors=QMap< QgsCodeEditorColorScheme::ColorRole, QColor >(), const QString &fontFamily=QString(), int fontSize=0)
Sets a custom appearance for the widget, disconnecting it from using the standard appearance taken fr...
static void setColor(QgsCodeEditorColorScheme::ColorRole role, const QColor &color)
Sets the color to use in the editor for the specified role.
void keyPressEvent(QKeyEvent *event) override
void setFoldingVisible(bool folding)
Set whether the folding controls are visible in the editor.
void runPostLexerConfigurationTasks()
Performs tasks which must be run after a lexer has been set for the widget.
virtual void initializeLexer()
Called when the dialect specific code lexer needs to be initialized (or reinitialized).
void setTitle(const QString &title)
Set the widget title.
void clearWarnings()
Clears all warning messages from the editor.
static QFont getMonospaceFont()
Returns the monospaced font to use for code editors.
void focusOutEvent(QFocusEvent *event) override
bool isFixedPitch(const QFont &font)
void insertText(const QString &text)
Insert text at cursor position, or replace any selected text if user has made a selection.
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.
QgsCodeEditor(QWidget *parent=nullptr, const QString &title=QString(), bool folding=false, bool margin=false)
Construct a new code editor.
bool lineNumbersVisible() const
Returns whether line numbers are visible in the editor.
QColor lexerColor(QgsCodeEditorColorScheme::ColorRole role) const
Returns the color to use in the lexer for the specified role.
Q_DECL_DEPRECATED void setMarginVisible(bool margin)
Set margin visible state.
static QColor defaultColor(QgsCodeEditorColorScheme::ColorRole role, const QString &theme=QString())
Returns the default color for the specified role.
void addWarning(int lineNumber, const QString &warning)
Adds a warning message and indicator to the specified a lineNumber.
static QColor color(QgsCodeEditorColorScheme::ColorRole role)
Returns the color to use in the editor for the specified role.
void optionsChanged()
This signal is emitted whenever the application options have been changed.
static QgsGui * instance()
Returns a pointer to the singleton instance.
Definition: qgsgui.cpp:69
static QgsCodeEditorColorSchemeRegistry * codeEditorColorSchemeRegistry()
Returns the global code editor color scheme registry, used for registering the color schemes for QgsC...
Definition: qgsgui.cpp:150
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static QColor decodeColor(const QString &str)