aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2010-12-21 08:47:18 -0300
committerMarcelo Lira <marcelo.lira@openbossa.org>2010-12-21 08:47:18 -0300
commitedf8c317a800c545705388a34918fc6786ea022f (patch)
treef29fc46ece67ed6a40ce1c51998145068116fab4 /examples
parent2f08ad954cca70ff7388240321d34e0162d1f3eb (diff)
highlightedtextedit example fixed.
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/designer/plugins/widgets/highlightedtextedit.py84
1 files changed, 42 insertions, 42 deletions
diff --git a/examples/designer/plugins/widgets/highlightedtextedit.py b/examples/designer/plugins/widgets/highlightedtextedit.py
index d1666ec..10d2f90 100755
--- a/examples/designer/plugins/widgets/highlightedtextedit.py
+++ b/examples/designer/plugins/widgets/highlightedtextedit.py
@@ -29,46 +29,46 @@ from PySide 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):
-
+
QtGui.QTextEdit.__init__(self, 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("QString", getCode, setCode)
-
+
+ code = QtCore.Property("QString", 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("QFont", getDisplayFont, setDisplayFont)
+
+ displayFont = QtCore.Property("QFont", getDisplayFont, setDisplayFont)
class PythonHighlighter(QtGui.QSyntaxHighlighter):
@@ -81,37 +81,37 @@ class PythonHighlighter(QtGui.QSyntaxHighlighter):
"continue", "exec", "import", "pass", "yield",
"def", "finally", "in", "print"
)
-
+
def __init__(self, document, base_format):
-
+
QtGui.QSyntaxHighlighter.__init__(self, 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():
+
+ if not text.strip():
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:
@@ -121,27 +121,27 @@ class PythonHighlighter(QtGui.QSyntaxHighlighter):
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 = text.indexOf(expression, start)
while index >= start and index < finish:
length = expression.matchedLength()
self.setFormat(index, min(length, finish - index), format)
index = text.indexOf(expression, 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)
@@ -164,28 +164,28 @@ class PythonHighlighter(QtGui.QSyntaxHighlighter):
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)