27#include <QFontDatabase>
30#include <Qsci/qscistyle.h>
32QMap< QgsCodeEditorColorScheme::ColorRole, QString > QgsCodeEditor::sColorRoleToSettingsKey
73 : QsciScintilla( parent )
74 , mWidgetTitle( title )
78 if ( !parent && mWidgetTitle.isEmpty() )
80 setWindowTitle( QStringLiteral(
"Text Editor" ) );
84 setWindowTitle( mWidgetTitle );
91 setHorizontalScrollBarPolicy( Qt::ScrollBarAsNeeded );
93 SendScintilla( SCI_SETADDITIONALSELECTIONTYPING, 1 );
94 SendScintilla( SCI_SETMULTIPASTE, 1 );
95 SendScintilla( SCI_SETVIRTUALSPACEOPTIONS, SCVS_RECTANGULARSELECTION );
100 setAnnotationDisplay( QsciScintilla::AnnotationBoxed );
112#if QSCINTILLA_VERSION >= 0x020800 && QSCINTILLA_VERSION < 0x020900
113 if ( event->reason() != Qt::ActiveWindowFocusReason )
127 QFocusEvent newFocusEvent( QEvent::FocusOut, Qt::ActiveWindowFocusReason );
128 QsciScintilla::focusOutEvent( &newFocusEvent );
133 QsciScintilla::focusOutEvent( event );
142 if ( event->key() == Qt::Key_Escape && !isListActive() )
145 QWidget::keyPressEvent( event );
149 QsciScintilla::keyPressEvent( event );
160 if ( mUseDefaultSettings )
161 return color( role );
163 if ( !mOverrideColors )
169 const QColor
color = mCustomColors.value( role );
176 if ( mUseDefaultSettings )
179 QFont font = QFontDatabase::systemFont( QFontDatabase::FixedFont );
182 if ( !mFontFamily.isEmpty() )
183 font.setFamily( mFontFamily );
187 font.setPointSize( mFontSize );
191 font.setPointSize( QLabel().font().pointSize() );
195 font.setPointSize( mFontSize );
198 const int fontSize = settings.
value( QStringLiteral(
"qgis/stylesheet/fontPointSize" ), 10 ).toInt();
199 font.setPointSize( fontSize );
202 font.setBold(
false );
222void QgsCodeEditor::setSciWidget()
228 setCaretLineVisible(
true );
234 setBraceMatching( QsciScintilla::SloppyBraceMatch );
241 setFolding( QsciScintilla::NoFoldStyle );
253 setFoldMarginColors( foldColor, foldColor );
255 setAutoIndent(
true );
256 setIndentationWidth( 4 );
257 setTabIndents(
true );
258 setBackspaceUnindents(
true );
261 setAutoCompletionThreshold( 2 );
262 setAutoCompletionSource( QsciScintilla::AcsAPIs );
270 setWindowTitle( title );
279 marginFont.setPointSize( 10 );
280 setMarginLineNumbers( 0,
true );
281 setMarginsFont( marginFont );
299 marginFont.setPointSize( 10 );
301 setMarginsFont( marginFont );
336void QgsCodeEditor::updateFolding()
343 setFolding( QsciScintilla::PlainFoldStyle );
347 setFolding( QsciScintilla::NoFoldStyle );
355 if ( hasSelectedText() )
357 replaceSelectedText( text );
362 getCursorPosition( &line, &index );
363 insertAt( text, line, index );
364 setCursorPosition( line, index + text.length() );
373 const QPalette
pal = qApp->palette();
378 return pal.color( QPalette::Highlight );
380 return pal.color( QPalette::HighlightedText );
385 else if ( theme.isEmpty() )
390 static const QMap< QgsCodeEditorColorScheme::ColorRole, QString > sColorRoleToIniKey
434 return scheme.
color( role );
440 if ( !settings.
value( QStringLiteral(
"codeEditor/overrideColors" ),
false,
QgsSettings::Gui ).toBool() )
442 const QString theme = settings.
value( QStringLiteral(
"codeEditor/colorScheme" ), QString(),
QgsSettings::Gui ).toString();
447 const QString
color = settings.
value( QStringLiteral(
"codeEditor/%1" ).arg( sColorRoleToSettingsKey.value( role ) ), QString(),
QgsSettings::Gui ).toString();
455 if (
color.isValid() )
461 settings.
remove( QStringLiteral(
"codeEditor/%1" ).arg( sColorRoleToSettingsKey.value( role ) ),
QgsSettings::Gui );
468 return font.fixedPitch();
473 QFont font = QFontDatabase::systemFont( QFontDatabase::FixedFont );
476 if ( !settings.
value( QStringLiteral(
"codeEditor/fontfamily" ), QString(),
QgsSettings::Gui ).toString().isEmpty() )
477 font.setFamily( settings.
value( QStringLiteral(
"codeEditor/fontfamily" ), QString(),
QgsSettings::Gui ).toString() );
479 const int fontSize = settings.
value( QStringLiteral(
"codeEditor/fontsize" ), 0,
QgsSettings::Gui ).toInt();
483 font.setPointSize( fontSize );
487 font.setPointSize( QLabel().font().pointSize() );
491 font.setPointSize( fontSize );
494 const int fontSize = settings.
value( QStringLiteral(
"qgis/stylesheet/fontPointSize" ), 10 ).toInt();
495 font.setPointSize( fontSize );
498 font.setBold(
false );
505 mUseDefaultSettings =
false;
506 mOverrideColors = !customColors.isEmpty();
507 mColorScheme = scheme;
508 mCustomColors = customColors;
509 mFontFamily = fontFamily;
510 mFontSize = fontSize;
519 markerAdd( lineNumber, MARKER_NUMBER );
521 font.setItalic(
true );
522 const QsciStyle styleAnn = QsciStyle( -1, QStringLiteral(
"Annotation" ),
527 annotate( lineNumber, warning, styleAnn );
528 mWarningLines.push_back( lineNumber );
533 for (
const int line : mWarningLines )
535 markerDelete( line );
536 clearAnnotations( line );
539 mWarningLines.clear();
546 getCursorPosition( &line, &index );
547 return line == lines() - 1;
552 setCursorPosition( 0, 0 );
553 ensureCursorVisible();
554 ensureLineVisible( 0 );
559 const int endLine = lines() - 1;
560 const int endLineLength = lineLength( endLine );
561 setCursorPosition( endLine, endLineLength );
562 ensureCursorVisible();
563 ensureLineVisible( endLine );
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.
@ 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
virtual void moveCursorToStart()
Moves the cursor to the start of the document and scrolls to ensure it is visible.
void setFoldingVisible(bool folding)
Set whether the folding controls are visible in the editor.
@ FoldingControls
Folding controls.
@ ErrorIndicators
Error indicators.
@ LineNumbers
Line numbers.
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
@ CodeFolding
Indicates that code folding should be enabled for the editor.
bool isCursorOnLastLine() const
Returns true if the cursor is on the last line of the document.
bool isFixedPitch(const QFont &font)
QgsCodeEditor(QWidget *parent=nullptr, const QString &title=QString(), bool folding=false, bool margin=false, QgsCodeEditor::Flags flags=QgsCodeEditor::Flags())
Flags controlling behavior of code editor.
void insertText(const QString &text)
Insert text at cursor position, or replace any selected text if user has made a selection.
virtual void moveCursorToEnd()
Moves the cursor to the end of the document and scrolls to ensure it is visible.
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.
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.
bool foldingVisible()
Returns true if the folding controls are visible in the editor.
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...
This class is a composition of two QSettings instances:
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)