Skip to content

Instantly share code, notes, and snippets.

@oglops
Created January 25, 2016 08:58
Show Gist options
  • Save oglops/219d3583540787e69e30 to your computer and use it in GitHub Desktop.
Save oglops/219d3583540787e69e30 to your computer and use it in GitHub Desktop.
PyQt4 port of the richtext/syntaxhighlighter example from Qt v4.x
#!/usr/bin/env python
"""PyQt4 port of the richtext/syntaxhighlighter example from Qt v4.x"""
from PyQt4 import QtCore, QtGui
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupFileMenu()
self.setupHelpMenu()
self.setupEditor()
self.setCentralWidget(self.editor)
self.setWindowTitle("Syntax Highlighter")
def about(self):
QtGui.QMessageBox.about(self, "About Syntax Highlighter",
"<p>The <b>Syntax Highlighter</b> example shows how to " \
"perform simple syntax highlighting by subclassing the " \
"QSyntaxHighlighter class and describing highlighting " \
"rules using regular expressions.</p>")
def newFile(self):
self.editor.clear()
def openFile(self, path=None):
if not path:
path = QtGui.QFileDialog.getOpenFileName(self, "Open File",
'', "C++ Files (*.cpp *.h)")
if path:
inFile = QtCore.QFile(path)
if inFile.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text):
text = inFile.readAll()
try:
# Python v3.
text = str(text, encoding='ascii')
except TypeError:
# Python v2.
text = str(text)
self.editor.setPlainText(text)
def setupEditor(self):
font = QtGui.QFont()
font.setFamily('Courier')
font.setFixedPitch(True)
font.setPointSize(10)
self.editor = QtGui.QTextEdit()
self.editor.setFont(font)
self.highlighter = Highlighter(self.editor.document())
def setupFileMenu(self):
fileMenu = QtGui.QMenu("&File", self)
self.menuBar().addMenu(fileMenu)
fileMenu.addAction("&New...", self.newFile, "Ctrl+N")
fileMenu.addAction("&Open...", self.openFile, "Ctrl+O")
fileMenu.addAction("E&xit", QtGui.qApp.quit, "Ctrl+Q")
def setupHelpMenu(self):
helpMenu = QtGui.QMenu("&Help", self)
self.menuBar().addMenu(helpMenu)
helpMenu.addAction("&About", self.about)
helpMenu.addAction("About &Qt", QtGui.qApp.aboutQt)
class Highlighter(QtGui.QSyntaxHighlighter):
def __init__(self, parent=None):
super(Highlighter, self).__init__(parent)
keywordFormat = QtGui.QTextCharFormat()
keywordFormat.setForeground(QtCore.Qt.darkBlue)
keywordFormat.setFontWeight(QtGui.QFont.Bold)
keywordPatterns = ["\\bchar\\b", "\\bclass\\b", "\\bconst\\b",
"\\bdouble\\b", "\\benum\\b", "\\bexplicit\\b", "\\bfriend\\b",
"\\binline\\b", "\\bint\\b", "\\blong\\b", "\\bnamespace\\b",
"\\boperator\\b", "\\bprivate\\b", "\\bprotected\\b",
"\\bpublic\\b", "\\bshort\\b", "\\bsignals\\b", "\\bsigned\\b",
"\\bslots\\b", "\\bstatic\\b", "\\bstruct\\b",
"\\btemplate\\b", "\\btypedef\\b", "\\btypename\\b",
"\\bunion\\b", "\\bunsigned\\b", "\\bvirtual\\b", "\\bvoid\\b",
"\\bvolatile\\b"]
self.highlightingRules = [(QtCore.QRegExp(pattern), keywordFormat)
for pattern in keywordPatterns]
classFormat = QtGui.QTextCharFormat()
classFormat.setFontWeight(QtGui.QFont.Bold)
classFormat.setForeground(QtCore.Qt.darkMagenta)
self.highlightingRules.append((QtCore.QRegExp("\\bQ[A-Za-z]+\\b"),
classFormat))
singleLineCommentFormat = QtGui.QTextCharFormat()
singleLineCommentFormat.setForeground(QtCore.Qt.red)
self.highlightingRules.append((QtCore.QRegExp("//[^\n]*"),
singleLineCommentFormat))
self.multiLineCommentFormat = QtGui.QTextCharFormat()
self.multiLineCommentFormat.setForeground(QtCore.Qt.red)
quotationFormat = QtGui.QTextCharFormat()
quotationFormat.setForeground(QtCore.Qt.darkGreen)
self.highlightingRules.append((QtCore.QRegExp("\".*\""),
quotationFormat))
functionFormat = QtGui.QTextCharFormat()
functionFormat.setFontItalic(True)
functionFormat.setForeground(QtCore.Qt.blue)
self.highlightingRules.append((QtCore.QRegExp("\\b[A-Za-z0-9_]+(?=\\()"),
functionFormat))
self.commentStartExpression = QtCore.QRegExp("/\\*")
self.commentEndExpression = QtCore.QRegExp("\\*/")
def highlightBlock(self, text):
for pattern, format in self.highlightingRules:
expression = QtCore.QRegExp(pattern)
index = expression.indexIn(text)
while index >= 0:
length = expression.matchedLength()
self.setFormat(index, length, format)
index = expression.indexIn(text, index + length)
self.setCurrentBlockState(0)
startIndex = 0
if self.previousBlockState() != 1:
startIndex = self.commentStartExpression.indexIn(text)
while startIndex >= 0:
endIndex = self.commentEndExpression.indexIn(text, startIndex)
if endIndex == -1:
self.setCurrentBlockState(1)
commentLength = len(text) - startIndex
else:
commentLength = endIndex - startIndex + self.commentEndExpression.matchedLength()
self.setFormat(startIndex, commentLength,
self.multiLineCommentFormat)
startIndex = self.commentStartExpression.indexIn(text,
startIndex + commentLength);
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.resize(640, 512)
window.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment