|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + ScriptEdit.py |
| 6 | + --------------------- |
| 7 | + Date : April 2013 |
| 8 | + Copyright : (C) 2013 by Alexander Bruy |
| 9 | + Email : alexander dot bruy at gmail dot com |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Alexander Bruy' |
| 21 | +__date__ = 'April 2013' |
| 22 | +__copyright__ = '(C) 2013, Alexander Bruy' |
| 23 | + |
| 24 | +# This will get replaced with a git SHA1 when you do a git archive |
| 25 | + |
| 26 | +__revision__ = '$Format:%H$' |
| 27 | + |
| 28 | +import os |
| 29 | + |
| 30 | +from qgis.PyQt.QtCore import Qt |
| 31 | +from qgis.PyQt.QtGui import QFont, QColor, QKeySequence |
| 32 | +from qgis.PyQt.QtWidgets import QShortcut |
| 33 | +from qgis.core import QgsApplication, QgsSettings |
| 34 | + |
| 35 | +from qgis.PyQt.Qsci import QsciScintilla, QsciLexerPython, QsciAPIs |
| 36 | + |
| 37 | + |
| 38 | +class ScriptEdit(QsciScintilla): |
| 39 | + |
| 40 | + def __init__(self, parent=None): |
| 41 | + QsciScintilla.__init__(self, parent) |
| 42 | + |
| 43 | + self.lexer = None |
| 44 | + self.api = None |
| 45 | + |
| 46 | + self.setCommonOptions() |
| 47 | + self.initShortcuts() |
| 48 | + |
| 49 | + def setCommonOptions(self): |
| 50 | + # Enable non-ASCII characters |
| 51 | + self.setUtf8(True) |
| 52 | + |
| 53 | + # Default font |
| 54 | + font = QFont() |
| 55 | + font.setFamily('Courier') |
| 56 | + font.setFixedPitch(True) |
| 57 | + font.setPointSize(20) |
| 58 | + self.setFont(font) |
| 59 | + self.setMarginsFont(font) |
| 60 | + |
| 61 | + self.setBraceMatching(QsciScintilla.SloppyBraceMatch) |
| 62 | + |
| 63 | + self.setWrapMode(QsciScintilla.WrapWord) |
| 64 | + self.setWrapVisualFlags(QsciScintilla.WrapFlagByText, |
| 65 | + QsciScintilla.WrapFlagNone, 4) |
| 66 | + |
| 67 | + self.setSelectionForegroundColor(QColor('#2e3436')) |
| 68 | + self.setSelectionBackgroundColor(QColor('#babdb6')) |
| 69 | + |
| 70 | + # Show line numbers |
| 71 | + self.setMarginWidth(1, '000') |
| 72 | + self.setMarginLineNumbers(1, True) |
| 73 | + self.setMarginsForegroundColor(QColor('#2e3436')) |
| 74 | + self.setMarginsBackgroundColor(QColor('#babdb6')) |
| 75 | + |
| 76 | + # Highlight current line |
| 77 | + self.setCaretLineVisible(True) |
| 78 | + self.setCaretLineBackgroundColor(QColor('#d3d7cf')) |
| 79 | + |
| 80 | + # Folding |
| 81 | + self.setFolding(QsciScintilla.BoxedTreeFoldStyle) |
| 82 | + self.setFoldMarginColors(QColor('#d3d7cf'), QColor('#d3d7cf')) |
| 83 | + |
| 84 | + # Mark column 80 with vertical line |
| 85 | + self.setEdgeMode(QsciScintilla.EdgeLine) |
| 86 | + self.setEdgeColumn(80) |
| 87 | + self.setEdgeColor(QColor('#eeeeec')) |
| 88 | + |
| 89 | + # Indentation |
| 90 | + self.setAutoIndent(True) |
| 91 | + self.setIndentationsUseTabs(False) |
| 92 | + self.setIndentationWidth(4) |
| 93 | + self.setTabIndents(True) |
| 94 | + self.setBackspaceUnindents(True) |
| 95 | + self.setTabWidth(4) |
| 96 | + |
| 97 | + # Autocomletion |
| 98 | + self.setAutoCompletionThreshold(2) |
| 99 | + self.setAutoCompletionSource(QsciScintilla.AcsAPIs) |
| 100 | + |
| 101 | + self.setFonts(10) |
| 102 | + self.initLexer() |
| 103 | + |
| 104 | + def setFonts(self, size): |
| 105 | + # Load font from Python console settings |
| 106 | + settings = QgsSettings() |
| 107 | + fontName = settings.value('pythonConsole/fontfamilytext', 'Monospace') |
| 108 | + fontSize = int(settings.value('pythonConsole/fontsize', size)) |
| 109 | + |
| 110 | + self.defaultFont = QFont(fontName) |
| 111 | + self.defaultFont.setFixedPitch(True) |
| 112 | + self.defaultFont.setPointSize(fontSize) |
| 113 | + self.defaultFont.setStyleHint(QFont.TypeWriter) |
| 114 | + self.defaultFont.setStretch(QFont.SemiCondensed) |
| 115 | + self.defaultFont.setLetterSpacing(QFont.PercentageSpacing, 87.0) |
| 116 | + self.defaultFont.setBold(False) |
| 117 | + |
| 118 | + self.boldFont = QFont(self.defaultFont) |
| 119 | + self.boldFont.setBold(True) |
| 120 | + |
| 121 | + self.italicFont = QFont(self.defaultFont) |
| 122 | + self.italicFont.setItalic(True) |
| 123 | + |
| 124 | + self.setFont(self.defaultFont) |
| 125 | + self.setMarginsFont(self.defaultFont) |
| 126 | + |
| 127 | + def initShortcuts(self): |
| 128 | + (ctrl, shift) = (self.SCMOD_CTRL << 16, self.SCMOD_SHIFT << 16) |
| 129 | + |
| 130 | + # Disable some shortcuts |
| 131 | + self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('D') + ctrl) |
| 132 | + self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('L') + ctrl) |
| 133 | + self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('L') + ctrl + |
| 134 | + shift) |
| 135 | + self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord('T') + ctrl) |
| 136 | + |
| 137 | + # self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord("Z") + ctrl) |
| 138 | + # self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord("Y") + ctrl) |
| 139 | + |
| 140 | + # Use Ctrl+Space for autocompletion |
| 141 | + self.shortcutAutocomplete = QShortcut(QKeySequence(Qt.CTRL + |
| 142 | + Qt.Key_Space), self) |
| 143 | + self.shortcutAutocomplete.setContext(Qt.WidgetShortcut) |
| 144 | + self.shortcutAutocomplete.activated.connect(self.autoComplete) |
| 145 | + |
| 146 | + def autoComplete(self): |
| 147 | + self.autoCompleteFromAll() |
| 148 | + |
| 149 | + def initLexer(self): |
| 150 | + self.lexer = QsciLexerPython() |
| 151 | + |
| 152 | + colorDefault = QColor('#2e3436') |
| 153 | + colorComment = QColor('#c00') |
| 154 | + colorCommentBlock = QColor('#3465a4') |
| 155 | + colorNumber = QColor('#4e9a06') |
| 156 | + colorType = QColor('#4e9a06') |
| 157 | + colorKeyword = QColor('#204a87') |
| 158 | + colorString = QColor('#ce5c00') |
| 159 | + |
| 160 | + self.lexer.setDefaultFont(self.defaultFont) |
| 161 | + self.lexer.setDefaultColor(colorDefault) |
| 162 | + |
| 163 | + self.lexer.setColor(colorComment, 1) |
| 164 | + self.lexer.setColor(colorNumber, 2) |
| 165 | + self.lexer.setColor(colorString, 3) |
| 166 | + self.lexer.setColor(colorString, 4) |
| 167 | + self.lexer.setColor(colorKeyword, 5) |
| 168 | + self.lexer.setColor(colorString, 6) |
| 169 | + self.lexer.setColor(colorString, 7) |
| 170 | + self.lexer.setColor(colorType, 8) |
| 171 | + self.lexer.setColor(colorCommentBlock, 12) |
| 172 | + self.lexer.setColor(colorString, 15) |
| 173 | + |
| 174 | + self.lexer.setFont(self.italicFont, 1) |
| 175 | + self.lexer.setFont(self.boldFont, 5) |
| 176 | + self.lexer.setFont(self.boldFont, 8) |
| 177 | + self.lexer.setFont(self.italicFont, 12) |
| 178 | + |
| 179 | + self.api = QsciAPIs(self.lexer) |
| 180 | + |
| 181 | + settings = QgsSettings() |
| 182 | + useDefaultAPI = bool(settings.value('pythonConsole/preloadAPI', |
| 183 | + True)) |
| 184 | + if useDefaultAPI: |
| 185 | + # Load QGIS API shipped with Python console |
| 186 | + self.api.loadPrepared( |
| 187 | + os.path.join(QgsApplication.pkgDataPath(), |
| 188 | + 'python', 'qsci_apis', 'pyqgis.pap')) |
| 189 | + else: |
| 190 | + # Load user-defined API files |
| 191 | + apiPaths = settings.value('pythonConsole/userAPI', []) |
| 192 | + for path in apiPaths: |
| 193 | + self.api.load(path) |
| 194 | + self.api.prepare() |
| 195 | + self.lexer.setAPIs(self.api) |
| 196 | + |
| 197 | + self.setLexer(self.lexer) |
0 commit comments