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 );
178 if ( !mFontFamily.isEmpty() )
179 font.setFamily( mFontFamily );
183 font.setPointSize( mFontSize );
187 font.setPointSize( QLabel().font().pointSize() );
191 font.setPointSize( mFontSize );
194 const 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 const QPalette
pal = qApp->palette();
352 return pal.color( QPalette::Highlight );
354 return pal.color( QPalette::HighlightedText );
359 else if ( theme.isEmpty() )
364 static const QMap< QgsCodeEditorColorScheme::ColorRole, QString > sColorRoleToIniKey
408 return scheme.
color( role );
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();
429 if (
color.isValid() )
435 settings.
remove( QStringLiteral(
"codeEditor/%1" ).arg( sColorRoleToSettingsKey.value( role ) ),
QgsSettings::Gui );
442 return font.fixedPitch();
447 QFont font = QFontDatabase::systemFont( QFontDatabase::FixedFont );
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 const 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 (
const int line : mWarningLines )
509 markerDelete( line );
510 clearAnnotations( line );
512 setMarginWidth( QgsCodeEditor::MarginRole::ErrorIndicators, 0 );
513 mWarningLines.clear();