Skip to content
Permalink
Browse files

Move more code out of QgsPythonConsoleBase

  • Loading branch information
nyalldawson committed Oct 5, 2020
1 parent 3aef8ec commit e66a0a025b3299834af752d13f990f5f779126aa
@@ -29,18 +29,9 @@

class QgsPythonConsoleBase(QgsCodeEditorPython):

MARKER_NUM = 6

def __init__(self, parent=None):
super().__init__(parent)

# Enable non-ascii chars
self.setUtf8(True)

# Set the default font
font = QFontDatabase.systemFont(QFontDatabase.FixedFont)
self.setFont(font)

# Margin 0 is used for line numbers (editor and output)
self.setMarginWidth(0, "00000")
# Margin 1 is used for the '>>>' prompt (console input)
@@ -49,9 +40,6 @@ def __init__(self, parent=None):
# Margin 2 is used for the 'folding' (editor)
self.setMarginWidth(2, "0")

self.setCaretLineVisible(True)
self.setCaretWidth(2)

self.iconRun = QgsApplication.getThemeIcon("console/mIconRunConsole.svg")
self.iconRunScript = QgsApplication.getThemeIcon("mActionStart.svg")
self.iconUndo = QgsApplication.getThemeIcon("mActionUndo.svg")
@@ -72,40 +60,7 @@ def __init__(self, parent=None):
self.iconPyQGISHelp = QgsApplication.getThemeIcon("console/iconHelpConsole.svg")

def setLexers(self):
self.lexer = QsciLexerPython()
self.lexer.setIndentationWarning(QsciLexerPython.Inconsistent)
self.lexer.setFoldComments(True)
self.lexer.setFoldQuotes(True)

font = self.getMonospaceFont()

self.lexer.setDefaultFont(font)
self.lexer.setDefaultColor(self.color(QgsCodeEditor.ColorRole.Default))
self.lexer.setColor(self.color(QgsCodeEditor.ColorRole.Comment), 1)
self.lexer.setColor(self.color(QgsCodeEditor.ColorRole.Number), 2)
self.lexer.setColor(self.color(QgsCodeEditor.ColorRole.Keyword), 5)
self.lexer.setColor(self.color(QgsCodeEditor.ColorRole.Class), 8)
self.lexer.setColor(self.color(QgsCodeEditor.ColorRole.Method), 9)
self.lexer.setColor(self.color(QgsCodeEditor.ColorRole.Decoration), 15)
self.lexer.setColor(self.color(QgsCodeEditor.ColorRole.CommentBlock), 12)
self.lexer.setColor(self.color(QgsCodeEditor.ColorRole.SingleQuote), 4)
self.lexer.setColor(self.color(QgsCodeEditor.ColorRole.DoubleQuote), 3)
self.lexer.setColor(self.color(QgsCodeEditor.ColorRole.TripleSingleQuote), 6)
self.lexer.setColor(self.color(QgsCodeEditor.ColorRole.TripleDoubleQuote), 7)
self.lexer.setColor(self.color(QgsCodeEditor.ColorRole.Default), 13)
self.lexer.setColor(QColor(Qt.red), 14)
self.lexer.setFont(font, 1)
self.lexer.setFont(font, 2)
self.lexer.setFont(font, 3)
self.lexer.setFont(font, 4)
self.lexer.setFont(font, QsciLexerPython.UnclosedString)

# ? only for editor and console ?
paperColor = self.color(QgsCodeEditor.ColorRole.Background)
for style in range(0, 33):
self.lexer.setPaper(paperColor, style)

self.api = QsciAPIs(self.lexer)
self.api = QsciAPIs(self.lexer())
checkBoxAPI = self.settings.value("pythonConsole/preloadAPI", True, type=bool)
checkBoxPreparedAPI = self.settings.value("pythonConsole/usePreparedAPIFile", False, type=bool)
if checkBoxAPI:
@@ -118,9 +73,7 @@ def setLexers(self):
for i in range(0, len(apiPath)):
self.api.load(apiPath[i])
self.api.prepare()
self.lexer.setAPIs(self.api)

self.setLexer(self.lexer)
self.lexer().setAPIs(self.api)


if __name__ == "__main__":
@@ -81,6 +81,8 @@ def eventFilter(self, obj, event):

class Editor(QgsPythonConsoleBase):

MARKER_NUM = 6

def __init__(self, parent=None):
super(Editor, self).__init__(parent)
self.parent = parent
@@ -98,41 +100,14 @@ def __init__(self, parent=None):
self.MARKER_NUM)

