|
| 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 | +# This will get replaced with a git SHA1 when you do a git archive |
| 24 | +__revision__ = '$Format:%H$' |
| 25 | + |
| 26 | +from PyQt4.QtCore import * |
| 27 | +from PyQt4.QtGui import * |
| 28 | +from PyQt4.Qsci import * |
| 29 | + |
| 30 | +from qgis.core import * |
| 31 | + |
| 32 | +class ScriptEdit(QsciScintilla): |
| 33 | + |
| 34 | + LEXER_PYTHON = 0 |
| 35 | + LEXER_R = 1 |
| 36 | + |
| 37 | + def __init__(self, parent=None): |
| 38 | + QsciScintilla.__init__(self, parent) |
| 39 | + |
| 40 | + self.lexer = None |
| 41 | + self.api = None |
| 42 | + self.lexerType = -1 |
| 43 | + |
| 44 | + self.setCommonOptions() |
| 45 | + self.initShortcuts() |
| 46 | + |
| 47 | + def setCommonOptions(self): |
| 48 | + # enable non-ASCII characters |
| 49 | + self.setUtf8(True) |
| 50 | + |
| 51 | + # default font |
| 52 | + font = QFont() |
| 53 | + font.setFamily("Courier") |
| 54 | + font.setFixedPitch(True) |
| 55 | + font.setPointSize(10) |
| 56 | + self.setFont(font) |
| 57 | + self.setMarginsFont(font) |
| 58 | + |
| 59 | + self.initLexer() |
| 60 | + |
| 61 | + self.setBraceMatching(QsciScintilla.SloppyBraceMatch) |
| 62 | + |
| 63 | + self.setWrapMode(QsciScintilla.WrapWord) |
| 64 | + self.setWrapVisualFlags(QsciScintilla.WrapFlagByText, QsciScintilla.WrapFlagNone, 4) |
| 65 | + |
| 66 | + self.setSelectionForegroundColor(QColor("#2e3436")) |
| 67 | + self.setSelectionBackgroundColor(QColor("#babdb6")) |
| 68 | + |
| 69 | + # show line numbers |
| 70 | + self.setMarginWidth(1, "000") |
| 71 | + self.setMarginLineNumbers(1, True) |
| 72 | + self.setMarginsForegroundColor(QColor("#2e3436")) |
| 73 | + self.setMarginsBackgroundColor(QColor("#babdb6")) |
| 74 | + |
| 75 | + # highlight current line |
| 76 | + self.setCaretLineVisible(True) |
| 77 | + self.setCaretLineBackgroundColor(QColor("#d3d7cf")) |
| 78 | + |
| 79 | + # folding |
| 80 | + self.setFolding(QsciScintilla.BoxedTreeFoldStyle) |
| 81 | + self.setFoldMarginColors(QColor("#d3d7cf"), QColor("#d3d7cf")) |
| 82 | + |
| 83 | + # mark column 80 with vertical line |
| 84 | + self.setEdgeMode(QsciScintilla.EdgeLine) |
| 85 | + self.setEdgeColumn(80) |
| 86 | + self.setEdgeColor(QColor("#eeeeec")) |
| 87 | + |
| 88 | + # indentation |
| 89 | + self.setAutoIndent(True) |
| 90 | + self.setIndentationsUseTabs(False) |
| 91 | + self.setIndentationWidth(4) |
| 92 | + self.setTabIndents(True) |
| 93 | + self.setBackspaceUnindents(True) |
| 94 | + self.setTabWidth(4) |
| 95 | + |
| 96 | + # autocomletion |
| 97 | + self.setAutoCompletionThreshold(2) |
| 98 | + self.setAutoCompletionSource(QsciScintilla.AcsAPIs) |
| 99 | + |
| 100 | + # load font from Python console settings |
| 101 | + settings = QSettings() |
| 102 | + fontName = settings.value("pythonConsole/fontfamilytext", "Monospace").toString() |
| 103 | + fontSize = settings.value("pythonConsole/fontsize", 10).toInt()[0] |
| 104 | + |
| 105 | + self.defaultFont = QFont(fontName) |
| 106 | + self.defaultFont.setFixedPitch(True) |
| 107 | + self.defaultFont.setPointSize(fontSize) |
| 108 | + self.defaultFont.setStyleHint(QFont.TypeWriter) |
| 109 | + self.defaultFont.setStretch(QFont.SemiCondensed) |
| 110 | + self.defaultFont.setLetterSpacing(QFont.PercentageSpacing, 87.0) |
| 111 | + self.defaultFont.setBold(False) |
| 112 | + |
| 113 | + self.boldFont = QFont(self.defaultFont) |
| 114 | + self.boldFont.setBold(True) |
| 115 | + |
| 116 | + self.italicFont = QFont(self.defaultFont) |
| 117 | + self.italicFont.setItalic(True) |
| 118 | + |
| 119 | + self.setFont(self.defaultFont) |
| 120 | + self.setMarginsFont(self.defaultFont) |
| 121 | + |
| 122 | + def initShortcuts(self): |
| 123 | + ctrl, shift = self.SCMOD_CTRL << 16, self.SCMOD_SHIFT << 16 |
| 124 | + |
| 125 | + # disable some shortcuts |
| 126 | + self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord("D") + ctrl) |
| 127 | + self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord("L") + ctrl) |
| 128 | + self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord("L") + ctrl + shift) |
| 129 | + self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord("T") + ctrl) |
| 130 | + #self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord("Z") + ctrl) |
| 131 | + #self.SendScintilla(QsciScintilla.SCI_CLEARCMDKEY, ord("Y") + ctrl) |
| 132 | + |
| 133 | + # use Ctrl+Space for autocompletion |
| 134 | + self.shortcutAutocomplete = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_Space), self) |
| 135 | + self.shortcutAutocomplete.setContext(Qt.WidgetShortcut) |
| 136 | + self.shortcutAutocomplete.activated.connect(self.autoComplete) |
| 137 | + |
| 138 | + def autoComplete(self): |
| 139 | + self.autoCompleteFromAll() |
| 140 | + |
| 141 | + def setLexerType(self, lexerType): |
| 142 | + self.lexerType = lexerType |
| 143 | + self.initLexer() |
| 144 | + |
| 145 | + def initLexer(self): |
| 146 | + if self.lexerType == self.LEXER_PYTHON: |
| 147 | + self.lexer = QsciLexerPython() |
| 148 | + |
| 149 | + colorDefault = QColor("#2e3436") |
| 150 | + colorComment = QColor("#c00") |
| 151 | + colorCommentBlock = QColor("#3465a4") |
| 152 | + colorNumber = QColor("#4e9a06") |
| 153 | + colorType = QColor("#4e9a06") |
| 154 | + colorKeyword = QColor("#204a87") |
| 155 | + colorString = QColor("#ce5c00") |
| 156 | + |
| 157 | + self.lexer.setDefaultFont(self.defaultFont) |
| 158 | + self.lexer.setDefaultColor(colorDefault) |
| 159 | + |
| 160 | + self.lexer.setColor(colorComment, 1) |
| 161 | + self.lexer.setColor(colorNumber, 2) |
| 162 | + self.lexer.setColor(colorString, 3) |
| 163 | + self.lexer.setColor(colorString, 4) |
| 164 | + self.lexer.setColor(colorKeyword, 5) |
| 165 | + self.lexer.setColor(colorString, 6) |
| 166 | + self.lexer.setColor(colorString, 7) |
| 167 | + self.lexer.setColor(colorType, 8) |
| 168 | + self.lexer.setColor(colorCommentBlock, 12) |
| 169 | + self.lexer.setColor(colorString, 15) |
| 170 | + |
| 171 | + self.lexer.setFont(self.italicFont, 1) |
| 172 | + self.lexer.setFont(self.boldFont, 5) |
| 173 | + self.lexer.setFont(self.boldFont, 8) |
| 174 | + self.lexer.setFont(self.italicFont, 12) |
| 175 | + |
| 176 | + self.api = QsciAPIs(self.lexer) |
| 177 | + |
| 178 | + settings = QSettings() |
| 179 | + useDefaultAPI = settings.value("pythonConsole/preloadAPI", True).toBool() |
| 180 | + if useDefaultAPI: |
| 181 | + # load QGIS API shipped with Python console |
| 182 | + self.api.loadPrepared(QgsApplication.pkgDataPath() + "/python/qsci_apis/pyqgis_master.pap") |
| 183 | + else: |
| 184 | + # load user-defined API files |
| 185 | + apiPaths = settings.value("pythonConsole/userAPI").toStringList() |
| 186 | + for path in apiPaths: |
| 187 | + self.api.load(path) |
| 188 | + self.api.prepare() |
| 189 | + self.lexer.setAPIs(self.api) |
| 190 | + elif self.lexerType == self.LEXER_R: |
| 191 | + # R lexer |
| 192 | + pass |
| 193 | + |
| 194 | + self.setLexer(self.lexer) |
0 commit comments