1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#!/usr/bin/env python
"""
highlightedtextedit.py
A PyQt custom widget example for Qt Designer.
Copyright (C) 2006 David Boddie <david@boddie.org.uk>
Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
from PyQt4 import QtCore, QtGui
class HighlightedTextEdit(QtGui.QTextEdit):
"""HighlightedTextEdit(QtGui.QTextEdit)
Provides a custom text editor with a simple built-in Python syntax
highlighter.
"""
def __init__(self, parent=None):
super(HighlightedTextEdit, self).__init__(parent)
self.setFrameShape(QtGui.QFrame.Box)
self.setFrameShadow(QtGui.QFrame.Plain)
char_format = QtGui.QTextCharFormat()
char_format.setFont(self.font())
self.highlighter = PythonHighlighter(self.document(), char_format)
# The code property is implemented with the getCode() and setCode()
# methods, and contains the plain text shown in the editor.
def getCode(self):
self._code = self.toPlainText()
return self._code
def setCode(self, text):
self.setPlainText(text)
code = QtCore.pyqtProperty(str, getCode, setCode)
# The displayFont property is implemented with the getDisplayFont() and
# setDisplayFont() methods, and contains the font used to display the
# text in the editor.
def getDisplayFont(self):
return QtGui.QWidget.font(self)
def setDisplayFont(self, font):
QtGui.QWidget.setFont(self, font)
self.highlighter.updateHighlighter(font)
self.update()
displayFont = QtCore.pyqtProperty(QtGui.QFont, getDisplayFont, setDisplayFont)
class PythonHighlighter(QtGui.QSyntaxHighlighter):
keywords = (
"and", "del", "for", "is", "raise",
"assert", "elif", "from", "lambda", "return",
"break", "else", "global", "not", "try",
"class", "except", "if", "or", "while",
"continue", "exec", "import", "pass", "yield",
"def", "finally", "in", "print"
)
def __init__(self, document, base_format):
super(PythonHighlighter, self).__init__(document)
self.base_format = base_format
self.document = document
self.updateHighlighter(base_format.font())
def highlightBlock(self, text):
self.setCurrentBlockState(0)
if text.trimmed().isEmpty():
self.setFormat(0, len(text), self.empty_format)
return
self.setFormat(0, len(text), self.base_format)
startIndex = 0
if self.previousBlockState() != 1:
startIndex = text.indexOf(self.multiLineStringBegin)
if startIndex > -1:
self.highlightRules(text, 0, startIndex)
else:
self.highlightRules(text, 0, len(text))
while startIndex >= 0:
endIndex = text.indexOf(self.multiLineStringEnd,
startIndex + len(self.multiLineStringBegin.pattern()))
if endIndex == -1:
self.setCurrentBlockState(1)
commentLength = text.length() - startIndex
else:
commentLength = endIndex - startIndex + \
self.multiLineStringEnd.matchedLength()
self.highlightRules(text, endIndex, len(text))
self.setFormat(startIndex, commentLength, self.multiLineStringFormat)
startIndex = text.indexOf(self.multiLineStringBegin,
startIndex + commentLength)
def highlightRules(self, text, start, finish):
for expression, format in self.rules:
index = expression.indexIn(text, start)
while index >= start and index < finish:
length = expression.matchedLength()
self.setFormat(index, min(length, finish - index), format)
index = expression.indexIn(text, index + length)
def updateFonts(self, font):
self.base_format.setFont(font)
self.empty_format = QtGui.QTextCharFormat(self.base_format)
self.empty_format.setFontPointSize(font.pointSize()/4.0)
self.keywordFormat = QtGui.QTextCharFormat(self.base_format)
self.keywordFormat.setForeground(QtCore.Qt.darkBlue)
self.keywordFormat.setFontWeight(QtGui.QFont.Bold)
self.callableFormat = QtGui.QTextCharFormat(self.base_format)
self.callableFormat.setForeground(QtCore.Qt.darkBlue)
self.magicFormat = QtGui.QTextCharFormat(self.base_format)
self.magicFormat.setForeground(QtGui.QColor(224,128,0))
self.qtFormat = QtGui.QTextCharFormat(self.base_format)
self.qtFormat.setForeground(QtCore.Qt.blue)
self.qtFormat.setFontWeight(QtGui.QFont.Bold)
self.selfFormat = QtGui.QTextCharFormat(self.base_format)
self.selfFormat.setForeground(QtCore.Qt.red)
#self.selfFormat.setFontItalic(True)
self.singleLineCommentFormat = QtGui.QTextCharFormat(self.base_format)
self.singleLineCommentFormat.setForeground(QtCore.Qt.darkGreen)
self.multiLineStringFormat = QtGui.QTextCharFormat(self.base_format)
self.multiLineStringFormat.setBackground(
QtGui.QBrush(QtGui.QColor(127,127,255)))
self.quotationFormat1 = QtGui.QTextCharFormat(self.base_format)
self.quotationFormat1.setForeground(QtCore.Qt.blue)
self.quotationFormat2 = QtGui.QTextCharFormat(self.base_format)
self.quotationFormat2.setForeground(QtCore.Qt.blue)
def updateRules(self):
self.rules = []
self.rules += map(lambda s: (QtCore.QRegExp(r"\b"+s+r"\b"),
self.keywordFormat), self.keywords)
self.rules.append((QtCore.QRegExp(r"\b[A-Za-z_]+\(.*\)"), self.callableFormat))
self.rules.append((QtCore.QRegExp(r"\b__[a-z]+__\b"), self.magicFormat))
self.rules.append((QtCore.QRegExp(r"\bself\b"), self.selfFormat))
self.rules.append((QtCore.QRegExp(r"\bQ([A-Z][a-z]*)+\b"), self.qtFormat))
self.rules.append((QtCore.QRegExp(r"#[^\n]*"), self.singleLineCommentFormat))
self.multiLineStringBegin = QtCore.QRegExp(r'\"\"\"')
self.multiLineStringEnd = QtCore.QRegExp(r'\"\"\"')
self.rules.append((QtCore.QRegExp(r'\"[^\n]*\"'), self.quotationFormat1))
self.rules.append((QtCore.QRegExp(r"'[^\n]*'"), self.quotationFormat2))
def updateHighlighter(self, font):
self.updateFonts(font)
self.updateRules()
self.setDocument(self.document)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
widget = HighlightedTextEdit()
widget.show()
sys.exit(app.exec_())
|