self.setMinimumHeight(120)
# self.setMinimumWidth(300)

self.setBraceMatching(QsciScintilla.SloppyBraceMatch)

# Folding
self.setFolding(QsciScintilla.PlainFoldStyle)
# self.setWrapMode(QsciScintilla.WrapWord)

# Edge Mode
self.setEdgeMode(QsciScintilla.EdgeLine)
self.setEdgeColumn(80)

self.SendScintilla(self.SCI_SETADDITIONALSELECTIONTYPING, 1)
self.SendScintilla(self.SCI_SETMULTIPASTE, 1)
self.SendScintilla(self.SCI_SETVIRTUALSPACEOPTIONS, self.SCVS_RECTANGULARSELECTION)

# self.setWrapMode(QsciScintilla.WrapCharacter)
self.setWhitespaceVisibility(QsciScintilla.WsVisibleAfterIndent)
# self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)

self.settingsEditor()

# Annotations
self.setAnnotationDisplay(QsciScintilla.ANNOTATION_BOXED)

# Indentation
self.setAutoIndent(True)
self.setIndentationsUseTabs(False)
self.setIndentationWidth(4)
self.setTabIndents(True)
self.setBackspaceUnindents(True)
self.setTabWidth(4)
self.setIndentationGuides(True)

# Disable command key
ctrl, shift = self.SCMOD_CTRL << 16, self.SCMOD_SHIFT << 16
self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('L') + ctrl)
@@ -167,18 +142,6 @@ def __init__(self, parent=None):
self.modificationAttempted.connect(self.fileReadOnly)

def settingsEditor(self):
self.setSelectionForegroundColor(self.color(QgsCodeEditor.ColorRole.SelectionForeground))
self.setSelectionBackgroundColor(self.color(QgsCodeEditor.ColorRole.SelectionBackground))
self.setMatchedBraceBackgroundColor(self.color(QgsCodeEditor.ColorRole.MatchedBraceBackground))
self.setMatchedBraceForegroundColor(self.color(QgsCodeEditor.ColorRole.MatchedBraceForeground))
self.setMarginsForegroundColor(self.color(QgsCodeEditor.ColorRole.MarginForeground))
self.setMarginsBackgroundColor(self.color(QgsCodeEditor.ColorRole.MarginBackground))
self.setIndentationGuidesForegroundColor(self.color(QgsCodeEditor.ColorRole.MarginForeground))
self.setIndentationGuidesBackgroundColor(self.color(QgsCodeEditor.ColorRole.MarginBackground))
self.setEdgeColor(self.color(QgsCodeEditor.ColorRole.Edge))
foldColor = self.color(QgsCodeEditor.ColorRole.Fold)
self.setFoldMarginColors(foldColor, foldColor)

# Set Python lexer
self.setLexers()
threshold = self.settings.value("pythonConsole/autoCompThreshold", 2, type=int)
@@ -195,9 +158,6 @@ def settingsEditor(self):
else:
self.setAutoCompletionSource(self.AcsNone)

self.setCaretLineBackgroundColor(self.color(QgsCodeEditor.ColorRole.CaretLine))
self.setCaretForegroundColor(self.color(QgsCodeEditor.ColorRole.Cursor))

def autoCompleteKeyBinding(self):
radioButtonSource = self.settings.value("pythonConsole/autoCompleteSource", 'fromAPI')
autoCompEnabled = self.settings.value("pythonConsole/autoCompleteEnabled", True, type=bool)
@@ -156,13 +156,6 @@ def insertInitText(self):
def refreshSettingsOutput(self):
# Set Python lexer
self.setLexers()
self.setSelectionForegroundColor(self.color(QgsCodeEditor.ColorRole.SelectionForeground))
self.setSelectionBackgroundColor(self.color(QgsCodeEditor.ColorRole.SelectionBackground))
self.setMarginsForegroundColor(self.color(QgsCodeEditor.ColorRole.MarginForeground))
self.setMarginsBackgroundColor(self.color(QgsCodeEditor.ColorRole.MarginBackground))

self.setCaretLineBackgroundColor(self.color(QgsCodeEditor.ColorRole.CaretLine))
self.setCaretForegroundColor(self.color(QgsCodeEditor.ColorRole.Cursor))

def clearConsole(self):
self.setText('')
@@ -61,9 +61,6 @@ def __init__(self, parent=None):

self.new_input_line = True

