19 #include "qgssettings.h"
27 #include <QFontDatabase>
29 #include <QFocusEvent>
30 #include <Qsci/qscistyle.h>
32 QMap< QgsCodeEditorColorScheme::ColorRole, QString > QgsCodeEditor::sColorRoleToSettingsKey
73 : QsciScintilla( parent )
74 , mWidgetTitle( title )
78 if ( !parent && mWidgetTitle.isEmpty() )
80 setWindowTitle( QStringLiteral(
"Text Editor" ) );
84 setWindowTitle( mWidgetTitle );
87 setHorizontalScrollBarPolicy( Qt::ScrollBarAsNeeded );
89 SendScintilla( SCI_SETADDITIONALSELECTIONTYPING, 1 );
90 SendScintilla( SCI_SETMULTIPASTE, 1 );
91 SendScintilla( SCI_SETVIRTUALSPACEOPTIONS, SCVS_RECTANGULARSELECTION );
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 );
108 #if QSCINTILLA_VERSION >= 0x020800 && QSCINTILLA_VERSION < 0x020900
109 if ( event->reason() != Qt::ActiveWindowFocusReason )
123 QFocusEvent newFocusEvent( QEvent::FocusOut, Qt::ActiveWindowFocusReason );
124 QsciScintilla::focusOutEvent( &newFocusEvent );
129 QsciScintilla::focusOutEvent( event );
138 if ( event->key() == Qt::Key_Escape && !isListActive() )
141 QWidget::keyPressEvent( event );
145 QsciScintilla::keyPressEvent( event );
156 if ( mUseDefaultSettings )
157 return color( role );
159 if ( !mOverrideColors )
165 const QColor
color = mCustomColors.value( role );
172 if ( mUseDefaultSettings )
175 QFont font = QFontDatabase::systemFont( QFontDatabase::FixedFont );
177 QgsSettings settings;
178 if ( !mFontFamily.isEmpty() )
179 font.setFamily( mFontFamily );
183 font.setPointSize( mFontSize );
187 font.setPointSize( QLabel().font().pointSize() );
191 font.setPointSize( mFontSize );
194 int fontSize = settings.value( QStringLiteral(
"qgis/stylesheet/fontPointSize" ), 10 ).toInt();
195 font.setPointSize( fontSize );
198 font.setBold(
false );
216 void QgsCodeEditor::setSciWidget()
222 setCaretLineVisible(
true );
228 setBraceMatching( QsciScintilla::SloppyBraceMatch );
235 setMarginWidth( QgsCodeEditor::MarginRole::ErrorIndicators, 0 );
244 setFoldMarginColors( foldColor, foldColor );
246 setAutoIndent(
true );
247 setIndentationWidth( 4 );
248 setTabIndents(
true );
249 setBackspaceUnindents(
true );
252 setAutoCompletionThreshold( 2 );
253 setAutoCompletionSource( QsciScintilla::AcsAPIs );
261 setWindowTitle( title );
270 marginFont.setPointSize( 10 );
271 setMarginLineNumbers( 0,
true );
272 setMarginsFont( marginFont );
273 setMarginWidth( QgsCodeEditor::MarginRole::LineNumbers, QStringLiteral(
"00000" ) );
279 setMarginWidth( QgsCodeEditor::MarginRole::LineNumbers, 0 );
280 setMarginWidth( QgsCodeEditor::MarginRole::ErrorIndicators, 0 );
281 setMarginWidth( QgsCodeEditor::MarginRole::FoldingControls, 0 );
290 marginFont.setPointSize( 10 );
291 setMarginLineNumbers( QgsCodeEditor::MarginRole::LineNumbers,
true );
292 setMarginsFont( marginFont );
293 setMarginWidth( QgsCodeEditor::MarginRole::LineNumbers, QStringLiteral(
"00000" ) );
299 setMarginLineNumbers( QgsCodeEditor::MarginRole::LineNumbers,
false );
300 setMarginWidth( QgsCodeEditor::MarginRole::LineNumbers, 0 );
306 return marginLineNumbers( QgsCodeEditor::MarginRole::LineNumbers );
314 setMarginWidth( QgsCodeEditor::MarginRole::FoldingControls,
"0" );
317 setFolding( QsciScintilla::PlainFoldStyle );
321 setFolding( QsciScintilla::NoFoldStyle );
322 setMarginWidth( QgsCodeEditor::MarginRole::FoldingControls, 0 );
329 if ( hasSelectedText() )
331 replaceSelectedText( text );
336 getCursorPosition( &line, &index );
337 insertAt( text, line, index );
338 setCursorPosition( line, index + text.length() );
347 QPalette
pal = qApp->palette();
352 return pal.color( QPalette::Highlight );
354 return pal.color( QPalette::HighlightedText );
359 else if ( theme.isEmpty() )
364 static QMap< QgsCodeEditorColorScheme::ColorRole, QString > sColorRoleToIniKey
408 return scheme.
color( role );
413 const QgsSettings settings;
414 if ( !settings.value( QStringLiteral(
"codeEditor/overrideColors" ),
false, QgsSettings::Gui ).toBool() )
416 const QString theme = settings.value( QStringLiteral(
"codeEditor/colorScheme" ), QString(), QgsSettings::Gui ).toString();
421 const QString
color = settings.value( QStringLiteral(
"codeEditor/%1" ).arg( sColorRoleToSettingsKey.value( role ) ), QString(), QgsSettings::Gui ).toString();
428 QgsSettings settings;
429 if (
color.isValid() )
431 settings.setValue( QStringLiteral(
"codeEditor/%1" ).arg( sColorRoleToSettingsKey.value( role ) ),
color.name(), QgsSettings::Gui );
435 settings.remove( QStringLiteral(
"codeEditor/%1" ).arg( sColorRoleToSettingsKey.value( role ) ), QgsSettings::Gui );
442 return font.fixedPitch();
447 QFont font = QFontDatabase::systemFont( QFontDatabase::FixedFont );
449 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() );
453 const int fontSize = settings.value( QStringLiteral(
"codeEditor/fontsize" ), 0, QgsSettings::Gui ).toInt();
457 font.setPointSize( fontSize );
461 font.setPointSize( QLabel().font().pointSize() );
465 font.setPointSize( fontSize );
468 int fontSize = settings.value( QStringLiteral(
"qgis/stylesheet/fontPointSize" ), 10 ).toInt();
469 font.setPointSize( fontSize );
472 font.setBold(
false );
479 mUseDefaultSettings =
false;
480 mOverrideColors = !customColors.isEmpty();
481 mColorScheme = scheme;
482 mCustomColors = customColors;
483 mFontFamily = fontFamily;
484 mFontSize = fontSize;
492 setMarginWidth( QgsCodeEditor::MarginRole::ErrorIndicators,
"000" );
493 markerAdd( lineNumber, MARKER_NUMBER );
495 font.setItalic(
true );
496 const QsciStyle styleAnn = QsciStyle( -1, QStringLiteral(
"Annotation" ),
501 annotate( lineNumber, warning, styleAnn );
502 mWarningLines.push_back( lineNumber );
507 for (
int line : mWarningLines )
509 markerDelete( line );
510 clearAnnotations( line );
512 setMarginWidth( QgsCodeEditor::MarginRole::ErrorIndicators, 0 );
513 mWarningLines.clear();
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 QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
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.
@ Decoration
Decoration color.
@ Identifier
Identifier 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.
@ Default
Default text color.
@ CaretLine
Caret line color.
@ IndentationGuide
Indentation guide line.
@ Background
Background color.
@ SingleQuote
Single quote color.
@ MarginBackground
Margin background color.
@ SelectionBackground
Selection background color.
@ MatchedBraceForeground
Matched brace foreground color.
@ Operator
Operator 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.
static QgsCodeEditorColorSchemeRegistry * codeEditorColorSchemeRegistry()
Returns the global code editor color scheme registry, used for registering the color schemes for QgsC...
static QColor decodeColor(const QString &str)