3 from PyQt4
.QtCore
import Qt
4 from PyQt4
.QtGui
import QFont
5 from PyQt4
.QtGui
import QSyntaxHighlighter
6 from PyQt4
.QtGui
import QTextCharFormat
13 class GitSyntaxHighlighter (QSyntaxHighlighter
):
15 def __init__ (self
, doc
):
16 QSyntaxHighlighter
.__init
__ (self
, doc
)
18 begin
= self
._mkformat
(QFont
.Bold
, Qt
.cyan
)
19 addition
= self
._mkformat
(QFont
.Bold
, Qt
.green
)
20 removal
= self
._mkformat
(QFont
.Bold
, Qt
.red
)
21 message
= self
._mkformat
(QFont
.Bold
, Qt
.yellow
, Qt
.black
)
24 ( re
.compile ('^(@@|\+\+\+|---)'), begin
),
25 ( re
.compile ('^\+'), addition
),
26 ( re
.compile ('^-'), removal
),
27 ( re
.compile ('^:'), message
),
30 def getFormat (self
, line
):
31 for regex
, rule
in self
._rules
:
32 if regex
.match (line
):
36 def highlightBlock (self
, qstr
):
37 ascii
= qstr
.toAscii().data()
39 format
= self
.getFormat (ascii
)
40 if format
: self
.setFormat (0, len (ascii
), format
)
42 def _mkformat (self
, weight
, color
, bgcolor
=None):
43 format
= QTextCharFormat()
44 format
.setFontWeight (weight
)
45 format
.setForeground (color
)
46 if bgcolor
: format
.setBackground (bgcolor
)
50 if __name__
== '__main__':
52 from PyQt4
import QtCore
, QtGui
54 class SyntaxTestDialog(QtGui
.QDialog
):
55 def __init__ (self
, parent
):
56 QtGui
.QDialog
.__init
__ (self
, parent
)
59 def setupUi(self
, CommandDialog
):
60 CommandDialog
.resize(QtCore
.QSize(QtCore
.QRect(0,0,720,512).size()).expandedTo(CommandDialog
.minimumSizeHint()))
62 self
.vboxlayout
= QtGui
.QVBoxLayout(CommandDialog
)
63 self
.vboxlayout
.setObjectName("vboxlayout")
65 self
.commandText
= QtGui
.QTextEdit(CommandDialog
)
68 font
.setFamily("Monospace")
70 self
.commandText
.setFont(font
)
71 self
.commandText
.setAcceptDrops(False)
72 #self.commandText.setReadOnly(True)
73 self
.vboxlayout
.addWidget(self
.commandText
)
75 GitSyntaxHighlighter (self
.commandText
.document())
78 app
= QtGui
.QApplication (sys
.argv
)
79 dialog
= SyntaxTestDialog (app
.activeWindow())