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 );
108#if QSCINTILLA_VERSION < 0x020d03
109 installEventFilter(
this );
116#if QSCINTILLA_VERSION >= 0x020800 && QSCINTILLA_VERSION < 0x020900
117 if ( event->reason() != Qt::ActiveWindowFocusReason )
131 QFocusEvent newFocusEvent( QEvent::FocusOut, Qt::ActiveWindowFocusReason );
132 QsciScintilla::focusOutEvent( &newFocusEvent );
137 QsciScintilla::focusOutEvent( event );
146 if ( event->key() == Qt::Key_Escape && !isListActive() )
149 QWidget::keyPressEvent( event );
153 QsciScintilla::keyPressEvent( event );
159#if QSCINTILLA_VERSION < 0x020d03
160 if ( watched ==
this && event->type() == QEvent::InputMethod )
169 return QsciScintilla::eventFilter( watched, event );
179 if ( mUseDefaultSettings )
180 return color( role );
182 if ( !mOverrideColors )
188 const QColor
color = mCustomColors.value( role );
195 if ( mUseDefaultSettings )
198 QFont font = QFontDatabase::systemFont( QFontDatabase::FixedFont );
201 if ( !mFontFamily.isEmpty() )
202 font.setFamily( mFontFamily );
206 font.setPointSize( mFontSize );
210 font.setPointSize( QLabel().font().pointSize() );
214 font.setPointSize( mFontSize );
217 const int fontSize = settings.
value( QStringLiteral(
"qgis/stylesheet/fontPointSize" ), 10 ).toInt();
218 font.setPointSize( fontSize );
221 font.setBold(
false );
241void QgsCodeEditor::setSciWidget()
247 setCaretLineVisible(
true );
253 setBraceMatching( QsciScintilla::SloppyBraceMatch );
260 setFolding( QsciScintilla::NoFoldStyle );
272 setFoldMarginColors( foldColor, foldColor );
274 setAutoIndent(
true );
275 setIndentationWidth( 4 );
276 setTabIndents(
true );
277 setBackspaceUnindents(
true );
280 setAutoCompletionThreshold( 2 );
281 setAutoCompletionSource( QsciScintilla::AcsAPIs );
289 setWindowTitle( title );
298 marginFont.setPointSize( 10 );
299 setMarginLineNumbers( 0,
true );
300 setMarginsFont( marginFont );
318 marginFont.setPointSize( 10 );
320 setMarginsFont( marginFont );
355void QgsCodeEditor::updateFolding()
362 setFolding( QsciScintilla::PlainFoldStyle );
366 setFolding( QsciScintilla::NoFoldStyle );
374 if ( hasSelectedText() )
376 replaceSelectedText( text );
381 getCursorPosition( &line, &index );
382 insertAt( text, line, index );
383 setCursorPosition( line, index + text.length() );
392 const QPalette
pal = qApp->palette();
397 return pal.color( QPalette::Highlight );
399 return pal.color( QPalette::HighlightedText );
404 else if ( theme.isEmpty() )
409 static const QMap< QgsCodeEditorColorScheme::ColorRole, QString > sColorRoleToIniKey
453 return scheme.
color( role );
459 if ( !settings.
value( QStringLiteral(
"codeEditor/overrideColors" ),
false,
QgsSettings::Gui ).toBool() )
461 const QString theme = settings.
value( QStringLiteral(
"codeEditor/colorScheme" ), QString(),
QgsSettings::Gui ).toString();
466 const QString
color = settings.
value( QStringLiteral(
"codeEditor/%1" ).arg( sColorRoleToSettingsKey.value( role ) ), QString(),
QgsSettings::Gui ).toString();
474 if (
color.isValid() )
480 settings.
remove( QStringLiteral(
"codeEditor/%1" ).arg( sColorRoleToSettingsKey.value( role ) ),
QgsSettings::Gui );
487 return font.fixedPitch();
492 QFont font = QFontDatabase::systemFont( QFontDatabase::FixedFont );
495 if ( !settings.
value( QStringLiteral(
"codeEditor/fontfamily" ), QString(),
QgsSettings::Gui ).toString().isEmpty() )
496 font.setFamily( settings.
value( QStringLiteral(
"codeEditor/fontfamily" ), QString(),
QgsSettings::Gui ).toString() );
498 const int fontSize = settings.
value( QStringLiteral(
"codeEditor/fontsize" ), 0,
QgsSettings::Gui ).toInt();
502 font.setPointSize( fontSize );
506 font.setPointSize( QLabel().font().pointSize() );
510 font.setPointSize( fontSize );
513 const int fontSize = settings.
value( QStringLiteral(
"qgis/stylesheet/fontPointSize" ), 10 ).toInt();
514 font.setPointSize( fontSize );
517 font.setBold(
false );
524 mUseDefaultSettings =
false;
525 mOverrideColors = !customColors.isEmpty();
526 mColorScheme = scheme;
527 mCustomColors = customColors;
528 mFontFamily = fontFamily;
529 mFontSize = fontSize;
538 markerAdd( lineNumber, MARKER_NUMBER );
540 font.setItalic(
true );
541 const QsciStyle styleAnn = QsciStyle( -1, QStringLiteral(
"Annotation" ),
546 annotate( lineNumber, warning, styleAnn );
547 mWarningLines.push_back( lineNumber );
552 for (
const int line : mWarningLines )
554 markerDelete( line );
555 clearAnnotations( line );
558 mWarningLines.clear();
565 getCursorPosition( &line, &index );
566 return line == lines() - 1;
571 setCursorPosition( 0, 0 );
572 ensureCursorVisible();
573 ensureLineVisible( 0 );
578 const int endLine = lines() - 1;
579 const int endLineLength = lineLength( endLine );
580 setCursorPosition( endLine, endLineLength );
581 ensureCursorVisible();
582 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.
bool eventFilter(QObject *watched, QEvent *event) override
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)