self.setCaretLineVisible(False)
self.setMarginLineNumbers(0, False) # NO linenumbers for the input line

self.buffer = []
self.continuationLine = False

@@ -80,10 +77,6 @@ def __init__(self, parent=None):

self.historyDlg = HistoryDialog(self)

# Brace matching: enable for a brace immediately before or after
# the current position
self.setBraceMatching(QsciScintilla.SloppyBraceMatch)

self.refreshSettingsShell()

# Don't want to see the horizontal scrollbar at all
@@ -115,7 +108,7 @@ def __init__(self, parent=None):
self.newShortcutCSS.activated.connect(self.showHistory)

def _setMinimumHeight(self):
font = self.lexer.defaultFont(0)
font = self.lexer().defaultFont(0)
fm = QFontMetrics(font)

self.setMinimumHeight(fm.height() + 10)
@@ -137,18 +130,12 @@ def refreshSettingsShell(self):
else:
self.setAutoCompletionSource(self.AcsNone)

self.setCaretLineBackgroundColor(self.color(QgsCodeEditor.ColorRole.CaretLine))
self.setCaretForegroundColor(self.color(QgsCodeEditor.ColorRole.Cursor))

self.setSelectionForegroundColor(self.color(QgsCodeEditor.ColorRole.SelectionForeground))
self.setSelectionBackgroundColor(self.color(QgsCodeEditor.ColorRole.SelectionBackground))
self.setMatchedBraceBackgroundColor(self.color(QgsCodeEditor.ColorRole.MatchedBraceBackground))
self.setMatchedBraceForegroundColor(self.color(QgsCodeEditor.ColorRole.MatchedBraceForeground))
self.setMarginsBackgroundColor(self.color(QgsCodeEditor.ColorRole.Background))

# Sets minimum height for input area based of font metric
self._setMinimumHeight()

self.setCaretLineVisible(False)
self.setMarginLineNumbers(0, False) # NO linenumbers for the input line

def showHistory(self):
if not self.historyDlg.isVisible():
self.historyDlg.show()
@@ -149,23 +149,23 @@ QgsCodeEditorOptionsWidget::QgsCodeEditorOptionsWidget( QWidget *parent )
} );

mPythonPreview->setText( R"""(def simple_function(x,y,z):
"""
Function docstring
"""
return [1, 1.2, "val", 'a string', {'a': 1, 'b': 2}]
"""
Function docstring
"""
return [1, 1.2, "val", 'a string', {'a': 1, 'b': 2}]
@my_decorator
def somefunc(param1: str='', param2=0):
r'''A docstring'''
if param1 > param2: # interesting
print('Gre\'ater'.lower())
return (param2 - param1 + 1 + 0b10l) or None
'''A docstring'''
if param1 > param2: # interesting
print('Gre\'ater'.lower())
return (param2 - param1 + 1 + 0b10l) or None
class SomeClass:
"""
My class docstring
"""
pass
"""
My class docstring
"""
pass
)""" );

mExpressionPreview->setText( R"""(aggregate(layer:='rail_stations',
@@ -35,14 +35,15 @@ QgsCodeEditorPython::QgsCodeEditorPython( QWidget *parent, const QList<QString>
{
setTitle( tr( "Python Editor" ) );
}

setCaretWidth( 2 );

QgsCodeEditorPython::initializeLexer();
}

void QgsCodeEditorPython::initializeLexer()
{
// current line
setCaretWidth( 2 );

setEdgeMode( QsciScintilla::EdgeLine );
setEdgeColumn( 80 );
setEdgeColor( lexerColor( ColorRole::Edge ) );
@@ -53,6 +54,11 @@ void QgsCodeEditorPython::initializeLexer()
QColor defaultColor = lexerColor( ColorRole::Default );

QsciLexerPython *pyLexer = new QsciLexerPython( this );

pyLexer->setIndentationWarning( QsciLexerPython::Inconsistent );
pyLexer->setFoldComments( true );
pyLexer->setFoldQuotes( true );

pyLexer->setDefaultFont( font );
pyLexer->setDefaultColor( defaultColor );
pyLexer->setDefaultPaper( lexerColor( ColorRole::Background ) );
@@ -126,6 +132,7 @@ void QgsCodeEditorPython::initializeLexer()
setMarginVisible( true );
setFoldingVisible( true );
setIndentationsUseTabs( false );
setIndentationGuides( true );
}


0 comments on commit e66a0a0

Please sign in to comment.
You can’t perform that action at this time.